From e94cc545d5060f2704933e6d065d05dff4099778 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 2 Sep 2026 16:16:45 -0700 Subject: [PATCH] refactor(mcp): unify HTTP/SSE/legacy transport serve+teardown into _serve_transport; split _run_stdio into ledger/preflight helpers --- tools/mcp_tool_transport.py | 668 ++++++++++++++++++------------------ 1 file changed, 328 insertions(+), 340 deletions(-) diff --git a/tools/mcp_tool_transport.py b/tools/mcp_tool_transport.py index 8a53d549f2..b60bad1556 100644 --- a/tools/mcp_tool_transport.py +++ b/tools/mcp_tool_transport.py @@ -3,7 +3,8 @@ import logging import asyncio import os -from typing import Dict, Optional +from contextlib import asynccontextmanager +from typing import Dict, Optional, Set from tools.mcp_tool_config import _wrap_command_with_watchdog from tools.mcp_tool_errors import NonMcpEndpointError, _apply_identity_header, _handshake_rejected_as_modern, _make_redirect_header_stripper, _resolve_client_cert from tools.mcp_tool_lifecycle import _filter_mcp_children, _orphan_stdio_pid_servers, _orphan_stdio_pids, _stdio_pgids, _stdio_pids @@ -11,6 +12,72 @@ from tools.mcp_tool_common import _core logger = logging.getLogger("tools.mcp_tool") +# JSON-RPC ``initialize`` body used by the content-type preflight POST. +_PROBE_INITIALIZE_BODY = ( + '{"jsonrpc":"2.0","id":"_probe",' + '"method":"initialize",' + '"params":{"protocolVersion":"2025-03-26",' + '"capabilities":{},' + '"clientInfo":{"name":"hermes-probe",' + '"version":"0.1"}}}' +) + + +def _content_type_base(resp) -> str: + """``content-type`` header of *resp* without parameters, lowercased.""" + return resp.headers.get("content-type", "").split(";")[0].strip().lower() + + +def _is_2xx(resp) -> bool: + return 200 <= resp.status_code < 300 + + +def _capture_pgids(pids: Set[int]) -> Dict[int, int]: + """pgid per live pid. Captured while the child is alive — getpgid fails once + it exits, and the sweep needs it to reach reparented descendants.""" + pgids: Dict[int, int] = {} + for pid in pids: + try: + pgids[pid] = os.getpgid(pid) + except (AttributeError, ProcessLookupError, OSError): + # AttributeError: Windows; ProcessLookupError: already exited. + pass + return pgids + + +def _pgroup_alive(pgid: Optional[int]) -> bool: + """Signal 0 to the group succeeds iff any member is alive (POSIX only).""" + _killpg = getattr(os, "killpg", None) + if pgid is None or _killpg is None: + return False + try: + _killpg(pgid, 0) + return True + except (ProcessLookupError, PermissionError, OSError): + return False + + +async def _osv_malware_preflight(server_name: str, command: str, args: list) -> None: + """OSV malware preflight: off-loop (blocking HTTPS) with a wall-clock bound so + a stalled handshake can't freeze discovery; fail-open on timeout. Must run + against the REAL command/args — the watchdog wrap rewrites argv to the + supervisor, which would turn the check into a no-op.""" + from tools.osv_check import check_package_for_malware + try: + malware_error = await asyncio.wait_for( + asyncio.to_thread(check_package_for_malware, command, args), + timeout=_core._OSV_MALWARE_CHECK_TIMEOUT_S, + ) + except asyncio.TimeoutError: + logger.warning( + "MCP server '%s': OSV malware preflight timed out after %.0fs " + "(network slow/unreachable) — proceeding without the check.", + server_name, _core._OSV_MALWARE_CHECK_TIMEOUT_S, + ) + return + if malware_error: + raise ValueError(f"MCP server '{server_name}': {malware_error}") + class MCPServerTransportMixin: """Methods of :class:`tools.mcp_tool.MCPServerTask` (mixed in; relies on its attributes).""" @@ -30,6 +97,17 @@ class MCPServerTransportMixin: return True return getattr(caps, "tools", None) is not None + def _session_kwargs(self) -> dict: + """ClientSession kwargs: sampling, elicitation, notification + logging callbacks.""" + kwargs = self._sampling.session_kwargs() if self._sampling else {} + if self._elicitation: + kwargs.update(self._elicitation.session_kwargs()) + if _core._MCP_NOTIFICATION_TYPES and _core._MCP_MESSAGE_HANDLER_SUPPORTED: + kwargs["message_handler"] = self._make_message_handler() + if _core._MCP_LOGGING_CALLBACK_SUPPORTED: + kwargs["logging_callback"] = self._make_logging_callback() + return kwargs + async def _negotiate_session(self, session, connect_timeout: float): """Negotiate the protocol era (``initialize`` vs ``server/discover``) and return its result. @@ -40,13 +118,18 @@ class MCPServerTransportMixin: servers that dominate today. ``stateless`` probes discover first (one legacy retry on error); ``legacy`` is handshake only, no fallback. Both result types expose ``.capabilities``, so downstream gates work on either. + A handshake TIMEOUT never triggers a fallback — it propagates. """ + def initialize(): + return asyncio.wait_for(session.initialize(), timeout=connect_timeout) + + def discover(): + return asyncio.wait_for(session.discover(), timeout=connect_timeout) + mode = str((self._config or {}).get("protocol", "auto")).lower().strip() if mode in ("stateless", "modern", "2026-07-28"): try: - return await asyncio.wait_for( - session.discover(), timeout=connect_timeout - ) + return await discover() except asyncio.TimeoutError: raise except Exception as exc: @@ -55,38 +138,28 @@ class MCPServerTransportMixin: "protocol=%s — falling back to the legacy handshake", self.name, exc, mode, ) - return await asyncio.wait_for( - session.initialize(), timeout=connect_timeout - ) + return await initialize() if mode in ("legacy", "handshake"): - return await asyncio.wait_for( - session.initialize(), timeout=connect_timeout - ) + return await initialize() if mode != "auto": logger.warning( "MCP server '%s': unknown protocol=%r — treating as 'auto' " "(valid: auto, stateless, legacy)", self.name, mode, ) try: - return await asyncio.wait_for( - session.initialize(), timeout=connect_timeout - ) + return await initialize() except asyncio.TimeoutError: raise except Exception as exc: - if not _handshake_rejected_as_modern(exc): - raise - if not hasattr(session, "discover"): - # mcp 1.x has no server/discover client — nothing to fall back to. + # mcp 1.x has no server/discover client — nothing to fall back to. + if not _handshake_rejected_as_modern(exc) or not hasattr(session, "discover"): raise logger.info( "MCP server '%s': legacy handshake rejected (%s) — " "retrying via server/discover (2026-07-28 stateless server)", self.name, exc, ) - return await asyncio.wait_for( - session.discover(), timeout=connect_timeout - ) + return await discover() async def _serve_session(self, session, connect_timeout: float, label: str = "", mark_lifecycle: bool = False) -> str: @@ -114,6 +187,74 @@ class MCPServerTransportMixin: ) return reason + async def _serve_transport(self, transport_cm, label: str, connect_timeout: float) -> str: + """Open *transport_cm*, wrap its streams in a ClientSession and serve it. + + Streams are unpacked positionally: mcp 1.x yields ``(read, write, + get_session_id)``, 2.x yields ``(read, write)``. A transport TaskGroup + drop maps to ``"reconnect"`` instead of backoff/park. + """ + try: + async with transport_cm as _streams: + async with _core.ClientSession( + _streams[0], _streams[1], **self._session_kwargs() + ) as session: + return await self._serve_session(session, connect_timeout, label) + except BaseExceptionGroup as _eg: + return self._reconnect_or_reraise_group(_eg) + + # ------------------------------------------------------------------ stdio + + def _resolve_stdio_config(self, config: dict): + """``(command, args, safe_env)`` from config, with the command resolved against the safe env.""" + command = config.get("command") + if not command: + raise ValueError(f"MCP server '{self.name}' has no 'command' in config") + args = config.get("args", []) + safe_env = _core._build_safe_env(config.get("env")) + command, safe_env = _core._resolve_stdio_command(command, safe_env) + return command, args, safe_env + + def _track_spawned_children(self, new_pids: Set[int]) -> None: + """Ledger the freshly spawned stdio children (pids, pgids, machine spawn ledger).""" + new_pgids = _capture_pgids(new_pids) + with _core._lock: + for _pid in new_pids: + _stdio_pids[_pid] = self.name + _stdio_pgids.update(new_pgids) + # Machine spawn ledger so startup sweeps can reap orphans after an + # unclean parent exit. Best-effort — never break startup. + for _pid in new_pids: + try: + from hermes_cli.process_identity import register_child + + register_child(_pid, "mcp-helper") + except Exception: + logger.debug( + "spawn-ledger register_child failed for MCP helper pid %s", + _pid, exc_info=True, + ) + + def _release_spawned_children(self, new_pids: Set[int]) -> None: + """Drop the ledger entries; any child (or its pgroup) still alive means SDK + teardown failed (common on cancel mid-way on Linux, where setsid() children + escape the cgroup) — mark it orphaned for the next cleanup sweep.""" + from gateway.status import _pid_exists + with _core._lock: + for _pid in new_pids: + _stdio_pids.pop(_pid, None) + for pid in new_pids: + # ``os.kill(pid, 0)`` is NOT a no-op on Windows; use the + # cross-platform check. Child exited but descendants may remain + # in its pgroup. + if _pid_exists(pid) or _pgroup_alive(_stdio_pgids.get(pid)): + _orphan_stdio_pids.add(pid) + _orphan_stdio_pid_servers[pid] = self.name + else: + # Nothing to reap — drop the pgid so PID reuse can't + # surface stale pgroup state later. + _stdio_pgids.pop(pid, None) + async def _run_stdio(self, config: dict): """Run the server using stdio transport.""" if config.get("identity_header") is not None: @@ -128,47 +269,13 @@ class MCPServerTransportMixin: "it is not installed. Run `hermes setup` to install MCP support, " "then retry." ) - - command = config.get("command") - args = config.get("args", []) - user_env = config.get("env") - - if not command: - raise ValueError( - f"MCP server '{self.name}' has no 'command' in config" - ) - - safe_env = _core._build_safe_env(user_env) - command, safe_env = _core._resolve_stdio_command(command, safe_env) - - # OSV malware preflight: off-loop (blocking HTTPS) with a wall-clock bound - # so a stalled handshake can't freeze discovery; fail-open on timeout. - # Must run against the REAL command/args — the watchdog wrap below rewrites - # argv to the supervisor, which would turn the check into a no-op. - from tools.osv_check import check_package_for_malware - try: - malware_error = await asyncio.wait_for( - asyncio.to_thread(check_package_for_malware, command, args), - timeout=_core._OSV_MALWARE_CHECK_TIMEOUT_S, - ) - except asyncio.TimeoutError: - logger.warning( - "MCP server '%s': OSV malware preflight timed out after %.0fs " - "(network slow/unreachable) — proceeding without the check.", - self.name, _core._OSV_MALWARE_CHECK_TIMEOUT_S, - ) - malware_error = None - if malware_error: - raise ValueError( - f"MCP server '{self.name}': {malware_error}" - ) - + command, args, safe_env = self._resolve_stdio_config(config) + await _osv_malware_preflight(self.name, command, args) # Parent-death watchdog: an ungraceful Hermes exit (kill -9, crash) can't # leave the child and its descendants running. Clean-exit reaping is # unchanged. POSIX-only (process groups); no-op elsewhere. AFTER the OSV # preflight so the check inspects the real package. command, args = _wrap_command_with_watchdog(command, args) - server_params = _core.StdioServerParameters( command=command, args=args, @@ -178,14 +285,7 @@ class MCPServerTransportMixin: # substitute U+FFFD instead of raising UnicodeDecodeError. encoding_error_handler="replace", ) - - sampling_kwargs = self._sampling.session_kwargs() if self._sampling else {} - if self._elicitation: - sampling_kwargs.update(self._elicitation.session_kwargs()) - if _core._MCP_NOTIFICATION_TYPES and _core._MCP_MESSAGE_HANDLER_SUPPORTED: - sampling_kwargs["message_handler"] = self._make_message_handler() - if _core._MCP_LOGGING_CALLBACK_SUPPORTED: - sampling_kwargs["logging_callback"] = self._make_logging_callback() + session_kwargs = self._session_kwargs() # Reap orphans from prior failed attempts before spawning, else each # reconnect retry piles up zombie pairs. Unscoped on purpose (also reaps @@ -209,42 +309,14 @@ class MCPServerTransportMixin: # children (slash_worker, LSP servers) that race into the snapshot # window: they share the TUI parent's pgid, so leaking them into # _stdio_pgids makes the shutdown killpg() kill the TUI itself. - new_pids = _filter_mcp_children( - _core._snapshot_child_pids() - pids_before - ) + new_pids = _filter_mcp_children(_core._snapshot_child_pids() - pids_before) if new_pids: - # Capture pgid while the child is alive — getpgid fails once it - # exits, and the sweep needs it to reach reparented descendants. - new_pgids: Dict[int, int] = {} - for _pid in new_pids: - try: - new_pgids[_pid] = os.getpgid(_pid) - except (AttributeError, ProcessLookupError, OSError): - # AttributeError: Windows; ProcessLookupError: already exited. - pass - with _core._lock: - for _pid in new_pids: - _stdio_pids[_pid] = self.name - _stdio_pgids.update(new_pgids) - # Machine spawn ledger so startup sweeps can reap orphans after - # an unclean parent exit. Best-effort — never break startup. - for _pid in new_pids: - try: - from hermes_cli.process_identity import register_child - - register_child(_pid, "mcp-helper") - except Exception: - logger.debug( - "spawn-ledger register_child failed for MCP " - "helper pid %s", - _pid, - exc_info=True, - ) + self._track_spawned_children(new_pids) # Tracked on the connection so in-flight calls fail fast when # the subprocess dies. self._stdio_child_pids = set(new_pids) async with _core.ClientSession( - read_stream, write_stream, **sampling_kwargs + read_stream, write_stream, **session_kwargs ) as session: # Bound the handshake: ``connect_timeout`` only bounds the # caller's ``.result()`` wait, not this coroutine. A server that @@ -258,37 +330,11 @@ class MCPServerTransportMixin: session, connect_timeout, mark_lifecycle=True ) finally: - # Runs on clean exit, exceptions AND cancellation. Any spawned PID - # still alive means SDK teardown failed (common on cancel mid-way on - # Linux, where setsid() children escape the cgroup): mark it orphaned - # for the next cleanup sweep. + # Runs on clean exit, exceptions AND cancellation. if new_pids: - from gateway.status import _pid_exists - _killpg = getattr(os, "killpg", None) - with _core._lock: - for _pid in new_pids: - _stdio_pids.pop(_pid, None) - for pid in new_pids: - # ``os.kill(pid, 0)`` is NOT a no-op on Windows; use the - # cross-platform check. - pid_alive = _pid_exists(pid) - pgroup_alive = False - pgid = _stdio_pgids.get(pid) - if not pid_alive and pgid is not None and _killpg is not None: - # Child exited but descendants may remain in its pgroup; - # signal 0 succeeds iff any member is alive. - try: - _killpg(pgid, 0) - pgroup_alive = True - except (ProcessLookupError, PermissionError, OSError): - pgroup_alive = False - if pid_alive or pgroup_alive: - _orphan_stdio_pids.add(pid) - _orphan_stdio_pid_servers[pid] = self.name - else: - # Nothing to reap — drop the pgid so PID reuse can't - # surface stale pgroup state later. - _stdio_pgids.pop(pid, None) + self._release_spawned_children(new_pids) + + # ------------------------------------------------------------------- HTTP async def _preflight_content_type( self, @@ -331,20 +377,10 @@ class MCPServerTransportMixin: resp = await client.head(url, headers=probe_headers) if resp.status_code in (405, 501): resp = await client.get(url, headers=probe_headers) - # Non-MCP content type on HEAD/GET: try a JSON-RPC POST before # rejecting, so POST-only servers aren't false positives. - ct = ( - resp.headers.get("content-type", "") - .split(";")[0] - .strip() - .lower() - ) - if ( - ct - and ct not in self._MCP_CONTENT_TYPES - and 200 <= resp.status_code < 300 - ): + ct = _content_type_base(resp) + if ct and ct not in self._MCP_CONTENT_TYPES and _is_2xx(resp): post_resp = await client.post( url, headers={ @@ -352,38 +388,21 @@ class MCPServerTransportMixin: "Content-Type": "application/json", "Accept": "application/json, text/event-stream", }, - content=( - '{"jsonrpc":"2.0","id":"_probe",' - '"method":"initialize",' - '"params":{"protocolVersion":"2025-03-26",' - '"capabilities":{},' - '"clientInfo":{"name":"hermes-probe",' - '"version":"0.1"}}}' - ), + content=_PROBE_INITIALIZE_BODY, ) - if 200 <= post_resp.status_code < 300: - post_ct = ( - post_resp.headers.get("content-type", "") - .split(";")[0] - .strip() - .lower() - ) - if post_ct in self._MCP_CONTENT_TYPES: - resp = post_resp + if _is_2xx(post_resp) and _content_type_base(post_resp) in self._MCP_CONTENT_TYPES: + resp = post_resp except _httpx.HTTPError: return # DNS/connect/timeout/transport error — let the SDK try. # Only judge 2xx: a 4xx/5xx may be an auth challenge or transient error - # the real handshake handles correctly. - if not (200 <= resp.status_code < 300): + # the real handshake handles correctly. No content type advertised → + # don't second-guess the SDK. + if not _is_2xx(resp): + return + ct_base = _content_type_base(resp) + if not ct_base or ct_base in self._MCP_CONTENT_TYPES: return - - ct_base = resp.headers.get("content-type", "").split(";")[0].strip().lower() - if not ct_base: - return # No content type advertised — don't second-guess the SDK. - if ct_base in self._MCP_CONTENT_TYPES: - return # Looks like a real MCP endpoint. - raise NonMcpEndpointError( f"MCP server '{self.name}' at {url} returned Content-Type " f"'{ct_base}', not an MCP response (expected one of: " @@ -406,15 +425,12 @@ class MCPServerTransportMixin: propagate), or no live session was reached this attempt (``_ready`` unset — connect failures must go through backoff, not hot-loop). """ - if self._shutdown_event.is_set(): - raise eg - fatal, _rest = eg.split((KeyboardInterrupt, SystemExit)) - if fatal is not None: - raise eg - cancelled, _rest = eg.split(asyncio.CancelledError) - if cancelled is not None: - raise eg - if not self._ready.is_set(): + if ( + self._shutdown_event.is_set() + or eg.split((KeyboardInterrupt, SystemExit))[0] is not None + or eg.split(asyncio.CancelledError)[0] is not None + or not self._ready.is_set() + ): raise eg logger.debug( "MCP server '%s': transport TaskGroup exited after a live session " @@ -423,8 +439,133 @@ class MCPServerTransportMixin: ) return "reconnect" + def _build_oauth_auth(self, url: str, config: dict): + """OAuth 2.1 PKCE via the central MCPOAuthManager so one provider is reused + across reconnects and shared with config-time CLI paths. On setup failure + (e.g. non-interactive without cached tokens) re-raise so only this server + is reported failed.""" + if self._auth_type != "oauth": + return None + try: + from tools.mcp_oauth_manager import get_manager + return get_manager().get_or_build_provider(self.name, url, config.get("oauth")) + except Exception as exc: + logger.warning("MCP OAuth setup failed for '%s': %s", self.name, exc) + raise + + def _sse_transport(self, url: str, headers: dict, connect_timeout: float, + ssl_verify, client_cert, oauth_auth, strict_cfg_headers: bool): + """``sse_client`` context manager for ``transport: sse`` entries.""" + if strict_cfg_headers: + # Fail closed: SSE cannot enforce the redirect boundary. + raise ValueError( + f"MCP server '{self.name}': strict_redirect_headers is " + "not supported on the SSE transport." + ) + if _core.sse_client is None: + raise ImportError( + f"MCP server '{self.name}' requires SSE transport but " + "mcp.client.sse.sse_client is not available. " + "Upgrade the mcp package to get SSE support." + ) + # sse_read_timeout bounds the gap between SSE events. SSE servers + # commonly idle for minutes, so tool_timeout (60s) would drop the + # stream; 300s matches the Streamable HTTP read timeout. + sse_kwargs: dict = { + "url": url, + "headers": headers or None, + "timeout": float(connect_timeout), + "sse_read_timeout": 300.0, + } + if oauth_auth is not None: + # Forward OAuth to sse_client, else OAuth SSE servers 401 silently. + sse_kwargs["auth"] = oauth_auth + if client_cert is not None or ssl_verify is not True: + # sse_client has no verify/cert kwargs: wrap the SDK defaults + # (follow_redirects=True) in an httpx_client_factory, forwarding + # the SDK's (headers, auth, timeout) and layering TLS on top. The + # client MUST come from the SDK's own httpx module (httpx2 on + # mcp >= 2.0) — see sdk_httpx(). + _httpx_mod = _core.sdk_httpx() + + def _mcp_http_client_factory(headers=None, timeout=None, auth=None): + kwargs: dict = { + "follow_redirects": True, + "verify": ssl_verify, + "timeout": timeout if timeout is not None else _httpx_mod.Timeout(30.0, read=300.0), + } + if headers is not None: + kwargs["headers"] = headers + if auth is not None: + kwargs["auth"] = auth + if client_cert is not None: + kwargs["cert"] = client_cert + return _httpx_mod.AsyncClient(**kwargs) + + sse_kwargs["httpx_client_factory"] = _mcp_http_client_factory + return _core.sse_client(**sse_kwargs) + + def _streamable_http_transport(self, url: str, headers: dict, connect_timeout: float, + ssl_verify, client_cert, oauth_auth, + strict_cfg_headers: bool, configured_header_names: set): + """Streamable HTTP context manager (mcp >= 1.24.0: caller-owned httpx client).""" + if not _core._MCP_NEW_HTTP: + return self._legacy_http_transport(url, headers, connect_timeout, ssl_verify, + oauth_auth, strict_cfg_headers) + # Build an explicit AsyncClient matching the SDK's create_mcp_http_client + # defaults. It MUST come from the SDK's httpx module (httpx2 on mcp >= 2.0) + # since the SDK sends its own Request objects through it — see sdk_httpx(). + httpx = _core.sdk_httpx() + _strip_auth_on_cross_origin_redirect = _make_redirect_header_stripper( + httpx.URL(url), + strict=strict_cfg_headers, + configured_header_names=configured_header_names, + ) + client_kwargs: dict = { + "follow_redirects": True, + "timeout": httpx.Timeout(float(connect_timeout), read=300.0), + "verify": ssl_verify, + "event_hooks": {"response": [_strip_auth_on_cross_origin_redirect]}, + } + if headers: + client_kwargs["headers"] = headers + if oauth_auth is not None: + client_kwargs["auth"] = oauth_auth + if client_cert is not None: + client_kwargs["cert"] = client_cert + + @asynccontextmanager + async def _owned_client_streams(): + # Caller owns the client lifecycle — the SDK skips cleanup when + # http_client is provided. + async with httpx.AsyncClient(**client_kwargs) as http_client: + async with _core.streamable_http_client(url, http_client=http_client) as streams: + yield streams + + return _owned_client_streams() + + def _legacy_http_transport(self, url: str, headers: dict, connect_timeout: float, + ssl_verify, oauth_auth, strict_cfg_headers: bool): + """Deprecated API (mcp < 1.24.0): the SDK owns the httpx client.""" + if strict_cfg_headers: + # Fail closed: without an owned client we cannot hook redirects, + # so the cross-origin header boundary cannot be enforced. + raise ImportError( + f"MCP server '{self.name}' requires mcp >= 1.24.0 to " + "enforce the portable redirect-header boundary " + "(strict_redirect_headers). Upgrade the mcp package." + ) + http_kwargs: dict = { + "headers": headers, + "timeout": float(connect_timeout), + "verify": ssl_verify, + } + if oauth_auth is not None: + http_kwargs["auth"] = oauth_auth + return _core.streamablehttp_client(url, **http_kwargs) + async def _run_http(self, config: dict): - """Run the server using HTTP/StreamableHTTP transport.""" + """Run the server using HTTP/StreamableHTTP (or SSE) transport.""" _core._ensure_mcp_sdk() if not _core._MCP_HTTP_AVAILABLE: raise ImportError( @@ -432,14 +573,13 @@ class MCPServerTransportMixin: "mcp.client.streamable_http is not available. " "Upgrade the mcp package to get HTTP support." ) - url = config["url"] headers = dict(config.get("headers") or {}) # Portable Agent Plugins v1 (strict_redirect_headers): configured # headers MUST NOT follow a redirect to a different origin. Capture the # configured names BEFORE client-generated headers are merged in. - _strict_cfg_headers = bool(config.get("strict_redirect_headers")) - _configured_header_names = {key.lower() for key in headers} + strict_cfg_headers = bool(config.get("strict_redirect_headers")) + configured_header_names = {key.lower() for key in headers} # Optional per-user identity header; explicit headers of the same name win. headers = _apply_identity_header(self.name, config, headers) # Some servers require MCP-Protocol-Version on the initial request; seed @@ -453,173 +593,21 @@ class MCPServerTransportMixin: connect_timeout = config.get("connect_timeout", _core._DEFAULT_CONNECT_TIMEOUT) ssl_verify = config.get("ssl_verify", True) client_cert = _resolve_client_cert(self.name, config) + oauth_auth = self._build_oauth_auth(url, config) - # OAuth 2.1 PKCE via the central MCPOAuthManager so one provider is - # reused across reconnects and shared with config-time CLI paths. On - # setup failure (e.g. non-interactive without cached tokens) re-raise so - # only this server is reported failed. - _oauth_auth = None - if self._auth_type == "oauth": - try: - from tools.mcp_oauth_manager import get_manager - _oauth_auth = get_manager().get_or_build_provider( - self.name, url, config.get("oauth"), - ) - except Exception as exc: - logger.warning("MCP OAuth setup failed for '%s': %s", self.name, exc) - raise - - sampling_kwargs = self._sampling.session_kwargs() if self._sampling else {} - if self._elicitation: - sampling_kwargs.update(self._elicitation.session_kwargs()) - if _core._MCP_NOTIFICATION_TYPES and _core._MCP_MESSAGE_HANDLER_SUPPORTED: - sampling_kwargs["message_handler"] = self._make_message_handler() - if _core._MCP_LOGGING_CALLBACK_SUPPORTED: - sampling_kwargs["logging_callback"] = self._make_logging_callback() - - # SSE transport (``transport: sse`` in the mcp_servers entry). if config.get("transport") == "sse": - if _strict_cfg_headers: - # Fail closed: SSE cannot enforce the redirect boundary. - raise ValueError( - f"MCP server '{self.name}': strict_redirect_headers is " - "not supported on the SSE transport." - ) - if _core.sse_client is None: - raise ImportError( - f"MCP server '{self.name}' requires SSE transport but " - "mcp.client.sse.sse_client is not available. " - "Upgrade the mcp package to get SSE support." - ) - # sse_read_timeout bounds the gap between SSE events. SSE servers - # commonly idle for minutes, so tool_timeout (60s) would drop the - # stream; 300s matches the Streamable HTTP read timeout below. - _sse_kwargs: dict = { - "url": url, - "headers": headers or None, - "timeout": float(connect_timeout), - "sse_read_timeout": 300.0, - } - if _oauth_auth is not None: - # Forward OAuth to sse_client, else OAuth SSE servers 401 silently. - _sse_kwargs["auth"] = _oauth_auth - if client_cert is not None or ssl_verify is not True: - # sse_client has no verify/cert kwargs: wrap the SDK defaults - # (follow_redirects=True) in an httpx_client_factory, forwarding - # the SDK's (headers, auth, timeout) and layering TLS on top. The - # client MUST come from the SDK's own httpx module (httpx2 on - # mcp >= 2.0) — see sdk_httpx(). - _httpx_mod = _core.sdk_httpx() - - _cert_for_factory = client_cert - _verify_for_factory = ssl_verify - - def _mcp_http_client_factory( - headers=None, timeout=None, auth=None, - ): - kwargs: dict = { - "follow_redirects": True, - "verify": _verify_for_factory, - } - if timeout is not None: - kwargs["timeout"] = timeout - else: - kwargs["timeout"] = _httpx_mod.Timeout(30.0, read=300.0) - if headers is not None: - kwargs["headers"] = headers - if auth is not None: - kwargs["auth"] = auth - if _cert_for_factory is not None: - kwargs["cert"] = _cert_for_factory - return _httpx_mod.AsyncClient(**kwargs) - - _sse_kwargs["httpx_client_factory"] = _mcp_http_client_factory - try: - async with _core.sse_client(**_sse_kwargs) as (read_stream, write_stream): - async with _core.ClientSession( - read_stream, write_stream, **sampling_kwargs - ) as session: - reason = await self._serve_session( - session, float(connect_timeout), "SSE" - ) - except BaseExceptionGroup as _eg: - # Transport TaskGroup dropped: reconnect instead of backoff/park. - reason = self._reconnect_or_reraise_group(_eg) - return reason - - if _core._MCP_NEW_HTTP: - # mcp >= 1.24.0: build an explicit AsyncClient matching the SDK's - # create_mcp_http_client defaults. It MUST come from the SDK's httpx - # module (httpx2 on mcp >= 2.0) since the SDK sends its own Request - # objects through it — see sdk_httpx(). - httpx = _core.sdk_httpx() - - _original_url = httpx.URL(url) - - _strip_auth_on_cross_origin_redirect = _make_redirect_header_stripper( - _original_url, - strict=_strict_cfg_headers, - configured_header_names=_configured_header_names, - ) - - client_kwargs: dict = { - "follow_redirects": True, - "timeout": httpx.Timeout(float(connect_timeout), read=300.0), - "verify": ssl_verify, - "event_hooks": {"response": [_strip_auth_on_cross_origin_redirect]}, - } - if headers: - client_kwargs["headers"] = headers - if _oauth_auth is not None: - client_kwargs["auth"] = _oauth_auth - if client_cert is not None: - client_kwargs["cert"] = client_cert - - # Caller owns the client lifecycle — the SDK skips cleanup when - # http_client is provided. - try: - async with httpx.AsyncClient(**client_kwargs) as http_client: - # Unpacked positionally: mcp 1.x yields (read, write, - # get_session_id), 2.x yields (read, write). - async with _core.streamable_http_client(url, http_client=http_client) as _streams: - read_stream, write_stream = _streams[0], _streams[1] - async with _core.ClientSession(read_stream, write_stream, **sampling_kwargs) as session: - reason = await self._serve_session( - session, float(connect_timeout), "HTTP" - ) - except BaseExceptionGroup as _eg: - # Transport TaskGroup dropped: reconnect instead of backoff/park. - reason = self._reconnect_or_reraise_group(_eg) - return reason + transport = self._sse_transport(url, headers, connect_timeout, ssl_verify, + client_cert, oauth_auth, strict_cfg_headers) + label = "SSE" else: - # Deprecated API (mcp < 1.24.0): the SDK owns the httpx client. - if _strict_cfg_headers: - # Fail closed: without an owned client we cannot hook redirects, - # so the cross-origin header boundary cannot be enforced. - raise ImportError( - f"MCP server '{self.name}' requires mcp >= 1.24.0 to " - "enforce the portable redirect-header boundary " - "(strict_redirect_headers). Upgrade the mcp package." - ) - _http_kwargs: dict = { - "headers": headers, - "timeout": float(connect_timeout), - "verify": ssl_verify, - } - if _oauth_auth is not None: - _http_kwargs["auth"] = _oauth_auth - try: - async with _core.streamablehttp_client(url, **_http_kwargs) as ( - read_stream, write_stream, _get_session_id, - ): - async with _core.ClientSession(read_stream, write_stream, **sampling_kwargs) as session: - reason = await self._serve_session( - session, float(connect_timeout), "legacy HTTP" - ) - except BaseExceptionGroup as _eg: - # Transport TaskGroup dropped: reconnect instead of backoff/park. - reason = self._reconnect_or_reraise_group(_eg) - return reason + transport = self._streamable_http_transport( + url, headers, connect_timeout, ssl_verify, client_cert, oauth_auth, + strict_cfg_headers, configured_header_names, + ) + label = "HTTP" if _core._MCP_NEW_HTTP else "legacy HTTP" + return await self._serve_transport(transport, label, float(connect_timeout)) + + # -------------------------------------------------------------- discovery async def _discover_tools(self): """Discover tools from the connected session.