Mcp
This module implements the MCP server interface between the agent and the LLM.
See https://docs.cursor.com/context/model-context-protocol for more information.
MCPServer
Bases: ABC
Base class for MCP servers that can be used to run a command or connect to an SSE server.
See https://modelcontextprotocol.io/introduction for more information.
Source code in pydantic_ai_slim/pydantic_ai/mcp.py
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 99 100 101 102 103 |
|
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
48 49 50 51 52 53 54 55 56 57 |
|
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
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|
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
76 77 78 79 80 81 82 83 84 85 86 |
|
MCPServerStdio
dataclass
Bases: MCPServer
An MCP server that runs a subprocess.
This class implements the stdio transport from the MCP specification. See https://modelcontextprotocol.io/docs/concepts/transports#standard-input%2Foutput-stdio for more information.
Example:
from pydantic_ai import Agent
from pydantic_ai.mcp import MCPServerStdio
server = MCPServerStdio('python', ['-m', 'pydantic_ai_examples.mcp_server'])
agent = Agent('openai:gpt-4o', mcp_servers=[server])
Source code in pydantic_ai_slim/pydantic_ai/mcp.py
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 |
|
MCPServerSSE
dataclass
Bases: MCPServer
An MCP server that connects to a remote server.
This class implements the SSE transport from the MCP specification. See https://modelcontextprotocol.io/docs/concepts/transports#server-sent-events-sse for more information.
Source code in pydantic_ai_slim/pydantic_ai/mcp.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
|