feat: support execution timeout for MCP tools in toolkit#1244
feat: support execution timeout for MCP tools in toolkit#1244owenliang wants to merge 2 commits intoagentscope-ai:mainfrom
Conversation
Add execution_timeout parameter to register_mcp_tools() to control MCP tool function execution timeout.
Summary of ChangesHello @owenliang, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the robustness of MCP tool function execution by integrating a configurable timeout mechanism. It addresses potential issues where tool functions might block indefinitely due to external factors like network problems, providing users with better control over their application's responsiveness and stability. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a timeout mechanism for MCP tool execution by adding an execution_timeout parameter to register_mcp_client. This is a valuable addition for handling potential blocking issues.
My review has identified a couple of points that need attention:
- The docstring for the new parameter doesn't fully align with the project's style guide for optional arguments.
- A more significant issue is the absence of unit tests for this new feature, which is a mandatory requirement according to the repository's contribution guidelines.
Please address these points to ensure the new feature is robust and well-documented.
- Update HttpStatefulClient to use httpx.AsyncClient with streamable_http_client - Update HttpStatelessClient to use httpx.AsyncClient with streamable_http_client - Pass client_kwargs directly to httpx.AsyncClient for flexible configuration - Fix execution_timeout docstring from defaults to to optional - Add execution_timeout test cases for both SSE and Streamable HTTP clients - Add slow_tool helper for timeout testing with time-based assertions - Disable slow_tool in existing tests to avoid schema mismatch
SummaryThis PR introduces two major changes to the MCP client implementation: 1. feat: Add
|
| File | Changes |
|---|---|
src/agentscope/mcp/_http_stateful_client.py |
Migrated to v2 API |
src/agentscope/mcp/_http_stateless_client.py |
Migrated to v2 API + client_kwargs passthrough |
src/agentscope/tool/_toolkit.py |
Docstring fix |
tests/mcp_sse_client_test.py |
Added timeout tests |
tests/mcp_streamable_http_client_test.py |
Added timeout tests |
Testing
- All 6 MCP-related tests pass
- 241 total tests pass (80 failures are due to missing optional dependencies, unrelated to this change)
- No deprecation warnings from MCP SDK
|
Additionally, a bug causing the MCP streamable HTTP client to hang was discovered. The issue has already been fixed, but a new version of the MCP Python package has not yet been released. We need to update the dependency promptly once it's available; otherwise, if the MCP server encounters an error during an MCP tool call, the call will hang indefinitely: modelcontextprotocol/python-sdk#1577 |
PR Title
AgentScope Version
Description
Background: MCP tool function execution may block indefinitely due to network or server issues, requiring timeout control.
Purpose: Add
execution_timeoutparameter toregister_mcp_tools()method to allow users to set execution timeout for MCP tool functions.Changes:
_toolkit.pyline 841: Addexecution_timeout: float | None = Noneparameter to method signature_toolkit.pylines 875-877: Add parameter documentation_toolkit.pyline 940: Passexecution_timeouttoget_callable_function()How to test:
Checklist
pre-commit run --all-filescommand