Skip to content

Conversation

@Unshure
Copy link
Member

@Unshure Unshure commented Sep 25, 2025

Description

Fixed the timeout issue with the mcp client when the http server times out before the tool finishes

I was able to replicate this bug by creating an mcp server with a tool that waits ~5 seconds, and then configuring the mcp client to time out before that:
MCP Server

from fastmcp import FastMCP
import time

app = FastMCP()

@app.tool("say")
def say(input: str) -> str:
    """Says something."""
    time.sleep(5)
    return f"{input}!"

if __name__ == "__main__":
    app.run(transport="http", host="localhost", port=8080)
    # app.run()

Test code:

from strands.tools.mcp import MCPClient
from mcp.client.streamable_http import streamablehttp_client

client = MCPClient(lambda: streamablehttp_client(
    sse_read_timeout=2,
    url="http://localhost:8080/mcp"))

with client as mcp:
    mcp.call_tool_sync(tool_use_id="123", name="say", arguments={"input": "Hello!"})

I ended up figuring out this is due to an odd behavior in the MCP client where it does not raise an exception automatically. The MCP Client we use, defined in mcp code here, takes in a parameter called message_handler which is meant to handle different kind of messages returned by the underlying server. One of those message types is an exception(ref), so if you dont raise that exception, it will be silently swallowed. This swallowed exception does not close the tool connection, and this causes the application to hang.

Ive implemented a message handler that checks if the message is an exception, and if so raises it. This helps avoid the silent failure we have seen so far. As not raising an exception is a very odd behavior, I have reached out on the mcp repo to try and understand why this is the case: modelcontextprotocol/python-sdk#1401

Related Issues

Documentation PR

N/A

Type of Change

Bug fix

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

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