---
title: "Observability"
description: "Watch every MCP request, tool, resource, and prompt call, and every authentication decision in Nuxt with Nitro hooks or observers, and send them to evlog, OpenTelemetry, Pino, or your own metrics."
canonical_url: "https://mcp-toolkit.nuxt.dev/advanced/observability"
---
# Observability

> Watch every MCP request, tool, resource, and prompt call, and every authentication decision in Nuxt with Nitro hooks or observers, and send them to evlog, OpenTelemetry, Pino, or your own metrics.

The toolkit reports what each server does at three points: the HTTP request, each definition call, and each authentication decision. You can listen through Nitro hooks in a server plugin, or pass observers to a server. Both see the same events on the default engine and on the [SDK engine](https://mcp-toolkit.nuxt.dev/advanced/engines).

An error that a hook listener or an observer throws is logged with `console.error`. It never reaches the client and never changes the response. A server without observers runs its handlers unwrapped, so unused observability costs nothing.

## Listen with Nitro hooks

Every discovered server calls these runtime hooks:

<table>
<thead>
  <tr>
    <th>
      Hook
    </th>
    
    <th>
      Called when
    </th>
    
    <th>
      Payload
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        mcp:request
      </code>
    </td>
    
    <td>
      An MCP request reaches a server
    </td>
    
    <td>
      <code>
        event
      </code>
      
      , <code>
        server
      </code>
      
      , <code>
        method
      </code>
      
      , <code>
        startedAt
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        mcp:request:end
      </code>
    </td>
    
    <td>
      The response is returned, or its event stream closes
    </td>
    
    <td>
      <code>
        { request, end }
      </code>
      
      , with <code>
        status
      </code>
      
      , <code>
        durationMs
      </code>
      
      , and <code>
        rejected
      </code>
      
       on <code>
        end
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        mcp:operation
      </code>
    </td>
    
    <td>
      A tool, resource, prompt, or completion handler is about to run
    </td>
    
    <td>
      The operation (see below)
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        mcp:operation:end
      </code>
    </td>
    
    <td>
      That handler finished
    </td>
    
    <td>
      <code>
        { operation, end }
      </code>
      
      , with <code>
        outcome
      </code>
      
      , <code>
        durationMs
      </code>
      
      , <code>
        result
      </code>
      
      , <code>
        error
      </code>
      
      , and <code>
        code
      </code>
      
       on <code>
        end
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        mcp:auth
      </code>
    </td>
    
    <td>
      Authentication, authorization, or a scope check decides
    </td>
    
    <td>
      <code>
        stage
      </code>
      
      , <code>
        outcome
      </code>
      
      , <code>
        scheme
      </code>
      
      , <code>
        subject
      </code>
      
      , <code>
        required
      </code>
      
      , <code>
        missing
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        mcp:tool:called
      </code>
    </td>
    
    <td>
      A tool call settles into a result
    </td>
    
    <td>
      <code>
        name
      </code>
      
      , <code>
        result
      </code>
      
      , <code>
        durationMs
      </code>
      
      , <code>
        event
      </code>
    </td>
  </tr>
</tbody>
</table>

Register a server plugin to forward them to your metrics or logs:

```ts [server/plugins/mcp-metrics.ts]
import { defineNitroPlugin } from 'nitropack/runtime'

export default defineNitroPlugin((nitroApp) => {
  nitroApp.hooks.hook('mcp:operation:end', ({ operation, end }) => {
    console.info({
      method: operation.method,
      name: operation.name,
      outcome: end.outcome,
      durationMs: Math.round(end.durationMs),
    })
  })
})
```

On Nitro 3, import `definePlugin` from `nitro` instead of `defineNitroPlugin` from `nitropack/runtime`.

`mcp:request` reports `method` only for 2026-07-28 requests, which send it in the `Mcp-Method` header. `mcp:tool:called` fires only for tool calls that settle into a result. `mcp:auth` reports authentication failures (`401`), `authorize` denials (`403`), and missing scopes (`-32003`). The engine rejects invalid arguments before any handler runs, so no operation hook sees them; `mcp:request:end` still reports the request.

## Read an operation's outcome

Each operation ends once, with one of these outcomes:

<table>
<thead>
  <tr>
    <th>
      Outcome
    </th>
    
    <th>
      Meaning
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        success
      </code>
    </td>
    
    <td>
      The handler returned a result.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        tool_error
      </code>
    </td>
    
    <td>
      A tool returned an <code>
        isError
      </code>
      
       result: a failure it handled.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        error
      </code>
    </td>
    
    <td>
      The handler threw. <code>
        end.error
      </code>
      
       holds the original value, even though the client receives an <code>
        isError
      </code>
      
       result.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        refused
      </code>
    </td>
    
    <td>
      The access token lacked a <a href="https://mcp-toolkit.nuxt.dev/authentication/scopes">
        scope
      </a>
      
       the definition declares.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        cancelled
      </code>
    </td>
    
    <td>
      The client went away or cancelled the request while the handler ran.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        input_required
      </code>
    </td>
    
    <td>
      The tool asked the client for more <a href="https://mcp-toolkit.nuxt.dev/advanced/elicitation">
        input
      </a>
      
      .
    </td>
  </tr>
</tbody>
</table>

The operation also carries `kind`, `method`, `name`, `uri`, `group`, `tags`, `annotations`, `arguments`, `requestId`, `sessionId`, `protocolVersion`, `era`, `client`, `trace`, `signal`, and `event`.

`arguments` are the validated arguments, as the client sent them. They're user data, so redact them before you write them anywhere. No payload carries the bearer token or API key, but `event` holds the raw request headers. Don't log `event` as a whole.

## Compose an observer

An observer is a plain object with optional `request`, `operation`, and `auth` hooks. A start hook can return a scope. Its `end` runs once when the work finishes. Its `run` wraps the work itself, which tracing uses to make a span active around the handler.

Add observers to a server in its [`index.ts`](https://mcp-toolkit.nuxt.dev/servers/server-file):

```ts [server/mcp/index.ts]
import { defineMcpObserver, defineMcpServer } from '@nuxtjs/mcp-toolkit/server'

const metrics = defineMcpObserver({
  name: 'metrics',
  operation: operation => ({
    end: ({ outcome, durationMs }) => {
      console.info({ name: operation.name, outcome, durationMs })
    },
  }),
  auth: (decision) => {
    if (decision.outcome === 'denied') console.warn({ stage: decision.stage, missing: decision.missing })
  },
})

export default defineMcpServer({ observers: [metrics] })
```

Observers belong to the server whose directory holds the index file. A server [composed](https://mcp-toolkit.nuxt.dev/servers/composition) with `createMcpHandler` doesn't call the Nitro hooks, so pass it observers instead:

```ts [server/routes/partner-mcp.ts]
import { createMcpHandler } from '@nuxtjs/mcp-toolkit/server'
import search from '../shared-mcp/search'
import metrics from '../shared-mcp/metrics'

export default createMcpHandler({ name: 'partner', tools: [search], observers: [metrics] })
```

### Send events to Pino

The same observer shape works for any logger or metrics client. With [Pino](https://getpino.io):

```ts [server/mcp/index.ts]
import pino from 'pino'
import { defineMcpObserver, defineMcpServer } from '@nuxtjs/mcp-toolkit/server'

const logger = pino()

export default defineMcpServer({
  observers: [defineMcpObserver({
    name: 'pino',
    operation: operation => ({
      end: ({ outcome, durationMs, error }) => {
        const fields = { mcp: { method: operation.method, name: operation.name, client: operation.client?.name }, outcome, durationMs }
        if (outcome === 'error') logger.error({ ...fields, err: error }, `${operation.method} ${operation.name}`)
        else logger.info(fields, `${operation.method} ${operation.name}`)
      },
    }),
  })],
})
```

## Log each request as one wide event with evlog

[evlog](https://evlog.dev) writes one structured event per HTTP request. With its module installed, each MCP request's event also carries the MCP method, session, protocol version, client, outcome, and duration. Fields that a tool sets with `useLogger(event)` land on the same event.

Installing evlog's module is enough:

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['evlog/nuxt', '@nuxtjs/mcp-toolkit'],
})
```

Set `mcp: { evlog: false }` to keep MCP fields off the event. A tool adds its own fields to the same event:

```ts [server/mcp/tools/refund.ts]
import { createError, useLogger } from 'evlog'
import { z } from 'zod'
import { defineMcpTool } from '@nuxtjs/mcp-toolkit/server'

export default defineMcpTool({
  inputSchema: z.object({ orderId: z.string() }),
  handler: ({ orderId }, event) => {
    useLogger(event).set({ order: { id: orderId } })
    throw createError({ message: 'Refund window closed', status: 409, why: 'Order older than 30 days' })
  },
})
```

The event for that call looks like this (shortened):

```json
{
  "level": "error",
  "path": "/mcp",
  "order": { "id": "o_42" },
  "mcp": {
    "method": { "name": "tools/call" },
    "protocol": { "version": "2026-07-28" },
    "client": { "name": "claude-code", "version": "2.1.0" },
    "operation": { "kind": "tool", "outcome": "error", "duration_ms": 37 }
  },
  "gen_ai": { "operation": { "name": "execute_tool" }, "tool": { "name": "refund" } },
  "jsonrpc": { "request": { "id": "7" } },
  "error": { "message": "Refund window closed", "why": "Order older than 30 days", "type": "EvlogError" }
}
```

A thrown error raises the event to `error` and keeps its `why`, `fix`, and `link`. A tool that returns `isError` sets `outcome: 'tool_error'` and keeps the level. A missing scope logs a warning. The field names follow the OpenTelemetry MCP semantic conventions, so evlog's OTLP drain exports them under the standard attribute names. The integration requires evlog 2.38 or later.

### Capture arguments and results

Arguments and results are user data, so evlog doesn't record them unless you ask:

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['evlog/nuxt', '@nuxtjs/mcp-toolkit'],
  mcp: { evlog: { arguments: { maxLength: 500 }, result: true } },
})
```

Captured values go through your evlog `redact` rules first, then `maxLength`. They're stored as JSON strings in `gen_ai.tool.call.arguments`, `gen_ai.prompt.variables`, and `gen_ai.tool.call.result`.

### Name and sample MCP events

An MCP request is an ordinary HTTP request to evlog. Your evlog `env`, `sampling`, `redact`, drains, and plugins apply to it as they do to any other route.

To give each server its own service name, map its route in `evlog.routes`. The first matching pattern wins, so list a nested server before its parent. In evlog 2.38, `/mcp/**` doesn't match `/mcp` itself, so list the exact route too:

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['evlog/nuxt', '@nuxtjs/mcp-toolkit'],
  mcp: { servers: { default: {}, admin: {} } },
  evlog: {
    env: { service: 'shop' },
    routes: {
      '/mcp/admin': { service: 'shop-admin-mcp' },
      '/mcp/admin/**': { service: 'shop-admin-mcp' },
      '/mcp': { service: 'shop-mcp' },
      '/mcp/**': { service: 'shop-mcp' },
    },
    sampling: { rates: { info: 10 }, keep: [{ status: 400 }] },
  },
})
```

With this configuration, evlog keeps 10% of successful MCP calls and every refused or failed request. To keep calls by their MCP fields, decide in the `evlog:emit:keep` hook:

```ts [server/plugins/evlog-keep.ts]
export default defineNitroPlugin((nitroApp) => {
  nitroApp.hooks.hook('evlog:emit:keep', (ctx) => {
    const mcp = ctx.context.mcp as { operation?: { outcome?: string, tags?: string[] } } | undefined
    if (mcp?.operation?.tags?.includes('destructive')) ctx.shouldKeep = true
  })
})
```

A thrown error raises the event to `error`, so error sampling rates apply to it. A tool that returns `isError` keeps the `info` level. To keep those too, match `mcp.operation.outcome === 'tool_error'` in the same hook.

### Use evlog in a composed server

`evlog()` from `@nuxtjs/mcp-toolkit/evlog` is the same observer, for a server composed with `createMcpHandler`:

```ts [server/routes/partner-mcp.ts]
import { createMcpHandler } from '@nuxtjs/mcp-toolkit/server'
import { evlog } from '@nuxtjs/mcp-toolkit/evlog'
import search from '../shared-mcp/search'

export default createMcpHandler({ name: 'partner', tools: [search], observers: [evlog()] })
```

When no evlog module created a request logger, as in a standalone Nitro, Deno, Bun, or Workers server, the observer creates one and emits it when the response ends. In that mode the event uses the `env`, `sampling`, and drain you set with `initLogger()`. `evlog()` also takes evlog's middleware options, such as `drain`, `redact`, and `waitUntil`. Pass `routes`, `include`, `exclude`, and `keep` there as well, because evlog reads them per middleware rather than from `initLogger()`.

## Trace calls with OpenTelemetry

`otel()` records each tool, resource, prompt, and completion call as a server span. It requires `@opentelemetry/api` and a tracer provider that your app registers:

```ts [server/mcp/index.ts]
import { defineMcpServer } from '@nuxtjs/mcp-toolkit/server'
import { otel } from '@nuxtjs/mcp-toolkit/otel'

export default defineMcpServer({ observers: [otel()] })
```

Spans are named after the MCP semantic conventions, such as `tools/call refund`. They carry `mcp.method.name`, `gen_ai.tool.name`, `jsonrpc.request.id`, and `mcp.protocol.version`. When the client sends a `traceparent` in `_meta`, the span continues the client's trace. Spans that your handler creates, such as a database query, nest under the MCP span.

A thrown error and a returned `isError` both mark the span as failed. `error.type` tells them apart: it holds the error's name, or `tool_error`. Pass `captureArguments` or `captureResult` to record those values. Each takes `true`, or a function that redacts the value first.

Sentry and Datadog read OpenTelemetry spans, so `otel()` works with their SDKs.

## Correlate with the client's trace

`event.context.mcp.trace` holds the W3C trace context (`traceparent`, `tracestate`, `baggage`) that the client sent in `_meta`. Use it to correlate a call with the caller's trace when you don't use `otel()`. Treat these fields as untrusted input.

## Replace removed hooks

<table>
<thead>
  <tr>
    <th>
      Removed
    </th>
    
    <th>
      Replacement
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        onToolCall
      </code>
    </td>
    
    <td>
      <code>
        observers
      </code>
      
      , or the <code>
        mcp:operation:end
      </code>
      
       hook
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        mcp:server:created
      </code>
    </td>
    
    <td>
      <a href="https://mcp-toolkit.nuxt.dev/servers/server-file">
        Server configuration
      </a>
      
       and <a href="https://mcp-toolkit.nuxt.dev/advanced/extension-plugins">
        extension plugins
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        mcp:config:resolved
      </code>
    </td>
    
    <td>
      <a href="https://mcp-toolkit.nuxt.dev/servers/server-file">
        Server configuration
      </a>
      
       and current-request authorization
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        mcp:definitions:paths
      </code>
    </td>
    
    <td>
      <a href="https://mcp-toolkit.nuxt.dev/servers/directories-layers">
        Directories and layers
      </a>
    </td>
  </tr>
</tbody>
</table>

The 1.0 runtime has no SDK server instance to mutate, so the server-creation hooks have no replacement hook.


## Sitemap

See the full [sitemap](https://mcp-toolkit.nuxt.dev/sitemap.md) for all pages.
