diff --git a/README.md b/README.md index 7387f48..65abf4c 100644 --- a/README.md +++ b/README.md @@ -202,6 +202,7 @@ tools: my-server: command: npx args: ["-y", "my-mcp-server"] + cwd: /path/to/working/directory # Optional working directory ``` **Configuration options**: @@ -213,6 +214,15 @@ tools: | `max_content_size` | `50000` | Maximum content size in characters. Content exceeding this limit is automatically truncated with a notice to prevent context window exhaustion | | `servers` | (optional) | Inline server configuration (overrides .amplifier/mcp.json) | +**Per-server configuration options**: + +| Option | Required | Description | +|--------|----------|-------------| +| `command` | Yes | Command to execute (e.g., `npx`, `python`, `node`) | +| `args` | No | Arguments for the command | +| `env` | No | Environment variables for the server process | +| `cwd` | No | Working directory for the server process. Falls back to `AMPLIFIER_WORK_DIRECTORY` env var if not specified | + **Environment variable override**: ```bash # Enable verbose output without changing bundle config diff --git a/amplifier_module_tool_mcp/client.py b/amplifier_module_tool_mcp/client.py index 6e75d34..c25c0d5 100644 --- a/amplifier_module_tool_mcp/client.py +++ b/amplifier_module_tool_mcp/client.py @@ -49,6 +49,7 @@ def __init__( command: str, args: list[str], env: dict[str, str] | None = None, + cwd: str | Path | None = None, reconnection_config: ReconnectionConfig | None = None, verbose_servers: bool = False, server_log_dir: Path | None = None, @@ -61,6 +62,7 @@ def __init__( command: Command to execute (e.g., "npx", "python") args: Arguments for the command env: Optional environment variables + cwd: Optional working directory for the server process reconnection_config: Reconnection configuration (uses defaults if None) verbose_servers: Whether to show server stderr output (default: False) server_log_dir: Directory for server logs when suppressed (default: ~/.amplifier/logs/mcp-servers/) @@ -69,6 +71,7 @@ def __init__( self.command = command self.args = args self.env = env or {} + self.cwd = cwd self.session: ClientSession | None = None self.tools: list[dict[str, Any]] = [] self.resources: list[dict[str, Any]] = [] @@ -143,6 +146,7 @@ async def _connection_task_impl(self) -> None: command=self.command, args=self.args, env=merged_env, + cwd=self.cwd, ) # Open log file if needed diff --git a/amplifier_module_tool_mcp/manager.py b/amplifier_module_tool_mcp/manager.py index baceda5..a7d89e3 100644 --- a/amplifier_module_tool_mcp/manager.py +++ b/amplifier_module_tool_mcp/manager.py @@ -152,9 +152,16 @@ async def _start_stdio_server(self, server_name: str, server_config: dict[str, A # Substitute environment variables env = {k: MCPConfig.substitute_env_vars(v) for k, v in env.items()} + # Get working directory from config, or fall back to AMPLIFIER_WORK_DIRECTORY env var + cwd = server_config.get("cwd") + if not cwd: + cwd = os.environ.get("AMPLIFIER_WORK_DIRECTORY") + if cwd: + logger.debug(f"Server '{server_name}' will use working directory: {cwd}") + # Create and connect client with verbose settings client = MCPClient( - server_name, command, args, env, verbose_servers=self.verbose_servers, server_log_dir=self.server_log_dir + server_name, command, args, env, cwd=cwd, verbose_servers=self.verbose_servers, server_log_dir=self.server_log_dir ) await client.connect()