Python APImcp

Core

Instrumentation

Python · Instrumentation

openinference-instrumentation-mcp

OpenInference MCP Instrumentation

Module
openinference.instrumentation.mcp
Version
2.1.5
Python
>=3.10, <3.16
Instruments
mcp >= 1.24.0

Install

pip install openinference-instrumentation-mcp

Usage

Instrument once, at startup, before the library is used. The MCPInstrumentor reference below lists the options.

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

classSource ↗
class MCPInstrumentor(BaseInstrumentor)

An instrumenter for MCP.

instrument

method
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.

Parameters

NameDescription
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
def uninstrument() -> None

Stop tracing: undo the patches instrument() made. Inherited from OpenTelemetry’s BaseInstrumentor.

instrumentation_dependencies

methodSource ↗
def instrumentation_dependencies() -> Collection[str]

InstrumentedStreamReader

classSource ↗
class InstrumentedStreamReader(ObjectProxy)

Attributes

NameType and value
last_contextcontextvars.Context | None

Context snapshot used by the MCP 2.0 dispatcher for spawned handlers.

InstrumentedStreamWriter

classSource ↗
class InstrumentedStreamWriter(ObjectProxy)

send

methodSource ↗
async def send(item: Any) -> Any

ItemWithContext

dataclassSource ↗
class ItemWithContext(item: Any, ctx: context.Context)

Attributes

NameType and value
itemAny
ctxcontext.Context

ContextSavingStreamWriter

classSource ↗
class ContextSavingStreamWriter(ObjectProxy)

send

methodSource ↗
async def send(item: Any) -> Any

ContextAttachingStreamReader

classSource ↗
class ContextAttachingStreamReader(ObjectProxy)