You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PydanticAI comes with two ways to connect to MCP servers:
20
20
21
-
-[`MCPServerHTTP`][pydantic_ai.mcp.MCPServerHTTP] which connects to an MCP server using the [HTTP SSE](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#http-with-sse) transport
21
+
-[`MCPServerSSE`][pydantic_ai.mcp.MCPServerSSE] which connects to an MCP server using the [HTTP SSE](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#http-with-sse) transport
22
+
-[`MCPServerStreamableHTTP`][pydantic_ai.mcp.MCPServerStreamableHTTP] which connects to an MCP server using the [Streamable HTTP](https://modelcontextprotocol.io/introduction#streamable-http) transport
22
23
-[`MCPServerStdio`][pydantic_ai.mcp.MCPServerStdio] which runs the server as a subprocess and connects to it using the [stdio](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#stdio) transport
23
24
24
25
Examples of both are shown below; [mcp-run-python](run-python.md) is used as the MCP server in both examples.
25
26
26
27
### SSE Client
27
28
28
-
[`MCPServerHTTP`][pydantic_ai.mcp.MCPServerHTTP] connects over HTTP using the [HTTP + Server Sent Events transport](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#http-with-sse) to a server.
29
+
[`MCPServerSSE`][pydantic_ai.mcp.MCPServerSSE] connects over HTTP using the [HTTP + Server Sent Events transport](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#http-with-sse) to a server.
29
30
30
31
!!! note
31
-
[`MCPServerHTTP`][pydantic_ai.mcp.MCPServerHTTP] requires an MCP server to be running and accepting HTTP connections before calling [`agent.run_mcp_servers()`][pydantic_ai.Agent.run_mcp_servers]. Running the server is not managed by PydanticAI.
32
+
[`MCPServerSSE`][pydantic_ai.mcp.MCPServerSSE] requires an MCP server to be running and accepting HTTP connections before calling [`agent.run_mcp_servers()`][pydantic_ai.Agent.run_mcp_servers]. Running the server is not managed by PydanticAI.
32
33
33
34
The name "HTTP" is used since this implemented will be adapted in future to use the new
34
35
[Streamable HTTP](https://github.com/modelcontextprotocol/specification/pull/206) currently in development.
@@ -43,9 +44,9 @@ deno run \
43
44
44
45
```python {title="mcp_sse_client.py" py="3.10"}
45
46
from pydantic_ai import Agent
46
-
from pydantic_ai.mcp importMCPServerHTTP
47
+
from pydantic_ai.mcp importMCPServerSSE
47
48
48
-
server =MCPServerHTTP(url='http://localhost:3001/sse') # (1)!
49
+
server =MCPServerSSE(url='http://localhost:3001/sse') # (1)!
result =await agent.run('How many days between 2000-01-01 and 2025-03-18?')
125
+
print(result.output)
126
+
#> There are 9,208 days between January 1, 2000, and March 18, 2025.
127
+
```
128
+
129
+
1. Create an agent with the MCP server attached.
130
+
2. Create a client session to connect to the server.
131
+
132
+
_(This example is complete, it can be run "as is" with Python 3.10+ — you'll need to add `asyncio.run(main())` to run `main`)_
133
+
87
134
### MCP "stdio" Server
88
135
89
136
The other transport offered by MCP is the [stdio transport](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#stdio) where the server is run as a subprocess and communicates with the client over `stdin` and `stdout`. In this case, you'd use the [`MCPServerStdio`][pydantic_ai.mcp.MCPServerStdio] class.
@@ -135,15 +182,15 @@ This allows you to use multiple servers that might have overlapping tool names w
0 commit comments