fix: log exceptions in tool calls at ERROR level - #3271
Open
gingeekrishna wants to merge 1 commit into
Open
Conversation
Resource and prompt handlers already call logger.exception() when an error occurs. Tool calls were silently swallowing exceptions into CallToolResult(is_error=True) with no server-side log, making it impossible for operators to diagnose tool failures without client-side visibility. Adds logger.exception() before the is_error return in _handle_call_tool, matching the pattern used in _handle_read_resource and _handle_get_prompt. Fixes modelcontextprotocol#3266 Signed-off-by: Radhakrishnan Panchayappan <gingeekrishna@gmail.com> Signed-off-by: Radhakrishnan Pachyappan <gingeekrishna@gmail.com>
2 tasks
There was a problem hiding this comment.
Pull request overview
Adds server-side ERROR logging for unexpected failures during tool invocation so operators can diagnose tool call issues that previously only surfaced as CallToolResult(is_error=True) to clients.
Changes:
- Log exceptions in
_handle_call_toolwithlogger.exception(...)before returning an error result. - Add an interaction test ensuring tool-call exceptions emit an ERROR log record containing the tool name.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/mcp/server/mcpserver/server.py |
Logs exceptions raised during tool calls via logger.exception before returning an is_error tool result. |
tests/interaction/mcpserver/test_tools.py |
Adds a regression test asserting tool-call exceptions are logged at ERROR level on the server logger. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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 #3266.
_handle_call_toolsilently converts all exceptions intoCallToolResult(is_error=True)without any server-side logging, making tool failures invisible in server logs. The existing resource and prompt handlers both calllogger.exception()before handling the error — this PR applies the same pattern to tool calls.Before: A tool raising
ValueError("something went wrong")returns anis_errorresult to the client with no trace in server logs.After: The same exception is logged at
ERRORlevel (with full traceback vialogger.exception) before theis_errorresult is returned.Change
src/mcp/server/mcpserver/server.py— one line added in_handle_call_tool:This mirrors the pattern in
_handle_read_resource(line 566) and_handle_get_prompt(line 1296).Test
Added
test_call_tool_exception_is_logged_server_sidetotests/interaction/mcpserver/test_tools.py. It asserts that when a tool raises an exception, a log record atERRORlevel containing the tool name is emitted bymcp.server.mcpserver.server.