Skip to main content
Connect a Pydantic AI agent to Civic using native MCP support. Pydantic AI’s MCPServerStreamableHTTP class handles session management and tool discovery automatically.

Prerequisites

  • Python 3.11+
  • A Civic account at nexus.civic.com with a configured toolkit
  • A Civic token and an Anthropic API key

Installation

pip install pydantic-ai anthropic python-dotenv

Environment Variables

CIVIC_URL=https://nexus.civic.com/hub/mcp?profile=your-toolkit
CIVIC_TOKEN=your-civic-token
ANTHROPIC_API_KEY=your-anthropic-key

Get Your Credentials

How to generate a Civic token and configure toolkit URL parameters

Connecting to Civic

Use MCPServerStreamableHTTP to configure the connection, then pass it to your agent via mcp_servers:
import os
import asyncio
from dotenv import load_dotenv
from pydantic_ai import Agent
from pydantic_ai.mcp import MCPServerStreamableHTTP

load_dotenv()

server = MCPServerStreamableHTTP(
    os.environ["CIVIC_URL"],
    headers={"Authorization": f"Bearer {os.environ['CIVIC_TOKEN']}"},
)

agent = Agent("anthropic:claude-sonnet-4-6", mcp_servers=[server])
Use mcp_servers=[server] — not toolsets=[server]. The mcp_servers parameter is the current Pydantic AI API for MCP connections.

Running the Agent

Wrap agent calls with agent.run_mcp_servers() to establish the MCP session:
async def main():
    async with agent.run_mcp_servers():
        result = await agent.run("What events do I have today?")
    print(result.output)

asyncio.run(main())

Production Configuration

For production agents, lock to a specific toolkit using the profile URL parameter:
CIVIC_URL=https://nexus.civic.com/hub/mcp?profile=your-production-toolkit
When a profile is specified, the session is locked by default — the agent cannot switch toolkits or modify its own guardrails.

Reference Implementation

pydantic-ai-reference-implementation-civic

Complete implementation with FastAPI chat UI, streaming responses, and deployment guide

Next Steps

Agent Deployment

Production deployment guide: profile locking, URL params, authentication

Guardrails

Constrain what tools your agent can call

Audit Trail

Query what your agent did via Civic Chat

Get Credentials

Token generation and URL parameter reference