|
37 | 37 | LoggingMessageNotificationParams, |
38 | 38 | NotificationParams, |
39 | 39 | PaginatedRequestParams, |
| 40 | + ReadResourceRequestParams, |
| 41 | + ReadResourceResult, |
40 | 42 | Tool, |
41 | 43 | ) |
42 | 44 | from mcp_types.version import LATEST_MODERN_VERSION, MODERN_PROTOCOL_VERSIONS |
@@ -1202,13 +1204,13 @@ async def test_modern_post_rejects_a_duplicated_routing_header() -> None: |
1202 | 1204 | async def test_modern_tools_call_mis_shaped_envelope_skips_validation_without_an_error_log( |
1203 | 1205 | caplog: pytest.LogCaptureFixture, |
1204 | 1206 | ) -> None: |
1205 | | - """A mis-shaped envelope value is a debug-logged client fault; the wire reply is dispatch's INVALID_PARAMS.""" |
| 1207 | + """A mis-shaped optional envelope value clears the ladder and fails during dispatch: in-band 200 INVALID_PARAMS.""" |
1206 | 1208 | body = _tool_call_body({"region": "eu"}) |
1207 | 1209 | body["params"]["_meta"][CLIENT_INFO_META_KEY] = "not-an-object" |
1208 | 1210 | with caplog.at_level(logging.DEBUG, logger=_streamable_http_modern.__name__): |
1209 | 1211 | async with _asgi_client(_x_mcp_server()) as http: |
1210 | 1212 | response = await http.post("/mcp", json=body, headers=_TOOL_CALL_HEADERS | {"mcp-param-region": "eu"}) |
1211 | | - assert response.status_code == 400 |
| 1213 | + assert response.status_code == 200 |
1212 | 1214 | assert response.json()["error"]["code"] == INVALID_PARAMS |
1213 | 1215 | module_records = [r for r in caplog.records if r.name == _streamable_http_modern.__name__] |
1214 | 1216 | assert all(r.levelno < logging.ERROR for r in module_records) |
@@ -1353,3 +1355,101 @@ async def post() -> None: |
1353 | 1355 | "io.modelcontextprotocol/subscriptionId": 9, |
1354 | 1356 | SERVER_INFO_META_KEY: {"name": "test", "version": "1.2.3"}, |
1355 | 1357 | } |
| 1358 | + |
| 1359 | + |
| 1360 | +def _read_resource_body(uri: str = "demo://does-not-exist", *, progress_token: str | None = None) -> dict[str, Any]: |
| 1361 | + meta: dict[str, Any] = { |
| 1362 | + PROTOCOL_VERSION_META_KEY: LATEST_MODERN_VERSION, |
| 1363 | + CLIENT_INFO_META_KEY: {"name": "raw", "version": "0.0.0"}, |
| 1364 | + CLIENT_CAPABILITIES_META_KEY: {}, |
| 1365 | + } |
| 1366 | + if progress_token is not None: |
| 1367 | + meta["progressToken"] = progress_token |
| 1368 | + return { |
| 1369 | + "jsonrpc": "2.0", |
| 1370 | + "id": 7800, |
| 1371 | + "method": "resources/read", |
| 1372 | + "params": {"uri": uri, "_meta": meta}, |
| 1373 | + } |
| 1374 | + |
| 1375 | + |
| 1376 | +def _unknown_resource_mcp_server() -> MCPServer: |
| 1377 | + mcp = MCPServer("repro") |
| 1378 | + |
| 1379 | + @mcp.resource("demo://widget") |
| 1380 | + def widget() -> str: |
| 1381 | + return "<html></html>" |
| 1382 | + |
| 1383 | + return mcp |
| 1384 | + |
| 1385 | + |
| 1386 | +@pytest.mark.parametrize( |
| 1387 | + ("json_response", "accept"), |
| 1388 | + [ |
| 1389 | + pytest.param(True, "application/json", id="json-mode"), |
| 1390 | + pytest.param(False, "application/json, text/event-stream", id="sse-deferral-fallback"), |
| 1391 | + ], |
| 1392 | +) |
| 1393 | +async def test_modern_handler_invalid_params_returns_http_200_in_json_and_sse_deferral( |
| 1394 | + json_response: bool, accept: str |
| 1395 | +) -> None: |
| 1396 | + """Spec-mandated: a dispatched handler INVALID_PARAMS (-32602, e.g. unknown resource) |
| 1397 | + MUST return HTTP 200 with the JSON-RPC error in-band, in both JSON mode and SSE |
| 1398 | + deferral fallback when the handler returns without emitting a notification.""" |
| 1399 | + mcp = _unknown_resource_mcp_server() |
| 1400 | + headers = {MCP_METHOD_HEADER: "resources/read", MCP_NAME_HEADER: "demo://does-not-exist"} |
| 1401 | + async with _asgi_client(mcp._lowlevel_server, json_response=json_response, accept=accept) as http: |
| 1402 | + with anyio.fail_after(5): |
| 1403 | + response = await http.post("/mcp", json=_read_resource_body(), headers=headers) |
| 1404 | + |
| 1405 | + assert response.status_code == 200 |
| 1406 | + assert response.headers["content-type"].split(";", 1)[0] == "application/json" |
| 1407 | + payload = response.json() |
| 1408 | + assert payload["id"] == 7800 |
| 1409 | + assert payload["error"]["code"] == INVALID_PARAMS |
| 1410 | + |
| 1411 | + |
| 1412 | +async def test_modern_handler_invalid_params_after_progress_streams_sse_200() -> None: |
| 1413 | + """Spec-mandated: in SSE mode, emitting a progress notification before raising |
| 1414 | + handler INVALID_PARAMS (-32602) returns HTTP 200 text/event-stream with the progress |
| 1415 | + event followed by the terminal -32602 error.""" |
| 1416 | + |
| 1417 | + async def read_resource(ctx: ServerRequestContext, params: ReadResourceRequestParams) -> ReadResourceResult: |
| 1418 | + await ctx.session.report_progress(1.0, total=1.0) |
| 1419 | + raise MCPError( |
| 1420 | + code=INVALID_PARAMS, |
| 1421 | + message=f"Unknown resource: {params.uri}", |
| 1422 | + data={"uri": str(params.uri)}, |
| 1423 | + ) |
| 1424 | + |
| 1425 | + server: Server[Any] = Server("test", on_read_resource=read_resource) |
| 1426 | + headers = {MCP_METHOD_HEADER: "resources/read", MCP_NAME_HEADER: "demo://does-not-exist"} |
| 1427 | + async with _asgi_client(server, json_response=False) as http: |
| 1428 | + with anyio.fail_after(5): |
| 1429 | + response = await http.post( |
| 1430 | + "/mcp", json=_read_resource_body(progress_token="tok-missing"), headers=headers |
| 1431 | + ) |
| 1432 | + |
| 1433 | + assert response.status_code == 200 |
| 1434 | + assert response.headers["content-type"].split(";", 1)[0] == "text/event-stream" |
| 1435 | + events = _sse_payloads(response.text) |
| 1436 | + assert len(events) == 2 |
| 1437 | + assert events[0]["method"] == "notifications/progress" |
| 1438 | + assert events[0]["params"]["progressToken"] == "tok-missing" |
| 1439 | + assert events[1]["id"] == 7800 |
| 1440 | + assert events[1]["error"]["code"] == INVALID_PARAMS |
| 1441 | + |
| 1442 | + |
| 1443 | +async def test_modern_ladder_invalid_params_rejection_stays_http_400() -> None: |
| 1444 | + """Spec-mandated: a pre-dispatch ladder rejection carrying -32602 (missing required |
| 1445 | + `_meta` envelope key) MUST still answer HTTP 400, preserving the origin split.""" |
| 1446 | + mcp = _unknown_resource_mcp_server() |
| 1447 | + body = _read_resource_body() |
| 1448 | + del body["params"]["_meta"][CLIENT_CAPABILITIES_META_KEY] |
| 1449 | + headers = {MCP_METHOD_HEADER: "resources/read", MCP_NAME_HEADER: "demo://does-not-exist"} |
| 1450 | + async with _asgi_client(mcp._lowlevel_server, json_response=True, accept="application/json") as http: |
| 1451 | + response = await http.post("/mcp", json=body, headers=headers) |
| 1452 | + |
| 1453 | + assert response.status_code == 400 |
| 1454 | + assert response.headers["content-type"].split(";", 1)[0] == "application/json" |
| 1455 | + assert response.json()["error"]["code"] == INVALID_PARAMS |
0 commit comments