/saaskit:add-mcp-oauthfrom saaskit
Adds MCP OAuth so an agent can protect the user's MCP server. Use when the user wants MCP OAuth or to protect their MCP server. It does not add API keys or client credentials (that's `add-api-auth`) or expose AgentKit tools over MCP (that's `expose-agentkit-mcp`).
Ships in saaskit. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Put OAuth 2.1 on the user's MCP server. Then stop.
/.well-known/oauth-protected-resource public.WWW-Authenticate and resource_metadata on a missing or invalid token. A bare 401 is a host silent-fail.validateToken with the dashboard Server URL as audience.npm install or start the server.SCALEKIT_ENVIRONMENT_URL, SCALEKIT_CLIENT_ID, SCALEKIT_CLIENT_SECRET. Never SCALEKIT_ENV_URL. Do not prepend https:// — the env URL already has a scheme.authorization_servers by adding a scheme onto SCALEKIT_ENVIRONMENT_URL.app if the file has one. Do not create a second express() app.@scalekit-sdk/node, Express, and MCP imports only when the repo is missing them. Do not run npm install.stdio cannot do OAuth. The default path is Express + StreamableHTTPServerTransport from @modelcontextprotocol/sdk/server/streamableHttp.js. FastAPI or FastMCP → open the matching file under references/ and stay there.
Keep the user's tools. Do not invent a new MCP server if one already exists.
Done when: this skill is the right path, and the transport is Streamable HTTP (or Step 5 will add it).
Write the imports. Do not run npm install. Ask the human to write missing SCALEKIT_ENVIRONMENT_URL, SCALEKIT_CLIENT_ID, and SCALEKIT_CLIENT_SECRET into .env from app.scalekit.com → Developers → Settings → API Credentials.
Put imports at the top of the file.
import express from 'express';
import { ScalekitClient } from '@scalekit-sdk/node';
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
const scalekit = new ScalekitClient(
process.env.SCALEKIT_ENVIRONMENT_URL,
process.env.SCALEKIT_CLIENT_ID,
process.env.SCALEKIT_CLIENT_SECRET
);
Reuse the existing Express app if the file has one. If there is no app yet:
const app = express();
app.use(express.json());
Do not register the MCP POST here. That route goes on after the Bearer middleware in Step 5.
Done when: the imports are in the file, and the client initializes from those env vars.
Print this checklist. Ask the human to write missing SCALEKIT_ENVIRONMENT_URL, SCALEKIT_CLIENT_ID, and SCALEKIT_CLIENT_SECRET into .env. Then write the routes. Do not invent those env values or Metadata JSON.
http://localhost:3002/, keep the trailing slash).Audience is that Server URL. If Server URL is empty, use the generated resource id.
Done when: the checklist is printed, and the routes use a Metadata JSON placeholder plus the Server URL (local default http://localhost:3002/).
Serve the copied Metadata JSON at /.well-known/oauth-protected-resource. Keep this route public. Register it before the Bearer middleware.
const metadata = /* paste dashboard Metadata JSON */;
app.get('/.well-known/oauth-protected-resource', (req, res) => {
res.json(metadata);
});
If you must edit authorization_servers, join SCALEKIT_ENVIRONMENT_URL with /resources/<RESOURCE_ID>. Do not prepend https://.
Done when: that path returns the dashboard JSON with no auth.
Register order: well-known → this auth → MCP POST. Express runs handlers in registration order.
const audience = 'http://localhost:3002/'; // dashboard Server URL
const metadataUrl = `${audience.replace(/\/$/, '')}/.well-known/oauth-protected-resource`;
const wwwAuthenticate = `Bearer realm="OAuth", resource_metadata="${metadataUrl}"`;
async function auth(req, res, next) {
if (req.path.includes('.well-known')) return next();
const header = req.headers.authorization || '';
const token = header.startsWith('Bearer ') ? header.slice(7).trim() : '';
if (!token) return res.status(401).set('WWW-Authenticate', wwwAuthenticate).end();
try {
await scalekit.validateToken(token, { audience: [audience] });
next();
} catch {
return res.status(401).set('WWW-Authenticate', wwwAuthenticate).end();
}
}
app.use(auth);
If POST / (or the existing MCP path) is already on the stack, move it below app.use(auth), or attach auth on that route: app.post('/', auth, handler).
If the repo has no Streamable HTTP route yet, add it now. Keep an existing path if the server already has one. Default is POST /.
const server = new McpServer({ name: 'mcp-server', version: '1.0.0' });
app.post('/', async (req, res) => {
const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined });
await server.connect(transport);
await transport.handleRequest(req, res, req.body);
});
If the file has no listener, add app.listen(Number(process.env.PORT) || 3002). Reuse an existing listener.
Done when: auth sits above the MCP POST, a request without a token returns 401 with WWW-Authenticate and resource_metadata, and a listener is on 3002 (or PORT).
If the user asked for tool-level scopes, re-validate with requiredScopes. Extra Express notes: references/express.md.
await scalekit.validateToken(token, {
audience: [audience],
requiredScopes: ['todo:write'],
});
Insufficient scope → 403 insufficient_scope.
Done when: scopes are skipped, or a tool checks requiredScopes.
curl -i -X POST http://localhost:3002/
curl -i http://localhost:3002/.well-known/oauth-protected-resource
Use the real MCP path if it is not /. Expect 401 + WWW-Authenticate with resource_metadata on the MCP POST, and JSON with resource, authorization_servers, and scopes_supported on well-known. Do not start the server. Do not run the curls.
Done when: the human has both curl commands.
Write the Node files and stop. Do not run npm install. Do not start the server. Do not write API keys. Do not expose AgentKit tools over MCP.
Done when: well-known is public, Bearer middleware validates audience, and this skill has stopped.
setup-saaskit if env is missingadd-api-auth for API keys or client credentialsexpose-agentkit-mcp to expose AgentKit tools over MCPWhat SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub scalekit-inc/authstack --plugin saaskit15 skills in this plugin
/add-api-authAdds API key or client-credentials auth so an agent can protect an API. Use when the user wants an API key or client credentials. It does not put OAuth 2.1 on an MCP server (that's `add-mcp-oauth`) or write SaaSKit login (that's `implement-saaskit`).
/check-saaskit-prodChecks SaaSKit production so an agent can finish a go-live checklist. Use when the user wants SaaSKit production or to go live SaaSKit. It does not write login (that's `implement-saaskit`) or run dryrun (that's `run-dryrun`).
/deploy-self-hostedDeploys self-hosted Scalekit so an agent can install Helm or on-prem. Use when the user wants self-hosted Scalekit or Helm. It does not write SaaSKit login (that's `implement-saaskit`).
/implement-access-controlImplements roles and permissions at a route so an agent can protect an endpoint. Use when the user wants roles, permissions, or RBAC. It does not write SaaSKit login (that's `implement-saaskit`).
/implement-saaskitImplements SaaSKit login so an agent can add a login route, a callback, session cookies, and logout. Use when the user wants to add SaaSKit login, a callback, or logout. It does not store, validate, refresh, or revoke a session after login (that's `manage-saaskit-sessions`) or write Next.js App Router auth (that's `implement-saaskit-nextjs`) or write Django, FastAPI, or Flask auth (that's `implement-saaskit-python`).
/implement-saaskit-nextjsImplements SaaSKit login in the Next.js App Router so an agent can add login, callback, session cookies, and logout. Use when the user wants SaaSKit Next.js or App Router auth. It does not write generic Node login (that's `implement-saaskit`) or write Django, FastAPI, or Flask auth (that's `implement-saaskit-python`).