Enhance AIClient and MCPServer to support tool registration with source tracking. Added logging for tool calls and improved error handling. Introduced methods for embedding token limit extraction and budget application in OpenAIModel. Added tests for MCP tool registration and execution.
This commit is contained in:
@@ -324,6 +324,43 @@ class MessageHandler:
|
||||
description=f"技能工具: {full_tool_name}",
|
||||
parameters={"type": "object", "properties": {}},
|
||||
function=tool_func,
|
||||
source="skills",
|
||||
)
|
||||
count += 1
|
||||
|
||||
return count
|
||||
|
||||
async def _register_mcp_tools(self) -> int:
|
||||
if not self.mcp_manager or not self.ai_client:
|
||||
return 0
|
||||
|
||||
tools = await self.mcp_manager.get_all_tools_for_ai()
|
||||
count = 0
|
||||
|
||||
for item in tools:
|
||||
function_info = item.get("function") if isinstance(item, dict) else None
|
||||
if not isinstance(function_info, dict):
|
||||
continue
|
||||
|
||||
full_tool_name = function_info.get("name")
|
||||
if not full_tool_name:
|
||||
continue
|
||||
|
||||
parameters = function_info.get("parameters")
|
||||
if not isinstance(parameters, dict):
|
||||
parameters = {"type": "object", "properties": {}}
|
||||
|
||||
async def _mcp_proxy(_full_tool_name=full_tool_name, **kwargs):
|
||||
if not self.mcp_manager:
|
||||
raise RuntimeError("MCP manager not initialized")
|
||||
return await self.mcp_manager.execute_tool(_full_tool_name, kwargs)
|
||||
|
||||
self.ai_client.register_tool(
|
||||
name=full_tool_name,
|
||||
description=f"MCP工具: {full_tool_name}",
|
||||
parameters=parameters,
|
||||
function=_mcp_proxy,
|
||||
source="mcp",
|
||||
)
|
||||
count += 1
|
||||
|
||||
@@ -439,6 +476,8 @@ class MessageHandler:
|
||||
self.mcp_manager = MCPManager(Path("config/mcp.json"))
|
||||
fs_server = FileSystemMCPServer(root_path=Path("data"))
|
||||
await self.mcp_manager.register_server(fs_server)
|
||||
mcp_tool_count = await self._register_mcp_tools()
|
||||
logger.info(f"MCP 工具注册完成: {mcp_tool_count} tools")
|
||||
except Exception as exc:
|
||||
logger.warning(f"MCP 初始化失败: {exc}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user