Module: Ask::MCP::TraceContext

Defined in:
lib/ask/mcp/trace_context.rb

Overview

OpenTelemetry trace context propagation via MCP _meta (2026-07-28, SEP-414). The convention reserves three _meta keys — traceparent, tracestate, baggage — so traces can span the client/server boundary.

Constant Summary collapse

KEYS =
%w[traceparent tracestate baggage].freeze

Class Method Summary collapse

Class Method Details

.from_headers(headers) ⇒ Object

Extract trace context from arbitrary HTTP-style headers, matching names case-insensitively and accepting Rack's HTTP_* env convention.

TraceContext.from_headers(rack_env)
# => { "traceparent" => "00-...", "tracestate" => "..." }


18
19
20
21
22
23
24
25
26
# File 'lib/ask/mcp/trace_context.rb', line 18

def from_headers(headers)
  headers.each_with_object({}) do |(name, value), out|
    key = name.to_s.downcase
    key = key.delete_prefix("http_") if key.start_with?("http_")
    next unless KEYS.include?(key)

    out[key] = value.to_s
  end
end

.from_meta(meta) ⇒ Object

Extract trace context from an MCP request's _meta hash (keys may be symbols or strings, as the JSON parser symbolizes keys).



30
31
32
33
34
35
36
# File 'lib/ask/mcp/trace_context.rb', line 30

def from_meta(meta)
  meta = meta || {}
  KEYS.each_with_object({}) do |key, out|
    value = meta[key] || meta[key.to_sym]
    out[key] = value.to_s if value
  end
end