← Back to Pull requests

fix(handlers)!: reject malformed tool, resource and prompt handler results #281

Open
mcollina opened this pull request with 204 lines diff across 4 files
View on GitHub
mcollina mcollina commented on Oct 13, 2026 11:19 UTC
Author

Summary

Handler results were passed through without checking their shape. A tool handler that returned undefined, a resource handler that returned {}, or a prompt handler without messages produced a JSON-RPC result that looked successful.

Now the server checks each handler result for the array its type requires (content for tools, contents for resources, messages for prompts). If that array is missing, the server logs an error and replies with JSON-RPC -32603 "Internal error". The reply has no data and no handler details. It is an error response, so it carries no result and nothing a client could cache. The check always runs, not only in strict mode.

Tool calls go through one shared check, so every path behaves the same way:

  • tools/call returns -32603.

  • A task-augmented tools/call ends in the failed status with statusMessage: 'Internal error', and tasks/result returns the same -32603 error.

  • app.mcpCallTool() returns { ok: false, reason: 'invalid-result' }. onToolCallComplete observers get the same outcome.
  • Tool errors are logged through app.log, because a task can outlive its request. Resource and prompt errors are logged through request.log, the same as the existing handler-failure logs there.

    Breaking change

  • McpCallToolOutcome has a new member, { ok: false, reason: 'invalid-result' }. TypeScript code that switches exhaustively on outcome.reason will need a new case.

  • Handlers that used to "succeed" with a malformed result now produce a -32603 error.
  • Tests

    test/malformed-handler-results.test.ts covers three kinds of bad result: undefined, {}, and a non-array field. It runs them against:

  • tools: TypeBox, JSON Schema, and schema-less tools, plus mcpCallTool (including the onToolCallComplete outcome) and task-augmented calls

  • resources

  • prompts
  • It also checks that the error is logged and that well-formed results still pass. 21 of the 22 new tests fail on main; the remaining one is the well-formed-results check. The full suite passes (620 tests).

    Fixes #252

    🤖 Generated with Claude Code

    https://claude.ai/code/session_01UF4S3w7iA83V9DVsasLJzM

    Diff: 204 lines (+200 / -4) across 4 files Reviewer: rozzilla Review duration: 59165m
    All Vouch review confidence checks passed
    Residual risk 0.08 = Change Risk (0.46) × (1 − Review Confidence (0.82)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.46 · 204 lines diff across 4 files
    0.46
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.77 · 2 substantive comment(s)
    0.77
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    82%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.318
    Real PR Conversation Timeline
    4 items
    rozzilla rozzilla commented on code on Oct 14, 2026 09:00 UTC
    Code Review
    src/types.ts:1
    @@ -237,6 +237,8 @@ export type McpCallToolOutcome =
       | { ok: false, reason: 'access-denied' }
       | { ok: false, reason: 'invalid-arguments', detail: string }
       | { ok: false, reason: 'task-required' }
    +  /** The handler returned something that is not a `CallToolResult` (no `content` array); details are logged */
    +  | { ok: false, reason: 'invalid-result' }

    we are introducing a new union member. This is source-breaking for consumers with exhaustive outcome.reason switches. It should be included in the next major version

    mcollina mcollina commented on code on Oct 14, 2026 10:07 UTC
    Code Review
    src/types.ts:1
    @@ -237,6 +237,8 @@ export type McpCallToolOutcome =
       | { ok: false, reason: 'access-denied' }
       | { ok: false, reason: 'invalid-arguments', detail: string }
       | { ok: false, reason: 'task-required' }
    +  /** The handler returned something that is not a `CallToolResult` (no `content` array); details are logged */
    +  | { ok: false, reason: 'invalid-result' }

    Agreed. I marked the PR as breaking (! in the title) and labelled it semver-major, so it ships with the next major release alongside the other breaking changes.

    rozzilla rozzilla approved these changes on Oct 14, 2026 10:40 UTC
    Approved Reviewer

    Approved these changes.

    Reviewers
    Assignees
    mcollina mcollina
    Labels
    risk: low confidence: 82.3% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 46.2% (MEDIUM) · Higher = larger blast radius
    46.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 82.3% · Lower = rushed or shallow review
    82.3%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 8.2% · Safe to merge
    8.2%