Load MCP tools from config file. This function: 1. Reads the MCP config file (with fallback to mcp-example.json) 2. Connects to each server (STDIO or URL-based) 3. Fetches tool definitions 4. Wraps them as Tool objects Supported config formats: - STDIO: {"command":
(config_path: str = "mcp.json")
| 328 | |
| 329 | |
| 330 | async def load_mcp_tools_async(config_path: str = "mcp.json") -> list[Tool]: |
| 331 | """ |
| 332 | Load MCP tools from config file. |
| 333 | |
| 334 | This function: |
| 335 | 1. Reads the MCP config file (with fallback to mcp-example.json) |
| 336 | 2. Connects to each server (STDIO or URL-based) |
| 337 | 3. Fetches tool definitions |
| 338 | 4. Wraps them as Tool objects |
| 339 | |
| 340 | Supported config formats: |
| 341 | - STDIO: {"command": "...", "args": [...], "env": {...}} |
| 342 | - URL-based: {"url": "https://...", "type": "sse|http|streamable_http", "headers": {...}} |
| 343 | |
| 344 | Per-server timeout overrides (optional): |
| 345 | - "connect_timeout": float - Connection timeout in seconds |
| 346 | - "execute_timeout": float - Tool execution timeout in seconds |
| 347 | - "sse_read_timeout": float - SSE read timeout in seconds |
| 348 | |
| 349 | Note: |
| 350 | - If mcp.json is not found, will automatically fallback to mcp-example.json |
| 351 | - User-specific mcp.json should be created by copying mcp-example.json |
| 352 | |
| 353 | Args: |
| 354 | config_path: Path to MCP configuration file (default: "mcp.json") |
| 355 | |
| 356 | Returns: |
| 357 | List of Tool objects representing MCP tools |
| 358 | """ |
| 359 | global _mcp_connections |
| 360 | |
| 361 | config_file = _resolve_mcp_config_path(config_path) |
| 362 | |
| 363 | if config_file is None: |
| 364 | print(f"MCP config not found: {config_path}") |
| 365 | return [] |
| 366 | |
| 367 | try: |
| 368 | with open(config_file, encoding="utf-8") as f: |
| 369 | config = json.load(f) |
| 370 | |
| 371 | mcp_servers = config.get("mcpServers", {}) |
| 372 | |
| 373 | if not mcp_servers: |
| 374 | print("No MCP servers configured") |
| 375 | return [] |
| 376 | |
| 377 | all_tools = [] |
| 378 | |
| 379 | # Connect to each enabled server |
| 380 | for server_name, server_config in mcp_servers.items(): |
| 381 | if server_config.get("disabled", False): |
| 382 | print(f"Skipping disabled server: {server_name}") |
| 383 | continue |
| 384 | |
| 385 | conn_type = _determine_connection_type(server_config) |
| 386 | url = server_config.get("url") |
| 387 | command = server_config.get("command") |