cd /news/ai-agents/build-a-pear-protocol-defi-trading-a… · home topics ai-agents article
[ARTICLE · art-64920] src=dev.to ↗ pub= topic=ai-agents verified=true sentiment=↑ positive

Build a Pear Protocol DeFi trading agent with MCP

A developer built a DeFi trading agent for Pear Protocol using the Model Context Protocol (MCP). The agent, powered by the open-source mcp-pear server, can browse pair markets, read positions and portfolio, and execute leveraged pair trades on Hyperliquid. The MCP server wraps Pear Protocol's API, allowing the agent to call tools like open_position and close_position via a uniform interface.

read3 min views1 publishedJul 18, 2026

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.

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.

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:

list_markets

, get_active_markets

, get_pair_ratio

, get_health

get_open_positions

, get_open_orders

, get_portfolio

, get_trade_history

, get_account_summary

open_position

, close_position

, adjust_position

, adjust_leverage

, set_risk_parameters

, cancel_order

No install needed. npx

fetches it:

npx -y @marvelcodes/mcp-pear@latest

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

Add this to your Claude Desktop MCP config:

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

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.

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

npx -y @marvelcodes/mcp-pear@latest setup

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"
      }
    }
  }
}

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

with your real PnL.

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"
}

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 }]
}

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:

open_position

fails with insufficient margin.Here's the shape of an agent loop that uses it:

get_active_markets

to find pairs with strong momentumget_pair_ratio

to check the current ratio and fundingopen_position

with a stop-loss attachedget_open_positions

, then close_position

when the thesis plays outEverything is a tool call, so the model reasons over live data and acts. No custom API glue.

PEAR_TRADE_ENABLED

off until you've tested read-only behaviour.mcp-pear

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

── more in #ai-agents 4 stories · sorted by recency
── more on @pear protocol 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/build-a-pear-protoco…] indexed:0 read:3min 2026-07-18 ·