MCP Servers
MCPServer
Bases: ABC
Base class for Model Context Protocol servers.
Source code in src/agents/mcp/server.py
63 64 65 66 67 68 69 70 71 72 73 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | |
__init__
__init__(
use_structured_content: bool = False,
require_approval: RequireApprovalSetting = None,
failure_error_function: ToolErrorFunction
| None
| _UnsetType = _UNSET,
)
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/never tool 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 into a model-visible error message. If explicitly set to None, tool errors will be raised instead of converted. If left unset, the agent-level configuration (or SDK default) will be used. |
_UNSET
|
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
list_tools(
run_context: RunContextWrapper[Any] | None = None,
agent: AgentBase | None = None,
) -> list[Tool]
List the tools available on the server.
call_tool
abstractmethod
async
list_prompts
abstractmethod
async
get_prompt
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
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 | |
__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,
)
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
|
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 exponential backoff between retries. |
1.0
|
message_handler
|
MessageHandlerFnT | None
|
Optional handler invoked for session messages as delivered by the ClientSession. |
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 into a model-visible error message. If explicitly set to None, tool errors will be raised instead of converted. If left unset, the agent-level configuration (or SDK default) will be used. |
_UNSET
|
Source code in src/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 in src/agents/mcp/server.py
connect
async
Connect to the server.
Source code in src/agents/mcp/server.py
cleanup
async
Cleanup the server.
Source code in src/agents/mcp/server.py
list_tools
async
list_tools(
run_context: RunContextWrapper[Any] | None = None,
agent: AgentBase | None = None,
) -> list[Tool]
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
list_prompts
async
List the prompts available on the server.
Source code in src/agents/mcp/server.py
get_prompt
async
Get a specific prompt from the server.
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
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 | |
__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,
)
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
|
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 exponential backoff between retries. |
1.0
|
message_handler
|
MessageHandlerFnT | None
|
Optional handler invoked for session messages as delivered by the ClientSession. |
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 into a model-visible error message. If explicitly set to None, tool errors will be raised instead of converted. If left unset, the agent-level configuration (or SDK default) will be used. |
_UNSET
|
Source code in src/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 in src/agents/mcp/server.py
connect
async
Connect to the server.
Source code in src/agents/mcp/server.py
cleanup
async
Cleanup the server.
Source code in src/agents/mcp/server.py
list_tools
async
list_tools(
run_context: RunContextWrapper[Any] | None = None,
agent: AgentBase | None = None,
) -> list[Tool]
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
list_prompts
async
List the prompts available on the server.
Source code in src/agents/mcp/server.py
get_prompt
async
Get a specific prompt from the server.
Source code in src/agents/mcp/server.py
MCPServerStreamableHttpParams
Bases: TypedDict
Mirrors the params inmcp.client.streamable_http.streamablehttp_client.
Source code in src/agents/mcp/server.py
timeout
instance-attribute
The timeout for the HTTP request. Defaults to 5 seconds.
sse_read_timeout
instance-attribute
The timeout for the SSE connection, in seconds. Defaults to 5 minutes.
httpx_client_factory
instance-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 in src/agents/mcp/server.py
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 | |
__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,
)
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, the timeout for the Streamable HTTP connection, whether we need to terminate 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 the URL. |
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 exponential backoff between retries. |
1.0
|
message_handler
|
MessageHandlerFnT | None
|
Optional handler invoked for session messages as delivered by the ClientSession. |
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 into a model-visible error message. If explicitly set to None, tool errors will be raised instead of converted. If left unset, the agent-level configuration (or SDK default) will be used. |
_UNSET
|
Source code in src/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 in src/agents/mcp/server.py
connect
async
Connect to the server.
Source code in src/agents/mcp/server.py
cleanup
async
Cleanup the server.
Source code in src/agents/mcp/server.py
list_tools
async
list_tools(
run_context: RunContextWrapper[Any] | None = None,
agent: AgentBase | None = None,
) -> list[Tool]
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
list_prompts
async
List the prompts available on the server.
Source code in src/agents/mcp/server.py
get_prompt
async
Get a specific prompt from the server.