mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-19 10:22:04 +08:00
Implement the new _internal package structure for AstrBot runtime: - Add AstrbotOrchestrator with LSP, MCP, ACP, ABP protocol clients - Add AstrbotGateway server with WebSocket support - Add comprehensive test suite for runtime module - Add tools base module for MCP tools Implements bootstrap function using anyio task groups for concurrent protocol client initialization.
46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
"""MCP tool wrapper."""
|
|
|
|
from datetime import timedelta
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
try:
|
|
import mcp
|
|
except (ModuleNotFoundError, ImportError):
|
|
mcp = None # type: ignore
|
|
|
|
from astrbot._internal.tools.base import FunctionTool
|
|
|
|
if TYPE_CHECKING:
|
|
from astrbot._internal.protocols.mcp.client import McpClient
|
|
|
|
|
|
class MCPTool(FunctionTool):
|
|
"""A function tool that calls an MCP service."""
|
|
|
|
def __init__(
|
|
self,
|
|
mcp_tool: "mcp.types.Tool", # type: ignore[name-defined]
|
|
mcp_client: "McpClient",
|
|
mcp_server_name: str,
|
|
**kwargs: Any,
|
|
) -> None:
|
|
super().__init__(
|
|
name=mcp_tool.name,
|
|
description=mcp_tool.description or "",
|
|
parameters=mcp_tool.inputSchema,
|
|
)
|
|
self.mcp_tool = mcp_tool
|
|
self.mcp_client = mcp_client
|
|
self.mcp_server_name = mcp_server_name
|
|
self.source = "mcp"
|
|
|
|
async def call(self, **kwargs: Any) -> Any:
|
|
"""Call the MCP tool with the given arguments."""
|
|
# Note: For actual usage, context.tool_call_timeout is needed
|
|
# but for simplicity we use a default timeout here
|
|
return await self.mcp_client.call_tool_with_reconnect(
|
|
tool_name=self.mcp_tool.name,
|
|
arguments=kwargs,
|
|
read_timeout_seconds=timedelta(seconds=60),
|
|
)
|