-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathtest_session_id_propagation.py
More file actions
309 lines (255 loc) · 10.6 KB
/
test_session_id_propagation.py
File metadata and controls
309 lines (255 loc) · 10.6 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
"""Tests for session_id propagation through the MCP stack."""
import json
from typing import Any
import pytest
from starlette.types import Message
from mcp.server.fastmcp import Context, FastMCP
from mcp.server.session import ServerSession
from mcp.server.streamable_http_manager import StreamableHTTPSessionManager
@pytest.mark.anyio
async def test_session_id_propagates_to_tool_context():
"""Test that session_id from transport propagates to tool Context."""
# Track session_id seen in tool
captured_session_id: str | None = None
# Create FastMCP server with a tool that captures session_id
mcp = FastMCP("test-session-id-server")
@mcp.tool()
async def get_session_info(ctx: Context[ServerSession, None]) -> dict[str, Any]:
"""Tool that returns session information."""
nonlocal captured_session_id
captured_session_id = ctx.session_id
return {
"session_id": ctx.session_id,
"request_id": ctx.request_id,
}
# Create session manager with JSON response mode for easier testing
manager = StreamableHTTPSessionManager(app=mcp._mcp_server, stateless=False, json_response=True)
async with manager.run():
# Prepare ASGI scope and messages
scope = {
"type": "http",
"method": "POST",
"path": "/mcp",
"headers": [
(b"content-type", b"application/json"),
(b"accept", b"application/json"),
],
}
# Create initialize request
initialize_request = {
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2025-03-26",
"capabilities": {},
"clientInfo": {"name": "test-client", "version": "1.0.0"},
},
}
# Track sent messages
sent_messages: list[Message] = []
receive_calls = 0
session_id_from_header: str | None = None
async def mock_receive():
nonlocal receive_calls
receive_calls += 1
if receive_calls == 1:
# First call: send initialize request
return {
"type": "http.request",
"body": json.dumps(initialize_request).encode(),
"more_body": False,
}
# Subsequent calls: end stream
return {"type": "http.disconnect"}
async def mock_send(message: Message):
sent_messages.append(message)
# Capture session ID from response header
if message["type"] == "http.response.start":
nonlocal session_id_from_header
headers = dict(message.get("headers", []))
if b"mcp-session-id" in headers:
session_id_from_header = headers[b"mcp-session-id"].decode()
# Handle request (initialize)
await manager.handle_request(scope, mock_receive, mock_send)
# Verify session ID was set in response header
assert session_id_from_header is not None, "Session ID should be in response header"
# Now make a tools/call request to test session_id in Context
# Reset for second request
receive_calls = 0
sent_messages.clear()
tool_call_request = {
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {"name": "get_session_info", "arguments": {}},
}
scope_with_session = {
**scope,
"headers": [
*scope["headers"],
(b"mcp-session-id", session_id_from_header.encode()),
],
}
async def mock_receive_tool_call():
nonlocal receive_calls
receive_calls += 1
if receive_calls == 1:
return {
"type": "http.request",
"body": json.dumps(tool_call_request).encode(),
"more_body": False,
}
return {"type": "http.disconnect"}
await manager.handle_request(scope_with_session, mock_receive_tool_call, mock_send)
# Parse the response to check if tool was called successfully
response_body = b""
for msg in sent_messages:
if msg["type"] == "http.response.body":
response_body += msg.get("body", b"")
# Verify we got a response
assert response_body, f"Should have received a response body, got messages: {sent_messages}"
# Decode and parse the response
response_text = response_body.decode()
print(f"Response: {response_text}") # Debug output
# Verify session_id was captured in tool context
assert captured_session_id is not None, (
f"session_id should be available in Context. Response was: {response_text}"
)
assert captured_session_id == session_id_from_header, (
f"session_id in Context ({captured_session_id}) should match "
f"session ID from header ({session_id_from_header})"
)
@pytest.mark.anyio
async def test_session_id_is_none_for_stateless_mode():
"""Test that session_id is None in stateless mode."""
# Track session_id seen in tool
captured_session_id: str | None = "not-set"
# Create FastMCP server
mcp = FastMCP("test-stateless-server")
@mcp.tool()
async def check_session(ctx: Context[ServerSession, None]) -> dict[str, Any]:
"""Tool that checks session_id."""
nonlocal captured_session_id
captured_session_id = ctx.session_id
return {"has_session_id": ctx.session_id is not None}
# Create session manager in stateless mode with JSON response for easier testing
manager = StreamableHTTPSessionManager(app=mcp._mcp_server, stateless=True, json_response=True)
async with manager.run():
scope = {
"type": "http",
"method": "POST",
"path": "/mcp",
"headers": [
(b"content-type", b"application/json"),
(b"accept", b"application/json"),
],
}
initialize_request = {
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2025-03-26",
"capabilities": {},
"clientInfo": {"name": "test-client", "version": "1.0.0"},
},
}
sent_messages: list[Message] = []
receive_calls = 0
async def mock_receive():
nonlocal receive_calls
receive_calls += 1
if receive_calls == 1:
return {
"type": "http.request",
"body": json.dumps(initialize_request).encode(),
"more_body": False,
}
return {"type": "http.disconnect"}
async def mock_send(message: Message):
sent_messages.append(message)
await manager.handle_request(scope, mock_receive, mock_send)
# In stateless mode, session_id should not be set
# (Note: This test primarily verifies no errors occur;
# we can't easily call a tool in stateless mode without a full integration test)
@pytest.mark.anyio
async def test_session_id_consistent_across_requests():
"""Test that session_id remains consistent across multiple requests in same session."""
# Track all session_ids seen
seen_session_ids: list[str | None] = []
# Create FastMCP server
mcp = FastMCP("test-consistency-server")
@mcp.tool()
async def track_session(ctx: Context[ServerSession, None]) -> dict[str, Any]:
"""Tool that tracks session_id."""
seen_session_ids.append(ctx.session_id)
return {"session_id": ctx.session_id, "call_number": len(seen_session_ids)}
# Create session manager with JSON response mode for easier testing
manager = StreamableHTTPSessionManager(app=mcp._mcp_server, stateless=False, json_response=True)
async with manager.run():
# First request: initialize and get session ID
scope = {
"type": "http",
"method": "POST",
"path": "/mcp",
"headers": [
(b"content-type", b"application/json"),
(b"accept", b"application/json"),
],
}
initialize_request = {
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2025-03-26",
"capabilities": {},
"clientInfo": {"name": "test-client", "version": "1.0.0"},
},
}
sent_messages: list[Message] = []
session_id_from_header: str | None = None
async def mock_receive_init():
return {
"type": "http.request",
"body": json.dumps(initialize_request).encode(),
"more_body": False,
}
async def mock_send(message: Message):
sent_messages.append(message)
if message["type"] == "http.response.start":
nonlocal session_id_from_header
headers = dict(message.get("headers", []))
if b"mcp-session-id" in headers:
session_id_from_header = headers[b"mcp-session-id"].decode()
await manager.handle_request(scope, mock_receive_init, mock_send)
assert session_id_from_header is not None
# Make multiple tool calls with same session ID
for call_num in range(3):
sent_messages.clear()
tool_call_request = {
"jsonrpc": "2.0",
"id": call_num + 2,
"method": "tools/call",
"params": {"name": "track_session", "arguments": {}},
}
scope_with_session = {
**scope,
"headers": [
*scope["headers"],
(b"mcp-session-id", session_id_from_header.encode()),
],
}
async def mock_receive_tool():
return {
"type": "http.request",
"body": json.dumps(tool_call_request).encode(),
"more_body": False,
}
await manager.handle_request(scope_with_session, mock_receive_tool, mock_send)
# Verify all calls saw the same session_id
assert len(seen_session_ids) == 3, "Should have made 3 tool calls"
assert all(sid == session_id_from_header for sid in seen_session_ids), (
f"All session_ids should match: {seen_session_ids}"
)