Implements OAuth 2.1 + PKCE authentication for remote MCP servers, covering dynamic client registration, token refresh, and scope design per the 2025 MCP auth spec.
Ships in mcp-mastery. 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.
None: SKILL.md is the whole skill.
Remote MCP servers use OAuth 2.1 + PKCE. Spec-compliant MCP clients discover your authorization server, obtain a client ID without manual setup, and authenticate users in a browser.
Spec status: check the current authorization spec at https://modelcontextprotocol.io/specification/latest/basic/authorization before building. In current revisions, servers MUST publish OAuth Protected Resource Metadata (RFC 9728), clients MUST send the
resourceparameter (RFC 8707), clients and authorization servers SHOULD support Client ID Metadata Documents for registration, and Dynamic Client Registration (RFC 7591) is deprecated, kept for backwards compatibility. The DCR example below still works with clients that use it; add CIMD support for new servers.
401 with WWW-Authenticate: Bearer resource_metadata="…" pointing to its Protected Resource Metadata/.well-known/oauth-authorization-server (or OpenID discovery) → gets authorization_endpoint, token_endpoint, and optionally registration_endpointclient_id: a Client ID Metadata Document URL (preferred), a pre-registered ID, or Dynamic Client Registration (deprecated fallback)authorization_endpoint with PKCE challenge → user approvestoken_endpoint → receives access + refresh tokensAuthorization: Bearer <token>No manual app registration for the user. They just click "Allow" in a browser.
// GET /.well-known/oauth-authorization-server
app.get("/.well-known/oauth-authorization-server", (req, res) => {
res.json({
issuer: "https://mcp.example.com",
authorization_endpoint: "https://mcp.example.com/oauth/authorize",
token_endpoint: "https://mcp.example.com/oauth/token",
registration_endpoint: "https://mcp.example.com/oauth/register",
scopes_supported: ["read:issues", "write:issues"],
response_types_supported: ["code"],
grant_types_supported: ["authorization_code", "refresh_token"],
code_challenge_methods_supported: ["S256"],
token_endpoint_auth_methods_supported: ["none"], // public clients
});
});
app.use("/mcp", (req, res, next) => {
const token = req.headers.authorization?.replace("Bearer ", "");
if (!token || !verifyJwt(token)) {
res.setHeader(
"WWW-Authenticate",
`Bearer resource_metadata="https://mcp.example.com/.well-known/oauth-protected-resource"`,
);
return res.status(401).json({ error: "unauthorized" });
}
req.user = decodeJwt(token);
next();
});
app.post("/oauth/register", async (req, res) => {
const { redirect_uris, client_name } = req.body;
const clientId = `mcp_${crypto.randomUUID()}`;
await db.clients.insert({ clientId, redirectUris: redirect_uris, name: client_name });
res.json({
client_id: clientId,
redirect_uris,
token_endpoint_auth_method: "none",
grant_types: ["authorization_code", "refresh_token"],
});
});
app.get("/oauth/authorize", async (req, res) => {
const { client_id, redirect_uri, code_challenge, code_challenge_method, state, scope } = req.query;
if (code_challenge_method !== "S256") return res.status(400).send("S256 required");
// render consent UI; on approval:
const code = crypto.randomUUID();
await db.codes.insert({ code, clientId: client_id, userId: req.session.userId, codeChallenge: code_challenge, scope });
res.redirect(`${redirect_uri}?code=${code}&state=${state}`);
});
app.post("/oauth/token", async (req, res) => {
const { grant_type, code, code_verifier, refresh_token } = req.body;
if (grant_type === "authorization_code") {
const row = await db.codes.findAndDelete({ code });
const hash = crypto.createHash("sha256").update(code_verifier).digest("base64url");
if (hash !== row.codeChallenge) return res.status(400).json({ error: "invalid_grant" });
return res.json(issueTokens(row.userId, row.scope));
}
if (grant_type === "refresh_token") {
const userId = verifyRefresh(refresh_token);
return res.json(issueTokens(userId));
}
});
Group by resource + action, not by tool:
read:issues → list/get issues
write:issues → create/update/close issues
read:repos → list/get repos
admin:webhooks → create/delete webhooks
Don't use full_access — clients can't reason about what they're granting.
Map scopes to MCP tools in your handler:
server.tool("create_issue", "...", schema, async (args, { authInfo }) => {
if (!authInfo.scopes.includes("write:issues")) {
throw new Error("Missing scope: write:issues. Reconnect with this scope enabled.");
}
// ...
});
MCP clients that implement the authorization spec (Claude Code and Claude Desktop among them) handle the OAuth flow when you configure a remote server without static headers:
{
"mcpServers": {
"linear": { "type": "http", "url": "https://mcp.linear.app/mcp" }
}
}
WWW-Authenticate header is present on 401/oauth/register must accept application/json and not require authinvalid_scopeplain and missing challenges/.well-known/oauth-protected-resource metadata doc (required by the MCP authorization spec) and validate that tokens were issued for your server (audience / resource)What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
/mcp-mastery:mcp-auth-oauthfrom mcp-mastery
npx claudepluginhub latestaiagents/agent-skills --plugin mcp-mastery7 skills in this plugin
/mcp-client-integrationConnect MCP servers to AI clients — Claude Desktop, Claude Code, Cursor, the Claude Agent SDK, and custom apps — including config file locations, auth, and debugging connection failures.
Use this skill when the user wants to wire an MCP server into a client, troubleshoot "server not connecting" errors, or build a custom MCP client with the SDK.
Activate when: MCP client, mcp.json, claude_desktop_config.json, connect MCP, MCP not working, list MCP tools, programmatic MCP client.
/mcp-resource-patternsUse MCP Resources correctly — the read-only, URI-addressable data primitive — and know when to pick resources, tools, or prompts. Covers templated URIs, subscriptions, and common mistakes.
Use this skill when designing MCP servers that expose data, deciding between tool vs resource, or implementing resource subscriptions for live data.
Activate when: MCP resource, resource template, resource vs tool, subscribe resource, MCP URI schema.
/mcp-security-sandboxingSecure MCP servers against prompt injection, tool abuse, excessive permission, and data exfiltration. Covers per-tool scopes, rate limiting, audit logging, and sandbox patterns for shell-adjacent tools.
Use this skill when deploying an MCP server to production, handling untrusted agents, or reviewing an MCP server for security issues.
Activate when: MCP security, MCP prompt injection, tool sandbox, MCP audit log, MCP rate limit, tool abuse, MCP threat model.
Other plugins' skills for the same job
2 plugins in one repository ship this exact skill · first indexed Nov 5, 2026