feat(python): add MCPToolProvider for MCP server integration#565
feat(python): add MCPToolProvider for MCP server integration#565cornelcroi wants to merge 1 commit into
Conversation
Adds MCPToolProvider as a drop-in AgentTools subclass that connects to one or more MCP servers (stdio or SSE) and exposes their tools transparently inside the existing tool_config mechanism — no changes to existing agents. Closes #299 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
| headers: Optional[dict[str, str]] = None | ||
|
|
||
|
|
||
| class MCPToolProvider(AgentTools): |
| try: | ||
| from .tools.mcp_tool_provider import MCPToolProvider, MCPServerConfig | ||
| __all__ = ["MCPToolProvider", "MCPServerConfig"] | ||
| except ImportError: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b490814bbb
ℹ️ 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".
| connect lazily via :meth:`tool_handler`. | ||
| """ | ||
| result = [] | ||
| for tool_name, (_session, mcp_tool) in self._tool_map.items(): |
There was a problem hiding this comment.
Populate MCP tools before formatting
When a fresh MCPToolProvider is passed to BedrockLLMAgent or AnthropicAgent as in the new example, the agents build the model request by calling to_bedrock_format()/to_claude_format() before tool_handler() ever runs. These formatters only iterate self._tool_map, which is empty until _ensure_connected() has been awaited, so the first request is sent with no MCP tools and the model never has a chance to call them. Please connect/list tools before formatting in the async agent path, or otherwise avoid returning an empty tool list on first use.
Useful? React with 👍 / 👎.
| mock_stdio_params_cls = MagicMock() | ||
|
|
||
| with ( | ||
| patch("agent_squad.tools.mcp_tool_provider.ClientSession", mock_client_session_cls), |
There was a problem hiding this comment.
Install or stub MCP before patching provider
In the Python CI workflow I checked, dependencies are installed only from python/test_requirements.txt, which does not include mcp. These tests claim to mock MCP, but patch("agent_squad.tools.mcp_tool_provider.ClientSession", ...) imports the package before the patch can apply; without the extra installed, agent_squad.tools is not importable and every test in this file errors during fixture setup. Add mcp to the test environment or stub the mcp modules in sys.modules before importing the provider.
Useful? React with 👍 / 👎.
Issue Link (REQUIRED)
Fixes #299
Summary
Changes
Adds
MCPToolProvider— a subclass ofAgentToolsthat connects to one or more MCP servers (stdio or SSE transport) and exposes their tools transparently inside the existingtool_configmechanism. No changes to any existing agent, classifier, or orchestrator.python/src/agent_squad/tools/mcp_tool_provider.pyMCPServerConfigdataclass covering stdio (command,args,env) and SSE (url,headers) transportsMCPToolProvider(AgentTools)with lazy connection, tool caching, and routing back to the correct server per tool callto_bedrock_format(),to_anthropic_format(),to_openai_format()— pass MCP's JSON Schema through directlyisErrorresults from MCP surfaced as error strings to the modelpython/setup.cfg: newmcp = mcp>=1.0.0optional extra, added toallpython/src/agent_squad/__init__.py: guarded re-export (silent skip whenmcpnot installed)User experience
Before: no MCP support.
After:
Install:
pip install agent-squad[mcp]Checklist
Acknowledgment
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.