MCP Servers
MCPServer
Bases: ABC
Base class for Model Context Protocol servers.
Source code in src/agents/mcp/server.py
connect
abstractmethod
async
Connect to the server. For example, this might mean spawning a subprocess or
opening a network connection. The server is expected to remain connected until
cleanup()
is called.
cleanup
abstractmethod
async
Cleanup the server. For example, this might mean closing a subprocess or closing a network connection.
list_tools
abstractmethod
async
call_tool
abstractmethod
async
MCPServerStdioParams
Bases: TypedDict
Mirrors mcp.client.stdio.StdioServerParameters
, but lets you pass params without another
import.
Source code in src/agents/mcp/server.py
command
instance-attribute
The executable to run to start the server. For example, python
or node
.
args
instance-attribute
Command line args to pass to the command
executable. For example, ['foo.py']
or
['server.js', '--port', '8080']
.
env
instance-attribute
The environment variables to set for the server. .
cwd
instance-attribute
The working directory to use when spawning the process.
encoding
instance-attribute
The text encoding used when sending/receiving messages to the server. Defaults to utf-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) for details.
Source code in src/agents/mcp/server.py
connect
async
Connect to the server.
Source code in src/agents/mcp/server.py
cleanup
async
list_tools
async
List the tools available on the server.
Source code in src/agents/mcp/server.py
call_tool
async
Invoke a tool on the server.
Source code in src/agents/mcp/server.py
invalidate_tools_cache
__init__
__init__(
params: MCPServerStdioParams,
cache_tools_list: bool = False,
name: str | 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 to start the server, the args to pass to the command, the environment variables to set for the server, the working directory to use when spawning the process, and the 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 the command. |
None
|
Source code in src/agents/mcp/server.py
MCPServerSseParams
Bases: TypedDict
Mirrors the params inmcp.client.sse.sse_client
.
Source code in src/agents/mcp/server.py
timeout
instance-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 in src/agents/mcp/server.py
connect
async
Connect to the server.
Source code in src/agents/mcp/server.py
cleanup
async
list_tools
async
List the tools available on the server.
Source code in src/agents/mcp/server.py
call_tool
async
Invoke a tool on the server.
Source code in src/agents/mcp/server.py
invalidate_tools_cache
__init__
__init__(
params: MCPServerSseParams,
cache_tools_list: bool = False,
name: str | 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 the timeout 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 the URL. |
None
|
Source code in src/agents/mcp/server.py
create_streams
create_streams() -> AbstractAsyncContextManager[
tuple[
MemoryObjectReceiveStream[
JSONRPCMessage | Exception
],
MemoryObjectSendStream[JSONRPCMessage],
]
]
Create the streams for the server.