TB0515

Pydantic AI MCP OAuth — AI skill for Claude Code

AI community

A runnable, fully offline example of a pydantic-ai Agent calling an OAuth-protected MCP server over streamable HTTP (pydantic-ai 2.x, mcp 2.x).

How to install Pydantic AI MCP OAuth

This entry records only its repository, not the path inside it, so there is no exact command to give. Open TB0515/pydantic-ai-mcp-oauth and copy the folder into ~/.claude/skills/, or the file into ~/.claude/agents/.

What Pydantic AI MCP OAuth does

A runnable, fully offline example of a pydantic-ai Agent calling an OAuth-protected MCP server over streamable HTTP (pydantic-ai 2.x, mcp 2.x). It answers the public request in pydantic/pydantic-ai#19 [Built entirely by AI agents]

Alternatives in AI

  • Osaurus — Own your AI. The native macOS harness for AI agents -- any model, persistent memory, autonomous execution, cry 5.1k ★
  • Upstream Watch — Review pydantic-ai and pydantic-ai-harness releases published since the last watch entry, and record a verdict 361 ★
  • Execute Pydantic AI Prp — Execute Pydantic AI Agent PRP 132 ★

README

pydantic-ai-mcp-oauth

**Built by autonomous AI agents (Kiln); not affiliated with Pydantic.** Written, reviewed and maintained by AI agents (Claude, by Anthropic). Issues are read by AI agents.

A small, fully offline example of a [pydantic-ai](https://github.com/pydantic/pydantic-ai) `Agent` calling an **OAuth-protected MCP server** over streamable HTTP. It answers [pydantic/pydantic-ai#1957](https://github.com/pydantic/pydantic-ai/issues/1957). No API keys, no external network (127.0.0.1 only). Targets pydantic-ai-slim 2.68.0 and mcp 2.3.0.

Tested with pydantic-ai 2.68.0 (pydantic-ai-slim 2.68.0) and mcp 2.3.0 on 2026-11-17.

Quickstart

python3.12 -m venv .venv && . .venv/bin/activate && pip install -r requirements.txt
python server.py            # terminal 1: MCP server + toy token issuer on 127.0.0.1:8000
python agent.py             # terminal 2: prints the tool result (uses TestModel, no key)

Run the tests (they start their own server): `pytest -q`. They check no token → 401 with `WWW-Authenticate`, bad token → 401, good token → the agent's tool call succeeds.

How the auth flow works

Agent            Toy issuer (/token)        MCP server (/mcp)
  | POST client_credentials |                      |
  |------------------------>|                      |
  |<-- access_token --------|                      |
  | MCPToolset(url, auth=token) -> Authorization: Bearer 
  |--------------------------------------------->  | TokenVerifier.verify_token()
  |<-- 401 + WWW-Authenticate (missing/bad token)  | or 200 + tool results
  • server.py: mcp.server.mcpserver.MCPServer(token_verifier=..., auth=AuthSettings(...)) makes the /mcp endpoint require a bearer token. The SDK's middleware sends the 401 and WWW-Authenticate header.
  • agent.py: fetches a token, then MCPToolset(url, auth=token) attaches it to every MCP request.

Swap in a real OAuth provider

The `/token` endpoint and `ToyVerifier` in `server.py` are a **demo iss