# Connect the MCP server

> Connect Claude to Legion's MCP server by signing in, or add the server to Claude Code, Cursor, VS Code, Codex, Windsurf and other clients with an API key, and check that it works.

Source: https://legion.ambient.xyz/docs/agents/mcp

After this page, your AI client can call Legion's tools. With Claude you sign in to Legion and approve, and no key is involved. With any other client, or a script, you paste one snippet for your client, set one environment variable, and check it with one question.

The server is at:

```text
https://api.legion.ambient.xyz/v1/mcp
```

It is a remote server over HTTP (Streamable HTTP). It speaks both kinds of MCP client on the same URL: the stateless protocol revision 2026-07-28, and the earlier revisions that start with `initialize`. Your client picks one; you don't configure it. There is nothing to install. Claude's clients sign in to Legion with OAuth. Other clients send your Legion API key in the `Authorization` header as a bearer token, the same key you use for the [API](https://legion.ambient.xyz/docs/api/authentication).

## Connect Claude

Claude connects by signing in to Legion. You don't create or paste an API key. Do this once; Claude then has Legion's tools in your conversations.

### Claude on the web and Claude Desktop

On the Free, Pro and Max plans:

1. Open **Customize**, then **Connectors**, then **Add custom connector**.
2. For the server URL, enter `https://api.legion.ambient.xyz/v1/mcp`.
3. Leave the OAuth credentials empty. Claude identifies itself to Legion. If the dialog asks how people connect, choose a sign-in option, not **No sign-in**. If it asks for the OAuth client, choose **Use Claude's published identity**.
4. Click **Add**, then click **Connect**.
5. Your browser opens. Sign in to Legion. If you belong to more than one organization, choose the one this connection works in. Choose **Agent** or **Agent + hosts**, and approve. See [What you approve](#what-you-approve).

On a Team or Enterprise plan, an Owner adds the connector for the organization: **Organization settings**, then **Connectors**, then **Add**, then **Custom**. The Owner enters the server URL above and clicks **Add**. Each member then opens **Customize**, then **Connectors**, finds the connector with the **Custom** label, clicks **Connect**, and signs in to Legion with their own account.

Then turn the connector on in a conversation, and ask. See [Check that it works](#check-that-it-works).

### Claude Code

Add the server, then sign in from inside Claude Code:

```sh
claude mcp add --transport http legion https://api.legion.ambient.xyz/v1/mcp
```

Start Claude Code and run `/mcp`. Choose the `legion` server and choose to authenticate. Your browser opens so you can sign in to Legion, choose an organization if you have more than one, choose **Agent** or **Agent + hosts**, and approve. The sign-in returns to an app on your own computer, so the browser may show a local address for a moment.

### What you approve

The page asks you to choose what the connection may do:

- **Agent** (the default): find, rent and manage GPUs and rentals. Spending still asks you to confirm each time in the app you connect.
- **Agent + hosts**: also lets it manage your hosts' listings. A host change also needs an organization owner. Choose this only if you want Claude to work on your hosts.

A connected app can't create API keys, invite members, change payouts or top up credit. There is no full-access option. You can approve only for an organization in which you are allowed to create API keys. See [Safety](https://legion.ambient.xyz/docs/agents/safety).

### Disconnect

Open the Legion app, then **Settings**, then **API keys**. Under **Connected apps**, click **Revoke** next to the app. Revoking works at once.

You can also disconnect from the client. In Claude, open **Customize**, then **Connectors**, and remove the connector. In Claude Code, run `/mcp`, open the server and choose **Clear authentication**.

A connection lasts up to 90 days. After that, Claude asks you to sign in to Legion again.

## Use an API key

Use an API key for scripts, CI and clients without OAuth. Other clients that support MCP sign-in (OAuth) may work with the server's URL on its own; if yours doesn't, use a key. The sections below set up Claude Code and Claude with a key, and Cursor, VS Code, Codex, Windsurf and other clients.

First, create a key for the agent in the Legion app: **Settings**, then **API keys**. Name it for the agent, such as `claude-code laptop`, and give it an expiry. Read [Safety](https://legion.ambient.xyz/docs/agents/safety) first.

If you are a provider and want the agent to manage your hosts, tick **Can manage hosts** under **Access** when you choose **Agent**. That gives the key the provider tools: reading hosts and earnings, and, with your yes, changing a host's price and listing, renaming it and removing it. Without it the agent has the renting tools only. An organization owner has to create that key, because only an owner can change a host. See [Safety](https://legion.ambient.xyz/docs/agents/safety).

Then put the key in the `LEGION_API_KEY` environment variable of the terminal you start your client from, without showing it or saving it in your shell history:

```sh
read -rs LEGION_API_KEY && export LEGION_API_KEY
```

Last, prefer the client's user-level configuration to a project file where you can. **Never commit a configuration file that contains a key.** The snippets below read the key from the environment, so they hold no key. If you add one to a project, put its file in `.gitignore`.

To try Legion safely first, end the URL with `?readonly=true`. The server then offers only the read tools: the agent can look at offers, rentals and spend, and cannot change anything.

```text
https://api.legion.ambient.xyz/v1/mcp?readonly=true
```

## Claude Code with an API key

Add the server for your user, so it works in every project. The single quotes keep `${LEGION_API_KEY}` as text, and Claude Code fills it in from your environment each time it starts:

```sh
claude mcp add-json --scope user legion '{"type":"http","url":"https://api.legion.ambient.xyz/v1/mcp","headers":{"Authorization":"Bearer ${LEGION_API_KEY}"}}'
```

To share the setup in a project, commit a `.mcp.json` file with the same server. It holds the name of the variable, not the key, so each person sets their own:

```json
{
  "mcpServers": {
    "legion": {
      "type": "http",
      "url": "https://api.legion.ambient.xyz/v1/mcp",
      "headers": { "Authorization": "Bearer ${LEGION_API_KEY}" }
    }
  }
}
```

Start Claude Code from the terminal where you set the variable. Type `/mcp` to see the server and its status.

## Claude on the web with an API key

Signing in is simpler: see [Connect Claude](#connect-claude). Use this only if you want one shared key. Claude connects to remote servers as custom connectors, and the connector sends your key in a request header. Claude's request headers are in beta, for a limited set of Claude organizations. If the **Add custom connector** dialog has no **Request headers** section, your Claude organization doesn't have them yet: use [Connect Claude](#connect-claude) instead.

1. Open **Customize**, then **Connectors**, then **Add**, then **Add custom connector**.
2. For the URL, enter the server's URL from the top of this page.
3. For **Authentication**, choose **No sign-in**. Do this before you add the header. With **Sign in now** or **Sign in when needed**, Claude leaves `authorization` out of the header list, because those options use that header themselves.
4. Under **Request headers**, choose `authorization` from the list. For the value, enter `Bearer ` and then your key, with the space: `Bearer YOUR_LEGION_API_KEY`. Claude sends the value exactly as you type it, so a value without `Bearer ` is refused with `401`. Mark it **Required**.
5. Click **Add**, and turn the connector on in a conversation.

You can't change a connector's header after you add it. To use a new key, remove the connector and add it again.

A connector can't read an environment variable, so Claude stores the header for you. On Team and Enterprise plans, an owner adds the connector under **Organization settings**, and every member then uses that one key. Create a key for it with an expiry, from a member of your Legion organization. Claude calls the server from Anthropic's network, which is fine: the server is public.

## Cursor

Add this to `~/.cursor/mcp.json` for your user, or `.cursor/mcp.json` for one project:

```json
{
  "mcpServers": {
    "legion": {
      "url": "https://api.legion.ambient.xyz/v1/mcp",
      "headers": { "Authorization": "Bearer ${env:LEGION_API_KEY}" }
    }
  }
}
```

Cursor must see the variable. On macOS and Windows, a Cursor you open from the Dock or the Start menu may not. Start it from the terminal where you set the variable, or set the variable for your whole account.

## VS Code

VS Code asks for the key once and stores it for you, so it is not in the file. Add this to `.vscode/mcp.json`, or open your user configuration with **MCP: Open User Configuration**:

```json
{
  "inputs": [
    {
      "type": "promptString",
      "id": "legion-api-key",
      "description": "Legion API key",
      "password": true
    }
  ],
  "servers": {
    "legion": {
      "type": "http",
      "url": "https://api.legion.ambient.xyz/v1/mcp",
      "headers": { "Authorization": "Bearer ${input:legion-api-key}" }
    }
  }
}
```

The top-level key is `servers`, not `mcpServers`. VS Code asks for the key the first time it starts the server.

## OpenAI Codex

Codex reads `~/.codex/config.toml` in its command line, IDE and desktop apps. Name the variable that holds the key. Do not put the key in the file:

```text
[mcp_servers.legion]
url = "https://api.legion.ambient.xyz/v1/mcp"
bearer_token_env_var = "LEGION_API_KEY"
# Ask before every tool call:
default_tools_approval_mode = "prompt"
```

## Windsurf

Add the server to Windsurf's MCP configuration file, `mcp_config.json`. Windsurf's documentation says where that file is for your version. Windsurf expands environment variables with `${env:NAME}`:

```json
{
  "mcpServers": {
    "legion": {
      "serverUrl": "https://api.legion.ambient.xyz/v1/mcp",
      "headers": { "Authorization": "Bearer ${env:LEGION_API_KEY}" }
    }
  }
}
```

## Other clients

Any client that supports remote MCP servers over Streamable HTTP with a custom header can connect. Give it:

| Setting | Value |
| --- | --- |
| URL | https://api.legion.ambient.xyz/v1/mcp |
| Transport | Streamable HTTP (remote HTTP) |
| Header | `Authorization: Bearer ` followed by your key |
| Protocol revisions | 2026-07-28 (stateless), and 2025-11-25, 2025-06-18 and 2025-03-26 (with `initialize`) |

If the client can read the key from an environment variable, use that. If it can't, it has to store the key in its configuration: keep that file private to you, and never commit it. For example, Gemini CLI writes the key you give it to `~/.gemini/settings.json`:

```sh
gemini mcp add --transport http --header "Authorization: Bearer $LEGION_API_KEY" legion https://api.legion.ambient.xyz/v1/mcp
```

Clients that only run local servers can't use this server.

## Check that it works

**List the tools.** In Claude Code, type `/mcp` and open `legion`. In other clients, open the list of MCP servers or tools. You should see the tools in the [Tools reference](https://legion.ambient.xyz/docs/agents/tools), such as `get_me`, `list_offers` and `create_rental`. In a read-only connection you see only the read tools.

**Call `get_me`.** Ask your agent:

```text
Call the Legion get_me tool and tell me which organization this connection is in, and its credential kind, scope and provider_tools.
```

You should get your name, your organization, your role, your permissions and the connection's `credential`: `scope` (`full` or `agent`) and `provider_tools`. The permissions are your role's; the `credential` says what the key or connection itself may call. If you connected Claude by signing in, its `kind` is `oauth_grant`, it names the app, and its `scope` is `agent`. Nothing is changed.

If you want to see the server answer without a client, send the request yourself. This lists the tools:

```sh
curl -sS -X POST "https://api.legion.ambient.xyz/v1/mcp" \
  -H "Authorization: Bearer $LEGION_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -H "MCP-Protocol-Version: 2025-06-18" \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'
```

A client on revision 2026-07-28 sends no `initialize`. Each request carries the protocol version and the client's capabilities in `params._meta`, and repeats the version and the method in headers. This asks the server which revisions it speaks:

```sh
curl -sS -X POST "https://api.legion.ambient.xyz/v1/mcp" \
  -H "Authorization: Bearer $LEGION_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -H "MCP-Protocol-Version: 2026-07-28" \
  -H "Mcp-Method: server/discover" \
  -d '{"jsonrpc":"2.0","id":1,"method":"server/discover","params":{"_meta":{"io.modelcontextprotocol/protocolVersion":"2026-07-28","io.modelcontextprotocol/clientCapabilities":{}}}}'
```

The answer lists `supportedVersions` and the server's capabilities. A `tools/call` on this revision also sends the tool's name in an `Mcp-Name` header.

## If it doesn't work

| You see | What it means | What to do |
| --- | --- | --- |
| `401 unauthorized`, or the client says it can't connect | With a key: the key is missing, wrong, expired or revoked. Often the client can't see `LEGION_API_KEY`, so it sent `Bearer ` with nothing after it. With Claude's sign-in: the connection ended, because you revoked it or 90 days passed. | With a key, check the variable in the terminal you start the client from, restart the client from it, and create a new key if needed. With Claude, connect again: click **Connect** on the connector, or run `/mcp` in Claude Code. |
| `403` with `oauth_token_mcp_only` | The sign-in of a connected app was used outside the MCP server, for example with `curl`. | Use an API key for scripts. A connected app works only through its MCP client. |
| `403` with `terms_not_accepted`, `org_suspended` or `access_not_approved` | The same gates as the API. | Fix it in the Legion app. See [Errors](https://legion.ambient.xyz/docs/errors). |
| `404` | The URL is wrong. | Copy the URL from the top of this page. |
| `403` with `api_key_required` | A signed-in session was used, not an API key. | Send your API key as the bearer token. |
| `403` with `api_key_scope` | A key with Agent access was used for something outside the MCP server's tools, for example with `curl`. The tools themselves all work with it. | Do that in the Legion app. |
| `403` with `origin_not_allowed` | The request came from a browser page, which may not call this server. | Use an MCP client or `curl`, not a web page. |
| `415` with `unsupported_media_type` | The request was not sent as JSON. | Send `Content-Type: application/json`. |
| `413` with `request_too_large` | The request body is over the limit. | Send a smaller request. |
| `429` with `rate_limited` | The key made too many requests. | Wait for `Retry-After` seconds. See [Rate limits](https://legion.ambient.xyz/docs/api/rate-limits). |
| `400` with JSON-RPC error `-32022` | The client asked for a protocol revision the server doesn't speak. The error's `data.supported` lists the ones it does. | Update the client, or let it retry with a listed revision. |
| `400` with JSON-RPC error `-32020` | The `MCP-Protocol-Version`, `Mcp-Method` or `Mcp-Name` header doesn't match the request body. Usually a proxy changed the request, or the client has a bug. | Remove the proxy, or update the client. |
| Only some tools appear | You connected with `?readonly=true`, or your client hides tools. | Remove `?readonly=true` to get the tools that change things. |

## Next steps

- [Tools reference](https://legion.ambient.xyz/docs/agents/tools)
- [Safety](https://legion.ambient.xyz/docs/agents/safety): keep your client's tool approval on.
- [Examples](https://legion.ambient.xyz/docs/agents/examples)
