From dcf37ebc451dcd2ea12ef0c409b21faa439cb589 Mon Sep 17 00:00:00 2001 From: Mark Stuart Date: Tue, 24 Nov 2026 16:44:32 -0800 Subject: [PATCH] Defer MCP compatibility imports until tools need them --- src/agents/mcp/util.py | 20 ++++++---- tests/tracing/test_import_side_effects.py | 45 +++++++++++++++++++++++ 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/src/agents/mcp/util.py b/src/agents/mcp/util.py index 9ae370d60f..d725425932 100644 --- a/src/agents/mcp/util.py +++ b/src/agents/mcp/util.py @@ -35,14 +35,6 @@ from ..tracing import FunctionSpanData, get_current_span, mcp_tools_span from ..util._custom_data import maybe_extract_custom_data from ..util._types import MaybeAwaitable -from ._compat import ( - MCPError, - image_mime_type, - mcp_error_message, - result_is_error, - result_structured_content, - tool_input_schema, -) from ._logging import get_mcp_server_log_message, get_mcp_server_log_name if TYPE_CHECKING: @@ -535,6 +527,8 @@ def to_function_tool( policies. If the server uses a callable approval policy, approvals default to required to avoid bypassing dynamic checks. """ + from ._compat import tool_input_schema + tool_public_name = tool_name_override or tool.name static_meta = cls._extract_static_meta(tool) defaults = _DiscoveredToolMeta(static_meta) if static_meta is not None else None @@ -654,6 +648,8 @@ async def _extract_custom_data( if extractor is None: return None + from ._compat import result_is_error, result_structured_content + extractor_context = MCPToolCustomDataContext( run_context=context, server_name=server.name, @@ -707,6 +703,14 @@ async def invoke_mcp_tool( tool_display_name: str | None = None, ) -> ToolOutput: """Invoke an MCP tool and return the result as ToolOutput.""" + from ._compat import ( + MCPError, + image_mime_type, + mcp_error_message, + result_is_error, + result_structured_content, + ) + tool_name_for_display = tool_display_name or tool.name json_decode_error: Exception | None = None try: diff --git a/tests/tracing/test_import_side_effects.py b/tests/tracing/test_import_side_effects.py index 7f3a3bc54a..d20ee20708 100644 --- a/tests/tracing/test_import_side_effects.py +++ b/tests/tracing/test_import_side_effects.py @@ -354,3 +354,48 @@ def fake_register(fn): assert payload["shutdown_registration_count"] == 1 assert payload["exporter_initialized"] is False assert payload["processor_initialized"] is False + + +def test_plain_tools_and_agents_do_not_import_mcp() -> None: + payload = _run_python( + """ +import asyncio +import importlib.abc +import json +import sys + +class BlockUnusedDependencies(importlib.abc.MetaPathFinder): + def find_spec(self, fullname, path, target=None): + if fullname.split('.', 1)[0] == 'mcp': + raise ModuleNotFoundError(fullname, name=fullname) + return None + +sys.meta_path.insert(0, BlockUnusedDependencies()) +from agents import Agent, RunContextWrapper, function_tool + +@function_tool +async def plain(x: int) -> int: + return x + +class EmptyServer: + name = 'empty' + listings = 0 + + async def list_tools(self, *args, **kwargs): + self.listings += 1 + return [] + +async def main(): + context = RunContextWrapper(context=None) + assert await Agent(name='plain', tools=[plain]).get_mcp_tools(context) == [] + server = EmptyServer() + result = await Agent(name='local', mcp_servers=[server]).get_mcp_tools(context) + assert server.listings == 1 + print(json.dumps({ + 'tools': result, 'mcp': 'mcp' in sys.modules, + })) + +asyncio.run(main()) +""" + ) + assert payload == {"tools": [], "mcp": False}