Skip to content

fix: log exceptions in tool calls at ERROR level - #3271

Open
gingeekrishna wants to merge 1 commit into
modelcontextprotocol:mainfrom
gingeekrishna:fix/3266-log-tool-call-exceptions
Open

fix: log exceptions in tool calls at ERROR level#3271
gingeekrishna wants to merge 1 commit into
modelcontextprotocol:mainfrom
gingeekrishna:fix/3266-log-tool-call-exceptions

Conversation

@gingeekrishna

Copy link
Copy Markdown

Summary

Fixes #3266.

_handle_call_tool silently converts all exceptions into CallToolResult(is_error=True) without any server-side logging, making tool failures invisible in server logs. The existing resource and prompt handlers both call logger.exception() before handling the error — this PR applies the same pattern to tool calls.

Before: A tool raising ValueError("something went wrong") returns an is_error result to the client with no trace in server logs.

After: The same exception is logged at ERROR level (with full traceback via logger.exception) before the is_error result is returned.

Change

src/mcp/server/mcpserver/server.py — one line added in _handle_call_tool:

except Exception as e:
    logger.exception(f"Error calling tool {params.name}")  # ← added
    return CallToolResult(content=[TextContent(type="text", text=str(e))], is_error=True)

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_side to tests/interaction/mcpserver/test_tools.py. It asserts that when a tool raises an exception, a log record at ERROR level containing the tool name is emitted by mcp.server.mcpserver.server.

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>
Copilot AI lite review requested due to automatic review settings August 8, 2026 19:08

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Re-trigger cubic

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_tool with logger.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.

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.

Log exceptions in tool calls

2 participants