MCPcopy
hub / github.com/modelcontextprotocol/python-sdk / test_request_context_isolation

Function test_request_context_isolation

tests/shared/test_sse.py:321–345  ·  view source on GitHub ↗

Each SSE connection's handlers see only that connection's request headers.

()

Source from the content-addressed store, hash-verified

319
320@pytest.mark.anyio
321async def test_request_context_isolation() -> None:
322 """Each SSE connection's handlers see only that connection's request headers."""
323 factory = in_process_client_factory(make_context_server_app())
324 contexts: list[dict[str, Any]] = []
325
326 # Connect three clients in turn, each with its own headers.
327 for i in range(3):
328 headers = {"X-Request-Id": f"request-{i}", "X-Custom-Value": f"value-{i}"}
329
330 async with sse_client(f"{BASE_URL}/sse", httpx_client_factory=factory, headers=headers) as streams:
331 async with ClientSession(*streams) as session:
332 await session.initialize()
333
334 tool_result = await session.call_tool("echo_context", {"request_id": f"request-{i}"})
335
336 assert len(tool_result.content) == 1
337 content = tool_result.content[0]
338 assert isinstance(content, TextContent)
339 contexts.append(json.loads(content.text))
340
341 assert len(contexts) == 3
342 for i, ctx in enumerate(contexts):
343 assert ctx["request_id"] == f"request-{i}"
344 assert ctx["headers"].get("x-request-id") == f"request-{i}"
345 assert ctx["headers"].get("x-custom-value") == f"value-{i}"
346
347
348def test_sse_message_id_coercion() -> None:

Callers

nothing calls this directly

Calls 6

sse_clientFunction · 0.90
ClientSessionClass · 0.90
make_context_server_appFunction · 0.85
initializeMethod · 0.45
call_toolMethod · 0.45

Tested by

no test coverage detected