DeFiPy is a unified Python SDK for agentic DeFi, covering analytics, simulation, and LLM-native tool schemas for autonomous agents. Built with modularity in mind, it exposes DeFi primitives as MCP tools and lets you isolate and extend your analytics by protocol using:
New here? The Quick Start walks through the essential Core Primitives — pool setup, Join, Swap — protocol by protocol. For onchain event access, use LiveProvider, which demonstrates the State Twin concept. The fork-and-evaluate worked example is the canonical v2.1 demonstration of the pattern against live mainnet state.
🤖 Building an agent?Wire DeFiPy primitives into Claude Desktop, Claude Code, or any MCP-compatible client. 10 curated tools ship with schemas; a reference MCP server closes the end-to-end loop.
📊 Running analytics?21 stateless, typed primitives across 9 categories — position analysis, risk, pool health, price scenarios, portfolio aggregation. Every primitive returns a structured dataclass.
📐 Learning the math?Hand-derived AMM math across all four protocols — sqrt-price and ticks for Uniswap V3, weighted pools for Balancer, amplification for Stableswap. Runnable as Jupyter notebooks.
# Ask the primitive: why is this LP position gaining or losing money?
result =AnalyzePosition().apply(
lp,
lp_init_amt=1.0,
entry_x_amt=1000,
entry_y_amt=100000,
)
print(f"Diagnosis: {result.diagnosis}")
print(f"Net PnL: {result.net_pnl:.4f}")
print(f"IL %: {result.il_percentage:.4f}")
print(f"Current val: {result.current_value:.4f}")
MockProvider ships canonical synthetic pools; StateTwinBuilder turns a snapshot into a usable exchange object; any primitive runs against it. The same pattern works against live chain state via LiveProvider — the primitives don’t care where the pool state came from:
from defipy.twin import LiveProvider, StateTwinBuilder
Most Python DeFi libraries wrap chain queries from The Graph. DeFiPy ships the math — hand-derived invariants for four AMM families, exposed as composable typed primitives that an LLM can call directly via Model Context Protocol.
Exact math, not approximations
No textbook IL formulas. Closed-form expressions derived from each protocol’s invariant — xy = k for V2, L = √(xy) with concentrated ticks for V3, weighted geometric mean for Balancer, the ε ↔ δ relation for Stableswap.
Substrate, not agent
DeFiPy is the foundation. It has zero LLM dependencies and zero web3 dependencies in the core install. Build whatever agent you want on top — DeFiMind, LangChain, custom MCP server, your own framework.
Composable typed primitives
Every primitive follows the same interface: stateless construction, computation at .apply(), typed dataclass return. Same call shape from a Jupyter notebook or from Claude calling it as a tool.
Plan, don't execute
No signing keys. Ever. Primitives like OptimalDepositSplit and EvaluateRebalance return structured plans the caller can inspect, serialize, approve, or hand to whatever execution runtime they trust.
Adds Web3Scout and web3.py for live chain integration via LiveProvider. The [book] extra carries the same packages with textbook-chapter-9 framing — either works for either purpose.