# openinference-instrumentation-mcp

**Python · Instrumentation**

OpenInference MCP Instrumentation

- **Module**: `openinference.instrumentation.mcp`
- **Version**: 2.1.6
- **Python**: >=3.10, <3.16
- **Instruments**: `mcp >= 1.24.0`

[PyPI](https://pypi.org/project/openinference-instrumentation-mcp/) · [README](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-mcp/README.md) · [Source](https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-mcp)

## Install

```sh
pip install openinference-instrumentation-mcp
```

## Usage

Instrument once, at startup, before the library is used. The [`MCPInstrumentor`](https://openinf.dev/python/openinference-instrumentation-mcp.html.md#openinference.instrumentation.mcp.MCPInstrumentor) reference below lists the options.

```python
from opentelemetry.sdk.trace import TracerProvider
from openinference.instrumentation.mcp import MCPInstrumentor

tracer_provider = TracerProvider()
# ...add a span processor that exports to Phoenix or any OTLP backend.

MCPInstrumentor().instrument(tracer_provider=tracer_provider)
```

## `openinference.instrumentation.mcp`

### `MCPInstrumentor`

_class_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-mcp/src/openinference/instrumentation/mcp/__init__.py#L99-L126)

```python
class MCPInstrumentor(BaseInstrumentor)
```

An instrumenter for MCP.

#### `instrument`

_method_

```python
def instrument(
    *,
    tracer_provider: Optional[TracerProvider] = None,
    skip_dep_check: bool = False,
) -> None
```

Start tracing `mcp`: patch it so its calls make OpenInference spans. Calling it again does nothing. Inherited from OpenTelemetry’s [`BaseInstrumentor`](https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/base/instrumentor.html).

**Parameters**

| Name | Description |
| - | - |
| `tracer_provider` | The tracer provider the spans are made with. Defaults to the global one, `opentelemetry.trace.get_tracer_provider()`. |
| `skip_dep_check` | Instrument even if the installed version of `mcp` is outside the supported range. |

#### `uninstrument`

_method_

```python
def uninstrument() -> None
```

Stop tracing: undo the patches `instrument()` made. Inherited from OpenTelemetry’s [`BaseInstrumentor`](https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/base/instrumentor.html).

#### `instrumentation_dependencies`

_method_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-mcp/src/openinference/instrumentation/mcp/__init__.py#L110-L111)

```python
def instrumentation_dependencies() -> Collection[str]
```

### `InstrumentedStreamReader`

_class_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-mcp/src/openinference/instrumentation/mcp/__init__.py#L129-L175)

```python
class InstrumentedStreamReader(ObjectProxy)
```

**Attributes**

| Name | Type and value |
| - | - |
| [`last_context`](https://openinf.dev/python/openinference-instrumentation-mcp.html.md#openinference.instrumentation.mcp.InstrumentedStreamReader.last_context) | `contextvars.Context \| None`Context snapshot used by the MCP 2.0 dispatcher for spawned handlers. |

### `InstrumentedStreamWriter`

_class_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-mcp/src/openinference/instrumentation/mcp/__init__.py#L178-L206)

```python
class InstrumentedStreamWriter(ObjectProxy)
```

#### `send`

_method_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-mcp/src/openinference/instrumentation/mcp/__init__.py#L186-L206)

```python
async def send(item: Any) -> Any
```

### `ItemWithContext`

_dataclass_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-mcp/src/openinference/instrumentation/mcp/__init__.py#L209-L212)

```python
class ItemWithContext(item: Any, ctx: context.Context)
```

**Attributes**

| Name | Type and value |
| - | - |
| [`item`](https://openinf.dev/python/openinference-instrumentation-mcp.html.md#openinference.instrumentation.mcp.ItemWithContext.item) | `Any` |
| [`ctx`](https://openinf.dev/python/openinference-instrumentation-mcp.html.md#openinference.instrumentation.mcp.ItemWithContext.ctx) | `context.Context` |

### `ContextSavingStreamWriter`

_class_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-mcp/src/openinference/instrumentation/mcp/__init__.py#L215-L225)

```python
class ContextSavingStreamWriter(ObjectProxy)
```

#### `send`

_method_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-mcp/src/openinference/instrumentation/mcp/__init__.py#L223-L225)

```python
async def send(item: Any) -> Any
```

### `ContextAttachingStreamReader`

_class_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-mcp/src/openinference/instrumentation/mcp/__init__.py#L228-L247)

```python
class ContextAttachingStreamReader(ObjectProxy)
```
