|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | | -from collections.abc import Iterator |
| 5 | +from collections.abc import Iterator, Mapping |
6 | 6 | from contextlib import contextmanager |
7 | | -from typing import Any |
| 7 | +from typing import Any, cast |
8 | 8 |
|
9 | 9 | from opentelemetry.context import Context |
10 | 10 | from opentelemetry.propagate import extract, inject |
11 | 11 | from opentelemetry.trace import SpanKind, get_tracer |
12 | 12 |
|
13 | 13 | _tracer = get_tracer("mcp-python-sdk") |
14 | 14 |
|
| 15 | +# Maps MCP JSON-RPC method names to GenAI semantic convention operation names. |
| 16 | +# https://github.com/open-telemetry/semantic-conventions-genai/blob/main/docs/gen-ai/mcp.md |
| 17 | +_METHOD_TO_GEN_AI_OPERATION: dict[str, str] = { |
| 18 | + "tools/call": "execute_tool", |
| 19 | + "tools/list": "list_tools", |
| 20 | + "resources/read": "read_resource", |
| 21 | + "resources/list": "list_resources", |
| 22 | + "resources/templates/list": "list_resources", |
| 23 | + "prompts/get": "get_prompt", |
| 24 | + "prompts/list": "list_prompts", |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +def build_span_attributes( |
| 29 | + method: str, |
| 30 | + request_id: Any, |
| 31 | + *, |
| 32 | + params: dict[str, Any] | None = None, |
| 33 | + server_name: str | None = None, |
| 34 | + session_id: str | None = None, |
| 35 | +) -> dict[str, Any]: |
| 36 | + """Build OTel span attributes for an MCP request. |
| 37 | +
|
| 38 | + Produces the base set of semantic convention attributes shared by both |
| 39 | + client (`SpanKind.CLIENT`) and server (`SpanKind.SERVER`) spans. |
| 40 | + Pass `server_name` and `session_id` for server-side spans. |
| 41 | + """ |
| 42 | + attrs: dict[str, Any] = { |
| 43 | + "rpc.system": "mcp", |
| 44 | + "mcp.method.name": method, |
| 45 | + "jsonrpc.request.id": str(request_id), |
| 46 | + } |
| 47 | + |
| 48 | + operation = _METHOD_TO_GEN_AI_OPERATION.get(method) |
| 49 | + if operation is not None: |
| 50 | + attrs["gen_ai.operation.name"] = operation |
| 51 | + |
| 52 | + if server_name is not None: |
| 53 | + attrs["rpc.service"] = server_name |
| 54 | + |
| 55 | + if params is not None: |
| 56 | + # gen_ai.tool.name — present on tools/call, prompts/get |
| 57 | + name = params.get("name") |
| 58 | + if isinstance(name, str): |
| 59 | + attrs["gen_ai.tool.name"] = name |
| 60 | + |
| 61 | + # mcp.resource.uri — present on resources/read; also on completion/complete via ref.uri |
| 62 | + uri: Any = params.get("uri") |
| 63 | + if uri is None: |
| 64 | + ref = params.get("ref") |
| 65 | + if isinstance(ref, dict): |
| 66 | + uri = cast(dict[str, Any], ref).get("uri") |
| 67 | + if uri is not None: |
| 68 | + attrs["mcp.resource.uri"] = str(uri) |
| 69 | + |
| 70 | + if session_id is not None: |
| 71 | + attrs["mcp.session.id"] = session_id |
| 72 | + |
| 73 | + return attrs |
| 74 | + |
15 | 75 |
|
16 | 76 | @contextmanager |
17 | 77 | def otel_span( |
|
0 commit comments