Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(otel): anchor MCP tool-call spans to the gateway's own trace, link the client's context (#38317)

yucheng-berri88cb83484ba3
diff --git a/litellm/integrations/otel/emitter.py b/litellm/integrations/otel/emitter.py
index 244e58eddf3..101dbc6538d 100644
--- a/litellm/integrations/otel/emitter.py
+++ b/litellm/integrations/otel/emitter.py
@@ -146,7 +146,7 @@ class SpanEmitter:
For callers that own and manage their own span lifecycle. ``tracer``
overrides the bound tracer for this span only, used for per-request
multi-tenant credential routing. ``links`` records related-but-not-parent
- spans (e.g. the transport span of an MCP message, per MCP semconv).
+ spans (e.g. the trace context an MCP client propagated in ``params._meta``).
"""
return (tracer or self._tracer).start_span(
name,
@@ -196,8 +196,8 @@ class SpanEmitter:
Return the span, or ``None`` if it was deduplicated away. ``tracer``
overrides the bound tracer for this span, used for per-request routing.
- ``links`` records related-but-not-parent spans (the transport span of an
- MCP message).
+ ``links`` records related-but-not-parent spans (e.g. the trace context an
+ MCP client propagated in ``params._meta``).
"""
# LLM-call and MCP tool-call spans carry a dedup key (their request's
# call id), so a sync+async double-firing coalesces. ``isinstance`` narrows
diff --git a/litellm/integrations/otel/logger.py b/litellm/integrations/otel/logger.py
index 4359b222d06..d2a32ef73b6 100644
--- a/litellm/integrations/otel/logger.py
+++ b/litellm/integrations/otel/logger.py
@@ -390,10 +390,10 @@ class OpenTelemetryV2(CustomLogger):
MCP tool calls reach the success/failure callbacks like any other request
(with ``call_type`` ``call_mcp_tool``), but they are not LLM calls and have
- no ``pre_call`` carrier — so they get their own CLIENT span here. Per the MCP
- semconv it parents to the trace context the client propagated in
- ``params._meta`` (or starts a new root) and links the transport span, rather
- than nesting under the HTTP/session span. Returns whether it handled the
+ no ``pre_call`` carrier — so they get their own CLIENT span here. It nests
+ under the transport span of the request carrying this message, and trace
+ context the client propagated in ``params._meta`` is recorded as a span
+ link (see ``resolve_mcp_span_context``). Returns whether it handled the
event, so the caller skips the LLM-call path. The whole span is emitted at
once (there is no boundary to open it at), deduped on the call id.
"""
@@ -436,9 +436,9 @@ class OpenTelemetryV2(CustomLogger):
Like a tool call, listing reaches the success/failure callbacks (here with
``call_type`` ``list_mcp_tools``) with no ``pre_call`` carrier, so it gets its
- own CLIENT span. Per the MCP semconv it parents to the ``params._meta`` trace
- context (or starts a new root) and links the transport span, rather than
- nesting under the HTTP/session span. Returns whether it handled the event so
+ own CLIENT span, nested under the transport span of the request carrying
+ this message with any ``params._meta`` trace context recorded as a span
+ link (see ``resolve_mcp_span_context``). Returns whether it handled the event so
the caller skips the LLM-call path.
"""
raw_payload: Final = kwargs.get("standard_logging_object")
diff --git a/litellm/integrations/otel/model/spans.py b/litellm/integrations/otel/model/spans.py
index 08318f78b7c..35fc50a2a83 100644
--- a/litellm/integrations/otel/model/spans.py
+++ b/litellm/integrations/otel/model/spans.py
@@ -10,6 +10,8 @@ Canonical hierarchy::
│ └── DB_CALL (CLIENT) # its key/user/team lookups nest here
├── GUARDRAIL (INTERNAL) # request-lifecycle hook, sibling of LLM_CALL
├── LLM_CALL (CLIENT)
+ ├── MCP_TOOL_CALL (CLIENT) # nests under the POST carrying the message
+ ├── MCP_LIST_TOOLS (CLIENT) # (client-propagated context is a span link)
└── DB_CALL (CLIENT) # e.g. the spend-log write
Guardrails parent to PROXY_REQUEST, not LLM_CALL: pre/during/post-call guardrail
@@ -18,14 +20,14 @@ before the LLM call even starts), so a guardrail is a sibling of the LLM call,
not a child of it. The emitter parents every span to the ambient OTel context
(the active server span), which matches this.
-MCP spans (``MCP_TOOL_CALL``, ``MCP_LIST_TOOLS``) have two shapes, chosen at emit
-time by :func:`resolve_mcp_span_context`. When the client propagates trace context
-in ``params._meta`` MCP and the HTTP transport are independent contexts per the
-OTel GenAI MCP semconv, so the span parents to that propagated context and records
-the ``PROXY_REQUEST`` transport span as a span *link*, never a parent — the shape
-this registry's ``parent=None, links=PROXY_REQUEST`` entry encodes. When nothing is
-propagated (the common case) the span nests under the transport span of the request
-carrying that message, so the tool call stays in one trace.
+MCP spans (``MCP_TOOL_CALL``, ``MCP_LIST_TOOLS``) are parented at emit time by
+:func:`resolve_mcp_span_context`: they nest under the ``PROXY_REQUEST`` transport
+span of the request carrying that message, so the tool call stays in one trace.
+Trace context the client propagated in ``params._meta`` (SEP-414) is recorded as
+a span *link*, never the parent — a remote parent would root the span in a trace
+whose root never reaches the gateway's tracing backend. Links always target that
+remote client context, never a registry role, so ``SpanSpec`` declares no link
+field; the concrete transport parent is resolved per message at emit time.
Not every service call becomes a span — :func:`span_role_for_service` decides:
@@ -85,25 +87,19 @@ class SpanSpec:
role: SpanRole
kind: LiteLLMSpanKind
parent: SpanRole | None
- links: SpanRole | None = None
SPAN_REGISTRY: Final[dict[SpanRole, SpanSpec]] = {
SpanRole.PROXY_REQUEST: SpanSpec(SpanRole.PROXY_REQUEST, LiteLLMSpanKind.SERVER, parent=None),
SpanRole.LLM_CALL: SpanSpec(SpanRole.LLM_CALL, LiteLLMSpanKind.CLIENT, parent=SpanRole.PROXY_REQUEST),
# The proxy is an MCP client to the upstream server, so MCP spans are CLIENT
- # spans. With trace context propagated in ``params._meta``, MCP and the HTTP
- # transport are independent contexts (OTel GenAI MCP semconv): the span parents
- # to the propagated context and records the PROXY_REQUEST transport span as a
- # span *link*, never a parent — the shape ``parent=None, links=PROXY_REQUEST``
- # encodes. With nothing propagated, ``resolve_mcp_span_context`` nests the span
- # under that message's transport span instead, keeping the call in one trace.
- SpanRole.MCP_TOOL_CALL: SpanSpec(
- SpanRole.MCP_TOOL_CALL, LiteLLMSpanKind.CLIENT, parent=None, links=SpanRole.PROXY_REQUEST
- ),
- SpanRole.MCP_LIST_TOOLS: SpanSpec(
- SpanRole.MCP_LIST_TOOLS, LiteLLMSpanKind.CLIENT, parent=None, links=SpanRole.PROXY_REQUEST
- ),
+ # spans. ``resolve_mcp_span_context`` nests them under the PROXY_REQUEST
+ # transport span of the request carrying that message (resolved per message at
+ # emit time), keeping the call in one trace. Trace context the client
+ # propagated in ``params._meta`` becomes a span *link* to that remote context,
+ # which is not a registry role, so ``SpanSpec`` has no link field.
+ SpanRole.MCP_TOOL_CALL: SpanSpec(SpanRole.MCP_TOOL_CALL, LiteLLMSpanKind.CLIENT, parent=SpanRole.PROXY_REQUEST),
+ SpanRole.MCP_LIST_TOOLS: SpanSpec(SpanRole.MCP_LIST_TOOLS, LiteLLMSpanKind.CLIENT, parent=SpanRole.PROXY_REQUEST),
SpanRole.GUARDRAIL: SpanSpec(SpanRole.GUARDRAIL, LiteLLMSpanKind.INTERNAL, parent=SpanRole.PROXY_REQUEST),
SpanRole.DB_CALL: SpanSpec(SpanRole.DB_CALL, LiteLLMSpanKind.CLIENT, parent=SpanRole.PROXY_REQUEST),
SpanRole.SERVICE: SpanSpec(SpanRole.SERVICE, LiteLLMSpanKind.INTERNAL, parent=SpanRole.PROXY_REQUEST),
@@ -209,8 +205,8 @@ def service_span_name(data: "ServiceSpanData") -> str:
def root_roles() -> list[SpanRole]:
- """Roles with no in-process parent. They start a new trace unless they adopt a
- remote parent (e.g. an MCP span joining the client's propagated context)."""
+ """Roles with no in-process parent, i.e. they start a new trace (only the
+ instrumentor-owned ``PROXY_REQUEST`` server span today)."""
return [role for role, spec in SPAN_REGISTRY.items() if spec.parent is None]
@@ -227,8 +223,6 @@ def validate_registry(
raise ValueError(f"SPAN_REGISTRY[{role}] has mismatched role {spec.role}")
if spec.parent is not None and spec.parent not in reg:
raise ValueError(f"span role {role} declares unknown parent {spec.parent}")
- if spec.links is not None and spec.links not in reg:
- raise ValueError(f"span role {role} declares unknown link target {spec.links}")
missing: Final = [role for role in SpanRole if role not in reg]
if missing:
raise ValueError(f"SPAN_REGISTRY is missing roles: {missing}")
diff --git a/litellm/integrations/otel/plumbing/context.py b/litellm/integrations/otel/plumbing/context.py
index 19b36c0b967..159a84b121f 100644
--- a/litellm/integrations/otel/plumbing/context.py
+++ b/litellm/integrations/otel/plumbing/context.py
@@ -57,8 +57,8 @@ def request_root_span() -> "Span | None":
# The W3C trace-context carrier (``traceparent``/``tracestate``/``baggage``) the
# MCP client propagated in the current request's ``params._meta``. The MCP gateway
-# sets it per message so the MCP span can parent to the client's span rather than
-# to the transport. A ``ContextVar`` because, like the root-span anchor, it must
+# sets it per message so the MCP span can record the client's span as a span
+# link. A ``ContextVar`` because, like the root-span anchor, it must
# ride the request task and be readable by the inline success-logging callback.
_mcp_message_trace_carrier: Final["ContextVar[Mapping[str, str] | None]"] = ContextVar(
"litellm_otel_mcp_message_trace_carrier", default=None
@@ -148,10 +148,10 @@ def _mcp_transport_span_context() -> "SpanContext | None":
Prefers the transport the gateway published for this specific message; falls
back to the ambient request anchor for paths that emit an MCP span on the
- request task itself (the REST MCP endpoints, the SDK). Parenting and linking
- only need the immutable context, and unlike ``mcp_message_transport_span`` they
- stay correct against a transport that has already finished, so this does not
- require the span to still be recording.
+ request task itself (the REST MCP endpoints). Parenting needs only the
+ immutable context, and unlike ``mcp_message_transport_span`` it stays correct
+ against a transport that has already finished, so this does not require the
+ span to still be recording.
"""
published: Final = _mcp_message_transport_span.get()
if published is not None:
@@ -222,25 +222,31 @@ def resolve_mcp_span_context(
) -> "tuple[Context, tuple[Link, ...]]":
"""Parent context + links for an MCP message span.
+ The span always nests under the transport span of the request carrying this
+ message, so a tool call and the ``POST`` that carried it stay in one trace.
+ The transport comes from :func:`_mcp_transport_span_context`, which is the
+ *current message's* POST rather than whatever request happened to open the
+ session, so a long-lived session does not glue every message under its first
+ request.
+
When the client propagates W3C trace context in the request's ``params._meta``
- (SEP-414), MCP and the underlying transport are independent lifecycles — one
- streamable-HTTP session multiplexes many messages, and the client's own span is
- the truthful parent. So, per the OTel GenAI MCP semconv:
-
- * parent to the trace context the client propagated (a *remote* parent), and
- * record the transport span as a *link*, never the parent.
-
- Almost no client implements SEP-414 yet, so in practice nothing is propagated.
- Rooting the span there splits a single tool call into two disconnected traces
- joined only by a link, which is how it surfaces in APM: the ``POST`` transaction
- and the ``tools/call`` span share no trace. With no remote parent to honor,
- parent to the transport span of the request carrying this message instead, so
- the call stays in one trace; no link is added since the transport is now the
- real parent. The transport comes from :func:`_mcp_transport_span_context`, which
- is the *current message's* POST rather than whatever request happened to open
- the session, so a long-lived session does not glue every message under its
- first request. With neither a remote parent nor a transport the returned context
- carries no span and the span legitimately starts its own root trace.
+ (SEP-414), that remote context is recorded as a span *link*, never the parent.
+ The OTel GenAI MCP semconv prefers the inverse (remote parent, transport link),
+ but the gateway's tracing backend only ever receives the gateway's half of such
+ a trace: parenting into the client's trace id roots the span in a trace whose
+ root span never reaches the backend, so the span is unreachable from the trace
+ view and the transport transaction shows a dangling link (observed with
+ clients that propagate synthetic trace ids). Anchoring to the gateway's own
+ request and linking the client's context keeps every trace renderable while
+ preserving the client-side correlation.
+
+ With no transport at all the span starts its own root trace, still carrying
+ the link — the client context is only ever a link, so this event keeps one
+ shape everywhere. Both returned contexts are built on an explicitly empty
+ base, so ambient (stale session) state can never leak in, and the span
+ inherits the transport's sampling decision exactly like every other
+ request-level span — a client's sampled flag neither forces nor suppresses
+ recording.
Only trace context (``traceparent``/``tracestate``) is extracted, never the
client's W3C Baggage: ``params._meta`` is caller-controlled, and the otel
@@ -251,13 +257,12 @@ def resolve_mcp_span_context(
never fall through to the ambient (stale session) span.
"""
source: Final = carrier if carrier is not None else _mcp_message_trace_carrier.get()
- parent: Final = _PROPAGATOR.extract(dict(source or {}), context=Context())
+ propagated: Final = get_current_span(_PROPAGATOR.extract(dict(source or {}), context=Context()))
+ links: Final = (Link(propagated.get_span_context()),) if is_recordable_span(propagated) else ()
transport: Final = _mcp_transport_span_context()
- if is_recordable_span(get_current_span(parent)):
- return parent, (Link(transport),) if transport is not None else ()
- if transport is not None:
- return context_from_span(NonRecordingSpan(transport)), ()
- return parent, ()
+ if transport is None:
+ return Context(), links
+ return context_from_span(NonRecordingSpan(transport), context=Context()), links
def is_recordable_span(obj: object) -> bool:
diff --git a/litellm/proxy/_experimental/mcp_server/server.py b/litellm/proxy/_experimental/mcp_server/server.py
index 3c6eb06bc71..57e59dab2d1 100644
--- a/litellm/proxy/_experimental/mcp_server/server.py
+++ b/litellm/proxy/_experimental/mcp_server/server.py
@@ -246,11 +246,12 @@ def _mcp_meta_trace_carrier(req_ctx: object) -> dict[str, str] | None:
"""The W3C trace context (``traceparent``/``tracestate``) the MCP client
propagated in the request's ``params._meta`` (SEP-414), or ``None``.
- When present, per the OTel MCP semconv the MCP span parents to this propagated
- context rather than to the HTTP transport (which is recorded as a link instead).
- When absent, the span nests under the transport span of the request carrying
- this specific message, so a streamable-HTTP session that multiplexes many
- messages still does not glue every message under the session's first request;
+ When present, the MCP span records this propagated context as a span *link*,
+ never the parent — a remote parent would root the span in a trace whose root
+ never reaches the gateway's tracing backend. The span itself nests under the
+ transport span of the request carrying this specific message, so a
+ streamable-HTTP session that multiplexes many messages still does not glue
+ every message under the session's first request;
see ``resolve_mcp_span_context``. The client's W3C Baggage is
deliberately excluded: it is caller-controlled, and the otel baggage processor
stamps allowlisted baggage keys (``litellm.team.id``, ``litellm.metadata.*``,
diff --git a/tests/test_litellm/integrations/otel/test_otel_v2_logger.py b/tests/test_litellm/integrations/otel/test_otel_v2_logger.py
index 455d84c764f..4973bda29e0 100644
--- a/tests/test_litellm/integrations/otel/test_otel_v2_logger.py
+++ b/tests/test_litellm/integrations/otel/test_otel_v2_logger.py
@@ -778,11 +778,15 @@ def test_mcp_span_roots_without_transport_or_propagated_context(
@pytest.mark.parametrize("make_payload, span_name", _MCP_SPAN_CASES)
-def test_mcp_span_parents_to_propagated_meta_trace_context(make_payload, span_name):
+def test_mcp_span_links_propagated_meta_trace_context_and_nests_under_transport(
+ make_payload, span_name
+):
"""When the client propagates W3C trace context in the request's
- ``params._meta`` (SEP-414), the MCP span parents to it (one distributed trace)
- and still links the transport span — never falling through to the
- ambient/session span."""
+ ``params._meta`` (SEP-414), the MCP span still nests under the gateway's own
+ transport span — one renderable trace — and records the client's context as a
+ span *link*. Parenting to the remote context instead would root the span in a
+ trace whose root span never reaches the gateway's tracing backend, leaving the
+ span unreachable from the trace view."""
logger, exporter = _logger()
transport = logger._emitter.start_span(
SpanRole.PROXY_REQUEST, LITELLM_PROXY_REQUEST_SPAN_NAME
@@ -801,12 +805,65 @@ def test_mcp_span_parents_to_propagated_meta_trace_context(make_payload, span_na
reset_mcp_message_trace_carrier(token)
transport.end()
span = next(s for s in exporter.get_finished_spans() if s.name == span_name)
- assert span.context.trace_id == 0x11111111111111111111111111111111
assert span.parent is not None
- assert span.parent.span_id == 0x2222222222222222
- assert [link.context.span_id for link in span.links] == [
- transport.get_span_context().span_id
+ assert span.parent.span_id == transport.get_span_context().span_id
+ assert span.context.trace_id == transport.get_span_context().trace_id
+ assert [link.context.trace_id for link in span.links] == [
+ 0x11111111111111111111111111111111
]
+ assert [link.context.span_id for link in span.links] == [0x2222222222222222]
+
+
[email protected]("make_payload, span_name", _MCP_SPAN_CASES)
+def test_mcp_span_without_transport_roots_and_links_propagated_context(
+ make_payload, span_name
+):
+ """With no transport span at all there is nothing of the gateway's to anchor
+ to, so the span starts its own root trace — and the client context stays a
+ span link there too, so the event keeps one shape everywhere."""
+ logger, exporter = _logger()
+ token = set_mcp_message_trace_carrier(
+ {"traceparent": "00-11111111111111111111111111111111-2222222222222222-01"}
+ )
+ try:
+ asyncio.run(
+ logger.async_log_success_event(
+ {"standard_logging_object": make_payload()}, None, None, None
+ )
+ )
+ finally:
+ reset_mcp_message_trace_carrier(token)
+ span = next(s for s in exporter.get_finished_spans() if s.name == span_name)
+ assert span.parent is None
+ assert span.context.trace_id != 0x11111111111111111111111111111111
+ assert [link.context.span_id for link in span.links] == [0x2222222222222222]
+
+
+def test_mcp_span_links_unsampled_client_traceparent():
+ """A client traceparent with the sampled flag off ('-00') still yields a valid
+ remote context, so the link is recorded; the span's own recording follows the
+ transport's sampling decision, never the client's flag."""
+ logger, exporter = _logger()
+ transport = logger._emitter.start_span(
+ SpanRole.PROXY_REQUEST, LITELLM_PROXY_REQUEST_SPAN_NAME
+ )
+ set_request_root_span(transport)
+ token = set_mcp_message_trace_carrier(
+ {"traceparent": "00-11111111111111111111111111111111-2222222222222222-00"}
+ )
+ try:
+ asyncio.run(
+ logger.async_log_success_event(
+ {"standard_logging_object": _mcp_list_payload()}, None, None, None
+ )
+ )
+ finally:
+ reset_mcp_message_trace_carrier(token)
+ transport.end()
+ span = next(s for s in exporter.get_finished_spans() if s.name == "tools/list")
+ assert span.parent is not None
+ assert span.parent.span_id == transport.get_span_context().span_id
+ assert [link.context.span_id for link in span.links] == [0x2222222222222222]
@pytest.mark.parametrize("make_payload, span_name", _MCP_SPAN_CASES)
@@ -839,8 +896,11 @@ def test_mcp_span_ignores_client_supplied_baggage(make_payload, span_name):
reset_mcp_message_trace_carrier(token)
transport.end()
span = next(s for s in exporter.get_finished_spans() if s.name == span_name)
- # Trace context still honored: proves the carrier was processed, not dropped wholesale.
- assert span.parent is not None and span.parent.span_id == 0x2222222222222222
+ # Trace context still honored (as a link): proves the carrier was processed,
+ # not dropped wholesale.
+ assert [link.context.span_id for link in span.links] == [0x2222222222222222]
+ assert span.parent is not None
+ assert span.parent.span_id == transport.get_span_context().span_id
# Identity is the authenticated payload's team, never the client's spoofed value.
assert span.attributes[LiteLLM.TEAM_ID] == "t1"
assert "litellm.metadata.user_api_key_user_id" not in span.attributes
@@ -888,10 +948,10 @@ def test_mcp_span_malformed_traceparent_nests_under_transport():
assert span.links == ()
-def test_mcp_span_links_this_messages_transport_when_context_is_propagated():
- """On the semconv path the transport is recorded as a link, and that link must
- point at the POST carrying this message too. Reading the stale session anchor
- would attribute the tool call to whichever request opened the session."""
+def test_mcp_span_with_propagated_context_nests_under_this_messages_transport():
+ """With client context propagated, the span must still anchor to the POST
+ carrying this message, not the stale session anchor — otherwise the tool call
+ is attributed to whichever request opened the session."""
logger, exporter = _logger()
session_opener = logger._emitter.start_span(
SpanRole.PROXY_REQUEST, LITELLM_PROXY_REQUEST_SPAN_NAME
@@ -916,10 +976,10 @@ def test_mcp_span_links_this_messages_transport_when_context_is_propagated():
session_opener.end()
this_message.end()
span = next(s for s in exporter.get_finished_spans() if s.name == "tools/list")
- assert span.parent is not None and span.parent.span_id == 0x2222222222222222
- assert [link.context.span_id for link in span.links] == [
- this_message.get_span_context().span_id
- ]
+ assert span.parent is not None
+ assert span.parent.span_id == this_message.get_span_context().span_id
+ assert span.context.trace_id == this_message.get_span_context().trace_id
+ assert [link.context.span_id for link in span.links] == [0x2222222222222222]
def test_pre_call_idempotent_keeps_first_span():
diff --git a/tests/test_litellm/integrations/otel/test_otel_v2_sources_of_truth.py b/tests/test_litellm/integrations/otel/test_otel_v2_sources_of_truth.py
index cc9b311084e..baa72b5a7fe 100644
--- a/tests/test_litellm/integrations/otel/test_otel_v2_sources_of_truth.py
+++ b/tests/test_litellm/integrations/otel/test_otel_v2_sources_of_truth.py
@@ -107,32 +107,29 @@ def test_registry_parent_integrity_no_orphans():
def test_registry_hierarchy_shape():
- # MCP roles have no in-process parent: per the MCP semconv they root (or adopt
- # the client's propagated _meta context), so they sit alongside PROXY_REQUEST.
- assert set(root_roles()) == {
- SpanRole.PROXY_REQUEST,
- SpanRole.MCP_TOOL_CALL,
- SpanRole.MCP_LIST_TOOLS,
- }
+ assert set(root_roles()) == {SpanRole.PROXY_REQUEST}
# Guardrails parent to the request span, not the LLM call: a pre-call
- # guardrail runs before the LLM call exists, so it's a sibling of it.
+ # guardrail runs before the LLM call exists, so it's a sibling of it. MCP
+ # spans nest under the transport span of the request carrying that message.
assert set(child_roles(SpanRole.PROXY_REQUEST)) == {
SpanRole.LLM_CALL,
SpanRole.GUARDRAIL,
SpanRole.DB_CALL,
SpanRole.SERVICE,
+ SpanRole.MCP_TOOL_CALL,
+ SpanRole.MCP_LIST_TOOLS,
}
assert SPAN_REGISTRY[SpanRole.LLM_CALL].kind is LiteLLMSpanKind.CLIENT
# The proxy is an MCP client to the upstream tool server: CLIENT span. Listing
# tools is the same client relationship, so it's a CLIENT span too.
assert SPAN_REGISTRY[SpanRole.MCP_TOOL_CALL].kind is LiteLLMSpanKind.CLIENT
assert SPAN_REGISTRY[SpanRole.MCP_LIST_TOOLS].kind is LiteLLMSpanKind.CLIENT
- # MCP spans don't nest under the transport: they link the PROXY_REQUEST span
- # instead of parenting to it (OTel GenAI MCP semconv).
- assert SPAN_REGISTRY[SpanRole.MCP_TOOL_CALL].parent is None
- assert SPAN_REGISTRY[SpanRole.MCP_LIST_TOOLS].parent is None
- assert SPAN_REGISTRY[SpanRole.MCP_TOOL_CALL].links is SpanRole.PROXY_REQUEST
- assert SPAN_REGISTRY[SpanRole.MCP_LIST_TOOLS].links is SpanRole.PROXY_REQUEST
+ # MCP spans nest under the transport span of the request carrying that
+ # message (resolved per message at emit time); a client-propagated context
+ # becomes a span link to that remote context, which is not a registry role
+ # (SpanSpec declares no link field at all).
+ assert SPAN_REGISTRY[SpanRole.MCP_TOOL_CALL].parent is SpanRole.PROXY_REQUEST
+ assert SPAN_REGISTRY[SpanRole.MCP_LIST_TOOLS].parent is SpanRole.PROXY_REQUEST
assert SPAN_REGISTRY[SpanRole.PROXY_REQUEST].kind is LiteLLMSpanKind.SERVER
assert SPAN_REGISTRY[SpanRole.GUARDRAIL].parent is SpanRole.PROXY_REQUEST
# An outbound datastore call is a CLIENT span; an internal service is INTERNAL.