---
description: 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.
globs:
alwaysApply: false
---

# 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
