# MCP is all you need

Samuel Colvin, Pydantic

AI Engineer World's Fair 2025

[Talk page](https://ai.engineer/talks/bmWZk9vTze0-mcp-is-all-you-need) · [Video](https://www.youtube.com/watch?v=bmWZk9vTze0)

## MCP is all you need: letting tools borrow the client’s model

MCP sampling lets an agent inside a tool request inference through its client. A PyPI research example shows how this separates specialized context, SQL repair and progress reporting from the main agent.

Familiarity with Python, LLM tool calling and basic SQL will help you follow the demonstration.

### How much infrastructure does agent communication need?

[0:17](https://www.youtube.com/watch?v=bmWZk9vTze0&t=17s)

When agents need to communicate, how much new infrastructure do we actually need? Samuel Colvin approaches that question from Python’s validation and agent tooling. He created [Pydantic](https://pydantic.dev/), which he reports was receiving about 360 million downloads a month, approximately 140 per second. He describes its use throughout general Python development and broadly across Python’s generative AI SDKs and agent frameworks.

Pydantic became a company at the beginning of 2023, then expanded into Pydantic AI, a Python agent framework built on similar principles, and Logfire, its commercial observability platform. Colvin also describes himself as a somewhat inactive co-maintainer of the [MCP Python SDK](https://github.com/modelcontextprotocol/python-sdk). Those projects bring together the concerns that recur throughout the demonstration: typed interfaces, agent execution and visibility into what happened.

The title riffs on Jason Liu’s talks, *Pydantic Is All You Need* and *Pydantic Is Still All You Need*. Its qualification matters: neither Pydantic nor MCP solves everything. The useful claim is narrower—**MCP already provides capabilities that can simplify communication between agents**, so inventing another communication mechanism may be unnecessary.

![Slide titled “what” explains the talk’s title and highlights MCP’s role in multi-agent communication, with the speaker inset below.](https://ai.engineer/api/talk-essay-media/85752644edea450c6a3b86c91925aa3d0b71bf1eeb96f216aa7cd9e60dfc3fac)

MCP for communication between autonomous agents.

The scope here is autonomous agents and application code, particularly Python, rather than Claude Desktop, Cursor, Zed or Windsurf. Colvin characterizes those interactive applications as MCP’s original focus. MCP’s prompts and resources can be valuable there; for the autonomous application he is building, they play a smaller role. Tool calling is the part that carries the design.

### Tool calling needs more than an endpoint description

[3:11](https://www.youtube.com/watch?v=bmWZk9vTze0&t=191s)

Why not describe the tools with OpenAPI? Colvin’s answer is that the requirements extend beyond describing a request and its final response. A running agent may need a changing tool inventory, messages during execution, or an inference request flowing in the opposite direction.

| Capability | What it enables |
| --- | --- |
| Dynamic tools | Tools appear or disappear as server state changes. |
| Logging | Information reaches the client before a tool finishes. |
| Sampling | A server asks its client to arrange inference. |
| Tracing and observability | Developers inspect execution across components. |
| Standard input/output | A tool server runs as a local subprocess. |

These are the requirements behind Colvin’s OpenAPI comparison. In particular, subprocess communication over standard input and standard output gives application code a useful way to compose local tools without treating every integration as an HTTP service.

### When a tool is itself an agent

[4:01](https://www.youtube.com/watch?v=bmWZk9vTze0&t=241s)

The familiar MCP architecture connects an agent to independently developed tools. The agent need not be designed for a particular tool, and the tool need not know the agent that will call it. Colvin compares this to a browser visiting a website: the shared protocol makes composition possible. Today’s browser monoculture weakens the diversity of that example, but the original interoperability principle still applies.

Now let one of those tools be an agent. It can reason with an LLM and call additional tools, either through MCP or through direct integrations. The composition still works, but each nested agent now needs model access, configuration and resources. A remote MCP server also has to account for the inference costs it incurs while answering someone else’s request.

**Sampling moves access to inference back through the client.** Instead of requiring every agent exposed as a tool to provision its own model connection, the tool can ask the originating client to obtain a model response on its behalf.

### The sampling round trip

[5:37](https://www.youtube.com/watch?v=bmWZk9vTze0&t=337s)

In MCP, [sampling](https://modelcontextprotocol.io/specification/2025-03-26/client/sampling) means a server can request a model generation through its client. The server is still serving a tool call; requesting inference is an intermediate step toward completing that call.

The request follows this sequence:

1. The outer agent calls its LLM, which selects an MCP tool.
2. The client sends the tool call to the MCP server.
3. The server determines that it needs an LLM response and sends a sampling request back to the client.
4. The client obtains the model response and passes it to the server.
5. The server finishes its work and returns the tool result, allowing the outer agent to continue.

The client retains control over model selection and permissions. Borrowing the client’s model access does not guarantee use of the identical outer model, and it does not make inference free.

At the time of the presentation, Colvin describes sampling as powerful but not widely supported. Pydantic AI’s implementation was still a pull request, covering both sides: client support for proxying model calls and server support for using the MCP client as the model connection. The demonstration therefore reflects that development version.

![Sampling slide with a complete sequence diagram connecting LLM, MCP client and MCP server through tool calls, sampling requests and responses.](https://ai.engineer/api/talk-essay-media/0a9843131fd6af31eb593502b21941a4e003205299abc19b1ba04f855ff1a692)

Sampling routes an MCP server’s LLM request through the client.

### A research tool that generates and repairs SQL

[7:06](https://www.youtube.com/watch?v=bmWZk9vTze0&t=426s)

The concrete application is a research agent for open-source packages and libraries. A complete research system would need many tools; this demonstration implements one: looking up package download counts in the public [PyPI dataset on BigQuery](https://packaging.python.org/en/latest/guides/analyzing-pypi-package-downloads/). The tool contains a Pydantic AI agent that turns a question into SQL.

The setup configures Logfire, declares the dependencies available during execution and permits retries when the model returns unsuitable output. A substantial system prompt gives the inner agent the table schema, instructions and examples. That prepares the model to generate a query, but generation alone is not the completion condition.

The **output validator** connects the generated SQL to the database:

1. Remove surrounding Markdown formatting when present.
2. Check that the query targets the expected table, returning corrective feedback if it does not.
3. Execute the query.
4. If execution fails, raise `ModelRetry` so the model receives feedback and can attempt a repair.

The key change is that a database error becomes another input to the agent rather than immediately ending the tool call.

The execution-to-retry boundary can be expressed in Python as a small helper, called after formatting and table checks:

```python
from collections.abc import Callable
from typing import TypeVar

from pydantic_ai import ModelRetry

Result = TypeVar("Result")


def execute_or_retry(
    sql: str,
    execute_query: Callable[[str], Result],
    query_error: type[Exception],
) -> Result:
    try:
        return execute_query(sql)
    except query_error as exc:
        raise ModelRetry(
            f"The query failed: {exc}. Correct the SQL and try again."
        ) from exc
```

The database adapter supplies the execution function and its query-error type. Pydantic AI handles the retry after `ModelRetry` propagates out of the validator, subject to the configured retry allowance. The demonstrated validator also calls `context.deps.mcp_context.log` as it works.

### Keep the user informed while the tool runs

[9:09](https://www.youtube.com/watch?v=bmWZk9vTze0&t=549s)

The MCP call context reaches the inner agent through its typed dependencies. With `deps_type` describing those dependencies, a validator—or another tool call—can access the MCP context through `context.deps.mcp_context`. Type hints expose the `log` method and its signature, so logging remains available inside the agent without losing the interface information supplied by the SDK.

Those log messages return to the client before the tool finishes and can ultimately reach the person waiting for the answer. MCP also has a separate progress-notification capability, which this example does not use. Logging can describe what is happening; progress notifications could report how far an operation has advanced when that information is available.

The same mechanism serves two interfaces. In a Cursor-style application, it can reassure the user that a tool is still working and explain its current activity. In a web research application, it can expose activity during a task that takes minutes. The final answer no longer has to be the first visible sign of work.

![Python editor showing a query validator with MCP context logging before execution, on a query error and after success.](https://ai.engineer/api/talk-essay-media/ef9838b20460142d7d4a311e0f429993af341a91878ccb759828066d2dd57fb8)

Query code logs execution, retries and success through MCP context.

### Put specialized context inside the tool

[10:34](https://www.youtube.com/watch?v=bmWZk9vTze0&t=634s)

After a successful query, the tool converts the output into a list of dictionaries and then formats it as XML-like text. Colvin chooses that representation because he finds models good at interpreting XML. The result is structured text for the outer model to read, rather than a final answer written directly for the user.

The server uses `FastMCP` bundled inside the MCP Python SDK, which Colvin distinguishes from the separate implementation sharing that name. This is the historical SDK API used in the recording; the repository’s current v2 quickstart has breaking changes. The server registers one PyPI-downloads tool, and the Python function’s docstring becomes the tool description supplied to the LLM that decides whether to call it.

The tool accepts the user’s question in natural language. An alternative would be to make the central agent generate SQL, placing the table schema and query instructions in its context or in a large tool description. Colvin reports that models do not respond well to excessive information in tool descriptions. More fundamentally, those instructions would occupy the main agent’s context on calls where it never uses the PyPI tool.

| SQL generation location | Main agent carries | Specialized work happens |
| --- | --- | --- |
| Central agent | Schema and query instructions | In the outer model call |
| Inside the tool | A description of the tool’s purpose | When the tool is invoked |

**Keeping inference inside the tool also keeps its specialized context there.** Sampling supplies model access across that boundary without requiring the main agent to carry the SQL-generation prompt. The tool returns a string, and the demonstrated server runs over standard input and standard output by default.

### Ask the question, then inspect the nested execution

[12:27](https://www.youtube.com/watch?v=bmWZk9vTze0&t=747s)

The main application registers one MCP server that launches the PyPI server script. The outer agent acts as the MCP client and makes the server’s tool available to its model. It also receives the current date, avoiding stale assumptions about what “this year” means. Colvin then asks how many downloads Pydantic has had this year and runs the agent live.

The live demonstration reports about 1.6 billion Pydantic downloads for the year to date. That is the application’s reported result, not a count of unique users; the recording does not establish the calendar year, exact cutoff or query filters needed to reproduce it.

Colvin next opens Logfire. The trace shown is from a run immediately before he came on stage, rather than the just-completed live run. He leaves the detailed treatment of MCP tracing to the following talk and focuses on reading the application’s execution. The outer agent calls **GPT-4o**, which selects the PyPI tool. It passes a natural-language description of the question; it does not generate the SQL itself.

The MCP client calls the server, which runs a second Pydantic AI agent. That inner agent requests inference through the client, producing the alternating client and server service entries visible in the trace. The nesting makes the sampling path inspectable: the server’s model work appears inside the tool execution, while the client supplies the model connection.

![Expanded trace with MCP client and server labels, nested agent runs, a sampling request, a model call and a SQL query row.](https://ai.engineer/api/talk-essay-media/0cbdf63c3b098f21b733b4ebc8843e55134958be8446f794e36520d08797dc45)

Logfire shows nested MCP calls and sampling in an execution trace.

Opening the outer model exchange shows the next boundary: the query’s XML-like result comes back as the tool response, and the outer LLM turns it into a human-readable answer. Opening the inner server-agent call reveals the SQL that was generated and executed. Colvin inspects it and says it looks correct. The demonstration ends with both levels visible—the answer delivered to the user and the database query underneath it—so a plausible sentence is not the only evidence available for understanding the result.

## Resources

- [MCP sampling specification](https://modelcontextprotocol.io/specification/2025-03-26/client/sampling) — The March 2025 protocol revision explains how servers request model generations through clients, including capability negotiation and model preferences.
- [Query PyPI download statistics](https://packaging.python.org/en/latest/guides/analyzing-pypi-package-downloads/) — PyPA's guide provides BigQuery setup, the download table schema, example SQL and limitations of download counts.
- [Build an MCP server with Pydantic AI](https://pydantic.dev/docs/ai/mcp/server/) — Current examples show a Pydantic AI agent inside an MCP tool and model requests routed through a sampling-capable client.
- [Pydantic's Q2 2025 update](https://pydantic.dev/articles/q2-2025-summary) — Samuel Colvin reports released MCP sampling support alongside Pydantic AI and Logfire improvements.
- [Official MCP Python SDK](https://github.com/modelcontextprotocol/python-sdk) — Python implementations for MCP clients and servers. The current repository documents SDK v2 and links to the earlier v1 line.
- [Pydantic's company announcement](https://pydantic.dev/articles/company-announcement) — Colvin's February 2023 account of starting the company and funding its next stage.

## Slides

### Whoami

Samuel Colvin created Pydantic in 2017; the Python validation library now sees roughly 350 million downloads per month. Pydantic Labs also makes Logfire and Pydantic AI, and Colvin co-maintains the MCP Python SDK.

[0:18](https://www.youtube.com/watch?v=bmWZk9vTze0&t=18s) [Samuel Colvin introduces Pydantic, Pydantic Logfire and Pydantic AI.](https://assets.ai.engineer/sites/aiecode2025/sha256/eeaaf9950022368a93ae5ee9d46e7a7359a1ba072b0d3f8714c10a7cf2ded17f.webp)

## whoami

Samuel Colvin — creator of Pydantic

Pydantic:
* Python library for data validation
* Created Pydantic in 2017 — long before Gen AI
* Now downloaded ~350M per month
* Used by all of FAANG
* Used by virtually every GenAI Python library — both provider SDKs and Agent Frameworks

Became a company (Pydantic Labs), backed by Sequoia in 2023, released:
* Pydantic Logfire (developer observability)
* Pydantic AI (agent framework)

Come to our booth for Logfire demo, t-shirts, etc.

(I'm also a co-maintainer of the MCP python SDK)

Interface text:
← Previous
2 / 9
Next →
AIE
AI Engineer World's Fair
Graphite
PRESENTED BY Microsoft
Microsoft
aws
smol ai

### What: MCP is all you need

Colvin argues MCP can handle much of the communication between autonomous agents, while acknowledging that the talk’s title overstates its scope.

[1:25](https://www.youtube.com/watch?v=bmWZk9vTze0&t=85s) [The speaker describes using MCP for communication among autonomous agents and qualifies the talk's title.](https://assets.ai.engineer/sites/aiecode2025/sha256/49bac86f98f663ecbf0c2682b8a3d793be47ae1b14e39222f2e6195d10cfbb9a.webp)

# what

MCP is all you need

* Inspired by Jason Liu's talk "Pydantic is all you need" and "Pydantic is still all you need"
* Same idea: that lots of people are over complicating things
* Same unrealistic title — no one is seriously claiming MCP can do everything

What I am saying is:

MCP can do a lot of multi-agent communications - specifically autonomous agents.

Video branding: AIE; AI Engineer World's Fair; Microsoft; aws; smol ai

Interface text: Previous; 3 / 9; Next

### MCP for multi-agent communication

The familiar MCP diagram connects one agent to multiple tools. The agent and tools can be designed independently, then composed through the protocol.

[3:43](https://www.youtube.com/watch?v=bmWZk9vTze0&t=223s) [A diagram shows an agent connected through MCP to three tools.](https://assets.ai.engineer/sites/aiecode2025/sha256/548e35376ae48f29b16d602bebbb2b9be54ad7f8593d8365e734e63b8123b3f7.webp)

# MCP for multi-agent communication

The way most people describe it:

Agent
MCP
tool
tool
tool

AIE
AI Engineer World's Fair
vellum
Microsoft
aws
smol ai

Interface text:
← Previous
5 / 9
Next →

[4:01](https://www.youtube.com/watch?v=bmWZk9vTze0&t=241s) [An agent connects through MCP to three tools.](https://assets.ai.engineer/sites/aiecode2025/sha256/d97cb6e9145f8f6fc689e12233651abcd5a3d3cd1d896b148d20a9998d182d1c.webp)

# MCP for multi-agent communication

The way most people describe it:

Agent
MCP
tool
tool
tool

AIE
AI Engineer World's Fair
vellum
Microsoft
aws
smol ai

Interface text:
← Previous
5 / 9
Next →

### MCP servers as clients

A tool can itself be an agent that connects to more tools. Each agent needs access to an LLM, creating configuration and resource costs.

[4:41](https://www.youtube.com/watch?v=bmWZk9vTze0&t=281s) [An agent connects to tools, including a tool or agent that connects to three further tools.](https://assets.ai.engineer/sites/aiecode2025/sha256/8194864e4cfeaee994adc85bf9fd3107781a0ee466be91033cf558ba4001d5fb.webp)

# MCP for multi-agent communication

But what if servers can be clients:

Agent
MCP
tool
tool
tool/agent
tool
tool
tool

[illegible]oblem.

Interface text:
← Previous
6 / 9
Next →
AIE
AI Engineer World's Fair
daily
Microsoft
aws
smol ai

### Sampling

During a tool call, an MCP server can request an LLM response through the client, then use that response before returning its result. The slide notes that sampling is powerful but not widely supported.

[5:36](https://www.youtube.com/watch?v=bmWZk9vTze0&t=336s) [A sequence diagram shows an MCP server requesting an LLM response through the MCP client during a tool call.](https://assets.ai.engineer/sites/aiecode2025/sha256/d1aca27d80314e2f0c5c8f4696a1b3ff6bc557c0a7e654d3c73d3afd57ec4ba0.webp)

# sampling

Give MCP servers the ability to make requests to LLMs via the client.

(Powerful feature of MCP, but not widely supported*)

LLM | MCP client | MCP server
MCP client → LLM: LLM call
LLM → MCP client: LLM tool call response
MCP client → MCP server: tool call
MCP server → MCP client: sampling "create message"
MCP client → LLM: LLM call
LLM → MCP client: LLM text response
MCP client → MCP server: sampling response
MCP server → MCP client: tool call response
LLM | MCP client | MCP server

Video branding: AIE; AI Engineer World's Fair; daily; Microsoft; aws; smol ai

Interface text: Previous; 7 / 9; Next

[6:36](https://www.youtube.com/watch?v=bmWZk9vTze0&t=396s) [Slide explaining MCP sampling, with a sequence diagram showing requests between an LLM, MCP client and MCP server.](https://assets.ai.engineer/sites/aiecode2025/sha256/c2d442ef450fc00b09f543f73ecce9bf74a2f47a7e1091a102435845c568f9a3.webp)

# sampling

Give MCP servers the ability to make requests to LLMs via the client.

(Powerful feature of MCP, but not widely supported*)

LLM | MCP client | MCP server
MCP client → LLM: LLM call
LLM → MCP client: LLM tool call response
MCP client → MCP server: tool call
MCP server → MCP client: sampling "create message"
MCP client → LLM: LLM call
LLM → MCP client: LLM text response
MCP client → MCP server: sampling response
MCP server → MCP client: tool call response
LLM | MCP client | MCP server

Interface text: Previous; 7 / 9; Next

### Library research example

A simplified research agent connects to a PyPI MCP server. The server uses sampling to turn a natural-language question into SQL; the example asks how often Pydantic was downloaded this year.

[7:06](https://www.youtube.com/watch?v=bmWZk9vTze0&t=426s) [Slide describing a library research agent and showing client code for a PyPI MCP server.](https://assets.ai.engineer/sites/aiecode2025/sha256/4add15e77d51482e936b033be718b51b8319b2f2eca5b8927dfd8e6e53691d0a.webp)

# Example

Library research tool.

* connects to pypi MCP server with natural language query
* MCP server uses sampling to convert query into SQL, runs sql.

from pydantic_ai import Agent
from pydantic_ai.mcp import MCPServerStdio

...

server = MCPServerStdio(command='uv', args=['run', 'pypi_mcp_server.py'])
libs_agent = Agent(
    'openai:gpt-4o',
    mcp_servers=[server],
    instructions='your job is to help the user research software libraries and packages using the tools provided',
)

async def main():
    async with libs_agent.run_mcp_servers():
        result = await libs_agent.run('How many times has pydantic been downloaded this year')
    print(result.output)

Interface text:
← Previous
8 / 9
Next →

### PyPI server setup

The PyPI server connects to BigQuery’s public download dataset and configures a Pydantic AI agent with an MCP context and two retries.

[7:42](https://www.youtube.com/watch?v=bmWZk9vTze0&t=462s) [Editor showing the PyPI server's BigQuery client, dependencies and agent configuration.](https://assets.ai.engineer/sites/aiecode2025/sha256/ff6260f8db113b0bb0fe0863bf9341e5750751a29ef658aea6876fb75fab6332.webp)

logfire.configure()
logfire.configure(service_name='mcp-server')
logfire.instrument_mcp()
logfire.instrument_pydantic_ai()

table_name = 'bigquery-public-data.pypi.file_downloads'
client = bigquery.Client()

@dataclass
class Deps:
    mcp_context: Context[ServerSession, None]

pypi_agent = Agent(
    retries=2,
    deps_type=Deps,
    system_prompt=f"""
Your job is to help users analyze downloads of python packages.

Interface text:
Zed Preview  File  Edit  Selection  View  Go  Window  Help
2025-06-ai-engineer-mcp
README.md  settings.json  ~/.config/zed  slides.template.html  libs_mcp_client.py  pyproject.toml  pypi_mcp_server.py
pypi_mcp_server.py
Samuel Colvin, 7 hours ago
14:20  Python 3.13.0 (venv)  Python
Wed 4 Jun 14:08

### PyPI server code walkthrough

The PyPI agent uses a BigQuery public dataset to analyze package downloads. Its system prompt supplies the table schema and asks for SQL; the agent also receives an MCP context dependency and allows two retries.

[7:48](https://www.youtube.com/watch?v=bmWZk9vTze0&t=468s) [Editor showing instructions to convert a user query into BigQuery SQL and part of the table schema.](https://assets.ai.engineer/sites/aiecode2025/sha256/02c19fb04682c7fe6ae0bbf3aa0a5b1e06dad62e2951247b5c52d38b489a530e.webp)

Your job is to help users analyze downloads of python packages.

Convert the user's query into a BigQuery SQL query against the `{table_name}`
table which has the following schema:

```sql
CREATE TABLE {table_name} (
    timestamp TIMESTAMP,
    country_code STRING, -- two letter ISO country code
    url STRING,
    project STRING,
    file STRUCT<
        filename STRING,
        project STRING,
        version STRING,
        type STRING
    >,
    details STRUCT<
        installer STRUCT<
            name STRING,

Interface text:
Zed Preview  File  Edit  Selection  View  Go  Window  Help
2025-06-ai-engineer-mcp
README.md  settings.json  ~/.config/zed  slides.template.html  libs_mcp_client.py  pyproject.toml  pypi_mcp_server.py
14:20  Python 3.13.0 (venv)  Python
Wed 4 Jun 14:08

[8:00](https://www.youtube.com/watch?v=bmWZk9vTze0&t=480s) [Python code defines an MCP context dependency and configures a PyPI download analysis agent.](https://assets.ai.engineer/sites/aiecode2025/sha256/09c48a664bc78def6f897d04724d1ab18f1c56e78fbd153111ad40ed2791a9bc.webp)

logfire.instrument_mcp()
logfire.instrument_pydantic_ai()

table_name = 'bigquery-public-data.pypi.file_downloads'
client = bigquery.Client()

@dataclass
class Deps:
    mcp_context: Context[ServerSession, None]

pypi_agent = Agent(
    retries=2,
    deps_type=Deps,
    system_prompt=f"""
Your job is to help users analyze downloads of python packages.

Convert the user's query into a BigQuery SQL query against the `{table_name}`

Interface text:
Zed Preview  File  Edit  Selection  View  Go  Window  Help
2025-06-ai-engineer-mcp
README.md  settings.json  ~/.config/zed  slides.template.html  libs_mcp_client.py  pyproject.toml  pypi_mcp_server.py
23:1 (4 lines, 70 characters)  Python 3.13.0 (venv)  Python
Wed 4 Jun 14:08

### Agent prompt and schema

The PyPI agent receives an MCP context, allows two retries, and uses a system prompt with the table schema to turn package-download questions into BigQuery SQL.

[8:09](https://www.youtube.com/watch?v=bmWZk9vTze0&t=489s) [Python code instructs an agent to generate BigQuery SQL and begins the table schema.](https://assets.ai.engineer/sites/aiecode2025/sha256/0fe41494cbc88e568b94f0d4ad6021bfb182c55f4c52a446ddc26d6ac5ccd346.webp)

@dataclass
class Deps:
    mcp_context: Context[ServerSession, None]

pypi_agent = Agent(
    retries=2,
    deps_type=Deps,
    system_prompt=f"""
Your job is to help users analyze downloads of python packages.

Convert the user's query into a BigQuery SQL query against the `{table_name}`
table which has the following schema:

```sql
CREATE TABLE {table_name} (
    timestamp TIMESTAMP,
    country_code STRING, -- two letter ISO country code
    url STRING,
    project STRING[illegible]

Interface text:
Zed Preview
File  Edit  Selection  View  Go  Window  Help
2025-06-ai-engineer-mcp
README.md  settings.json  slides.template.html  libs_mcp_client.py  pyproject.toml  pypi_mcp_server.py
28:1  Python 3.13.0 (venv)  Python
Wed 4 Jun 14:08

### SQL output validator

The output validator strips Markdown code fences, checks for the required table name, and runs the SQL. A BadRequest triggers ModelRetry so the agent can try again.

[8:21](https://www.youtube.com/watch?v=bmWZk9vTze0&t=501s) [A Python output validator strips a code fence, checks the table, runs a query and retries invalid queries.](https://assets.ai.engineer/sites/aiecode2025/sha256/7e83ad29bbbb5113dda8190dff96ed70ec82100eb53a372cec8bbca5040110a2.webp)

@pypi_agent.output_validator
async def run_query(ctx: RunContext[Deps], sql: str) -> str:
    # remove "```sql ... ```"
    m = re.search(r'```\w*\n(.*?)```', sql, flags=re.S)
    if m:
        sql = m.group(1).strip()

    logfire.info('running {sql}', sql=sql)
    await ctx.deps.mcp_context.log('info', 'running query')
    if f'from `{table_name}`' not in sql.lower():
        raise ModelRetry(f'Query must be against the `{table_name}` table')
    try:
        query_job = client.query(sql)
        rows = query_job.result()
    except BadRequest as e:
        await ctx.deps.mcp_context.log('warning', 'query error retrying')
        raise ModelRetry(f'Invalid query: {e}') from e
    await ctx.deps.mcp_context.log('info', 'query successful')

Interface text:
Zed Preview
File  Edit  Selection  View  Go  Window  Help
2025-06-ai-engineer-mcp
README.md  settings.json  ~/.config/zed  slides.template.html  libs_mcp_client.py  pyproject.toml  pypi_mcp_server.py
28:1  Python 3.13.0 (venv)  Python
Wed 4 Jun 14:09

### Query results and retries

The validator runs the SQL and raises ModelRetry on a bad request, prompting the LLM to try again. Successful rows become dictionaries, then XML.

[8:39](https://www.youtube.com/watch?v=bmWZk9vTze0&t=519s) [Python code runs a query, retries errors and formats result rows as XML.](https://assets.ai.engineer/sites/aiecode2025/sha256/4bf9d62c2ceb6436aebf234326e3e84f03981664ebef853ff2aa2dd4c1c6c43c.webp)

if m:
    sql = m.group(1).strip()

logfire.info('running {sql}', sql=sql)
await ctx.deps.mcp_context.log('info', 'running query')
if f'from `{table_name}`' not in sql.lower():
    raise ModelRetry(f'Query must be against the `{table_name}` table')
try:
    query_job = client.query(sql)
    rows = query_job.result()
except BadRequest as e:
    await ctx.deps.mcp_context.log('warning', 'query error retrying')
    raise ModelRetry(f'Invalid query: {e}') from e
await ctx.deps.mcp_context.log('info', 'query successful')
data = [dict(row) for row in rows]  # type: ignore
return format_as_xml(data, item_tag='row', include_root_tag=False)

mcp = FastMCP('PyPI query', log_level='WARNING')

Interface text:
Zed Preview
File Edit Selection View Go Window Help
Wed 4 Jun 14:09
2025-06-ai-engineer-mcp
README.md | settings.json ~/.config/zed | slides.template.html | libs_mcp_client.py | pyproject.toml | pypi_mcp_server.py
pypi_mcp_server.py › async def run_query
Samuel Colvin, 6 hours ago
135:55 | Python 3.13.0 (venv) | Python

### MCP context and query logging

The validator accesses the MCP context through a typed dependency and uses it to log query progress, errors, and success.

[9:02](https://www.youtube.com/watch?v=bmWZk9vTze0&t=542s) [Python validator code highlights a call that logs query progress through the MCP context.](https://assets.ai.engineer/sites/aiecode2025/sha256/479c2154ac56d118a38c45aaf3f75085b8ee28178cfcd7fd1087d1575d90bb26.webp)

async def run_query(ctx: RunContext[Deps], sql: str) -> str:
    # remove "```sql... ```"
    m = re.search(r'```\w*\n(.*?)```', sql, flags=re.S)
    if m:
        sql = m.group(1).strip()

    logfire.info('running {sql}', sql=sql)
    await ctx.deps.mcp_context.log('info', 'running query')
    if f'from `{table_name}`' not in sql.lower():
        raise ModelRetry(f'Query must be against the `{table_name}` table')
    try:
        query_job = client.query(sql)
        rows = query_job.result()
    except BadRequest as e:
        await ctx.deps.mcp_context.log('warning', 'query error retrying')
        raise ModelRetry(f'Invalid query: {e}') from e
    await ctx.deps.mcp_context.log('info', 'query successful')
    data = [dict(row) for row in rows]  # type: ignore
    return format_as_xml(data, item_tag='row', include_root_tag=False)

Interface text:
Zed Preview
File  Edit  Selection  View  Go  Window  Help
2025-06-ai-engineer-mcp
README.md  settings.json  ~/.config/zed  slides.template.html  libs_mcp_client.py  pyproject.toml  pypi_mcp_server.py
pypi_mcp_server.py › async def run_query
Samuel Colvin, 3 hours ago
127:11 (24 characters)  Python 3.13.0 (venv)  Python
Wed 4 Jun 14:09

[9:14](https://www.youtube.com/watch?v=bmWZk9vTze0&t=554s) [Python editor showing imports, BigQuery setup, and a dependency class containing an MCP context.](https://assets.ai.engineer/sites/aiecode2025/sha256/9336cc7e129552db3b78bb568ebb9be15a45f163fdd417bee6ce5d93ed25685f.webp)

from google.cloud import bigquery
from mcp import ServerSession
from mcp.server.fastmcp import Context, FastMCP
from pydantic_ai import Agent, ModelRetry, RunContext, format_as_xml
from pydantic_ai.models.mcp_sampling import MCPSamplingModel

logfire.configure()
logfire.configure(service_name='mcp-server')
logfire.instrument_mcp()
logfire.instrument_pydantic_ai()

table_name = 'bigquery-public-data.pypi.file_downloads'
client = bigquery.Client()

@dataclass
class Deps:
    mcp_context: Context[ServerSession, None]

Interface text:
Zed Preview
File  Edit  Selection  View  Go  Window  Help
2025-06-ai-engineer-mcp
README.md  settings.json  ~/.config/zed  slides.template.html  libs_mcp_client.py  pyproject.toml  pypi_mcp_server.py
pypi_mcp_server.py › class Deps
Samuel Colvin, 3 hours ago
26:25 (7 characters)  Python 3.13.0 (venv)  Python
Wed 4 Jun 14:10

### Query validation and logging

The agent validator uses the MCP context to send query updates to the client, checks the target table, and requests a retry when a query is invalid.

[9:25](https://www.youtube.com/watch?v=bmWZk9vTze0&t=565s) [Python editor showing a query validator that logs status, checks the table, runs a query, and retries on errors.](https://assets.ai.engineer/sites/aiecode2025/sha256/cd385a66c4f0adcec3596fbcce755edfd9e5fbb02d9f8852db546d146648faec.webp)

@pypi_agent.output_validator
async def run_query(ctx: RunContext[Deps], sql: str) -> str:
    # remove "```sql...```"
    m = re.search(r'```\w*\n(.*?)```', sql, flags=re.S)
    if m:
        sql = m.group(1).strip()

    logfire.info('running {sql}', sql=sql)
    await ctx.deps.mcp_context.log('info', 'running query')
    if f'from `{table_name}`' not in sql.lower():
        raise ModelRetry(f'Query must be against the `{table_name}` table')
    try:
        query_job = client.query(sql)
        rows = query_job.result()
    except BadRequest as e:
        await ctx.deps.mcp_context.log('warning', 'query error retrying')
        raise ModelRetry(f'Invalid query: {e}') from e
    await ctx.deps.mcp_context.log('info', 'query successful')
    data = [dict(row) for row in rows]  # type: ignore

Interface text:
Zed Preview
File Edit Selection View Go Window Help
2025-06-ai-engineer-mcp
README.md | settings.json ~/.config/zed | slides.template.html | libs_mcp_client.py | pyproject.toml | pypi_mcp_server.py
pypi_mcp_server.py > class Deps
26:25 (7 characters) | Python 3.13.0 (venv) | Python
Wed 4 Jun 14:10

### MCP log method

The validator calls the MCP context’s log method to send status messages to the client while the query is still running. The tooltip shows the method’s levels and signature.

[9:40](https://www.youtube.com/watch?v=bmWZk9vTze0&t=580s) [Editor tooltip showing the MCP context log method signature over the query validator.](https://assets.ai.engineer/sites/aiecode2025/sha256/fc5b6ecc0653a8370caf842e4c68d5d34e849521ec50c9199f98433d97cdf1ff.webp)

@pypi_agent.output_validator
async def run_query(ctx: RunContext[Deps], sql: str) -> str:
    # remove "```sql...```"
    m = re.search(r'```\w*\n(.*?)```', sql, flags=re.S)
    if m:
        sql = m.group(1).strip()

    logfire.info('running {sql}', sql=sql)
    await ctx.deps.mcp_context.log('info', 'running query')
    if f'from `{table_name}`' n[illegible]
        raise ModelRetry(f'Quer[illegible]
    try:
        query_job = client.quer[illegible]
        rows = query_job.result[illegible]
    except BadRequest as e:
        await ctx.deps.mcp_cont[illegible]
        raise ModelRetry(f'Inva[illegible]
    await ctx.deps.mcp_context.[illegible]
    data = [dict(row) for row i[illegible]

Tooltip:
(method) def log(
    level: Literal['debug', 'info', 'warning', 'error'],
    message: str,
    *,
    logger_name: str | None = None
) -> CoroutineType[Any, Any, None]

Send a log message to the client.

Args:
    level: Log level (debug, info, warning, error)

Interface text:
Zed Preview  File  Edit  Selection  View  Go  Window  Help
Wed 4 Jun 14:10
2025-06-ai-engineer-mcp
README.md  settings.json  ~/.config/zed  slides.template.html  libs_mcp_client.py  pyproject.toml  pypi_mcp_server.py
pypi_mcp_server.py > class Deps
Python 3.13.0 (venv)  Python

### Query results and server setup

The query validator checks for the required table, executes the SQL and logs its status, then converts the results to XML. The code also initializes a FastMCP server.

[10:38](https://www.youtube.com/watch?v=bmWZk9vTze0&t=638s) [Python editor showing query results converted to XML and a FastMCP server initialized.](https://assets.ai.engineer/sites/aiecode2025/sha256/5abade87d801fb26786a9833494a9e2f386c076addd78ddacbb8ea9c51c0a20a.webp)

        sql = m.group(1).strip()

    logfire.info('running {sql}', sql=sql)
    await ctx.deps.mcp_context.log('info', 'running query')
    if f'from `{table_name}`' not in sql.lower():
        raise ModelRetry(f'Query must be against the `{table_name}` table')
    try:
        query_job = client.query(sql)
        rows = query_job.result()
    except BadRequest as e:
        await ctx.deps.mcp_context.log('warning', 'query error retrying')
        raise ModelRetry(f'Invalid query: {e}') from e
    await ctx.deps.mcp_context.log('info', 'query successful')
    data = [dict(row) for row in rows]  # type: ignore
    return format_as_xml(data, item_tag='row', include_root_tag=False)

mcp = FastMCP('PyPI query', log_level='WARNING')

Interface text:
Zed Preview
File Edit Selection View Go Window Help
2025-06-ai-engineer-mcp
README.md
settings.json ~/.config/zed
slides.template.html
libs_mcp_client.py
pyproject.toml
pypi_mcp_server.py
pypi_mcp_server.py › class Deps
Sign in
Wed 4 Jun 14:11
26:25 (7 characters) Python 3.13.0 (venv) Python

### PyPI downloads tool

FastMCP registers a PyPI downloads tool whose docstring becomes its description for the LLM. The tool passes the user's question to an agent using an MCP sampling model.

[11:01](https://www.youtube.com/watch?v=bmWZk9vTze0&t=661s) [Python editor showing a FastMCP tool that passes a question to an agent using an MCP sampling model.](https://assets.ai.engineer/sites/aiecode2025/sha256/573a1ca737d33d060163057bd00890e73e114a77961033c083a4bed966e133a1.webp)

    data = [dict(row) for row in rows]  # type: ignore
    return format_as_xml(data, item_tag='row', include_root_tag=False)

mcp = FastMCP('PyPI query', log_level='WARNING')

@mcp.tool()
async def pypi_downloads(question: str, ctx: Context[ServerSession, None]) -> str:
    """Analyze downloads of packages from the Python package index PyPI to answer questions about package downloads."""
    result = await pypi_agent.run(question, model=MCPSamplingModel(session=ctx.session), deps=Deps(ctx))
    return result.output

if __name__ == '__main__':
    mcp.run()
    # from devtools import debug

Interface text:
Zed Preview
File Edit Selection View Go Window Help
2025-06-ai-engineer-mcp
README.md  settings.json ~/.config/zed  slides.template.html  libs_mcp_client.py  pyproject.toml  pypi_mcp_server.py
pypi_mcp_server.py  async def pypi_downloads
Sign in
Wed 4 Jun 14:11
146:13 (107 characters)  Python 3.13.0 (venv)  Python

### PyPI query execution

The query code checks for the named table, runs the SQL, raises a retry on a bad request, and formats the returned rows as XML.

[11:28](https://www.youtube.com/watch?v=bmWZk9vTze0&t=688s) [Code validates and runs a query, handles errors, and formats rows as XML.](https://assets.ai.engineer/sites/aiecode2025/sha256/eb0fef279071a54707bc614924e520b1381efd305ccca109ec3ef91e2f2f2ab4.webp)

    await ctx.deps.mcp_context.log('info', 'running query')
    if f'from `{table_name}`' not in sql.lower():
        raise ModelRetry(f'Query must be against the `{table_name}` table')
    try:
        query_job = client.query(sql)
        rows = query_job.result()
    except BadRequest as e:
        await ctx.deps.mcp_context.log('warning', 'query error retrying')
        raise ModelRetry(f'Invalid query: {e}') from e
    await ctx.deps.mcp_context.log('info', 'query successful')
    data = [dict(row) for row in rows]  # type: ignore
    return format_as_xml(data, item_tag='row', include_root_tag=False)

mcp = FastMCP('PyPI query', log_level='WARNING')

@mcp.tool()
async def pypi_downloads(question: str, ctx: Context[ServerSession, None]) -> str:

Interface text:
Zed Preview
File Edit Selection View Go Window Help
2025-06-ai-engineer-mcp
README.md  settings.json  ~/.config/zed  slides.template.html  libs_mcp_client.py  pyproject.toml  pypi_mcp_server.py
pypi_mcp_server.py > async def pypi_downloads
Sign in
Wed 4 Jun 14:12
147:39  Python 3.13.0 (venv)  Python

### PyPI downloads tool

The PyPI tool runs an agent with an MCP sampling model and returns its output. The speaker says inference inside the tool keeps query instructions out of the main agent’s context.

[11:49](https://www.youtube.com/watch?v=bmWZk9vTze0&t=709s) [Code registers a PyPI downloads tool and runs an agent using an MCP sampling model.](https://assets.ai.engineer/sites/aiecode2025/sha256/a0a2ee47f980b08dc788faee6f9ce3b9dc84955e4ba604d71b7e812c5ebd7c1c.webp)

data = [dict(row) for row in rows]  # type: ignore
return format_as_xml(data, item_tag='row', include_root_tag=False)

mcp = FastMCP('PyPI query', log_level='WARNING')

@mcp.tool()
async def pypi_downloads(question: str, ctx: Context[ServerSession, None]) -> str:
    """Analyze downloads of packages from the Python package index PyPI to answer questions
    about package downloads."""
    result = await pypi_agent.run(question, model=MCPSamplingModel(session=ctx.session),
    deps=Deps(ctx))
    return result.output

if __name__ == '__main__':
    [illegible]run()
    [illegible]om devtools import debug

Interface text:
Zed Preview  File  Edit  Selection  View  Go  Window  Help
2025-06-ai-engineer-mcp
README.md  settings.json  slides.template.html  libs_mcp_client.py  pyproject.toml  pypi_mcp_server.py
pypi_mcp_server.py › async def pypi_downloads
Wed 4 Jun 14:12
AIE
AI Engineer World's Fair
vellum
Microsoft
aws
smol ai

### Running the MCP server

The tool returns the agent’s output, then the script runs the MCP server. The speaker notes that the server uses standard I/O by default.

[12:21](https://www.youtube.com/watch?v=bmWZk9vTze0&t=741s) [Code returns the agent output and runs the MCP server from the main entry point.](https://assets.ai.engineer/sites/aiecode2025/sha256/e9ed03b61acb06b6450c17a82002a819cb96ced9f9c68cddf7ab3ba8cd86be93.webp)

    """Analyze downloads of packages from the Python package index PyPI to answer questions about package downloads."""
    result = await pypi_agent.run(question, model=MCPSamplingModel(session=ctx.session), deps=Deps(ctx))
    return result.output

if __name__ == '__main__':
    mcp.run()
    # from devtools import debug
    # result = pypi_agent.run_sync('How many times has pydantic been downloaded this year?', model='openai:gpt-4o')
    # debug(result.output)

Interface text:
Zed Preview · File · Edit · Selection · View · Go · Window · Help
2025-06-ai-engineer-mcp
README.md · settings.json · slides.template.html · libs_mcp_client.py · pyproject.toml · pypi_mcp_server.py
pypi_mcp_server.py › async def pypi_downloads
Samuel Colvin, 6 hours ago
148:25 (6 characters) · Python 3.13.0 (venv) · Python
AIE · AI Engineer World's Fair · vellum · Microsoft · aws · smol ai

### MCP client configuration

The main application defines an agent that connects to the PyPI MCP server over standard I/O. The server runs as a local script, and the agent can call its tools to research software packages.

[12:31](https://www.youtube.com/watch?v=bmWZk9vTze0&t=751s) [Code defines a log handler and connects an agent to a local MCP server.](https://assets.ai.engineer/sites/aiecode2025/sha256/de1c0327d850cb9529ed490bd1f6ca8ca7b0b843c478a35f94e7f072304a34b6.webp)

async def log_handler(params: LoggingMessageNotificationParams):
    print(f'{params.level}: {params.data}')

server = MCPServerStdio(command='uv', args=['run', 'pypi_mcp_server.py'],
    log_handler=log_handler)
libs_agent = Agent(
    'openai:gpt-4o',
    mcp_servers=[server],
    instructions='your job is to help the user research software libraries and packages using the tools provided',
)

@libs_agent.system_prompt
def add_date():
    [illegible]rn f'Today is {date.today():%Y-%m-%d}'

Interface text:
Zed Preview
File Edit Selection View Go Window Help
Wed 4 Jun 14:13
2025-06-ai-engineer-mcp
README.md
settings.json
slides.template.html
libs_mcp_client.py
pyproject.toml
pypi_mcp_server.py
libs_mcp_client.py › async def log_handler
Samuel Colvin, 3 hours ago
15:44
Python 3.13.0 (venv)
Python
AIE
AI Engineer World's Fair
vellum
Microsoft
aws
smol ai

### MCP client agent and PyPI download query

The client connects an agent to the PyPI MCP server, adds the current date to its system prompt, and asks how many times Pydantic has been downloaded this year.

[12:42](https://www.youtube.com/watch?v=bmWZk9vTze0&t=762s) [Code configures the client agent and adds the current date to its system prompt.](https://assets.ai.engineer/sites/aiecode2025/sha256/314a53b57717e2ee754ec6cca8522b622af79bacc31338cdc0275e12dc745dd3.webp)

server = MCPServerStdio(command='uv', args=['run', 'pypi_mcp_server.py'], log_handler=log_handler)
libs_agent = Agent(
    'openai:gpt-4o',
    mcp_servers=[server],
    instructions='your job is to help the user research software libraries and packages using the tools provided',
)

@libs_agent.system_prompt
def add_date():
    return f'Today is {date.today():%Y-%m-%d}'

[illegible]

Interface text:
Zed Preview
2025-06-ai-engineer-mcp
README.md
settings.json
slides.template.html
libs_mcp_client.py
pyproject.toml
pypi_mcp_server.py
AIE
AI Engineer World's Fair
vellum
Microsoft
aws
smol ai

[12:57](https://www.youtube.com/watch?v=bmWZk9vTze0&t=777s) [Python code supplies the current date and asks an agent for Pydantic downloads this year.](https://assets.ai.engineer/sites/aiecode2025/sha256/fd82078251a7823d4fa20217e27999da683ce561df7ac6fc1fb21f0a34725e97.webp)

@libs_agent.system_prompt
def add_date():
    return f'Today is {date.today():%Y-%m-%d}'

async def main():
    async with libs_agent.run_mcp_servers():
        result = await libs_agent.run('How many times has pydantic been downloaded this year')
    print(result.output)

if __name__ == '__main__':
    import asyncio

    asyncio.run(main())

Interface text:
Zed Preview
2025-06-ai-engineer-mcp
README.md
settings.json
slides.template.html
libs_mcp_client.py
pyproject.toml
pypi_mcp_server.py
Samuel Colvin, 6 hours ago
AIE
AI Engineer World's Fair
vellum
Microsoft
aws
smol ai

### Nested MCP sampling trace

A run from before the talk shows the outer agent calling GPT-4o, which selects pypi_downloads. The nested trace follows an MCP sampling request back to the client, then shows a BigQuery count operation.

[13:33](https://www.youtube.com/watch?v=bmWZk9vTze0&t=813s) [A zoomed trace shows nested tool calls, MCP sampling, and database operations.](https://assets.ai.engineer/sites/aiecode2025/sha256/6598267b028aa3c9935466ed346c4de9c055b16aaf6e9ea9c7066cc1ebaee023.webp)

[partially obscured] libs_agent run <ongoing?>
mcp-client  MCP request: initialize
mcp-client  16 libs_agent run <ongoing?>
mcp-client  MCP request: initialize
mcp-client  20 libs_agent run
  mcp-client  1 MCP request: tools/list
    mcp-server  MCP server handle request: tools/list
  mcp-client  chat gpt-4o
  mcp-client  1 MCP request: tools/list
    mcp-server  MCP server handle request: tools/list
  mcp-client  11 running 1 tool
    mcp-client  10 running tool: pypi_downloads
      mcp-client  9 MCP request: tools/call pypi_downloads
        mcp-server  8 MCP server handle request: tools/call
          mcp-server  7 pypi_agent run
            mcp-server  3 chat mcp-sampling
              mcp-server  2 MCP request: sampling/createMessage
                mcp-client  1 MCP client handle request: sampling/createMessage
                  mcp-client  chat gpt-4o
            mcp-server  running SELECT COUNT(*) AS num_downloads FROM `bigquery-public-data....project = 'pydantic' AND EXTRACT(YEAR FROM timesta[illegible]
            mcp-server  BigQuery.job.begin
            BigQuery.getQueryResults
[partially obscured] MCP request: tools/list
[partially obscured] MCP server handle request: tools/list

Interface text:
Chrome  File  Edit  View  History  Bookmarks  Profiles  Tab  Window  Help
MCP is all you need
Live · samuelcolvin/docs-exa…
MCP sampling by samuelcolv…
logfire-us.pydantic.dev/samuelcolvin/docs-examples
Gmail  Calendar  Google Keep  Pydantic deck  samuelcolvin  Pydantic Org  platform  pydantic-ai  GitHub  Linear  Notion  CF discord  Website Analytics…  All Bookmarks
Wed 4 Jun 14:16

## Full transcript

[0:00](https://www.youtube.com/watch?v=bmWZk9vTze0&t=0s) [upbeat music] So yeah, I'm talking about, uh, MCP Is All You Need.

[0:17](https://www.youtube.com/watch?v=bmWZk9vTze0&t=17s) A bit about who I am before we get started. I'm best known as the creator of Pydantic, uh, data validation library for Python that is, uh, fairly ubiquitous, downloaded about three hundred and sixty million times a month.

[0:30](https://www.youtube.com/watch?v=bmWZk9vTze0&t=30s) So someone pointed out to me that's like a hundred and forty times a second. Uh, Pydantic is used in general Python development everywhere, but also in GenAI. So it's used in all of the SDKs and agent frameworks in Python, basically.

[0:45](https://www.youtube.com/watch?v=bmWZk9vTze0&t=45s) Uh, Pydantic became a company, uh, uh, beginning of '23, and we have, uh, built two things beyond Pydantic since then. Pydantic AI, uh, an agent framework for Python built on the same principles as Pydantic.

[0:59](https://www.youtube.com/watch?v=bmWZk9vTze0&t=59s) Um, and Pydantic Logfire observability platform, um, which is our-- which is the commercial part of what we do. Um, I'm also a somewhat inactive co-maintainer of the MCP Python SDK.

[1:14](https://www.youtube.com/watch?v=bmWZk9vTze0&t=74s) Um, so MCP Is All You Need is obviously, uh, a play on Jason Liu's talks, Pydantic Is All You Need, that he gave [REDACTED:username] AI Engineer, I think first of all nearly two years ago, and then the second one, Pydantic Is Still All You Need, maybe li- this time last year.

[1:35](https://www.youtube.com/watch?v=bmWZk9vTze0&t=95s) Um, and it has the same basic idea, that people are overcomplicating something that we can use a single tool for, and I guess also similarly, the title is completely unrealistic.

[1:45](https://www.youtube.com/watch?v=bmWZk9vTze0&t=105s) Of course, Pydantic is not all you need, uh, and neither is MCP for everything. But it has the-- we have the-- I think where, where we agree is that there are an awful lot of things that MCP can do and that people are overcomplicating the situation sometimes trying to come up with new ways of doing agent-to-agent communication.

[2:03](https://www.youtube.com/watch?v=bmWZk9vTze0&t=123s) Um, I'm talking here specifically about autonomous agents or code that you're writing. I'm not talking about the, um,

[2:15](https://www.youtube.com/watch?v=bmWZk9vTze0&t=135s) uh, Claude Desktop or Cursor, uh, Zed, Windsurf, et cetera, use case of coding agents. Those were what MCP was originally primarily designed for. Um, I don't know whether or not David Soria Parra would say that, that what we're doing using MCP from Python is, uh, he definitely wouldn't say it's a misuse, but it-- I don't think it, it

[2:35](https://www.youtube.com/watch?v=bmWZk9vTze0&t=155s) was the primary, uh, desi-- uh, use case for, um, for MCP. So two of the, of the primitives of MCP, prompts and resources, probably don't come into this use case that much.

[2:52](https://www.youtube.com/watch?v=bmWZk9vTze0&t=172s) They're very useful or, or should be very useful in the kind of Cursor-type use case. They don't really apply in what we're talking about here. Um,

[3:01](https://www.youtube.com/watch?v=bmWZk9vTze0&t=181s) but tool calling, the third primitive, is extremely useful for what we're trying to do here. Um, tool calling is a lot more complicated than you might [REDACTED:username] first think.

[3:11](https://www.youtube.com/watch?v=bmWZk9vTze0&t=191s) A lot of people say to me about MCP, "Ah, but couldn't it just be, uh, OpenAPI? Why do we need this, uh, custom protocol for doing it?" Um, and there's a number of reasons.

[3:22](https://www.youtube.com/watch?v=bmWZk9vTze0&t=202s) The idea of dynamic tools, the tools that come and go during an agent execution, depending on the state of the server. Logging, so being able to return data to the user

[3:32](https://www.youtube.com/watch?v=bmWZk9vTze0&t=212s) while the tool is still executing. Sampling, which I'm gonna talk about a lot today, perhaps the most confusingly named part of MCP, if not tech in general right now.

[3:43](https://www.youtube.com/watch?v=bmWZk9vTze0&t=223s) Uh, and stuff like tracing, observability. Um, and I, I would also add to that actually, uh, uh, MCP's way of being allowed to operate as effectively a subprocess over standard in and standard out is extremely useful for lots of use cases, and OpenAPI wouldn't, wouldn't solve those problems.

[4:01](https://www.youtube.com/watch?v=bmWZk9vTze0&t=241s) So this is the kind of prototypical image that you will see from lots of people of what, uh, MCP is all about. The idea is we have some agent.

[4:10](https://www.youtube.com/watch?v=bmWZk9vTze0&t=250s) We have any number of different tools that we can connect to that agent, and the point is that, like, the agent doesn't need to be designed with those particular tools in mind, and those tools can be designed without knowing anything about the agent.

[4:21](https://www.youtube.com/watch?v=bmWZk9vTze0&t=261s) And we can just compose the two together in the same way that, uh, I can go and use a browser, and the web application or the website I'm going to doesn't need to know anything about the browser.

[4:30](https://www.youtube.com/watch?v=bmWZk9vTze0&t=270s) I mean, I know we live in a kind of monoculture of browsers now, but, like, [REDACTED:username] least the ideal originally was we could have many different browsers all connecting over the same protocol.

[4:37](https://www.youtube.com/watch?v=bmWZk9vTze0&t=277s) MCP is following the same idea. But it can get more complicated than this. We can have situations like this where, uh, we have tools within our system which are themselves agents and are doing agentic things, need access to an LLM.

[4:53](https://www.youtube.com/watch?v=bmWZk9vTze0&t=293s) They, of course, can then in turn connect to other tools over MCP or, or directly connecting to tools. This, this works nicely. This is elegant, but there's a problem.

[5:03](https://www.youtube.com/watch?v=bmWZk9vTze0&t=303s) Every single agent in our system needs access to an LLM. And so we need to go and configure that. We need to work out resources for that. And if we are,

[5:14](https://www.youtube.com/watch?v=bmWZk9vTze0&t=314s) um, using remote MCP servers, if that remote MCP server needs to, um, use an LLM, well, now it's worried about what the cost is gonna be of doing that.

[5:24](https://www.youtube.com/watch?v=bmWZk9vTze0&t=324s) What, what if the, uh, remote agent that's operating as a tool could effectively piggyback off the, uh, the model that the original agent has access to? That's what sampling gives us.

[5:37](https://www.youtube.com/watch?v=bmWZk9vTze0&t=337s) So as I say, I think sampling is a

[5:40](https://www.youtube.com/watch?v=bmWZk9vTze0&t=340s) somewhat, uh... That's not making that any bigger, unfortunately. Um, is that clear on screen? I may-- Maybe I'll make it bigger like that. Um, sampling is this idea of a, of a way where within MCP, the protocol, the, um, server can effectively make a request back through the client to the LLM.

[6:00](https://www.youtube.com/watch?v=bmWZk9vTze0&t=360s) So in this case, client makes a request, uh, starts some sort of-

[6:05](https://www.youtube.com/watch?v=bmWZk9vTze0&t=365s) Agentic query makes a call to the LLM. LLM comes back and says, "I want to call that particular tool," which is an MCP server. A client takes care of making that call to the MCP server.

[6:15](https://www.youtube.com/watch?v=bmWZk9vTze0&t=375s) The MCP server now says, "Hey, I actually need to be able to use an LLM to answer whatever this question is." So that then gets sent back to the client.

[6:23](https://www.youtube.com/watch?v=bmWZk9vTze0&t=383s) The client proxies that request to the LLM, receives the response from the LLM, sends that, uh, on to the MCP server, and the MCP server then returns, and we can continue on our way.

[6:36](https://www.youtube.com/watch?v=bmWZk9vTze0&t=396s) Um, sampling is very powerful, not that widely supported [REDACTED:username] the moment. Um, I'm gonna demo it today with Pydantic AI, where we have support for sampling... Well, I'll be honest, it's a PR right now, but it will be...

[6:49](https://www.youtube.com/watch?v=bmWZk9vTze0&t=409s) Soon it will be merged. Um, we have support for sampling both as a, uh, as the client, so knowing how to proxy the, those LLM calls, and as a server, basically being able to register use the MCP client as, as the LLM.

[7:06](https://www.youtube.com/watch?v=bmWZk9vTze0&t=426s) So this example is obviously, like all examples, trivialized or simplified to be, to fit on screen. The idea is that we- we're building a, like, research agent which is gonna go and research open source, uh, packages or libraries for us.

[7:21](https://www.youtube.com/watch?v=bmWZk9vTze0&t=441s) And we've implemented one of the many tools that you'd, in fact, need for this, and that tool is, um,

[7:28](https://www.youtube.com/watch?v=bmWZk9vTze0&t=448s) making... Uh, I will switch now to code and show you, uh, the one tool that we have.

[7:36](https://www.youtube.com/watch?v=bmWZk9vTze0&t=456s) Uh, I'm in completely the wrong file. Here we are. Um,

[7:41](https://www.youtube.com/watch?v=bmWZk9vTze0&t=461s) so this tool is querying BigQuery, uh, the BigQuery public data set for, uh, PyPI to get, uh, numbers about the number of downloads of a particular package. So this is, this is pretty standard Pydantic AI, uh, Pydantic AI code.

[7:58](https://www.youtube.com/watch?v=bmWZk9vTze0&t=478s) We've configured Logfire, which I'll show you in a moment. We have the dependencies that the, uh, that the agent has access to while it's running. We said we can do some retries, so if the agent returns, if the LLM returns the wrong data, we can send a retry.

[8:13](https://www.youtube.com/watch?v=bmWZk9vTze0&t=493s) Big system prompt where we give it basically the schema of the table, uh, tell it what to do, give it a few examples, yada, yada. But then we get to this, is the, probably the, the powerful bit.

[8:22](https://www.youtube.com/watch?v=bmWZk9vTze0&t=502s) So as an output validator, we are gonna go and first of all, we're gonna strip out, uh, markdown block quotes from the SQL, um, if they're there. Then we will, uh, check that the table name is right that it's querying against and tell it that it shouldn't if it- it shouldn't.

[8:39](https://www.youtube.com/watch?v=bmWZk9vTze0&t=519s) And then we're gonna go and run the query. And critically, if the query fails, we're gonna, uh, raise ModelRetry within Pydantic AI to go and retry, uh, making the, um,

[8:54](https://www.youtube.com/watch?v=bmWZk9vTze0&t=534s) uh, making the request to the, um, LLM again, saying... asking the LLM to, to, uh, attempt to, to retry this. And what we're... The other thing we're doing throughout this, you'll see here, is we have this context.deps.mcp_context.log.

[9:09](https://www.youtube.com/watch?v=bmWZk9vTze0&t=549s) So you'll see here when we defined deps_type, we said that that was gonna be an instance of this MCP, uh, context, which is what we get when you call the MCP server.

[9:19](https://www.youtube.com/watch?v=bmWZk9vTze0&t=559s) So what we're doing here is we're having a, we're providing a typesafe way within, in this case, um, the agent validator, but it could be in a tool call if you wanted it to be, to access that context.

[9:31](https://www.youtube.com/watch?v=bmWZk9vTze0&t=571s) And so we can see here that we know, uh, um, in the type hint, uh, uh, the, the, the type is, uh, MCP context. And so we have this log function, and we know its signature, and we can go and make this log call.

[9:44](https://www.youtube.com/watch?v=bmWZk9vTze0&t=584s) The point is this is going to return to the client and ultimately to the user watching before the, the thing is completed. So you can get kind of progress updates as we go.

[9:54](https://www.youtube.com/watch?v=bmWZk9vTze0&t=594s) MCP also has a context, a concept of progress, which I'm not using here, but you could imagine that also being valuable. If you knew how far through the query you were, you could show an update in progress.

[10:04](https://www.youtube.com/watch?v=bmWZk9vTze0&t=604s) So the idea, I think the original principle of, uh, logging like this is that you have the, the Cursor-style agent running, and we want to be able to give updates to the user, "Don't worry, I'm still going," before it's finished and exactly what's happening.

[10:18](https://www.youtube.com/watch?v=bmWZk9vTze0&t=618s) But you could also imagine this being useful if you were using MCP. If this was research agent, was, uh, running as a web application, you wanted to show the user what was going on.

[10:27](https://www.youtube.com/watch?v=bmWZk9vTze0&t=627s) This deep research might take, you know, minutes to run. We can give these logs while the tool call is still executing.

[10:34](https://www.youtube.com/watch?v=bmWZk9vTze0&t=634s) And then we're just gonna take the, the output, turn it into a list of dicts, and then format it as XML so you get a nice, uh...

[10:43](https://www.youtube.com/watch?v=bmWZk9vTze0&t=643s) Models are very good [REDACTED:username], [REDACTED:username] basically reviewing XML data, so we basically return whatever the query results are as that kind of XML-ish data, which the LLM will then be good [REDACTED:username], uh, interpreting.

[10:55](https://www.youtube.com/watch?v=bmWZk9vTze0&t=655s) Now we get to the MCP bit. So in this code, we are setting up an MCP server using FastMCP. There are two versions of FastMCP right now, confusingly. This is the one from inside the MCP SDK.

[11:08](https://www.youtube.com/watch?v=bmWZk9vTze0&t=668s) Um, we... The docstring for our function... So we- we're registering one tool here, PyPI downloads, and our docstring from that function will end up becoming the description on the tool that is ultimately fed to the LLM that chooses to go and call it.

[11:24](https://www.youtube.com/watch?v=bmWZk9vTze0&t=684s) Um, and we're going to pass in the user's question. And I think one of the, one of the important things to say here is, of course, you could set this up to generate the SQL within your, uh, central agent.

[11:38](https://www.youtube.com/watch?v=bmWZk9vTze0&t=698s) You could include all of the, um, uh, description of the SQL, the instructions within your, within the, the description of the tool. Uh, models don't seem to like that much data inside a tool description.

[11:51](https://www.youtube.com/watch?v=bmWZk9vTze0&t=711s) But more to the point, we're just gonna blow up the context window of our main agent. If we're gonna ship all of this context on how to make these queries into our main agent, that's just all overhead in all of our calls to that agent, regardless of whether we're gonna call this particular tool.

[12:05](https://www.youtube.com/watch?v=bmWZk9vTze0&t=725s) So doing this kind of thing where we're doing the inference inside a tool is a powerful way of effectively limiting- Uh, the context window of the, of the main running agent.

[12:15](https://www.youtube.com/watch?v=bmWZk9vTze0&t=735s) And then we're just gonna return this output, which will be a string, the value returned from, from here. And we'll just run the, run the MCP server. And by default, the MCP server will run over standard IO.

[12:27](https://www.youtube.com/watch?v=bmWZk9vTze0&t=747s) Um, and then we come to our, our main application. So here we have a definition of our agent, and you see we've defined one MCP server that's just gonna run the, the script I just showed you, the PyPI MCP server.

[12:41](https://www.youtube.com/watch?v=bmWZk9vTze0&t=761s) Um, and so then this agent will act as the client. It has that registered as a tool to be able to call. Uh, we're also gonna set the... give it the current date, uh, so it doesn't, uh, assume it's 20- 2023 as they often do.

[12:56](https://www.youtube.com/watch?v=bmWZk9vTze0&t=776s) Um, and now we can go and ultimately run our main agent, ask it, for example, how many downloads h- Pydantic has had this year. And I'm gonna be brave and run it and see what happens.

[13:07](https://www.youtube.com/watch?v=bmWZk9vTze0&t=787s) Uh, and it has succeeded, and it has, uh, gone and told us, uh, that we had, whatever, 1.6 billion downloads this year. But probably more interesting is to come and look [REDACTED:username] what that looks like in Logfire.

[13:17](https://www.youtube.com/watch?v=bmWZk9vTze0&t=797s) So if you look [REDACTED:username]... Is it gonna come through to Logfire, or are we having a failure here as well? This, I will admit, this is the run from just before, uh, I came on stage, but it, it would look exactly the same.

[13:27](https://www.youtube.com/watch?v=bmWZk9vTze0&t=807s) So I'm not gonna talk too much about observability and how we do, uh, how MCP observability or tracing works within MCP, 'cause I know there's a talk coming up directly after me talking about that.

[13:39](https://www.youtube.com/watch?v=bmWZk9vTze0&t=819s) So think of this as a kind of, uh, spoiler for what's gonna come up. But you can see we, we run our outer agent. It decides to... It calls, uh, uh, GPT-4o, uh, which decides, "Sure enough, I'm gonna go and call this tool."

[13:55](https://www.youtube.com/watch?v=bmWZk9vTze0&t=835s) Uh, it doesn't need to think about generating the SQL. It can just have a natural language description of the query that we're trying to make. We then, um, this is the MCP client, as you can see here.

[14:05](https://www.youtube.com/watch?v=bmWZk9vTze0&t=845s) MCP client then calls into the MCP server, um, makes the... which then, again, runs a different, uh, Pydantic AI, uh, agent, which then makes a call to an LLM, which happens through proxying it through the client.

[14:19](https://www.youtube.com/watch?v=bmWZk9vTze0&t=859s) So that's why you can see the service going client, server, uh, client, server.

[14:26](https://www.youtube.com/watch?v=bmWZk9vTze0&t=866s) Ultimately, if you look [REDACTED:username] the top level, uh, exchange with the model, you'll see here,

[14:32](https://www.youtube.com/watch?v=bmWZk9vTze0&t=872s) yeah, the, the, the out- ultimate output was it, which had the, the return response from running the query was, was this kind of XML-ish data. And then the LLM was able to turn that into a human description of what was going on.

[14:45](https://www.youtube.com/watch?v=bmWZk9vTze0&t=885s) I think the other interesting thing probably is we can go and look in. We should be able to see the actual SQL that was called. So this is the agent call inside, uh, MCP server.

[14:55](https://www.youtube.com/watch?v=bmWZk9vTze0&t=895s) And you can see here the SQL it wrote, and you can confirm that it, it indeed looks correct. Um, I am going to, uh, go on from there and say, um, thank you very much.

[15:08](https://www.youtube.com/watch?v=bmWZk9vTze0&t=908s) Um, we are [REDACTED:username] the booth, the, the Pydantic booth, so if anyone has any questions on this, wants to see this fail in numerous other exciting ways, very happy to, to talk to you.

[15:16](https://www.youtube.com/watch?v=bmWZk9vTze0&t=916s) Yeah, come and say hi. [outro music]
