{"slug": "build-a-pear-protocol-defi-trading-agent-with-mcp", "title": "Build a Pear Protocol DeFi trading agent with MCP", "summary": "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.", "body_md": "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**.\n\nWe'll use [ mcp-pear](https://github.com/MarvelNwachukwu/mcp-pear), an open-source MCP server that wraps Pear Protocol's API.\n\n[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.\n\nPear settles on Hyperliquid, so every pair position is really a pair of perp legs executed on-chain.\n\nThe **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.\n\n`mcp-pear`\n\nexposes Pear as a set of MCP tools:\n\n`list_markets`\n\n, `get_active_markets`\n\n, `get_pair_ratio`\n\n, `get_health`\n\n`get_open_positions`\n\n, `get_open_orders`\n\n, `get_portfolio`\n\n, `get_trade_history`\n\n, `get_account_summary`\n\n`open_position`\n\n, `close_position`\n\n, `adjust_position`\n\n, `adjust_leverage`\n\n, `set_risk_parameters`\n\n, `cancel_order`\n\nNo install needed. `npx`\n\nfetches it:\n\n```\nnpx -y @marvelcodes/mcp-pear@latest\n```\n\nThe public tools work with zero config, so your agent can browse markets and pair ratios right away.\n\nAdd this to your Claude Desktop MCP config:\n\n```\n{\n  \"mcpServers\": {\n    \"pear\": {\n      \"command\": \"npx\",\n      \"args\": [\"-y\", \"@marvelcodes/mcp-pear@latest\"]\n    }\n  }\n}\n```\n\nRestart Claude and ask: *\"What are the top gaining pair markets on Pear right now?\"* It calls `get_active_markets`\n\nand answers from live data.\n\nTo read *your* positions and portfolio, mint a Pear API key:\n\n```\nnpx -y @marvelcodes/mcp-pear@latest setup\n```\n\nThis opens a browser, asks you to sign once with your wallet, and writes `PEAR_API_KEY`\n\n+ `PEAR_ADDRESS`\n\n. Add them to the config:\n\n```\n{\n  \"mcpServers\": {\n    \"pear\": {\n      \"command\": \"npx\",\n      \"args\": [\"-y\", \"@marvelcodes/mcp-pear@latest\"],\n      \"env\": {\n        \"PEAR_API_KEY\": \"your_key\",\n        \"PEAR_ADDRESS\": \"0xyour_address\"\n      }\n    }\n  }\n}\n```\n\nNow *\"How's my portfolio doing this week?\"* works. It calls `get_portfolio`\n\nwith your real PnL.\n\nTrade execution is **off by default**. You opt in on purpose:\n\n```\n\"env\": {\n  \"PEAR_API_KEY\": \"your_key\",\n  \"PEAR_ADDRESS\": \"0xyour_address\",\n  \"PEAR_TRADE_ENABLED\": \"true\"\n}\n```\n\nWith that flag set, the agent can call `open_position`\n\n. A pair trade looks like this: long ETH, short BTC, 3x, $100 notional.\n\n```\n{\n  \"executionType\": \"MARKET\",\n  \"leverage\": 3,\n  \"usdValue\": 100,\n  \"slippage\": 0.01,\n  \"longAssets\":  [{ \"asset\": \"ETH\", \"weight\": 1 }],\n  \"shortAssets\": [{ \"asset\": \"BTC\", \"weight\": 1 }]\n}\n```\n\n`open_position`\n\nalso takes `TRIGGER`\n\n, `TWAP`\n\n, and `LADDER`\n\nexecution types, plus attached `stopLoss`\n\n/ `takeProfit`\n\n. Pear signs the trade server-side, so **the MCP server never holds your private keys**.\n\nTwo Hyperliquid gotchas worth knowing:\n\n`open_position`\n\nfails with insufficient margin.Here's the shape of an agent loop that uses it:\n\n`get_active_markets`\n\nto find pairs with strong momentum`get_pair_ratio`\n\nto check the current ratio and funding`open_position`\n\nwith a stop-loss attached`get_open_positions`\n\n, then `close_position`\n\nwhen the thesis plays outEverything is a tool call, so the model reasons over live data and acts. No custom API glue.\n\n`PEAR_TRADE_ENABLED`\n\noff until you've tested read-only behaviour.`mcp-pear`\n\nis an independent, open-source community project, not affiliated with Pear Protocol. PRs and issues welcome.", "url": "https://wpnews.pro/news/build-a-pear-protocol-defi-trading-agent-with-mcp", "canonical_source": "https://dev.to/marvelcodes/build-a-pear-protocol-defi-trading-agent-with-mcp-499o", "published_at": "2026-07-18 23:35:46+00:00", "updated_at": "2026-07-18 23:58:49.871183+00:00", "lang": "en", "topics": ["ai-agents", "developer-tools", "artificial-intelligence", "ai-tools"], "entities": ["Pear Protocol", "Hyperliquid", "Model Context Protocol", "mcp-pear", "Claude Desktop", "ETH", "BTC"], "alternates": {"html": "https://wpnews.pro/news/build-a-pear-protocol-defi-trading-agent-with-mcp", "markdown": "https://wpnews.pro/news/build-a-pear-protocol-defi-trading-agent-with-mcp.md", "text": "https://wpnews.pro/news/build-a-pear-protocol-defi-trading-agent-with-mcp.txt", "jsonld": "https://wpnews.pro/news/build-a-pear-protocol-defi-trading-agent-with-mcp.jsonld"}}