MCP Servers
MCPServer
Bases: ABC
Base class for Model Context Protocol servers.
Source code in src/agents/mcp/server.py
__init__
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
use_structured_content
|
bool
|
Whether to use |
False
|
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
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 | |
__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 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
|
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
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 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 | |
__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 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
|
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
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 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 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 | |
__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, 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
|
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.