Skip to main content
Add Sentry tracing and error reporting to your existing Node.js mcp-use server. Each tool call gets a span with its tool name and duration. Returned tool errors and thrown exceptions also appear in Sentry Issues. This recipe uses MCP middleware, so it does not need access to mcp-use’s internal SDK instances. It monitors tool callbacks; it is not a replacement for instrumentation of the full HTTP transport or every MCP operation.

Try the live example

Select monitored_report, choose an outcome, and click Execute. Try success for a report, tool_error for a returned tool error, and exception for a thrown exception. No chat model or API key is needed to use the demo.
Open the example in a new tab.
This hosted demo sends traces and intentional errors to Manufact’s demo Sentry project. The tool result is visible here; run your own copy with your DSN to inspect the corresponding issues and traces in your Sentry account.

Prerequisites

The demo tool runs locally and does not call an LLM or another paid API. Sentry receives the telemetry when a DSN is configured; without one, the tool still works but no events are sent.

Add the integration

Install Sentry in your server project:
Add your project’s DSN to .env:
.env
Keep .env out of version control. Initialize Sentry once in your server entry point, then add the middleware below. If your application already initializes Sentry, reuse that initialization rather than calling Sentry.init() again. Copy this complete example into src/index.ts, or keep your existing tools and add the initialization and middleware:
src/index.ts
withIsolationScope keeps each call’s error tags separate from concurrent requests. startSpan ends the span when the callback finishes. Returning isError: true is a valid MCP result, so the middleware explicitly marks it as failed and reports a stable message. Exceptions are captured and rethrown to preserve normal MCP error handling.

Try it

Start or restart your server so it loads .env:
Open the Inspector URL printed by the CLI. Select Tools → monitored_report and try each input: In your Sentry project, look for the transaction/span named tools/call monitored_report in Traces, and the two error types in Issues. Error events have the mcp.tool.name tag. Delivery and indexing are asynchronous; leave the development server running briefly after making the calls.

Adapt it to your server

  • Replace monitored_report with your own tools. The middleware covers registered tool callbacks; discovery and failures rejected before callback dispatch are outside its scope.
  • The demo samples every trace with tracesSampleRate: 1.0. Choose a suitable sample rate for production traffic.
  • Tool arguments and returned content are not attached to telemetry. Captured exceptions still include their messages and stack traces; redact sensitive application errors using Sentry’s beforeSend configuration as needed.
  • defaultIntegrations: false intentionally disables automatic integrations for this focused example. Applications that already use Sentry should retain their existing setup and check for overlapping error capture. Automatic HTTP/database instrumentation has additional initialization requirements; this recipe only demonstrates explicit spans.
  • For a long-running Node server, let Sentry send in the background. For a short-lived script, await Sentry.flush(2000) before exit. Serverless deployments need their platform’s supported lifecycle integration so queued telemetry can finish; do not assume a returned HTTP response keeps the process alive.
See the complete Sentry example for the runnable project and tests, Sentry’s custom instrumentation guide and error capture documentation. This example uses the public middleware API; Sentry’s wrapMcpServerWithSentry targets underlying MCP SDK instances and should not be applied directly to the outer mcp-use server.