# XDC AI - agent setup

You are setting up **XDC AI**, which lets you pay for x402 services on the XDC
network with USDC. You get a **non-custodial smart wallet** (a Safe you own; an
on-chain spending limit set by the human caps what you can spend) and you **never
pay gas** (payments use EIP-3009; a relayer covers the XDC gas).

## 0. Which path? (read first)
- **Shell agents (Claude Code, Cursor, Codex, a terminal):** follow this CLI guide - it needs a shell + outbound network.
- **MCP-capable agents (claude.ai, ChatGPT, custom frameworks):** add the connector `https://api.xdcai.tech/mcp` (Settings → Connectors → Add custom connector), then authenticate with OAuth. The connector exposes `wallet_address`, `wallet_balance`, `marketplace_list`, `call`, and the DeFi tools. Until OAuth completes every tool returns 401 `authorization_required`. Two ways to authenticate:
  - **Browser on the agent's own machine:** your client opens an authorize URL; complete the login in a browser ON THAT MACHINE (the redirect is a localhost loopback).
  - **Headless / remote agent (recommended for a server, a Mac Mini, or a bot you drive by chat):** use the **device-code** flow so you can approve from any device (your phone included), with no browser on the agent's machine:
    1. POST to `https://xdcai.tech/api/oauth/device_authorization` (optionally `{ "client_name": "your agent" }`). You get back `device_code`, `user_code`, `client_id`, `verification_uri` (`https://xdcai.tech/device`), `expires_in`, and `interval`.
    2. Show the human the `user_code` and the URL. They open `https://xdcai.tech/device`, sign in, enter the code, and Approve.
    3. Poll `POST https://xdcai.tech/api/oauth/token` with `grant_type=urn:ietf:params:oauth:grant-type:device_code`, your `device_code`, and `client_id`, every `interval` seconds. It returns `authorization_pending` until approved, then `{ access_token, refresh_token }`. Send `Authorization: Bearer <access_token>` to the MCP server from then on.
  - **No OAuth support at all (Grok custom connectors, many framework MCP clients):** use `https://api.xdcai.tech/mcp/open`. Same tools, no 401 challenge, and it adds the in-chat login tools: call `login_link`, give the user the returned `connectUrl` to open, then poll `login_poll` until it returns a `sessionToken` (or use `login_start` + `login_verify` for an emailed code). Pass that `sessionToken` to the tools that accept one, and call `refresh_session` when it expires.
  - **Static API key (headless agents, CI):** generate one at `https://xdcai.tech/account/settings` and send it as `Authorization: Bearer xdck_...` to either URL. No browser, no OAuth. Clients that cannot read a streamed response get plain JSON back, so a basic HTTP client is enough.

## 1. Install / invoke the CLI

```bash
npx xdcai@latest --help
```

No install step is required - `npx` fetches it on demand.

## 2. Log in (email → one-time code → an XDC wallet)

```bash
npx xdcai@latest login        # accept the Terms, enter email, then the emailed code
npx xdcai@latest wallet address
```

A non-custodial smart wallet is created for the account. Show the user the address to fund.

## 3. Fund the wallet with USDC

Ask the human to send USDC to the wallet address (on XDC mainnet). Check it:

```bash
npx xdcai@latest wallet balance
```

The agent pays **USDC** per call; **gas (XDC) is sponsored** - the user never
needs native XDC.

## 4. Discover services

```bash
npx xdcai@latest marketplace list
npx xdcai@latest marketplace list --search lending,rates,defi   # ranked by relevance
npx xdcai@latest marketplace list --capability xdc.gas-price --max-price 0.01
```

A live example service: `https://api.xdcai.tech` (manifest at
`/`, MCP discovery at `POST /mcp`).

## 5. Pay & call

```bash
npx xdcai@latest call "https://api.xdcai.tech/x402/echo?msg=hi"
```

`call` runs the full x402 loop: request the URL → on HTTP 402 sign a USDC
payment authorization from the wallet → resend with the `X-PAYMENT` header →
return the result. On success it prints the settlement tx and an
`https://xdcscan.com/tx/...` explorer link.

## 6. Transfer USDC (gasless)

```bash
npx xdcai@latest wallet transfer 0.5 USDC 0xRecipientAddress
```

Signs an EIP-3009 transfer; a relayer submits it and pays the gas. Returns the
explorer link and whether it succeeded.

## Reference
- Commands: `login`, `whoami`, `logout`, `wallet address|balance|fund|transfer`,
  `marketplace list`, `call`.
- All output is JSON-friendly for parsing.
- Docs: https://xdcai.tech/integrate
