# OAuth 2.1 & Device Flow for Automated Crypto Trading Agents

> Eterna MCP uses OAuth 2.1 (`https://ai-auth.eterna.exchange`) with PKCE for interactive clients (Claude, Cursor, ChatGPT) and OAuth Device Authorization Flow for headless terminals and CLIs (`@eterna-hybrid-exchange/cli`). Every OAuth sign-in automatically provisions an isolated no-KYC trading sub-account with zero raw exchange API secrets exposed to the LLM.

- Canonical URL: https://ai.eterna.exchange/guides/mcp-oauth-authentication-for-agents
- Last updated: November 23, 2026
- Author: Eterna Engineering (Security & Agent Architecture Team)
- MCP endpoint: https://mcp.eterna.exchange/mcp

Static exchange API keys are the #1 security failure mode in AI trading bots: keys leak into chat logs, prompt histories, or `.env` files with overly broad permissions. Eterna replaces static API keys with standard OAuth 2.1 discovery and scoped tokens.

## Key Facts

| Metric | Value | Notes |
|--------|-------|-------|
| Auth Issuer | ai-auth.eterna.exchange | RFC 8414 & RFC 9728 discovery |
| Scope | mcp:full | Scoped to isolated agent sub-account |
| Secrets in Prompt | Zero | LLM never sees exchange signing keys |
| Account Isolation | 1 sub-account / auth | Blast-radius containment by default |

## 1. Automatic MCP OAuth Discovery Flow

Modern MCP clients discover Eterna's authorization server automatically without manual client IDs.

1. **Client requests protected resource metadata**: When an MCP client connects to `https://mcp.eterna.exchange/mcp`, an unauthenticated request returns `401` pointing to `/.well-known/oauth-protected-resource`.
2. **Authorization server metadata & PKCE**: The client reads `/.well-known/oauth-authorization-server` on `ai-auth.eterna.exchange`, performs dynamic registration if needed, and opens a browser login with PKCE.
3. **Sub-account auto-provisioning**: On first sign-in, Eterna creates an isolated trading sub-account automatically (no KYC, no separate `register_agent` tool call required).
4. **Token refresh & revocation**: Short-lived access tokens authenticate each `execute_code` call; revoking access immediately cuts off agent execution without rotating master account keys.

## 2. Common Auth Issues & Fixes

| Error | Cause | Fix |
|-------|-------|-----|
| 401 Unauthorized on /mcp | Access token expired or missing Authorization Bearer header | Re-authenticate via your client's /mcp menu or run `npx @eterna-hybrid-exchange/cli auth login`. |
| OAuth callback failed in local IDE | Browser popup blocked or stale cached MCP session | Clear the cached MCP OAuth token in Cursor/Claude settings and reconnect to `https://mcp.eterna.exchange/mcp`. |
| Insufficient balance after login | Funds were deposited to Funding Wallet but not transferred to Trading Wallet | Call `eterna.getAllCoinsBalance()` and `eterna.transferToTrading({ coin: 'USDT', amount })`. |

## Frequently Asked Questions

### Does the LLM ever see my underlying exchange API key?

No. The LLM only passes TypeScript code to `execute_code`. Signing and exchange execution happen server-side inside the authenticated gateway.

### Is there a `register_agent` MCP tool I need to call first?

No. There is no `register_agent` tool. Signing in via OAuth 2.1 provisions your isolated sub-account automatically.

## References & Primary Sources

- [Eterna Auth Markdown Guide](https://ai.eterna.exchange/auth.md): Machine-readable authentication reference.
- [OAuth Authorization Server Discovery](https://ai.eterna.exchange/.well-known/oauth-authorization-server): Live RFC 8414 metadata endpoint.

## Related Guides

- [No-KYC Crypto Trading for AI Agents: Isolated Sub-Accounts & OAuth 2.1](https://ai.eterna.exchange/guides/no-kyc-ai-crypto-trading)
- [Running Multiple AI Trading Agents Safely with Isolated Sub-Accounts](https://ai.eterna.exchange/guides/multi-agent-crypto-portfolio-isolation)
- [Eterna CLI Guide: Terminal TypeScript Strategies with `@eterna-hybrid-exchange/cli`](https://ai.eterna.exchange/guides/eterna-cli-trading-automation)
- [OpenAI Agents SDK & Codex MCP Crypto Trading](https://ai.eterna.exchange/integrations/openai-agents-sdk)
