Skip to content

Commit 36ed9d8

Browse files
committed
fix transport tests to match stateful http transport
1 parent bc8fa74 commit 36ed9d8

File tree

1 file changed

+94
-8
lines changed

1 file changed

+94
-8
lines changed

tests/test_http_real_transport.py

Lines changed: 94 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,21 @@ async def test_http_list_tools(http_client: httpx.AsyncClient, server: str) -> N
190190
)
191191
assert init_response.status_code == 200
192192

193+
# Extract session ID from the initialize response
194+
session_id = init_response.headers.get("mcp-session-id")
195+
assert session_id is not None, "Server should return a session ID"
196+
193197
initialized_response = await http_client.post(
194198
mcp_path,
195199
json={
196200
"jsonrpc": "2.0",
197201
"method": "notifications/initialized",
198202
},
199-
headers={"Accept": "application/json, text/event-stream", "Content-Type": "application/json"},
203+
headers={
204+
"Accept": "application/json, text/event-stream",
205+
"Content-Type": "application/json",
206+
"mcp-session-id": session_id,
207+
},
200208
)
201209
assert initialized_response.status_code == 202
202210

@@ -207,7 +215,11 @@ async def test_http_list_tools(http_client: httpx.AsyncClient, server: str) -> N
207215
"method": "tools/list",
208216
"id": 2,
209217
},
210-
headers={"Accept": "application/json, text/event-stream", "Content-Type": "application/json"},
218+
headers={
219+
"Accept": "application/json, text/event-stream",
220+
"Content-Type": "application/json",
221+
"mcp-session-id": session_id,
222+
},
211223
)
212224

213225
assert response.status_code == 200
@@ -245,13 +257,21 @@ async def test_http_call_tool(http_client: httpx.AsyncClient, server: str) -> No
245257
)
246258
assert init_response.status_code == 200
247259

260+
# Extract session ID from the initialize response
261+
session_id = init_response.headers.get("mcp-session-id")
262+
assert session_id is not None, "Server should return a session ID"
263+
248264
initialized_response = await http_client.post(
249265
mcp_path,
250266
json={
251267
"jsonrpc": "2.0",
252268
"method": "notifications/initialized",
253269
},
254-
headers={"Accept": "application/json, text/event-stream", "Content-Type": "application/json"},
270+
headers={
271+
"Accept": "application/json, text/event-stream",
272+
"Content-Type": "application/json",
273+
"mcp-session-id": session_id,
274+
},
255275
)
256276
assert initialized_response.status_code == 202
257277

@@ -266,7 +286,11 @@ async def test_http_call_tool(http_client: httpx.AsyncClient, server: str) -> No
266286
"arguments": {"item_id": 1},
267287
},
268288
},
269-
headers={"Accept": "application/json, text/event-stream", "Content-Type": "application/json"},
289+
headers={
290+
"Accept": "application/json, text/event-stream",
291+
"Content-Type": "application/json",
292+
"mcp-session-id": session_id,
293+
},
270294
)
271295

272296
assert response.status_code == 200
@@ -305,13 +329,21 @@ async def test_http_ping(http_client: httpx.AsyncClient, server: str) -> None:
305329
)
306330
assert init_response.status_code == 200
307331

332+
# Extract session ID from the initialize response
333+
session_id = init_response.headers.get("mcp-session-id")
334+
assert session_id is not None, "Server should return a session ID"
335+
308336
initialized_response = await http_client.post(
309337
mcp_path,
310338
json={
311339
"jsonrpc": "2.0",
312340
"method": "notifications/initialized",
313341
},
314-
headers={"Accept": "application/json, text/event-stream", "Content-Type": "application/json"},
342+
headers={
343+
"Accept": "application/json, text/event-stream",
344+
"Content-Type": "application/json",
345+
"mcp-session-id": session_id,
346+
},
315347
)
316348
assert initialized_response.status_code == 202
317349

@@ -322,7 +354,11 @@ async def test_http_ping(http_client: httpx.AsyncClient, server: str) -> None:
322354
"method": "ping",
323355
"id": 4,
324356
},
325-
headers={"Accept": "application/json, text/event-stream", "Content-Type": "application/json"},
357+
headers={
358+
"Accept": "application/json, text/event-stream",
359+
"Content-Type": "application/json",
360+
"mcp-session-id": session_id,
361+
},
326362
)
327363

328364
assert response.status_code == 200
@@ -354,14 +390,39 @@ async def test_http_invalid_method(http_client: httpx.AsyncClient, server: str)
354390
"""Test error handling for invalid methods."""
355391
mcp_path = "/mcp"
356392

393+
# First initialize to get a session ID
394+
init_response = await http_client.post(
395+
mcp_path,
396+
json={
397+
"jsonrpc": "2.0",
398+
"method": "initialize",
399+
"id": 1,
400+
"params": {
401+
"protocolVersion": types.LATEST_PROTOCOL_VERSION,
402+
"capabilities": {},
403+
"clientInfo": {"name": "test-client", "version": "1.0.0"},
404+
},
405+
},
406+
headers={"Accept": "application/json, text/event-stream", "Content-Type": "application/json"},
407+
)
408+
assert init_response.status_code == 200
409+
410+
# Extract session ID from the initialize response
411+
session_id = init_response.headers.get("mcp-session-id")
412+
assert session_id is not None, "Server should return a session ID"
413+
357414
response = await http_client.post(
358415
mcp_path,
359416
json={
360417
"jsonrpc": "2.0",
361418
"method": "invalid/method",
362419
"id": 5,
363420
},
364-
headers={"Accept": "application/json, text/event-stream", "Content-Type": "application/json"},
421+
headers={
422+
"Accept": "application/json, text/event-stream",
423+
"Content-Type": "application/json",
424+
"mcp-session-id": session_id,
425+
},
365426
)
366427

367428
assert response.status_code == 200
@@ -377,14 +438,39 @@ async def test_http_notification_handling(http_client: httpx.AsyncClient, server
377438
"""Test that notifications return 202 Accepted without response body."""
378439
mcp_path = "/mcp"
379440

441+
# First initialize to get a session ID
442+
init_response = await http_client.post(
443+
mcp_path,
444+
json={
445+
"jsonrpc": "2.0",
446+
"method": "initialize",
447+
"id": 1,
448+
"params": {
449+
"protocolVersion": types.LATEST_PROTOCOL_VERSION,
450+
"capabilities": {},
451+
"clientInfo": {"name": "test-client", "version": "1.0.0"},
452+
},
453+
},
454+
headers={"Accept": "application/json, text/event-stream", "Content-Type": "application/json"},
455+
)
456+
assert init_response.status_code == 200
457+
458+
# Extract session ID from the initialize response
459+
session_id = init_response.headers.get("mcp-session-id")
460+
assert session_id is not None, "Server should return a session ID"
461+
380462
response = await http_client.post(
381463
mcp_path,
382464
json={
383465
"jsonrpc": "2.0",
384466
"method": "notifications/cancelled",
385467
"params": {"requestId": "test-123"},
386468
},
387-
headers={"Accept": "application/json, text/event-stream", "Content-Type": "application/json"},
469+
headers={
470+
"Accept": "application/json, text/event-stream",
471+
"Content-Type": "application/json",
472+
"mcp-session-id": session_id,
473+
},
388474
)
389475

390476
assert response.status_code == 202

0 commit comments

Comments
 (0)