diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index b0ccf74c63b..56d408a796e 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -21868,6 +21868,7 @@ async def _stream_mcp_asgi_response(handle_fn, scope: dict, receive) -> "Streami while True: chunk = await body_queue.get() if chunk is None: + await handler_task break yield chunk finally: diff --git a/tests/unit/proxy/mcp_server/test_mcp_logging.py b/tests/unit/proxy/mcp_server/test_mcp_logging.py index bdf8dff2909..9fc8ac4a1e4 100644 --- a/tests/unit/proxy/mcp_server/test_mcp_logging.py +++ b/tests/unit/proxy/mcp_server/test_mcp_logging.py @@ -15,6 +15,7 @@ from mcp.server.auth.middleware.auth_context import auth_context_var from mcp.shared.exceptions import MCPError from mcp.types import CallToolResult, EmptyResult, Request, RequestParams, TextContent from mcp.types import Tool as MCPTool +from pydantic import ConfigDict import litellm from litellm.caching.caching import DualCache @@ -31,6 +32,12 @@ from litellm.proxy.mcp_server.server import ( from litellm.types.mcp import MCPPostCallResponseObject +class SDKRejectionParams(RequestParams): + model_config = ConfigDict(frozen=True) + name: int | None = None + arguments: dict[str, str] | None = None + + class SDKRejectionLogger(CustomLogger): def __init__(self) -> None: super().__init__() @@ -93,7 +100,9 @@ async def test_sdk_rejection_emits_one_redacted_zero_cost_failure( ) -> None: async with Client(server, mode="legacy") as client: with pytest.raises(MCPError) as rejected: - await client.session.send_request(Request(method=method, params=RequestParams(**params)), EmptyResult) + await client.session.send_request( + Request(method=method, params=SDKRejectionParams.model_validate(params)), EmptyResult + ) assert rejected.value.code == code assert rejected.value.message == message await client.session.send_ping() diff --git a/tests/unit/proxy/test_mcp_asgi_response.py b/tests/unit/proxy/test_mcp_asgi_response.py index d030f65af4b..a9e884d680b 100644 --- a/tests/unit/proxy/test_mcp_asgi_response.py +++ b/tests/unit/proxy/test_mcp_asgi_response.py @@ -1,11 +1,70 @@ import asyncio +from typing import Final import pytest from fastapi import HTTPException +from starlette.types import Message, Receive, Scope, Send from litellm.proxy.proxy_server import _stream_mcp_asgi_response +@pytest.mark.asyncio +async def test_completed_response_finishes_handler_cleanup(): + body_consumed: Final = asyncio.Event() + completed: Final = asyncio.Event() + + async def handle_fn(_scope: Scope, _receive: Receive, send: Send) -> None: + await send({"type": "http.response.start", "status": 200, "headers": []}) + await send({"type": "http.response.body", "body": b"initialized", "more_body": False}) + await body_consumed.wait() + completed.set() + + async def receive() -> Message: + return {"type": "http.request", "body": b"", "more_body": False} + + response: Final = await _stream_mcp_asgi_response( + handle_fn, {"type": "http", "method": "POST", "path": "/mcp", "headers": []}, receive + ) + body: Final = aiter(response.body_iterator) + assert await anext(body) == b"initialized" + body_consumed.set() + assert await anext(body, None) is None + assert completed.is_set() + + +@pytest.mark.asyncio +async def test_disconnected_response_cancels_handler(): + body_received: Final = asyncio.Event() + cancelled: Final = asyncio.Event() + hold_open: Final = asyncio.Event() + + async def handle_fn(_scope: Scope, _receive: Receive, send: Send) -> None: + try: + await send({"type": "http.response.start", "status": 200, "headers": []}) + await send({"type": "http.response.body", "body": b"event", "more_body": True}) + await hold_open.wait() + finally: + cancelled.set() + + async def receive() -> Message: + return {"type": "http.request", "body": b"", "more_body": False} + + response: Final = await _stream_mcp_asgi_response( + handle_fn, {"type": "http", "method": "POST", "path": "/mcp", "headers": []}, receive + ) + + async def consume() -> None: + async for _ in response.body_iterator: + body_received.set() + + consumer: Final = asyncio.create_task(consume()) + await body_received.wait() + consumer.cancel() + with pytest.raises(asyncio.CancelledError): + await consumer + assert cancelled.is_set() + + @pytest.mark.asyncio async def test_stream_mcp_asgi_response_propagates_pre_header_http_exception(): async def handle_fn(_scope, _receive, _send):