cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

📦 New Package: Automatic OpenTelemetry Instrumentation for MCP

theharithsa
Dynatrace Champion
Dynatrace Champion

I’ve just published opentelemetry-instrumentation-mcp, a (v1.0.0) package that makes it effortless to add observability to Model Context Protocol (MCP) applications.

Why it matters:
This package automatically instruments every MCP tool call with a span and exports those spans via OTLP to Dynatrace. No more boilerplate around tracer.startSpan – you get built‑in OpenTelemetry setup and error handling out of the box

🔑Key features

  • Automatic instrumentation of MCP tool calls – each invocation gets its own span

  • Built‑in OpenTelemetry NodeSDK – no manual SDK configuration needed

  • Zero‑code integration – just import and go

  • OTLP export to Dynatrace – the package reads OTEL_EXPORTER_OTLP_ENDPOINT and DYNATRACE_API_TOKEN environment variables and sends your traces to Dynatrace for you

  • Comprehensive tracing with error tracking – exceptions are captured and spans are marked with error status automatically

🚀Getting started

Install the package from npm:

npm install @theharithsa/opentelemetry-instrumentation-mcp

For most users, the auto‑registration approach is easiest: at the very top of your application’s entry point (e.g., src/index.ts), add a single import line:

import '@theharithsa/opentelemetry-instrumentation-mcp/register';

That’s it! The package will start the OpenTelemetry SDK, load the default Node instrumentations and the MCP instrumentation, and export spans to your Dynatrace tenant. Just ensure these environment variables are set

  • OTEL_EXPORTER_OTLP_ENDPOINT – your Dynatrace OTLP endpoint (e.g., https://{environmentid}.apps.dynatrace.com/api/v2/otlp/v1/traces)
  • DYNATRACE_API_TOKEN – a token with trace ingest permissions

 

theharithsa_0-1754163395288.png

For advanced use cases, you can create your own NodeSDK instance and pass new McpInstrumentation() alongside other instrumentations

🛠️ Custom attributes

Need to enrich spans with more context? Because the instrumentation runs your tool callback in the proper OpenTelemetry context, you can obtain the active span in your callback and set attributes like the model name, API name or custom metadata:

import { trace, context } from '@opentelemetry/api';

// inside your MCP tool callback…
const span = trace.getSpan(context.active());
span?.setAttribute('model.name', modelName);
span?.setAttribute('api.name', apiName);
// …rest of your logic

Alternatively, you can modify the instrumentation itself to set default attributes on every span.

đź”—Useful links

If you try out the package, let us know what you think! Pull requests and issues are welcome on GitHub.

Love more, hate less; Technology for all, together we grow.
0 REPLIES 0

Featured Posts