|
| 1 | +--- |
| 2 | +title: MCP Tracing |
| 3 | +sidebar_title: Tracing |
| 4 | +--- |
| 5 | + |
| 6 | +The MCP Server module is the instrumentation for the [anthropic MCP](https://www.anthropic.com/news/model-context-protocol) SDKs. At the moment it only supports the [MCP Typescript SDK](https://github.com/modelcontextprotocol/typescript-sdk/). |
| 7 | + |
| 8 | +## Spans Conventions |
| 9 | + |
| 10 | +For your MCP Server data to show up in Sentry, specific spans must be created with well-defined names and data attributes. See below. |
| 11 | + |
| 12 | +We follow the draft [OpenTelemetry MCP Semantic Conventions](https://github.com/open-telemetry/semantic-conventions/pull/2083) as closely as possible, with some opinionated additions for improved MCP observability. We should keep an eye on the conventions and update this guide as needed as they are currently in draft and subject to change. |
| 13 | + |
| 14 | +### Common Span Attributes |
| 15 | + |
| 16 | +All MCP Server spans share common attributes and follow consistent patterns: |
| 17 | + |
| 18 | +| Data Attribute | Type | Requirement Level | Description | Example | |
| 19 | +| :-------------------------- | :----- | :---------------- | :---------------------------------------------------------------------------- | :------------------------------------- | |
| 20 | +| `mcp.method.name` | string | required | The name of the request or notification method. | `"tools/call"` | |
| 21 | +| `mcp.transport` | string | required | The transport method used for MCP communication. **[2]** | `"stdio"` | |
| 22 | +| `network.transport` | string | required | OSI transport layer protocol. **[3]** | `"pipe"` | |
| 23 | +| `mcp.request.id` | string | optional | A unique identifier for the request. | `"42"` | |
| 24 | +| `mcp.session.id` | string | optional | Identifies the MCP session. Can be undefined as MCP servers can be stateless. | `"191c4850af6c49..."` | |
| 25 | +| `network.protocol.version` | string | optional | The version of JSON RPC protocol used. | `"2.0"` | |
| 26 | + |
| 27 | +**[2]** Well-defined values for data attribute `mcp.transport`: |
| 28 | + |
| 29 | +| Value | Description | |
| 30 | +| :------- | :------------------------------------ | |
| 31 | +| `"http"` | HTTP transport | |
| 32 | +| `"sse"` | Server-Sent Events transport | |
| 33 | +| `"stdio"` | Standard input/output transport | |
| 34 | +| `"unknown"`* | Unknown transport (for custom transports)| |
| 35 | + |
| 36 | +*`"unknown"` is a placeholder for custom transports. We could try to infer the name from the transport object. |
| 37 | + |
| 38 | +**[3]** Well-defined values for data attribute `network.transport`: |
| 39 | + |
| 40 | +| Value | Description | |
| 41 | +| :------- | :------------------------------------ | |
| 42 | +| `"pipe"` | Named pipe (for stdio transport) | |
| 43 | +| `"tcp"` | TCP (for HTTP/SSE transport) | |
| 44 | + |
| 45 | +### Session Management |
| 46 | + |
| 47 | +MCP servers can be either stateless or stateful. Understanding this distinction is important for proper tracing implementation and session data handling. (See [MCP Server Session Management](https://modelcontextprotocol.io/docs/concepts/transports#session-management) for more details.) |
| 48 | + |
| 49 | +#### Stateless vs Stateful Servers |
| 50 | + |
| 51 | +**Stateless Servers**: These servers treat each request independently without maintaining session state between requests. The `mcp.session.id` attribute may be undefined or not meaningful for such servers. |
| 52 | + |
| 53 | +**Stateful Servers**: These servers maintain session state across multiple requests from the same client. They use a consistent `mcp.session.id` that is shared across all messages within the same session lifecycle. All MCP request from the client should always have the HTTP header `X-MCP-Session-Id` set to the same value. |
| 54 | + |
| 55 | +#### Session Data Attributes |
| 56 | + |
| 57 | +For stateful servers, additional session metadata is collected during the initialization phase and attached to all subsequent spans within the same session. This provides observability and context for the entire session lifecycle. |
| 58 | + |
| 59 | +| Data Attribute | Type | Requirement Level | Description | Example | |
| 60 | +| :-------------------------- | :----- | :---------------- | :---------------------------------------------------------------------------- | :------------------------------------- | |
| 61 | +| `mcp.client.name` | string | optional | Name of the MCP client application. | `"claude-desktop"` | |
| 62 | +| `mcp.client.title` | string | optional | Display title of the MCP client application. | `"Claude Desktop"` | |
| 63 | +| `mcp.client.version` | string | optional | Version of the MCP client application. | `"1.0.0"` | |
| 64 | +| `mcp.server.name` | string | optional | Name of the MCP server application. | `"my-mcp-server"` | |
| 65 | +| `mcp.server.title` | string | optional | Display title of the MCP server application. | `"My MCP Server"` | |
| 66 | +| `mcp.server.version` | string | optional | Version of the MCP server application. | `"2.1.3"` | |
| 67 | +| `mcp.protocol.version` | string | optional | MCP protocol version used in the session. | `"2024-11-05"` | |
| 68 | + |
| 69 | +#### Session Lifecycle |
| 70 | + |
| 71 | +1. **Initialization**: During the `initialize` request, session metadata (client info, server info, protocol version) is established and stored. |
| 72 | + |
| 73 | +2. **Session Propagation**: For all subsequent requests within the same session (tool calls, prompt requests, resource access), the session metadata is automatically attached to each span. |
| 74 | + |
| 75 | +3. **Evolution**: As the MCP specification evolves, additional session-level metadata may be captured and propagated, providing richer context for observability. |
| 76 | + |
| 77 | +This session-based approach allows us to: |
| 78 | +- Track client and server versions across infrastructure |
| 79 | +- Analyze protocol version adoption and compatibility |
| 80 | +- Group and correlate all operations within a single client session |
| 81 | +- Monitor session duration and patterns |
| 82 | + |
| 83 | +### MCP Server Span Types |
| 84 | + |
| 85 | +MCP Server spans use the operation value `"mcp.server"` and fall into these categories: |
| 86 | + |
| 87 | +**Server Methods** (op: `"mcp.server"`): |
| 88 | +- **Tool calls** - `tools/call {tool_name}` - Execute client-requested tools |
| 89 | +- **Prompt requests** - `prompts/get {prompt_name}` - Provide prompt templates to clients |
| 90 | +- **Resource access** - `resources/read {resource_uri}` - Serve resources to clients |
| 91 | + |
| 92 | +**Notifications** (op: `"mcp.notification.client_to_server"` or `"mcp.notification.server_to_client"`): |
| 93 | +- One-way messages that don't expect responses (e.g., `notifications/cancelled`) |
| 94 | + |
| 95 | +**Others** (op: `"mcp.server"`): |
| 96 | +- **Initialization** - `initialize` - Handle client connection setup |
| 97 | + |
| 98 | +### Tool Call Span |
| 99 | + |
| 100 | +Describes MCP tool execution requests from clients. |
| 101 | + |
| 102 | +- The spans `op` MUST be `"mcp.server"`. |
| 103 | +- The span `name` SHOULD follow the pattern `"tools/call {mcp.tool.name}"`. (e.g. `"tools/call get_weather"`) |
| 104 | +- The `mcp.method.name` attribute MUST be `"tools/call"`. |
| 105 | +- The `mcp.tool.name` attribute SHOULD be set to the tool name. (e.g. `"get_weather"`) |
| 106 | +- All [Common Span Attributes](#common-span-attributes) SHOULD be set (all `required` common attributes MUST be set). |
| 107 | + |
| 108 | +Additional attributes on the span: |
| 109 | + |
| 110 | +| Data Attribute | Type | Requirement Level | Description | Example | |
| 111 | +| :------------------------------------ | :------ | :---------------- | :---------------------------------------------------------------------------- | :------------------------------------- | |
| 112 | +| `mcp.tool.result.is_error` | boolean | optional | Whether the tool execution resulted in an error. | `true` | |
| 113 | +| `mcp.tool.result.content_count` | int | optional | Number of content items in the tool result. | `3` | |
| 114 | +| `mcp.tool.result.content` | string | optional | Serialized content of the tool result. **[0]** | `"[{\"type\":\"text\"}]"` | |
| 115 | +| `mcp.request.argument.<key>` | string | optional | Arguments passed to the tool. **[1]** | `"Seattle, WA"` | |
| 116 | + |
| 117 | +**[0]**: Span attributes only allow primitive data types. This means you need to use a stringified version of a list of dictionaries. Do NOT set `[{"type": "text"}]` but rather the string `"[{\"type\": \"text\"}]"`. |
| 118 | +**[1]**: Arguments are prefixed with `mcp.request.argument.` followed by the argument key. For example, if a tool receives `{location: "Seattle, WA"}`, it becomes `mcp.request.argument.location`. |
| 119 | + |
| 120 | +### Prompt Span |
| 121 | + |
| 122 | +Describes MCP prompt requests from clients. |
| 123 | + |
| 124 | +- The spans `op` MUST be `"mcp.server"`. |
| 125 | +- The span `name` SHOULD follow the pattern `"prompts/get {mcp.prompt.name}"`. (e.g. `"prompts/get analyze-code"`) |
| 126 | +- The `mcp.method.name` attribute MUST be `"prompts/get"`. |
| 127 | +- The `mcp.prompt.name` attribute SHOULD be set to the prompt name. (e.g. `"analyze-code"`) |
| 128 | +- All [Common Span Attributes](#common-span-attributes) SHOULD be set (all `required` common attributes MUST be set). |
| 129 | + |
| 130 | +Additional attributes on the span: |
| 131 | + |
| 132 | +| Data Attribute | Type | Requirement Level | Description | Example | |
| 133 | +| :------------------------------------ | :----- | :---------------- | :---------------------------------------------------------------------------- | :------------------------------------- | |
| 134 | +| `mcp.request.argument.<key>` | string | optional | Additional arguments passed to the prompt. **[1]** | `"javascript"` | |
| 135 | + |
| 136 | +### Resource Span |
| 137 | + |
| 138 | +Describes MCP resource access requests from clients. |
| 139 | + |
| 140 | +- The spans `op` MUST be `"mcp.server"`. |
| 141 | +- The span `name` SHOULD follow the pattern `"resources/read {mcp.resource.uri}"`. (e.g. `"resources/read file:///path/to/file"`) |
| 142 | +- The `mcp.method.name` attribute MUST be `"resources/read"`. |
| 143 | +- The `mcp.resource.uri` attribute SHOULD be set to the resource URI. (e.g. `"file:///path/to/file"`) |
| 144 | +- All [Common Span Attributes](#common-span-attributes) SHOULD be set (all `required` common attributes MUST be set). |
| 145 | + |
| 146 | +Additional attributes on the span: |
| 147 | + |
| 148 | +| Data Attribute | Type | Requirement Level | Description | Example | |
| 149 | +| :------------------------------------ | :----- | :---------------- | :---------------------------------------------------------------------------- | :------------------------------------- | |
| 150 | +| `mcp.resource.name` | string | optional | The name of the resource being accessed. | `"sentry-docs-platform"` | |
| 151 | + |
| 152 | +### Initialize Span |
| 153 | + |
| 154 | +Describes MCP initialization requests from clients. |
| 155 | + |
| 156 | +- The spans `op` MUST be `"mcp.server"`. |
| 157 | +- The span `name` SHOULD be `"initialize"`. |
| 158 | +- The `mcp.method.name` attribute MUST be `"initialize"`. |
| 159 | +- All [Common Span Attributes](#common-span-attributes) SHOULD be set (all `required` common attributes MUST be set). |
| 160 | + |
| 161 | +Additional attributes on the span: |
| 162 | + |
| 163 | +| Data Attribute | Type | Requirement Level | Description | Example | |
| 164 | +| :------------------------------------ | :----- | :---------------- | :---------------------------------------------------------------------------- | :------------------------------------- | |
| 165 | +| `client.address` | string | optional | Client address - domain name or IP address. | `"127.0.0.1"` | |
| 166 | +| `client.port` | int | optional | Client port number. | `8080` | |
| 167 | + |
| 168 | +### Notification Span |
| 169 | + |
| 170 | +Describes MCP notification messages (one-way messages that don't expect a response). |
| 171 | + |
| 172 | +- The spans `op` MUST be `"mcp.notification.client_to_server"` or `"mcp.notification.server_to_client"`. |
| 173 | +- The span `name` SHOULD be the notification method name. (e.g. `"notifications/cancelled"`) |
| 174 | +- The `mcp.method.name` attribute MUST be set to the notification method. (e.g. `"notifications/cancelled"`) |
| 175 | +- All [Common Span Attributes](#common-span-attributes) SHOULD be set (all `required` common attributes MUST be set). |
| 176 | + |
0 commit comments