Skip to content

fix(agentgateway): bound MCP protocol calls with a wall-clock deadline - #319

Open
wangzhengzhuo05 wants to merge 1 commit into
SAP:mainfrom
wangzhengzhuo05:fix/mcp-protocol-deadline
Open

fix(agentgateway): bound MCP protocol calls with a wall-clock deadline#319
wangzhengzhuo05 wants to merge 1 commit into
SAP:mainfrom
wangzhengzhuo05:fix/mcp-protocol-deadline

Conversation

@wangzhengzhuo05

Copy link
Copy Markdown

What

When an MCP server is unresponsive, session.initialize() / list_tools() / call_tool() in the LoB flow hang indefinitely instead of failing fast.

Root cause

The httpx.AsyncClient timeout only bounds individual HTTP chunk reads. An MCP stream's SSE keep-alives reset that timer continuously, so an unresponsive server never trips the client timeout — the protocol call stays alive forever.

How

Add _mcp_call_with_deadline(), which wraps a protocol call in asyncio.wait_for and raises AgentGatewaySDKError on timeout. Keep-alives cannot extend a wall-clock deadline. Applied to initialize, list_tools (in list_server_tools) and initialize, call_tool (in call_mcp_tool_lob), matching the issue's expectation that listing fails fast with a clear error.

Tests

  • test_initialize_timeout_raises_agent_gateway_error — stalled initialize fails fast on list_server_tools.
  • test_list_tools_timeout_raises_agent_gateway_error — stalled list_tools fails fast.
  • test_call_tool_timeout_raises_agent_gateway_error — stalled call_tool fails fast.
  • Mutation-checked: with the deadline removed, the test hangs indefinitely (the exact bug reported).
  • All 63 tests/agentgateway/unit/test_lob.py tests pass.

AI-generated code disclosure: This contribution was prepared with the assistance of an AI coding agent (Hermes Agent), following SAP's guideline for AI-generated code contributions. The fix and tests were reviewed and verified locally before submission.

Fixes #313

The httpx.AsyncClient timeout only bounds individual HTTP chunk reads;
SSE keep-alives from an unresponsive MCP server reset that timer
continuously, so session.initialize() / list_tools() / call_tool()
could hang forever instead of failing fast.

Add _mcp_call_with_deadline() which wraps a protocol call in
asyncio.wait_for, enforcing a deadline that keep-alives cannot extend,
and raises AgentGatewaySDKError on timeout. Apply it to initialize,
list_tools and call_tool in the LoB flow so tool listing and invocation
both surface a clear error when the server stalls.

Fixes SAP#313
@wangzhengzhuo05
wangzhengzhuo05 requested a review from a team as a code owner September 9, 2026 04:22
@cla-assistant

cla-assistant Bot commented Sep 9, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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.

MCP tool listing hangs indefinitely when server is unresponsive

1 participant