MCP Servers
MCPServer
Bases:ABC
Base class for Model Context Protocol servers.
Source code insrc/agents/mcp/server.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 | |
cached_toolsproperty
Return the most recently fetched tools list, if available.
Implementations may returnNone when tools have not been fetched yet or caching isdisabled.
__init__
__init__(use_structured_content:bool=False,require_approval:RequireApprovalSetting=None,failure_error_function:ToolErrorFunction|None|_UnsetType=_UNSET,tool_meta_resolver:MCPToolMetaResolver|None=None,)Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
use_structured_content | bool | Whether to use | False |
require_approval | RequireApprovalSetting | Approval policy for tools on this server. Accepts "always"/"never",a dict of tool names to those values, a boolean, or an object with always/nevertool lists (mirroring TS requireApproval). Normalized into a needs_approval policy. | None |
failure_error_function | ToolErrorFunction | None |_UnsetType | Optional function used to convert MCP tool failures intoa model-visible error message. If explicitly set to None, tool errors will beraised instead of converted. If left unset, the agent-level configuration (orSDK default) will be used. | _UNSET |
tool_meta_resolver | MCPToolMetaResolver | None | Optional callable that produces MCP request metadata ( | None |
Source code insrc/agents/mcp/server.py
connectabstractmethodasync
Connect to the server. For example, this might mean spawning a subprocess oropening a network connection. The server is expected to remain connected untilcleanup() is called.
cleanupabstractmethodasync
Cleanup the server. For example, this might mean closing a subprocess orclosing a network connection.
list_toolsabstractmethodasync
list_tools(run_context:RunContextWrapper[Any]|None=None,agent:AgentBase|None=None,)->list[Tool]call_toolabstractmethodasync
list_promptsabstractmethodasync
get_promptabstractmethodasync
MCPServerStdioParams
Bases:TypedDict
Mirrorsmcp.client.stdio.StdioServerParameters, but lets you pass params without anotherimport.
Source code insrc/agents/mcp/server.py
commandinstance-attribute
The executable to run to start the server. For example,python ornode.
argsinstance-attribute
Command line args to pass to thecommand executable. For example,['foo.py'] or['server.js', '--port', '8080'].
envinstance-attribute
The environment variables to set for the server. .
cwdinstance-attribute
The working directory to use when spawning the process.
encodinginstance-attribute
The text encoding used when sending/receiving messages to the server. Defaults toutf-8.
MCPServerStdio
Bases:_MCPServerWithClientSession
MCP server implementation that uses the stdio transport. See the [spec](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#stdio) fordetails.
Source code insrc/agents/mcp/server.py
__init__
__init__(params:MCPServerStdioParams,cache_tools_list:bool=False,name:str|None=None,client_session_timeout_seconds:float|None=5,tool_filter:ToolFilter=None,use_structured_content:bool=False,max_retry_attempts:int=0,retry_backoff_seconds_base:float=1.0,message_handler:MessageHandlerFnT|None=None,require_approval:RequireApprovalSetting=None,failure_error_function:ToolErrorFunction|None|_UnsetType=_UNSET,tool_meta_resolver:MCPToolMetaResolver|None=None,)Create a new MCP server based on the stdio transport.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params | MCPServerStdioParams | The params that configure the server. This includes the command to run tostart the server, the args to pass to the command, the environment variables toset for the server, the working directory to use when spawning the process, andthe text encoding used when sending/receiving messages to the server. | required |
cache_tools_list | bool | Whether to cache the tools list. If | False |
name | str | None | A readable name for the server. If not provided, we'll create one from thecommand. | None |
client_session_timeout_seconds | float | None | the read timeout passed to the MCP ClientSession. | 5 |
tool_filter | ToolFilter | The tool filter to use for filtering tools. | None |
use_structured_content | bool | Whether to use | False |
max_retry_attempts | int | Number of times to retry failed list_tools/call_tool calls.Defaults to no retries. | 0 |
retry_backoff_seconds_base | float | The base delay, in seconds, for exponentialbackoff between retries. | 1.0 |
message_handler | MessageHandlerFnT | None | Optional handler invoked for session messages as delivered by theClientSession. | None |
require_approval | RequireApprovalSetting | Approval policy for tools on this server. Accepts "always"/"never",a dict of tool names to those values, or an object with always/never tool lists. | None |
failure_error_function | ToolErrorFunction | None |_UnsetType | Optional function used to convert MCP tool failures intoa model-visible error message. If explicitly set to None, tool errors will beraised instead of converted. If left unset, the agent-level configuration (orSDK default) will be used. | _UNSET |
tool_meta_resolver | MCPToolMetaResolver | None | Optional callable that produces MCP request metadata ( | None |
Source code insrc/agents/mcp/server.py
create_streams
create_streams()->AbstractAsyncContextManager[tuple[MemoryObjectReceiveStream[SessionMessage|Exception],MemoryObjectSendStream[SessionMessage],GetSessionIdCallback|None,]]Create the streams for the server.
Source code insrc/agents/mcp/server.py
connectasync
Connect to the server.
Source code insrc/agents/mcp/server.py
cleanupasync
Cleanup the server.
Source code insrc/agents/mcp/server.py
list_toolsasync
list_tools(run_context:RunContextWrapper[Any]|None=None,agent:AgentBase|None=None,)->list[Tool]List the tools available on the server.
Source code insrc/agents/mcp/server.py
call_toolasync
Invoke a tool on the server.
Source code insrc/agents/mcp/server.py
list_promptsasync
List the prompts available on the server.
get_promptasync
Get a specific prompt from the server.
Source code insrc/agents/mcp/server.py
MCPServerSseParams
Bases:TypedDict
Mirrors the params inmcp.client.sse.sse_client.
Source code insrc/agents/mcp/server.py
timeoutinstance-attribute
The timeout for the HTTP request. Defaults to 5 seconds.
MCPServerSse
Bases:_MCPServerWithClientSession
MCP server implementation that uses the HTTP with SSE transport. See the [spec](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#http-with-sse)for details.
Source code insrc/agents/mcp/server.py
__init__
__init__(params:MCPServerSseParams,cache_tools_list:bool=False,name:str|None=None,client_session_timeout_seconds:float|None=5,tool_filter:ToolFilter=None,use_structured_content:bool=False,max_retry_attempts:int=0,retry_backoff_seconds_base:float=1.0,message_handler:MessageHandlerFnT|None=None,require_approval:RequireApprovalSetting=None,failure_error_function:ToolErrorFunction|None|_UnsetType=_UNSET,tool_meta_resolver:MCPToolMetaResolver|None=None,)Create a new MCP server based on the HTTP with SSE transport.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params | MCPServerSseParams | The params that configure the server. This includes the URL of the server,the headers to send to the server, the timeout for the HTTP request, and thetimeout for the SSE connection. | required |
cache_tools_list | bool | Whether to cache the tools list. If | False |
name | str | None | A readable name for the server. If not provided, we'll create one from theURL. | None |
client_session_timeout_seconds | float | None | the read timeout passed to the MCP ClientSession. | 5 |
tool_filter | ToolFilter | The tool filter to use for filtering tools. | None |
use_structured_content | bool | Whether to use | False |
max_retry_attempts | int | Number of times to retry failed list_tools/call_tool calls.Defaults to no retries. | 0 |
retry_backoff_seconds_base | float | The base delay, in seconds, for exponentialbackoff between retries. | 1.0 |
message_handler | MessageHandlerFnT | None | Optional handler invoked for session messages as delivered by theClientSession. | None |
require_approval | RequireApprovalSetting | Approval policy for tools on this server. Accepts "always"/"never",a dict of tool names to those values, or an object with always/never tool lists. | None |
failure_error_function | ToolErrorFunction | None |_UnsetType | Optional function used to convert MCP tool failures intoa model-visible error message. If explicitly set to None, tool errors will beraised instead of converted. If left unset, the agent-level configuration (orSDK default) will be used. | _UNSET |
tool_meta_resolver | MCPToolMetaResolver | None | Optional callable that produces MCP request metadata ( | None |
Source code insrc/agents/mcp/server.py
create_streams
create_streams()->AbstractAsyncContextManager[tuple[MemoryObjectReceiveStream[SessionMessage|Exception],MemoryObjectSendStream[SessionMessage],GetSessionIdCallback|None,]]Create the streams for the server.
Source code insrc/agents/mcp/server.py
connectasync
Connect to the server.
Source code insrc/agents/mcp/server.py
cleanupasync
Cleanup the server.
Source code insrc/agents/mcp/server.py
list_toolsasync
list_tools(run_context:RunContextWrapper[Any]|None=None,agent:AgentBase|None=None,)->list[Tool]List the tools available on the server.
Source code insrc/agents/mcp/server.py
call_toolasync
Invoke a tool on the server.
Source code insrc/agents/mcp/server.py
list_promptsasync
List the prompts available on the server.
get_promptasync
Get a specific prompt from the server.
Source code insrc/agents/mcp/server.py
MCPServerStreamableHttpParams
Bases:TypedDict
Mirrors the params inmcp.client.streamable_http.streamablehttp_client.
Source code insrc/agents/mcp/server.py
timeoutinstance-attribute
The timeout for the HTTP request. Defaults to 5 seconds.
sse_read_timeoutinstance-attribute
The timeout for the SSE connection, in seconds. Defaults to 5 minutes.
httpx_client_factoryinstance-attribute
httpx_client_factory:NotRequired[HttpClientFactory]Custom HTTP client factory for configuring httpx.AsyncClient behavior.
MCPServerStreamableHttp
Bases:_MCPServerWithClientSession
MCP server implementation that uses the Streamable HTTP transport. See the [spec](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#streamable-http)for details.
Source code insrc/agents/mcp/server.py
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 9991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090 | |
__init__
__init__(params:MCPServerStreamableHttpParams,cache_tools_list:bool=False,name:str|None=None,client_session_timeout_seconds:float|None=5,tool_filter:ToolFilter=None,use_structured_content:bool=False,max_retry_attempts:int=0,retry_backoff_seconds_base:float=1.0,message_handler:MessageHandlerFnT|None=None,require_approval:RequireApprovalSetting=None,failure_error_function:ToolErrorFunction|None|_UnsetType=_UNSET,tool_meta_resolver:MCPToolMetaResolver|None=None,)Create a new MCP server based on the Streamable HTTP transport.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params | MCPServerStreamableHttpParams | The params that configure the server. This includes the URL of the server,the headers to send to the server, the timeout for the HTTP request, thetimeout for the Streamable HTTP connection, whether we need toterminate on close, and an optional custom HTTP client factory. | required |
cache_tools_list | bool | Whether to cache the tools list. If | False |
name | str | None | A readable name for the server. If not provided, we'll create one from theURL. | None |
client_session_timeout_seconds | float | None | the read timeout passed to the MCP ClientSession. | 5 |
tool_filter | ToolFilter | The tool filter to use for filtering tools. | None |
use_structured_content | bool | Whether to use | False |
max_retry_attempts | int | Number of times to retry failed list_tools/call_tool calls.Defaults to no retries. | 0 |
retry_backoff_seconds_base | float | The base delay, in seconds, for exponentialbackoff between retries. | 1.0 |
message_handler | MessageHandlerFnT | None | Optional handler invoked for session messages as delivered by theClientSession. | None |
require_approval | RequireApprovalSetting | Approval policy for tools on this server. Accepts "always"/"never",a dict of tool names to those values, or an object with always/never tool lists. | None |
failure_error_function | ToolErrorFunction | None |_UnsetType | Optional function used to convert MCP tool failures intoa model-visible error message. If explicitly set to None, tool errors will beraised instead of converted. If left unset, the agent-level configuration (orSDK default) will be used. | _UNSET |
tool_meta_resolver | MCPToolMetaResolver | None | Optional callable that produces MCP request metadata ( | None |
Source code insrc/agents/mcp/server.py
create_streams
create_streams()->AbstractAsyncContextManager[tuple[MemoryObjectReceiveStream[SessionMessage|Exception],MemoryObjectSendStream[SessionMessage],GetSessionIdCallback|None,]]Create the streams for the server.
Source code insrc/agents/mcp/server.py
connectasync
Connect to the server.
Source code insrc/agents/mcp/server.py
cleanupasync
Cleanup the server.
Source code insrc/agents/mcp/server.py
list_toolsasync
list_tools(run_context:RunContextWrapper[Any]|None=None,agent:AgentBase|None=None,)->list[Tool]List the tools available on the server.
Source code insrc/agents/mcp/server.py
call_toolasync
Invoke a tool on the server.
Source code insrc/agents/mcp/server.py
list_promptsasync
List the prompts available on the server.
get_promptasync
Get a specific prompt from the server.