|
| 1 | +"""MCP server configuration loading utilities.""" |
| 2 | + |
| 3 | +import json |
| 4 | +from pathlib import Path |
| 5 | +from typing import Dict, List, Optional |
| 6 | + |
| 7 | +from mcp import StdioServerParameters |
| 8 | +from mcp.client.stdio import stdio_client |
| 9 | + |
| 10 | +from .mcp_client import MCPClient |
| 11 | +from .mcp_types import MCPTransport |
| 12 | + |
| 13 | + |
| 14 | +class MCPServerConfig: |
| 15 | + """Configuration for an MCP server following MCP standards.""" |
| 16 | + |
| 17 | + def __init__( |
| 18 | + self, |
| 19 | + name: str, |
| 20 | + command: str, |
| 21 | + args: Optional[List[str]] = None, |
| 22 | + env: Optional[Dict[str, str]] = None, |
| 23 | + timeout: Optional[int] = None, |
| 24 | + ): |
| 25 | + """Initialize MCP server configuration. |
| 26 | +
|
| 27 | + Args: |
| 28 | + name: Server name |
| 29 | + command: Command to run the server |
| 30 | + args: Command arguments |
| 31 | + env: Environment variables |
| 32 | + timeout: Timeout in milliseconds |
| 33 | + """ |
| 34 | + self.name = name |
| 35 | + self.command = command |
| 36 | + self.args = args or [] |
| 37 | + self.env = env or {} |
| 38 | + self.timeout = timeout or 60000 |
| 39 | + |
| 40 | + def create_client(self) -> MCPClient: |
| 41 | + """Create an MCPClient from this configuration.""" |
| 42 | + |
| 43 | + def transport_callable() -> MCPTransport: |
| 44 | + server_params = StdioServerParameters(command=self.command, args=self.args, env=self.env) |
| 45 | + return stdio_client(server_params) |
| 46 | + |
| 47 | + return MCPClient(transport_callable) |
| 48 | + |
| 49 | + @classmethod |
| 50 | + def from_config(cls, config_path: str) -> List["MCPServerConfig"]: |
| 51 | + """Load MCP server configurations from standard mcp.json format. |
| 52 | +
|
| 53 | + Args: |
| 54 | + config_path: Path to the MCP configuration file |
| 55 | +
|
| 56 | + Returns: |
| 57 | + List of MCPServerConfig instances |
| 58 | +
|
| 59 | + Config file examples: |
| 60 | + Anthropic MCP Server Config Examples: (https://modelcontextprotocol.io/examples) |
| 61 | + AmazonQ MCP Server Config Examples: (https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/command-line-mcp-understanding-config.html) |
| 62 | +
|
| 63 | + Expected format: |
| 64 | + { |
| 65 | + "mcpServers": { |
| 66 | + "server-name": { |
| 67 | + "command": "command-to-run", |
| 68 | + "args": ["arg1", "arg2"], |
| 69 | + "env": { |
| 70 | + "ENV_VAR1": "value1", |
| 71 | + "ENV_VAR2": "value2" |
| 72 | + }, |
| 73 | + "timeout": 60000 |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +
|
| 78 | + """ |
| 79 | + config_file = Path(config_path) |
| 80 | + if not config_file.exists(): |
| 81 | + raise FileNotFoundError(f"Config file not found: {config_path}") |
| 82 | + |
| 83 | + with open(config_file) as f: |
| 84 | + config_data = json.load(f) |
| 85 | + |
| 86 | + servers = [] |
| 87 | + mcp_server_name = set() |
| 88 | + mcp_servers = config_data.get("mcpServers", {}) |
| 89 | + expected_attrs = {"command", "args", "env", "timeout"} |
| 90 | + |
| 91 | + for name, server_config in mcp_servers.items(): |
| 92 | + if len(name) == 0 or len(name) > 250 or server_config.get("command") is None or name in mcp_server_name: |
| 93 | + raise ValueError(f"Invalid server configuration for {name}") |
| 94 | + if set(server_config.keys()) - expected_attrs: |
| 95 | + raise ValueError(f"Invalid server configuration for {name}") |
| 96 | + |
| 97 | + servers.append( |
| 98 | + cls( |
| 99 | + name=name, |
| 100 | + command=server_config["command"], |
| 101 | + args=server_config.get("args"), |
| 102 | + env=server_config.get("env"), |
| 103 | + timeout=server_config.get("timeout"), |
| 104 | + ) |
| 105 | + ) |
| 106 | + mcp_server_name.add(name) |
| 107 | + |
| 108 | + return servers |
0 commit comments