|
| 1 | +--- |
| 2 | +title: Using MCP Servers |
| 3 | +description: Connect Marvin agents to Model Context Protocol (MCP) servers for extended capabilities. |
| 4 | +--- |
| 5 | + |
| 6 | +<Warning> |
| 7 | + MCP support is experimental and subject to change. [Open an issue on GitHub](https://github.com/PrefectHQ/marvin/issues) if you'd like to see a feature added or have any feedback. |
| 8 | +</Warning> |
| 9 | + |
| 10 | +Marvin agents can connect to [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) servers, allowing them to access a wide array of external tools and data sources. |
| 11 | + |
| 12 | +## What is MCP? |
| 13 | + |
| 14 | +MCP provides a standardized way for AI agents to interface with tools, resources, prompts etc. If a thing in the world has an MCP server, you can trivially write an agent that interacts with it (on the terms of the MCP server implementation). |
| 15 | + |
| 16 | +## Connecting an Agent to an MCP Server |
| 17 | + |
| 18 | +To give your agent access to an MCP server, you simply pass `MCPServer` instances to the `mcp_servers` argument when creating your `Agent`. |
| 19 | + |
| 20 | +Marvin uses `pydantic-ai` under the hood for MCP server interactions. The most common server type you'll likely use is `MCPServerStdio`, which communicates with an MCP server running as a subprocess via its standard input/output. |
| 21 | + |
| 22 | +### Example: Using a Python Interpreter via MCP |
| 23 | + |
| 24 | +Let's say you want your agent to be able to run Python code. There are MCP servers that provide a Python interpreter as a tool. The `jsr:@pydantic/mcp-run-python` package is one such server that can be run with Deno. |
| 25 | + |
| 26 | +```python |
| 27 | +from marvin.agents import Agent |
| 28 | +from pydantic_ai.mcp import MCPServerStdio |
| 29 | + |
| 30 | +# Configure the MCP server for running Python code |
| 31 | +# This requires Deno and the jsr:@pydantic/mcp-run-python package |
| 32 | +# You can install the Deno package with: |
| 33 | +# deno install -A -g jsr:@pydantic/mcp-run-python |
| 34 | +run_python_mcp_server = MCPServerStdio( |
| 35 | + command="deno", # Or the full path to your deno executable |
| 36 | + args=["run", "-A", "jsr:@pydantic/mcp-run-python", "stdio"], |
| 37 | +) |
| 38 | + |
| 39 | +# Create an agent and provide the MCP server |
| 40 | +coder_agent = Agent( |
| 41 | + name="Coder", |
| 42 | + instructions="Use the Python interpreter to solve tasks.", |
| 43 | + mcp_servers=[run_python_mcp_server] |
| 44 | +) |
| 45 | + |
| 46 | +async def run_code(): |
| 47 | + result = await coder_agent.run_async( |
| 48 | + "Use python to calculate the square root of 256 and tell me the result." |
| 49 | + ) |
| 50 | + print(result) |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + asyncio.run(run_code()) |
| 54 | +``` |
| 55 | + |
| 56 | +<Tip> |
| 57 | + Make sure the command specified in `MCPServerStdio` (e.g., `"deno"` or `"uvx"`) is available in your system's PATH or provide the full path to the executable. |
| 58 | +</Tip> |
| 59 | + |
| 60 | +### Example: Using a Git Tool Server via MCP |
| 61 | + |
| 62 | +Another common use case is interacting with Git repositories. The `mcp-server-git` package (runnable with `uvx`, part of `uv`) provides Git tools over MCP. |
| 63 | + |
| 64 | +```python |
| 65 | +from marvin.agents import Agent |
| 66 | +from pydantic_ai.mcp import MCPServerStdio |
| 67 | + |
| 68 | +# Configure the MCP server for Git tools |
| 69 | +# This requires uv and mcp-server-git (installable via: uvx mcp-server-git, if needed) |
| 70 | +git_mcp_server = MCPServerStdio( |
| 71 | + command="uvx", |
| 72 | + args=["mcp-server-git"] |
| 73 | +) |
| 74 | + |
| 75 | +version_control_agent = Agent( |
| 76 | + name="VersionControlPro", |
| 77 | + instructions="Use Git tools to inspect the repository.", |
| 78 | + mcp_servers=[git_mcp_server] |
| 79 | +) |
| 80 | + |
| 81 | +async def get_repo_info(): |
| 82 | + result = await version_control_agent.run_async( |
| 83 | + "Get the latest commit message from this repository." |
| 84 | + ) |
| 85 | + print(result) |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + asyncio.run(get_repo_info()) |
| 89 | +``` |
| 90 | + |
| 91 | +## How it Works |
| 92 | + |
| 93 | +<Tip> |
| 94 | + tldr: It's basically just function calling (for now) |
| 95 | +</Tip> |
| 96 | + |
| 97 | +When an agent configured with `mcp_servers` runs: |
| 98 | +1. Marvin discovers the tools provided by each active MCP server. |
| 99 | +2. These discovered tools (e.g., `run_python_code` from the Python server, or `git_log` from the Git server) are made available to the LLM just like standard Python tools. |
| 100 | +3. If the LLM decides to use an MCP tool, Marvin's engine handles the communication with the specified `MCPServer` instance. |
| 101 | +4. The result from the MCP server is processed and returned to the LLM, which then formulates the final response. |
| 102 | + |
| 103 | +## Exploring Further |
| 104 | + |
| 105 | +For a more comprehensive example demonstrating multiple MCP servers and tools, check out the example script in the Marvin repository: |
| 106 | + |
| 107 | +[`examples/agent_mcp.py`](https://github.com/PrefectHQ/marvin/blob/main/examples/agent_mcp.py) |
| 108 | + |
| 109 | +This example showcases an agent that uses both a Python interpreter and Git tools via MCP to perform a multi-step task. You can run it locally to see MCP integration in action. |
| 110 | + |
| 111 | +<Accordion title="Running the example/agent_mcp.py script"> |
| 112 | +First, ensure you have the necessary MCP servers installed. For `examples/agent_mcp.py`, this typically means having Deno and `jsr:@pydantic/mcp-run-python` available, as well as `uv` and `mcp-server-git` (if you intend to use the git server part). |
| 113 | + |
| 114 | +Then, from the root of the Marvin repository, you can run: |
| 115 | +```bash |
| 116 | +uv run examples/agent_mcp.py |
| 117 | +``` |
| 118 | +You should see the agent using tools like `git_log` and `run_python_code` to complete its task. |
| 119 | +</Accordion> |
| 120 | + |
| 121 | +This setup allows Marvin agents to flexibly leverage a growing ecosystem of MCP-compatible tools and services. |
| 122 | + |
| 123 | +```python |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +``` |
0 commit comments