Files
xiaozhi-esp32-server/main/xiaozhi-server/core/mcp/manager.py
T

132 lines
4.4 KiB
Python
Raw Normal View History

2025-03-20 08:59:45 +08:00
"""MCP服务管理器"""
2025-04-12 17:36:04 +08:00
2025-03-20 18:20:09 +08:00
import os, json
2025-03-20 08:59:45 +08:00
from typing import Dict, Any, List
from .MCPClient import MCPClient
from config.logger import setup_logging
2025-04-12 17:36:04 +08:00
from plugins_func.register import register_function, ToolType
from config.config_loader import get_project_dir
2025-03-20 08:59:45 +08:00
TAG = __name__
2025-04-12 17:36:04 +08:00
2025-03-20 08:59:45 +08:00
class MCPManager:
"""管理多个MCP服务的集中管理器"""
2025-04-12 17:36:04 +08:00
def __init__(self, conn) -> None:
2025-03-20 08:59:45 +08:00
"""
初始化MCP管理器
"""
self.conn = conn
self.logger = setup_logging()
2025-04-12 17:36:04 +08:00
self.config_path = get_project_dir() + "data/.mcp_server_settings.json"
2025-03-20 18:20:09 +08:00
if os.path.exists(self.config_path) == False:
self.config_path = ""
2025-04-12 17:36:04 +08:00
self.logger.bind(tag=TAG).warning(
f"请检查mcp服务配置文件:data/.mcp_server_settings.json"
)
2025-03-20 18:20:09 +08:00
self.client: Dict[str, MCPClient] = {}
2025-03-20 08:59:45 +08:00
self.tools = []
def load_config(self) -> Dict[str, Any]:
"""加载MCP服务配置
Returns:
Dict[str, Any]: 服务配置字典
"""
2025-03-20 18:20:09 +08:00
if len(self.config_path) == 0:
return {}
2025-04-12 17:36:04 +08:00
2025-03-20 08:59:45 +08:00
try:
2025-04-12 17:36:04 +08:00
with open(self.config_path, "r", encoding="utf-8") as f:
2025-03-20 08:59:45 +08:00
config = json.load(f)
2025-04-12 17:36:04 +08:00
return config.get("mcpServers", {})
2025-03-20 08:59:45 +08:00
except Exception as e:
2025-04-12 17:36:04 +08:00
self.logger.bind(tag=TAG).error(
f"Error loading MCP config from {self.config_path}: {e}"
)
2025-03-20 08:59:45 +08:00
return {}
async def initialize_servers(self) -> None:
"""初始化所有MCP服务"""
config = self.load_config()
for name, srv_config in config.items():
if not srv_config.get("command"):
2025-04-12 17:36:04 +08:00
self.logger.bind(tag=TAG).warning(
f"Skipping server {name}: command not specified"
)
2025-03-20 08:59:45 +08:00
continue
try:
client = MCPClient(srv_config)
await client.initialize()
self.client[name] = client
self.logger.bind(tag=TAG).info(f"Initialized MCP client: {name}")
client_tools = client.get_available_tools()
self.tools.extend(client_tools)
for tool in client_tools:
2025-04-12 17:36:04 +08:00
func_name = "mcp_" + tool["function"]["name"]
register_function(func_name, tool, ToolType.MCP_CLIENT)(
self.execute_tool
)
self.conn.func_handler.function_registry.register_function(
func_name
)
2025-03-20 08:59:45 +08:00
except Exception as e:
2025-04-12 17:36:04 +08:00
self.logger.bind(tag=TAG).error(
f"Failed to initialize MCP server {name}: {e}"
)
2025-03-24 10:28:48 +08:00
self.conn.func_handler.upload_functions_desc()
2025-03-20 08:59:45 +08:00
def get_all_tools(self) -> List[Dict[str, Any]]:
"""获取所有服务的工具function定义
Returns:
List[Dict[str, Any]]: 所有工具的function定义列表
"""
return self.tools
def is_mcp_tool(self, tool_name: str) -> bool:
"""检查是否是MCP工具
Args:
tool_name: 工具名称
Returns:
bool: 是否是MCP工具
"""
for tool in self.tools:
2025-04-12 17:36:04 +08:00
if (
tool.get("function") != None
and tool["function"].get("name") == tool_name
):
2025-03-20 08:59:45 +08:00
return True
return False
async def execute_tool(self, tool_name: str, arguments: Dict[str, Any]) -> Any:
"""执行工具调用
Args:
tool_name: 工具名称
2025-04-12 17:36:04 +08:00
arguments: 工具参数
2025-03-20 08:59:45 +08:00
Returns:
Any: 工具执行结果
Raises:
ValueError: 工具未找到时抛出
"""
2025-04-12 17:36:04 +08:00
self.logger.bind(tag=TAG).info(
f"Executing tool {tool_name} with arguments: {arguments}"
)
2025-03-20 08:59:45 +08:00
for client in self.client.values():
2025-03-20 11:52:37 +08:00
if client.has_tool(tool_name):
2025-03-20 08:59:45 +08:00
return await client.call_tool(tool_name, arguments)
2025-04-12 17:36:04 +08:00
2025-03-20 08:59:45 +08:00
raise ValueError(f"Tool {tool_name} not found in any MCP server")
async def cleanup_all(self) -> None:
for name, client in self.client.items():
try:
await client.cleanup()
self.logger.bind(tag=TAG).info(f"Cleaned up MCP client: {name}")
except Exception as e:
2025-04-12 17:36:04 +08:00
self.logger.bind(tag=TAG).error(
f"Error cleaning up MCP client {name}: {e}"
)
2025-03-20 08:59:45 +08:00
self.client.clear()