ctx4-ai
MCP server for portable context management across AI assistants, providing tools to store and retrieve persistent context, instructions, and execute sandboxed bash commands with automatic git commits, using OAuth 2.1 and magic link authentication.
README
ctx4.ai
Your portable context layer for Claude & ChatGPT
Free to use: ctx4-ai.vercel.app
Features
- MCP Server — Powered by mcp-handler. Supports Streamable HTTP and SSE transports via a single dynamic route.
- OAuth 2.1 Authorization — Full authorization code flow with PKCE, dynamic client registration, and a consent screen. MCP clients authenticate automatically.
- Magic Link Auth — Passwordless sign-in with Supabase magic links. No passwords to manage.
- Token Verification — Server-side JWT verification using Supabase JWKS. MCP tool calls are authenticated and scoped to the signed-in user.
- Protected Resource Metadata — Standards-compliant
/.well-known/oauth-protected-resourceendpoint for automatic MCP client configuration. - Shadcn UI + Tailwind v4 — Pre-configured UI components with Tailwind CSS v4, Biome linting, and formatting.
How It Works
MCP Client (e.g. Claude, Cursor)
│
├─ Discovers OAuth config via /.well-known/oauth-protected-resource
├─ Registers dynamically with Supabase OAuth 2.1 Server
├─ Opens browser → user signs in (magic link) → approves scopes
└─ Exchanges code for tokens (PKCE)
│
▼
app/[transport]/route.ts ← MCP handler (mcp-handler)
│
├─ verifyToken() ← validates Bearer JWT via Supabase JWKS
└─ server tools ← MCP tools (ctx_instructions, ctx_bash)
The MCP handler in app/[transport]/route.ts responds on /mcp (HTTP transport). Authentication is handled by withMcpAuth, which delegates to lib/auth/verify-token.ts for JWT validation.
Supabase session middleware runs via proxy.ts, protecting browser routes while allowing Bearer-authenticated MCP requests to pass through.
Quick Start
Prerequisites
1. Clone and install
git clone https://github.com/dubzdubz/ctx4-ai.git
cd ctx4-ai
pnpm install
2. Configure environment
cp .env.example .env
Fill in your credentials (see .env.example for the full list):
# Supabase (from Dashboard → Settings → API)
NEXT_PUBLIC_SUPABASE_URL=https://your-project.supabase.co
NEXT_PUBLIC_SUPABASE_ANON_KEY=your-anon-key
SUPABASE_SERVICE_ROLE_KEY=your-service-role-key
NEXT_PUBLIC_APP_URL=http://localhost:3000
# Database (Supabase PostgreSQL connection string)
DATABASE_URL=postgresql://postgres.[project-ref]:[password]@aws-0-[region].pooler.supabase.com:6543/postgres
# GitHub App (from GitHub Developer Settings)
NEXT_PUBLIC_GITHUB_APP_SLUG=your-app-slug
GITHUB_APP_ID=123456
GITHUB_APP_PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\n...\n-----END RSA PRIVATE KEY-----"
GITHUB_APP_CLIENT_ID=Iv1.xxxxxxxxxx
GITHUB_APP_CLIENT_SECRET=xxxxxxxxxxxx
# Vercel (for sandbox auth — run `vercel link && vercel env pull`)
VERCEL_OIDC_TOKEN=your-vercel-oidc-token
3. Configure Supabase Auth
Magic link auth:
In Supabase Dashboard → Authentication → URL Configuration:
- Set Site URL to
http://localhost:3000(or your production URL) - Add
http://localhost:3000/auth/confirmto Redirect URLs
OAuth 2.1 Server:
In Supabase Dashboard → Authentication → OAuth Server:
- Enable OAuth 2.1 Server
- Set Authorization Path to:
/auth/oauth/authorize - (Optional) Enable Dynamic Client Registration to allow MCP clients to self-register
4. Run the dev server
pnpm dev
Open http://localhost:3000 to see the app.
Connecting an MCP Client
Add the server to your MCP client (Claude, Cursor, etc.):
{
"servers": {
"ctx4-ai": {
"url": "http://localhost:3000/mcp"
}
}
}
The client will automatically:
- Discover OAuth configuration from Supabase
- Register itself dynamically (if enabled)
- Open browser for user authorization
- Exchange tokens and make authenticated MCP requests
Running Onboarding (Optional but Recommended)
Once connected, run the onboarding prompt to set up your context repo:
/ctx4:onboarding
This will scaffold your repo and ask a few questions about your preferences and workflow.
Adding System Prompt (Optional but Recommended)
For best results, add this to your client's custom instructions or system prompt:
Use the ctx4 MCP to manage my long-term context and memory. Always call ctx_instructions first before using ctx_bash to understand how to interact with my context. Use it to store preferences, learnings, and anything that should persist across conversations.
Where to add:
- Claude: Settings → General → Personal Preferences
- ChatGPT: Settings → Personalization → Custom Instructions
Testing with MCP Inspector
Use the MCP Inspector to test and debug your server:
npx @mcpjam/inspector@latest
Available Tools
| Tool | Description |
|---|---|
ctx_instructions |
Returns full context bootstrap (guide + instructions + context + resource/skill indexes) |
ctx_bash |
Sandboxed bash in /vercel/sandbox with auto git commit and push |
Project Structure
├── app/
│ ├── [transport]/route.ts # MCP handler (HTTP transport)
│ ├── .well-known/ # OAuth protected resource metadata
│ ├── api/
│ │ ├── github/ # GitHub App endpoints (callback, repos, select-repo, disconnect)
│ │ └── oauth/approve/route.ts # OAuth consent approval endpoint
│ ├── auth/
│ │ ├── confirm/page.tsx # Magic link confirmation
│ │ ├── error/page.tsx # Auth error page
│ │ ├── login/page.tsx # Login page (magic link form)
│ │ └── oauth/authorize/page.tsx # OAuth consent screen
│ ├── settings/page.tsx # User settings / profile + GitHub integration
│ ├── layout.tsx # Root layout
│ ├── page.tsx # Landing page
│ └── globals.css # Tailwind v4 + Shadcn theme
├── components/
│ ├── auth/ # Auth components (login, logout, consent)
│ ├── github/ # GitHub integration UI (repo manager)
│ └── ui/ # Shadcn UI components
├── lib/
│ ├── auth/verify-token.ts # JWT verification for MCP auth
│ ├── db/ # Drizzle ORM (schema, queries, client)
│ ├── github/app.ts # GitHub App client (Octokit, installation tokens)
│ ├── sandbox/ # SandboxManager, SandboxManagerPool, scanner
│ ├── supabase/ # Supabase client/server/middleware
│ └── tools/ # MCP tool implementations
├── drizzle/ # Database migrations
├── proxy.ts # Next.js middleware (session + route protection)
├── drizzle.config.ts # Drizzle ORM configuration
├── biome.jsonc # Biome linter/formatter config
└── .env.example # Environment variables template
Customization
Adding MCP Tools
Register new tools in app/[transport]/route.ts:
server.registerTool(
"my_tool",
{
title: "My Tool",
description: "Does something useful.",
inputSchema: {
query: z.string(),
},
},
async ({ query }, extra) => {
const userEmail = extra.authInfo?.extra?.email;
return {
content: [{ type: "text", text: `Result for ${query}` }],
};
},
);
Modifying Auth Scopes
Edit the requiredScopes array in app/[transport]/route.ts to enforce specific OAuth scopes:
const authHandler = withMcpAuth(handler, verifyToken, {
required: true,
requiredScopes: ["openid", "email"], // empty by default (no scope enforcement)
// ...
});
Adjusting Route Protection
Edit proxy.ts and lib/supabase/middleware.ts to change which paths are public or protected. The MCP transport route (/mcp) is automatically bypassed when a Bearer token is present.
Tech Stack
| Dependency | Purpose |
|---|---|
| Next.js 16 | React framework (App Router) |
| mcp-handler | MCP server adapter for Next.js |
| @modelcontextprotocol/sdk | MCP SDK |
| Supabase | Auth (OAuth 2.1) & PostgreSQL |
| Drizzle ORM | Database ORM & migrations |
| Vercel Sandbox | Isolated command execution (Firecracker microVM) |
| Octokit | GitHub App integration |
| Shadcn UI + Tailwind v4 | UI components & styling |
| Biome | Linting & formatting |
References
- mcp-handler — Vercel adapter for MCP servers on Next.js
- Supabase OAuth 2.1 Server — OAuth 2.1 and OIDC identity provider docs
- MCP Inspector — Test and debug MCP servers
- MCP Server Concepts — Tools, resources, and prompts
- MCP Authorization Specification — OAuth flow for MCP
- Next.js Documentation — Framework docs
License
Apache-2.0 — see LICENSE.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。