> ## Documentation Index
> Fetch the complete documentation index at: https://docs.legend.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Using with Claude Code

> Step-by-step guide to using Legend with Claude Code

# Using Legend with Claude Code

Connect Claude Code to Legend and manage DeFi portfolios through natural conversation.

## Quick Setup (Local MCP — recommended)

The local MCP server gives Claude Code full capabilities including one-shot plan execution with automatic signing.

### 1. Install the CLI

```bash theme={null}
brew install legend-hq/tap/legend-cli   # macOS
# or: cargo install legend-cli          # any platform
```

### 2. Log in

```bash theme={null}
legend-cli login
```

Sign in with Google. This saves a 30-day JWT to your profile.

### 3. Connect the local MCP server

```bash theme={null}
claude mcp add legend -- legend-cli mcp serve
```

### 4. Verify

```
/mcp
```

You should see "legend" with a green status and tools like `list_accounts`, `get_portfolio`, `plan_and_execute`.

### 5. Create a signing account (optional, for transactions)

Ask Claude:

> "Create a new Legend account with keygen"

Claude calls `create_account`, which generates a P256 key in macOS Keychain and creates a Turnkey-backed sub-account. This is a one-time setup.

## Alternative: Remote MCP

If you only need read-only access (portfolios, activities, reference data), the remote MCP server is simpler:

```bash theme={null}
claude mcp add --transport http legend-remote https://prime-api.legend.xyz/mcp
```

Then authenticate via `/mcp` > "legend" > "Authenticate" > Google SSO.

## Example Session

### Read-only operations (no signing needed)

> **You:** What's in my Legend portfolio?

Claude calls `list_accounts` then `get_portfolio` and reports your holdings:

> "You have $1,500.25 USDC on Base earning 4.2% APY via Compound, and 0.5 ETH worth ~$1,623."

> **You:** What are the best yield rates for USDC?

Claude reads the `yield_markets` from your portfolio and compares:

> "Best USDC yields: Compound on Base at 4.2%, Aave on Arbitrum at 3.8%, Morpho on Ethereum at 5.1%."

### Creating and executing plans (local MCP)

> **You:** Put \$500 of my USDC into that Morpho vault on Ethereum

With the local MCP server, Claude calls `plan_and_execute` — it creates the plan, signs it with your P256 key, and executes in one step:

> "Done. I executed an earn plan for 500,000,000 USDC (smallest units) into Morpho on Ethereum. Transaction is executing now."

> **You:** What happened with that transaction?

Claude calls `get_activities` and reports:

> "The earn transaction completed successfully. Your USDC is now earning 5.1% APY on Morpho."

### Reference data

> **You:** What networks and assets does Legend support?

Claude calls `list_networks` and `list_assets`:

> "Legend supports Base, Ethereum, Arbitrum, Optimism, Polygon, Unichain, World Chain, and HyperEVM. Available assets include USDC, USDT, WETH, cbBTC, wstETH, and more."

## Add a CLAUDE.md Snippet

Drop this into your project's `CLAUDE.md` so Claude uses Legend tools effectively:

```markdown theme={null}
## Legend

This project uses Legend for DeFi operations via MCP tools.

- **Amounts** are in smallest units: 1 USDC = 1000000 (6 decimals), 1 ETH = 1e18
- **Plans** expire after 2 minutes — execute promptly after creation
- **yield_markets** keys: `{protocol}/{network}/{address}/{asset}` —
  comet -> "compound", aave -> "aave", morpho_vault -> "morpho_vault"
- Use `plan_and_execute` for one-shot plan creation + signing + execution
- Call `list_accounts` first if you don't know the account ID
- Call `get_portfolio` to see balances and yield markets before creating plans
- For USDC/USDT, report amounts as dollars ($300.25, not "300250000")
```

## Tips

* **Start with reads** — `list_accounts`, `get_portfolio`, `list_networks` don't need signing. Great for exploration.
* **Plans are free** — creating a plan doesn't commit to anything. It's a preview.
* **Plans expire** — 2 minutes to sign and execute after creation.
* **Use `plan_and_execute`** — one tool call instead of three separate steps (create, sign, execute).
* **Use `/mcp` to debug** — shows connection status, tools, and errors.
* **JSON output from CLI** — the `--json` flag on any CLI command outputs JSON that Claude can parse easily.
