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
from pydantic_graph import End, FullStatePersistence
529
529
from count_down import CountDown, CountDownState, count_down_graph
530
530
@@ -593,7 +593,7 @@ We can run the `count_down_graph` from [above](#iterating-over-a-graph), using [
593
593
594
594
As you can see in this code, `run_node` requires no external application state (apart from state persistence) to be run, meaning graphs can easily be executed by distributed execution and queueing systems.
When the model interacts with these servers, it will see the prefixed tool names, but the prefixes will be automatically handled when making tool calls.
279
+
280
+
## MCP Sampling
281
+
282
+
!!! info "What is MCP Sampling?"
283
+
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.
284
+
285
+
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.
286
+
287
+
Confusingly it has nothing to do with the concept of "sampling" in observability, or frankly the concept of "sampling" in any other domain.
288
+
289
+
??? info "Sampling Diagram"
290
+
Here's a mermaid diagram that may or may not make the data flow clearer:
Pydantic AI supports sampling as both a client and server. See the [server](./server.md#mcp-sampling) documentation for details on how to use sampling within a server.
312
+
313
+
Sampling is automatically supported by Pydantic AI agents when they act as a client.
314
+
315
+
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).
result =await agent.run('Create an image of a robot in a punk style.')
368
+
print(result.output)
369
+
#> Image file written to robot_punk.svg.
370
+
```
371
+
372
+
_(This example is complete, it can be run "as is" with Python 3.10+)_
373
+
374
+
You can disallow sampling by settings [`allow_sampling=False`][pydantic_ai.mcp.MCPServerStdio.allow_sampling] when creating the server reference, e.g.:
0 commit comments