PydanticAI MCP Experiment

by eschmidt42

333 downloads
Not rated
GitHub

About

Minimalist examples to provide your own MCP servers to your local llm models.

Details

Author
eschmidt42
Downloads
333
Categories
Other

- Four transport examples: stdio, HTTP via mcp.run, FastAPI, and Starlette
- Uses pydantic_ai and local Ollama models
- Minimal, self‑contained client/server pairs per example
- Logging integration with Logfire (optional)
- Managed with uv for reproducible environments

The project is managed with uv. After cloning, run uv sync to install dependencies. Then change into any example folder (e.g., examples/0_subprocess) and start the server with python server.py, followed by python client.py in another terminal. The setup uses Logfire for logging, which will prompt for authentication on first run.

PydanticAI MCP Experiment

> Minimalist examples to provide your own MCP servers to your local llm models.

Under examples/ are different configurations providing an MCP client and server using pydantic_ai and local models / ollama to get your hands dirty and tinker.

Setup

This project is uv managed. So to set up run

uv sync

Examples

There are 4 different examples for running a MCP server (locally) via:

Stdio -> examples/0_subprocess
HTTP
mcp.run -> examples/1_http_mcp_run
FastAPI -> examples/2_http_fastapi
Starlette -> examples/3_http_starlette

Each folder contains a client.py and server.py file.

The servers should be started before running the client.py file.

The scripts are set up such that when you change into the respective example folder you can run each simply using

python server.py

and

python client.py

Logging

Since pydantic_ai is used here the natural choice for logging is logfire. Hence upon running the client.py files you will be asked to authenticate with logfire. You can change the backend to something like otel-tui, but I tried and failed to find the prompt and response messages I wanted to see.

References

pydantic + ollama: https://ai.pydantic.dev/models/openai/#example-local-usage
pydantic ai + mcp + "stdio" server: https://ai.pydantic.dev/mcp/client/#mcp-stdio-server
logfire setup: https://logfire.pydantic.dev/docs/#logfire - requires account creation

  • logfire with otel-tui backend: https://ai.pydantic.dev/logfire/#logfire-with-an-alternative-otel-backend -> does not show the prompts and responses (despite them being present in the screenshots in the docs)

No reviews yet — be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.