Skip to content

fix: return isError instead of throwing McpError for missing API key#162

Open
blackwell-systems wants to merge 1 commit into
tavily-ai:mainfrom
blackwell-systems:fix/return-iserror-for-missing-api-key
Open

fix: return isError instead of throwing McpError for missing API key#162
blackwell-systems wants to merge 1 commit into
tavily-ai:mainfrom
blackwell-systems:fix/return-iserror-for-missing-api-key

Conversation

@blackwell-systems
Copy link
Copy Markdown

@blackwell-systems blackwell-systems commented Apr 29, 2026

Summary

Fixes #161. When TAVILY_API_KEY is not set, the CallToolRequestSchema handler throws McpError(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

JSON-RPC error: {"code": -32600, "message": "TAVILY_API_KEY environment variable is required..."}

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_KEY is unset.

Overview
Prevents MCP clients from treating a missing TAVILY_API_KEY as a server crash by replacing the thrown McpError(ErrorCode.InvalidRequest, ...) in the CallToolRequestSchema handler with a normal tool response containing content and isError: true.

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

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
@blackwell-systems blackwell-systems requested a review from a team April 29, 2026 00:38
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.

Missing TAVILY_API_KEY throws McpError instead of returning isError: true

1 participant