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".

@seratch
Copy link
Member

seratch commented Jan 5, 2026

I will check later but overall it looks safe and good to me.

@seratch
Copy link
Member

seratch commented Jan 19, 2026

@codex review again

@seratch seratch marked this pull request as draft January 19, 2026 06:37
@seratch seratch removed this from the 0.7.x milestone Jan 19, 2026
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.

Reviewed commit: a64f752bb1

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +169 to +171
wrapped_error = AgentsException("Error invoking MCP tool crashing_tool: Crash!")
expected_error_msg = default_tool_error_function(tool_context, wrapped_error)
assert result == expected_error_msg

Choose a reason for hiding this comment

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

P2 Badge Fix expected error message mismatch in MCP test

The test builds expected_error_msg from AgentsException("Error invoking MCP tool crashing_tool: Crash!"), but invoke_mcp_tool actually wraps failures as "Error invoking MCP tool <name> on server '<server>'", so the result returned by function_tool.on_invoke_tool will include the server name (e.g., fake_mcp_server) and this exact equality assertion will fail. To keep the test reliable, construct the expected message using the same format as invoke_mcp_tool or assert on a stable substring.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants