fix: return isError instead of throwing McpError for missing API key#162
Open
blackwell-systems wants to merge 1 commit into
Open
Conversation
When TAVILY_API_KEY is not set, the tool handler threw McpError which produces a JSON-RPC error frame. MCP clients treat this as a server crash rather than a recoverable failure. Return isError: true with a descriptive message so agents can self-correct. This matches the existing error handling pattern used for Axios API errors at lines 554-573. Fixes tavily-ai#161
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.
Summary
Fixes #161. When
TAVILY_API_KEYis not set, theCallToolRequestSchemahandler throwsMcpError(ErrorCode.InvalidRequest, ...), which produces a JSON-RPC error frame. MCP clients treat this as a server crash rather than a recoverable failure.Fix
Replace the
throw new McpError(...)with a return of{ content: [...], isError: true }. This lets agents read the error message and self-correct (e.g., prompt the user to set the key) instead of seeing an opaque server crash.This matches the existing error handling pattern already used for Axios API errors at lines 554-573 in the same file.
Before
Agents see a server crash. No recovery path.
After
{ "content": [{"type": "text", "text": "TAVILY_API_KEY environment variable is required..."}], "isError": true }Agents see a tool error with a clear message. Can retry or ask the user.
Note
Low Risk
Low risk: localized change to request handler error handling that only affects behavior when
TAVILY_API_KEYis unset.Overview
Prevents MCP clients from treating a missing
TAVILY_API_KEYas a server crash by replacing the thrownMcpError(ErrorCode.InvalidRequest, ...)in theCallToolRequestSchemahandler with a normal tool response containingcontentandisError: true.Reviewed by Cursor Bugbot for commit 586a808. Bugbot is set up for automated code reviews on this repo. Configure here.