Looking to connect Claude Code, Cursor, or another AI assistant to Monocle? See Monocle MCP. This page instruments the MCP server your application exposes.
For the full list of required span attributes and what powers the MCP dashboard, see the MCP feature page.
Installation
npm install @monocle.sh/instrumentation-mcpVersion 1.1 supports @modelcontextprotocol/sdk v1 and @modelcontextprotocol/server v2. Install the SDK your application uses. Both peer dependencies are optional.
With the AdonisJS agent
Install this package explicitly and use the direct import shown below. Initialize the agent before loading your MCP server module, then call instrumentMcpServer() before connecting the transport. No integrations entry is needed.
AdonisJS agent 2.x no longer bundles MCP or exports @monocle.sh/adonisjs-agent/mcp. Use
@monocle.sh/instrumentation-mcp instead. See the migration guide
when upgrading from 1.x.
Usage
Call instrumentMcpServer() before registering tools, resources, or prompts and before connecting a transport. It traces requests and notifications through that transport and captures handler exceptions.
SDK v2 with HTTP
Use the native createMcpHandler to serve protocol 2026-07-28 and legacy stateless clients. Changing imports alone does not enable the new protocol. Instrument each fresh server inside the factory.
import { createServer } from 'node:http'
import { z } from 'zod'
import { createMcpHandler, McpServer } from '@modelcontextprotocol/server'
import { toNodeHandler } from '@modelcontextprotocol/node'
import { instrumentMcpServer } from '@monocle.sh/instrumentation-mcp'
const handler = createMcpHandler(() => {
const server = new McpServer({ name: 'my-mcp-server', version: '1.0.0' })
instrumentMcpServer(server, {
serverName: 'my-mcp-server',
serverVersion: '1.0.0',
})
server.registerTool(
'search',
{ inputSchema: z.object({ query: z.string() }) },
async ({ query }) => ({
content: [{ type: 'text', text: `Search requested: ${query}` }],
}),
)
return server
})
createServer(toNodeHandler(handler)).listen(3000, '127.0.0.1')Install @modelcontextprotocol/node for the Node adapter. Authenticate requests before passing them to the handler. If your framework parses the body, pass it as the third argument to toNodeHandler(handler)(req, res, parsedBody).
The SDK does not create OpenTelemetry spans itself. It passes trace metadata through to handlers. Requests rejected before a server connects, including authentication failures, require HTTP instrumentation. The v2 subscriptions/listen stream bypasses connect and is not covered by this package.
SDK v1 with stdio
import { z } from 'zod'
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
import { instrumentMcpServer } from '@monocle.sh/instrumentation-mcp'
const server = new McpServer({
name: 'my-mcp-server',
version: '1.0.0',
})
instrumentMcpServer(server)
server.registerTool('search', { inputSchema: { query: z.string() } }, async ({ query }) => {
return { content: [{ type: 'text', text: `Search requested: ${query}` }] }
})
const transport = new StdioServerTransport()
await server.connect(transport)Configuration
instrumentMcpServer(server, {
// Capture tool/prompt arguments in span attributes (default: false)
recordInputs: true,
// Capture tool results and prompt messages in span attributes (default: false)
recordOutputs: true,
// Override auto-detected server name/version
serverName: 'my-server',
serverVersion: '1.0.0',
})Spans
The instrumentation creates spans for three categories of MCP activity.
Request spans
A SERVER span is created for every incoming JSON-RPC request (tools/call, resources/read, prompts/get, initialize, etc.).
The span name includes the target when available: tools/call search, resources/read config://app, prompts/get weather.
Notification spans
A SERVER span is created for incoming notifications (e.g., notifications/initialized, resources/changed).
Outgoing notification spans
A CLIENT span is created for outgoing notifications sent by the server (e.g., resources/changed).
Error spans
Unknown transport errors produce an INTERNAL span named mcp.transport.error with the exception recorded. Recognized client rejections, including unsupported protocol versions and invalid negotiation headers, produce a mcp.transport.rejected event without an exception or error status. The original error callback still runs.
Every JSON-RPC error completes its request span and sets rpc.jsonrpc.error_code and rpc.jsonrpc.error_message. Internal and server error codes mark the span as failed. Handler exceptions remain visible.
Client identity and trace propagation
Legacy connections read client identity and protocol version from initialize. Protocol 2026-07-28 carries them on each request in params._meta, under io.modelcontextprotocol/clientInfo and io.modelcontextprotocol/protocolVersion. A stateless legacy connection cannot retain identity between HTTP requests.
The configured OpenTelemetry propagator extracts traceparent, tracestate, and baggage from params._meta. With a W3C trace context propagator and an async context manager, the MCP span joins the caller's trace and handler spans inherit that context. The Monocle agent supplies this OpenTelemetry setup.
Use explicit serverName and serverVersion options for stable server identity. Automatic detection depends on private SDK fields.
SDK-specific attributes
In addition to the standard MCP attributes, this instrumentation emits these extra attributes when recording is enabled.
Input attributes (when recordInputs: true)
Tool arguments are captured as mcp.request.argument.{key} (JSON-stringified per key).
Output attributes (when recordOutputs: true)
| Attribute | Description |
|---|---|
mcp.tool.result.content_count | Number of content items returned |
mcp.tool.result.{i}.content_type | Content type of each item |
mcp.tool.result.{i}.content | Content value of each item |
mcp.prompt.result.description | Prompt description |
mcp.prompt.result.message_count | Number of messages returned |
mcp.prompt.result.{i}.role | Message role |
mcp.prompt.result.{i}.content | Message content |