Skip to content

Fix ImportError for trace_tool_span in MCP workbench for backward compatibility #6798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 11, 2025

Problem

Users installing autogen-ext v0.6.4 encounter an ImportError when trying to import MCP tools:

from autogen_ext.tools.mcp import SseMcpToolAdapter, SseServerParams

This fails with:

ImportError: cannot import name 'trace_tool_span' from 'autogen_core'

The issue occurs because trace_tool_span was not available in the released autogen-core v0.6.4, but the MCP workbench code attempts to import it unconditionally.

Solution

Made the trace_tool_span import conditional to maintain backward compatibility with older autogen-core versions while preserving full functionality when the function is available.

Key changes in _workbench.py:

  1. Conditional import with graceful fallback:
# Import trace_tool_span conditionally for backward compatibility
try:
    from autogen_core import trace_tool_span
except ImportError:
    # If trace_tool_span is not available (older autogen-core versions),
    # use a no-op context manager
    trace_tool_span = None
  1. Context creation that works with both scenarios:
# Use trace_tool_span if available, otherwise use nullcontext for no-op
tracing_context = (
    trace_tool_span(
        tool_name=name,
        tool_call_id=call_id,
    )
    if trace_tool_span is not None
    else nullcontext()
)

Benefits

  • Backward compatibility: Works with autogen-core v0.6.4 and earlier
  • Forward compatibility: Preserves full tracing when trace_tool_span is available
  • Minimal changes: Only 21 lines modified in a single file
  • No functionality loss: Telemetry/tracing works normally when available
  • Graceful degradation: No-op context when tracing unavailable

Testing

Validated the fix handles both scenarios correctly:

  • With trace_tool_span available: Full tracing functionality preserved
  • Without trace_tool_span: Graceful fallback to no-op context manager
  • Original user import scenario now works without errors

Fixes #6791.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: ekzhu <320302+ekzhu@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] ImportError: cannot import name 'trace_tool_span' from 'autogen_core' (/opt/conda/lib/python3.12/site-packages/autogen_core/__init__.py) Fix ImportError for trace_tool_span in MCP workbench for backward compatibility Jul 11, 2025
@Copilot Copilot AI requested a review from ekzhu July 11, 2025 11:46
Copilot finished work on behalf of ekzhu July 11, 2025 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ImportError: cannot import name 'trace_tool_span' from 'autogen_core' (/opt/conda/lib/python3.12/site-packages/autogen_core/__init__.py)
2 participants