# Set up your agent

> Connect Cursor, Claude Code or Codex in one step, and give your agent the rules it needs to use Nexus402 — or to build for it — correctly the first time.

Source: https://www.nexus402.dev/docs/agents/

Two jobs, and this page covers both. **Developers** connect an agent to the gateway so it can call paid tools. **Creators** have an agent build the server that serves their tools. Either way the agent should know the contract, not guess it, so each job comes with a skill you can drop into the agent you already use. There is nothing to install and no SDK: these are a link, a command, a config entry and two text files.

Every snippet reads your key from the `NEXUS402_API_KEY` environment variable. Set it first, with a key of its own for each agent so its spend cap bounds what that agent can spend:

**Set your key (macOS or Linux; on Windows use setx)**

```bash
export NEXUS402_API_KEY="nx_dev_..."
```

## Install agent tools

Paste this into Claude Code, Codex or Cursor. Your agent connects itself to Nexus402 and adds the skills, for whichever tool it is.

**Prompt for your agent**

```text
Set up Nexus402 for me in this coding agent.
1. Read https://www.nexus402.dev/docs/agents.md and work out which tool you are (Claude Code, Codex or Cursor).
2. Add the Nexus402 MCP server https://api.nexus402.dev/v1/mcp the way that page shows for your tool, sending the X-Nexus-Api-Key header from the NEXUS402_API_KEY environment variable.
3. Install the skills listed at https://www.nexus402.dev/.well-known/skills/index.json where your tool keeps skills or rules.
Never write my key into a file, a commit or a reply. If NEXUS402_API_KEY is not set, stop and tell me to set it.
Then check the connection lists tools, and tell me what you changed.
```

Connect the gateway, then add the two skills.

**Terminal**

```bash
claude mcp add --transport http --scope user nexus402 https://api.nexus402.dev/v1/mcp \
  --header "X-Nexus-Api-Key: $NEXUS402_API_KEY"

mkdir -p ~/.claude/skills/nexus402-tools && curl -fsSL https://www.nexus402.dev/.well-known/skills/nexus402-tools/SKILL.md -o ~/.claude/skills/nexus402-tools/SKILL.md
mkdir -p ~/.claude/skills/nexus402-endpoint && curl -fsSL https://www.nexus402.dev/.well-known/skills/nexus402-endpoint/SKILL.md -o ~/.claude/skills/nexus402-endpoint/SKILL.md
```

Add the gateway to `~/.codex/config.toml`, then add the skills with `npx skills add https://www.nexus402.dev`.

**~/.codex/config.toml**

```toml
[mcp_servers.nexus402]
url = "https://api.nexus402.dev/v1/mcp"
env_http_headers = { "X-Nexus-Api-Key" = "NEXUS402_API_KEY" }
tool_timeout_sec = 130
```

Add to Cursor

Opens Cursor and asks to add the gateway. Your key is read from `NEXUS402_API_KEY`.

Or add it by hand to `~/.cursor/mcp.json`. Then add the skills with `npx skills add https://www.nexus402.dev`.

**~/.cursor/mcp.json**

```json
{
  "mcpServers": {
    "nexus402": {
      "url": "https://api.nexus402.dev/v1/mcp",
      "headers": {
        "X-Nexus-Api-Key": "${env:NEXUS402_API_KEY}"
      }
    }
  }
}
```

Skills-only installs don't update automatically.

**Terminal**

```bash
npx skills add https://www.nexus402.dev
```

Every option reads your key from `NEXUS402_API_KEY`; none writes it into a file. [Learn more](https://www.nexus402.dev/docs/agents/)

## Connect in one step

### Cursor

Add to Cursor

Opens Cursor and asks to add the gateway. Your key is read from `NEXUS402_API_KEY`.

If the button does nothing, add this to `~/.cursor/mcp.json` instead:

**~/.cursor/mcp.json**

```json
{
  "mcpServers": {
    "nexus402": {
      "url": "https://api.nexus402.dev/v1/mcp",
      "headers": {
        "X-Nexus-Api-Key": "${env:NEXUS402_API_KEY}"
      }
    }
  }
}
```

### Claude Code

**Run once in a terminal**

```bash
claude mcp add --transport http --scope user nexus402 https://api.nexus402.dev/v1/mcp \
  --header "X-Nexus-Api-Key: $NEXUS402_API_KEY"
```

Your shell fills in the key when you run it, and Claude Code keeps it in your private `~/.claude.json`. Check it with `claude mcp list`.

### OpenAI Codex

**~/.codex/config.toml**

```toml
[mcp_servers.nexus402]
url = "https://api.nexus402.dev/v1/mcp"
env_http_headers = { "X-Nexus-Api-Key" = "NEXUS402_API_KEY" }
tool_timeout_sec = 130
```

Check it with `codex mcp list`. The full guides, with troubleshooting, are under [Connect with MCP](https://www.nexus402.dev/docs/developers/mcp-clients/).

## Many tools, small context: meta mode

The address above lists every tool your key may call, each with its full schema, at the start of every session. With a large catalogue that is a lot of context. Use `https://api.nexus402.dev/v1/mcp/meta` instead, with the same key and header, and your agent sees four small tools:

- `nexus402_search` finds tools by words, category, price or free calls, ranked by match and measured reliability. Free.
- `nexus402_describe` shows one tool's arguments, examples, price, free calls left and how many paid calls your key's cap still allows. Free.
- `nexus402_call` calls a tool by name. It is charged exactly like a direct call, never more than the `max_price_micro` it passes, and a refusal comes back as an error your agent can read and act on.
- `nexus402_budget` shows the key's 24-hour cap, what it spent and what remains. Free.

Tools your key used in the last week are listed directly beside the four, so a repeated task needs no search.

> **Running an agent with no one approving calls**
>
> Give it a key with a low daily cap, a maximum price per call, and, if the work allows it, a list of tools. Those three are what bound an unattended agent, and only you can set them. A client that "always allows"
>
> nexus402_call
>
> allows every tool the key may call, so use the plain address if you want to approve tools one by one.

## Skills for your agent

Each skill is a short file of rules, checked against the gateway's code on every build. Claude Code loads a skill when the task matches its description; Cursor applies a rule when it is relevant; Codex reads `AGENTS.md` in your repository.

The quickest way to add both is the open `skills` tool, which reads the skills this site publishes at [`/.well-known/skills/index.json`](https://www.nexus402.dev/.well-known/skills/index.json) and asks which of your agents to install them into. It is not ours and installs nothing of ours but these two text files:

**Add both skills to your agents**

```bash
npx skills add https://www.nexus402.dev
```

Or add them by hand, per agent:

| Skill | For | Files |
| --- | --- | --- |
| `nexus402-tools` | Developers whose agent calls paid tools | [Claude skill](https://www.nexus402.dev/.well-known/skills/nexus402-tools/SKILL.md) · [Cursor rule](https://www.nexus402.dev/docs/agents/rules/nexus402-tools.mdc) |
| `nexus402-endpoint` | Creators whose agent builds their tool server | [Claude skill](https://www.nexus402.dev/.well-known/skills/nexus402-endpoint/SKILL.md) · [Cursor rule](https://www.nexus402.dev/docs/agents/rules/nexus402-endpoint.mdc) |

### nexus402-tools

Use when calling tools through the Nexus402 MCP gateway, or when a Nexus402 call fails with 401, 402, 404, 422, 429, 502 or 503. Covers connecting, keys, idempotent retries, free calls and every refusal code.

**Claude Code: install for every project**

```bash
mkdir -p ~/.claude/skills/nexus402-tools && curl -fsSL https://www.nexus402.dev/.well-known/skills/nexus402-tools/SKILL.md -o ~/.claude/skills/nexus402-tools/SKILL.md
```

**Cursor: add as a rule in this project**

```bash
mkdir -p .cursor/rules && curl -fsSL https://www.nexus402.dev/docs/agents/rules/nexus402-tools.mdc -o .cursor/rules/nexus402-tools.mdc
```

**Codex or any agent: paste into AGENTS.md**

```markdown
# Calling tools through Nexus402

Nexus402 is one remote MCP server, `https://api.nexus402.dev/v1/mcp`, that lists every published
tool and charges a fixed price per call to a pre-funded wallet. There is no SDK.

## Connecting
- Send the key in the `X-Nexus-Api-Key` header. Read it from the `NEXUS402_API_KEY`
  environment variable. Never write the key into a file, a commit, a log or a
  reply, and never send it as `Authorization`: the gateway refuses an
  `nx_dev_` key there (`400 Authorization_Rejected`), because
  `Authorization` is passed through to the tool's creator.
- `tools/list`, `initialize` and `ping` are free. Only `tools/call` is billed.
- A tool is named `handle/server/tool`; clients may show it with underscores
  (`mcp__nexus402__handle_server_tool`) and still call the original name.
  The gateway also accepts that underscore form (`handle_server_tool`, the
  tool's listed name) on `tools/call`, billed exactly the same.

## Money
- Each call costs the tool's fixed price, once. `X-Nexus-Price` on the answer
  is what it cost, in dollars (`0.05`); it is absent when nothing was charged.
- A tool may give free calls per day, week or month. Its answers carry
  `X-Nexus-Free-Quota-Remaining`; a free call carries no `X-Nexus-Price`.
- A failure before any output is never charged. Once a result has started
  streaming, the charge stands.

## Retrying safely
- To make a call safe to retry, send `Idempotency-Key: <a new UUID>` with it.
  Reuse that key ONLY to retry the same call.
- `422 Idempotency_Conflict` means that key already paid for a call. Do not
  retry it with the same key: nothing will be charged twice, and the original
  result is not replayed. Use a fresh key only for a genuinely new call.
- Honour `Retry-After` on 429 and 503. Do not loop on 402: it will not pass
  until someone adds funds or raises the key's cap.

## Refusal codes
- `402 Budget_Exhausted` (charged: No). X-Nexus-Required-Amount (and the body's required) is the call's full price in decimal dollars, not the shortfall. Add funds or raise that key's cap.
- `429 Rate_Limited` (charged: No). Honour Retry-After wherever a 429 carries one. Retrying sooner meets the same window and wastes the attempt.
- `429 key_rate_limited` (charged: No). Honour Retry-After, which is the seconds left in the window. Spread load across keys only if that reflects real separate workloads.
- `502 Upstream_Unavailable` (charged: No). Retry. A failure after the response began is not a 502: the status stays 200, the payload_truncated trailer is sent, and the charge stands.
- `503 Lock_Unavailable` (charged: No). Retry with backoff. A 503 is never a silent debit.
- `422 Idempotency_Conflict` (charged: No). Check your call log for the first attempt. Use a fresh key for a new request; a key whose call was refunded may be retried.
- `400 Authorization_Rejected` (charged: No). Put the tool's own credential there, never your Nexus key.
- `404 Unknown_Tool` (charged: No). Check the name against GET /v1/tools. An unpublished tool is not listed there, and neither is a retired version.
- `404 Unknown_Version` (charged: No). Drop the @v pin to resolve the highest published version, or pin one that GET /v1/tools still lists.
- `405 Method_Not_Allowed` (charged: No). Read the Allow header and use the method it names.
- `401` with `{"error":{"code":"unauthorized"}}`: the key is missing, wrong,
  revoked or expired. Some clients show this as a sign-in or OAuth prompt; the
  gateway does not use OAuth. Check `NEXUS402_API_KEY`.

## Reference
- Developer docs: https://www.nexus402.dev/docs/developers/
- Building an agent in code (MCP SDKs, LangChain, OpenAI Agents SDK): https://www.nexus402.dev/docs/developers/frameworks/
- Every error code: https://www.nexus402.dev/docs/developers/errors/
- All docs as Markdown: https://www.nexus402.dev/llms-full.txt
```

### nexus402-endpoint

Use when building, changing or debugging an HTTPS endpoint that serves MCP tools through Nexus402. Covers the signed request, the hello, the X-Nexus-Proof header, the result shape, limits and publishing.

**Claude Code: install for every project**

```bash
mkdir -p ~/.claude/skills/nexus402-endpoint && curl -fsSL https://www.nexus402.dev/.well-known/skills/nexus402-endpoint/SKILL.md -o ~/.claude/skills/nexus402-endpoint/SKILL.md
```

**Cursor: add as a rule in this project**

```bash
mkdir -p .cursor/rules && curl -fsSL https://www.nexus402.dev/docs/agents/rules/nexus402-endpoint.mdc -o .cursor/rules/nexus402-endpoint.mdc
```

**Codex or any agent: paste into AGENTS.md**

```markdown
# Building a Nexus402 tool endpoint

Nexus402 hosts nothing. The creator runs an HTTPS endpoint; the gateway
meters calls and forwards each paid `tools/call` to it, signed. Start from
the tested samples rather than from scratch:
https://www.nexus402.dev/docs/agents/examples/server.mjs and https://www.nexus402.dev/docs/agents/examples/verify.mjs
(Node 18+, no dependencies), or a tested starter kit in Node.js, TypeScript,
Python, Go, PHP, Rust, Java, C# or Ruby at https://www.nexus402.dev/docs/creators/build-a-server/#starters
(each downloads as https://www.nexus402.dev/downloads/starters/nexus402-<language>.zip).

## What the endpoint receives
- Always `POST` to the exact published URL. Body: the developer's MCP
  `tools/call` request, byte for byte. Dispatch on the LAST segment of
  `params.name` (`acme/text@v2/word_count` -> `word_count`).
- `initialize`, `tools/list` and notifications never arrive: the gateway
  answers them from the published description and input schema.
- Headers: `X-Nexus-Signature` (`ed25519:` + base64), `X-Nexus-Timestamp`
  (unix seconds), `X-Correlation-ID`, `X-Nexus-Creator-Key` (`nx_crt_`).

## Verifying
- Signature: Ed25519 over six lines joined by `\n`, no trailing newline:
  method, the host you PUBLISHED (not the inbound Host header), path with
  query, timestamp, correlation id, lowercase hex SHA-256 of the RAW body.
  Public keys: `GET https://api.nexus402.dev/v1/signing-key`; accept any key listed.
- Refuse a timestamp more than 60 seconds from now in either direction.
- Compare `X-Nexus-Creator-Key` in constant time. Keep the key in an
  environment variable, never in the repository.

## The hello
When a version is published, the gateway sends one signed `POST` with an
EMPTY body and NO creator key. Verify its signature, skip the key check, run
no tool, and answer `200` with the proof. Without this, publishing fails
with `422 endpoint_hello_failed`.

## What the endpoint returns
- `X-Nexus-Proof: sha256:` + unpadded base64url of SHA-256 over
  `nexus-endpoint-proof-v1\n<X-Correlation-ID>\n<X-Nexus-Timestamp>\n<X-Nexus-Signature>`.
  Set it on EVERY answer to a verified request, before the status line.
  A `2xx` without a valid proof is voided: nobody is charged or paid.
- On success, the BARE MCP result with `Content-Type: application/json`, for
  example `{"content":[{"type":"text","text":"4"}]}`. Never wrap it in
  `jsonrpc`/`id`: the gateway does, and a second envelope would nest.
- `2xx` is a paid call, even with `"isError": true`. Any `4xx` or `5xx`
  refunds the developer; a proved `4xx` is never counted against the
  endpoint, a `5xx` always is. Twenty failures in a row de-list the version.
- A developer never pays for your failure: when the tool itself fails, answer
  a `5xx`; when the request is wrong before any work, a `4xx`. Keep
  `"isError": true` (paid) for a call the caller's input made fail after the
  tool did work.

## Limits
Headers within 5 s, no silence over 5 s, 120 s per call, 10 MiB response,
HTTPS on port 443 to public addresses only, no redirects followed.

## Reference
- Build a Server: https://www.nexus402.dev/docs/creators/build-a-server/
- Verifying a request: https://www.nexus402.dev/docs/creators/verifying/
- All docs as Markdown: https://www.nexus402.dev/llms-full.txt
```

The creator skill points at the two files our test suite runs against the gateway's real signer, so an agent can start from working code: [`server.mjs`](https://www.nexus402.dev/docs/agents/examples/server.mjs) and [`verify.mjs`](https://www.nexus402.dev/docs/agents/examples/verify.mjs).

## Docs your agent can read

- **Copy for LLM** and **View as Markdown** sit under the title of every docs page.
- Every docs page is also at its URL with `.md` in place of the trailing slash, for example [`/docs/developers/errors.md`](https://www.nexus402.dev/docs/developers/errors.md).
- [`/llms.txt`](https://www.nexus402.dev/llms.txt) is the index, and [`/llms-full.txt`](https://www.nexus402.dev/llms-full.txt) is every docs page in one file.
- When a call fails, [Every error code](https://www.nexus402.dev/docs/developers/errors/#ask-your-agent) has a ready prompt for each refusal.

> **The skills are rules, not code**
>
> Nothing here runs on your machine but what you choose to run. The skill files are plain Markdown with no instructions to fetch or run anything; read them before you add them.
