Skip to content

fix(agentgateway): opt-in handle_tool_error for graceful MCP tool-error propagation - #386

Merged
NicoleMGomes merged 2 commits into
SAP:mainfrom
prashantrakheja:feat/agw-tool-error-propagation
Oct 14, 2026
Merged

NicoleMGomes merged 2 commits into
SAP:mainfrom
prashantrakheja:feat/agw-tool-error-propagation

Conversation

@prashantrakheja

Copy link
Copy Markdown
Contributor

What

Adds an opt-in handle_tool_error parameter to mcp_tool_to_langchain. When set, Agent
Gateway tool-call failures are surfaced to the LLM as a LangChain ToolException (converted
to an error ToolMessage the model can see and self-correct on) instead of propagating as a
raw SDK exception that crashes the agent loop.

Default behavior is unchanged: when the parameter is omitted (None), the converter behaves
exactly as before.

Why

An agent that converts Agent Gateway tools with mcp_tool_to_langchain and then invokes one
would crash on any tool failure. Two things combined to cause this:

  1. The converter never raised ToolException, so LangChain's handle_tool_error (which only
    catches ToolException) could not intercept the failure.
  2. The converted tool left handle_tool_error at LangChain's default (False), so even a
    ToolException would be re-raised rather than handled.

Both MCP error kinds were affected: tool-execution errors (isError: true results) and
JSON-RPC protocol errors (e.g. -32602). Both crashed the agent loop instead of reaching the
LLM.

…or propagation

Adds an opt-in handle_tool_error parameter to mcp_tool_to_langchain. When set,
Agent Gateway tool-call failures are surfaced to the LLM as a LangChain ToolException
(converted to an error ToolMessage) instead of propagating as a raw SDK exception that
crashes the agent loop. Default behavior is unchanged when the parameter is omitted.

Covers both MCP error kinds (tool-execution isError results and JSON-RPC protocol
errors). Includes a version bump to 0.58.5.

4 new unit tests; 315 agentgateway tests pass; ruff + ty clean.
@prashantrakheja
prashantrakheja requested a review from a team as a code owner October 13, 2026 20:11
Comment thread src/sap_cloud_sdk/agentgateway/converters.py Outdated
Comment thread src/sap_cloud_sdk/agentgateway/converters.py
Comment thread src/sap_cloud_sdk/agentgateway/converters.py Outdated
Comment thread src/sap_cloud_sdk/agentgateway/converters.py
Address review feedback on error propagation:

- call_mcp_tool now surfaces JSON-RPC protocol errors (not just isError
  results): catch McpError (bare or wrapped in an anyio BaseExceptionGroup)
  and raise AgentGatewayServerError carrying the server message + code.
- Check isError before the empty-content early return so an error result
  with empty content raises instead of being swallowed as a success .
- agw_client re-raises a clean AgentGatewaySDKError found inside an
  ExceptionGroup instead of burying it under a generic wrapper; log a
  handled error at INFO (breadcrumb) rather than ERROR+stack trace.
- Drop the internal gateway URL from the isError exception message (kept
  in an operator WARNING log); keep the tool name for attribution.
- Remove review-flagged comments in converters.py; document
  handle_tool_error in the user guide.

Add unit tests for find_mcp_error, _find_sdk_error, grouped McpError,
and isError-with-empty-content.
@NicoleMGomes
NicoleMGomes merged commit bfe489b into SAP:main Oct 14, 2026
10 checks passed
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.

2 participants