Skip to content

Commit 8661849

Browse files
authored
[3/n] Add an MCP stdio example (#337)
### Summary: Spins up a stdio server with some local files, then asks the model questions. ### Test Plan: Run the example, see it work. (repeat of #322)
2 parents 4f8cbfa + de1e9a7 commit 8661849

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# MCP Filesystem Example
2+
3+
This example uses the [fileystem MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/filesystem), running locally via `npx`.
4+
5+
Run it via:
6+
7+
```
8+
uv run python python examples/mcp/filesystem_example/main.py
9+
```
10+
11+
## Details
12+
13+
The example uses the `MCPServerStdio` class from `agents`, with the command:
14+
15+
```bash
16+
npx -y "@modelcontextprotocol/server-filesystem" <samples_directory>
17+
```
18+
19+
It's only given access to the `sample_files` directory adjacent to the example, which contains some sample data.
20+
21+
Under the hood:
22+
23+
1. The server is spun up in a subprocess, and exposes a bunch of tools like `list_directory()`, `read_file()`, etc.
24+
2. We add the server instance to the Agent via `mcp_agents`.
25+
3. Each time the agent runs, we call out to the MCP server to fetch the list of tools via `server.list_tools()`.
26+
4. If the LLM chooses to use an MCP tool, we call the MCP server to run the tool via `server.run_tool()`.
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import asyncio
2+
import os
3+
import shutil
4+
5+
from agents import Agent, Runner, trace
6+
from agents.mcp import MCPServer, MCPServerStdio
7+
8+
9+
async def run(mcp_server: MCPServer):
10+
agent = Agent(
11+
name="Assistant",
12+
instructions="Use the tools to read the filesystem and answer questions based on those files.",
13+
mcp_servers=[mcp_server],
14+
)
15+
16+
# List the files it can read
17+
message = "Read the files and list them."
18+
print(f"Running: {message}")
19+
result = await Runner.run(starting_agent=agent, input=message)
20+
print(result.final_output)
21+
22+
# Ask about books
23+
message = "What is my #1 favorite book?"
24+
print(f"\n\nRunning: {message}")
25+
result = await Runner.run(starting_agent=agent, input=message)
26+
print(result.final_output)
27+
28+
# Ask a question that reads then reasons.
29+
message = "Look at my favorite songs. Suggest one new song that I might like."
30+
print(f"\n\nRunning: {message}")
31+
result = await Runner.run(starting_agent=agent, input=message)
32+
print(result.final_output)
33+
34+
35+
async def main():
36+
current_dir = os.path.dirname(os.path.abspath(__file__))
37+
samples_dir = os.path.join(current_dir, "sample_files")
38+
39+
async with MCPServerStdio(
40+
params={
41+
"command": "npx",
42+
"args": ["-y", "@modelcontextprotocol/server-filesystem", samples_dir],
43+
}
44+
) as server:
45+
with trace(workflow_name="MCP Filesystem Example"):
46+
await run(server)
47+
48+
49+
if __name__ == "__main__":
50+
# Let's make sure the user has npx installed
51+
if not shutil.which("npx"):
52+
raise RuntimeError("npx is not installed. Please install it with `npm install -g npx`.")
53+
54+
asyncio.run(main())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
1. To Kill a Mockingbird – Harper Lee
2+
2. Pride and Prejudice – Jane Austen
3+
3. 1984 – George Orwell
4+
4. The Hobbit – J.R.R. Tolkien
5+
5. Harry Potter and the Sorcerer’s Stone – J.K. Rowling
6+
6. The Great Gatsby – F. Scott Fitzgerald
7+
7. Charlotte’s Web – E.B. White
8+
8. Anne of Green Gables – Lucy Maud Montgomery
9+
9. The Alchemist – Paulo Coelho
10+
10. Little Women – Louisa May Alcott
11+
11. The Catcher in the Rye – J.D. Salinger
12+
12. Animal Farm – George Orwell
13+
13. The Chronicles of Narnia: The Lion, the Witch, and the Wardrobe – C.S. Lewis
14+
14. The Book Thief – Markus Zusak
15+
15. A Wrinkle in Time – Madeleine L’Engle
16+
16. The Secret Garden – Frances Hodgson Burnett
17+
17. Moby-Dick – Herman Melville
18+
18. Fahrenheit 451 – Ray Bradbury
19+
19. Jane Eyre – Charlotte Brontë
20+
20. The Little Prince – Antoine de Saint-Exupéry
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- In the summer, I love visiting London.
2+
- In the winter, Tokyo is great.
3+
- In the spring, San Francisco.
4+
- In the fall, New York is the best.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
1. "Here Comes the Sun" – The Beatles
2+
2. "Imagine" – John Lennon
3+
3. "Bohemian Rhapsody" – Queen
4+
4. "Shake It Off" – Taylor Swift
5+
5. "Billie Jean" – Michael Jackson
6+
6. "Uptown Funk" – Mark Ronson ft. Bruno Mars
7+
7. "Don’t Stop Believin’" – Journey
8+
8. "Dancing Queen" – ABBA
9+
9. "Happy" – Pharrell Williams
10+
10. "Wonderwall" – Oasis

0 commit comments

Comments
 (0)