MCP Servers
MCPServer
Bases: ABC
Base class for Model Context Protocol servers.
ソースコード位置: src/agents/mcp/server.py
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 471 472 473 474 475 476 477 478 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 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 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 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 | |
cached_tools
property
Return the most recently fetched tools list, if available.
Implementations may return None when tools have not been fetched yet or caching is
disabled.
__init__
__init__(
use_structured_content: bool = False,
require_approval: RequireApprovalSetting = None,
failure_error_function: ToolErrorFunction
| None
| _UnsetType = _UNSET,
tool_meta_resolver: MCPToolMetaResolver | None = None,
custom_data_extractor: MCPToolCustomDataExtractor
| None = None,
)
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
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, an object with always/never
tool lists (mirroring TS requireApproval), or a sync/async callable that receives
|
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
|
tool_meta_resolver
|
MCPToolMetaResolver | None
|
Optional callable that produces MCP request metadata ( |
None
|
custom_data_extractor
|
MCPToolCustomDataExtractor | None
|
Optional callable that produces SDK-only custom data for emitted MCP tool output items. |
None
|
ソースコード位置: 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
list_resources
async
List the resources available on the server.
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
cursor
|
str | None
|
An opaque pagination cursor returned in a previous
:class: |
None
|
Returns a :class:~mcp.types.ListResourcesResult. When the result contains
a nextCursor field, call this method again with that cursor to retrieve
the next page. Subclasses that do not support resources may leave this
unimplemented; it will raise :exc:NotImplementedError at call time.
ソースコード位置: src/agents/mcp/server.py
list_resource_templates
async
List the resource templates available on the server.
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
cursor
|
str | None
|
An opaque pagination cursor returned in a previous
:class: |
None
|
Returns a :class:~mcp.types.ListResourceTemplatesResult. When the result
contains a nextCursor field, call this method again with that cursor to
retrieve the next page. Subclasses that do not support resource templates
may leave this unimplemented; it will raise :exc:NotImplementedError at
call time.
ソースコード位置: src/agents/mcp/server.py
read_resource
async
Read the contents of a specific resource by URI.
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
uri
|
str
|
The URI of the resource to read. See :class: |
必須 |
Returns a :class:~mcp.types.ReadResourceResult. Subclasses that do not
support resources may leave this unimplemented; it will raise
:exc:NotImplementedError at call time.
ソースコード位置: src/agents/mcp/server.py
MCPServerStdioParams
Bases: TypedDict
Mirrors mcp.client.stdio.StdioServerParameters, but lets you pass params without another
import.
ソースコード位置: 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.
ソースコード位置: src/agents/mcp/server.py
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 | |
__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,
tool_meta_resolver: MCPToolMetaResolver | None = None,
custom_data_extractor: MCPToolCustomDataExtractor
| None = None,
)
Create a new MCP server based on the stdio transport.
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
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. |
必須 |
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 MCP ClientSession read timeout. Positive finite
values representable by |
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
|
tool_meta_resolver
|
MCPToolMetaResolver | None
|
Optional callable that produces MCP request metadata ( |
None
|
custom_data_extractor
|
MCPToolCustomDataExtractor | None
|
Optional callable that produces SDK-only custom data for emitted MCP tool output items. |
None
|
ソースコード位置: src/agents/mcp/server.py
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 | |
create_streams
connect
async
Connect to the server.
ソースコード位置: src/agents/mcp/server.py
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 | |
cleanup
async
Cleanup the server.
ソースコード位置: src/agents/mcp/server.py
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 | |
list_tools
async
list_tools(
run_context: RunContextWrapper[Any] | None = None,
agent: AgentBase | None = None,
) -> list[Tool]
List the tools available on the server.
ソースコード位置: src/agents/mcp/server.py
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 | |
call_tool
async
call_tool(
tool_name: str,
arguments: dict[str, Any] | None,
meta: dict[str, Any] | None = None,
) -> CallToolResult
Invoke a tool on the server.
ソースコード位置: src/agents/mcp/server.py
list_prompts
async
List the prompts available on the server.
ソースコード位置: src/agents/mcp/server.py
get_prompt
async
Get a specific prompt from the server.
ソースコード位置: src/agents/mcp/server.py
list_resources
async
List the resources available on the server.
ソースコード位置: src/agents/mcp/server.py
list_resource_templates
async
List the resource templates available on the server.
ソースコード位置: src/agents/mcp/server.py
read_resource
async
Read the contents of a specific resource by URI.
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
uri
|
str
|
The URI of the resource to read. See :class: |
必須 |
ソースコード位置: src/agents/mcp/server.py
MCPServerSseParams
Bases: TypedDict
Mirrors the params in mcp.client.sse.sse_client.
ソースコード位置: 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.
auth
instance-attribute
Optional httpx authentication handler (e.g. httpx.BasicAuth, a custom
httpx.Auth subclass for OAuth token refresh, etc.). When provided, it is
passed directly to the underlying httpx.AsyncClient used by the SSE transport.
httpx_client_factory
instance-attribute
httpx_client_factory: NotRequired[HttpClientFactory]
Custom HTTP client factory for configuring httpx.AsyncClient behavior (e.g. to set custom SSL certificates, proxies, or other transport options).
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.
ソースコード位置: src/agents/mcp/server.py
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 | |
__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,
tool_meta_resolver: MCPToolMetaResolver | None = None,
custom_data_extractor: MCPToolCustomDataExtractor
| None = None,
)
Create a new MCP server based on the HTTP with SSE transport.
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
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. |
必須 |
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 MCP ClientSession read timeout. Positive finite
values representable by |
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
|
tool_meta_resolver
|
MCPToolMetaResolver | None
|
Optional callable that produces MCP request metadata ( |
None
|
custom_data_extractor
|
MCPToolCustomDataExtractor | None
|
Optional callable that produces SDK-only custom data for emitted MCP tool output items. |
None
|
ソースコード位置: src/agents/mcp/server.py
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 | |
create_streams
Create the streams for the server.
ソースコード位置: src/agents/mcp/server.py
connect
async
Connect to the server.
ソースコード位置: src/agents/mcp/server.py
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 | |
cleanup
async
Cleanup the server.
ソースコード位置: src/agents/mcp/server.py
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 | |
list_tools
async
list_tools(
run_context: RunContextWrapper[Any] | None = None,
agent: AgentBase | None = None,
) -> list[Tool]
List the tools available on the server.
ソースコード位置: src/agents/mcp/server.py
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 | |
call_tool
async
call_tool(
tool_name: str,
arguments: dict[str, Any] | None,
meta: dict[str, Any] | None = None,
) -> CallToolResult
Invoke a tool on the server.
ソースコード位置: src/agents/mcp/server.py
list_prompts
async
List the prompts available on the server.
ソースコード位置: src/agents/mcp/server.py
get_prompt
async
Get a specific prompt from the server.
ソースコード位置: src/agents/mcp/server.py
list_resources
async
List the resources available on the server.
ソースコード位置: src/agents/mcp/server.py
list_resource_templates
async
List the resource templates available on the server.
ソースコード位置: src/agents/mcp/server.py
read_resource
async
Read the contents of a specific resource by URI.
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
uri
|
str
|
The URI of the resource to read. See :class: |
必須 |
ソースコード位置: src/agents/mcp/server.py
MCPServerStreamableHttpParams
Bases: TypedDict
Mirrors the params in mcp.client.streamable_http.streamablehttp_client.
ソースコード位置: 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.
auth
instance-attribute
Optional httpx authentication handler (e.g. httpx.BasicAuth, a custom
httpx.Auth subclass for OAuth token refresh, etc.). When provided, it is
passed directly to the underlying httpx.AsyncClient used by the Streamable HTTP
transport.
ignore_initialized_notification_failure
instance-attribute
Whether to ignore failures when sending the best-effort
notifications/initialized POST.
Defaults to False. When set to True, initialized-notification failures are
logged and ignored so subsequent requests on the same transport can continue.
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.
ソースコード位置: src/agents/mcp/server.py
1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 | |
session_id
property
The MCP session ID assigned by the server, or None if not yet connected or if the server did not issue a session ID.
The session ID is stable for the lifetime of this server instance's connection. You can persist it and pass it back via the Mcp-Session-Id request header (params["headers"]) on a new MCPServerStreamableHttp instance to resume the same server-side session across process restarts or stateless workers.
Example::
async with MCPServerStreamableHttp(params={"url": url}) as server:
session_id = server.session_id
# In a new worker / process:
async with MCPServerStreamableHttp(
params={"url": url, "headers": {"Mcp-Session-Id": session_id}}
) as server:
# Resumes the same server-side session.
...
__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,
tool_meta_resolver: MCPToolMetaResolver | None = None,
custom_data_extractor: MCPToolCustomDataExtractor
| None = None,
)
Create a new MCP server based on the Streamable HTTP transport.
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
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. |
必須 |
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 MCP ClientSession read timeout. Positive finite
values representable by |
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
|
tool_meta_resolver
|
MCPToolMetaResolver | None
|
Optional callable that produces MCP request metadata ( |
None
|
custom_data_extractor
|
MCPToolCustomDataExtractor | None
|
Optional callable that produces SDK-only custom data for emitted MCP tool output items. |
None
|
ソースコード位置: src/agents/mcp/server.py
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 | |
create_streams
Create the streams for the server.
ソースコード位置: src/agents/mcp/server.py
connect
async
Connect to the server.
ソースコード位置: src/agents/mcp/server.py
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 | |
cleanup
async
Cleanup the server.
ソースコード位置: src/agents/mcp/server.py
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 | |
list_tools
async
list_tools(
run_context: RunContextWrapper[Any] | None = None,
agent: AgentBase | None = None,
) -> list[Tool]
List the tools available on the server.
ソースコード位置: src/agents/mcp/server.py
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 | |
list_prompts
async
List the prompts available on the server.
ソースコード位置: src/agents/mcp/server.py
get_prompt
async
Get a specific prompt from the server.
ソースコード位置: src/agents/mcp/server.py
list_resources
async
List the resources available on the server.
ソースコード位置: src/agents/mcp/server.py
list_resource_templates
async
List the resource templates available on the server.
ソースコード位置: src/agents/mcp/server.py
read_resource
async
Read the contents of a specific resource by URI.
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
uri
|
str
|
The URI of the resource to read. See :class: |
必須 |