Fix: return protocol errors for invalid arguments and server errors#285
Merged
koic merged 1 commit intomodelcontextprotocol:mainfrom Apr 1, 2026
Conversation
koic
reviewed
Apr 1, 2026
|
|
||
| result | ||
| rescue => e | ||
| report_exception(e, { request: request }) |
Member
There was a problem hiding this comment.
call_tool no longer calls report_exception before re-raising. The rescue here now receives RequestHandlerError instead of the original exception, so exception_reporter loses the original
class and backtrace.
--- a/lib/mcp/server.rb
+++ b/lib/mcp/server.rb
@@ -317,7 +317,8 @@ module MCP
result
rescue => e
- report_exception(e, { request: request })
+ reported = e.is_a?(RequestHandlerError) ? (e.original_error || e) : e
+ report_exception(reported, { request: request })
if e.is_a?(RequestHandlerError)
add_instrumentation_data(error: e.error_type)
raise e
Contributor
Author
There was a problem hiding this comment.
Took the liberty of refactoring the rescue block to avoid the duplicated is_a? checks when I implemented this suggestion
koic
reviewed
Apr 1, 2026
Per the MCP spec, invalid arguments and server errors are protocol errors (JSON-RPC error responses), not tool execution errors (isError: true). PR modelcontextprotocol#165 moved all error cases to isError: true, and PR modelcontextprotocol#231 only restored protocol errors for unknown tools. This fixes the remaining two cases: invalid arguments (-32602) and server errors (-32603). Ref: https://modelcontextprotocol.io/specification/2024-11-05/server/tools#error-handling
81da497 to
8dd0379
Compare
koic
approved these changes
Apr 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
PR #231 fixed unknown tool calls to return protocol errors per the MCP spec, but two of the three protocol error cases still return
isError: trueinstead of JSON-RPC errors:isError: trueinstead of-32602isError: trueinstead of-32603Per the MCP spec, these are protocol errors, not tool execution errors.
Fixes #284.
How Has This Been Tested?
Updated existing tests to assert JSON-RPC error responses instead of
isError: trueresults. All tests pass, rubocop clean.Breaking Changes
Clients relying on
isError: truefor invalid arguments or server errors will now receive JSON-RPC error responses instead. This matches the behavior PR #231 introduced for unknown tools.Types of changes
Checklist