@@ -190,13 +190,21 @@ async def test_http_list_tools(http_client: httpx.AsyncClient, server: str) -> N
190
190
)
191
191
assert init_response .status_code == 200
192
192
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
+
193
197
initialized_response = await http_client .post (
194
198
mcp_path ,
195
199
json = {
196
200
"jsonrpc" : "2.0" ,
197
201
"method" : "notifications/initialized" ,
198
202
},
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
+ },
200
208
)
201
209
assert initialized_response .status_code == 202
202
210
@@ -207,7 +215,11 @@ async def test_http_list_tools(http_client: httpx.AsyncClient, server: str) -> N
207
215
"method" : "tools/list" ,
208
216
"id" : 2 ,
209
217
},
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
+ },
211
223
)
212
224
213
225
assert response .status_code == 200
@@ -245,13 +257,21 @@ async def test_http_call_tool(http_client: httpx.AsyncClient, server: str) -> No
245
257
)
246
258
assert init_response .status_code == 200
247
259
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
+
248
264
initialized_response = await http_client .post (
249
265
mcp_path ,
250
266
json = {
251
267
"jsonrpc" : "2.0" ,
252
268
"method" : "notifications/initialized" ,
253
269
},
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
+ },
255
275
)
256
276
assert initialized_response .status_code == 202
257
277
@@ -266,7 +286,11 @@ async def test_http_call_tool(http_client: httpx.AsyncClient, server: str) -> No
266
286
"arguments" : {"item_id" : 1 },
267
287
},
268
288
},
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
+ },
270
294
)
271
295
272
296
assert response .status_code == 200
@@ -305,13 +329,21 @@ async def test_http_ping(http_client: httpx.AsyncClient, server: str) -> None:
305
329
)
306
330
assert init_response .status_code == 200
307
331
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
+
308
336
initialized_response = await http_client .post (
309
337
mcp_path ,
310
338
json = {
311
339
"jsonrpc" : "2.0" ,
312
340
"method" : "notifications/initialized" ,
313
341
},
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
+ },
315
347
)
316
348
assert initialized_response .status_code == 202
317
349
@@ -322,7 +354,11 @@ async def test_http_ping(http_client: httpx.AsyncClient, server: str) -> None:
322
354
"method" : "ping" ,
323
355
"id" : 4 ,
324
356
},
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
+ },
326
362
)
327
363
328
364
assert response .status_code == 200
@@ -354,14 +390,39 @@ async def test_http_invalid_method(http_client: httpx.AsyncClient, server: str)
354
390
"""Test error handling for invalid methods."""
355
391
mcp_path = "/mcp"
356
392
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
+
357
414
response = await http_client .post (
358
415
mcp_path ,
359
416
json = {
360
417
"jsonrpc" : "2.0" ,
361
418
"method" : "invalid/method" ,
362
419
"id" : 5 ,
363
420
},
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
+ },
365
426
)
366
427
367
428
assert response .status_code == 200
@@ -377,14 +438,39 @@ async def test_http_notification_handling(http_client: httpx.AsyncClient, server
377
438
"""Test that notifications return 202 Accepted without response body."""
378
439
mcp_path = "/mcp"
379
440
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
+
380
462
response = await http_client .post (
381
463
mcp_path ,
382
464
json = {
383
465
"jsonrpc" : "2.0" ,
384
466
"method" : "notifications/cancelled" ,
385
467
"params" : {"requestId" : "test-123" },
386
468
},
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
+ },
388
474
)
389
475
390
476
assert response .status_code == 202
0 commit comments