Skip to content

Commit 6cb7b43

Browse files
committed
fix(openai-agents): also emit spans for MCP tool calls done by the LLM
1 parent 9b58d31 commit 6cb7b43

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

sentry_sdk/integrations/openai_agents/spans/ai_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
_set_input_data,
88
_set_output_data,
99
_set_usage_data,
10+
_create_mcp_execute_tool_spans,
1011
)
1112

1213
from typing import TYPE_CHECKING
@@ -37,3 +38,4 @@ def update_ai_client_span(span, agent, get_response_kwargs, result):
3738
_set_usage_data(span, result.usage)
3839
_set_input_data(span, get_response_kwargs)
3940
_set_output_data(span, result)
41+
_create_mcp_execute_tool_spans(span, result)

sentry_sdk/integrations/openai_agents/utils.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sentry_sdk
22
from sentry_sdk.ai.utils import set_data_normalized
3-
from sentry_sdk.consts import SPANDATA
3+
from sentry_sdk.consts import SPANDATA, SPANSTATUS, OP
44
from sentry_sdk.integrations import DidNotEnable
55
from sentry_sdk.scope import should_send_default_pii
66
from sentry_sdk.tracing_utils import set_span_errored
@@ -156,3 +156,21 @@ def _set_output_data(span, result):
156156
set_data_normalized(
157157
span, SPANDATA.GEN_AI_RESPONSE_TEXT, output_messages["response"]
158158
)
159+
160+
161+
def _create_mcp_execute_tool_spans(span, result):
162+
# type: (sentry_sdk.tracing.Span, agents.Result) -> None
163+
for output in result.output:
164+
if output.__class__.__name__ == "McpCall":
165+
with sentry_sdk.start_span(
166+
op=OP.GEN_AI_EXECUTE_TOOL,
167+
description=f"execute_tool {output.name}",
168+
start_timestamp=span.start_timestamp,
169+
) as span:
170+
span.set_tag(SPANDATA.GEN_AI_TOOL_TYPE, "mcp")
171+
span.set_tag(SPANDATA.GEN_AI_TOOL_NAME, output.name)
172+
if should_send_default_pii():
173+
span.set_data(SPANDATA.GEN_AI_TOOL_INPUT, output.arguments)
174+
span.set_data(SPANDATA.GEN_AI_TOOL_OUTPUT, output.output)
175+
if output.error:
176+
span.set_status(SPANSTATUS.ERROR)

0 commit comments

Comments
 (0)