DEV Community

fino
fino

Posted on • Originally published at github.com

I built a cross-chain swap SDK for AI agents (MCP-native, handles cold start gas automatically)

I needed to swap 0.003 ETH (on Base) to MYST tokens (on Polygon) for an AI agent I was building. Simple enough, right?

Two hours later, I had manually:

  1. Found the best DEX route on Paraswap
  2. Realized I had no POL on Polygon for gas — so the bridged USDC was stuck
  3. Manually sent POL from another wallet
  4. Set amountOutMin wrong and got sandwiched
  5. Fumbled the slippage config and the tx reverted

There was no single tool that handled all of this. So I built one.


What is AutoSwap?

AutoSwap is a cross-chain swap SDK built specifically for AI agents. It wraps the entire swap pipeline — DEX routing, bridging, native gas resolution, and slippage protection — into a single function call.

from autoswap import swap

result = swap(
    from_token="ETH",
    from_chain="base",
    to_token="MYST",
    to_chain="polygon",
    amount=0.003,
    slippage_max=2.0,
    dry_run=True,  # simulate first — always
)

print(result.route_taken)
# → "ETH→USDC (base) | bridge USDC base→polygon | USDC→MYST (polygon)"

print(f"~{result.amount_out:.4f} MYST")
# → "~47.3281 MYST"
Enter fullscreen mode Exit fullscreen mode

That's it. One call.


The Problems AutoSwap Solves

1. The Cold Start Gas Problem

This is the most under-discussed problem in cross-chain DeFi.

You bridge USDC from Base to Polygon. It arrives. Now you need to swap it to MYST. But you have zero POL on Polygon. Your USDC is stuck, and you can't do anything without gas.

AutoSwap detects this automatically and resolves it via a gasless relay before executing your swap. You never have to think about it.

2. amountOutMin=0 (The MEV Sandwich Attacker's Best Friend)

Every tutorial, every StackOverflow answer, every quick script I've seen sets amountOutMin=0 "for simplicity." This is the #1 way to get sandwiched.

AutoSwap never sets amountOutMin=0. Period. Slippage is calculated against real liquidity data from the DEX. The hard maximum is 50% — any route exceeding that is rejected outright.

3. Best Route Selection

AutoSwap queries both Paraswap (50+ DEX aggregation) and Uniswap V3 in parallel, picks the highest expected output, and validates it before building the transaction.

4. Bridge Complexity

Cross-chain routing via Across Protocol with ~2-5 second fills, automatic fallback if the primary route fails, and USDC as the default bridge token (deepest liquidity).


Architecture

autoswap/
├── src/
│   ├── swap.py      ← Orchestrator
│   ├── router.py    ← DEX routing (Paraswap + Uniswap V3)
│   ├── bridge.py    ← Cross-chain bridge (Across Protocol)
│   ├── gas.py       ← Native gas resolver
│   └── safety.py    ← Slippage + sandwich protection
├── autoswap/        ← Python package
├── npm/             ← JavaScript wrapper
└── mcp-tool.json    ← MCP tool descriptor (for AI agents)
Enter fullscreen mode Exit fullscreen mode

MCP Native: Claude/Cursor/Cline Can Call It Directly

This is where it gets interesting for AI agent builders.

AutoSwap ships as an MCP (Model Context Protocol) tool. Add this to your Claude Desktop or Cursor config:

{
  "mcpServers": {
    "autoswap": {
      "command": "python3",
      "args": ["-m", "autoswap.mcp_handler"],
      "env": {
        "ETH_PRIVATE_KEY": "your_key"
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Now Claude can execute cross-chain swaps directly in conversation:

User: "Swap 0.003 ETH on Base to MYST on Polygon, show me the route first"

Claude: [calls autoswap.swap with dry_run=true]
→ Route: ETH→USDC (base) | bridge USDC base→polygon | USDC→MYST (polygon)
→ Expected output: ~47.3 MYST
→ Bridge fee: 0.08 USDC

"Execute it."

Claude: [calls autoswap.swap with dry_run=false]
→ Received: 47.1 MYST ✅
Enter fullscreen mode Exit fullscreen mode

No manual steps. No fumbling with raw transaction builders. The agent just calls swap() and gets a result.


Dry-Run Mode: Safe by Default

AutoSwap is designed for agents that need to verify before executing. dry_run=True builds the entire transaction pipeline, checks liquidity, calculates fees, and returns the full route — without submitting anything to the blockchain.

# Always dry-run first
result = swap("ETH", "base", "MYST", "polygon", 0.003, dry_run=True)
print(f"Route: {result.route_taken}")
print(f"Expected: {result.amount_out:.4f} MYST")
print(f"Bridge fee: {result.fees.get('bridge_fee', 0):.4f} USDC")

# Only execute if the numbers look right
if result.success and result.amount_out > 40:
    live = swap("ETH", "base", "MYST", "polygon", 0.003, dry_run=False)
    print(f"TX: {live.tx_hashes}")
Enter fullscreen mode Exit fullscreen mode

JavaScript / npm

There's a thin JS wrapper for Node.js environments:

npm install autoswap
Enter fullscreen mode Exit fullscreen mode
const { swap, quote } = require('autoswap');

const result = await swap({
  fromToken: 'ETH',
  fromChain: 'base',
  toToken: 'MYST',
  toChain: 'polygon',
  amount: 0.003,
  dryRun: true,
});

console.log(result.routeTaken);
Enter fullscreen mode Exit fullscreen mode

Supported Networks

Chain Status
Base ✅ Full support
Polygon ✅ Full support
Arbitrum ✅ Full support
Optimism ✅ Full support
Ethereum 🔜 Coming soon

Comparison

Feature AutoSwap Li.Fi 1inch Relay.link
Single function call
Cold start gas fix ✅ automatic ❌ manual
Sandwich protection ✅ built-in ⚠️ ⚠️
amountOutMin=0 guard ✅ always ⚠️ ⚠️
MCP / AI agent ready ✅ native
Dry-run simulation ⚠️
No API key needed
Open source (MIT) ✅ GPL

Install

pip install autoswap
# or
npm install autoswap
Enter fullscreen mode Exit fullscreen mode

GitHub: https://github.com/fino-oss/autoswap


Built from real pain. If you've ever manually sent POL to unstick a bridged USDC, this is for you.

Happy to answer questions — especially if you're building AI agents that need DeFi access.

Top comments (0)