Skip to content

fix(mcp): preserve errors in schema-valid clients - #54

Merged
Kikobeats merged 1 commit into
masterfrom
fix/mcp-errors-schema-valid-clients
Sep 13, 2026
Merged

fix(mcp): preserve errors in schema-valid clients#54
Kikobeats merged 1 commit into
masterfrom
fix/mcp-errors-schema-valid-clients

Conversation

@Kikobeats

@Kikobeats Kikobeats commented Sep 13, 2026

Copy link
Copy Markdown
Member

Problem

Since #47, every tool declares an outputSchema describing its successful structuredContent ({ data: ... }). Error results, however, were also sent as structuredContent ({ error: ... }).

The MCP SDK skips output validation server-side when isError is set, but a spec-strict client can validate any structuredContent it receives against the advertised schema. { error: ... } never matches { data: ... }, so those clients replace the actionable error payload (message, code, reason, hint, upgrade) with a schema-validation failure, hiding the very guidance the error taxonomy (#39) exists to provide.

Fix

Omit structuredContent on error results and keep the full JSON error payload in the text content, which is what MCP clients fall back to when no structuredContent is present. One-line behavioral change in the single choke point (toToolResponse in microlink-client.js), through which every tool error flows:

  • Success: unchanged (structuredContent: { data: ... } + text mirror).
  • Error: isError: true + text content carrying the identical JSON payload, no structuredContent.

Tests

Updated the error envelope assertions (errors.test.js, tools.test.js, output-schemas.test.js) to assert the new contract: structuredContent absent on errors, payload intact in text content. MCP suite 103/103 (8 live tests skipped as usual), standard lint green.


Note

Low Risk
Single response-shaping change with broad test updates; success paths unchanged and error payloads remain in text content.

Overview
MCP tool errors no longer include structuredContent, so strict clients that validate responses against each tool’s success-only outputSchema ({ data: ... }) still see the full actionable JSON (codes, hints, upgrade guidance) in text content instead of a schema failure masking it.

The change is centralized in toToolResponse: successes still return structuredContent: { data: ... } plus a text mirror; errors set isError: true and put the same JSON payload only in content[0].text.

Tests across errors.test.js, tools.test.js, and output-schemas.test.js now assert structuredContent is absent on errors and read assertions from parsed text content.

Reviewed by Cursor Bugbot for commit b77c021. Bugbot is set up for automated code reviews on this repo. Configure here.

Summary by CodeRabbit

  • Bug Fixes

    • Error responses now omit structuredContent, preserving compatibility with clients that validate successful response schemas.
    • Error details remain available in the response’s text content as serialized JSON.
    • Successful tool responses continue to include structured content alongside formatted text.
  • Tests

    • Updated error-handling and validation coverage to verify the revised response format.

A tool's outputSchema describes successful structuredContent. Clients
that validate structuredContent against it reject the error payload
({ error: ... } against a { data: ... } schema), replacing the
actionable error with a validation failure. Omit structuredContent on
error results; the full JSON error payload stays in the text content,
which is what MCP clients fall back to.

Signed-off-by: Jose Francisco 'Kiko' Verdú Gambín <2096101+Kikobeats@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Sep 13, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 6551ba1e-5000-4da7-ade1-a2a6279b0e54

📥 Commits

Reviewing files that changed from the base of the PR and between 69d1f76 and b77c021.

📒 Files selected for processing (4)
  • packages/mcp/src/microlink-client.js
  • packages/mcp/test/errors.test.js
  • packages/mcp/test/output-schemas.test.js
  • packages/mcp/test/tools.test.js

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

MCP tool responses now omit structuredContent for errors. Error details remain JSON-serialized in content[0].text. Successful responses retain structuredContent. Tests now validate this response shape.

Changes

MCP error response shape

Layer / File(s) Summary
Conditional response construction
packages/mcp/src/microlink-client.js
toToolResponse adds structuredContent only when isError is false. Error responses contain isError and JSON-stringified content.
Error response validation
packages/mcp/test/errors.test.js, packages/mcp/test/output-schemas.test.js, packages/mcp/test/tools.test.js
Tests parse error details from content[0].text and verify that structuredContent is undefined for error responses.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to b77c0

The change preserves successful structured responses and keeps complete error details in text content without identified merge-blocking risk.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main MCP change: preserving error details for schema-validating clients.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/mcp-errors-schema-valid-clients

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 34746554383

Warning

No base build found for commit 69d1f76 on master.
Coverage changes can't be calculated without a base build.
If a base build is processing, this comment will update automatically when it completes.

Coverage: 79.515%

Details

  • Patch coverage: 9 of 9 lines across 1 file are fully covered (100%).

Uncovered Changes

No uncovered changes found.

Coverage Regressions

Requires a base build to compare against. How to fix this →


Coverage Stats

Coverage Status
Relevant Lines: 5195
Covered Lines: 4141
Line Coverage: 79.71%
Relevant Branches: 868
Covered Branches: 680
Branch Coverage: 78.34%
Branches in Coverage %: Yes
Coverage Strength: 25.72 hits per line

💛 - Coveralls

@Kikobeats
Kikobeats merged commit 0fb8de9 into master Sep 13, 2026
9 checks passed
@Kikobeats
Kikobeats deleted the fix/mcp-errors-schema-valid-clients branch September 13, 2026 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants