Skip to content

Commit 2261aab

Browse files
authored
Adding Git MCP server example (openai#343)
- Adding new example showing a single agent interacting with local MCP server for git
2 parents b5ba229 + 5385f8b commit 2261aab

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

examples/mcp/filesystem_example/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MCP Filesystem Example
22

3-
This example uses the [fileystem MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/filesystem), running locally via `npx`.
3+
This example uses the [filesystem MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/filesystem), running locally via `npx`.
44

55
Run it via:
66

examples/mcp/git_example/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# MCP Git Example
2+
3+
This example uses the [git MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/git), running locally via `uvx`.
4+
5+
Run it via:
6+
7+
```
8+
uv run python examples/mcp/git_example/main.py
9+
```
10+
11+
## Details
12+
13+
The example uses the `MCPServerStdio` class from `agents`, with the command:
14+
15+
```bash
16+
uvx mcp-server-git
17+
```
18+
Prior to running the agent, the user is prompted to provide a local directory path to their git repo. Using that, the Agent can invoke Git MCP tools like `git_log` to inspect the git commit log.
19+
20+
Under the hood:
21+
22+
1. The server is spun up in a subprocess, and exposes a bunch of tools like `git_log()`
23+
2. We add the server instance to the Agent via `mcp_agents`.
24+
3. Each time the agent runs, we call out to the MCP server to fetch the list of tools via `server.list_tools()`. The result is cached.
25+
4. If the LLM chooses to use an MCP tool, we call the MCP server to run the tool via `server.run_tool()`.

examples/mcp/git_example/main.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import asyncio
2+
import shutil
3+
4+
from agents import Agent, Runner, trace
5+
from agents.mcp import MCPServer, MCPServerStdio
6+
7+
8+
async def run(mcp_server: MCPServer, directory_path: str):
9+
agent = Agent(
10+
name="Assistant",
11+
instructions=f"Answer questions about the git repository at {directory_path}, use that for repo_path",
12+
mcp_servers=[mcp_server],
13+
)
14+
15+
message = "Who's the most frequent contributor?"
16+
print("\n" + "-" * 40)
17+
print(f"Running: {message}")
18+
result = await Runner.run(starting_agent=agent, input=message)
19+
print(result.final_output)
20+
21+
message = "Summarize the last change in the repository."
22+
print("\n" + "-" * 40)
23+
print(f"Running: {message}")
24+
result = await Runner.run(starting_agent=agent, input=message)
25+
print(result.final_output)
26+
27+
28+
async def main():
29+
# Ask the user for the directory path
30+
directory_path = input("Please enter the path to the git repository: ")
31+
32+
async with MCPServerStdio(
33+
params={
34+
"command": "uvx",
35+
"args": [
36+
"mcp-server-git"
37+
]
38+
}
39+
) as server:
40+
with trace(workflow_name="MCP Git Example"):
41+
await run(server, directory_path)
42+
43+
44+
if __name__ == "__main__":
45+
if not shutil.which("uvx"):
46+
raise RuntimeError("uvx is not installed. Please install it with `pip install uvx`.")
47+
48+
asyncio.run(main())

0 commit comments

Comments
 (0)