fix(examples): return isError for input validation instead of internal error#838
Conversation
…l error The echo and add tool handlers use return nil, fmt.Errorf(...) for input validation failures (wrong type, missing argument). In the SDK, this produces -32603 (InternalError), which agents cannot recover from. Changed to mcp.NewToolResultError(...), nil which returns isError: true with a descriptive message. This matches the pattern used by the SDK's own NewTypedToolHandler and NewStructuredToolHandler, and gives agents actionable feedback they can use to self-correct. Internal errors (notification send failures) are unchanged since those represent actual server-side failures, not input validation. Fixes mark3labs#837
|
Connected to Huly®: MCP_G-403 |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughTwo tool handlers in the example server are updated to return MCP-structured tool errors via ChangesExample Tool Error Handling
Estimated Code Review Effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly Related PRs
Suggested Reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.Comment |
Summary
The everything server's
echoandaddtool handlers return(nil, fmt.Errorf(...))for input validation failures (wrong type, missing argument). In the SDK, this produces-32603(InternalError), which agents cannot distinguish from a server crash.Changed to
mcp.NewToolResultError(...), nilwhich returnsisError: truewith a descriptive message the agent can act on.Before / After
echowith{"message": 42}-32603"invalid message argument"isError: true"invalid message argument: expected string"echowith{}-32603"invalid message argument"isError: true"invalid message argument: expected string"addwith{"a": "x", "b": 2}-32603"invalid number arguments"isError: true"invalid number arguments: expected numeric values"Why this matters
The everything server is the reference example. Developers learning from it copy the
return nil, fmt.Errorf(...)pattern. The SDK's ownNewTypedToolHandler(typed_tools.go:19) andNewStructuredToolHandler(typed_tools.go:32) already useNewToolResultErrorfor argument binding failures, but the untyped example teaches the opposite.Internal errors (notification send failures at lines 408, 446) are unchanged since those represent actual server-side failures.
Test plan
go build ./examples/everything/passesgo test ./... -shortpasses (all packages)isError: trueinstead of-32603Fixes #837
Summary by CodeRabbit