Skip to content

Commit 65d62be

Browse files
jonathan-fultoncrivetimihai
authored andcommitted
fix: propagate error messages in /mcp endpoint responses
Previously, exceptions in tool invocation were caught and an empty list was returned, hiding error details from clients. Now errors are re-raised to let the MCP SDK properly convert them to JSON-RPC error responses. This ensures clients see actual error messages (e.g., '401 Unauthorized') instead of empty responses. Fixes #2570 Signed-off-by: Jonathan Fulton <jonathan@jonathanfulton.com>
1 parent 4ec41c7 commit 65d62be

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

mcpgateway/transports/streamablehttp_transport.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,9 @@ def _convert_meta(meta: Any) -> dict[str, Any] | None:
639639
return unstructured
640640
except Exception as e:
641641
logger.exception(f"Error calling tool '{name}': {e}")
642-
return []
642+
# Re-raise the exception so the MCP SDK can properly convert it to an error response
643+
# This ensures error details are propagated to the client instead of returning empty results
644+
raise
643645

644646

645647
@mcp_app.list_tools()

tests/unit/mcpgateway/transports/test_streamablehttp_transport.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ async def fake_get_db():
261261

262262
@pytest.mark.asyncio
263263
async def test_call_tool_exception(monkeypatch, caplog):
264-
"""Test call_tool returns [] and logs exception on error."""
264+
"""Test call_tool re-raises exception after logging for proper MCP SDK error handling."""
265265
# First-Party
266266
from mcpgateway.transports.streamablehttp_transport import call_tool, tool_service
267267

@@ -275,8 +275,8 @@ async def fake_get_db():
275275
monkeypatch.setattr(tool_service, "invoke_tool", AsyncMock(side_effect=Exception("fail!")))
276276

277277
with caplog.at_level("ERROR"):
278-
result = await call_tool("mytool", {"foo": "bar"})
279-
assert result == []
278+
with pytest.raises(Exception, match="fail!"):
279+
await call_tool("mytool", {"foo": "bar"})
280280
assert "Error calling tool 'mytool': fail!" in caplog.text
281281

282282

0 commit comments

Comments
 (0)