[Server][Client] Bridge W3C trace context between HTTP headers and _meta (SEP-414) (#473)

RequestMeta::parseTraceContext() now falls back to the native traceparent/
tracestate/baggage HTTP headers when _meta carries none, and HeaderFactory
mirrors an application-set _meta trace context onto those same headers on
outbound requests. _meta wins when both are present.
This commit is contained in:
Christopher Hertel 2026-08-24 01:26:22 +02:00 • committed by GitHub
commit d59950f1ac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -81,6 +81,45 @@ final class HeaderFactoryTest extends TestCase
$this->assertSame(['MCP-Protocol-Version' => '2026-07-28'], $headers);
}
#[TestDox('mirrors trace context an application put in `_meta` onto its native headers')]
public function testTraceContextMirrorsOntoHeaders(): void
{
$payload = [
'method' => 'tools/list',
'params' => [
'_meta' => [
'traceparent' => '00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01',
'tracestate' => 'acme=1',
],
],
];
$headers = $this->headersFor($payload);
$this->assertSame('00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01', $headers['traceparent']);
$this->assertSame('acme=1', $headers['tracestate']);
}
#[TestDox('a response with no method still mirrors its trace context')]
public function testTraceContextMirrorsOntoAResponse(): void
{
$payload = ['id' => 1, 'result' => [], 'params' => ['_meta' => ['traceparent' => 'tp-1']]];
$this->assertSame(
['MCP-Protocol-Version' => '2026-07-28', 'traceparent' => 'tp-1'],
$this->headersFor($payload),
);
}
#[TestDox('an untraced request mirrors nothing')]
public function testNoTraceContextMirrorsNothing(): void
{
$headers = $this->headersFor(['method' => 'tools/list', 'params' => []]);
$this->assertArrayNotHasKey('traceparent', $headers);
$this->assertArrayNotHasKey('tracestate', $headers);
}
/**
* @param array<string, mixed> $payload
*