Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/agents/mcp/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
45 changes: 45 additions & 0 deletions tests/tracing/test_import_side_effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Loading