forked from modelcontextprotocol/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sse_disconnect.py
More file actions
53 lines (43 loc) · 1.55 KB
/
test_sse_disconnect.py
File metadata and controls
53 lines (43 loc) · 1.55 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
import asyncio
from uuid import UUID
import pytest
from starlette.types import Message, Scope
from mcp.server.sse import SseServerTransport
@pytest.mark.anyio
async def test_sse_disconnect_handle():
transport = SseServerTransport(endpoint="/sse")
# Create a minimal ASGI scope for an HTTP GET request
scope: Scope = {
"type": "http",
"method": "GET",
"path": "/sse",
"headers": [],
}
send_disconnect = False
# Dummy receive and send functions
async def receive() -> dict:
nonlocal send_disconnect
if not send_disconnect:
send_disconnect = True
return {"type": "http.request"}
else:
return {"type": "http.disconnect"}
async def send(message: Message) -> None:
await asyncio.sleep(0)
# Run the connect_sse context manager
async with transport.connect_sse(scope, receive, send) as (
read_stream,
write_stream,
):
# Assert that streams are provided
assert read_stream is not None
assert write_stream is not None
# There should be exactly one session
assert len(transport._read_stream_writers) == 1
# Check that the session key is a UUID
session_id = next(iter(transport._read_stream_writers.keys()))
assert isinstance(session_id, UUID)
# Check that the session_id should be clean up
assert session_id not in transport._read_stream_writers
# After context exits, session should be cleaned up
assert len(transport._read_stream_writers) == 0