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
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
|
__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,
)
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
|
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.
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
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 |
|
__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,
)
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
|
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.
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.
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
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 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 |
|
__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,
)
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, and the timeout for the Streamable HTTP connection and whether we need to terminate on close. |
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
|
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.
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.