mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(mcp): preserve stateful sessions after responses
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
parent
6c7e0ee39b
commit
cc6b6a032e
3 changed files with 70 additions and 1 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue