fix(mcp): preserve stdio argv passthrough

This commit is contained in:
helix4u
2026-06-11 08:59:55 -07:00
committed by Teknium
parent ee1a744ace
commit dca11b6650
8 changed files with 226 additions and 17 deletions
@@ -138,3 +138,80 @@ class TestApplyProfileOverrideHermesHomeGuard:
_apply_profile_override()
assert os.environ.get("HERMES_HOME") is None
def test_subcommand_profile_flag_is_not_consumed(self, tmp_path, monkeypatch):
"""Command argv flags named --profile must stay with that command.
Docker Desktop's MCP Toolkit uses `docker mcp gateway run --profile ...`.
When that argv is passed through `hermes mcp add --args`, the early
profile pre-parser must not interpret the Docker profile as a Hermes
profile.
"""
hermes_root = tmp_path / ".hermes"
hermes_root.mkdir(parents=True, exist_ok=True)
argv = [
"hermes",
"mcp",
"add",
"docker-research",
"--command",
"docker",
"--args",
"mcp",
"gateway",
"run",
"--profile",
"research",
]
monkeypatch.setattr(Path, "home", lambda: tmp_path)
monkeypatch.delenv("HERMES_HOME", raising=False)
monkeypatch.setattr(sys, "argv", list(argv))
from hermes_cli.main import _apply_profile_override
_apply_profile_override()
assert os.environ.get("HERMES_HOME") is None
assert sys.argv == argv
def test_profile_after_chat_subcommand_is_still_consumed(self, tmp_path, monkeypatch):
"""Profile flags historically work after normal Hermes subcommands."""
result = _run_apply_profile_override(
tmp_path,
monkeypatch,
hermes_home=None,
active_profile="coder",
argv=["hermes", "chat", "-p", "coder", "-q", "hello"],
)
assert result is not None
assert result.endswith("coder")
assert sys.argv == ["hermes", "chat", "-q", "hello"]
def test_top_level_profile_after_value_flag_is_consumed(self, tmp_path, monkeypatch):
"""Top-level --profile still works after other top-level value flags."""
result = _run_apply_profile_override(
tmp_path,
monkeypatch,
hermes_home=None,
active_profile="coder",
argv=["hermes", "-m", "gpt-5", "--profile", "coder", "chat"],
)
assert result is not None
assert result.endswith("coder")
assert sys.argv == ["hermes", "-m", "gpt-5", "chat"]
def test_top_level_profile_after_continue_flag_is_consumed(self, tmp_path, monkeypatch):
"""--continue has an optional value, so a following --profile is a flag."""
result = _run_apply_profile_override(
tmp_path,
monkeypatch,
hermes_home=None,
active_profile="coder",
argv=["hermes", "--continue", "--profile", "coder"],
)
assert result is not None
assert result.endswith("coder")
assert sys.argv == ["hermes", "--continue"]
@@ -41,6 +41,7 @@ def _build_parser():
mcp_add.add_argument("name")
mcp_add.add_argument("--url")
mcp_add.add_argument("--command", dest="mcp_command")
mcp_add.add_argument("--args", nargs=argparse.REMAINDER, default=[])
return parser
@@ -85,3 +86,26 @@ class TestMcpAddCommandDest:
assert args.command == "mcp"
assert args.mcp_command is None
assert args.url is None
def test_args_passthrough_keeps_nested_option_flags(self):
"""`--args` must keep command flags like Docker MCP's --profile."""
parser = _build_parser()
args = parser.parse_args(
[
"mcp",
"add",
"docker-research",
"--command",
"docker",
"--args",
"mcp",
"gateway",
"run",
"--profile",
"research",
]
)
assert args.command == "mcp"
assert args.mcp_command == "docker"
assert args.args == ["mcp", "gateway", "run", "--profile", "research"]
+27
View File
@@ -1378,6 +1378,33 @@ class TestBuildSafeEnv:
assert "DATABASE_URL" not in result
assert "API_SECRET" not in result
def test_windows_location_vars_passed_without_secrets(self):
"""Windows launcher tools need location vars, but secrets stay filtered."""
from tools.mcp_tool import _build_safe_env
fake_env = {
"PATH": r"C:\Windows\System32",
"ProgramFiles": r"C:\Program Files",
"ProgramData": r"C:\ProgramData",
"ProgramW6432": r"C:\Program Files",
"LOCALAPPDATA": r"C:\Users\alice\AppData\Local",
"APPDATA": r"C:\Users\alice\AppData\Roaming",
"USERPROFILE": r"C:\Users\alice",
"GITHUB_TOKEN": "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"OPENAI_API_KEY": "sk-proj-abc123",
}
with patch.dict("os.environ", fake_env, clear=True):
result = _build_safe_env(None)
assert result["ProgramFiles"] == r"C:\Program Files"
assert result["ProgramData"] == r"C:\ProgramData"
assert result["ProgramW6432"] == r"C:\Program Files"
assert result["LOCALAPPDATA"].endswith("Local")
assert result["APPDATA"].endswith("Roaming")
assert result["USERPROFILE"] == r"C:\Users\alice"
assert "GITHUB_TOKEN" not in result
assert "OPENAI_API_KEY" not in result
# ---------------------------------------------------------------------------
# _sanitize_error