# Build a Pear Protocol DeFi trading agent with MCP

> Source: <https://dev.to/marvelcodes/build-a-pear-protocol-defi-trading-agent-with-mcp-499o>
> Published: 2026-07-18 23:35:46+00:00

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](https://github.com/MarvelNwachukwu/mcp-pear), an open-source MCP server that wraps Pear Protocol's API.

[Pear Protocol](https://pearprotocol.io) 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 momentum`get_pair_ratio`

to check the current ratio and funding`open_position`

with a stop-loss attached`get_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.
