strands.tools.mcp.mcp_instrumentation
¶
OpenTelemetry instrumentation for Model Context Protocol (MCP) tracing.
Enables distributed tracing across MCP client-server boundaries by injecting OpenTelemetry context into MCP request metadata (_meta field) and extracting it on the server side, creating unified traces that span from agent calls through MCP tool executions.
Based on: https://github.com/traceloop/openllmetry/tree/main/packages/opentelemetry-instrumentation-mcp Related issue: https://github.com/modelcontextprotocol/modelcontextprotocol/issues/246
_instrumentation_applied = False
module-attribute
¶
ItemWithContext
dataclass
¶
Wrapper for items that need to carry OpenTelemetry context.
Used to preserve tracing context across async boundaries in MCP sessions, ensuring that distributed traces remain connected even when messages are processed asynchronously.
Attributes:
| Name | Type | Description |
|---|---|---|
item |
Any
|
The original item being wrapped |
ctx |
Context
|
The OpenTelemetry context associated with the item |
Source code in strands/tools/mcp/mcp_instrumentation.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
SessionContextAttachingReader
¶
Bases: ObjectProxy
A proxy reader that restores OpenTelemetry context from wrapped items.
Wraps an async message stream reader to detect ItemWithContext instances and restore their associated OpenTelemetry context during processing. This completes the context preservation cycle started by SessionContextSavingWriter.
Source code in strands/tools/mcp/mcp_instrumentation.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | |
__aenter__()
async
¶
Enter the async context manager by delegating to the wrapped object.
Source code in strands/tools/mcp/mcp_instrumentation.py
309 310 311 | |
__aexit__(exc_type, exc_value, traceback)
async
¶
Exit the async context manager by delegating to the wrapped object.
Source code in strands/tools/mcp/mcp_instrumentation.py
313 314 315 | |
__aiter__()
async
¶
Iterate over items, restoring context for ItemWithContext instances.
For items wrapped with context, temporarily activates the associated OpenTelemetry context during processing, then properly detaches it. Regular items are yielded without context modification.
Yields:
| Type | Description |
|---|---|
AsyncGenerator[Any, None]
|
Unwrapped items processed under their associated OpenTelemetry context |
Source code in strands/tools/mcp/mcp_instrumentation.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | |
__init__(wrapped)
¶
Initialize the context-attaching reader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wrapped
|
Any
|
The original async stream reader to wrap |
required |
Source code in strands/tools/mcp/mcp_instrumentation.py
301 302 303 304 305 306 307 | |
SessionContextSavingWriter
¶
Bases: ObjectProxy
A proxy writer that preserves OpenTelemetry context with outgoing items.
Wraps an async message stream writer to capture the current OpenTelemetry context and associate it with outgoing items. This enables context preservation across async boundaries in MCP session processing.
Source code in strands/tools/mcp/mcp_instrumentation.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
__aenter__()
async
¶
Enter the async context manager by delegating to the wrapped object.
Source code in strands/tools/mcp/mcp_instrumentation.py
269 270 271 | |
__aexit__(exc_type, exc_value, traceback)
async
¶
Exit the async context manager by delegating to the wrapped object.
Source code in strands/tools/mcp/mcp_instrumentation.py
273 274 275 | |
__init__(wrapped)
¶
Initialize the context-saving writer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wrapped
|
Any
|
The original async stream writer to wrap |
required |
Source code in strands/tools/mcp/mcp_instrumentation.py
261 262 263 264 265 266 267 | |
send(item)
async
¶
Send an item while preserving the current OpenTelemetry context.
Captures the current context and wraps the item with it, enabling the receiving side to restore the appropriate tracing context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
Any
|
The item to send through the stream |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Result of sending the wrapped item |
Source code in strands/tools/mcp/mcp_instrumentation.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
TransportContextExtractingReader
¶
Bases: ObjectProxy
A proxy reader that extracts OpenTelemetry context from MCP messages.
Wraps an async message stream reader to automatically extract and activate OpenTelemetry context from the _meta field of incoming MCP requests. This enables server-side trace continuation from client-injected context.
The reader handles both SessionMessage and JSONRPCMessage formats, and supports both dict and Pydantic model parameter structures.
Source code in strands/tools/mcp/mcp_instrumentation.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
__aenter__()
async
¶
Enter the async context manager by delegating to the wrapped object.
Source code in strands/tools/mcp/mcp_instrumentation.py
203 204 205 | |
__aexit__(exc_type, exc_value, traceback)
async
¶
Exit the async context manager by delegating to the wrapped object.
Source code in strands/tools/mcp/mcp_instrumentation.py
207 208 209 | |
__aiter__()
async
¶
Iterate over messages, extracting and activating context as needed.
For each incoming message, checks if it contains tracing context in the _meta field. If found, extracts and activates the context for the duration of message processing, then properly detaches it.
Yields:
| Type | Description |
|---|---|
AsyncGenerator[Any, None]
|
Messages from the wrapped stream, processed under the appropriate |
AsyncGenerator[Any, None]
|
OpenTelemetry context |
Source code in strands/tools/mcp/mcp_instrumentation.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
__init__(wrapped)
¶
Initialize the context-extracting reader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wrapped
|
Any
|
The original async stream reader to wrap |
required |
Source code in strands/tools/mcp/mcp_instrumentation.py
195 196 197 198 199 200 201 | |
mcp_instrumentation()
¶
Apply OpenTelemetry instrumentation patches to MCP components.
This function instruments three key areas of MCP communication: 1. Client-side: Injects tracing context into tool call requests 2. Transport-level: Extracts context from incoming messages 3. Session-level: Manages bidirectional context flow
The patches enable distributed tracing by: - Adding OpenTelemetry context to the _meta field of MCP requests - Extracting and activating context on the server side - Preserving context across async message processing boundaries
This function is idempotent - multiple calls will not accumulate wrappers.
Source code in strands/tools/mcp/mcp_instrumentation.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |