Skip to main content

Getting Started with MCP using OpenAI Agents

A practical walkthrough for building OpenAI Agents that use the Model Context Protocol (MCP) to access tools, files, and trace data via Weave.
Created on March 27|Last edited on March 27
Want to make OpenAI Agents smarter by connecting them to external tools like file systems or analytics platforms?
This guide shows how to use the Model Context Protocol (MCP) to hook agents into structured tools - starting with files and ending with a powerful Weights & Biases integration.
💡
OpenAI’s Agents SDK provides a powerful framework for building AI assistants that can use tools, access context, and reason over real-world data. But for agents to do anything useful, they need structured access to external systems—APIs, files, services, and other tools. That’s where the Model Context Protocol (MCP) comes in.
MCP is an open standard that defines how LLM-based applications like agents can connect to external resources. It gives agents a clean, consistent interface for interacting with tools, documents, and predefined prompts, without relying on custom integrations for each use case.
In this guide, we’ll walk through how to use MCP inside an OpenAI Agent, starting with a simple example: letting the agent explore and reason about a folder of files using the official filesystem server. This will demonstrate how agents can interact with the outside world in a dynamic and extensible way using MCP.


Table of contents



What is the Model Context Protocol (MCP)?

The Model Context Protocol (MCP) is an open standard that gives LLM applications a unified, secure interface for accessing external tools, data, and prompt templates. It streamlines integration by standardizing resource access and enables consistent deployment across AI platforms.
LLM applications like chatbots rely on external resources to deliver accurate, dynamic responses. These resources fall into three key categories:
  • Tools: Enable the model to execute actions like querying a database or sending an email.
  • Resources: Provide structured data such as documents, logs, or API responses that the model can reference.
  • Prompts: Are predefined templates that guide interactions, ensuring consistency and efficiency.
Without a standardized framework, each LLM-powered application must build its own method to connect to these resources, leading to unnecessary complexity and redundant development. MCP solves this by offering a common protocol, so developers can build once and deploy across multiple AI platforms with ease.

Does OpenAI support MCP?

Yes, OpenAI’s Agents SDK supports MCP out-of-the-box. This built-in feature allows any agent created with the SDK to connect to one or more MCP servers and seamlessly access external tools, data, and prompt templates without custom integration. It is designed for efficiency.
When constructing an Agent, you can register an MCP server using the mcp_servers argument. The SDK automatically manages discovery, tool execution, and response routing. It supports both local (MCPServerStdio) and remote (MCPServerSse) servers, making it simple to integrate everything from local scripts to cloud APIs.
This integration opens up an ecosystem of MCP-compatible servers—like file browsers, SQL interfaces, and web tools—that can be reused across agents with zero custom code. Moreover, the tools you build for one agent can work in any environment that follows the MCP specification.

Tutorial: Build an intelligent file exploration agent with MCP and Weights & Biases

Now that we’ve covered what MCP is and how it fits into the OpenAI Agents SDK, let’s work through a simple example.
In this tutorial, we’ll build an agent that can explore a folder of files using MCP. The agent will use the official server-filesystem MCP server to list, read, and reason about the contents of text files—no custom integration required.
To start, we will install a few packages, clone a few example files, and run a script that connects the agent to the filesystem server via MCP. This will give you a working end-to-end setup where the agent dynamically discovers available tools and uses them to answer questions based on real data.
python -m venv env
source env/bin/activate
pip install openai-agents weave
curl -Ls https://astral.sh/uv/install.sh | sh
Next, clone the example files with the following command:
git clone --depth=1 https://github.com/openai/openai-agents-python.git temp_agents && cp -R temp_agents/examples/mcp/filesystem_example/sample_files ./ && cp temp_agents/examples/mcp/filesystem_example/main.py ./ && rm -rf temp_agents
You should now have:
sample_files/
main.py
If you’d like to add Weave logging to your OpenAI Agent, replace your main.py with the code below. This file launches an MCP server via npx, giving the agent access to your files and enabling MCP tool calls to list, read, and analyze them.
Run the agent with:
import asyncio
import os
import shutil

from agents import Agent, Runner, set_trace_processors
from agents.mcp import MCPServer, MCPServerStdio
from weave.integrations.openai_agents.openai_agents import WeaveTracingProcessor
import weave

# Initialize Weave project
weave.init("mcp_stuff")
set_trace_processors([WeaveTracingProcessor()]) # Enable Weave tracing

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)

message = "What is my #1 favorite book?"
print(f"\n\nRunning: {message}")
result = await Runner.run(starting_agent=agent, input=message)
print(result.final_output)

message = "Look at my favorite songs. Suggest one new song that I might like."
print(f"\n\nRunning: {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())
The main.py file launches a subprocess MCP server using npx @modelcontextprotocol/server-filesystem, giving the agent access to the sample_files directory. The agent uses MCP tool calls to list, read, and analyze files.
You can run it with:
uv run python main.py
After running the script, your agent will navigate the files in the sample_files directory, and answer the query we asked it, which was “what is my favorite book?”


Integrating the W&B MCP Server with OpenAI Agents

Now that we have a simple example under our belt, we will move on to a more sophisticated use case using the W&B Weave MCP server. Weave is a platform for inspecting and analyzing the behavior of LLM agents by logging detailed trace data. This example shows how to create a chatbot powered by OpenAI Agents, and use MCP to connect to a Weave server for querying and visualizing trace data.
To start, clone the W&B Weave MCP server code:
git clone https://github.com/wandb/mcp-server.git
Locate the path to the server script within the repository. Then, create a new script that acts as a chatbot connected to Weave. The script uses OpenAI Agents with the MCP protocol over stdio and leverages Weave’s tracing capabilities to log and query agent interactions. This setup lets you ask queries like “generate a report of recent LLM activity in the byyoung3/qat project” and view detailed outputs.
import asyncio
import os
import shutil

from agents import Agent, Runner, set_trace_processors
from agents.mcp import MCPServerStdio
from weave.integrations.openai_agents.openai_agents import WeaveTracingProcessor
import weave

weave.init("oai_agent")
set_trace_processors([WeaveTracingProcessor()]) # Enables automatic Weave tracing

SERVER_DIR = "/Users/brettyoung/Desktop/dev_24/tutorials/oai_mcp/mcp-server/src/mcp_server" # put your full path to the weave server here
UV_PATH = shutil.which("uv")

async def setup_agent():
mcp_server = MCPServerStdio(
name="Weave MCP Server (via uv)",
params={
"command": UV_PATH,
"args": ["--directory", SERVER_DIR, "run", "server.py"],
},
)
await mcp_server.__aenter__()
agent = Agent(
name="Assistant",
instructions="Use the tools to query Weave or analyze traces to answer questions.",
mcp_servers=[mcp_server],
)
return agent, mcp_server

# @weave.op
async def chat_loop(agent):
print("\nChat with the Weave Agent (type 'exit' to quit):\n")
while True:
msg = input("You: ").strip()
if msg.lower() in ["exit", "quit"]:
break
result = await Runner.run(starting_agent=agent, input=msg)
print(f"Assistant: {result.final_output}\n")

async def main():
if not UV_PATH:
raise RuntimeError("uv not found in PATH")
agent, mcp_server = await setup_agent()
try:
await chat_loop(agent)
finally:
await mcp_server.__aexit__(None, None, None)

if __name__ == "__main__":
asyncio.run(main())

Here’s what the script does:
  1. Initializes Weave for tracking agent interactions.
  2. Launches the Weave MCP server using uv and connects via stdio.
  3. Creates an Agent that can access Weave’s tools for querying projects, traces, and metrics.
  4. Runs a REPL-style chat loop that maintains message history for context and lets you interact with the agent naturally.
You can now run the script and start asking questions like:
generate a report of recent LLM activity in the byyoung3/qat project

After entering the query, your agent will begin querying the Weave MCP server, and create a report for the recent traces in the “qat” project I have inside Weave:


Conclusion

This walkthrough is meant to be a quick walkthrough on how OpenAI Agents, the MCP protocol, and W&B Weave can work together to build intelligent tools powered by local and remote capabilities.
By using the Weave MCP server locally and giving the agent access via stdio, we enabled deep introspection into agent behavior — including the ability to query historical runs, generate reports, and analyze usage patterns across projects. MCP acts as the glue between the agent and the outside world, turning any tool, database, or service into something the agent can reason over and use. And Weave serves as the observability layer, letting you inspect, debug, and monitor every step of the agent’s thought process.
Together, this stack gives you the foundation to build powerful, introspectable agents — whether you’re navigating files, managing infrastructure, or querying production telemetry.
You can head here for a few more examples from OpenAI.
Iterate on AI agents and models faster. Try Weights & Biases today.