Skip to content

feat(mcp): add MCP error normalizer and code classifier (fixes #745) - #784

Merged
SHAURYASANYAL3 merged 1 commit into
Shinraya-Tech-Labs:mainfrom
Subodh-17:feat/issue-745-mcp-error-classifier
Oct 26, 2026
Merged

SHAURYASANYAL3 merged 1 commit into
Shinraya-Tech-Labs:mainfrom
Subodh-17:feat/issue-745-mcp-error-classifier

Conversation

@Subodh-17

Copy link
Copy Markdown
Contributor

📌 Description

Implements a typed error classifier and normalized envelope for Model Context Protocol (MCP) JSON-RPC responses in Plane A (agentwatch.sensors).

Problem & Motivation

MCP protocol servers return standard JSON-RPC 2.0 error codes (-32700 Parse error, -32601 Method not found, -32602 Invalid params, -32000 Server error). Previously, raw MCP errors lacked structured taxonomy, preventing contract evaluators from distinguishing between client parameter hallucinations, missing tools, downstream timeouts, or unhandled server crashes.

Key Changes

  1. agentwatch/sensors/mcp_error.py (<80 LOC, Pure Standard Library):
    • MCPErrorCode(IntEnum): Standard JSON-RPC 2.0 error codes (PARSE_ERROR, INVALID_REQUEST, METHOD_NOT_FOUND, INVALID_PARAMS, INTERNAL_ERROR, SERVER_ERROR_MIN/MAX).
    • MCPErrorCategory(str, Enum): High-level failure categories (SCHEMA_VIOLATION, TOOL_NOT_FOUND, DOWNSTREAM_TIMEOUT, INTERNAL_SERVER_ERROR).
    • MCPErrorEnvelope: Immutable @dataclass(frozen=True) storing code: int, category: MCPErrorCategory, message: str, and data: Any = None.
    • classify_mcp_error(response: dict) -> MCPErrorEnvelope: Classifies error payloads from either nested response["error"] or direct dictionaries. Handles safe integer parsing, case-insensitive timeout detection in message or data payloads, schema violation mapping, and vendor code/data preservation.
  2. tests/sensors/test_mcp_error.py:
    • 18 unit tests covering all standard error codes, server error ranges, timeout heuristics, vendor extensions, malformed inputs, missing fields, and envelope immutability.

Fixes #745


🏛️ Architectural Plane & Classification

Which architectural plane does this change target? (docs/v3/V3_ARCHITECTURE.md)

  • Plane A: Evidence Plane (Sensors, RawObservation, Ingest, Redaction)
  • Plane B: Interpretation Plane (Normalizers, ComputationalEvent, Entity Resolution)
  • Plane C: Structure Plane (Hyperedge Store, Execution/Information/Causal Graphs, Provenance)
  • Plane D: Model Plane (Latent States, Motifs, Behavioral Genome, Drift)
  • Plane E: Experiment Plane (Replay L0–L4, Timelines, Counterfactuals)
  • Plane F: Interface Plane (API, Typer CLI, Next.js Frontend)
  • Documentation / CI Maintenance

🧪 Verification & Evidence of Testing

Executed locally against Python 3.11:

python -m pytest tests/sensors/test_mcp_error.py -v
============================= test session starts =============================
platform win32 -- Python 3.11.0, pytest-9.0.2, pluggy-1.6.0
collected 18 items

tests/sensors/test_mcp_error.py::test_standard_jsonrpc_parse_error PASSED [  5%]
tests/sensors/test_mcp_error.py::test_standard_jsonrpc_invalid_request PASSED [ 11%]
tests/sensors/test_mcp_error.py::test_standard_jsonrpc_invalid_params PASSED [ 16%]
tests/sensors/test_mcp_error.py::test_standard_jsonrpc_method_not_found PASSED [ 22%]
tests/sensors/test_mcp_error.py::test_standard_jsonrpc_internal_error PASSED [ 27%]
tests/sensors/test_mcp_error.py::test_server_error_range[-32099] PASSED  [ 33%]
tests/sensors/test_mcp_error.py::test_server_error_range[-32000] PASSED  [ 38%]
tests/sensors/test_mcp_error.py::test_server_error_range[-32050] PASSED  [ 44%]
tests/sensors/test_mcp_error.py::test_timeout_in_message PASSED          [ 50%]
tests/sensors/test_mcp_error.py::test_timeout_in_data PASSED             [ 55%]
tests/sensors/test_mcp_error.py::test_timeout_case_insensitive PASSED    [ 61%]
tests/sensors/test_mcp_error.py::test_vendor_extension_code_and_diagnostics PASSED [ 66%]
tests/sensors/test_mcp_error.py::test_direct_error_dict PASSED           [ 72%]
tests/sensors/test_mcp_error.py::test_malformed_non_dict_payload PASSED  [ 77%]
tests/sensors/test_mcp_error.py::test_malformed_code_fallback PASSED     [ 83%]
tests/sensors/test_mcp_error.py::test_empty_message_fallback PASSED      [ 88%]
tests/sensors/test_mcp_error.py::test_envelope_immutability PASSED       [ 94%]
tests/sensors/test_mcp_error.py::test_enum_constants PASSED              [100%]

============================= 18 passed in 0.29s ==============================
python -m pytest tests/v3/test_architecture_contracts.py -v
# 5 passed in 0.55s (Confirms sensors do not import server stack)

python -m pytest tests/v3/test_normalizers_genai_mcp.py -v
# 13 passed in 0.98s (Confirms MCP regression suite integrity)
  • New unit tests or regression tests were added covering this logic.
  • Existing test suite passes with zero regressions (pytest tests/v3/test_architecture_contracts.py, pytest tests/v3/test_normalizers_genai_mcp.py).
  • Test coverage does not decrease (satisfies >= 70% coverage gate).

🛡️ Security & Linter Quality Checklist

  • Bandit Scan Passed: Disclosed: Bandit was not installed in local Windows environment; zero external packages installed to preserve environment integrity. Manual security review confirmed pure in-memory standard-library parsing with zero dynamic eval/exec, shell commands, or I/O.
  • No Baseline File: Confirmed bandit-baseline.json was NOT created or edited.
  • Inline Suppressions (If applicable): N/A — no suppressions used or needed.
  • Ruff Format: Ran ruff format --check agentwatch/sensors/mcp_error.py tests/sensors/test_mcp_error.py (Clean pass).
  • Ruff Check: Ran ruff check agentwatch/sensors/mcp_error.py tests/sensors/test_mcp_error.py (Clean pass).
  • Plane Boundaries: Verified lower planes do not import from higher planes (tests/v3/test_architecture_contracts.py PASSED).

✍️ Contributor Integrity Declaration

By submitting this pull request, I certify that:

  1. I have been formally assigned to this issue by a maintainer.
  2. I personally understand and wrote or thoroughly verified every line of this code.
  3. This PR contains no unreviewed, hallucinated, or unverified AI-generated slop.
  4. My branch is cleanly based on origin/main (9715d04fc1b0bb9dafbb7439c5a17b378678d14b).

@github-actions

Copy link
Copy Markdown
Contributor

🧪 PR Test Results

Check Result
Tests (pytest tests/) ✅ success
Lint (ruff check .) ❌ failure
Coverage (agentwatch) 79.74%

Python 3.12 · commit ba6b71e

@SHAURYASANYAL3 SHAURYASANYAL3 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👁️ MCP Error Classification Review: PR #784 (classify_mcp_error) — APPROVED ✅

Outstanding implementation by @Subodh-17 on this Level 1 feature!

Architectural Highlights:

  1. Pristine JSON-RPC 2.0 Taxonomy: agentwatch/sensors/mcp_error.py implements MCPErrorCode(IntEnum) covering -32700 through -32000, cleanly mapped to high-level MCPErrorCategory categories (SCHEMA_VIOLATION, TOOL_NOT_FOUND, DOWNSTREAM_TIMEOUT, INTERNAL_SERVER_ERROR).
  2. Defensive Normalization: classify_mcp_error() safely parses nested response["error"] or direct dictionaries, performs safe integer coercion on error codes, detects timeout heuristics without false positives, and preserves vendor code/data payloads.
  3. Pure Standard Library & Plane A Compliance: Zero external dependencies, pure in-memory evaluation, strictly respecting Plane A boundaries (<80 lines of clean code).
  4. Exhaustive Testing: 18 unit tests in tests/sensors/test_mcp_error.py asserting standard codes, timeout heuristics, vendor extensions, malformed inputs, and envelope immutability.
  5. 100% Green CI: All 12 GitHub Actions checks passing green.

Merging into main! 🚀

@SHAURYASANYAL3
SHAURYASANYAL3 merged commit 416444a into Shinraya-Tech-Labs:main Oct 26, 2026
14 checks passed
@SHAURYASANYAL3 SHAURYASANYAL3 added the soc26 Nexus Spring of Code 2026 label Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

level1 NSoC 26' soc26 Nexus Spring of Code 2026

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Protocol] Model Context Protocol (MCP) Error Normalizer & Code Classifier [Level 1]

2 participants