MCP Servers
MCPServer
Bases: ABC
Base class for Model Context Protocol servers.
Source code in src/agents/mcp/server.py
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 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 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 | |
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,
)
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, 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
|
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.
Source code in src/agents/mcp/server.py
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cursor
|
str | None
|
An opaque pagination cursor returned in a previous
:class: |
None
|
Returns a :class:~mcp.types.ListResourcesResult. When the result contains
a next_cursor field under MCP v2 or nextCursor under MCP v1, 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.
Source code in src/agents/mcp/server.py
list_resource_templates
async
List the resource templates available on the server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cursor
|
str | None
|
An opaque pagination cursor returned in a previous
:class: |
None
|
Returns a :class:~mcp.types.ListResourceTemplatesResult. When the result
contains a next_cursor field under MCP v2 or nextCursor under MCP
v1, 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.
Source code in src/agents/mcp/server.py
read_resource
async
Read the contents of a specific resource by URI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
The URI of the resource to read. See :class: |
required |
Returns a :class:~mcp.types.ReadResourceResult. Subclasses that do not
support resources may leave this unimplemented; it will raise
:exc:NotImplementedError at call time.
Source code in src/agents/mcp/server.py
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
1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 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 | |
__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.
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 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
|
Source code in src/agents/mcp/server.py
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 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 | |
create_streams
connect
async
Connect to the server.
Source code in src/agents/mcp/server.py
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 | |
cleanup
async
Cleanup the server.
Source code in src/agents/mcp/server.py
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 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 | |
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
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 | |
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.
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
list_resources
async
List the resources available on the server.
Source code in src/agents/mcp/server.py
list_resource_templates
async
List the resource templates available on the server.
Source code in src/agents/mcp/server.py
read_resource
async
Read the contents of a specific resource by URI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
The URI of the resource to read. See :class: |
required |
Source code in src/agents/mcp/server.py
MCPServerSseParams
Bases: TypedDict
Mirrors the params in mcp.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.
sse_read_timeout
instance-attribute
The timeout for the SSE connection, in seconds. Defaults to 5 minutes.
auth
instance-attribute
Optional authentication handler for the installed MCP SDK's HTTP stack.
Use httpx.Auth with MCP v1 or httpx2.Auth with MCP v2.
httpx_client_factory
instance-attribute
httpx_client_factory: NotRequired[HttpClientFactory]
Custom HTTP client factory for the installed MCP SDK's HTTP stack.
Return httpx.AsyncClient with MCP v1 or httpx2.AsyncClient with MCP v2.
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
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 | |
__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.
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 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
|
Source code in src/agents/mcp/server.py
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 | |
create_streams
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
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 | |
cleanup
async
Cleanup the server.
Source code in src/agents/mcp/server.py
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 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 | |
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
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 | |
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.
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
list_resources
async
List the resources available on the server.
Source code in src/agents/mcp/server.py
list_resource_templates
async
List the resource templates available on the server.
Source code in src/agents/mcp/server.py
read_resource
async
Read the contents of a specific resource by URI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
The URI of the resource to read. See :class: |
required |
Source code in src/agents/mcp/server.py
MCPServerStreamableHttpParams
Bases: TypedDict
Mirrors the params in mcp.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 the installed MCP SDK's HTTP stack.
Return httpx.AsyncClient with MCP v1 or httpx2.AsyncClient with MCP v2.
auth
instance-attribute
Optional authentication handler for the installed MCP SDK's HTTP stack.
Use httpx.Auth with MCP v1 or httpx2.Auth with MCP v2.
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. This
option requires MCP Python SDK v1; MCP v2 rejects it before connecting because its
public transport API does not expose these failures.
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
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 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 | |
session_id
property
The legacy MCP session ID assigned by the server, if one is available.
MCP 2026-07-28 does not use protocol sessions, so this property returns None for a modern connection. It also returns None before connection or when a legacy server does not issue a session ID. A legacy session ID is stable for this instance's connection and can be passed through the Mcp-Session-Id request header when reconnecting to a server that supports legacy session resumption.
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.
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 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
|
Source code in src/agents/mcp/server.py
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 | |
create_streams
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
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 | |
cleanup
async
Cleanup the server.
Source code in src/agents/mcp/server.py
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 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 | |
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
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 | |
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
list_resources
async
List the resources available on the server.
Source code in src/agents/mcp/server.py
list_resource_templates
async
List the resource templates available on the server.
Source code in src/agents/mcp/server.py
read_resource
async
Read the contents of a specific resource by URI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
The URI of the resource to read. See :class: |
required |