Skip to content

Conversation

@habema
Copy link
Contributor

@habema habema commented Dec 31, 2025

Fixes an issue where MCP tool timeouts (and other errors) would halt the entire agent flow instead of being handled gracefully.

Problem:
When an MCP tool timed out or errored, it would raise an AgentsException that propagated up and stopped the agent. This was inconsistent with regular function tools, which handle errors gracefully via failure_error_function.

Solution:
Wrapped the MCP tool invocation in to_function_tool with error handling that catches exceptions and converts them to error messages using default_tool_error_function, matching the behavior of regular function tools.

Changes:

  • Added error handling wrapper in MCPUtil.to_function_tool()
  • Errors now return error messages instead of raising exceptions
  • New tests added

Repro Script:

import asyncio

from agents import Agent, Runner
from agents.mcp import MCPServerStreamableHttp
from agents.model_settings import ModelSettings


async def main():
    async with MCPServerStreamableHttp(
        name="Streamable HTTP Python Server",
        client_session_timeout_seconds=1, # Very low value for testing purposes
        params={
            "url": "https://docs.mcp.cloudflare.com/mcp",
        },
    ) as server:
        agent = Agent(
            name="Assistant",
            instructions="Use the tools to answer the questions.",
            mcp_servers=[server],
            model_settings=ModelSettings(tool_choice="required"),
        )

        message = "Can you give me some information on how to use the Workers AI Search binding?"
        result = await Runner.run(starting_agent=agent, input=message)
        print(result.final_output)


if __name__ == "__main__":
    asyncio.run(main())

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

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.

1 participant