Skip to main content

Overview

Legend gives AI agents access to DeFi yield, borrowing, trading (perpetuals and prediction markets coming soon), and cross-chain transfers across 8 EVM networks and Solana (coming soon). Whether you connect through the MCP Server, the CLI, or the REST API directly, you get access to the same execution engine — the same one powering Legend’s consumer app with real money and real users. Skill reference: legend.xyz/SKILL.md

Golden Path

Paste this into your agent to get started:
For manual setup → MCP Setup

Integration Options

Runs via legend-cli mcp serve. Includes all tools plus plan_and_execute — create, sign, and execute in one tool call. No separate signing step. → Full setup

Remote MCP Server

The hosted server at https://prime-api.legend.xyz/mcp. Works with Claude Code, Cursor, OpenAI Responses API, and any MCP-compatible host. Best for read-only operations or shared team access. Executing plans requires a separate CLI signing step.

CLI

Direct key management, signing, and API access. Useful for scripting and CI/CD.
CLI Reference

REST API

API Reference

Authentication

OAuth JWTs last 30 days. Both OAuth and query keys work with all integration paths.

How It Works: Plan → Sign → Execute

Every action that moves funds follows a three-step flow. This is what keeps Legend non-custodial — signing keys never touch Legend’s servers.

1. Plan

Your agent requests an operation. Legend returns a full human-readable plan — amounts, routes, protocols, fees — before anything happens onchain. Plans are free and expire after 2 minutes.
Response includes:
  • plan_id — valid for 2 minutes
  • details — every step, every chain, human-readable
  • details.eip712_data.digest — the hash to sign

2. Sign

The CLI signs using the P256 key stored in your macOS Keychain. Turnkey then produces the EIP-712 signature using the account’s secp256k1 wallet key. The P256 key never leaves hardware. The secp256k1 key never leaves Turnkey’s secure infrastructure. Legend never sees your private key. Nothing moves without your approval.

3. Execute

Legend verifies the signature, submits the transaction onchain, and handles gas, bridging, and token approvals atomically. The plan_and_execute MCP tool does all three in one call.

Working with Amounts

Amounts are always in the token’s smallest unit:

Core Actions

Earn

Check available rates, then deploy:
The yield_markets keys in the portfolio tell you the protocol to use:

Withdraw from Yield

Swap

Use either sell_amount or buy_amount, not both:

Transfer

Borrow

Advanced

  • loop-long — Open leveraged long positions on Morpho markets
  • migrate — Move supply positions between protocols atomically
  • reinvest-rewards — Claim, swap, bridge, and re-supply rewards in one plan
  • swap-and-supply — Swap and deploy into yield in a single signature
  • plan/perp — Perpetual positions on Hyperliquid (coming soon)
  • plan/predict — Prediction markets on Polymarket (coming soon)
  • plan/spend — Pay real-world expenses via virtual card (coming soon)

Reading the Portfolio

get_portfolio returns everything about an account:
  • balances — token amounts per chain. Key format: token/{network}/{symbol}/{wallet}. Values use scientific notation: "300.25e6" = 300.25 USDC.
  • prices — USD prices. Key: token/{SYMBOL}. Value: "2713.04".
  • yield_markets — where the account is earning, with supply_apr.
  • borrow_markets — open borrows with borrow_apr and collateral info.
  • rewards — unclaimed protocol rewards.

Supported Networks

Cross-chain is automatic — specify the target network and Legend bridges funds as needed.

Typical Workflows

Deploy idle USDC to best yield

Swap ETH for USDC

Move funds to another wallet

Check transaction status


Account Structure

Every integration starts with an Account — your organization’s container. Under it you create sub-accounts, each with its own onchain smart wallet and signer key.
Sub-accounts are fully segregated — separate wallet, separate signer. Provision one per agent, per strategy, or per user. If one is compromised, the blast radius is limited to that sub-account.

Non-Custodial Design

Legend never holds signing keys. The security model:
  • Reading (portfolios, activities, reference data) — requires only API auth (query key or OAuth JWT)
  • Planning (create earn/swap/transfer plans) — same, just API auth. Plans are free previews.
  • Executing (moving funds) — requires a cryptographic signature from the account’s P256 key. The agent signs locally via the local MCP server or CLI. Legend verifies the signature but never has access to the key.
When you run legend-cli accounts create --keygen, the CLI:
  1. Generates a P256 key in your macOS Keychain (hardware-protected, non-exportable)
  2. Sends only the public key to Legend, which creates a Turnkey sub-organization with that key
  3. Saves the key reference and account details to ~/.legend/prod/profiles/default.json

Safety Properties

Whitelisted protocols only — Agents can only route funds into audited, whitelisted DeFi protocols. No arbitrary contract calls. Plan expiry — Plans expire after 2 minutes. A stale plan cannot execute. If conditions change, the agent replans. Replay protection — Every plan is domain-bound and chain-bound via EIP-712. A signature cannot be replayed on a different action or chain. Secure key storage — The P256 signing key lives in macOS Keychain. It cannot be exported or accessed remotely. Revocable at any time — Rotating or revoking a signing key immediately stops that agent. No need to drain wallets or unwind positions. Agent-in-the-loop — Run a planner agent that constructs the intent and a separate signing agent that verifies the plan before approving. No human needs to review every transaction.

Error Handling


Next Steps

MCP Setup

Connect in 2 minutes

MCP Tools

Complete tool reference

CLI Reference

Full CLI command reference

Claude Code Guide

Full walkthrough

API Reference

Complete endpoint documentation

OpenAI Agents

OpenAI Responses API setup

Data Sources

Free-tier APIs for strategy signals