Skip to main content
Constructs a TelemetryManager, wires up the OpenTelemetry Node SDK, then calls McpServerInstrumentation.instrument() to patch server.registerTool so every subsequent tool registration produces spans and metrics automatically.

Parameters

McpServer
required
An instance of McpServer from @modelcontextprotocol/sdk/server/mcp. Must be passed before any tools are registered (instrumentation only applies to tools registered after this call).
TelemetryConfig
required
Configuration for the telemetry pipeline. See the TelemetryConfig reference for every field.

Returns

ObservabilityInstance
An object with helpers for custom instrumentation and shutdown:
  • startActiveSpan(name, attributes, fn)
  • getHistogram(name, options)
  • getIncrementCounter(name, options)
  • processTelemetryAttributes(data)
  • shutdown(): Promise<void>
See ObservabilityInstance.

Example

Side effects

Calling instrumentServer does several things at once:
  1. Validates the config via ConfigValidator.validate(config). Throws on invalid input (missing endpoint, bad sampling rate, malformed auth).
  2. Stores the API key for the safety-check API. The token from exporterAuth.apiKey or exporterAuth.token is forwarded.
  3. Starts the OpenTelemetry Node SDK with trace + metric exporters, sampler, and resource detectors.
  4. Patches server.registerTool to wrap every handler in an instrumented function.

When to call it

Always immediately after constructing the McpServer, before any registerTool:
Tools registered before instrumentServer are not instrumented.

Errors

All thrown synchronously at the call site.

See also

TelemetryConfig

Every config field with defaults.

ObservabilityInstance

The object returned by this function.