{"record":{"id":"9c5ec0f0e2f69a28","repo":"rohitg00/ai-engineering-from-scratch","slug":"server-did-not-return-json-rpc-error-expected-cod","errorCode":null,"errorMessage":"server did not return JSON-RPC error {expected_code}","messagePattern":"server did not return JSON-RPC error (.+?)","errorType":"validation","errorClass":"ProtocolViolation","httpStatus":null,"severity":"error","filePath":"phases/13-tools-and-protocols/31-mcp-conformance-versioning-and-operations/code/main.py","lineNumber":428,"sourceCode":"        raise ProtocolViolation(\"JSON-RPC error requires integer code and string message\")\n    expected_status = {-32020: 400, -32021: 400, -32022: 400, -32601: 404}.get(code)\n    if expected_status is not None and status != expected_status:\n        raise ProtocolViolation(f\"JSON-RPC error {code} requires HTTP {expected_status}\")\n\n\ndef validate_protocol_error_evidence(case: Transcript, expected_code: int) -> None:\n    if case.response_body is None:\n        raise ProtocolViolation(\"expected JSON-RPC error response is missing\")\n    validate_response(\n        case.request,\n        case.response_status,\n        case.response_body,\n        case.era,\n        set(case.capabilities),\n    )\n    error = case.response_body.get(\"error\")\n    if not isinstance(error, dict) or error.get(\"code\") != expected_code:\n        raise ProtocolViolation(f\"server did not return JSON-RPC error {expected_code}\")\n\n\ndef run_transcript(case: Transcript) -> CaseResult:\n    evidence = redact(\n        {\n            \"headers\": case.headers,\n            \"request\": case.request,\n            \"responseStatus\": case.response_status,\n            \"responseBody\": case.response_body,\n        }\n    )\n    try:\n        validate_request(case.headers, case.request, case.era)\n        if \"id\" not in case.request:\n            expected_status, expected_body = notification_http_outcome(case.request)\n            if (case.response_status, case.response_body) != (expected_status, expected_body):\n                raise ProtocolViolation(\"notification received a response body\")\n        elif case.response_body is None:","sourceCodeStart":410,"sourceCodeEnd":446,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/13-tools-and-protocols/31-mcp-conformance-versioning-and-operations/code/main.py#L410-L446","documentation":"Final evidence check in validate_protocol_error_evidence: the response's error object either is not a dict or its code does not equal the expected_code the case is trying to prove. It means the server returned a different error than the conformance scenario demands, so the expected protocol violation was not demonstrated.","triggerScenarios":"Thrown at phases/13-tools-and-protocols/31-mcp-conformance-versioning-and-operations/code/main.py:428 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Align the server's error code with the expected code for the scenario (e.g. -32601 for unknown methods)","Check for a catch-all handler masking the specific error with a generic code","Assert on the exact code in server tests, not just that an error occurred","Update the expected_code if the spec mapping in the case is stale"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}