> ## Documentation Index
> Fetch the complete documentation index at: https://futureagi.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Model Context Protocol (MCP)

## 1. Installation

First install the traceAI package to access the observability framework

<CodeGroup>
  ```bash Python theme={null}
  pip install traceAI-mcp
  ```

  ```bash JS/TS theme={null}
  npm install @traceai/mcp @traceai/fi-core @opentelemetry/instrumentation @modelcontextprotocol/sdk
  ```
</CodeGroup>

<Note>
  You also need to install the orchestration package that will utilize the MCP server.

  For example, if you are using the OpenAI MCP server, you need to install the `traceAI-openai-agents` package.

  ```bash theme={null}
  pip install traceAI-openai-agents
  ```
</Note>

***

## 2. Set Environment Variables

Set up your environment variables to authenticate with both FutureAGI and OpenAI.

<CodeGroup>
  ```python Python theme={null}
  import os

  os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
  os.environ["FI_API_KEY"] = "your-futureagi-api-key"
  os.environ["FI_SECRET_KEY"] = "your-futureagi-secret-key"
  ```

  ```typescript JS/TS theme={null}
  process.env.FI_API_KEY = "your-futureagi-api-key";
  process.env.FI_SECRET_KEY = "your-futureagi-secret-key";
  // If your MCP client/server uses OpenAI tools, also set:
  // process.env.OPENAI_API_KEY = "your-openai-api-key";
  ```
</CodeGroup>

***

## 3. Initialize Trace Provider

Set up the trace provider to create a new project in FutureAGI, establish telemetry data pipelines .

<CodeGroup>
  ```python Python theme={null}
  from fi_instrumentation import register
  from fi_instrumentation.fi_types import ProjectType

  trace_provider = register(
      project_type=ProjectType.EXPERIMENT,
      project_name="openai_project",
  )
  ```

  ```typescript JS/TS theme={null}
  import { register, ProjectType } from "@traceai/fi-core";

  const tracerProvider = register({
    project_type: ProjectType.EXPERIMENT,
    project_name: "mcp_project",
  });
  ```
</CodeGroup>

***

## 4. Instrument your Project

Instrument your Project with OpenAI Agents Instrumentor. This step ensures that all interactions with the OpenAI are tracked and monitored.

<CodeGroup>
  ```python Python theme={null}
  from traceai_openai_agents import OpenAIAgentsInstrumentor
  from traceai_mcp import MCPInstrumentor


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

  ```typescript JS/TS theme={null}
  import { MCPInstrumentation } from "@traceai/mcp";
  import * as MCPClientStdioModule from "@modelcontextprotocol/sdk/client/stdio";
  import * as MCPServerStdioModule from "@modelcontextprotocol/sdk/server/stdio";

  // MCP must be manually instrumented as it doesn't have a traditional module structure
  const mcpInstrumentation = new MCPInstrumentation({});
  mcpInstrumentation.manuallyInstrument({
    clientStdioModule: MCPClientStdioModule,
    serverStdioModule: MCPServerStdioModule,
  });
  ```
</CodeGroup>

***

## 5. Interact with MCP Server

Interact with the MCP Server as you normally would. Our Instrumentor will automatically trace and send the telemetry data to our platform.

```python theme={null}

import asyncio
import os
import shutil

from agents import Agent, Runner
from agents.mcp import MCPServer, MCPServerStdio

from traceai_openai_agents import OpenAIAgentsInstrumentor
from traceai_mcp import MCPInstrumentor

from fi_instrumentation import register
from fi_instrumentation.fi_types import ProjectType

trace_provider = register(
    project_type=ProjectType.EXPERIMENT,
    project_name="mcp_project",
)



OpenAIAgentsInstrumentor().instrument(tracer_provider=trace_provider)
MCPInstrumentor().instrument(tracer_provider=trace_provider)

async def run(mcp_server: MCPServer):
    agent = Agent(
        name="Assistant",
        instructions="Use the tools to read the filesystem and answer questions based on those files.",
        mcp_servers=[mcp_server],
    )

    message = "Read the files and list them."
    print(f"Running: {message}")
    result = await Runner.run(starting_agent=agent, input=message)
    print(result.final_output)


async def main():
    current_dir = os.path.dirname(os.path.abspath(__file__))
    samples_dir = os.path.join(current_dir, "sample_files")

    async with MCPServerStdio(
        name="Filesystem Server, via npx",
        params={
            "command": "npx",
            "args": ["-y", "@modelcontextprotocol/server-filesystem", samples_dir],
        },
    ) as server:
        await run(server)


if __name__ == "__main__":
    if not shutil.which("npx"):
        raise RuntimeError("npx is not installed. Please install it with `npm install -g npx`.")

    asyncio.run(main())
```
