Skip to content

Commit 78fbe27

Browse files
feat(core): reserved trace context _meta keys and propagation docs (SEP-414) (modelcontextprotocol#2270)
Co-authored-by: Felix Weinberger <3823880+felixweinberger@users.noreply.github.com>
1 parent e84c3e9 commit 78fbe27

8 files changed

Lines changed: 330 additions & 5 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@modelcontextprotocol/core': patch
3+
---
4+
5+
Add reserved trace context `_meta` key constants (`TRACEPARENT_META_KEY`, `TRACESTATE_META_KEY`, `BAGGAGE_META_KEY`) per SEP-414, plus docs and a passthrough regression test. The spec reserves the unprefixed `_meta` keys `traceparent`, `tracestate`, and `baggage` (W3C Trace Context / W3C Baggage formats) for distributed tracing; the SDK passes them through untouched.

docs/client.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import {
2626
SdkError,
2727
SdkErrorCode,
2828
SSEClientTransport,
29-
StreamableHTTPClientTransport
29+
StreamableHTTPClientTransport,
30+
TRACEPARENT_META_KEY,
31+
TRACESTATE_META_KEY
3032
} from '@modelcontextprotocol/client';
3133
import { StdioClientTransport } from '@modelcontextprotocol/client/stdio';
3234
```
@@ -580,6 +582,58 @@ const transport = new StreamableHTTPClientTransport(new URL('http://localhost:30
580582
});
581583
```
582584

585+
## Trace context propagation
586+
587+
The MCP specification ([SEP-414](https://github.com/modelcontextprotocol/modelcontextprotocol/pull/414)) reserves the unprefixed `_meta` keys `traceparent`, `tracestate`, and `baggage` for distributed trace context, as an exception to the usual `_meta` key prefix rule. When present, the values must follow the [W3C Trace Context](https://www.w3.org/TR/trace-context/) and [W3C Baggage](https://www.w3.org/TR/baggage/) formats. The SDK does not interpret these keys — `_meta` passes through both directions untouched — so you can propagate OpenTelemetry context across any transport, including stdio where HTTP headers are unavailable. The key names are exported as `TRACEPARENT_META_KEY`, `TRACESTATE_META_KEY`, and `BAGGAGE_META_KEY`.
588+
589+
Attach trace context to a single request via `_meta`:
590+
591+
```ts source="../examples/client/src/clientGuide.examples.ts#traceContext_perRequest"
592+
// Values would normally come from your tracer's active span context.
593+
const result = await client.callTool({
594+
name: 'calculate-bmi',
595+
arguments: { weightKg: 70, heightM: 1.75 },
596+
_meta: {
597+
[TRACEPARENT_META_KEY]: '00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01',
598+
[TRACESTATE_META_KEY]: 'vendor1=opaqueValue1'
599+
}
600+
});
601+
console.log(result.content);
602+
```
603+
604+
Or inject it into every outgoing request with fetch middleware (Streamable HTTP transport):
605+
606+
```ts source="../examples/client/src/clientGuide.examples.ts#traceContext_middleware"
607+
const traceContextMiddleware = createMiddleware(async (next, input, init) => {
608+
if (typeof init?.body !== 'string') {
609+
return next(input, init);
610+
}
611+
const message = JSON.parse(init.body) as {
612+
method?: string;
613+
params?: { _meta?: Record<string, unknown>; [key: string]: unknown };
614+
};
615+
// Only requests and notifications carry params._meta; skip responses.
616+
if (message.method === undefined) {
617+
return next(input, init);
618+
}
619+
message.params = {
620+
...message.params,
621+
_meta: {
622+
...message.params?._meta,
623+
// Replace with values from your tracer's active span context.
624+
[TRACEPARENT_META_KEY]: '00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01'
625+
}
626+
};
627+
return next(input, { ...init, body: JSON.stringify(message) });
628+
});
629+
630+
const transport = new StreamableHTTPClientTransport(new URL('http://localhost:3000/mcp'), {
631+
fetch: applyMiddlewares(traceContextMiddleware)(fetch)
632+
});
633+
```
634+
635+
On the server side, handlers can read the incoming trace context from `ctx.mcpReq._meta` — see the [server guide](./server.md#trace-context-propagation).
636+
583637
## Resumption tokens
584638

585639
When using SSE-based streaming, the server can assign event IDs. Pass `onresumptiontoken` to track them, and `resumptionToken` to resume from where you left off after a disconnection:

docs/server.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { randomUUID } from 'node:crypto';
2222
import { createMcpExpressApp } from '@modelcontextprotocol/express';
2323
import { NodeStreamableHTTPServerTransport } from '@modelcontextprotocol/node';
2424
import type { CallToolResult, ResourceLink } from '@modelcontextprotocol/server';
25-
import { completable, McpServer, ResourceTemplate } from '@modelcontextprotocol/server';
25+
import { completable, McpServer, ResourceTemplate, TRACEPARENT_META_KEY } from '@modelcontextprotocol/server';
2626
import { StdioServerTransport } from '@modelcontextprotocol/server/stdio';
2727
import * as z from 'zod/v4';
2828
```
@@ -384,6 +384,34 @@ server.registerTool(
384384

385385
`progress` must increase on each call. `total` and `message` are optional. If the client does not provide a `progressToken`, skip the notification.
386386

387+
## Trace context propagation
388+
389+
The MCP specification ([SEP-414](https://github.com/modelcontextprotocol/modelcontextprotocol/pull/414)) reserves the unprefixed `_meta` keys `traceparent`, `tracestate`, and `baggage` for distributed trace context, as an exception to the usual `_meta` key prefix rule. When present, the values must follow the [W3C Trace Context](https://www.w3.org/TR/trace-context/) and [W3C Baggage](https://www.w3.org/TR/baggage/) formats. The SDK does not interpret these keys — `_meta` passes through untouched on any transport, including stdio. The key names are exported as `TRACEPARENT_META_KEY`, `TRACESTATE_META_KEY`, and `BAGGAGE_META_KEY`.
390+
391+
Read the caller's trace context from `ctx.mcpReq._meta` in a handler:
392+
393+
```ts source="../examples/server/src/serverGuide.examples.ts#registerTool_traceContext"
394+
server.registerTool(
395+
'traced-operation',
396+
{
397+
description: 'Operation that participates in distributed tracing',
398+
inputSchema: z.object({ query: z.string() })
399+
},
400+
async ({ query }, ctx): Promise<CallToolResult> => {
401+
// e.g. '00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01'
402+
const traceparent = ctx.mcpReq._meta?.[TRACEPARENT_META_KEY];
403+
if (typeof traceparent === 'string') {
404+
// Continue the caller's trace, e.g. start a child span with your
405+
// OpenTelemetry tracer using this trace context.
406+
}
407+
408+
return { content: [{ type: 'text', text: `Results for ${query}` }] };
409+
}
410+
);
411+
```
412+
413+
To propagate context onward (for example on a server-initiated sampling request, or back on a response), set the same keys in the outgoing `_meta`. See the [client guide](./client.md#trace-context-propagation) for injecting trace context on the client side.
414+
387415
## Server-initiated requests
388416

389417
MCP is bidirectional — servers can send requests *to* the client during tool execution, as long as the client declares matching capabilities (see [Architecture](https://modelcontextprotocol.io/docs/learn/architecture) in the MCP overview).

examples/client/src/clientGuide.examples.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import {
2121
SdkError,
2222
SdkErrorCode,
2323
SSEClientTransport,
24-
StreamableHTTPClientTransport
24+
StreamableHTTPClientTransport,
25+
TRACEPARENT_META_KEY,
26+
TRACESTATE_META_KEY
2527
} from '@modelcontextprotocol/client';
2628
import { StdioClientTransport } from '@modelcontextprotocol/client/stdio';
2729
//#endregion imports
@@ -522,6 +524,55 @@ async function middleware_basic() {
522524
return transport;
523525
}
524526

527+
/** Example: Attach W3C Trace Context to a single request via `_meta`. */
528+
async function traceContext_perRequest(client: Client) {
529+
//#region traceContext_perRequest
530+
// Values would normally come from your tracer's active span context.
531+
const result = await client.callTool({
532+
name: 'calculate-bmi',
533+
arguments: { weightKg: 70, heightM: 1.75 },
534+
_meta: {
535+
[TRACEPARENT_META_KEY]: '00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01',
536+
[TRACESTATE_META_KEY]: 'vendor1=opaqueValue1'
537+
}
538+
});
539+
console.log(result.content);
540+
//#endregion traceContext_perRequest
541+
}
542+
543+
/** Example: Client middleware that injects trace context into every outgoing request. */
544+
async function traceContext_middleware() {
545+
//#region traceContext_middleware
546+
const traceContextMiddleware = createMiddleware(async (next, input, init) => {
547+
if (typeof init?.body !== 'string') {
548+
return next(input, init);
549+
}
550+
const message = JSON.parse(init.body) as {
551+
method?: string;
552+
params?: { _meta?: Record<string, unknown>; [key: string]: unknown };
553+
};
554+
// Only requests and notifications carry params._meta; skip responses.
555+
if (message.method === undefined) {
556+
return next(input, init);
557+
}
558+
message.params = {
559+
...message.params,
560+
_meta: {
561+
...message.params?._meta,
562+
// Replace with values from your tracer's active span context.
563+
[TRACEPARENT_META_KEY]: '00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01'
564+
}
565+
};
566+
return next(input, { ...init, body: JSON.stringify(message) });
567+
});
568+
569+
const transport = new StreamableHTTPClientTransport(new URL('http://localhost:3000/mcp'), {
570+
fetch: applyMiddlewares(traceContextMiddleware)(fetch)
571+
});
572+
//#endregion traceContext_middleware
573+
return transport;
574+
}
575+
525576
/** Example: Track resumption tokens for SSE reconnection. */
526577
async function resumptionToken_basic(client: Client) {
527578
//#region resumptionToken_basic
@@ -572,4 +623,6 @@ void errorHandling_toolErrors;
572623
void errorHandling_lifecycle;
573624
void errorHandling_timeout;
574625
void middleware_basic;
626+
void traceContext_perRequest;
627+
void traceContext_middleware;
575628
void resumptionToken_basic;

examples/server/src/serverGuide.examples.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { randomUUID } from 'node:crypto';
1313
import { createMcpExpressApp } from '@modelcontextprotocol/express';
1414
import { NodeStreamableHTTPServerTransport } from '@modelcontextprotocol/node';
1515
import type { CallToolResult, ResourceLink } from '@modelcontextprotocol/server';
16-
import { completable, McpServer, ResourceTemplate } from '@modelcontextprotocol/server';
16+
import { completable, McpServer, ResourceTemplate, TRACEPARENT_META_KEY } from '@modelcontextprotocol/server';
1717
import { StdioServerTransport } from '@modelcontextprotocol/server/stdio';
1818
import * as z from 'zod/v4';
1919
//#endregion imports
@@ -319,6 +319,29 @@ function registerTool_progress(server: McpServer) {
319319
//#endregion registerTool_progress
320320
}
321321

322+
/** Example: Tool that reads W3C Trace Context from request `_meta`. */
323+
function registerTool_traceContext(server: McpServer) {
324+
//#region registerTool_traceContext
325+
server.registerTool(
326+
'traced-operation',
327+
{
328+
description: 'Operation that participates in distributed tracing',
329+
inputSchema: z.object({ query: z.string() })
330+
},
331+
async ({ query }, ctx): Promise<CallToolResult> => {
332+
// e.g. '00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01'
333+
const traceparent = ctx.mcpReq._meta?.[TRACEPARENT_META_KEY];
334+
if (typeof traceparent === 'string') {
335+
// Continue the caller's trace, e.g. start a child span with your
336+
// OpenTelemetry tracer using this trace context.
337+
}
338+
339+
return { content: [{ type: 'text', text: `Results for ${query}` }] };
340+
}
341+
);
342+
//#endregion registerTool_traceContext
343+
}
344+
322345
// ---------------------------------------------------------------------------
323346
// Server-initiated requests
324347
// ---------------------------------------------------------------------------
@@ -543,6 +566,7 @@ void registerTool_errorHandling;
543566
void registerTool_annotations;
544567
void registerTool_logging;
545568
void registerTool_progress;
569+
void registerTool_traceContext;
546570
void registerTool_sampling;
547571
void registerTool_elicitation;
548572
void registerTool_roots;

packages/core/src/exports/public/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export * from '../../types/types.js';
7171

7272
// Constants
7373
export {
74+
BAGGAGE_META_KEY,
7475
CLIENT_CAPABILITIES_META_KEY,
7576
CLIENT_INFO_META_KEY,
7677
DEFAULT_NEGOTIATED_PROTOCOL_VERSION,
@@ -84,7 +85,9 @@ export {
8485
PARSE_ERROR,
8586
PROTOCOL_VERSION_META_KEY,
8687
RELATED_TASK_META_KEY,
87-
SUPPORTED_PROTOCOL_VERSIONS
88+
SUPPORTED_PROTOCOL_VERSIONS,
89+
TRACEPARENT_META_KEY,
90+
TRACESTATE_META_KEY
8891
} from '../../types/constants.js';
8992

9093
// Enums

packages/core/src/types/constants.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,44 @@ export const CLIENT_CAPABILITIES_META_KEY = 'io.modelcontextprotocol/clientCapab
3737
*/
3838
export const LOG_LEVEL_META_KEY = 'io.modelcontextprotocol/logLevel';
3939

40+
/*
41+
* Reserved `_meta` keys for distributed trace context propagation (SEP-414).
42+
*
43+
* These unprefixed keys are reserved by the MCP specification as an explicit
44+
* exception to the `_meta` key prefix rule. The SDK does not interpret them;
45+
* they pass through `_meta` untouched for OpenTelemetry-style propagation.
46+
*/
47+
48+
/**
49+
* `_meta` key carrying W3C Trace Context for distributed tracing (SEP-414).
50+
*
51+
* When present, the value MUST follow the W3C `traceparent` header format,
52+
* e.g. `00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01`.
53+
*
54+
* @see https://www.w3.org/TR/trace-context/#traceparent-header
55+
*/
56+
export const TRACEPARENT_META_KEY = 'traceparent';
57+
58+
/**
59+
* `_meta` key carrying vendor-specific trace state for distributed tracing (SEP-414).
60+
*
61+
* When present, the value MUST follow the W3C `tracestate` header format,
62+
* e.g. `vendor1=value1,vendor2=value2`.
63+
*
64+
* @see https://www.w3.org/TR/trace-context/#tracestate-header
65+
*/
66+
export const TRACESTATE_META_KEY = 'tracestate';
67+
68+
/**
69+
* `_meta` key carrying cross-cutting propagation values for distributed tracing (SEP-414).
70+
*
71+
* When present, the value MUST follow the W3C Baggage header format,
72+
* e.g. `userId=alice,serverRegion=us-east-1`.
73+
*
74+
* @see https://www.w3.org/TR/baggage/
75+
*/
76+
export const BAGGAGE_META_KEY = 'baggage';
77+
4078
/* JSON-RPC types */
4179
export const JSONRPC_VERSION = '2.0';
4280

0 commit comments

Comments
 (0)