Skip to content

fix(mcp): apply timeout_s to tools registered with @self.tool() - #1731

Open
Aafimalek wants to merge 4 commits into
openenv:mainfrom
Aafimalek:fix/mcp-mode-tool-timeout
Open

Aafimalek wants to merge 4 commits into
openenv:mainfrom
Aafimalek:fix/mcp-mode-tool-timeout

Conversation

@Aafimalek

@Aafimalek Aafimalek commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

In _async_handle_call_tool, tools registered with @self.tool() (the mode-aware branch) were called directly. Only the FastMCP path used asyncio.wait_for, so timeout_s was ignored: a tool sleeping 3 s under timeout_s=0.5 returned after 3.1 s with no error. Sync tools also blocked the event loop.

The branch now awaits the call under asyncio.wait_for, running sync functions through asyncio.to_thread. On expiry it returns the same ToolErrorType.TIMEOUT error and message as the FastMCP path.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • New environment
  • Refactoring

Alignment Checklist

Before submitting, verify:

  • I have read .claude/docs/PRINCIPLES.md and this PR aligns with our principles
  • I have checked .claude/docs/INVARIANTS.md and no invariants are violated
  • I have run /pre-submit-pr (or bash .claude/hooks/lint.sh and tests) and addressed all issues

RFC Status

  • Not required (bug fix, docs, minor refactoring)
  • RFC exists: #___
  • RFC needed (will create before merge)

Test Plan

  • New TestModeToolTimeout::test_slow_mode_tool_times_out[sync|async] in tests/core/test_mcp/test_mode_aware_tools.py. On main: 2 failed, 19 passed. With the fix: 21 passed, and tests/core/test_mcp plus test_mode_selection.py give 166 passed (WSL, Python 3.12).
  • bash .claude/hooks/lint.sh passes.
  • Not run: the full test suite.

Claude Code Review

Alignment review by Opus 5.5, in the format of .claude/skills/alignment-review/SKILL.md.

Automated Checks

  • Lint: PASS, bash .claude/hooks/lint.sh
  • Debug code: CLEAN

Tier 1: Fixes Required

None.

Tier 2: Alignment Discussion

A sync tool that times out keeps running in its worker thread until it returns, because Python can't cancel a thread. The step returns the TIMEOUT error, but step() called through asyncio.run still waits for that thread at shutdown. The FastMCP path has the same limitation for tools run in threads.


Note

Medium Risk
Changes core MCP tool execution (timeouts, threading, error classification) for all @self.tool() calls; behavior is more correct but sync tools now run off the event loop with documented caveats.

Overview
Fixes a bug where timeout_s was ignored for tools registered via @self.tool() (the mode-aware branch in _async_handle_call_tool). Those calls ran inline without asyncio.wait_for, while FastMCP-registered tools already honored the timeout.

Mode-aware invocations are now wrapped in asyncio.wait_for with the same default and ToolErrorType.TIMEOUT response as the FastMCP path. Sync tools run through asyncio.to_thread so they no longer block the event loop. TimeoutError raised inside a tool (e.g. upstream HTTP) is distinguished from MCP deadline expiry via _ToolRaisedTimeout and surfaced as EXECUTION_ERROR, not timeout.

The @self.tool() docstring documents thread-affine state and that timed-out sync work may keep running until the thread finishes.

TestModeToolTimeout adds coverage for slow sync/async tools, in-tool timeouts, fast return on expiry, and non-blocking sync execution.

Reviewed by Cursor Bugbot for commit 7c31dcd. Bugbot is set up for automated code reviews on this repo. Configure here.

Mode-aware tools were called directly, ignoring the step timeout, and sync tools blocked the event loop. Run them under asyncio.wait_for (sync ones via asyncio.to_thread) and return the same TIMEOUT error as the FastMCP path.
cursoragent and others added 2 commits December 2, 2026 11:47
…and event-loop tests

Co-authored-by: burtenshaw <burtenshaw@users.noreply.github.com>
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

The docs for this pull request built successfully. A maintainer can publish a preview of this commit by adding the docs-preview label and re-running the Docs check.

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want higher recall? High effort reviews run extra passes and find more bugs. A team admin can switch effort levels in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit f616949. Configure here.

The timeout handler for @self.tool() tools also caught TimeoutError
raised inside the tool (socket, urllib, aiohttp). On Python 3.11+
asyncio.TimeoutError is the builtin TimeoutError, so those were
reported as ToolErrorType.TIMEOUT with a message naming timeout_s.

Wrap the tool's own timeout before it reaches wait_for, so only
timeout_s expiring maps to TIMEOUT. The tool's error is returned as
EXECUTION_ERROR with its own message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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