|
| 1 | +import json |
| 2 | +import urllib.parse |
| 3 | +import asyncio |
| 4 | +import threading |
| 5 | +from typing import Optional, Union, List, Dict |
| 6 | +from contextlib import AsyncExitStack |
| 7 | + |
| 8 | +from mcp import ClientSession, StdioServerParameters |
| 9 | +from mcp.client.stdio import stdio_client |
| 10 | + |
| 11 | +from qwen_agent.log import logger |
| 12 | +from qwen_agent.tools.base import BaseTool, register_tool |
| 13 | + |
| 14 | +from dotenv import load_dotenv |
| 15 | + |
| 16 | +class MCPManager: |
| 17 | + _instance = None # Private class variable to store the unique instance |
| 18 | + |
| 19 | + def __new__(cls, *args, **kwargs): |
| 20 | + if cls._instance is None: |
| 21 | + cls._instance = super(MCPManager, cls).__new__(cls, *args, **kwargs) |
| 22 | + cls._instance.__init__() |
| 23 | + return cls._instance |
| 24 | + |
| 25 | + def __init__(self): |
| 26 | + if not hasattr(self, 'clients'): |
| 27 | + """Set a new event loop in a separate thread""" |
| 28 | + load_dotenv() # Load environment variables from .env file |
| 29 | + self.clients: dict = {} |
| 30 | + self.exit_stack = AsyncExitStack() |
| 31 | + self.loop = asyncio.new_event_loop() |
| 32 | + self.loop_thread = threading.Thread(target=self.start_loop, daemon=True) |
| 33 | + self.loop_thread.start() |
| 34 | + |
| 35 | + def start_loop(self): |
| 36 | + asyncio.set_event_loop(self.loop) |
| 37 | + self.loop.run_forever() |
| 38 | + |
| 39 | + def is_valid_mcp_servers(self, config: dict): |
| 40 | + """Example of mcp servers configuration: |
| 41 | + { |
| 42 | + "mcpServers": { |
| 43 | + "memory": { |
| 44 | + "command": "npx", |
| 45 | + "args": ["-y", "@modelcontextprotocol/server-memory"] |
| 46 | + }, |
| 47 | + "filesystem": { |
| 48 | + "command": "npx", |
| 49 | + "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/allowed/files"] |
| 50 | + }, |
| 51 | + "github": { |
| 52 | + "command": "npx", |
| 53 | + "args": ["-y", "@modelcontextprotocol/server-github"], |
| 54 | + "env": { |
| 55 | + "GITHUB_PERSONAL_ACCESS_TOKEN": "<YOUR_TOKEN>" |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + """ |
| 61 | + |
| 62 | + # Check if the top-level key "mcpServers" exists and its value is a dictionary |
| 63 | + if not isinstance(config, dict) or 'mcpServers' not in config or not isinstance(config['mcpServers'], dict): |
| 64 | + return False |
| 65 | + mcp_servers = config['mcpServers'] |
| 66 | + # Check each sub-item under "mcpServers" |
| 67 | + for key in mcp_servers: |
| 68 | + server = mcp_servers[key] |
| 69 | + # Each sub-item must be a dictionary and contain the keys "command" and "args" |
| 70 | + if not isinstance(server, dict) or 'command' not in server or 'args' not in server: |
| 71 | + return False |
| 72 | + # "command" must be a string |
| 73 | + if not isinstance(server['command'], str): |
| 74 | + return False |
| 75 | + # "args" must be a list |
| 76 | + if not isinstance(server['args'], list): |
| 77 | + return False |
| 78 | + # If the "env" key exists, it must be a dictionary |
| 79 | + if 'env' in server and not isinstance(server['env'], dict): |
| 80 | + return False |
| 81 | + return True |
| 82 | + |
| 83 | + def initConfig(self, config: Dict): |
| 84 | + logger.info(f'Initialize from config {config}. ') |
| 85 | + if not self.is_valid_mcp_servers(config): |
| 86 | + raise ValueError('Config format error') |
| 87 | + # Submit coroutine to the event loop and wait for the result |
| 88 | + future = asyncio.run_coroutine_threadsafe(self.init_config_async(config), self.loop) |
| 89 | + try: |
| 90 | + result = future.result() # You can specify a timeout if desired |
| 91 | + return result |
| 92 | + except Exception as e: |
| 93 | + logger.info(f"Error executing function: {e}") |
| 94 | + return None |
| 95 | + |
| 96 | + async def init_config_async(self, config: Dict): |
| 97 | + tools : list = [] |
| 98 | + mcp_servers = config['mcpServers'] |
| 99 | + for server_name in mcp_servers: |
| 100 | + client = MCPClient() |
| 101 | + server = mcp_servers[server_name] |
| 102 | + await client.connection_server(self.exit_stack, server) # Attempt to connect to the server |
| 103 | + self.clients[server_name] = client # Add to clients dict after successful connection |
| 104 | + for tool in client.tools: |
| 105 | + """MCP tool example: |
| 106 | + { |
| 107 | + "name": "read_query", |
| 108 | + "description": "Execute a SELECT query on the SQLite database", |
| 109 | + "inputSchema": { |
| 110 | + "type": "object", |
| 111 | + "properties": { |
| 112 | + "query": { |
| 113 | + "type": "string", |
| 114 | + "description": "SELECT SQL query to execute" |
| 115 | + } |
| 116 | + }, |
| 117 | + "required": ["query"] |
| 118 | + } |
| 119 | + """ |
| 120 | + parameters = tool.inputSchema |
| 121 | + # The required field in inputSchema may be empty and needs to be initialized. |
| 122 | + if 'required' not in parameters: |
| 123 | + parameters['required'] = [] |
| 124 | + register_name = server_name + "-" + tool.name |
| 125 | + agent_tool = self.create_tool_class(register_name, server_name, tool.name, tool.description, parameters) |
| 126 | + tools.append(agent_tool) |
| 127 | + return tools |
| 128 | + |
| 129 | + def create_tool_class(self, register_name, server_name, tool_name, tool_desc, tool_parameters): |
| 130 | + @register_tool(register_name) |
| 131 | + class ToolClass(BaseTool): |
| 132 | + description = tool_desc |
| 133 | + parameters = tool_parameters |
| 134 | + |
| 135 | + def call(self, params: Union[str, dict], **kwargs) -> str: |
| 136 | + tool_args = json.loads(params) |
| 137 | + # Submit coroutine to the event loop and wait for the result |
| 138 | + manager = MCPManager() |
| 139 | + client = manager.clients[server_name] |
| 140 | + future = asyncio.run_coroutine_threadsafe(client.execute_function(tool_name, tool_args), manager.loop) |
| 141 | + try: |
| 142 | + result = future.result() |
| 143 | + return result |
| 144 | + except Exception as e: |
| 145 | + logger.info(f"Error executing function: {e}") |
| 146 | + return None |
| 147 | + return "Function executed" |
| 148 | + |
| 149 | + ToolClass.__name__ = f"{register_name}_Class" |
| 150 | + return ToolClass() |
| 151 | + |
| 152 | + async def clearup(self): |
| 153 | + await self.exit_stack.aclose() |
| 154 | + |
| 155 | + |
| 156 | +class MCPClient: |
| 157 | + def __init__(self): |
| 158 | + # Initialize session and client objects |
| 159 | + self.session: Optional[ClientSession] = None |
| 160 | + self.tools : list = None |
| 161 | + |
| 162 | + async def connection_server(self, exit_stack, mcp_server): |
| 163 | + """Connect to an MCP server and retrieve the available tools.""" |
| 164 | + try: |
| 165 | + server_params = StdioServerParameters( |
| 166 | + command = mcp_server["command"], |
| 167 | + args = mcp_server["args"], |
| 168 | + env = mcp_server.get("env", None) |
| 169 | + ) |
| 170 | + stdio_transport = await exit_stack.enter_async_context(stdio_client(server_params)) |
| 171 | + self.stdio, self.write = stdio_transport |
| 172 | + self.session = await exit_stack.enter_async_context(ClientSession(self.stdio, self.write)) |
| 173 | + |
| 174 | + await self.session.initialize() |
| 175 | + |
| 176 | + list_tools = await self.session.list_tools() |
| 177 | + self.tools = list_tools.tools |
| 178 | + except Exception as e: |
| 179 | + logger.info(f"Failed to connect to server: {e}") |
| 180 | + |
| 181 | + async def execute_function(self, tool_name, tool_args: dict): |
| 182 | + response = await self.session.call_tool(tool_name, tool_args) |
| 183 | + for content in response.content: |
| 184 | + if content.type == 'text': |
| 185 | + return content.text |
| 186 | + else: |
| 187 | + return "execute error" |
0 commit comments