|
| 1 | +import contextlib |
| 2 | + |
| 3 | +import anyio |
| 4 | +import httpx |
| 5 | +import pytest |
| 6 | +from httpx_sse import ServerSentEvent |
| 7 | + |
| 8 | +from mcp.client.streamable_http import RequestContext, StreamableHTTPTransport |
| 9 | +from mcp.shared.message import ClientMessageMetadata, SessionMessage |
| 10 | +from mcp.types import JSONRPCRequest |
| 11 | + |
| 12 | + |
| 13 | +class _RaiseEventSource: |
| 14 | + def __init__(self, response: httpx.Response) -> None: |
| 15 | + self.response = response |
| 16 | + |
| 17 | + async def aiter_sse(self): |
| 18 | + yield ServerSentEvent(event="message", data="", id=None, retry=None) |
| 19 | + raise RuntimeError("boom") |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.anyio |
| 23 | +async def test_handle_sse_response_closes_response_on_exception(monkeypatch: pytest.MonkeyPatch) -> None: |
| 24 | + closed = False |
| 25 | + |
| 26 | + async def spy_aclose() -> None: |
| 27 | + nonlocal closed |
| 28 | + closed = True |
| 29 | + |
| 30 | + response = httpx.Response(200, headers={"content-type": "text/event-stream"}) |
| 31 | + response.aclose = spy_aclose # type: ignore[method-assign] |
| 32 | + |
| 33 | + monkeypatch.setattr("mcp.client.streamable_http.EventSource", _RaiseEventSource) |
| 34 | + |
| 35 | + send_stream, receive_stream = anyio.create_memory_object_stream[SessionMessage | Exception](1) |
| 36 | + async with send_stream, receive_stream: |
| 37 | + transport = StreamableHTTPTransport("http://example.invalid/mcp") |
| 38 | + async with httpx.AsyncClient(transport=httpx.MockTransport(lambda _: httpx.Response(200))) as client: |
| 39 | + ctx = RequestContext( |
| 40 | + client=client, |
| 41 | + session_id=None, |
| 42 | + session_message=SessionMessage(JSONRPCRequest(method="initialize", params={}, jsonrpc="2.0", id=1)), |
| 43 | + metadata=ClientMessageMetadata(), |
| 44 | + read_stream_writer=send_stream, |
| 45 | + ) |
| 46 | + await transport._handle_sse_response(response, ctx) |
| 47 | + |
| 48 | + assert closed |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.anyio |
| 52 | +async def test_handle_resumption_request_closes_response_when_aconnect_sse_raises( |
| 53 | + monkeypatch: pytest.MonkeyPatch, |
| 54 | +) -> None: |
| 55 | + @contextlib.asynccontextmanager |
| 56 | + async def fake_aconnect_sse(*_args, **_kwargs): |
| 57 | + raise RuntimeError("connect failed") |
| 58 | + yield |
| 59 | + |
| 60 | + monkeypatch.setattr("mcp.client.streamable_http.aconnect_sse", fake_aconnect_sse) |
| 61 | + |
| 62 | + send_stream, receive_stream = anyio.create_memory_object_stream[SessionMessage | Exception](1) |
| 63 | + async with send_stream, receive_stream: |
| 64 | + transport = StreamableHTTPTransport("http://example.invalid/mcp") |
| 65 | + metadata = ClientMessageMetadata(resumption_token="1") |
| 66 | + async with httpx.AsyncClient(transport=httpx.MockTransport(lambda _: httpx.Response(200))) as client: |
| 67 | + ctx = RequestContext( |
| 68 | + client=client, |
| 69 | + session_id=None, |
| 70 | + session_message=SessionMessage(JSONRPCRequest(method="initialize", params={}, jsonrpc="2.0", id=1)), |
| 71 | + metadata=metadata, |
| 72 | + read_stream_writer=send_stream, |
| 73 | + ) |
| 74 | + |
| 75 | + with pytest.raises(RuntimeError, match="connect failed"): |
| 76 | + await transport._handle_resumption_request(ctx) |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.anyio |
| 80 | +async def test_handle_resumption_request_closes_response_on_exception(monkeypatch: pytest.MonkeyPatch) -> None: |
| 81 | + closed = False |
| 82 | + |
| 83 | + async def spy_aclose() -> None: |
| 84 | + nonlocal closed |
| 85 | + closed = True |
| 86 | + |
| 87 | + response = httpx.Response( |
| 88 | + 200, |
| 89 | + headers={"content-type": "text/event-stream"}, |
| 90 | + request=httpx.Request("GET", "http://example.invalid/mcp"), |
| 91 | + ) |
| 92 | + response.aclose = spy_aclose # type: ignore[method-assign] |
| 93 | + |
| 94 | + @contextlib.asynccontextmanager |
| 95 | + async def fake_aconnect_sse(*_args, **_kwargs): |
| 96 | + yield _RaiseEventSource(response) |
| 97 | + |
| 98 | + monkeypatch.setattr("mcp.client.streamable_http.aconnect_sse", fake_aconnect_sse) |
| 99 | + |
| 100 | + send_stream, receive_stream = anyio.create_memory_object_stream[SessionMessage | Exception](1) |
| 101 | + async with send_stream, receive_stream: |
| 102 | + transport = StreamableHTTPTransport("http://example.invalid/mcp") |
| 103 | + metadata = ClientMessageMetadata(resumption_token="1") |
| 104 | + async with httpx.AsyncClient(transport=httpx.MockTransport(lambda _: httpx.Response(200))) as client: |
| 105 | + ctx = RequestContext( |
| 106 | + client=client, |
| 107 | + session_id=None, |
| 108 | + session_message=SessionMessage(JSONRPCRequest(method="initialize", params={}, jsonrpc="2.0", id=1)), |
| 109 | + metadata=metadata, |
| 110 | + read_stream_writer=send_stream, |
| 111 | + ) |
| 112 | + |
| 113 | + with pytest.raises(RuntimeError, match="boom"): |
| 114 | + await transport._handle_resumption_request(ctx) |
| 115 | + |
| 116 | + assert closed |
0 commit comments