Skip to content

Conversation

@DePasqualeOrg
Copy link

@DePasqualeOrg DePasqualeOrg commented Jan 15, 2026

While developing my Swift SDK for MCP, I encountered the following MCP spec compliance issues in the TypeScript SDK:

Tool Error Handling

When an unknown or disabled tool is called, it throws an McpError, which is caught and returned as a CallToolResult with isError: true.

The spec distinguishes between protocol errors and tool execution errors:

Protocol Errors:

  1. Protocol Errors: Standard JSON-RPC errors for issues like:
    • Unknown tools
    • Invalid arguments
    • Server errors

Tool Execution Errors:

  1. Tool Execution Errors: Reported in tool results with isError: true:
    • API failures
    • Invalid input data
    • Business logic errors

Protocol error example for unknown tool:

{
  "jsonrpc": "2.0",
  "id": 3,
  "error": {
    "code": -32602,
    "message": "Unknown tool: invalid_tool_name"
  }
}

I moved the unknown/disabled tool checking to before the try/catch block so that these protocol errors propagate as JSON-RPC errors rather than being caught and returned as a CallToolResult.

Resource Error Handling

The spec also requires that unknown resources return error code -32002 (ResourceNotFound) rather than -32602 (InvalidParams). This PR adds ErrorCode.ResourceNotFound to the ErrorCode enum and updates the resource handler to use it.

How Has This Been Tested?

  • Updated the unknown tool test to verify the correct error code and behavior
  • Added a new test for disabled tools
  • Updated the resource not found test to verify the correct error code (-32002)
  • Updated the prompt not found test to verify the correct error code (-32602)
  • One unrelated test was failing on my machine, so I fixed it by updating the expected error message. Then I noticed it failed in CI. Apparently the error message differs depending on the environment, so it now accepts either variant. Now all tests pass.

Breaking Changes

  • Users who expected CallToolResult with isError: true for unknown/disabled tools will now receive rejected promises with McpError
  • Users who checked for error code -32602 on unknown resources should update to expect -32002

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

@DePasqualeOrg DePasqualeOrg requested a review from a team as a code owner January 15, 2026 14:43
@changeset-bot
Copy link

changeset-bot bot commented Jan 15, 2026

🦋 Changeset detected

Latest commit: 28b6268

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@modelcontextprotocol/core Minor
@modelcontextprotocol/server Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@DePasqualeOrg DePasqualeOrg changed the title Fix/unknown tool protocol error Fix error handling for unknown and disabled tools Jan 15, 2026
@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 15, 2026

Open in StackBlitz

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@1389
npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@1389

commit: 28b6268

@DePasqualeOrg DePasqualeOrg changed the title Fix error handling for unknown and disabled tools fix: throw protocol error for unknown and disabled tools Jan 15, 2026
@DePasqualeOrg DePasqualeOrg marked this pull request as draft January 15, 2026 14:46
@DePasqualeOrg DePasqualeOrg force-pushed the fix/unknown-tool-protocol-error branch from 6571c9b to 59500f6 Compare January 15, 2026 20:09
@DePasqualeOrg DePasqualeOrg marked this pull request as ready for review January 15, 2026 20:11
@DePasqualeOrg DePasqualeOrg marked this pull request as draft January 15, 2026 21:19
The error message differs depending on the environment.
@DePasqualeOrg DePasqualeOrg force-pushed the fix/unknown-tool-protocol-error branch from 59500f6 to e68454d Compare January 15, 2026 21:31
@DePasqualeOrg DePasqualeOrg changed the title fix: throw protocol error for unknown and disabled tools fix: correct error handling for unknown tools and resources Jan 15, 2026
@DePasqualeOrg DePasqualeOrg marked this pull request as ready for review January 15, 2026 21:35
@DePasqualeOrg DePasqualeOrg force-pushed the fix/unknown-tool-protocol-error branch from e68454d to 28b6268 Compare January 15, 2026 21:53
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.

1 participant