Skip to content

[BUG] MCP Client Doesn't Recover from a Timeout #664

@wt-bryan

Description

@wt-bryan

Checks

  • I have updated to the lastest minor and patch version of Strands
  • I have checked the documentation and this is not expected behavior
  • I have searched ./issues and there are no duplicates of my issue

Strands Version

1.4.0

Python Version

3.12.11

Operating System

macOS 15.6

Installation Method

pip

Steps to Reproduce

Code:

import asyncio
from dotenv import load_dotenv
from fastmcp.client.auth import OAuth
from mcp.client.sse import sse_client
import os
from strands import Agent
from strands.models.litellm import LiteLLMModel
from strands.tools.mcp import MCPClient

load_dotenv()

atlassian_mcp_url = "https://mcp.atlassian.com/v1/sse"


model = LiteLLMModel(
    client_args={"api_key": os.getenv("LITE_LLM_KEY")},
    # **model_config
    model_id="claude-4-sonnet",
    params={
        "max_tokens": 1000,
        "temperature": 0.7,
    },
)


async def test_strands_agents_mcp_sse(prompt: str, sse_mcp_client: MCPClient):
    try:
        # First run: Don't approve or deny OAuth permissions - wait for 30 seconds to get the timeout
        # Second run: This and all subsequent attempt to connect will immediately fail
        with sse_mcp_client:
            tools = sse_mcp_client.list_tools_sync()
            agent = Agent(model=model, tools=tools)
            agent(prompt)
    except Exception as e:
        print(f"Error connecting to Strands MCP server: {e}")
        return


async def main():
    #### Atlassian MCP setup ####
    oauth = OAuth(mcp_url=atlassian_mcp_url)

    sse_mcp_client_oauth = MCPClient(
        lambda: sse_client(url=atlassian_mcp_url, auth=oauth)
    )

    await test_strands_agents_mcp_sse("Which tickets are open?", sse_mcp_client_oauth)

    await test_strands_agents_mcp_sse(
        "Which tickets are in progress?", sse_mcp_client_oauth
    )


if __name__ == "__main__":
    asyncio.run(main())
  1. Run the code snippet
  2. When the OAuth screen appears, do nothing for 30+ seconds

Expected Behavior

The next request attempts the OAuth flow again

Actual Behavior

Subsequent requests fail immediately when attempting to connect to MCP Server with Error connecting to Strands MCP server: the client session is currently running error.

Additional Context

This shouldn't be specific to the Atlassian MCP server, it's just the only one that I know of that I have access to and requires OAuth. It also isn't specific to FastMCP. The issue should also be reproducible if some other process caused a timeout after the _background_thread is started in MCPClient.start()

This might be related to #625 but I opened a separate issue since that issue states that no exception is thrown. It also states that the agent gets frozen waiting for a response and I don't observe that happening here.

Possible Solution

The issue seems to be within MCPClient.start(). The background thread is started but isn't closed/cleaned up if an error occurs.

Related Issues

No response

Metadata

Metadata

Assignees

Labels

area-mcpMCP relatedbugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions