{{ message }}
Repository navigation
Expand file tree
/
Copy pathtest_streamable_http.py
More file actions
1281 lines (1084 loc) · 56.8 KB
/
Copy pathtest_streamable_http.py
File metadata and controls
1281 lines (1084 loc) · 56.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Tests for the streamable-HTTP client transport.
The full client<->server round trip is also pinned by the interaction suite under
tests/interaction/transports/. Tests here cover SSE event sizes and HTTP-seam details
that the public client never exposes.
"""
import base64
import json
from collections.abc import AsyncIterator, Callable, Mapping
from typing import Any
import anyio
import httpx2
import pytest
from inline_snapshot import snapshot
from mcp_types import (
CLIENT_CAPABILITIES_META_KEY,
CLIENT_INFO_META_KEY,
CONNECTION_CLOSED,
INVALID_REQUEST,
METHOD_NOT_FOUND,
PROTOCOL_VERSION_META_KEY,
CallToolRequestParams,
CallToolResult,
JSONRPCError,
JSONRPCNotification,
JSONRPCRequest,
JSONRPCResponse,
ListToolsResult,
PaginatedRequestParams,
TextContent,
Tool,
)
from mcp_types.version import LATEST_MODERN_VERSION
from starlette.applications import Starlette
from starlette.routing import Mount
from starlette.types import Receive, Scope, Send
from mcp import Client, MCPError
from mcp.client.streamable_http import (
MAX_RECONNECTION_ATTEMPTS,
RequestContext,
StreamableHTTPTransport,
streamable_http_client,
)
from mcp.server import Server, ServerRequestContext
from mcp.server._streamable_http_modern import handle_modern_request
from mcp.server.streamable_http_manager import StreamableHTTPSessionManager
from mcp.server.subscriptions import InMemorySubscriptionBus, ListenHandler, ServerEvent
from mcp.shared._context_streams import ContextSendStream, create_context_streams
from mcp.shared.dispatcher import CallOptions, DispatchContext
from mcp.shared.inbound import MCP_METHOD_HEADER, MCP_PROTOCOL_VERSION_HEADER, encode_header_value
from mcp.shared.jsonrpc_dispatcher import JSONRPCDispatcher
from mcp.shared.message import ClientMessageMetadata, ServerMessageMetadata, SessionMessage
from mcp.shared.transport_context import TransportContext
from tests.interaction.transports import StreamingASGITransport
from tests.shared.test_dispatcher import Recorder, echo_handlers
@pytest.mark.anyio
async def test_configured_sse_limit_accepts_a_tool_result_larger_than_one_mebibyte() -> None:
"""SDK-defined: a larger SSE limit accepts a large tool result sent as one POST SSE event."""
payload = "x" * (2 * 1024 * 1024)
async def list_tools(ctx: ServerRequestContext, params: PaginatedRequestParams | None) -> ListToolsResult:
return ListToolsResult(tools=[Tool(name="large", input_schema={"type": "object"})])
async def call_tool(ctx: ServerRequestContext, params: CallToolRequestParams) -> CallToolResult:
assert params.name == "large"
return CallToolResult(content=[TextContent(text=payload)])
manager = StreamableHTTPSessionManager(app=Server("large-result", on_list_tools=list_tools, on_call_tool=call_tool))
app = Starlette(routes=[Mount("/mcp", app=manager.handle_request)])
with anyio.fail_after(5):
async with (
manager.run(),
httpx2.AsyncClient(transport=StreamingASGITransport(app)) as http,
Client(
streamable_http_client("http://localhost/mcp", http_client=http, max_sse_event_size=4 * 1024 * 1024),
mode="legacy",
) as client,
):
result = await client.call_tool("large", {})
assert result.content == [TextContent(text=payload)]
@pytest.mark.anyio
async def test_default_sse_limit_fails_one_large_tool_call_and_keeps_the_session_usable() -> None:
"""SDK-defined: the 1 MiB default rejects one oversized tool result without poisoning the session."""
calls: list[str] = []
async def list_tools(ctx: ServerRequestContext, params: PaginatedRequestParams | None) -> ListToolsResult:
return ListToolsResult(tools=[Tool(name=name, input_schema={"type": "object"}) for name in ("large", "small")])
async def call_tool(ctx: ServerRequestContext, params: CallToolRequestParams) -> CallToolResult:
calls.append(params.name)
return CallToolResult(content=[TextContent(text="x" * (2 * 1024 * 1024) if params.name == "large" else "ok")])
manager = StreamableHTTPSessionManager(
app=Server("bounded-result", on_list_tools=list_tools, on_call_tool=call_tool)
)
app = Starlette(routes=[Mount("/mcp", app=manager.handle_request)])
with anyio.fail_after(5):
async with (
manager.run(),
httpx2.AsyncClient(transport=StreamingASGITransport(app)) as http,
Client(streamable_http_client("http://localhost/mcp", http_client=http), mode="legacy") as client,
):
with pytest.raises(MCPError) as exc_info:
await client.call_tool("large", {})
result = await client.call_tool("small", {})
assert exc_info.value.error.code == CONNECTION_CLOSED
# httpx2 owns the error detail after this prefix.
assert exc_info.value.error.message.startswith("SSE stream failed: ")
assert result.content == [TextContent(text="ok")]
assert calls == ["large", "small"]
@pytest.mark.anyio
async def test_configured_sse_limit_accepts_a_large_get_notification() -> None:
"""SDK-defined: a configured limit accepts a large standalone GET event.
The raw peer forces the notification onto GET, a transport detail hidden by the typed client.
"""
payload = "x" * (2 * 1024 * 1024)
notification = json.dumps({"jsonrpc": "2.0", "method": "notifications/message", "params": {"data": payload}})
def handler(request: httpx2.Request) -> httpx2.Response:
if request.method == "GET":
return httpx2.Response(
200, content=f"data: {notification}\n\n".encode(), headers={"content-type": "text/event-stream"}
)
body = json.loads(request.content)
if body.get("method") == "initialize":
return httpx2.Response(
200, json={"jsonrpc": "2.0", "id": body["id"], "result": {}}, headers={"mcp-session-id": "s1"}
)
return httpx2.Response(202)
with anyio.fail_after(5):
async with (
httpx2.AsyncClient(transport=httpx2.MockTransport(handler)) as http,
streamable_http_client(
"http://test/mcp", http_client=http, terminate_on_close=False, max_sse_event_size=4 * 1024 * 1024
) as (
read,
write,
),
):
await write.send(SessionMessage(JSONRPCRequest(jsonrpc="2.0", id=1, method="initialize", params={})))
await read.receive()
await write.send(SessionMessage(JSONRPCNotification(jsonrpc="2.0", method="notifications/initialized")))
received = await read.receive()
assert isinstance(received, SessionMessage)
assert isinstance(received.message, JSONRPCNotification)
assert received.message.method == "notifications/message"
assert received.message.params == {"data": payload}
class _ParkedSSEStream(httpx2.AsyncByteStream):
"""An SSE response body that emits one frame, then parks until closed.
`opened` fires when the body is consumed; `closed` fires when httpx2 releases it.
"""
def __init__(self, content: bytes = b": parked\n\n") -> None:
self.opened = anyio.Event()
self.closed = anyio.Event()
self._release = anyio.Event()
self._content = content
async def __aiter__(self) -> AsyncIterator[bytes]:
self.opened.set()
yield self._content
await self._release.wait()
async def aclose(self) -> None:
self.closed.set()
self._release.set()
@pytest.mark.anyio
async def test_oversized_get_event_reconnects_and_delivers_later_messages() -> None:
"""SDK-defined: a bad GET event does not prevent later server messages or POSTs.
The raw peer forces a bad GET followed by a fresh GET stream, which the typed client cannot schedule.
"""
gets: list[httpx2.Request] = []
def handler(request: httpx2.Request) -> httpx2.Response:
if request.method == "GET":
gets.append(request)
if len(gets) == 1:
content = b'retry: 0\n\ndata: {"jsonrpc":"2.0","method":"notifications/message","params":{"data":"'
content += b"x" * 2048 + b'"}}\n\n'
return httpx2.Response(200, content=content, headers={"content-type": "text/event-stream"})
return httpx2.Response(
200,
stream=_ParkedSSEStream(
b'data: {"jsonrpc":"2.0","method":"notifications/message","params":{"data":"ok"}}\n\n'
),
headers={"content-type": "text/event-stream"},
)
body = json.loads(request.content)
if body.get("method") == "initialize":
return httpx2.Response(
200, json={"jsonrpc": "2.0", "id": body["id"], "result": {}}, headers={"mcp-session-id": "s1"}
)
if "id" in body:
return httpx2.Response(200, json={"jsonrpc": "2.0", "id": body["id"], "result": {}})
return httpx2.Response(202)
with anyio.fail_after(5):
async with (
httpx2.AsyncClient(transport=httpx2.MockTransport(handler)) as http,
streamable_http_client(
"http://test/mcp", http_client=http, terminate_on_close=False, max_sse_event_size=1024
) as (read, write),
):
await write.send(SessionMessage(JSONRPCRequest(jsonrpc="2.0", id=1, method="initialize", params={})))
await read.receive()
await write.send(SessionMessage(JSONRPCNotification(jsonrpc="2.0", method="notifications/initialized")))
notification = await read.receive()
await write.send(SessionMessage(JSONRPCRequest(jsonrpc="2.0", id=2, method="tools/list", params={})))
reply = await read.receive()
assert isinstance(notification, SessionMessage)
assert isinstance(notification.message, JSONRPCNotification)
assert notification.message.params == {"data": "ok"}
assert isinstance(reply, SessionMessage)
assert isinstance(reply.message, JSONRPCResponse)
assert reply.message.id == 2
assert len(gets) == 2
@pytest.mark.anyio
@pytest.mark.parametrize("limit", [0, -1])
async def test_streamable_http_rejects_nonpositive_sse_event_limits(limit: int) -> None:
"""SDK-defined: a nonpositive per-event byte limit is rejected before any HTTP request is sent."""
with pytest.raises(ValueError) as exc_info:
async with streamable_http_client("http://test/mcp", max_sse_event_size=limit):
raise NotImplementedError
assert str(exc_info.value) == snapshot("max_sse_event_size must be positive or None")
@pytest.mark.anyio
async def test_none_disables_the_sse_event_limit() -> None:
"""SDK-defined: opting out accepts a POST SSE response larger than the 1 MiB default.
The raw peer forces SSE for the request; the typed server can choose a JSON response instead.
"""
payload = "x" * (2 * 1024 * 1024)
def handler(request: httpx2.Request) -> httpx2.Response:
body = json.loads(request.content)
return httpx2.Response(
200,
content=f'data: {{"jsonrpc":"2.0","id":{body["id"]},"result":{{"text":"{payload}"}}}}\n\n'.encode(),
headers={"content-type": "text/event-stream"},
)
with anyio.fail_after(5):
async with (
httpx2.AsyncClient(transport=httpx2.MockTransport(handler)) as http,
streamable_http_client("http://test/mcp", http_client=http, max_sse_event_size=None) as (read, write),
):
await write.send(SessionMessage(JSONRPCRequest(jsonrpc="2.0", id=1, method="tools/list", params={})))
reply = await read.receive()
assert isinstance(reply, SessionMessage)
assert isinstance(reply.message, JSONRPCResponse)
assert reply.message.result == {"text": payload}
@pytest.mark.anyio
async def test_configured_sse_limit_fails_an_oversized_resumption_event() -> None:
"""SDK-defined: an oversized resumption event fails its request.
The raw peer sends an oversized replay regardless of token state, which a typed server cannot force.
"""
seen: list[str] = []
def handler(request: httpx2.Request) -> httpx2.Response:
seen.append(request.method)
return httpx2.Response(
200,
content=b'data: {"jsonrpc":"2.0","id":1,"result":{"text":"' + b"x" * 2048 + b'"}}\n\n',
headers={"content-type": "text/event-stream"},
)
with anyio.fail_after(5):
async with (
httpx2.AsyncClient(transport=httpx2.MockTransport(handler)) as http,
streamable_http_client("http://test/mcp", http_client=http, max_sse_event_size=1024) as (read, write),
):
await write.send(
SessionMessage(
JSONRPCRequest(jsonrpc="2.0", id=1, method="tools/call", params={}),
metadata=ClientMessageMetadata(resumption_token="evt-1"),
)
)
reply = await read.receive()
assert isinstance(reply, SessionMessage)
assert isinstance(reply.message, JSONRPCError)
assert reply.message.error.code == CONNECTION_CLOSED
# httpx2 owns the error detail after this prefix.
assert reply.message.error.message.startswith("SSE stream failed: ")
assert seen == ["GET"]
@pytest.mark.anyio
async def test_configured_sse_limit_does_not_replay_an_oversized_reconnected_event() -> None:
"""SDK-defined: a reconnect that receives an oversized event fails once; replay cannot make it smaller.
The raw peer forces a priming event followed by an oversized replay, which a typed server cannot schedule exactly.
"""
seen: list[str] = []
def handler(request: httpx2.Request) -> httpx2.Response:
seen.append(request.method)
if request.method == "POST":
return httpx2.Response(
200, content=b"id: evt-1\nretry: 0\n\n", headers={"content-type": "text/event-stream"}
)
assert request.headers["last-event-id"] == "evt-1"
return httpx2.Response(
200,
content=b'data: {"jsonrpc":"2.0","id":1,"result":{"text":"' + b"x" * 2048 + b'"}}\n\n',
headers={"content-type": "text/event-stream"},
)
with anyio.fail_after(5):
async with (
httpx2.AsyncClient(transport=httpx2.MockTransport(handler)) as http,
streamable_http_client("http://test/mcp", http_client=http, max_sse_event_size=1024) as (read, write),
):
await write.send(SessionMessage(JSONRPCRequest(jsonrpc="2.0", id=1, method="tools/call", params={})))
reply = await read.receive()
assert isinstance(reply, SessionMessage)
assert isinstance(reply.message, JSONRPCError)
assert reply.message.error.code == CONNECTION_CLOSED
# httpx2 owns the error detail after this prefix.
assert reply.message.error.message.startswith("SSE stream failed: ")
assert seen == ["POST", "GET"]
@pytest.mark.anyio
async def test_non_sse_reconnection_response_retries_before_failing_the_request() -> None:
"""SDK-defined: a temporary non-SSE reconnect response does not discard the request.
The raw peer forces a non-SSE GET followed by a valid replay, which the typed server cannot produce.
"""
seen: list[str] = []
def handler(request: httpx2.Request) -> httpx2.Response:
seen.append(request.method)
if request.method == "POST":
return httpx2.Response(
200, content=b"id: evt-1\nretry: 0\n\n", headers={"content-type": "text/event-stream"}
)
assert request.headers["last-event-id"] == "evt-1"
if len(seen) == 2:
return httpx2.Response(200, content=b"temporary proxy response", headers={"content-type": "text/plain"})
return httpx2.Response(
200,
content=b'data: {"jsonrpc":"2.0","id":1,"result":{}}\n\n',
headers={"content-type": "text/event-stream"},
)
with anyio.fail_after(5):
async with (
httpx2.AsyncClient(transport=httpx2.MockTransport(handler)) as http,
streamable_http_client("http://test/mcp", http_client=http, max_sse_event_size=1024) as (read, write),
):
await write.send(SessionMessage(JSONRPCRequest(jsonrpc="2.0", id=1, method="tools/call", params={})))
reply = await read.receive()
assert isinstance(reply, SessionMessage)
assert isinstance(reply.message, JSONRPCResponse)
assert reply.message.id == 1
assert seen == ["POST", "GET", "GET"]
@pytest.mark.parametrize(
("raw", "expected", "wrapped"),
[
("add", snapshot("add"), False),
("", snapshot(""), False),
("tool with spaces", snapshot("tool with spaces"), False),
(" add", snapshot("=?base64?IGFkZA==?="), True),
("add ", snapshot("=?base64?YWRkIA==?="), True),
("résumé", snapshot("=?base64?csOpc3Vtw6k=?="), True),
("a\r\nb", snapshot("=?base64?YQ0KYg==?="), True),
("=?base64?Zm9v?=", snapshot("=?base64?PT9iYXNlNjQ/Wm05dj89?="), True),
],
)
def test_mcp_name_header_values_are_base64_wrapped_when_unsafe_for_an_http_field(
raw: str, expected: str, wrapped: bool
) -> None:
"""Printable-ASCII names pass verbatim; CR/LF, non-ASCII, edge-whitespace, and sentinel-shaped names are wrapped.
The ``=?base64?...?=`` sentinel is the spec's RFC 7230 safety gate for the ``Mcp-Name`` header.
Wrapped values round-trip through base64 so the server can recover the original name. A leading
or trailing space is wrapped because RFC 7230 forbids it in field-values (h11 rejects on real
transports); an empty value is allowed and passes verbatim.
"""
encoded = encode_header_value(raw)
assert encoded == expected
if wrapped:
assert encoded.startswith("=?base64?") and encoded.endswith("?=")
assert base64.b64decode(encoded.removeprefix("=?base64?").removesuffix("?=")).decode() == raw
else:
assert encoded == raw
@pytest.mark.anyio
async def test_post_request_merges_per_message_metadata_headers() -> None:
"""`ClientMessageMetadata.headers` on a `SessionMessage` are merged into the outgoing POST headers
(SDK-defined: the headers sidecar is the path the session uses to reach the transport)."""
recorded: list[httpx2.Request] = []
def handler(request: httpx2.Request) -> httpx2.Response:
recorded.append(request)
body = json.loads(request.content)
return httpx2.Response(200, json={"jsonrpc": "2.0", "id": body["id"], "result": {}})
with anyio.fail_after(5):
async with (
httpx2.AsyncClient(transport=httpx2.MockTransport(handler)) as http,
streamable_http_client("http://test/mcp", http_client=http) as (read, write),
):
await write.send(
SessionMessage(
message=JSONRPCRequest(jsonrpc="2.0", id=1, method="tools/list", params={}),
metadata=ClientMessageMetadata(headers={"x-test": "v"}),
)
)
reply = await read.receive()
assert isinstance(reply, SessionMessage)
assert [r.method for r in recorded] == ["POST"]
assert recorded[0].headers["x-test"] == "v"
@pytest.mark.anyio
async def test_pre_session_bare_404_maps_to_method_not_found() -> None:
"""A bare HTTP 404 (no JSON-RPC body) before any session-id is held maps to METHOD_NOT_FOUND.
Gateways and legacy servers 404 at the HTTP layer for unknown methods; with no session yet,
"Session terminated" is meaningless, and the discover→initialize fallback ladder keys on -32601.
"""
def handler(request: httpx2.Request) -> httpx2.Response:
return httpx2.Response(404)
with anyio.fail_after(5):
async with (
httpx2.AsyncClient(transport=httpx2.MockTransport(handler)) as http,
streamable_http_client("http://test/mcp", http_client=http) as (read, write),
):
await write.send(SessionMessage(JSONRPCRequest(jsonrpc="2.0", id=1, method="server/discover", params={})))
reply = await read.receive()
assert isinstance(reply, SessionMessage)
assert isinstance(reply.message, JSONRPCError)
assert reply.message.error.code == METHOD_NOT_FOUND
@pytest.mark.anyio
async def test_initialize_post_clears_cached_pv_header_and_unstamped_posts_read_it() -> None:
"""``initialize`` discards the cached protocol-version header; every other POST reads it.
Steps:
1. A stamped probe POST caches its ``MCP-Protocol-Version`` header.
2. An ``initialize`` POST clears that cache before building headers, so the fallback
handshake never carries a probe-stamped value.
3. A subsequent stamped POST re-seeds the cache with the negotiated version.
4. An unstamped POST (a JSON-RPC response written by the dispatcher, which never
passes through the session's stamp) then reads the cache and carries the
negotiated version — the spec MUST for all post-initialization HTTP requests.
"""
recorded: list[httpx2.Request] = []
def handler(request: httpx2.Request) -> httpx2.Response:
recorded.append(request)
body = json.loads(request.content)
if "id" not in body or "result" in body:
return httpx2.Response(202)
return httpx2.Response(200, json={"jsonrpc": "2.0", "id": body["id"], "result": {}})
with anyio.fail_after(5):
async with (
httpx2.AsyncClient(transport=httpx2.MockTransport(handler)) as http,
streamable_http_client("http://test/mcp", http_client=http) as (read, write),
):
await write.send(
SessionMessage(
message=JSONRPCRequest(jsonrpc="2.0", id=1, method="server/discover", params={}),
metadata=ClientMessageMetadata(headers={MCP_PROTOCOL_VERSION_HEADER: "2026-07-28"}),
)
)
await read.receive()
await write.send(SessionMessage(JSONRPCRequest(jsonrpc="2.0", id=2, method="initialize", params={})))
await read.receive()
await write.send(
SessionMessage(
message=JSONRPCNotification(jsonrpc="2.0", method="notifications/initialized"),
metadata=ClientMessageMetadata(headers={MCP_PROTOCOL_VERSION_HEADER: "2025-11-25"}),
)
)
# An unstamped JSON-RPC response — what the dispatcher writes when answering
# a server-initiated request (sampling/elicitation/roots).
await write.send(SessionMessage(JSONRPCResponse(jsonrpc="2.0", id=99, result={})))
assert [r.method for r in recorded] == ["POST", "POST", "POST", "POST"]
assert recorded[0].headers[MCP_PROTOCOL_VERSION_HEADER] == "2026-07-28"
assert MCP_PROTOCOL_VERSION_HEADER not in recorded[1].headers
assert recorded[2].headers[MCP_PROTOCOL_VERSION_HEADER] == "2025-11-25"
assert recorded[3].headers[MCP_PROTOCOL_VERSION_HEADER] == "2025-11-25"
def _sse_or_ack_handler(
parked: _ParkedSSEStream, posted: list[dict[str, Any]], frame_posted: anyio.Event
) -> Callable[[httpx2.Request], httpx2.Response]:
"""Requests get the parked SSE body; notifications get 202 and set `frame_posted`."""
def handler(request: httpx2.Request) -> httpx2.Response:
body = json.loads(request.content)
posted.append(body)
if "id" in body:
return httpx2.Response(200, headers={"content-type": "text/event-stream"}, stream=parked)
frame_posted.set()
return httpx2.Response(202)
return handler
@pytest.mark.anyio
async def test_modern_cancelled_frame_aborts_the_matching_in_flight_post() -> None:
"""At 2026 an outbound `notifications/cancelled` never POSTs — closing the named
request's response stream IS the wire's cancellation signal — so the transport
aborts the in-flight POST and swallows the frame."""
parked = _ParkedSSEStream()
posted: list[dict[str, Any]] = []
def handler(request: httpx2.Request) -> httpx2.Response:
posted.append(json.loads(request.content))
return httpx2.Response(200, headers={"content-type": "text/event-stream"}, stream=parked)
with anyio.fail_after(5):
async with (
httpx2.AsyncClient(transport=httpx2.MockTransport(handler)) as http,
streamable_http_client("http://test/mcp", http_client=http) as (_read, write),
):
await write.send(
SessionMessage(
message=JSONRPCRequest(jsonrpc="2.0", id="listen-1", method="subscriptions/listen", params={}),
metadata=ClientMessageMetadata(headers={MCP_PROTOCOL_VERSION_HEADER: LATEST_MODERN_VERSION}),
)
)
await parked.opened.wait()
await write.send(
SessionMessage(
JSONRPCNotification(
jsonrpc="2.0", method="notifications/cancelled", params={"requestId": "listen-1"}
)
)
)
await parked.closed.wait()
assert [body["method"] for body in posted] == ["subscriptions/listen"]
@pytest.mark.anyio
@pytest.mark.parametrize("stamped_version", [None, "2025-11-25"], ids=["no-version-yet", "2025-11-25"])
async def test_legacy_cancelled_frame_posts_and_leaves_the_stream_open(stamped_version: str | None) -> None:
"""Below 2026 — or before any stamped POST has revealed the version — the frame is
the spec's cancellation signal: it POSTs, and the request's stream stays open
(a 2025 disconnect is explicitly not a cancel)."""
parked = _ParkedSSEStream()
posted: list[dict[str, Any]] = []
frame_posted = anyio.Event()
handler = _sse_or_ack_handler(parked, posted, frame_posted)
with anyio.fail_after(5):
async with (
httpx2.AsyncClient(transport=httpx2.MockTransport(handler)) as http,
streamable_http_client("http://test/mcp", http_client=http) as (_read, write),
):
metadata = (
ClientMessageMetadata(headers={MCP_PROTOCOL_VERSION_HEADER: stamped_version})
if stamped_version is not None
else None
)
await write.send(
SessionMessage(
message=JSONRPCRequest(jsonrpc="2.0", id=1, method="tools/call", params={}),
metadata=metadata,
)
)
await parked.opened.wait()
await write.send(
SessionMessage(
JSONRPCNotification(jsonrpc="2.0", method="notifications/cancelled", params={"requestId": 1})
)
)
await frame_posted.wait()
# Checked before teardown: exiting the transport cancels the parked POST.
assert not parked.closed.is_set()
assert [body["method"] for body in posted] == ["tools/call", "notifications/cancelled"]
@pytest.mark.anyio
@pytest.mark.parametrize(
"params",
[
pytest.param({"requestId": 999}, id="unknown-id"),
pytest.param({"requestId": True}, id="bool-must-not-alias-request-id-1"),
pytest.param({"requestId": "1"}, id="string-1-must-not-match-int-1"),
pytest.param({}, id="no-request-id"),
pytest.param(None, id="no-params"),
],
)
async def test_modern_cancelled_frames_matching_no_post_are_swallowed(params: dict[str, Any] | None) -> None:
"""At 2026 the frame is swallowed even when it aborts nothing — the wire defines no
client-to-server notifications, so a late cancel racing the response must not leak
a POST — and a mismatched id must not abort someone else's stream."""
parked = _ParkedSSEStream()
posted: list[dict[str, Any]] = []
def handler(request: httpx2.Request) -> httpx2.Response:
body = json.loads(request.content)
posted.append(body)
if body.get("id") == 1:
return httpx2.Response(200, headers={"content-type": "text/event-stream"}, stream=parked)
return httpx2.Response(200, json={"jsonrpc": "2.0", "id": body["id"], "result": {}})
with anyio.fail_after(5):
async with (
httpx2.AsyncClient(transport=httpx2.MockTransport(handler)) as http,
streamable_http_client("http://test/mcp", http_client=http) as (read, write),
):
await write.send(
SessionMessage(
message=JSONRPCRequest(jsonrpc="2.0", id=1, method="subscriptions/listen", params={}),
metadata=ClientMessageMetadata(headers={MCP_PROTOCOL_VERSION_HEADER: LATEST_MODERN_VERSION}),
)
)
await parked.opened.wait()
await write.send(
SessionMessage(JSONRPCNotification(jsonrpc="2.0", method="notifications/cancelled", params=params))
)
# A follow-up request completing proves the loop moved past the swallowed frame.
await write.send(SessionMessage(JSONRPCRequest(jsonrpc="2.0", id=2, method="ping", params={})))
reply = await read.receive()
# Checked before teardown: exiting the transport cancels the parked POST.
assert not parked.closed.is_set()
assert isinstance(reply, SessionMessage)
assert isinstance(reply.message, JSONRPCResponse)
assert reply.message.id == 2
assert [body["method"] for body in posted] == ["subscriptions/listen", "ping"]
@pytest.mark.anyio
async def test_handler_scoped_cancelled_frames_are_translated_at_modern_too() -> None:
"""A cancel carrying `ServerMessageMetadata` (a handler abandoning its own
back-channel request) still names one of OUR outbound ids — every spec-legal
cancel names a request its sender issued — so at 2026 it aborts that POST and
stays off the wire like any other."""
parked = _ParkedSSEStream()
posted: list[dict[str, Any]] = []
frame_posted = anyio.Event()
handler = _sse_or_ack_handler(parked, posted, frame_posted)
with anyio.fail_after(5):
async with (
httpx2.AsyncClient(transport=httpx2.MockTransport(handler)) as http,
streamable_http_client("http://test/mcp", http_client=http) as (_read, write),
):
await write.send(
SessionMessage(
message=JSONRPCRequest(jsonrpc="2.0", id=1, method="tools/call", params={}),
metadata=ClientMessageMetadata(headers={MCP_PROTOCOL_VERSION_HEADER: LATEST_MODERN_VERSION}),
)
)
await parked.opened.wait()
await write.send(
SessionMessage(
message=JSONRPCNotification(
jsonrpc="2.0", method="notifications/cancelled", params={"requestId": 1}
),
metadata=ServerMessageMetadata(related_request_id=99),
)
)
await parked.closed.wait()
assert [body["method"] for body in posted] == ["tools/call"]
assert not frame_posted.is_set()
@pytest.mark.anyio
async def test_cancel_for_a_request_sent_under_2025_still_posts_after_modern_adoption() -> None:
"""The translation follows the era the NAMED request was sent under, not the
cache at cancel time: a request POSTed under 2025 keeps 2025 cancellation
semantics (frame on the wire, stream left open) even after a later message
flips the negotiated version to 2026."""
parked = _ParkedSSEStream()
posted: list[dict[str, Any]] = []
frame_posted = anyio.Event()
def handler(request: httpx2.Request) -> httpx2.Response:
body = json.loads(request.content)
posted.append(body)
if body.get("id") == 1:
return httpx2.Response(200, headers={"content-type": "text/event-stream"}, stream=parked)
if "id" in body:
return httpx2.Response(200, json={"jsonrpc": "2.0", "id": body["id"], "result": {}})
frame_posted.set()
return httpx2.Response(202)
with anyio.fail_after(5):
async with (
httpx2.AsyncClient(transport=httpx2.MockTransport(handler)) as http,
streamable_http_client("http://test/mcp", http_client=http) as (read, write),
):
await write.send(
SessionMessage(
message=JSONRPCRequest(jsonrpc="2.0", id=1, method="tools/call", params={}),
metadata=ClientMessageMetadata(headers={MCP_PROTOCOL_VERSION_HEADER: "2025-11-25"}),
)
)
await parked.opened.wait()
# A modern-stamped request flips the cached negotiated version.
await write.send(
SessionMessage(
message=JSONRPCRequest(jsonrpc="2.0", id=2, method="ping", params={}),
metadata=ClientMessageMetadata(headers={MCP_PROTOCOL_VERSION_HEADER: LATEST_MODERN_VERSION}),
)
)
reply = await read.receive()
assert isinstance(reply, SessionMessage)
assert isinstance(reply.message, JSONRPCResponse)
await write.send(
SessionMessage(
JSONRPCNotification(jsonrpc="2.0", method="notifications/cancelled", params={"requestId": 1})
)
)
await frame_posted.wait()
# Checked before teardown: exiting the transport cancels the parked POST.
assert not parked.closed.is_set()
assert [body["method"] for body in posted] == ["tools/call", "ping", "notifications/cancelled"]
class _SignalingBus(InMemorySubscriptionBus):
"""Signals subscribe/unsubscribe so a test observes the stream lifecycle through
the bus Protocol (the public seam) instead of polling handler internals."""
def __init__(self) -> None:
super().__init__()
self.subscribed = anyio.Event()
self.unsubscribed = anyio.Event()
def subscribe(self, listener: Callable[[ServerEvent], None]) -> Callable[[], None]:
unsubscribe = super().subscribe(listener)
self.subscribed.set()
def unsubscribe_and_signal() -> None:
unsubscribe()
self.unsubscribed.set()
return unsubscribe_and_signal
@pytest.mark.anyio
async def test_scope_cancel_aborts_a_modern_listen_post_end_to_end() -> None:
"""Over a real ASGI bridge: cancelling the caller of a parked `subscriptions/listen`
closes the POST's response stream — the server treats the disconnect as the cancel
and releases the subscription — and no `notifications/cancelled` crosses the wire."""
bus = _SignalingBus()
server = Server("test", on_subscriptions_listen=ListenHandler(bus))
async def app(scope: Scope, receive: Receive, send: Send) -> None:
async with server.lifespan(server) as lifespan_state:
await handle_modern_request(server, None, False, lifespan_state, scope, receive, send)
posted_methods: list[str] = []
async def record_request(request: httpx2.Request) -> None:
posted_methods.append(json.loads(request.content)["method"])
acked = anyio.Event()
async def on_notify(dctx: DispatchContext[TransportContext], method: str, params: Mapping[str, Any] | None) -> None:
assert method == "notifications/subscriptions/acknowledged"
acked.set()
on_request, _ = echo_handlers(Recorder())
with anyio.fail_after(15):
async with (
httpx2.AsyncClient(
transport=StreamingASGITransport(app),
base_url="http://testserver",
event_hooks={"request": [record_request]},
) as http,
streamable_http_client("http://testserver/mcp", http_client=http) as (read, write),
):
dispatcher: JSONRPCDispatcher[TransportContext] = JSONRPCDispatcher(read, write)
async with anyio.create_task_group() as tg: # pragma: no branch
await tg.start(dispatcher.run, on_request, on_notify)
listen_scope = anyio.CancelScope()
async def send_listen() -> None:
params: dict[str, Any] = {
"_meta": {
PROTOCOL_VERSION_META_KEY: LATEST_MODERN_VERSION,
CLIENT_INFO_META_KEY: {"name": "test-client", "version": "0"},
CLIENT_CAPABILITIES_META_KEY: {},
},
"notifications": {"toolsListChanged": True},
}
opts: CallOptions = {
"request_id": "listen-1",
"headers": {
MCP_PROTOCOL_VERSION_HEADER: LATEST_MODERN_VERSION,
MCP_METHOD_HEADER: "subscriptions/listen",
},
}
with listen_scope:
await dispatcher.send_raw_request("subscriptions/listen", params, opts)
tg.start_soon(send_listen)
await acked.wait()
assert bus.subscribed.is_set()
assert not bus.unsubscribed.is_set()
listen_scope.cancel()
await bus.unsubscribed.wait()
tg.cancel_scope.cancel()
assert posted_methods == ["subscriptions/listen"]
class _CompletingSSEStream(httpx2.AsyncByteStream):
"""An SSE body that delivers one JSON-RPC response, then parks in `aclose`.
Holding `aclose` keeps the finished POST task alive past its response, so a
test can re-register the same request id underneath it before releasing.
"""
def __init__(self, response_body: dict[str, Any]) -> None:
self._event = f"data: {json.dumps(response_body)}\n\n".encode()
self.release = anyio.Event()
async def __aiter__(self) -> AsyncIterator[bytes]:
yield self._event
async def aclose(self) -> None:
await self.release.wait()
@pytest.mark.anyio
async def test_a_finished_post_task_does_not_evict_a_reused_ids_new_registration() -> None:
"""Request ids are reusable once resolved; a finished POST task unwinding late
must not pop the successor's registration, or a cancel for the reused id would
find nothing to abort and the live POST would leak past the cancellation."""
completing = _CompletingSSEStream({"jsonrpc": "2.0", "id": "dup-1", "result": {}})
parked = _ParkedSSEStream()
posted: list[dict[str, Any]] = []
streams = [completing, parked]
def handler(request: httpx2.Request) -> httpx2.Response:
posted.append(json.loads(request.content))
return httpx2.Response(200, headers={"content-type": "text/event-stream"}, stream=streams.pop(0))
with anyio.fail_after(5):
async with (
httpx2.AsyncClient(transport=httpx2.MockTransport(handler)) as http,
streamable_http_client("http://test/mcp", http_client=http) as (read, write),
):
modern = ClientMessageMetadata(headers={MCP_PROTOCOL_VERSION_HEADER: LATEST_MODERN_VERSION})
await write.send(
SessionMessage(
message=JSONRPCRequest(jsonrpc="2.0", id="dup-1", method="tools/call", params={}),
metadata=modern,
)
)
reply = await read.receive()
assert isinstance(reply, SessionMessage)
assert isinstance(reply.message, JSONRPCResponse)
# The first task is now parked in `aclose`; reuse its id underneath it.
await write.send(
SessionMessage(
message=JSONRPCRequest(jsonrpc="2.0", id="dup-1", method="subscriptions/listen", params={}),
metadata=modern,
)
)
await parked.opened.wait()
completing.release.set()
await anyio.wait_all_tasks_blocked()
# The successor's registration survived: a cancel still aborts it.
await write.send(
SessionMessage(
JSONRPCNotification(jsonrpc="2.0", method="notifications/cancelled", params={"requestId": "dup-1"})
)
)
await parked.closed.wait()
assert [body["method"] for body in posted] == ["tools/call", "subscriptions/listen"]
class _DyingSSEStream(httpx2.AsyncByteStream):
"""Emits one id-less comment then breaks - a non-resumable stream dropping."""
def __init__(self) -> None:
self.opened = anyio.Event()
async def __aiter__(self) -> AsyncIterator[bytes]:
self.opened.set()
yield b": hello\n\n"
raise httpx2.ReadError("connection reset")
async def aclose(self) -> None:
pass
@pytest.mark.anyio
async def test_a_non_resumable_sse_drop_resolves_the_request_with_an_error() -> None:
"""A per-request SSE stream that dies having carried no event ids can never deliver its
response; the transport resolves the waiter with CONNECTION_CLOSED instead of hanging forever."""
dying = _DyingSSEStream()
def handler(request: httpx2.Request) -> httpx2.Response:
return httpx2.Response(200, headers={"content-type": "text/event-stream"}, stream=dying)
with anyio.fail_after(5):
async with (
httpx2.AsyncClient(transport=httpx2.MockTransport(handler)) as http,
streamable_http_client("http://test/mcp", http_client=http) as (read, write),
):
await write.send(
SessionMessage(JSONRPCRequest(jsonrpc="2.0", id="listen-1", method="subscriptions/listen", params={}))
)
reply = await read.receive()
assert isinstance(reply, SessionMessage)
assert isinstance(reply.message, JSONRPCError)
assert reply.message.id == "listen-1"
assert reply.message.error.code == CONNECTION_CLOSED
class _DeliverOnCommandSSEStream(httpx2.AsyncByteStream):
"""Parks after opening, then delivers one JSON-RPC response when told."""
def __init__(self, response_body: dict[str, Any]) -> None:
self._event = f"data: {json.dumps(response_body)}\n\n".encode()
self.opened = anyio.Event()
self.deliver = anyio.Event()
async def __aiter__(self) -> AsyncIterator[bytes]:
self.opened.set()
await self.deliver.wait()
yield self._event
async def aclose(self) -> None:
pass
@pytest.mark.anyio
async def test_a_superseded_posts_late_real_response_cannot_answer_the_successor() -> None:
"""SDK-defined: re-issuing an id severs the superseded POST, so nothing from its
stream (a late real response, or a synthesized error for its death) can resolve
the reused id's waiter; only the successor's own response arrives."""
stale = _DeliverOnCommandSSEStream({"jsonrpc": "2.0", "id": "dup-1", "result": {"origin": "stale"}})
succeeding = _DeliverOnCommandSSEStream({"jsonrpc": "2.0", "id": "dup-1", "result": {"origin": "fresh"}})
streams: list[httpx2.AsyncByteStream] = [stale, succeeding]
def handler(request: httpx2.Request) -> httpx2.Response:
return httpx2.Response(200, headers={"content-type": "text/event-stream"}, stream=streams.pop(0))
with anyio.fail_after(5):
async with (
httpx2.AsyncClient(transport=httpx2.MockTransport(handler)) as http,
streamable_http_client("http://test/mcp", http_client=http) as (read, write),
):
await write.send(SessionMessage(JSONRPCRequest(jsonrpc="2.0", id="dup-1", method="tools/call", params={})))
await stale.opened.wait()
await write.send(SessionMessage(JSONRPCRequest(jsonrpc="2.0", id="dup-1", method="tools/call", params={})))
await succeeding.opened.wait()
stale.deliver.set()
await anyio.wait_all_tasks_blocked()
succeeding.deliver.set()
reply = await read.receive()
assert isinstance(reply, SessionMessage)
assert isinstance(reply.message, JSONRPCResponse), reply.message
assert reply.message.result == {"origin": "fresh"}
@pytest.mark.anyio
async def test_a_202_to_a_request_resolves_the_waiter_with_an_error() -> None:
You can’t perform that action at this time.
