mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
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
This commit is contained in:
parent
f2118fc767
commit
ebc0a4a831
2 changed files with 38 additions and 1 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue