‎02 Aug 2025 08:41 PM - edited ‎02 Aug 2025 08:42 PM
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
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
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
For advanced use cases, you can create your own NodeSDK instance and pass new McpInstrumentation() alongside other instrumentations
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.
📦npm package: @theharithsa/opentelemetry-instrumentation-mcp
đź’»GitHub repo: github.com/theharithsa/opentelemetry-instrumentation-mcp
If you try out the package, let us know what you think! Pull requests and issues are welcome on GitHub.