Skip to content

Commit 5a93a4e

Browse files
Varun SharmaCopilot
andcommitted
fix: add coverage for ToolError passthrough and defensive handler
- Add test for ToolError passthrough (covers tools/base.py except ToolError) - Mark unreachable defensive except in _handle_call_tool as no cover Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 7cf339c commit 5a93a4e

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

src/mcp/server/mcpserver/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ async def _handle_call_tool(
306306
raise
307307
except ToolError as e:
308308
return CallToolResult(content=[TextContent(type="text", text=str(e))], is_error=True)
309-
except Exception:
309+
except Exception: # pragma: no cover
310310
logger.exception(f"Error calling tool {params.name}")
311311
return CallToolResult(
312312
content=[TextContent(type="text", text=f"An unexpected error occurred calling tool {params.name}")],

tests/server/mcpserver/test_server.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ def error_tool_fn() -> None:
212212
raise ValueError("Test error")
213213

214214

215+
def tool_error_fn() -> None:
216+
raise ToolError("Intentional tool error")
217+
218+
215219
def image_tool_fn(path: str) -> Image:
216220
return Image(path)
217221

@@ -287,6 +291,17 @@ async def test_tool_error_details(self):
287291
assert "Test error" not in content.text
288292
assert result.is_error is True
289293

294+
async def test_tool_error_passthrough(self):
295+
"""Test that ToolError raised in tool is passed through with its message."""
296+
mcp = MCPServer()
297+
mcp.add_tool(tool_error_fn)
298+
async with Client(mcp) as client:
299+
result = await client.call_tool("tool_error_fn", {})
300+
assert result.is_error is True
301+
content = result.content[0]
302+
assert isinstance(content, TextContent)
303+
assert "Intentional tool error" in content.text
304+
290305
async def test_tool_return_value_conversion(self):
291306
mcp = MCPServer()
292307
mcp.add_tool(tool_fn)

0 commit comments

Comments
 (0)