### Summary: This enables users to **use** MCP inside the SDK. 1. You add a list of MCP servers to `Agent`, via `mcp_server=[...]` 2. When an agent runs, we look up its MCP tools and add them to the list of tools. 3. When a tool call occurs, we call the relevant MCP server. Notes: 1. There's some refactoring to make sure we send the full list of tools to the Runner/Model etc. 2. Right now, you could have a locally defined tool that conflicts with an MCP defined tool. I didn't add errors for that, will do in a followup. ### Test Plan: See unit tests. Also has an end to end example next PR.
38 lines
947 B
Python
38 lines
947 B
Python
import pytest
|
|
|
|
from agents.exceptions import UserError
|
|
from agents.mcp.server import _MCPServerWithClientSession
|
|
|
|
|
|
class CrashingClientSessionServer(_MCPServerWithClientSession):
|
|
def __init__(self):
|
|
super().__init__(cache_tools_list=False)
|
|
self.cleanup_called = False
|
|
|
|
def create_streams(self):
|
|
raise ValueError("Crash!")
|
|
|
|
async def cleanup(self):
|
|
self.cleanup_called = True
|
|
await super().cleanup()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_server_errors_cause_error_and_cleanup_called():
|
|
server = CrashingClientSessionServer()
|
|
|
|
with pytest.raises(ValueError):
|
|
await server.connect()
|
|
|
|
assert server.cleanup_called
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_not_calling_connect_causes_error():
|
|
server = CrashingClientSessionServer()
|
|
|
|
with pytest.raises(UserError):
|
|
await server.list_tools()
|
|
|
|
with pytest.raises(UserError):
|
|
await server.call_tool("foo", {})
|