The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(mcp): remote-session OAuth hint names the configured redirect_host
teknium19dd36c56cf66
diff --git a/tests/tools/test_mcp_oauth.py b/tests/tools/test_mcp_oauth.pyindex 7650d30e526..53a7ec06651 100644--- a/tests/tools/test_mcp_oauth.py+++ b/tests/tools/test_mcp_oauth.py@@ -416,6 +416,21 @@ class TestRedirectHandlerSshHint:assert "ssh -N -L" in errassert "Remote session detected" in err+ def test_ssh_hint_names_the_configured_redirect_host(self, monkeypatch, capsys):+ """A pre-registered client (Asana) registers ``http://localhost:<port>/callback`` verbatim,+ so the remote-session hint must name the same host the provider redirects to."""+ import tools.mcp_oauth as mco+ monkeypatch.setattr(mco, "_is_interactive", lambda: True)+ monkeypatch.setenv("SSH_CLIENT", "1.2.3.4 1234 22")+ monkeypatch.setattr(mco, "_can_open_browser", lambda: False)++ handler = _make_redirect_handler(27890, redirect_host="localhost")+ self._run(handler("https://mcp.example/authorize"))++ err = capsys.readouterr().err+ assert "http://localhost:27890/callback" in err+ assert "http://127.0.0.1:27890/callback" not in err+def test_configured_redirect_uri_shows_proxy_hint_not_tunnel(self, monkeypatch, capsys):"""With a proxy redirect_uri, the SSH hint must not push the loopback tunnel.diff --git a/tools/mcp_oauth.py b/tools/mcp_oauth.pyindex fbb45edc552..a65acf8c6c0 100644--- a/tools/mcp_oauth.py+++ b/tools/mcp_oauth.py@@ -731,7 +731,7 @@ _SSH_HINT_PROXY = (" which forwards to the callback listener on this machine — no SSH tunnel needed.\n")_SSH_HINT_LOOPBACK = (" Remote session detected. After you authorize, the provider redirects to\n"- " http://127.0.0.1:{port}/callback\n"+ " http://{host}:{port}/callback\n"" which only the listener on THIS machine can receive. Two options:\n""\n"" 1. Easiest — when your browser shows a connection error after\n"@@ -746,14 +746,16 @@ _SSH_HINT_LOOPBACK = (" See: https://hermes-agent.nousresearch.com/docs/guides/oauth-over-ssh\n")-def _announce_authorization_url(authorization_url: str, port: int, redirect_uri: str | None) -> None:+def _announce_authorization_url(+ authorization_url: str, port: int, redirect_uri: str | None, redirect_host: str | None = None,+) -> None:"""Print the URL (always, as the fallback) and open the browser when possible."""print(f"\n MCP OAuth: authorization required.\n Open this URL in your browser:\n\n {authorization_url}\n", file=sys.stderr)if os.getenv("SSH_CLIENT") or os.getenv("SSH_TTY"):if redirect_uri:print(_SSH_HINT_PROXY.format(redirect_uri=redirect_uri), file=sys.stderr)elif port:- print(_SSH_HINT_LOOPBACK.format(port=port), file=sys.stderr)+ print(_SSH_HINT_LOOPBACK.format(host=redirect_host or "127.0.0.1", port=port), file=sys.stderr)if not _can_open_browser():note = "Headless environment detected — open the URL manually."else:@@ -764,9 +766,11 @@ def _announce_authorization_url(authorization_url: str, port: int, redirect_uri:print(f" ({note})\n", file=sys.stderr)-def _make_redirect_handler(port: int, redirect_uri: str | None = None):+def _make_redirect_handler(port: int, redirect_uri: str | None = None, redirect_host: str | None = None):"""Redirect handler closing over this flow's port (a closure, not ``_oauth_port``, keeps concurrent- flows isolated). ``redirect_uri`` is a configured proxy callback (None for loopback) and only tailors the hint.+ flows isolated). ``redirect_uri`` is a configured proxy callback (None for loopback) and only tailors the+ hint; ``redirect_host`` is the loopback hostname the provider will actually redirect to (see+ :func:`_resolve_redirect_uri`).Using a closure instead of reading the module-level ``_oauth_port`` avoids cross-server state pollutionwhen multiple MCP servers run OAuth concurrently (fixes #44588).@@ -787,7 +791,7 @@ def _make_redirect_handler(port: int, redirect_uri: str | None = None):_raise_if_non_interactive("MCP OAuth requires browser authorization but no interactive session is available (non-interactive/background context).")- _announce_authorization_url(authorization_url, port, redirect_uri)+ _announce_authorization_url(authorization_url, port, redirect_uri, redirect_host)return _redirect_handlerdiff --git a/tools/mcp_oauth_provider.py b/tools/mcp_oauth_provider.pyindex fce8354844a..94e216e0a1c 100644--- a/tools/mcp_oauth_provider.py+++ b/tools/mcp_oauth_provider.py@@ -443,7 +443,7 @@ def build_provider_kwargs(cfg: dict, storage: "HermesTokenStorage", *, ssh_proxyreturn {"client_metadata": client_metadata,"storage": storage,- "redirect_handler": mo._make_redirect_handler(port, redirect_uri=redirect_uri),+ "redirect_handler": mo._make_redirect_handler(port, redirect_uri=redirect_uri, redirect_host=cfg.get("redirect_host")),# mcp 2.0 dropped OAuthClientProvider's own `timeout`; the configured# `oauth.timeout` bounds the callback waiter's poll loop instead."callback_handler": mo._make_callback_waiter(port, cfg.get("_cimd_url"), timeout=float(cfg.get("timeout", 300))),