Improve MCP client working directory handling

- Use os.getcwd() directly instead of PWD fallback (more reliable)
- Add cwd parameter to _run_command for per-command overrides

Amp-Thread-ID: https://ampcode.com/threads/T-bf7caacc-f242-4224-b730-1f4442a7ed50
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-10-17 14:22:17 -07:00
parent 14c744861c
commit ff0ce6c3a7

View File

@@ -114,8 +114,8 @@ class BdClient:
"""
if self.working_dir:
return self.working_dir
# Fall back to PWD environment variable or current directory
return os.environ.get("PWD", os.getcwd())
# Use process working directory (set by MCP client at spawn time)
return os.getcwd()
def _global_flags(self) -> list[str]:
"""Build list of global flags for bd commands.
@@ -134,11 +134,12 @@ class BdClient:
flags.append("--no-auto-import")
return flags
async def _run_command(self, *args: str) -> object:
async def _run_command(self, *args: str, cwd: str | None = None) -> object:
"""Run bd command and parse JSON output.
Args:
*args: Command arguments to pass to bd
cwd: Optional working directory override for this command
Returns:
Parsed JSON output (dict or list)
@@ -148,6 +149,7 @@ class BdClient:
BdCommandError: If bd command fails
"""
cmd = [self.bd_path, *args, *self._global_flags(), "--json"]
working_dir = cwd if cwd is not None else self._get_working_dir()
# Log database routing for debugging
import sys