# @arizeai/openinference-instrumentation-mcp

**JavaScript · Instrumentation**

OpenInference instrumentation for MCP

- **Version**: 0.2.40
- **Instruments**: `@modelcontextprotocol/sdk >=1.0.0`

[npm](https://www.npmjs.com/package/@arizeai/openinference-instrumentation-mcp) · [README](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-instrumentation-mcp/README.md) · [Source](https://github.com/Arize-ai/openinference/tree/main/js/packages/openinference-instrumentation-mcp)

## Install

```sh
npm install @arizeai/openinference-instrumentation-mcp
```

## Usage

Register the instrumentation once, at startup, before the library is imported. The [`MCPInstrumentation`](https://openinf.dev/js/openinference-instrumentation-mcp.html.md#MCPInstrumentation) reference below lists its options.

```ts
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
import { MCPInstrumentation } from "@arizeai/openinference-instrumentation-mcp";

const provider = new NodeTracerProvider({
  // ...a span processor that exports to Phoenix or any OTLP backend.
});
provider.register();

registerInstrumentations({
  instrumentations: [new MCPInstrumentation()],
});
```

If the library is imported first (as ES modules often are), patch it directly with [`manuallyInstrument()`](https://openinf.dev/js/openinference-instrumentation-mcp.html.md#MCPInstrumentation.manuallyInstrument).

## `@arizeai/openinference-instrumentation-mcp`

### `isPatched`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-instrumentation-mcp/src/mcp.ts#L41)

```typescript
function isPatched(
  moduleName: "@modelcontextprotocol/sdk/client/sse" | "@modelcontextprotocol/sdk/server/sse" | "@modelcontextprotocol/sdk/client/stdio" | "@modelcontextprotocol/sdk/server/stdio" | "@modelcontextprotocol/sdk/client/streamableHttp" | "@modelcontextprotocol/sdk/server/streamableHttp",
): boolean
```

function to check if instrumentation is enabled / disabled for the module

### `MCPInstrumentation`

_class_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-instrumentation-mcp/src/mcp.ts#L50)

```typescript
class MCPInstrumentation extends InstrumentationBase<InstrumentationConfig>
```

Instrumentation for the MCP SDK which propagates context between client and server to allow traces to be connected across tool calls.

> **See also**
>
> `InstrumentationConfig`

#### `new MCPInstrumentation`

_constructor_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-instrumentation-mcp/src/mcp.ts#L51)

```typescript
new MCPInstrumentation(
  options?: { instrumentationConfig?: InstrumentationConfig },
): MCPInstrumentation
```

**Parameters**

| Name | Type | Description |
| - | - | - |
| `instrumentationConfig?` | `InstrumentationConfig` | The config for the instrumentation |

#### `manuallyInstrument`

_method_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-instrumentation-mcp/src/mcp.ts#L74)

```typescript
manuallyInstrument(
  modules: {
    clientSSEModule?: __module;
    serverSSEModule?: __module;
    clientStdioModule?: __module;
    serverStdioModule?: __module;
    clientStreamableHTTPModule?: __module;
    serverStreamableHTTPModule?: __module;
  },
): void
```

Manually instruments the MCP client and/or server modules. Currently, auto-instrumentation does not work with the MCP SDK and this method must be used to enable instrumentation.

**Parameters**

| Name | Type | Description |
| - | - | - |
| `modules` | `{ clientSSEModule?: __module; serverSSEModule?: __module; clientStdioModule?: __module; serverStdioModule?: __module; clientStreamableHTTPModule?: __module; serverStreamableHTTPModule?: __module }` | The modules to manually instrument. |
| `modules.clientSSEModule?` | `__module` | The MCP client SSE module, e.g. require(‘@modelcontextprotocol/sdk/client/sse.js’) |
| `modules.serverSSEModule?` | `__module` | The MCP server SSE module, e.g. require(‘@modelcontextprotocol/sdk/server/sse.js’) |
| `modules.clientStdioModule?` | `__module` | The MCP client stdio module, e.g. require(‘@modelcontextprotocol/sdk/server/stdio.js’) |
| `modules.serverStdioModule?` | `__module` | The MCP server stdio module, e.g. require(‘@modelcontextprotocol/sdk/client/stdio.js’) |
| `modules.clientStreamableHTTPModule?` | `__module` | The MCP client streamable HTTP module, e.g. require(‘@modelcontextprotocol/sdk/client/streamableHttp.js’) |
| `modules.serverStreamableHTTPModule?` | `__module` | The MCP server streamable HTTP module, e.g. require(‘@modelcontextprotocol/sdk/server/streamableHttp.js’) |
