Documentation
¶
Overview ¶
Package mcpotel provides OpenTelemetry instrumentation middleware for MCP servers built with the official go-sdk.
It follows the OpenTelemetry semantic conventions for MCP: https://opentelemetry.io/docs/specs/semconv/gen-ai/mcp/
Index ¶
Constants ¶
const ( // AttrMCPMethodName is the MCP method being called (e.g., "tools/call"). AttrMCPMethodName = attribute.Key("mcp.method.name") // AttrMCPSessionID identifies the MCP session. AttrMCPSessionID = attribute.Key("mcp.session.id") // AttrMCPResourceURI is the URI of the resource being read. AttrMCPResourceURI = attribute.Key("mcp.resource.uri") // AttrGenAIToolName is the name of the tool being called. AttrGenAIToolName = attribute.Key("gen_ai.tool.name") // AttrGenAIPromptName is the name of the prompt being retrieved. AttrGenAIPromptName = attribute.Key("gen_ai.prompt.name") // AttrErrorType classifies the error. AttrErrorType = attribute.Key("error.type") )
Attribute keys following OTel MCP semantic conventions.
Variables ¶
This section is empty.
Functions ¶
func ErrorMessageFull ¶
ErrorMessageFull records the complete error message. Use this only when you are confident your error messages never contain PII.
func Middleware ¶
func Middleware(cfg Config) mcp.Middleware
Middleware returns an MCP middleware that instruments every incoming method call with OpenTelemetry spans and metrics.
Usage:
server := mcp.NewServer(impl, opts)
server.AddReceivingMiddleware(mcpotel.Middleware(mcpotel.Config{
ServiceName: "my-mcp-server",
}))
func URISchemeOnly ¶
URISchemeOnly records only the URI scheme (e.g., "file://", "miro://"). Use this when resource URIs may contain user-identifiable paths.
Types ¶
type Config ¶
type Config struct {
// ServiceName is used as the OTel service.name resource attribute.
// Required.
ServiceName string
// ServiceVersion is used as the service.version resource attribute.
// Optional.
ServiceVersion string
// TracerProvider supplies the tracer. Defaults to otel.GetTracerProvider().
TracerProvider trace.TracerProvider
// MeterProvider supplies the meter. Defaults to otel.GetMeterProvider().
MeterProvider metric.MeterProvider
// Filter returns false for methods that should not be instrumented.
// When nil, all methods are instrumented.
Filter func(method string) bool
// RedactError controls how error messages are recorded in spans and metrics.
// Error messages from tool handlers may contain PII (e.g., user emails,
// file paths). This function lets you sanitize or classify them.
//
// When nil, defaults to recording the Go error type name only (e.g.,
// "*json.SyntaxError"), not the full message. Set to ErrorMessageFull
// to record complete error messages if your errors are known to be PII-free.
RedactError func(err error) string
// RedactURI controls how resource URIs are recorded in spans and metrics.
// URIs may contain user-identifiable paths or query parameters.
//
// When nil, defaults to recording the full URI. Set to URISchemeOnly
// to record only the scheme (e.g., "file://", "user://").
RedactURI func(uri string) string
}
Config controls the behavior of the OpenTelemetry middleware.