MCP Servers
MCPServer
基础: ABC
模型上下文协议服务器的基类。
源代码位于 src/agents/mcp/server.py
__init__
参数
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
使用结构化内容
|
bool
|
是否在调用 MCP 工具时使用 |
False
|
源代码位于 src/agents/mcp/server.py
connect abstractmethod async
连接到服务器。例如,这可能意味着启动一个子进程或打开一个网络连接。服务器应保持连接,直到调用 cleanup()。
cleanup abstractmethod async
list_tools abstractmethod async
list_tools(
run_context: RunContextWrapper[Any] | None = None,
agent: AgentBase | None = None,
) -> list[Tool]
call_tool abstractmethod async
list_prompts abstractmethod async
get_prompt abstractmethod async
MCPServerStdioParams
基础: TypedDict
镜像 mcp.client.stdio.StdioServerParameters,但允许您在没有其他导入的情况下传递参数。
源代码位于 src/agents/mcp/server.py
args instance-attribute
传递给 command 可执行文件的命令行参数。例如,['foo.py'] 或 ['server.js', '--port', '8080']。
MCPServerStdio
基类:_MCPServerWithClientSession
使用 stdio 传输的 MCP 服务器实现。有关详细信息,请参阅 [规范] (https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#stdio)。
源代码位于 src/agents/mcp/server.py
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 461 462 463 464 465 466 467 468 469 470 | |
__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,
)
创建基于 stdio 传输的新 MCP 服务器。
参数
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
参数
|
MCPServerStdioParams
|
配置服务器的参数。这包括启动服务器要运行的命令、传递给命令的参数、服务器要设置的环境变量、启动进程时要使用的当前工作目录以及发送/接收到服务器的消息使用的文本编码。 |
required |
缓存工具列表
|
bool
|
是否缓存工具列表。如果为 |
False
|
name
|
str | None
|
服务器的可读名称。如果未提供,我们将从命令创建它。 |
None
|
客户端会话超时秒数
|
float | None
|
传递给 MCP ClientSession 的读取超时时间。 |
5
|
工具过滤器
|
ToolFilter
|
用于过滤工具的工具过滤器。 |
None
|
使用结构化内容
|
bool
|
是否在调用 MCP 工具时使用 |
False
|
最大重试次数
|
int
|
失败的 list_tools/call_tool 调用的重试次数。默认为不重试。 |
0
|
重试退避秒数基数
|
float
|
重试之间指数退避的秒数基本延迟。 |
1.0
|
消息处理程序
|
MessageHandlerFnT | None
|
可选的处理程序,由 ClientSession 传递的消息调用。 |
None
|
源代码位于 src/agents/mcp/server.py
创建流
create_streams() -> AbstractAsyncContextManager[
tuple[
MemoryObjectReceiveStream[
SessionMessage | Exception
],
MemoryObjectSendStream[SessionMessage],
GetSessionIdCallback | None,
]
]
创建服务器的流。
源代码位于 src/agents/mcp/server.py
connect async
连接到服务器。
源代码位于 src/agents/mcp/server.py
cleanup async
list_tools async
list_tools(
run_context: RunContextWrapper[Any] | None = None,
agent: AgentBase | None = None,
) -> list[Tool]
列出服务器上可用的工具。
源代码位于 src/agents/mcp/server.py
call_tool async
调用服务器上的工具。
源代码位于 src/agents/mcp/server.py
list_prompts async
列出服务器上可用的提示。
源代码位于 src/agents/mcp/server.py
get_prompt async
从服务器获取特定提示。
源代码位于 src/agents/mcp/server.py
MCPServerSseParams
基础: TypedDict
镜像 mcp.client.sse.sse_client 中的参数。
源代码位于 src/agents/mcp/server.py
MCPServerSse
基类:_MCPServerWithClientSession
使用 HTTP 与 SSE 传输的 MCP 服务器实现。有关详细信息,请参阅 [规范] (https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#http-with-sse)。
源代码位于 src/agents/mcp/server.py
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 558 559 560 561 562 563 564 565 566 567 568 569 570 571 | |
__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,
)
创建基于 HTTP 与 SSE 传输的新 MCP 服务器。
参数
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
参数
|
MCPServerSseParams
|
配置服务器的参数。这包括服务器的 URL、发送到服务器的标头、HTTP 请求的超时时间以及 SSE 连接的超时时间。 |
required |
缓存工具列表
|
bool
|
是否缓存工具列表。如果为 |
False
|
name
|
str | None
|
服务器的可读名称。如果未提供,我们将从 URL 创建它。 |
None
|
客户端会话超时秒数
|
float | None
|
传递给 MCP ClientSession 的读取超时时间。 |
5
|
工具过滤器
|
ToolFilter
|
用于过滤工具的工具过滤器。 |
None
|
使用结构化内容
|
bool
|
是否在调用 MCP 工具时使用 |
False
|
最大重试次数
|
int
|
失败的 list_tools/call_tool 调用的重试次数。默认为不重试。 |
0
|
重试退避秒数基数
|
float
|
重试之间指数退避的秒数基本延迟。 |
1.0
|
消息处理程序
|
MessageHandlerFnT | None
|
可选的处理程序,由 ClientSession 传递的消息调用。 |
None
|
源代码位于 src/agents/mcp/server.py
创建流
create_streams() -> AbstractAsyncContextManager[
tuple[
MemoryObjectReceiveStream[
SessionMessage | Exception
],
MemoryObjectSendStream[SessionMessage],
GetSessionIdCallback | None,
]
]
创建服务器的流。
源代码位于 src/agents/mcp/server.py
connect async
连接到服务器。
源代码位于 src/agents/mcp/server.py
cleanup async
list_tools async
list_tools(
run_context: RunContextWrapper[Any] | None = None,
agent: AgentBase | None = None,
) -> list[Tool]
列出服务器上可用的工具。
源代码位于 src/agents/mcp/server.py
call_tool async
调用服务器上的工具。
源代码位于 src/agents/mcp/server.py
list_prompts async
列出服务器上可用的提示。
源代码位于 src/agents/mcp/server.py
get_prompt async
从服务器获取特定提示。
源代码位于 src/agents/mcp/server.py
MCPServerStreamableHttpParams
基础: TypedDict
镜像 mcp.client.streamable_http.streamablehttp_client 中的参数。
源代码位于 src/agents/mcp/server.py
sse_read_timeout instance-attribute
SSE 连接的超时时间,以秒为单位。默认为 5 分钟。
httpx_client_factory instance-attribute
httpx_client_factory: NotRequired[HttpClientFactory]
配置 httpx.AsyncClient 行为的自定义 HTTP 客户端工厂。
MCPServerStreamableHttp
基类:_MCPServerWithClientSession
使用 Streamable HTTP 传输的 MCP 服务器实现。有关详细信息,请参阅 [规范] (https://modelcontextprotocol.com.cn/specification/2025-03-26/basic/transports#streamable-http)。
源代码位于 src/agents/mcp/server.py
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 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 | |
__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,
)
创建基于 Streamable HTTP 传输的新 MCP 服务器。
参数
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
参数
|
MCPServerStreamableHttpParams
|
配置服务器的参数。这包括服务器的 URL、发送到服务器的标头、HTTP 请求的超时时间、Streamable HTTP 连接的超时时间、是否需要在关闭时终止以及可选的自定义 HTTP 客户端工厂。 |
required |
缓存工具列表
|
bool
|
是否缓存工具列表。如果为 |
False
|
name
|
str | None
|
服务器的可读名称。如果未提供,我们将从 URL 创建它。 |
None
|
客户端会话超时秒数
|
float | None
|
传递给 MCP ClientSession 的读取超时时间。 |
5
|
工具过滤器
|
ToolFilter
|
用于过滤工具的工具过滤器。 |
None
|
使用结构化内容
|
bool
|
是否在调用 MCP 工具时使用 |
False
|
最大重试次数
|
int
|
失败的 list_tools/call_tool 调用的重试次数。默认为不重试。 |
0
|
重试退避秒数基数
|
float
|
重试之间指数退避的秒数基本延迟。 |
1.0
|
消息处理程序
|
MessageHandlerFnT | None
|
可选的处理程序,由 ClientSession 传递的消息调用。 |
None
|
源代码位于 src/agents/mcp/server.py
创建流
create_streams() -> AbstractAsyncContextManager[
tuple[
MemoryObjectReceiveStream[
SessionMessage | Exception
],
MemoryObjectSendStream[SessionMessage],
GetSessionIdCallback | None,
]
]
创建服务器的流。
源代码位于 src/agents/mcp/server.py
connect async
连接到服务器。
源代码位于 src/agents/mcp/server.py
cleanup async
list_tools async
list_tools(
run_context: RunContextWrapper[Any] | None = None,
agent: AgentBase | None = None,
) -> list[Tool]
列出服务器上可用的工具。
源代码位于 src/agents/mcp/server.py
call_tool async
调用服务器上的工具。
源代码位于 src/agents/mcp/server.py
list_prompts async
列出服务器上可用的提示。
源代码位于 src/agents/mcp/server.py
get_prompt async
从服务器获取特定提示。