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