Skip to content
2 changes: 2 additions & 0 deletions sentry_sdk/integrations/openai_agents/spans/ai_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
_set_input_data,
_set_output_data,
_set_usage_data,
_create_mcp_execute_tool_spans,
)

from typing import TYPE_CHECKING
Expand Down Expand Up @@ -37,3 +38,4 @@ def update_ai_client_span(span, agent, get_response_kwargs, result):
_set_usage_data(span, result.usage)
_set_input_data(span, get_response_kwargs)
_set_output_data(span, result)
_create_mcp_execute_tool_spans(span, result)
22 changes: 21 additions & 1 deletion sentry_sdk/integrations/openai_agents/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sentry_sdk
from sentry_sdk.ai.utils import set_data_normalized
from sentry_sdk.consts import SPANDATA
from sentry_sdk.consts import SPANDATA, SPANSTATUS, OP
from sentry_sdk.integrations import DidNotEnable
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.tracing_utils import set_span_errored
Expand Down Expand Up @@ -156,3 +156,23 @@ def _set_output_data(span, result):
set_data_normalized(
span, SPANDATA.GEN_AI_RESPONSE_TEXT, output_messages["response"]
)


def _create_mcp_execute_tool_spans(span, result):
# type: (sentry_sdk.tracing.Span, agents.Result) -> None
for output in result.output:
if output.__class__.__name__ == "McpCall":
with sentry_sdk.start_span(
op=OP.GEN_AI_EXECUTE_TOOL,
description=f"execute_tool {output.name}",
start_timestamp=span.start_timestamp,
) as execute_tool_span:
execute_tool_span.set_tag(SPANDATA.GEN_AI_TOOL_TYPE, "mcp")
execute_tool_span.set_tag(SPANDATA.GEN_AI_TOOL_NAME, output.name)
if should_send_default_pii():
execute_tool_span.set_data(
SPANDATA.GEN_AI_TOOL_INPUT, output.arguments
)
span.set_data(SPANDATA.GEN_AI_TOOL_OUTPUT, output.output)
if output.error:
execute_tool_span.set_status(SPANSTATUS.ERROR)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: MCP Tool Spans Lack Agent Context Data

The _create_mcp_execute_tool_spans function creates execute_tool spans for MCP calls without setting agent context data. These spans are missing information like gen_ai.agent.name, gen_ai.system, and gen_ai.request.model, making them inconsistent with regular execute_tool spans. This occurs because the function doesn't receive the agent parameter.

Additional Locations (1)

Fix in Cursor Fix in Web