Skip to main content
Glama
README.md
# Serverless MCP Connector

[![CI](https://github.com/jamesfourth/serverless-mcp-connector/actions/workflows/ci.yml/badge.svg)](https://github.com/jamesfourth/serverless-mcp-connector/actions/workflows/ci.yml)
[![MCP Protocol](https://img.shields.io/badge/MCP-2025--03--26%20Streamable%20HTTP-0f766e.svg)](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#streamable-http)
[![Runtime Dependencies](https://img.shields.io/badge/runtime%20dependencies-0-16a34a.svg)](./package.json)
[![Line Coverage Gate](https://img.shields.io/badge/coverage%20gate-%E2%89%A590%25%20lines-2563eb.svg)](./.github/workflows/ci.yml)
[![Node.js](https://img.shields.io/badge/node-%3E%3D22-339933.svg)](./package.json)
[![License: MIT](https://img.shields.io/badge/license-MIT-64748b.svg)](./LICENSE)

A zero-dependency, stateless **Model Context Protocol (MCP) Streamable HTTP** connector built for **Vercel**:

- **Static / Edge layer (`public/`)** — Zero-build service status page (`index.html`) and discovery manifest (`/.well-known/mcp.json`) served directly from Vercel's Edge CDN.
- **Serverless MCP Route (`api/mcp.ts`)** — Stateless JSON-RPC 2.0 Streamable HTTP handler (`POST`, `DELETE`, `OPTIONS`; `GET` returns `405` per spec) that delivers prompts to a downstream agent and returns the response.
- **Pluggable Agent Backend (`lib/agent/`)** — Supports `openai` (and OpenAI-compatible APIs), `anthropic`, `gemini`, custom HTTP `webhook`, and a deterministic `mock` provider for local testing.
- **Pluggable Storage (`lib/storage/`)** — Supports **Vercel Blob** (REST API), **Cloudflare R2** (S3-compatible API with built-in AWS SigV4 signing), and a **local filesystem** fallback for dev. Persists execution logs and offloads large response artifacts with MCP resource URIs.
- **Bearer Auth (`lib/auth.ts`)** — Constant-time `Authorization: Bearer <MCP_API_KEY>` / `x-api-key` verification with an explicit `MCP_ALLOW_PUBLIC=true` toggle for local dev (fails closed in production if unconfigured).

## Architecture

```text
MCP Client (Claude / Cursor / Agent)
   │  POST /api/mcp (JSON-RPC 2.0 + Bearer Auth)
   ▼
Vercel Serverless Function (api/mcp.ts)
   ├── lib/auth.ts        → Timing-safe Bearer / x-api-key check & CORS
   ├── lib/mcp/           → Stateless MCP Streamable HTTP server
   ├── lib/agent/         → Downstream agent delivery (OpenAI / Anthropic / Gemini / Webhook / Mock)
   └── lib/storage/       → Execution logs & large artifacts (Vercel Blob / Cloudflare R2 / Local)

Vercel Static / Edge CDN (public/)
   ├── /                  → Minimal service status & client config manifest
   └── /.well-known/mcp.json → Static discovery descriptor
```

## Testing & Verification

Every claim (zero runtime dependencies, MCP Streamable HTTP conformance, Bearer auth, AWS SigV4 signing, R2/Blob persistence, and artifact offloading) is verified automatically in [`.github/workflows/ci.yml`](./.github/workflows/ci.yml) on **Node 22 and Node 24** and can be reproduced locally in seconds:

```bash
# 1. Run all unit, integration, and MCP conformance tests (enforces >= 90% line coverage)
npm run test:coverage

# 2. Run the live HTTP black-box MCP conformance verifier against an ephemeral local server
npm run verify

# 3. Run the same black-box verifier against a live deployed Vercel URL
MCP_TARGET_URL=https://your-deployment.vercel.app MCP_API_KEY=your-secret npm run verify

# 4. Run strict TypeScript typechecking (after npm install)
npm run typecheck
```

| Verification Layer | Command | What It Validates |
| :--- | :--- | :--- |
| Unit & Conformance Suite | `npm run test:coverage` | 22 tests across auth/CORS, SigV4 + R2/Blob/Local storage, all 5 agent adapters, JSON-RPC error codes (`-32700` to `-32002`), batch requests, SSE framing, artifact offloading, and `>=90%` line coverage (`96.6%` current). |
| Live HTTP Verifier | `npm run verify` | 9 end-to-end HTTP checks against `/api/health`, `/.well-known/mcp.json`, and `/api/mcp` (`401` enforcement, `405` on `GET`, `initialize`, `202` notification, `deliver_prompt`, `resources/read`, SSE framing, `delete_run`). |
| CI Zero-Dep Gate | `.github/workflows/ci.yml` | Asserts `package.json` has `0` runtime `dependencies` and runs strict `tsc --noEmit`. |

## Quickstart (Local)

Requires **Node.js 22+** (runs TypeScript natively with zero build step and zero runtime dependencies):

```bash
cp .env.example .env.local
npm run test:coverage
npm run verify
npm run dev
```

Verify the running server with `curl`:

```bash
# Health check
curl http://127.0.0.1:3000/api/health

# Deliver a prompt via MCP tools/call
curl -X POST http://127.0.0.1:3000/api/mcp \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/call",
    "params": {
      "name": "deliver_prompt",
      "arguments": {
        "prompt": "Summarize the latest deployment status."
      }
    }
  }'
```

## MCP Surface

### Tools
| Tool | Description |
| :--- | :--- |
| `deliver_prompt` | Sends `prompt` (with optional `systemPrompt`, `model`, `temperature`, `maxTokens`, `metadata`, `persist`, `saveAsArtifact`) to the downstream agent and returns the response plus storage resource links. |
| `list_runs` | Lists persisted execution logs from Blob/R2 with pagination (`limit`, `cursor`). |
| `get_run` | Retrieves a persisted run log by `runId` (and optionally loads the offloaded artifact body). |
| `delete_run` | Removes a persisted run log and its offloaded artifact from storage. |

### Resources
| URI Template | MIME Type | Contents |
| :--- | :--- | :--- |
| `mcp://connector/runs/{runId}` | `application/json` | Full execution log (prompt, model, latency, token usage, preview, metadata). |
| `mcp://connector/artifacts/{runId}` | `text/markdown` | Full offloaded response body when size exceeds `ARTIFACT_INLINE_THRESHOLD_BYTES` (default `16384`) or `saveAsArtifact: true`. |

### Prompts
- `agent_handoff` — Formats an `objective`, optional `context`, and optional `format` into a structured handoff message.

## Configuration

See [`.env.example`](./.env.example) for all options. Key variables:

### Security
- `MCP_API_KEY` — Required Bearer token for `/api/mcp`.
- `MCP_ALLOW_PUBLIC` — Set `true` only for unauthenticated local dev. In production (`VERCEL_ENV=production`), `/api/mcp` fails closed (`503`) if `MCP_API_KEY` is unset and `MCP_ALLOW_PUBLIC` is not `true`.

### Agent Provider (`AGENT_PROVIDER`)
- `mock` (default) — Zero-config local mock agent.
- `openai` — Requires `AGENT_API_KEY`; optional `AGENT_MODEL` (`gpt-4o-mini`), `AGENT_BASE_URL` (for OpenRouter, Groq, etc.).
- `anthropic` — Requires `AGENT_API_KEY`; optional `AGENT_MODEL` (`claude-3-5-sonnet-latest`).
- `gemini` — Requires `AGENT_API_KEY`; optional `AGENT_MODEL` (`gemini-2.5-flash`).
- `webhook` — Requires `AGENT_WEBHOOK_URL`; optional `AGENT_API_KEY` sent as `Authorization: Bearer`.

### Storage Backend (`STORAGE_PROVIDER`)
- `auto` (default) — Selects `vercel-blob` if `BLOB_READ_WRITE_TOKEN` is set, else `r2` if `R2_*` credentials are set, else `local`.
- `vercel-blob` — Requires `BLOB_READ_WRITE_TOKEN`.
- `r2` — Requires `R2_ACCOUNT_ID` (or `R2_ENDPOINT`), `R2_BUCKET_NAME`, `R2_ACCESS_KEY_ID`, `R2_SECRET_ACCESS_KEY` (optional `R2_REGION=auto`).
- `local` — Writes to `LOCAL_STORAGE_DIR` (defaults to `.data/storage`, or `/tmp/mcp-connector-storage` on Vercel).

## Deploy to Vercel

1. Push this repository to GitHub or run `vercel` directly from the project root.
2. In Vercel Project Settings → Environment Variables, set:
   - `MCP_API_KEY` (generate a strong random secret) and `MCP_ALLOW_PUBLIC=false`
   - `AGENT_PROVIDER` + corresponding provider key (`AGENT_API_KEY` or `AGENT_WEBHOOK_URL`)
   - Storage: either link a **Vercel Blob** store (auto-sets `BLOB_READ_WRITE_TOKEN`) or configure **Cloudflare R2** (`R2_ACCOUNT_ID`, `R2_BUCKET_NAME`, `R2_ACCESS_KEY_ID`, `R2_SECRET_ACCESS_KEY`).
3. Verify the live deployment with `MCP_TARGET_URL=https://<your-deployment>.vercel.app MCP_API_KEY=<key> npm run verify`.
4. Point your MCP client to `https://<your-deployment>.vercel.app/api/mcp`:

```json
{
  "mcpServers": {
    "serverless-connector": {
      "type": "http",
      "url": "https://<your-deployment>.vercel.app/api/mcp",
      "headers": {
        "Authorization": "Bearer YOUR_MCP_API_KEY"
      }
    }
  }
}
```

## License

[MIT](./LICENSE)