Skip to content

Commit 8ae416e

Browse files
committed
Use SpanKind enum directly instead of string conversion
1 parent f79767e commit 8ae416e

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

src/mcp/server/lowlevel/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ async def main():
4545
from typing import Any, Generic, cast
4646

4747
import anyio
48-
from opentelemetry.trace import StatusCode
48+
from opentelemetry.trace import SpanKind, StatusCode
4949
from starlette.applications import Starlette
5050
from starlette.middleware import Middleware
5151
from starlette.middleware.authentication import AuthenticationMiddleware
@@ -457,7 +457,7 @@ async def _handle_request(
457457

458458
with otel_span(
459459
span_name,
460-
kind="SERVER",
460+
kind=SpanKind.SERVER,
461461
attributes={"mcp.method.name": req.method, "jsonrpc.request.id": message.request_id},
462462
context=parent_context,
463463
) as span:

src/mcp/shared/_otel.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717
def otel_span(
1818
name: str,
1919
*,
20-
kind: str = "INTERNAL",
20+
kind: SpanKind,
2121
attributes: dict[str, Any] | None = None,
2222
context: Context | None = None,
2323
) -> Iterator[Any]:
2424
"""Create an OTel span."""
25-
span_kind = getattr(SpanKind, kind, SpanKind.INTERNAL)
26-
with _tracer.start_as_current_span(name, kind=span_kind, attributes=attributes, context=context) as span:
25+
with _tracer.start_as_current_span(name, kind=kind, attributes=attributes, context=context) as span:
2726
yield span
2827

2928

src/mcp/shared/session.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import anyio
1111
from anyio.streams.memory import MemoryObjectSendStream
12+
from opentelemetry.trace import SpanKind
1213
from pydantic import BaseModel, TypeAdapter
1314
from typing_extensions import Self
1415

@@ -274,7 +275,7 @@ async def send_request(
274275

275276
with otel_span(
276277
span_name,
277-
kind="CLIENT",
278+
kind=SpanKind.CLIENT,
278279
attributes={"mcp.method.name": request.method, "jsonrpc.request.id": request_id},
279280
):
280281
# Inject W3C trace context into _meta (SEP-414).

0 commit comments

Comments
 (0)