# Claude Code Api

> OpenAI-compatible API wrapper for Anthropic's Claude Code CLI, enabling integration with any OpenAI API client (like opencode, Continue, etc.) while leveraging Claude's advanced coding capabilities. Supports both OAuth and API key authentication with a built-in web interface for testing.

- **Type:** MCP server
- **Install:** `agentstack add mcp-bethington-claude-code-api`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [bethington](https://agentstack.voostack.com/s/bethington)
- **Installs:** 0
- **Category:** [AI & ML](https://agentstack.voostack.com/c/ai-and-ml)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [bethington](https://github.com/bethington)
- **Source:** https://github.com/bethington/claude-code-api

## Install

```sh
agentstack add mcp-bethington-claude-code-api
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# Claude Code API

OpenAI-compatible API wrapper for Claude Code CLI. Enables any OpenAI-compatible client (like [opencode](https://github.com/sst/opencode)) to use Claude Code's full capabilities including file editing, bash execution, and tool use.

## Features

- **OpenAI-compatible API** - Works with any client that supports OpenAI's chat completions API
- **Hybrid pool architecture** - Hot workers for active conversations, automatic eviction of idle workers
- **Conversation persistence** - Sessions saved to disk, can be resumed later
- **Streaming support** - Real-time SSE streaming responses
- **Docker ready** - Easy deployment with Docker Compose

## Quick Start

1. **Clone and configure:**
   ```bash
   cd claude-code-api
   cp .env.example .env
   # Edit .env and add your ANTHROPIC_API_KEY
   ```

2. **Start with Docker Compose:**
   ```bash
   docker-compose up -d
   ```

3. **Test the API:**
   ```bash
   curl http://localhost:3000/health

   curl -X POST http://localhost:3000/v1/chat/completions \
     -H "Content-Type: application/json" \
     -d '{"model":"claude-code","messages":[{"role":"user","content":"Hello!"}]}'
   ```

## API Endpoints

### Chat Completions (OpenAI-compatible)
```
POST /v1/chat/completions
```

**Request:**
```json
{
  "model": "claude-code",
  "messages": [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Hello!"}
  ],
  "stream": false
}
```

**Conversation ID:** Specify via:
- `X-Conversation-ID` header (recommended)
- Model suffix: `"model": "claude-code:my-conversation"`
- Auto-generated if not provided

### Other Endpoints

| Endpoint | Method | Description |
|----------|--------|-------------|
| `/health` | GET | Health check and stats |
| `/v1/models` | GET | List available models |
| `/conversations` | GET | List active conversations |
| `/conversations/:id` | DELETE | Evict a conversation |

## Configuration with opencode

Add to your `opencode.json`:

```json
{
  "provider": {
    "claude-code": {
      "npm": "@ai-sdk/openai-compatible",
      "options": {
        "baseURL": "http://localhost:3000/v1"
      },
      "models": {
        "claude-code": {
          "limit": {
            "context": 200000,
            "output": 64000
          }
        }
      }
    }
  }
}
```

## Environment Variables

| Variable | Default | Description |
|----------|---------|-------------|
| `ANTHROPIC_API_KEY` | (required) | Your Anthropic API key |
| `POOL_SIZE` | 5 | Maximum concurrent workers |
| `IDLE_TIMEOUT_MS` | 300000 | Worker idle timeout (5 min) |
| `PORT` | 3000 | Server port |
| `WORKSPACE_DIR` | /workspace | Working directory for Claude |

## Architecture

```
┌─────────────────────────────────────────────────────────────┐
│                     Docker Container                         │
│                                                              │
│  ┌────────────────────────────────────────────────────┐     │
│  │           Express.js API Server (server.js)         │     │
│  │  POST /v1/chat/completions (OpenAI-compatible)     │     │
│  └────────────────────────┬───────────────────────────┘     │
│                           │                                  │
│  ┌────────────────────────▼───────────────────────────┐     │
│  │           Hybrid Pool Manager (pool.js)             │     │
│  │  - Session affinity routing                         │     │
│  │  - LRU eviction when at capacity                   │     │
│  │  - Automatic idle cleanup                          │     │
│  └────────────────────────┬───────────────────────────┘     │
│                           │                                  │
│  ┌────────────────────────▼───────────────────────────┐     │
│  │           Claude Code Workers (worker.js)           │     │
│  │  ┌─────────┐  ┌─────────┐  ┌─────────┐            │     │
│  │  │Worker 1 │  │Worker 2 │  │Worker 3 │  ...       │     │
│  │  │conv-abc │  │conv-xyz │  │ (idle)  │            │     │
│  │  └─────────┘  └─────────┘  └─────────┘            │     │
│  │                                                     │     │
│  │  claude -p --input-format stream-json              │     │
│  │           --output-format stream-json              │     │
│  │           --session-id            │     │
│  └─────────────────────────────────────────────────────┘     │
│                                                              │
│  Session Storage: /root/.claude/                             │
└──────────────────────────────────────────────────────────────┘
```

## Development

**Run locally (without Docker):**
```bash
npm install
export ANTHROPIC_API_KEY=sk-ant-...
npm start
```

**Run with file watching:**
```bash
npm run dev
```

## License

MIT

## Source & license

This open-source MCP server is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [bethington](https://github.com/bethington)
- **Source:** [bethington/claude-code-api](https://github.com/bethington/claude-code-api)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** yes
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** yes
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/mcp-bethington-claude-code-api
- Seller: https://agentstack.voostack.com/s/bethington
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
