Learning resourcesReviewed · Pydantic

Pydantic AI MCP server and sampling guide

This Pydantic AI guide teaches Python developers to share AI agents with other applications, but its sampling client needs a real model call added. It demonstrates Model Context Protocol (MCP) servers, Python clients and model requests routed through the client.

Researched 15 Nov 2026

In short.

This Pydantic AI guide helps Python developers share an AI agent with other applications, but its sampling client returns a fixed answer instead of calling a model. It demonstrates an MCP tool with direct model access, then adapts it to request model responses through the client. The basic client cannot handle sampling, and preserving prior tool history requires a compatible sampling format. No examples were executed, so runtime behavior, model availability and deployment suitability remain unverified.

Researched and written by an AI model and checked by a second AI model. How it works

Covers the complete selected page at [https://pydantic.dev/docs/ai/mcp/server/index.md], including its introductory documentation-index and query-parameter notices, four Python listings and sampling compatibility notes. The introductory notices concern documentation access and do not establish runtime requirements. Linked pages, the documentation index and the MCP SDK repository are excluded. Installation, model access, tool calls, sampling exchanges, compatibility, performance and deployment behavior were not executed or tested.

The overview

Identity

The resource is the Pydantic AI documentation page titled Server at [https://pydantic.dev/docs/ai/mcp/server/index.md]. It explains how a Pydantic AI agent can operate inside a Model Context Protocol (MCP) server, which exposes callable tools to client applications.

The page contains four Python listings: a basic server and client, followed by a server and client using MCP sampling. It is a focused integration guide rather than a complete application or deployment manual.

Assessment

The guide is useful for understanding where model calls happen in an MCP integration. Its paired examples show a server calling a configured model directly and a server requesting a response through its client.

Its practical limit is the gap between demonstrating the protocol and connecting a real model in the sampling callback. The callback returns a fixed response, and the page does not establish installation compatibility, operational reliability or production readiness.

Users

Python developers who already use Pydantic AI or are building MCP tools are the clearest audience. The examples are particularly relevant when deciding whether the server or its client should provide model access.

Beginners can follow the small poem-generation example, but they will need familiarity with asynchronous Python, imports, subprocesses and client sessions. The page does not teach those foundations or explain provider authentication.

Capabilities

The basic example registers an asynchronous poet tool with FastMCP. The tool accepts a theme, runs a Pydantic AI agent and returns its text output. A Python MCP client initializes a session, calls the tool and reads the first text content item.

The sampling example uses MCPSamplingModel to request model responses through the MCP client. The page also describes continuing a conversation with message_history, including preservation of function calls, results and retry feedback when the client supports the required sampling format.

Using it

Requirements

The examples require Python and the libraries imported as mcp and pydantic_ai. The client launches a local Python server process and communicates over standard input and output. Supported Python versions, package versions and installation commands are not specified.

The direct example needs access to its configured model. Sampling instead requires a client that handles sampling requests; the basic client shown on the page does not. Authentication requirements and the availability of the displayed Anthropic model identifier are not established.

Mechanism

In the direct path, a client calls poet with a theme. The server builds a prompt, awaits Agent.run and returns the agent's output through MCP. The model is configured on the server-side agent.

In the sampling path, the tool receives an MCP Context and supplies its session to MCPSamplingModel. Model requests travel back through that session to the client's sampling_callback. The illustrated callback inspects the request and constructs a text response; connecting it to an actual model is left as a TODO.

Setup

For the direct example, save the server listing as mcp_server.py. The matching client launches that filename with Python, opens a ClientSession, initializes it and calls poet. Its env=dict(os.environ) argument forwards the parent's environment to the local server process.

For sampling, use the matching mcp_server_sampling.py server and register sampling_callback when constructing ClientSession. The page supplies no dependency installation procedure or credentials configuration, and the callback still needs a real model integration.

Example

Both clients request a poem with the theme socks. The direct server instructs its configured agent to rhyme, and the documentation displays a sample poem.

The sampling client returns the fixed text Socks for a fox. and labels the response model fictional-llm. This illustrates the response structure without demonstrating model inference. All displayed outputs are documentation examples; none were independently reproduced.

Assessment & evidence

Limitations

The basic client cannot handle sampling requests; the page explicitly says using it with the sampling server would produce an error. The custom sampling client supplies a fixed response rather than a real model connection.

Preserving function calls, results and retry feedback in conversation history requires a client supporting the 2025-11-25 MCP sampling format or later. Multimodal tool results are not supported, and replaying tool history does not let the sampling agent call new tools.

The page supplies no benchmarks, authentication design, concurrency results, deployment instructions or detailed failure-handling guidance. Those aspects are not assessed.

Reasoning

The paired examples make the guide useful for an architectural decision: placing model access in the tool server or requesting it through the client. They expose the extra callback and client support needed for sampling.

The evidence supports using the page to understand and adapt this integration pattern. It does not establish which approach is faster, cheaper or more reliable, nor whether either example meets a particular application's deployment needs.

Alternatives

Within the page, direct model access is an alternative to MCP sampling. The direct example configures the model on the server; sampling moves responsibility for supplying model responses to the client.

For a sampling-capable client, the page suggests using a Pydantic AI agent or implementing a sampling_callback with the MCP Python SDK. It calls the Pydantic AI approach the simplest option, but its linked setup documentation is outside this assessment. No competing frameworks are compared.

Evidence

The evidence consists of the complete documentation page at [https://pydantic.dev/docs/ai/mcp/server/index.md], captured on 15 November 2026. The captured source revision is sha256:96fd6ad4d56a6b0fa960941d602cf7801757184e4df01aa63c4b21e2966255d4.

Its code listings and explanatory text support the described integration patterns and compatibility restrictions. Linked documentation and the MCP Python SDK repository were not assessed. No code was executed, so the page's sample outputs and statement that an example can run as supplied are not execution evidence.

Access & freshness

Costs

The page provides no prices, token usage, quotas or hosting estimates. Total implementation and operating costs are unknown.

The direct example calls a configured model, while a real sampling implementation would call a model through the client. Neither model's pricing is established here. The fixed sampling reply provides no evidence about inference cost.

Rights

The supplied page does not state licensing or redistribution terms for its prose or code examples. It also does not establish rights governing model outputs. Those permissions are unknown from this page alone.

Freshness

The capture timestamp is 2026-11-15T04:30:05.165Z. The page does not provide a publication date, last-updated date or pinned package versions, so its age and compatibility with particular releases are unknown.

The 2025-11-25 date identifies a sampling format requirement for preserving tool history. It is not a publication date or Pydantic AI version. The current availability of the model identifier used in the direct example remains unverified.

Actions

Choose where model access should live before adapting the examples. Establish compatible package versions and provider configuration for the direct approach. For sampling, implement the real model call inside sampling_callback and use a client that supports sampling.

If continuing conversations with prior tool activity, verify support for the required sampling format and account for the stated history limitations. Review which environment variables the local server receives. Confirm behavior in the intended client and model combination before relying on the integration; no such execution evidence is supplied here.

Evidence for this report

selected-page.txtselected · checked 2026-11-15