Skip to main content
Glama
README.md
# mini-swe-mcp

A [`mini-swe-agent`](https://github.com/SWE-agent/mini-swe-agent) harness exposed as a stateless MCP server with a built-in single-user OAuth 2.0 backend and optional [RTK](https://www.rtk-ai.app/docs/) output filtering.

Instead of bundling its own LLM loop, your MCP client (e.g. Claude) acts as the model while this server provides `mini-swe-agent`'s exact environment, tool, and observation format:

```
MCP client ──OAuth──▶ /authorize (one password) ──▶ bearer token ──▶ POST /mcp
                                                                         │
                       tool "bash" ──▶ (optional rtk rewrite) ──▶ bash -lc (detached pgid)
                                                                         │
                       JSON observation { returncode, output } ◀─────────┘
```

## What makes it `mini-swe-agent`

- **Single `bash` tool**: matches `mini-swe-agent`'s `BASH_TOOL` (`{ command }`, plus optional `timeout` and optional `raw` when `rtk_enabled` is on). No custom file-edit tools — files are viewed and edited via standard bash (`nl`, `sed`, `cat <<'EOF'`).
- **`LocalEnvironment` execution**: every command runs in a fresh, non-persistent subshell (`bash -lc`) in its own process group (`detached: true` + `kill(-pid, SIGKILL)` on timeout so child processes are never orphaned), with `mini.yaml`'s environment variables (`PAGER=cat`, `MANPAGER=cat`, `LESS=-R`, `PIP_PROGRESS_BAR=off`, `TQDM_DISABLE=1`, plus `GIT_PAGER=cat`, `TERM=dumb`, `NO_COLOR=1`).
- **`mini.yaml` JSON observation format**: returns `{ "returncode": 0, "output": "..." }`, or `{ "returncode": 0, "output_head": "...", "output_tail": "...", "elided_chars": N, "warning": "Output too long." }` when output exceeds `MAX_OUTPUT_CHARS` (default `10000`, keeping 5,000 head + 5,000 tail chars in bounded memory), plus `exception_info` on timeout/spawn errors.
- **Same OAuth 2.0 backend**: one password in `.env`, one baked OAuth client, PKCE S256, dynamic client registration, refresh token rotation, brute-force lockout, stateless Streamable HTTP `/mcp`.

## Quick start

Requires [Bun](https://bun.sh) and `bash`. RTK is optional (`rtk_enabled` in `config.json`).

```sh
bun install
cp .env.example .env      # set MCP_PASSWORD, OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET
bun run start             # listens on 0.0.0.0:8080 per config.json
```

Point an MCP client at `http://localhost:8080/mcp` (or your public tunnel origin). The client receives `401` with `resource_metadata`, discovers the OAuth endpoints, renders the password login form, and returns with a bearer token.

Scripts: `bun run start`, `bun run dev` (watch mode), `bun run typecheck`.

## The `bash` tool

| argument | | |
| --- | --- | --- |
| `command` | required | Bash command executed in a fresh subshell (`bash -lc`). |
| `raw` | optional | Bypass RTK and return unfiltered output with a larger character cap (`MAX_RAW_OUTPUT_CHARS`). Only present when `rtk_enabled` is `true`. |
| `timeout` | optional | 1–600 seconds; default from `config.json` `timeout` or `SHELL_TIMEOUT` (`60`). On expiry, the entire process group receives `SIGKILL`, partial output is preserved, `returncode` is `-1`, and `exception_info` reports the timeout. |

### Observation format

Normal output (`< MAX_OUTPUT_CHARS`, default `10000`):

```json
{
  "returncode": 0,
  "output": "hello world\n"
}
```

Truncated output (`> MAX_OUTPUT_CHARS`):

```json
{
  "returncode": 0,
  "output_head": "...",
  "output_tail": "...",
  "elided_chars": 42000,
  "warning": "Output too long."
}
```

Timeout or execution error (also sets MCP `isError: true`; ordinary non-zero exit codes set `isError: false` and report `returncode` in the JSON payload):

```json
{
  "returncode": -1,
  "output": "partial output before timeout\n",
  "exception_info": "An error occurred while executing the command: Command '...' timed out after 5 seconds"
}
```

## Optional RTK integration

When `"rtk_enabled": true` in `config.json`:

1. Before running a command (unless `raw: true`), the server runs `rtk rewrite "<command>"`. If RTK provides a filtered equivalent, that command runs instead and `rtk_command` is included in the JSON observation.
2. If RTK elides output and prints `rtk recall <hash>`, the JSON observation includes `"rtk_recall": ["rtk recall <hash>"]` so the model can retrieve full output using the same `bash` tool.
3. Passing `raw: true` skips RTK and sets `"rtk_bypassed": true`.

Setting `"rtk_enabled": false` removes RTK completely from execution, the `bash` tool schema, and JSON observations.

## OAuth backend

Unchanged from `shell-mcp`:

```
POST /mcp                                    → 401 + WWW-Authenticate: Bearer resource_metadata="…"
GET  /.well-known/oauth-protected-resource   → resource, authorization_servers
GET  /.well-known/oauth-authorization-server → endpoints, S256, grant types
POST /oauth/register                         → 201, baked client_id (DCR)
GET  /oauth/authorize                        → password login form
POST /oauth/authorize                        → 302 redirect_uri?code=…&state=…
POST /oauth/token                            → access_token + refresh_token
POST /oauth/revoke                           → drops the token
```

- Authorization codes: single-use, 60s TTL, bound to `redirect_uri` and PKCE S256 challenge.
- Tokens: in-memory opaque bearer tokens (`TOKEN_TTL`, default 12h) with rotating refresh tokens.
- Brute-force protection: 8 failed logins within 5 minutes triggers a 60s lockout.

## Configuration

`config.json` (override path with `CONFIG_PATH`):

```json
{
  "url": "https://shell.example.com",
  "host": "0.0.0.0",
  "port": 8080,
  "allowed_redirects": [],
  "allow_any_redirect": false,
  "rtk_enabled": true
}
```

Optional keys in `config.json`: `cwd`, `timeout`, `max_output_chars`, `max_raw_output_chars`, `descriptions`.

`descriptions` lets you override the `bash` tool and parameter descriptions exposed to the MCP client (each key is optional and falls back to the built-in default):

| key | |
| --- | --- |
| `tool` | Base description of the `bash` tool. |
| `rtk` | Suffix appended to `tool` when `rtk_enabled` is `true` (set to `""` to omit). |
| `command` | Description of the `command` parameter. |
| `raw` | Description of the `raw` parameter (used when `rtk_enabled` is `true`). |
| `timeout` | Description of the `timeout` parameter; `{timeout}` is replaced with the effective default timeout in seconds. |

Environment variables (`.env`):

| variable | default | |
| --- | --- | --- |
| `MCP_PASSWORD` | required | Single user password for `/oauth/authorize`. |
| `OAUTH_CLIENT_ID` / `OAUTH_CLIENT_SECRET` | required | Baked OAuth client credentials. |
| `CONFIG_PATH` | `./config.json` | Alternate config file. |
| `SHELL_WORKDIR` | `$HOME` | Working directory for subshells. |
| `SHELL_TIMEOUT` | `60` | Default per-command timeout in seconds. |
| `RTK_BIN` | `rtk` | Path to RTK binary when `rtk_enabled` is true. |
| `MAX_OUTPUT_CHARS` | `10000` | Observation truncation threshold (head + tail). |
| `MAX_RAW_OUTPUT_CHARS` | `65536` | Truncation threshold when `raw: true`. |
| `TOKEN_TTL` | `43200` | Access token lifetime in seconds. |
| `MCP_USER` | `user` | Label shown on the login form. |

## Project layout

```
src/config.ts       config.json + env loading & validation
src/environment.ts  LocalEnvironment execution, process-group kill, RTK rewrite, JSON observation formatter
src/mcp.ts          McpServer with bash tool
src/oauth.ts        OAuth 2.0 authorization server (PKCE S256, DCR, tokens, lockout)
src/index.ts        Bun.serve HTTP router, bearer auth, stateless Streamable HTTP MCP transport
```