
{"id":163846,"date":"2026-05-11T13:03:46","date_gmt":"2026-05-11T13:03:46","guid":{"rendered":"https:\/\/mycryptomania.com\/?p=163846"},"modified":"2026-05-11T13:03:46","modified_gmt":"2026-05-11T13:03:46","slug":"introducing-agentic-defi-with-defipy-v2","status":"publish","type":"post","link":"https:\/\/mycryptomania.com\/?p=163846","title":{"rendered":"Introducing Agentic DeFi with DeFiPy v2"},"content":{"rendered":"<p><a href=\"http:\/\/defipy.org\/\">DeFiPy v2<\/a> enables proactive reasoning via its <a href=\"https:\/\/www.defipy.org\/twin-concept\/\"><em>State Twin interface<\/em><\/a><em> <\/em>serving as the mathematical cortex of the\u00a0AIIntroduces <a href=\"https:\/\/www.defipy.org\/agentic-primitives\/\">21 new agentic primitives<\/a> built on top of\u00a0v1Includes <a href=\"https:\/\/www.defipy.org\/mcp-demo\/\">MCP integration<\/a> serving as the bridge to agentic\u00a0systems<\/p>\n<h3><strong>From a desktop tool to industrial infrastructure<\/strong><\/h3>\n<p>DeFiPy v1 was built for the human research analyst, a Python library you imported into a notebook to answer questions about DeFi. This was built to ask questions like: <em>Why is this LP position losing money? What\u2019s the impermanent loss at 30% drawdown? How does this V3 fee tier compare to V2 for the same capital?<\/em> One would typically open Jupyter, import defipy, apply core primitives on various scenarios, read the result, apply it to your analysis, and write your\u00a0report.<\/p>\n<p>However, <a href=\"http:\/\/defipy.org\/\">DeFiPy v2<\/a> is built for the agentic era. The same hand-derived AMM math now ships as MCP tools, which is callable by autonomous systems at production scale, utilizing an additional 21 new agentic primitives built on top. The State Twin abstraction lets the same primitive call run against a synthetic recipe, live mainnet state, or a historical block; one shape in and one shape out, just like how an analyst would reason about something for proactive reasoning. An LLM agent can discover the tool schemas, pick a primitive, run it against a twin built from real chain state, and synthesize a proactively reasoned response grounded in the\u00a0math.<\/p>\n<p>The math underneath is the same, however, now the surface around it is different. What v2 ships is <em>the surface that an agentic system can build\u00a0on<\/em>.<\/p>\n<p>DeFiPy v1 first went out in 2024, and today in 2026, v2 has just been released on PyPI. This article introduces the three pieces that make v2 a substrate for agentic DeFi: (1) A<strong>gentic Primitives,<\/strong> (2) <strong>State Twins,<\/strong> and (3) <strong>MCP integration<\/strong>. Each one is its own deeper article in the series ahead; this is the high-level map.<\/p>\n<h3><strong>Three pieces<\/strong><\/h3>\n<h4><strong>1. Agentic primitives\u200a\u2014\u200athe math\u00a0layer<\/strong><\/h4>\n<p>DeFiPy ships <strong>21 stateless, typed primitives <\/strong>across seven categories\u200a\u2014\u200aposition analysis, pool health, risk, execution, optimization, comparison, and portfolio aggregation. Each one answers a specific LP question with exact math derived from the protocol\u2019s invariant; xy = k for V2, concentrated-tick math for V3, weighted geometric mean for Balancer, the \u03b5 \u2194 \u03b4 amplification relation for Stableswap. The same call shape works across all four AMM families.<\/p>\n<p>Most DeFi tools wrap APIs, however DeFiPy ships the math. When something like a notebook, a backtest, an LLM agent\u200a\u2014\u200aasks \u201cis this pool healthy?\u201d the answer comes from hand-derived invariants applied to typed pool state, not from a probabilistic interpretation of cached chain data. Every primitive returns a structured dataclass with named fields, ready to feed into whatever consumes it\u00a0next.<\/p>\n<p>from defipy import CheckPoolHealth<\/p>\n<p>health = CheckPoolHealth().apply(lp)<\/p>\n<p>print(f&#8221;TVL (token0): {health.tvl_in_token0:,.2f}&#8221;)<br \/>print(f&#8221;Fee tier: {health.fee_pips} pips&#8221;)<br \/>print(f&#8221;Current tick: {health.tick_current}&#8221;)<\/p>\n<p>That same lp argument can come from a synthetic recipe, a live chain read, or a custom data source\u200a\u2014\u200athe primitive doesn\u2019t know or care. Which brings us to the second\u00a0piece.<\/p>\n<p><em>A later article in this series goes deeper into the primitive catalog and how each category maps to the LP questions it\u00a0answers.<\/em><\/p>\n<h4><strong>2. State Twin\u200a\u2014\u200athe substrate layer<\/strong><\/h4>\n<p>A State Twin is a <strong>mathematically exact off-chain replica of an AMM pool\u2019s state<\/strong>, built from a typed snapshot. The architecture has three\u00a0parts:<\/p>\n<p><strong>Provider<\/strong>\u200a\u2014\u200aknows about <em>sources<\/em>. MockProvider produces synthetic recipes; LiveProvider pulls live chain state; a custom provider could read from a CSV or a database.<strong>Snapshot<\/strong>\u200a\u2014\u200aa typed dataclass (V2PoolSnapshot, V3PoolSnapshot, etc.) emitted by the provider. Decimal-adjusted reserves, block context, the full pool state in normalized form.<strong>Builder<\/strong>\u200a\u2014\u200aStateTwinBuilder().build(snapshot) dispatches on snapshot type and returns the protocol\u2019s exchange object\u200a\u2014\u200athe lp that every primitive consumes.<\/p>\n<p>The point is the symmetry. Two lines change between synthetic and live; the third\u200a\u2014\u200athe primitive call\u200a\u2014\u200ais identical:<\/p>\n<p># Synthetic recipe &#8211; for tests, notebooks, CI<br \/>provider = MockProvider()<br \/>lp = StateTwinBuilder().build(provider.snapshot(&#8220;eth_dai_v2&#8221;))<\/p>\n<p># Live mainnet state &#8211; for analytics on real pools<br \/>provider = LiveProvider(&#8220;https:\/\/eth-mainnet.g.alchemy.com\/v2\/&lt;key&gt;&#8221;)<br \/>lp = StateTwinBuilder().build(<br \/>provider.snapshot(&#8220;uniswap_v2:0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc&#8221;)<br \/>)<\/p>\n<p># Same primitive call against either lp<br \/>health = CheckPoolHealth().apply(lp)<\/p>\n<p>This is the layer most agentic-DeFi systems are missing. Without it, every reasoning step is bound to chain time\u200a\u2014\u200aevery \u201cwhat if?\u201d requires an RPC roundtrip. With it, state lives in memory, off-chain, replayable. The agent can fork a twin a hundred ways under price scenarios and run the math against each fork before committing to anything.<\/p>\n<p>LiveProvider shipped for Uniswap V2 and V3 in v2.1; Balancer and Stableswap LiveProvider follow in v2.2. Block pinning is automatic, Multicall3 batches V3 reads into one RPC roundtrip, and provider.get_w3() is the read-only escape hatch for callers who want to wire their own signing infrastructure on\u00a0top.<\/p>\n<p><em>Next week\u2019s article goes deep on the State Twin, what it is, why it\u2019s the load-bearing abstraction, and what it makes possible that wasn\u2019t possible\u00a0before.<\/em><\/p>\n<h4><strong>3. MCP integration\u200a\u2014\u200athe bridge to agentic\u00a0systems<\/strong><\/h4>\n<p>The third piece is the bridge from substrate to LLM. defipy.tools emits <a href=\"https:\/\/modelcontextprotocol.io\/\">Model Context Protocol<\/a> (MCP) schemas for <strong>10 curated leaf primitives<\/strong>, and a reference MCP server at python\/mcp\/defipy_mcp_server.py closes the end-to-end loop.<\/p>\n<p>What that means in practice: ask Claude <em>\u201dis the eth_dai_v2 pool healthy? any rug signals?\u201d<\/em> The MCP server dispatches CheckPoolHealth and DetectRugSignals against a twin, the primitives return typed dataclass results, and Claude synthesizes a response that\u2019s correct because <em>the primitives encoded the domain<\/em>\u200a\u2014\u200anot because the LLM hallucinated DeFi math well. The LLM doesn\u2019t need to know how to compute impermanent loss from sqrt-price ratios. It needs to know that AnalyzePosition exists, what its inputs are, and what its result fields mean. The schema does the\u00a0rest.<\/p>\n<p>pip install &#8216;defipy[agentic]&#8217; # composes [chain] + [mcp]<\/p>\n<p>That\u2019s the install for the full stack\u200a\u2014\u200aLiveProvider for chain reads, plus the MCP server SDK for serving primitives as tools to Claude Desktop, Claude Code, or any MCP-compatible client. Equivalent to pip install \u2018defipy[chain,mcp]\u2019 but spelled with\u00a0intent.<\/p>\n<p>DeFiPy itself stays LLM-free. The math layer has zero LLM dependencies and zero network calls at core. MCP is the integration surface\u200a\u2014\u200ait wires the substrate up to consumers that <em>are<\/em> LLMs, without contaminating the substrate with their concerns.<\/p>\n<p><em>A later article in this series develops the trust-layer thesis\u200a\u2014\u200awhy deterministic primitives at the LLM boundary matter, and what\u2019s still\u00a0missing.<\/em><\/p>\n<h3><strong>The architectural shape<\/strong><\/h3>\n<p>Three layers, each with zero dependencies on the layer above. Agentic primitives are the <strong>math layer <\/strong>which is pure analytics, importable in any Python environment. The State Twin is the <strong>substrate layer <\/strong>which are typed snapshots and exchange objects, optional chain reads behind the [chain] install. Finally, MCP integration is the <strong>agentic integration layer<\/strong> which are schemas and a stdio-transport server, behind the [mcp]\u00a0install.<\/p>\n<p>A notebook user installs defipy and gets the math. An analytics consumer adds [chain] and gets live state. An agent runtime adds [mcp] (or [agentic] for the full stack) and gets the substrate exposed as tools. Each consumer gets exactly the surface they need; nothing is\u00a0imposed.<\/p>\n<p>That\u2019s what makes it a substrate. The library has no opinions about who\u2019s consuming it, what they\u2019re consuming it for, or how the result gets used downstream. This is composition by\u00a0design.<\/p>\n<h3><strong>What\u2019s preserved<\/strong><\/h3>\n<p>DeFiPy v1 isn\u2019t deprecated; the 21 primitives all existed in v1.2.0, what v2 added is the surface around them. The legacy monolithic event-driven agents in python\/prod\/agents\/ are preserved as chapter 9 reference material from <em>Hands-On AMMs with Python<\/em>. v1 readers and textbook users don\u2019t lose anything\u200a\u2014\u200av2 is a different architectural pattern for a different consumer, its an extension of v1\u2019s correctness for AI\u00a0utility.<\/p>\n<h3><strong>Where to\u00a0start<\/strong><\/h3>\n<p>pip install &#8216;defipy[agentic]&#8217;<\/p>\n<p>&gt; <em>Note for zsh users (default on macOS):<\/em> wrap the install spec in single quotes so zsh doesn\u2019t try to glob the brackets. Bash users can use either\u00a0form.<\/p>\n<p><strong>Documentation:<\/strong> <a href=\"https:\/\/defipy.org\/\">defipy.org<\/a><strong>State Twin concept:<\/strong> <a href=\"https:\/\/defipy.org\/twin-concept\/\">defipy.org\/twin-concept\/<\/a><strong>LiveProvider docs:<\/strong> <a href=\"https:\/\/defipy.org\/live-provider\/\">defipy.org\/live-provider\/<\/a><strong>Fork-and-evaluate worked example:<\/strong> <a href=\"https:\/\/defipy.org\/fork-evaluate\/\">defipy.org\/fork-evaluate\/<\/a><strong>GitHub: <\/strong><a href=\"https:\/\/github.com\/defipy-devs\/defipy\">github.com\/defipy-devs\/defipy<\/a><strong>Textbook (v1 reference):<\/strong> <a href=\"https:\/\/www.amazon.com\/dp\/B0G3RV5QRB\">DeFiPy: Python SDK for On-Chain Analytics<\/a><\/p>\n<p>If the architectural argument resonates\u200a\u2014\u200aor if you\u2019ve been hitting the chain-reality ceiling without a name for it\u200a\u2014\u200athe docs are the next\u00a0click.<\/p>\n<p>\ud83d\udd17 SPDX-Anchor: <a href=\"https:\/\/anchorregistry.ai\/AR-2026-YdPXB5g\">anchorregistry.ai\/AR-2026-YdPXB5g<\/a><\/p>\n<p><em>DeFiPy is built and maintained by <\/em><a href=\"https:\/\/github.com\/icmoore\"><em>Ian Moore<\/em><\/a><em>. Apache 2.0 licensed. The full v2.1.0 release notes are on <\/em><a href=\"https:\/\/github.com\/defipy-devs\/defipy\/releases\"><em>GitHub<\/em><\/a><em>. For consulting inquiries on integrating DeFiPy into agent runtimes, fund analytics pipelines, or protocol monitoring infrastructure, please reach out via <\/em><a href=\"https:\/\/www.defimind.ai\/\"><em>defimind.ai<\/em><\/a><em>.<\/em><\/p>\n<p><a href=\"https:\/\/medium.com\/coinmonks\/introducing-agentic-defi-with-defipy-v2-4c252038d155\">Introducing Agentic DeFi with DeFiPy v2<\/a> was originally published in <a href=\"https:\/\/medium.com\/coinmonks\">Coinmonks<\/a> on Medium, where people are continuing the conversation by highlighting and responding to this story.<\/p>","protected":false},"excerpt":{"rendered":"<p>DeFiPy v2 enables proactive reasoning via its State Twin interface serving as the mathematical cortex of the\u00a0AIIntroduces 21 new agentic primitives built on top of\u00a0v1Includes MCP integration serving as the bridge to agentic\u00a0systems From a desktop tool to industrial infrastructure DeFiPy v1 was built for the human research analyst, a Python library you imported into [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":163847,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-163846","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-interesting"],"_links":{"self":[{"href":"https:\/\/mycryptomania.com\/index.php?rest_route=\/wp\/v2\/posts\/163846"}],"collection":[{"href":"https:\/\/mycryptomania.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mycryptomania.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/mycryptomania.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=163846"}],"version-history":[{"count":0,"href":"https:\/\/mycryptomania.com\/index.php?rest_route=\/wp\/v2\/posts\/163846\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mycryptomania.com\/index.php?rest_route=\/wp\/v2\/media\/163847"}],"wp:attachment":[{"href":"https:\/\/mycryptomania.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=163846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mycryptomania.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=163846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mycryptomania.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=163846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}