# ContextLLM API Documentation

> ContextLLM — deep research &amp; planning for coding. Your agent can ask for the latest technology over our ContextLLM MCP for deep research and planning.

---

## MCP (JSON-RPC 2.0)

**Endpoint:** `POST https://trucopilot.com/api/contextllm/mcp`

**Auth:** Bearer token in the `Authorization` header. Tokens are prefixed `cllm_*` and are minted from the Account → ContextLLM page.

**Transport:** MCP Streamable HTTP (JSON-RPC 2.0 over HTTP POST). Compatible with Claude Code, Cursor, and any MCP client.

### Claude Code config

```json
{
  "mcpServers": {
    "trucopilot-contextllm": {
      "url": "https://trucopilot.com/api/contextllm/mcp",
      "headers": { "Authorization": "Bearer cllm_YOUR_TOKEN" }
    }
  }
}
```

---

## MCP Tools

### resolve-library

Find a repository by library or project name. Returns canonical owner/repo, stars, language, and description.

**Input**

| Field | Type   | Required | Description                                      |
|-------|--------|----------|--------------------------------------------------|
| name  | string | yes      | Library or project name (e.g. `"react"`, `"tokio"`) |

**Request**

```json
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "resolve-library",
    "arguments": { "name": "tokio" }
  }
}
```

**Response**

```json
{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "content": [{
      "type": "text",
      "text": "{\"full_name\":\"tokio-rs/tokio\",\"stars\":27000,\"language\":\"Rust\",\"description\":\"A runtime for writing reliable async Rust applications.\"}"
    }]
  }
}
```

---

### get-docs

Retrieve a repo's summary, README excerpt, and top content chunks for a given `owner/repo`.

**Input**

| Field     | Type   | Required | Description                                         |
|-----------|--------|----------|-----------------------------------------------------|
| full_name | string | yes      | Repository in `owner/repo` form (e.g. `"tokio-rs/tokio"`) |

**Request**

```json
{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "tools/call",
  "params": {
    "name": "get-docs",
    "arguments": { "full_name": "tokio-rs/tokio" }
  }
}
```

**Response**

```json
{
  "jsonrpc": "2.0",
  "id": 2,
  "result": {
    "content": [{
      "type": "text",
      "text": "{\"summary_md\":\"Tokio is an async runtime for Rust ...\",\"readme_url\":\"https://github.com/tokio-rs/tokio/blob/master/README.md\",\"chunks\":[\"Tokio uses an epoll-based I/O driver ...\",\"The scheduler is work-stealing ...\"]}"
    }]
  }
}
```

---

### ask

Deep research question answered over the full indexed corpus. Uses RAG: retrieve → rerank → synthesize. Returns a cited Markdown answer.

**Input**

| Field   | Type   | Required | Description                        |
|---------|--------|----------|------------------------------------|
| message | string | yes      | Research question in natural language |

**Request**

```json
{
  "jsonrpc": "2.0",
  "id": 3,
  "method": "tools/call",
  "params": {
    "name": "ask",
    "arguments": { "message": "How does Tokio schedule async tasks?" }
  }
}
```

**Response**

```json
{
  "jsonrpc": "2.0",
  "id": 3,
  "result": {
    "content": [{
      "type": "text",
      "text": "{\"answer_md\":\"Tokio uses a work-stealing scheduler ...\",\"citations\":[{\"full_name\":\"tokio-rs/tokio\",\"html_url\":\"https://github.com/tokio-rs/tokio\"}]}"
    }]
  }
}
```

---

### plan

Generate a coding plan and recommended repo picks for a build goal. Returns structured Markdown with architecture guidance and a shortlist of relevant repositories.

**Input**

| Field   | Type   | Required | Description                              |
|---------|--------|----------|------------------------------------------|
| message | string | yes      | Description of what you want to build    |

**Request**

```json
{
  "jsonrpc": "2.0",
  "id": 4,
  "method": "tools/call",
  "params": {
    "name": "plan",
    "arguments": { "message": "Build a distributed task queue in Go with priority lanes" }
  }
}
```

**Response**

```json
{
  "jsonrpc": "2.0",
  "id": 4,
  "result": {
    "content": [{
      "type": "text",
      "text": "{\"plan_md\":\"## Architecture\\n\\n1. Use `asynq` for Redis-backed queue...\\n\\n## Recommended repos\\n...\",\"repos\":[{\"full_name\":\"hibiken/asynq\",\"html_url\":\"https://github.com/hibiken/asynq\"}]}"
    }]
  }
}
```

---

### get-trending

Return the current trending slice of the indexed corpus (mirrors GitHub trending, refreshed every 24 hours). No input required.

**Input**

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| *(none)* | — | — | — |

**Request**

```json
{
  "jsonrpc": "2.0",
  "id": 5,
  "method": "tools/call",
  "params": {
    "name": "get-trending",
    "arguments": {}
  }
}
```

**Response**

```json
{
  "jsonrpc": "2.0",
  "id": 5,
  "result": {
    "content": [{
      "type": "text",
      "text": "[{\"full_name\":\"anthropics/anthropic-sdk-python\",\"stars\":4200,\"language\":\"Python\",\"description\":\"The official Python library for the Anthropic API.\"}]"
    }]
  }
}
```

---

## HTTP Endpoints

These endpoints are accessible without an MCP client. They require a Bearer token in the `Authorization` header except where noted.

### POST /api/contextllm/ask

Deep research over the corpus. Same pipeline as the `ask` MCP tool.

**Headers**

```
Authorization: Bearer cllm_YOUR_TOKEN
Content-Type: application/json
```

**Body**

```json
{ "message": "How does React reconcile the virtual DOM?" }
```

**Response 200**

```json
{
  "answer_md": "React uses a fiber-based reconciliation algorithm ...",
  "citations": [
    { "full_name": "facebook/react", "html_url": "https://github.com/facebook/react" }
  ]
}
```

**Error responses**

| Status | Meaning                                 |
|--------|-----------------------------------------|
| 401    | Missing or invalid Bearer token         |
| 429    | Rate limit exceeded                     |
| 503    | LLM provider unavailable                |

---

### POST /api/contextllm/plan

Generate a coding plan + repo picks. Same pipeline as the `plan` MCP tool.

**Headers**

```
Authorization: Bearer cllm_YOUR_TOKEN
Content-Type: application/json
```

**Body**

```json
{ "message": "Build a Rust CLI that streams JSON logs to Elasticsearch" }
```

**Response 200**

```json
{
  "plan_md": "## Approach\n\nUse `serde_json` for parsing ...",
  "repos": [
    { "full_name": "serde-rs/serde", "html_url": "https://github.com/serde-rs/serde" }
  ]
}
```

---

### GET /api/contextllm/search

Semantic repository search over the indexed corpus. No auth required.

**Query parameters**

| Parameter | Type    | Required | Default | Description                           |
|-----------|---------|----------|---------|---------------------------------------|
| q         | string  | yes      | —       | Natural-language search query         |
| limit     | integer | no       | 10      | Max results to return (1–50)          |

**Example**

```
GET /api/contextllm/search?q=async+runtime+rust&limit=5
```

**Response 200**

```json
{
  "repos": [
    {
      "full_name": "tokio-rs/tokio",
      "stars": 27000,
      "language": "Rust",
      "description": "A runtime for writing reliable async Rust applications.",
      "score": 0.94
    }
  ]
}
```

---

## Public Pages

| Path                       | Description                                                |
|----------------------------|------------------------------------------------------------|
| `GET /contextllm`          | Product page: Ask chatbox, setup & API, submit a source    |
| `GET /contextllm/docs`     | This documentation page (interactive HTML)                 |
| `GET /contextllm/docs.md`  | Raw Markdown version of this file                          |

*(The corpus directory is an internal admin view, not a public page.)*

---

## Rate Limits

| Surface                  | Limit                         |
|--------------------------|-------------------------------|
| Public chatbox (IP)      | 20 requests / minute          |
| Public ask/plan (IP)     | 5 requests / minute           |
| Submit a source (IP)     | 6 requests / minute           |
| API (token)              | 60 requests / minute          |
| Embedding / ingest       | Governed by provider quotas   |

Responses exceeding the limit return HTTP 429 with a `Retry-After` header.

---

## Changelog

See `CHANGELOG.md` for version history and breaking changes.
