DEV Community

Cover image for Build a Pear Protocol DeFi trading agent with MCP
Marvellous
Marvellous

Posted on

Build a Pear Protocol DeFi trading agent with MCP

If you want an AI agent that can actually trade, not just chat about charts, the Model Context Protocol (MCP) is the cleanest way to wire a model to a real exchange. This post builds a Pear Protocol DeFi trading agent: one that can browse pair markets, read your positions and portfolio, and (when you turn it on) open and close leveraged pair trades on Hyperliquid.

We'll use mcp-pear, an open-source MCP server that wraps Pear Protocol's API.

What is Pear Protocol?

Pear Protocol is a Hyperliquid-backed platform for pair trading. You go long one basket and short another in a single position, and profit from the ratio between them moving your way, regardless of overall market direction. Classic example: long ETH, short BTC if you think ETH outperforms, without betting on crypto as a whole.

Pear settles on Hyperliquid, so every pair position is really a pair of perp legs executed on-chain.

What is MCP, and why use it here?

The Model Context Protocol is an open standard that lets an LLM call external tools through one uniform interface. Instead of gluing Pear's REST API to your model by hand, you run an MCP server that describes its tools to the model, and any MCP client (Claude Desktop, Cursor, Cline, or your own agent) can use them.

mcp-pear exposes Pear as a set of MCP tools:

  • Public (no auth): list_markets, get_active_markets, get_pair_ratio, get_health
  • Authenticated read: get_open_positions, get_open_orders, get_portfolio, get_trade_history, get_account_summary
  • Write (opt-in): open_position, close_position, adjust_position, adjust_leverage, set_risk_parameters, cancel_order

Step 1: run the server

No install needed. npx fetches it:

npx -y @marvelcodes/mcp-pear@latest
Enter fullscreen mode Exit fullscreen mode

The public tools work with zero config, so your agent can browse markets and pair ratios right away.

Step 2: wire it into Claude Desktop

Add this to your Claude Desktop MCP config:

{
  "mcpServers": {
    "pear": {
      "command": "npx",
      "args": ["-y", "@marvelcodes/mcp-pear@latest"]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Restart Claude and ask: "What are the top gaining pair markets on Pear right now?" It calls get_active_markets and answers from live data.

Step 3: add your account (read tools)

To read your positions and portfolio, mint a Pear API key:

npx -y @marvelcodes/mcp-pear@latest setup
Enter fullscreen mode Exit fullscreen mode

This opens a browser, asks you to sign once with your wallet, and writes PEAR_API_KEY + PEAR_ADDRESS. Add them to the config:

{
  "mcpServers": {
    "pear": {
      "command": "npx",
      "args": ["-y", "@marvelcodes/mcp-pear@latest"],
      "env": {
        "PEAR_API_KEY": "your_key",
        "PEAR_ADDRESS": "0xyour_address"
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Now "How's my portfolio doing this week?" works. It calls get_portfolio with your real PnL.

Step 4: let the agent trade (carefully)

Trade execution is off by default. You opt in on purpose:

"env": {
  "PEAR_API_KEY": "your_key",
  "PEAR_ADDRESS": "0xyour_address",
  "PEAR_TRADE_ENABLED": "true"
}
Enter fullscreen mode Exit fullscreen mode

With that flag set, the agent can call open_position. A pair trade looks like this: long ETH, short BTC, 3x, $100 notional.

{
  "executionType": "MARKET",
  "leverage": 3,
  "usdValue": 100,
  "slippage": 0.01,
  "longAssets":  [{ "asset": "ETH", "weight": 1 }],
  "shortAssets": [{ "asset": "BTC", "weight": 1 }]
}
Enter fullscreen mode Exit fullscreen mode

open_position also takes TRIGGER, TWAP, and LADDER execution types, plus attached stopLoss / takeProfit. Pear signs the trade server-side, so the MCP server never holds your private keys.

Two Hyperliquid gotchas worth knowing:

  • ~$10 minimum notional per order. Smaller orders get rejected.
  • Spot vs Perps balance. USDC bridged onto Hyperliquid often lands in your Spot balance. Move it to Perps first or open_position fails with insufficient margin.

Putting it together

Here's the shape of an agent loop that uses it:

  1. get_active_markets to find pairs with strong momentum
  2. get_pair_ratio to check the current ratio and funding
  3. Model decides direction
  4. open_position with a stop-loss attached
  5. Poll get_open_positions, then close_position when the thesis plays out

Everything is a tool call, so the model reasons over live data and acts. No custom API glue.

Safety notes

  • Keep PEAR_TRADE_ENABLED off until you've tested read-only behaviour.
  • Start with tiny notionals.
  • The write tools execute real trades with real money. Constrain the agent, monitor it, and set risk parameters like you would any trading bot.

Links

mcp-pear is an independent, open-source community project, not affiliated with Pear Protocol. PRs and issues welcome.

Top comments (1)

Collapse
 
mads_hansen_27b33ebfee4c9 profile image
Mads Hansen

For real-money execution I would tighten the boundary before enabling the flag. Pin an exact package version and integrity digest instead of running @latest; use separate read and trade credentials/processes; and enforce max notional, leverage, slippage, allowed assets, daily loss, position count, and expiry in deterministic code outside the model. Every order also needs a client idempotency key so a timeout or agent retry cannot duplicate it. Pair trades add another failure mode: one leg may fill while the other rejects or partially fills, so the tool contract should return per-leg state and drive explicit hedge, cancel, or compensation logic before the agent continues.