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
Copy file name to clipboardExpand all lines: docs/mcp/client.md
+21-21Lines changed: 21 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@ Examples of all three are shown below; [mcp-run-python](run-python.md) is used a
26
26
27
27
Each MCP server instance is a [toolset](../toolsets.md) and can be registered with an [`Agent`][pydantic_ai.Agent] using the `toolsets` argument.
28
28
29
-
You can use the [`async with agent`][pydantic_ai.Agent.__aenter__] context manager to open and close connections to all registered servers (and in the case of stdio servers, start and stop the subprocesses) around the context where they'll be used in agent runs. You can also use [`async with server`][pydantic_ai.mcp.MCPServer.__aenter__] to manage the connection or subprocess of a specific server, for example if you'd like to use it with multiple agents. If you don't explicitly enter one of these context managers to set up the server, this will be done automatically when it's needed (e.g. to list the available tools or call a specific tool), but it's more efficient to do so around the entire context where you expect the servers to be used.
29
+
You can use the [`async with agent.setup()`][pydantic_ai.Agent.__aenter__] context manager to open and close connections to all registered servers (and in the case of stdio servers, start and stop the subprocesses) around the context where they’ll be used in agent runs. You can also use [`async with server`][pydantic_ai.mcp.MCPServer.__aenter__] to manage the connection or subprocess of a specific server, for example if you’d like to use it with multiple agents. If you don’t explicitly enter one of these context managers to set up the server, this will be done automatically when it’s needed (e.g. to list the available tools or call a specific tool), but it’s more efficient to do so around the entire context where you expect the servers to be used.
30
30
31
31
### Streamable HTTP Client
32
32
@@ -61,7 +61,7 @@ server = MCPServerStreamableHTTP('http://localhost:8000/mcp') # (1)!
result =await agent.run('How many days between 2000-01-01 and 2025-03-18?')
66
66
print(result.output)
67
67
#> There are 9,208 days between January 1, 2000, and March 18, 2025.
@@ -71,18 +71,18 @@ async def main():
71
71
2. Create an agent with the MCP server attached.
72
72
3. Create a client session to connect to the server.
73
73
74
-
_(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`)_
74
+
_(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`)_
75
75
76
-
**What's happening here?**
76
+
**What’s happening here?**
77
77
78
-
- The model is receiving the prompt "how many days between 2000-01-01 and 2025-03-18?"
79
-
- The model decides "Oh, I've got this `run_python_code` tool, that will be a good way to answer this question", and writes some python code to calculate the answer.
78
+
- The model is receiving the prompt “how many days between 2000-01-01 and 2025-03-18?”
79
+
- The model decides “Oh, I’ve got this `run_python_code` tool, that will be a good way to answer this question”, and writes some python code to calculate the answer.
80
80
- The model returns a tool call
81
81
- Pydantic AI sends the tool call to the MCP server using the SSE transport
82
82
- The model is called again with the return value of running the code
83
83
- The model returns the final answer
84
84
85
-
You can visualise this clearly, and even see the code that's run by adding three lines of code to instrument the example with [logfire](https://logfire.pydantic.dev/docs):
85
+
You can visualise this clearly, and even see the code that’s run by adding three lines of code to instrument the example with [logfire](https://logfire.pydantic.dev/docs):
[`MCPServerSSE`][pydantic_ai.mcp.MCPServerSSE] requires an MCP server to be running and accepting HTTP connections before running the agent. Running the server is not managed by Pydantic AI.
104
104
105
-
The name "HTTP" is used since this implementation will be adapted in future to use the new
105
+
The name “HTTP” is used since this implementation will be adapted in future to use the new
106
106
[Streamable HTTP](https://github.com/modelcontextprotocol/specification/pull/206) currently in development.
107
107
108
108
Before creating the SSE client, we need to run the server (docs [here](run-python.md)):
result =await agent.run('How many days between 2000-01-01 and 2025-03-18?')
127
127
print(result.output)
128
128
#> There are 9,208 days between January 1, 2000, and March 18, 2025.
@@ -132,11 +132,11 @@ async def main():
132
132
2. Create an agent with the MCP server attached.
133
133
3. Create a client session to connect to the server.
134
134
135
-
_(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`)_
135
+
_(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`)_
136
136
137
-
### MCP "stdio" Server
137
+
### MCP “stdio” Server
138
138
139
-
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.
139
+
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.
result =await agent.run('How many days between 2000-01-01 and 2025-03-18?')
163
163
print(result.output)
164
164
#> There are 9,208 days between January 1, 2000, and March 18, 2025.
@@ -202,7 +202,7 @@ agent = Agent(
202
202
203
203
204
204
asyncdefmain():
205
-
asyncwith agent:
205
+
asyncwith agent.setup():
206
206
result =await agent.run('Echo with deps set to 42', deps=42)
207
207
print(result.output)
208
208
#> {"echo_deps":{"echo":"This is an echo message","deps":42}}
@@ -273,7 +273,7 @@ server = MCPServerSSE(
273
273
agent = Agent("openai:gpt-4o", toolsets=[server])
274
274
275
275
asyncdefmain():
276
-
asyncwith agent:
276
+
asyncwith agent.setup():
277
277
result =await agent.run('How many days between 2000-01-01 and 2025-03-18?')
278
278
print(result.output)
279
279
#> There are 9,208 days between January 1, 2000, and March 18, 2025.
@@ -285,7 +285,7 @@ async def main():
285
285
286
286
## MCP Sampling
287
287
288
-
!!! info "What is MCP Sampling?"
288
+
!!! info “What is MCP Sampling?”
289
289
In MCP [sampling](https://modelcontextprotocol.io/docs/concepts/sampling) is a system by which an MCP server can make LLM calls via the MCP client - effectively proxying requests to an LLM via the client over whatever transport is being used.
290
290
291
291
Sampling is extremely useful when MCP servers need to use Gen AI but you don't want to provision them each with their own LLM credentials or when a public MCP server would like the connecting client to pay for LLM calls.
@@ -318,11 +318,11 @@ Pydantic AI supports sampling as both a client and server. See the [server](./se
318
318
319
319
Sampling is automatically supported by Pydantic AI agents when they act as a client.
320
320
321
-
To be able to use sampling, an MCP server instance needs to have a [`sampling_model`][pydantic_ai.mcp.MCPServerStdio.sampling_model] set. This can be done either directly on the server using the constructor keyword argument or the property, or by using [`agent.set_mcp_sampling_model()`][pydantic_ai.Agent.set_mcp_sampling_model] to set the agent's model or one specified as an argument as the sampling model on all MCP servers registered with that agent.
321
+
To be able to use sampling, an MCP server instance needs to have a [`sampling_model`][pydantic_ai.mcp.MCPServerStdio.sampling_model] set. This can be done either directly on the server using the constructor keyword argument or the property, or by using [`agent.set_mcp_sampling_model()`][pydantic_ai.Agent.set_mcp_sampling_model] to set the agent’s model or one specified as an argument as the sampling model on all MCP servers registered with that agent.
322
322
323
-
Let's say we have an MCP server that wants to use sampling (in this case to generate an SVG as per the tool arguments).
323
+
Let’s say we have an MCP server that wants to use sampling (in this case to generate an SVG as per the tool arguments).
'`run_mcp_servers` is deprecated, use `async with agent:` instead. If you need to set a sampling model on all MCP servers, use `agent.set_mcp_sampling_model()`.'
1838
+
'`run_mcp_servers` is deprecated, use `async with agent.setup():` instead. If you need to set a sampling model on all MCP servers, use `agent.set_mcp_sampling_model()`.'
0 commit comments