From b99d6e146ec451dec460a252dffee5e04ae1dbd9 Mon Sep 17 00:00:00 2001 From: Rohan Mehta Date: Wed, 16 Jul 2025 11:44:54 -0400 Subject: [PATCH] MCP: add support for tool_result.structuredContent --- pyproject.toml | 2 +- src/agents/mcp/server.py | 39 +++++++++++++++++++++++++++++++++++ src/agents/mcp/util.py | 10 ++++++++- tests/mcp/helpers.py | 1 + tests/mcp/test_mcp_tracing.py | 6 +++--- uv.lock | 35 +++++++++++++++++++++++++++---- 6 files changed, 84 insertions(+), 9 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index df6915c35..ba0fd60d2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,7 @@ dependencies = [ "typing-extensions>=4.12.2, <5", "requests>=2.0, <3", "types-requests>=2.0, <3", - "mcp>=1.9.4, <2; python_version >= '3.10'", + "mcp>=1.11.0, <2; python_version >= '3.10'", ] classifiers = [ "Typing :: Typed", diff --git a/src/agents/mcp/server.py b/src/agents/mcp/server.py index 91a9274fc..66332549c 100644 --- a/src/agents/mcp/server.py +++ b/src/agents/mcp/server.py @@ -28,6 +28,17 @@ class MCPServer(abc.ABC): """Base class for Model Context Protocol servers.""" + def __init__(self, use_structured_content: bool = False): + """ + Args: + use_structured_content: Whether to use `tool_result.structured_content` when calling an + MCP tool.Defaults to False for backwards compatibility - most MCP servers still + include the structured content in the `tool_result.content`, and using it by + default will cause duplicate content. You can set this to True if you know the + server will not duplicate the structured content in the `tool_result.content`. + """ + self.use_structured_content = use_structured_content + @abc.abstractmethod async def connect(self): """Connect to the server. For example, this might mean spawning a subprocess or @@ -86,6 +97,7 @@ def __init__( cache_tools_list: bool, client_session_timeout_seconds: float | None, tool_filter: ToolFilter = None, + use_structured_content: bool = False, ): """ Args: @@ -98,7 +110,13 @@ def __init__( client_session_timeout_seconds: the read timeout passed to the MCP ClientSession. tool_filter: The tool filter to use for filtering tools. + use_structured_content: Whether to use `tool_result.structured_content` when calling an + MCP tool. Defaults to False for backwards compatibility - most MCP servers still + include the structured content in the `tool_result.content`, and using it by + default will cause duplicate content. You can set this to True if you know the + server will not duplicate the structured content in the `tool_result.content`. """ + super().__init__(use_structured_content=use_structured_content) self.session: ClientSession | None = None self.exit_stack: AsyncExitStack = AsyncExitStack() self._cleanup_lock: asyncio.Lock = asyncio.Lock() @@ -346,6 +364,7 @@ def __init__( name: str | None = None, client_session_timeout_seconds: float | None = 5, tool_filter: ToolFilter = None, + use_structured_content: bool = False, ): """Create a new MCP server based on the stdio transport. @@ -364,11 +383,17 @@ def __init__( command. client_session_timeout_seconds: the read timeout passed to the MCP ClientSession. tool_filter: The tool filter to use for filtering tools. + use_structured_content: Whether to use `tool_result.structured_content` when calling an + MCP tool. Defaults to False for backwards compatibility - most MCP servers still + include the structured content in the `tool_result.content`, and using it by + default will cause duplicate content. You can set this to True if you know the + server will not duplicate the structured content in the `tool_result.content`. """ super().__init__( cache_tools_list, client_session_timeout_seconds, tool_filter, + use_structured_content, ) self.params = StdioServerParameters( @@ -429,6 +454,7 @@ def __init__( name: str | None = None, client_session_timeout_seconds: float | None = 5, tool_filter: ToolFilter = None, + use_structured_content: bool = False, ): """Create a new MCP server based on the HTTP with SSE transport. @@ -449,11 +475,17 @@ def __init__( client_session_timeout_seconds: the read timeout passed to the MCP ClientSession. tool_filter: The tool filter to use for filtering tools. + use_structured_content: Whether to use `tool_result.structured_content` when calling an + MCP tool. Defaults to False for backwards compatibility - most MCP servers still + include the structured content in the `tool_result.content`, and using it by + default will cause duplicate content. You can set this to True if you know the + server will not duplicate the structured content in the `tool_result.content`. """ super().__init__( cache_tools_list, client_session_timeout_seconds, tool_filter, + use_structured_content, ) self.params = params @@ -514,6 +546,7 @@ def __init__( name: str | None = None, client_session_timeout_seconds: float | None = 5, tool_filter: ToolFilter = None, + use_structured_content: bool = False, ): """Create a new MCP server based on the Streamable HTTP transport. @@ -535,11 +568,17 @@ def __init__( client_session_timeout_seconds: the read timeout passed to the MCP ClientSession. tool_filter: The tool filter to use for filtering tools. + use_structured_content: Whether to use `tool_result.structured_content` when calling an + MCP tool. Defaults to False for backwards compatibility - most MCP servers still + include the structured content in the `tool_result.content`, and using it by + default will cause duplicate content. You can set this to True if you know the + server will not duplicate the structured content in the `tool_result.content`. """ super().__init__( cache_tools_list, client_session_timeout_seconds, tool_filter, + use_structured_content, ) self.params = params diff --git a/src/agents/mcp/util.py b/src/agents/mcp/util.py index 18cf4440a..bf09b8767 100644 --- a/src/agents/mcp/util.py +++ b/src/agents/mcp/util.py @@ -198,8 +198,16 @@ async def invoke_mcp_tool( # string. We'll try to convert. if len(result.content) == 1: tool_output = result.content[0].model_dump_json() + # Append structured content if it exists and we're using it. + if server.use_structured_content and result.structuredContent: + tool_output = f"{tool_output}\n{json.dumps(result.structuredContent)}" elif len(result.content) > 1: - tool_output = json.dumps([item.model_dump(mode="json") for item in result.content]) + tool_results = [item.model_dump(mode="json") for item in result.content] + if server.use_structured_content and result.structuredContent: + tool_results.append(result.structuredContent) + tool_output = json.dumps(tool_results) + elif server.use_structured_content and result.structuredContent: + tool_output = json.dumps(result.structuredContent) else: logger.error(f"Errored MCP tool result: {result}") tool_output = "Error running tool." diff --git a/tests/mcp/helpers.py b/tests/mcp/helpers.py index 31d43c228..f5723e7ce 100644 --- a/tests/mcp/helpers.py +++ b/tests/mcp/helpers.py @@ -61,6 +61,7 @@ def __init__( tool_filter: ToolFilter = None, server_name: str = "fake_mcp_server", ): + super().__init__(use_structured_content=False) self.tools: list[MCPTool] = tools or [] self.tool_calls: list[str] = [] self.tool_results: list[str] = [] diff --git a/tests/mcp/test_mcp_tracing.py b/tests/mcp/test_mcp_tracing.py index 54575dcb5..33dfa5ea1 100644 --- a/tests/mcp/test_mcp_tracing.py +++ b/tests/mcp/test_mcp_tracing.py @@ -62,7 +62,7 @@ async def test_mcp_tracing(): "data": { "name": "test_tool_1", "input": "", - "output": '{"type":"text","text":"result_test_tool_1_{}","annotations":null}', # noqa: E501 + "output": '{"type":"text","text":"result_test_tool_1_{}","annotations":null,"meta":null}', # noqa: E501 "mcp_data": {"server": "fake_mcp_server"}, }, }, @@ -133,7 +133,7 @@ async def test_mcp_tracing(): "data": { "name": "test_tool_2", "input": "", - "output": '{"type":"text","text":"result_test_tool_2_{}","annotations":null}', # noqa: E501 + "output": '{"type":"text","text":"result_test_tool_2_{}","annotations":null,"meta":null}', # noqa: E501 "mcp_data": {"server": "fake_mcp_server"}, }, }, @@ -197,7 +197,7 @@ async def test_mcp_tracing(): "data": { "name": "test_tool_3", "input": "", - "output": '{"type":"text","text":"result_test_tool_3_{}","annotations":null}', # noqa: E501 + "output": '{"type":"text","text":"result_test_tool_3_{}","annotations":null,"meta":null}', # noqa: E501 "mcp_data": {"server": "fake_mcp_server"}, }, }, diff --git a/uv.lock b/uv.lock index 918c3d0be..1427afd24 100644 --- a/uv.lock +++ b/uv.lock @@ -1047,22 +1047,24 @@ wheels = [ [[package]] name = "mcp" -version = "1.9.4" +version = "1.11.0" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "anyio", marker = "python_full_version >= '3.10'" }, { name = "httpx", marker = "python_full_version >= '3.10'" }, { name = "httpx-sse", marker = "python_full_version >= '3.10'" }, + { name = "jsonschema", marker = "python_full_version >= '3.10'" }, { name = "pydantic", marker = "python_full_version >= '3.10'" }, { name = "pydantic-settings", marker = "python_full_version >= '3.10'" }, { name = "python-multipart", marker = "python_full_version >= '3.10'" }, + { name = "pywin32", marker = "python_full_version >= '3.10' and sys_platform == 'win32'" }, { name = "sse-starlette", marker = "python_full_version >= '3.10'" }, { name = "starlette", marker = "python_full_version >= '3.10'" }, { name = "uvicorn", marker = "python_full_version >= '3.10' and sys_platform != 'emscripten'" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/06/f2/dc2450e566eeccf92d89a00c3e813234ad58e2ba1e31d11467a09ac4f3b9/mcp-1.9.4.tar.gz", hash = "sha256:cfb0bcd1a9535b42edaef89947b9e18a8feb49362e1cc059d6e7fc636f2cb09f", size = 333294, upload-time = "2025-06-12T08:20:30.158Z" } +sdist = { url = "https://files.pythonhosted.org/packages/3a/f5/9506eb5578d5bbe9819ee8ba3198d0ad0e2fbe3bab8b257e4131ceb7dfb6/mcp-1.11.0.tar.gz", hash = "sha256:49a213df56bb9472ff83b3132a4825f5c8f5b120a90246f08b0dac6bedac44c8", size = 406907, upload-time = "2025-07-10T16:41:09.388Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/97/fc/80e655c955137393c443842ffcc4feccab5b12fa7cb8de9ced90f90e6998/mcp-1.9.4-py3-none-any.whl", hash = "sha256:7fcf36b62936adb8e63f89346bccca1268eeca9bf6dfb562ee10b1dfbda9dac0", size = 130232, upload-time = "2025-06-12T08:20:28.551Z" }, + { url = "https://files.pythonhosted.org/packages/92/9c/c9ca79f9c512e4113a5d07043013110bb3369fc7770040c61378c7fbcf70/mcp-1.11.0-py3-none-any.whl", hash = "sha256:58deac37f7483e4b338524b98bc949b7c2b7c33d978f5fafab5bde041c5e2595", size = 155880, upload-time = "2025-07-10T16:41:07.935Z" }, ] [[package]] @@ -1537,7 +1539,7 @@ requires-dist = [ { name = "graphviz", marker = "extra == 'viz'", specifier = ">=0.17" }, { name = "griffe", specifier = ">=1.5.6,<2" }, { name = "litellm", marker = "extra == 'litellm'", specifier = ">=1.67.4.post1,<2" }, - { name = "mcp", marker = "python_full_version >= '3.10'", specifier = ">=1.9.4,<2" }, + { name = "mcp", marker = "python_full_version >= '3.10'", specifier = ">=1.11.0,<2" }, { name = "numpy", marker = "python_full_version >= '3.10' and extra == 'voice'", specifier = ">=2.2.0,<3" }, { name = "openai", specifier = ">=1.96.1,<2" }, { name = "pydantic", specifier = ">=2.10,<3" }, @@ -2111,6 +2113,31 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/fc/b8/ff33610932e0ee81ae7f1269c890f697d56ff74b9f5b2ee5d9b7fa2c5355/python_xlib-0.33-py2.py3-none-any.whl", hash = "sha256:c3534038d42e0df2f1392a1b30a15a4ff5fdc2b86cfa94f072bf11b10a164398", size = 182185, upload-time = "2022-12-25T18:52:58.662Z" }, ] +[[package]] +name = "pywin32" +version = "311" +source = { registry = "https://pypi.org/simple" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/7b/40/44efbb0dfbd33aca6a6483191dae0716070ed99e2ecb0c53683f400a0b4f/pywin32-311-cp310-cp310-win32.whl", hash = "sha256:d03ff496d2a0cd4a5893504789d4a15399133fe82517455e78bad62efbb7f0a3", size = 8760432, upload-time = "2025-07-14T20:13:05.9Z" }, + { url = "https://files.pythonhosted.org/packages/5e/bf/360243b1e953bd254a82f12653974be395ba880e7ec23e3731d9f73921cc/pywin32-311-cp310-cp310-win_amd64.whl", hash = "sha256:797c2772017851984b97180b0bebe4b620bb86328e8a884bb626156295a63b3b", size = 9590103, upload-time = "2025-07-14T20:13:07.698Z" }, + { url = "https://files.pythonhosted.org/packages/57/38/d290720e6f138086fb3d5ffe0b6caa019a791dd57866940c82e4eeaf2012/pywin32-311-cp310-cp310-win_arm64.whl", hash = "sha256:0502d1facf1fed4839a9a51ccbcc63d952cf318f78ffc00a7e78528ac27d7a2b", size = 8778557, upload-time = "2025-07-14T20:13:11.11Z" }, + { url = "https://files.pythonhosted.org/packages/7c/af/449a6a91e5d6db51420875c54f6aff7c97a86a3b13a0b4f1a5c13b988de3/pywin32-311-cp311-cp311-win32.whl", hash = "sha256:184eb5e436dea364dcd3d2316d577d625c0351bf237c4e9a5fabbcfa5a58b151", size = 8697031, upload-time = "2025-07-14T20:13:13.266Z" }, + { url = "https://files.pythonhosted.org/packages/51/8f/9bb81dd5bb77d22243d33c8397f09377056d5c687aa6d4042bea7fbf8364/pywin32-311-cp311-cp311-win_amd64.whl", hash = "sha256:3ce80b34b22b17ccbd937a6e78e7225d80c52f5ab9940fe0506a1a16f3dab503", size = 9508308, upload-time = "2025-07-14T20:13:15.147Z" }, + { url = "https://files.pythonhosted.org/packages/44/7b/9c2ab54f74a138c491aba1b1cd0795ba61f144c711daea84a88b63dc0f6c/pywin32-311-cp311-cp311-win_arm64.whl", hash = "sha256:a733f1388e1a842abb67ffa8e7aad0e70ac519e09b0f6a784e65a136ec7cefd2", size = 8703930, upload-time = "2025-07-14T20:13:16.945Z" }, + { url = "https://files.pythonhosted.org/packages/e7/ab/01ea1943d4eba0f850c3c61e78e8dd59757ff815ff3ccd0a84de5f541f42/pywin32-311-cp312-cp312-win32.whl", hash = "sha256:750ec6e621af2b948540032557b10a2d43b0cee2ae9758c54154d711cc852d31", size = 8706543, upload-time = "2025-07-14T20:13:20.765Z" }, + { url = "https://files.pythonhosted.org/packages/d1/a8/a0e8d07d4d051ec7502cd58b291ec98dcc0c3fff027caad0470b72cfcc2f/pywin32-311-cp312-cp312-win_amd64.whl", hash = "sha256:b8c095edad5c211ff31c05223658e71bf7116daa0ecf3ad85f3201ea3190d067", size = 9495040, upload-time = "2025-07-14T20:13:22.543Z" }, + { url = "https://files.pythonhosted.org/packages/ba/3a/2ae996277b4b50f17d61f0603efd8253cb2d79cc7ae159468007b586396d/pywin32-311-cp312-cp312-win_arm64.whl", hash = "sha256:e286f46a9a39c4a18b319c28f59b61de793654af2f395c102b4f819e584b5852", size = 8710102, upload-time = "2025-07-14T20:13:24.682Z" }, + { url = "https://files.pythonhosted.org/packages/a5/be/3fd5de0979fcb3994bfee0d65ed8ca9506a8a1260651b86174f6a86f52b3/pywin32-311-cp313-cp313-win32.whl", hash = "sha256:f95ba5a847cba10dd8c4d8fefa9f2a6cf283b8b88ed6178fa8a6c1ab16054d0d", size = 8705700, upload-time = "2025-07-14T20:13:26.471Z" }, + { url = "https://files.pythonhosted.org/packages/e3/28/e0a1909523c6890208295a29e05c2adb2126364e289826c0a8bc7297bd5c/pywin32-311-cp313-cp313-win_amd64.whl", hash = "sha256:718a38f7e5b058e76aee1c56ddd06908116d35147e133427e59a3983f703a20d", size = 9494700, upload-time = "2025-07-14T20:13:28.243Z" }, + { url = "https://files.pythonhosted.org/packages/04/bf/90339ac0f55726dce7d794e6d79a18a91265bdf3aa70b6b9ca52f35e022a/pywin32-311-cp313-cp313-win_arm64.whl", hash = "sha256:7b4075d959648406202d92a2310cb990fea19b535c7f4a78d3f5e10b926eeb8a", size = 8709318, upload-time = "2025-07-14T20:13:30.348Z" }, + { url = "https://files.pythonhosted.org/packages/c9/31/097f2e132c4f16d99a22bfb777e0fd88bd8e1c634304e102f313af69ace5/pywin32-311-cp314-cp314-win32.whl", hash = "sha256:b7a2c10b93f8986666d0c803ee19b5990885872a7de910fc460f9b0c2fbf92ee", size = 8840714, upload-time = "2025-07-14T20:13:32.449Z" }, + { url = "https://files.pythonhosted.org/packages/90/4b/07c77d8ba0e01349358082713400435347df8426208171ce297da32c313d/pywin32-311-cp314-cp314-win_amd64.whl", hash = "sha256:3aca44c046bd2ed8c90de9cb8427f581c479e594e99b5c0bb19b29c10fd6cb87", size = 9656800, upload-time = "2025-07-14T20:13:34.312Z" }, + { url = "https://files.pythonhosted.org/packages/c0/d2/21af5c535501a7233e734b8af901574572da66fcc254cb35d0609c9080dd/pywin32-311-cp314-cp314-win_arm64.whl", hash = "sha256:a508e2d9025764a8270f93111a970e1d0fbfc33f4153b388bb649b7eec4f9b42", size = 8932540, upload-time = "2025-07-14T20:13:36.379Z" }, + { url = "https://files.pythonhosted.org/packages/59/42/b86689aac0cdaee7ae1c58d464b0ff04ca909c19bb6502d4973cdd9f9544/pywin32-311-cp39-cp39-win32.whl", hash = "sha256:aba8f82d551a942cb20d4a83413ccbac30790b50efb89a75e4f586ac0bb8056b", size = 8760837, upload-time = "2025-07-14T20:12:59.59Z" }, + { url = "https://files.pythonhosted.org/packages/9f/8a/1403d0353f8c5a2f0829d2b1c4becbf9da2f0a4d040886404fc4a5431e4d/pywin32-311-cp39-cp39-win_amd64.whl", hash = "sha256:e0c4cfb0621281fe40387df582097fd796e80430597cb9944f0ae70447bacd91", size = 9590187, upload-time = "2025-07-14T20:13:01.419Z" }, + { url = "https://files.pythonhosted.org/packages/60/22/e0e8d802f124772cec9c75430b01a212f86f9de7546bda715e54140d5aeb/pywin32-311-cp39-cp39-win_arm64.whl", hash = "sha256:62ea666235135fee79bb154e695f3ff67370afefd71bd7fea7512fc70ef31e3d", size = 8778162, upload-time = "2025-07-14T20:13:03.544Z" }, +] + [[package]] name = "pyyaml" version = "6.0.2"