mcpobs

package
v1.141.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package mcpobs is what every MCP request carries for observation (#1889, #1893): the caller's W3C trace context read from params._meta or the HTTP headers, the protocol revision the session negotiated, the MCP semantic convention attribute keys, and the observer of every method other than tools/call. A tools/call has its own observers in pkg/middleware (MCPTracingMiddleware, MCPMetricsMiddleware), which read the same trace context through this package so the two never disagree about it.

Index

Constants

View Source
const (
	AttrMethodName      = "mcp.method.name"
	AttrGenAIToolName   = "gen_ai.tool.name"
	AttrGenAIOperation  = "gen_ai.operation.name"
	AttrSessionID       = "mcp.session.id"
	AttrProtocolVersion = "mcp.protocol.version"
	AttrErrorType       = "error.type"

	// GenAIOperationExecuteTool is gen_ai.operation.name on a tools/call span.
	GenAIOperationExecuteTool = "execute_tool"
)

The MCP semantic convention keys (GenAI conventions, status Development). error.type is the bounded error category of a failed request; a succeeded request does not carry it, as the convention requires.

View Source
const MethodToolsCall = "tools/call"

MethodToolsCall is the one MCP method this package's observer leaves to the tool-call observers.

Variables

This section is empty.

Functions

func Middleware

func Middleware(tracer *observability.Tracer, metrics *observability.Metrics) mcp.Middleware

Middleware observes every MCP method other than tools/call: a server span named by the method, as the MCP semantic conventions say ("tools/list", "resources/read"), carrying mcp.method.name, mcp.session.id and mcp.protocol.version, with error.type on a failure; and mcp_requests_total / mcp_request_duration_seconds by method and status. It is the OUTERMOST receiving middleware, so the span and the duration cover the list decorators and the typing of the result as well as the SDK's own handler. A tools/call passes straight through to its own observers. Nil-safe on both: with neither enabled the hop is a method compare per request.

func ProtocolVersion

func ProtocolVersion(req mcp.Request) string

ProtocolVersion is the MCP protocol revision the session negotiated, or empty before initialization or off a session the platform did not open.

func Status

func Status(err error) string

Status is the bounded status of a method's outcome: ok with no error; client_err for a JSON-RPC error the caller caused (an invalid request or params, a method that does not exist, a resource that does not exist, which the SDK reports as invalid params); internal_err otherwise. The resource-not-found code is the MCP one (-32002).

func TraceContext

func TraceContext(ctx context.Context, req mcp.Request) context.Context

TraceContext continues the caller's trace (#1893): the W3C traceparent and tracestate are read from params._meta, where the MCP semantic conventions carry them, and else from the HTTP request's headers (req.GetExtra().Header, nil on stdio and on an in-process session). With neither, or an invalid header, ctx is returned as it was and the span the caller opens is a root; with a sampled parent, ParentBased keeps the whole trace. _meta wins over the header: it is the one the caller wrote for this request, where a header may be the client library's own.

func WithTraceMeta

func WithTraceMeta(ctx context.Context, meta mcp.Meta) mcp.Meta

WithTraceMeta writes ctx's trace context into meta as the traceparent and tracestate entries TraceContext reads, so a request that crosses an in-process transport, where no header and no context travel with it, continues the caller's trace: a managed script's tool calls, each a child of the run's span (#1897). meta is returned, allocated when it was nil and ctx carries a trace, and unchanged when ctx carries none.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL