From 0944390c3f48a5b8bb61c212658ffbc2bda87fb3 Mon Sep 17 00:00:00 2001 From: Dmitry Pimenov Date: Tue, 25 Mar 2025 16:30:50 -0700 Subject: [PATCH 1/3] adding Git MCP server example --- examples/mcp/git_example/main.py | 48 ++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 examples/mcp/git_example/main.py diff --git a/examples/mcp/git_example/main.py b/examples/mcp/git_example/main.py new file mode 100644 index 0000000..5ce747a --- /dev/null +++ b/examples/mcp/git_example/main.py @@ -0,0 +1,48 @@ +import asyncio +import os +import shutil + +from agents import Agent, Runner, trace +from agents.mcp import MCPServer, MCPServerStdio + +async def run(mcp_server: MCPServer, directory_path: str): + agent = Agent( + name="Assistant", + instructions=f"Answer questions about the git repository at {directory_path}, use that for repo_path", + mcp_servers=[mcp_server], + ) + + message = f"Who's the most frequent contributor?" + print("\n" + "-" * 40) + print(f"Running: {message}") + result = await Runner.run(starting_agent=agent, input=message) + print(result.final_output) + + message = f"Summarize the last change in the repository." + print("\n" + "-" * 40) + print(f"Running: {message}") + result = await Runner.run(starting_agent=agent, input=message) + print(result.final_output) + + +async def main(): + # Ask the user for the directory path + directory_path = input("Please enter the path to the git repository: ") + + async with MCPServerStdio( + params={ + "command": "uvx", + "args": [ + "mcp-server-git" + ] + } + ) as server: + with trace(workflow_name="MCP Git Example"): + await run(server, directory_path) + + +if __name__ == "__main__": + if not shutil.which("uvx"): + raise RuntimeError("uvx is not installed. Please install it with `pip install uvx`.") + + asyncio.run(main()) \ No newline at end of file From 88c8acbc5a6df6458c85a9a57afd8d03de0515ef Mon Sep 17 00:00:00 2001 From: Dmitry Pimenov Date: Tue, 25 Mar 2025 16:34:00 -0700 Subject: [PATCH 2/3] fixing lint issues --- examples/mcp/git_example/main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/mcp/git_example/main.py b/examples/mcp/git_example/main.py index 5ce747a..cfc1510 100644 --- a/examples/mcp/git_example/main.py +++ b/examples/mcp/git_example/main.py @@ -1,10 +1,10 @@ import asyncio -import os import shutil from agents import Agent, Runner, trace from agents.mcp import MCPServer, MCPServerStdio + async def run(mcp_server: MCPServer, directory_path: str): agent = Agent( name="Assistant", @@ -12,13 +12,13 @@ async def run(mcp_server: MCPServer, directory_path: str): mcp_servers=[mcp_server], ) - message = f"Who's the most frequent contributor?" + message = "Who's the most frequent contributor?" print("\n" + "-" * 40) print(f"Running: {message}") result = await Runner.run(starting_agent=agent, input=message) print(result.final_output) - message = f"Summarize the last change in the repository." + message = "Summarize the last change in the repository." print("\n" + "-" * 40) print(f"Running: {message}") result = await Runner.run(starting_agent=agent, input=message) @@ -45,4 +45,4 @@ if __name__ == "__main__": if not shutil.which("uvx"): raise RuntimeError("uvx is not installed. Please install it with `pip install uvx`.") - asyncio.run(main()) \ No newline at end of file + asyncio.run(main()) From 5385f8b10ed22beb356a8727e6c2b85f12886163 Mon Sep 17 00:00:00 2001 From: Dmitry Pimenov Date: Tue, 25 Mar 2025 16:52:52 -0700 Subject: [PATCH 3/3] added readme, fixed typo --- examples/mcp/filesystem_example/README.md | 2 +- examples/mcp/git_example/README.md | 25 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 examples/mcp/git_example/README.md diff --git a/examples/mcp/filesystem_example/README.md b/examples/mcp/filesystem_example/README.md index 682afc8..1b6b183 100644 --- a/examples/mcp/filesystem_example/README.md +++ b/examples/mcp/filesystem_example/README.md @@ -1,6 +1,6 @@ # MCP Filesystem Example -This example uses the [fileystem MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/filesystem), running locally via `npx`. +This example uses the [filesystem MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/filesystem), running locally via `npx`. Run it via: diff --git a/examples/mcp/git_example/README.md b/examples/mcp/git_example/README.md new file mode 100644 index 0000000..35ff7cd --- /dev/null +++ b/examples/mcp/git_example/README.md @@ -0,0 +1,25 @@ +# MCP Git Example + +This example uses the [git MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/git), running locally via `uvx`. + +Run it via: + +``` +uv run python examples/mcp/git_example/main.py +``` + +## Details + +The example uses the `MCPServerStdio` class from `agents`, with the command: + +```bash +uvx mcp-server-git +``` +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. + +Under the hood: + +1. The server is spun up in a subprocess, and exposes a bunch of tools like `git_log()` +2. We add the server instance to the Agent via `mcp_agents`. +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. +4. If the LLM chooses to use an MCP tool, we call the MCP server to run the tool via `server.run_tool()`.