Last active
March 26, 2025 21:31
-
-
Save seratch/bd4e2b3a8ed036ac9841e569eb668ba5 to your computer and use it in GitHub Desktop.
OpenAI Agents SDK MCP server example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# OpenAI Agents SDK MCP server example | |
# | |
# How to run this app: | |
# $ pip install -U openai-agents | |
# $ python app.py | |
# | |
# See also: | |
# - https://openai.github.io/openai-agents-python/mcp/ | |
# - https://github.com/openai/openai-agents-python/tree/main/examples/mcp | |
# - https://github.com/modelcontextprotocol/servers/tree/main/src/filesystem | |
import asyncio | |
import os | |
import shutil | |
import logging | |
from agents import Agent, Runner, gen_trace_id, trace | |
from agents.mcp import MCPServerStdio | |
logging.basicConfig(level=logging.WARNING) | |
# If you want to see more logs, enable the following lines: | |
# logging.basicConfig(level=logging.INFO) | |
# logging.getLogger("openai.agents").setLevel(logging.DEBUG) | |
async def main(): | |
command = f"npx -y @modelcontextprotocol/server-filesystem {os.path.dirname(os.path.abspath(__file__))}" | |
params = {"command": command.split(" ")[0], "args": command.split(" ")[1:]} | |
async with MCPServerStdio(name="Filesystem Server via npx", params=params) as fs_server: | |
trace_id = gen_trace_id() | |
with trace(workflow_name="MCP Filesystem Example", trace_id=trace_id): | |
print(f"View trace: https://platform.openai.com/traces/{trace_id}\n") | |
agent = Agent( | |
name="MCP filesystem agent", | |
instructions="Use the tools to read the filesystem.", | |
mcp_servers=[fs_server], | |
) | |
print("Reading files using @modelcontextprotocol/server-filesystem ...") | |
result = await Runner.run( | |
starting_agent=agent, input="Tell what the files are." | |
) | |
print(result.final_output) | |
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()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python app.py | |
Secure MCP Filesystem Server running on stdio | |
Allowed directories: [ '/Users/seratch/tmp/mcp-sample' ] | |
View trace: https://platform.openai.com/traces/trace_5ce7ade2a2aa4a6989c6c38bff543363 | |
Reading files using @modelcontextprotocol/server-filesystem ... | |
The directory contains the following files: | |
1. **`app.py`**: A Python script file located in the root directory. | |
2. **`sample_files` Directory**: | |
- **`a.txt`**: A text file inside the `sample_files` directory. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you already have python3 + npx, the following command immediately runs the code: