Skip to main content
Glama
README.md
# mcp-oauth-starter

[![CI](https://github.com/konstantinbozukov/mcp-oauth-starter/actions/workflows/ci.yml/badge.svg)](https://github.com/konstantinbozukov/mcp-oauth-starter/actions/workflows/ci.yml)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)

Copy-and-run **OAuth 2.1 remote MCP** starter for Cloudflare Workers: per-user token isolation, MCP tool annotations, structured audit logging, rate limits, and a built-in directory pre-flight.

## What you get

- **OAuth 2.1** split deployment: authorization server (`auth-server/`) + MCP resource server (`mcp-server/`) linked by a Service Binding
- **Per-user isolation** via encrypted `props` (`userId`, `login`, `upstreamAccessToken`, `scopes`) — no cross-request token cache
- **Fixed-scope tools** with MCP annotations (`readOnlyHint`, `destructiveHint`, …)
- **Audit log** — one JSON line per tool call (no secrets or argument bodies)
- **Rate limit** — per-user fixed window (`429` + `Retry-After`)
- **Pre-flight** — `node scripts/preflight.mjs` for directory-style checks

Upstream sign-in defaults to **GitHub OAuth** (demo). Swap it for your own IdP or your product’s OAuth in `auth-server/`.

## Quickstart

```bash
git clone https://github.com/konstantinbozukov/mcp-oauth-starter.git
cd mcp-oauth-starter
npm ci
cp .dev.vars.example auth-server/.dev.vars   # fill GitHub OAuth placeholders
```

Create a Cloudflare KV namespace for `OAUTH_KV` and set IDs in `auth-server/wrangler.jsonc` (placeholders only in repo).

Terminal 1 (auth server):

```bash
npm run dev -w auth-server
```

Terminal 2 (MCP resource server):

```bash
npm run dev -w mcp-server
```

Local defaults: auth `http://127.0.0.1:8787`, MCP `http://127.0.0.1:8788/mcp`.

### Connect from Claude / Cursor / MCP Inspector

```json
{
  "mcpServers": {
    "mcp-oauth-starter": {
      "url": "http://127.0.0.1:8788/mcp"
    }
  }
}
```

Complete the browser consent + GitHub sign-in when the client starts OAuth. Request scopes `mcp:read`, `mcp:write`, and `offline_access` for full tool access.

## Tools

| Tool | Title | Read-only | Destructive | Notes |
| --- | --- | --- | --- | --- |
| `whoami` | Who am I | yes | no | Login, id, granted scopes |
| `list_my_repos` | List my repositories | yes | no | `limit` 1–50, `visibility` enum |
| `create_issue` | Create issue | no | no | Needs `mcp:write` + `confirm: true` |
| `close_issue` | Close issue | no | **yes** | Needs `mcp:write` + `confirm: true` |

## Architecture

```mermaid
sequenceDiagram
  participant Client as MCP client
  participant MCP as mcp-server (resource)
  participant Auth as auth-server (AS)
  participant GH as GitHub OAuth/API

  Client->>MCP: POST /mcp (no token)
  MCP-->>Client: 401 WWW-Authenticate
  Client->>Auth: OAuth authorize + PKCE
  Auth->>GH: Upstream authorize
  GH-->>Auth: Callback + code
  Auth-->>Client: Redirect with code
  Client->>Auth: Token exchange
  Client->>MCP: POST /mcp Bearer token
  MCP->>Auth: validateToken (service binding)
  MCP->>GH: Tool call with user upstream token
```

## Security model

- MCP access tokens are validated on every request; upstream tokens live only in encrypted `props`.
- No module-level cache of upstream tokens (see `tests/isolation.test.ts`).
- Audit records include `userId`, `tool`, `outcome`, `durationMs`, optional `upstreamStatus` — never tokens or free-text arguments.

## Pre-flight

```bash
node scripts/preflight.mjs http://127.0.0.1:8788/mcp            # local, no auth wrapper
node scripts/preflight.mjs http://127.0.0.1:8788/mcp --auth oauth
```

Checks include: `initialize` + `tools/list`, annotation/schema rules, catch-all detection, prompt-injection scan, error quality, optional response size (`--sample '{...}'`), and OAuth discovery when `--auth oauth`.

## Adapting to your API (five edits)

1. Replace GitHub upstream in `auth-server/src/index.ts` and `auth-server/src/github-upstream.ts` with your IdP helpers.
2. Change tool implementations in `mcp-server/src/create-server.ts` / `mcp-server/src/github-api.ts` to call **your** REST API using `props.upstreamAccessToken`.
3. Update `scopesSupported` / `MCP_SCOPES` in `shared/oauth-scopes.ts` and consent copy in `auth-server/src/consent.ts`.
4. Set production URLs in both `wrangler.jsonc` files (`ISSUER`, `MCP_SERVER_URL`, `AUTH_SERVER_URL`).
5. Rename registry metadata in `server.json` / `glama.json` to a namespace you control (see note in `server.json`).

## Directory readiness

See [docs/DIRECTORY-READINESS.md](docs/DIRECTORY-READINESS.md). Registry files are **prepared only** — nothing in this repo publishes to the MCP Registry or third-party directories.

## Deploy (your Cloudflare account)

1. `npx wrangler kv namespace create OAUTH_KV` (auth worker) and update `wrangler.jsonc`.
2. `npx wrangler secret put GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET` on **auth-server** (do not commit).
3. Deploy **auth-server** first, then **mcp-server** (service binding target must exist).
4. Point clients at `https://<mcp-worker>/mcp`.
5. When ready, publish `server.json` under your own `io.github.<org>/...` namespace (manual step; not automated here).

## Development

```bash
npm ci
npm run typecheck
npm test
```

Tests use Vitest in Node with mocked `fetch` and OAuth shims — no credentials and no network (aligned with brief: plain Vitest + in-process handlers, not `@cloudflare/vitest-pool-workers` due to Vitest 5 peer mismatch at build time).

---

Built with AI assistance and human review. Part of the mcp.edge.report showcase. MIT licensed.