update mcp name

This commit is contained in:
liangxinbing 2025-03-20 02:03:20 +08:00
parent 8fca2ff1b7
commit 74f438bde3
2 changed files with 15 additions and 14 deletions

View File

@ -7,7 +7,7 @@ from app.logger import logger
from app.prompt.mcp import MULTIMEDIA_RESPONSE_PROMPT, NEXT_STEP_PROMPT, SYSTEM_PROMPT from app.prompt.mcp import MULTIMEDIA_RESPONSE_PROMPT, NEXT_STEP_PROMPT, SYSTEM_PROMPT
from app.schema import AgentState, Message from app.schema import AgentState, Message
from app.tool.base import ToolResult from app.tool.base import ToolResult
from app.tool.mcp import MCP from app.tool.mcp import MCPClients
class MCPAgent(ToolCallAgent): class MCPAgent(ToolCallAgent):
@ -24,8 +24,8 @@ class MCPAgent(ToolCallAgent):
next_step_prompt: str = NEXT_STEP_PROMPT next_step_prompt: str = NEXT_STEP_PROMPT
# Initialize MCP tool collection # Initialize MCP tool collection
mcp_tools: MCP = Field(default_factory=MCP) mcp_tools: MCPClients = Field(default_factory=MCPClients)
available_tools: MCP = None # Will be set in initialize() available_tools: MCPClients = None # Will be set in initialize()
max_steps: int = 20 max_steps: int = 20
connection_type: str = "stdio" # "stdio" or "sse" connection_type: str = "stdio" # "stdio" or "sse"
@ -141,7 +141,7 @@ class MCPAgent(ToolCallAgent):
# Refresh tools periodically # Refresh tools periodically
if self.current_step % self._refresh_tools_interval == 0: if self.current_step % self._refresh_tools_interval == 0:
added, removed = await self._refresh_tools() await self._refresh_tools()
# All tools removed indicates shutdown # All tools removed indicates shutdown
if not self.mcp_tools.tool_map: if not self.mcp_tools.tool_map:
logger.info("MCP service has shut down, ending interaction") logger.info("MCP service has shut down, ending interaction")

View File

@ -1,5 +1,5 @@
from contextlib import AsyncExitStack from contextlib import AsyncExitStack
from typing import Any, Dict, List, Optional from typing import List, Optional
from mcp import ClientSession, StdioServerParameters from mcp import ClientSession, StdioServerParameters
from mcp.client.sse import sse_client from mcp.client.sse import sse_client
@ -11,14 +11,13 @@ from app.tool.base import BaseTool, ToolResult
from app.tool.tool_collection import ToolCollection from app.tool.tool_collection import ToolCollection
class MCPServerTool(BaseTool): class MCPClientTool(BaseTool):
"""Represents a tool available on the MCP server.""" """Represents a tool proxy that can be called on the MCP server from the client side."""
schema: Dict[str, Any] = None
session: Optional[ClientSession] = None session: Optional[ClientSession] = None
async def execute(self, **kwargs) -> ToolResult: async def execute(self, **kwargs) -> ToolResult:
"""Execute the tool on the MCP server.""" """Execute the tool by making a remote call to the MCP server."""
if not self.session: if not self.session:
return ToolResult(error="Not connected to MCP server") return ToolResult(error="Not connected to MCP server")
@ -32,12 +31,14 @@ class MCPServerTool(BaseTool):
return ToolResult(error=f"Error executing tool: {str(e)}") return ToolResult(error=f"Error executing tool: {str(e)}")
class MCP(ToolCollection): class MCPClients(ToolCollection):
"""AN MCP tool collection that connects to an MCP server and executes commands.""" """
A collection of tools that connects to an MCP server and manages available tools through the Model Context Protocol.
"""
session: Optional[ClientSession] = None session: Optional[ClientSession] = None
exit_stack: AsyncExitStack = None exit_stack: AsyncExitStack = None
description: str = "MCP tools for server interaction" description: str = "MCP client tools for server interaction"
def __init__(self): def __init__(self):
super().__init__() # Initialize with empty tools list super().__init__() # Initialize with empty tools list
@ -91,10 +92,10 @@ class MCP(ToolCollection):
# Create proper tool objects for each server tool # Create proper tool objects for each server tool
for tool in response.tools: for tool in response.tools:
server_tool = MCPServerTool( server_tool = MCPClientTool(
name=tool.name, name=tool.name,
description=tool.description, description=tool.description,
schema=tool.inputSchema, parameters=tool.inputSchema,
session=self.session, session=self.session,
) )
self.tool_map[tool.name] = server_tool self.tool_map[tool.name] = server_tool