pydantic_ai.mcp
MCPServer
Bases: ABC
Base class for attaching agents to MCP servers.
See https://modelcontextprotocol.io for more information.
Source code in pydantic_ai_slim/pydantic_ai/mcp.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
client_streams
abstractmethod
async
client_streams() -> AsyncIterator[
tuple[
MemoryObjectReceiveStream[
JSONRPCMessage | Exception
],
MemoryObjectSendStream[JSONRPCMessage],
]
]
Create the streams for the MCP server.
Source code in pydantic_ai_slim/pydantic_ai/mcp.py
43 44 45 46 47 48 49 50 51 52 |
|
list_tools
async
list_tools() -> list[ToolDefinition]
Retrieve tools that are currently active on the server.
Note: - We don't cache tools as they might change. - We also don't subscribe to the server to avoid complexity.
Source code in pydantic_ai_slim/pydantic_ai/mcp.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
call_tool
async
Call a tool on the server.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool_name
|
str
|
The name of the tool to call. |
required |
arguments
|
dict[str, Any]
|
The arguments to pass to the tool. |
required |
Returns:
Type | Description |
---|---|
CallToolResult
|
The result of the tool call. |
Source code in pydantic_ai_slim/pydantic_ai/mcp.py
71 72 73 74 75 76 77 78 79 80 81 |
|
MCPServerStdio
dataclass
Bases: MCPServer
Runs an MCP server in a subprocess and communicates with it over stdin/stdout.
This class implements the stdio transport from the MCP specification. See https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#stdio for more information.
Note
Using this class as an async context manager will start the server as a subprocess when entering the context, and stop it when exiting the context.
Example:
from pydantic_ai import Agent
from pydantic_ai.mcp import MCPServerStdio
server = MCPServerStdio('npx', ['-y', '@pydantic/mcp-run-python', 'stdio']) # (1)!
agent = Agent('openai:gpt-4o', mcp_servers=[server])
async def main():
async with agent.run_mcp_servers(): # (2)!
...
- See MCP Run Python for more information.
- This will start the server as a subprocess and connect to it.
Source code in pydantic_ai_slim/pydantic_ai/mcp.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
|
env
class-attribute
instance-attribute
The environment variables the CLI server will have access to.
By default the subprocess will not inherit any environment variables from the parent process.
If you want to inherit the environment variables from the parent process, use env=os.environ
.
MCPServerHTTP
dataclass
Bases: MCPServer
An MCP server that connects over streamable HTTP connections.
This class implements the SSE transport from the MCP specification. See https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#http-with-sse for more information.
The name "HTTP" is used since this implemented will be adapted in future to use the new Streamable HTTP currently in development.
Note
Using this class as an async context manager will create a new pool of HTTP connections to connect to a server which should already be running.
Example:
from pydantic_ai import Agent
from pydantic_ai.mcp import MCPServerHTTP
server = MCPServerHTTP('http://localhost:3001/sse') # (1)!
agent = Agent('openai:gpt-4o', mcp_servers=[server])
async def main():
async with agent.run_mcp_servers(): # (2)!
...
- E.g. you might be connecting to a server run with
npx @pydantic/mcp-run-python sse
, see MCP Run Python for more information. - This will connect to a server running on
localhost:3001
.
Source code in pydantic_ai_slim/pydantic_ai/mcp.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
|
url
instance-attribute
url: str
The URL of the SSE endpoint on the MCP server.
For example for a server running locally, this might be http://localhost:3001/sse
.