From ebc0a4a8317fadc4eef242b44e9364c301af71ff Mon Sep 17 00:00:00 2001 From: Devansh Date: Thu, 22 Oct 2026 12:18:55 +0530 Subject: [PATCH] fix(mcp): send tool-call stream headers once the call is authorized OAuthScopeResponse holds a tools/call response's http.response.start so a managed OAuth scope challenge can replace it with a 403. Once the call was allowed, the held start still waited for the next body message, which for a slow tool is the 15s SSE keep-alive ping. Send it as soon as the call is allowed instead. Fixes #46659 --- .../mcp_server/scope_challenge.py | 17 +++++++++++++- .../test_mcp_oauth_passthrough_tools.py | 22 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/_experimental/mcp_server/scope_challenge.py b/litellm/proxy/_experimental/mcp_server/scope_challenge.py index c326418bee7..ab2cdeb772b 100644 --- a/litellm/proxy/_experimental/mcp_server/scope_challenge.py +++ b/litellm/proxy/_experimental/mcp_server/scope_challenge.py @@ -36,6 +36,7 @@ class OAuthScopeResponse: self._start: Message | None = None self._challenge: MCPUpstreamAuthError | None = None self._refused = False + self._release: asyncio.Task[None] | None = None @property def pending(self) -> bool: @@ -43,6 +44,16 @@ class OAuthScopeResponse: def allow(self) -> None: self._ready.set() + if self._start is not None and self._release is None: + self._release = asyncio.create_task(self._release_start()) + + async def _release_start(self) -> None: + """Send the held headers once nothing can replace them, without waiting for the first body chunk.""" + async with self._lock: + if self._start is not None and self._challenge is None and not self._refused: + start: Final = self._start + self._start = None + await self._send(start) def deny(self, error: MCPUpstreamAuthError) -> None: if self.pending and error.status_code == 403 and error.required_scope is not None: @@ -53,9 +64,13 @@ class OAuthScopeResponse: if message["type"] == "http.response.start": self._start = message if message["status"] != 200: - self.allow() + self._ready.set() + if not self.pending: + await self._release_start() return await self._ready.wait() + if self._release is not None: + await self._release async with self._lock: if self._refused: return diff --git a/tests/unit/proxy/_experimental/mcp_server/test_mcp_oauth_passthrough_tools.py b/tests/unit/proxy/_experimental/mcp_server/test_mcp_oauth_passthrough_tools.py index b0d53f9c93b..30149302aa8 100644 --- a/tests/unit/proxy/_experimental/mcp_server/test_mcp_oauth_passthrough_tools.py +++ b/tests/unit/proxy/_experimental/mcp_server/test_mcp_oauth_passthrough_tools.py @@ -611,6 +611,28 @@ async def test_scope_middleware_releases_waiting_response_on_validation_or_cance assert send.await_args_list[-1].args[0]["body"] == b"error" +@pytest.mark.asyncio +@pytest.mark.parametrize("allowed_before_start", [False, True]) +async def test_scope_authorization_sends_stream_headers_before_the_first_body_chunk(allowed_before_start): + from litellm.proxy._experimental.mcp_server.scope_challenge import OAuthScopeResponse + + send = AsyncMock() + gate = OAuthScopeResponse(send, "https://gateway.example", "/mcp/docs") + start = {"type": "http.response.start", "status": 200, "headers": [(b"content-type", b"text/event-stream")]} + if allowed_before_start: + gate.allow() + await gate(start) + if not allowed_before_start: + send.assert_not_awaited() + gate.allow() + await asyncio.sleep(0) + # A long-running tool sends no body chunk for a while, so the headers must not wait for one. + send.assert_awaited_once_with(start) + await gate({"type": "http.response.body", "body": b"event: message\n", "more_body": True}) + assert send.await_args_list[-1].args[0]["body"] == b"event: message\n" + assert send.await_count == 2 + + @pytest.mark.asyncio async def test_scope_response_preserves_transport_validation_errors(): from litellm.proxy._experimental.mcp_server.scope_challenge import OAuthScopeResponse