@unhandled/instrumentation-mcp-server
v0.1.15
Published
Model Context Protocol server tracing for Unhandled
Readme
@unhandled/instrumentation-mcp-server
Traces the MCP server your app exposes, built on @modelcontextprotocol/sdk 1.x or @modelcontextprotocol/server 2.x. It is a standalone package: there is no agent integration entry, so install it explicitly.
pnpm add @unhandled/instrumentation-mcp-serverUsage
Initialize the Unhandled agent (or any OpenTelemetry SDK) first, then instrument the server before it connects.
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
import { instrumentMcpServer } from '@unhandled/instrumentation-mcp-server'
const server = new McpServer({ name: 'my-server', version: '1.0.0' })
instrumentMcpServer(server, { recordInputs: true })
server.registerTool('search', { inputSchema: { query: z.string() } }, handler)
await server.connect(new StdioServerTransport())Call instrumentMcpServer before connect(). Calling it afterwards emits a UNHANDLED_MCP_LATE_INSTRUMENTATION process warning; only messages received after the call are traced. A low-level Server is accepted as well as McpServer, from either SDK. On 2.x, createMcpHandler connects a new transport per request; instrument the server in the handler's factory and each request is traced. Instrumenting the same server twice is a no-op.
The server is observed at its transport, so every message is covered, including unknown methods and requests handled by the SDK itself.
Options
| Option | Default | Effect |
| --------------- | ------------- | -------------------------------------------------------------- |
| recordInputs | false | Record tool and prompt arguments as attributes |
| recordOutputs | false | Record tool results and prompt messages as attributes |
| serverName | auto-detected | Overrides the name from the McpServer / Server constructor |
| serverVersion | auto-detected | Overrides the version from the constructor |
Spans
| Event | Kind | Name |
| ------------------------- | -------- | --------------------------------------------------------------------------- |
| Incoming JSON-RPC request | SERVER | <method> <target>, or the bare method (tools/call search, initialize) |
| Incoming notification | SERVER | The method, for example notifications/initialized |
| Outgoing notification | CLIENT | The method, for example notifications/tools/list_changed |
| Transport error | INTERNAL | mcp.transport.error, with the exception recorded |
Targets: the tool name for tools/call, the prompt name for prompts/get, the URI for resources/read, resources/subscribe and resources/unsubscribe.
Handlers run inside the request span, so spans you create in a tool handler are its children. A notification sent from a handler is a child of that request span.
A JSON-RPC error response does not get its own span. The request span gets rpc.jsonrpc.error_code and rpc.jsonrpc.error_message and ERROR status. A tool that returns isError: true (which includes thrown handler errors and unknown tool names, because the SDK converts them) gets ERROR status too. Requests still pending when the transport closes end with ERROR.
Attributes
Always set:
| Attribute | Where |
| ----------------------------------------------------- | ------------------------------------------------------- |
| mcp.method.name | every span (the transport error span uses its own name) |
| mcp.server.name, mcp.server.version | every span |
| mcp.client.name, mcp.client.version | after initialize (the initialize span itself too) |
| mcp.tool.name | tools/call |
| mcp.tool.result.is_error | tools/call responses |
| rpc.jsonrpc.error_code, rpc.jsonrpc.error_message | requests answered with an error |
With recordInputs:
mcp.request.argument.<key>: JSON string of each argument oftools/callandprompts/get.
With recordOutputs:
mcp.tool.result.content_count,mcp.tool.result.<i>.content_type,mcp.tool.result.<i>.contentmcp.prompt.result.description,mcp.prompt.result.message_count,mcp.prompt.result.<i>.role,mcp.prompt.result.<i>.content
Text content is recorded as the raw text; any other content item is recorded as its JSON.
For AI agents
A coding agent can install this package, check that its data arrives and fix the errors it reports through the Unhandled MCP server at https://unhandled.net/mcp. In Claude Code, the Unhandled plugin adds that server plus skills for instrumenting a service, fixing a production error with a failing test first and verifying a deploy:
claude plugin marketplace add treboryx/unhandled
claude plugin install unhandled@unhandledCodex, Cursor, Gemini CLI and other agents connect to the same URL. Coding agents has the setup for each one and an AGENTS.md section to add to your repository.
