Pydantic AI + Neo4j Integration
Overview
Pydantic AI is a Python framework for building production-ready AI agents. Leveraging Pydantic’s powerful validation and serialization capabilities, it provides type-safe tool definitions, structured outputs, dependency injection, and seamless integration with multiple LLM providers.
Installation:
pip install pydantic-ai neo4j
Key Features:
-
Type-safe tool definitions with automatic schema generation
-
Structured outputs validated by Pydantic models
-
Native MCP support
-
Dependency injection for clean, testable code
Examples
| Notebook | Description |
|---|---|
Walkthrough of Pydantic AI with Neo4j integration, including MCP server setup, custom tool creation, and query execution |
Extension Points
1. MCP Integration
Pydantic AI has native MCP support. MCP servers are passed directly via the toolsets parameter to the agent.
-
Neo4j MCP Server: Leverage the official Neo4j MCP Server for ready-made integration
MCP Authentication
Supported Mechanisms:
✅ HTTP Headers (HTTP/SSE transport) - For remote MCP servers, pass credentials via the headers parameter using Basic or Bearer authentication.
✅ Environment Variables (STDIO transport) - For local MCP servers, credentials can be passed via environment variables at spawn time.
✅ OAuth 2.0 (via FastMCPToolset) - Available through the FastMCP-based client.
Configuration Example (HTTP transport):
credentials = base64.b64encode(
f"{os.environ['NEO4J_USERNAME']}:{os.environ['NEO4J_PASSWORD']}".encode()
).decode()
mcp_server = MCPServerStreamableHTTP(
'http://localhost:80/mcp',
headers={'Authorization': f'Basic {credentials}'},
)