fix: log exceptions in tool calls at ERROR level

Closed
GI
2mo ago
+27-0

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.