MCP Servers
MCPServer
Bases:ABC
Base class for Model Context Protocol servers.
Source code insrc/agents/mcp/server.py
__init__
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
use_structured_content | bool | Whether to use | False |
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,)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 |
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
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,)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 |
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
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
__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,)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 |
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
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.