Skip to content

fix: error response for MCP - #341

Merged
NicoleMGomes merged 3 commits into
mainfrom
fix/mcp-error
Sep 16, 2026
Merged

NicoleMGomes merged 3 commits into
mainfrom
fix/mcp-error

Conversation

@NicoleMGomes

@NicoleMGomes NicoleMGomes commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Disclaimer: Do not include SAP-internal or customer-specific information in this PR (e.g. internal system URLs, customer names, tenant IDs, or confidential configurations). This is a public repository.

Description

Fixes a crash in call_mcp_tool_lob and call_mcp_tool_customer when an MCP tool returns an error response (isError=True).

Previously, when an MCP tool signalled a business error via the MCP protocol error flag, the SDK only logged the error and silently returned the error text as a successful result. This caused two problems:

  1. AttributeError: 'NoneType' object has no attribute 'isError' — when langchain-mcp-adapters (≥0.3.x) intercepts a CallToolResult with isError=True and raises a ToolException before returning to the SDK, the SDK received None from session.call_tool() and crashed on the .content access.
  2. Silent failure masking — even without langchain-mcp-adapters, an MCP tool error was silently swallowed and returned to the agent as a normal string, making the agent believe the tool call succeeded.

Fix: Both call_mcp_tool_lob (_lob.py) and call_mcp_tool_customer (_customer.py) now:

  • Guard against a None result from session.call_tool() and raise AgentGatewayServerError with a descriptive message.
  • Raise AgentGatewayServerError (instead of logging) when mcp_is_error(result) is True, so the error propagates correctly up to agw_client.call_mcp_tool() where it is caught and re-raised as AgentGatewaySDKError.

Related Issue

Closes #

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Code refactoring
  • Dependency update

How to Test

  1. Set up an agent that calls an MCP tool which returns an error response from the backend (e.g. an invalid change number in a BOM update).
  2. Without this fix, the call raises AttributeError: 'NoneType' object has no attribute 'isError' (when using langchain-mcp-adapters) or silently returns the error text as a success.
  3. With this fix, the call raises AgentGatewaySDKError: Tool invocation failed for '<name>': Tool '<name>' on '<url>' returned an error: <error text> — the agent framework receives a proper exception and can handle or surface it to the user.

Unit tests:

uv run pytest tests/agentgateway/unit/test_lob.py::TestCallMcpToolLob \
              tests/agentgateway/unit/test_customer.py::TestCallMcpToolCustomer -v

Checklist

  • I have read the Contributing Guidelines
  • I have verified that my changes solve the issue
  • I have added/updated automated tests to cover my changes
  • All tests pass locally
  • I have verified that my code follows the Code Guidelines
  • I have updated documentation (if applicable)
  • I have added type hints for all public APIs
  • My code does not contain sensitive information (credentials, tokens, etc.)
  • I have followed Conventional Commits for commit messages

Additional Notes

The AgentGatewayServerError exception class already existed and was already documented as the correct exception for isError=True tool results (exceptions.py:33). This fix aligns the actual behavior with the documented intent.

The outer except Exception handler in AgwClient.call_mcp_tool (agw_client.py) catches AgentGatewayServerError and wraps it in AgentGatewaySDKError with a Tool invocation failed for '<name>' prefix, so callers only need to handle AgentGatewaySDKError.

@NicoleMGomes
NicoleMGomes marked this pull request as ready for review September 16, 2026 11:56
@NicoleMGomes
NicoleMGomes requested a review from a team as a code owner September 16, 2026 11:56
@NicoleMGomes
NicoleMGomes merged commit 594d2a1 into main Sep 16, 2026
10 of 11 checks passed
@NicoleMGomes
NicoleMGomes deleted the fix/mcp-error branch September 16, 2026 14:04
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.

3 participants