Skip to content

feat: add OAuth scope challenges and service tokens - #421

Open
HugoRCD wants to merge 1 commit into
v1-08-toolkit-typesfrom
v1-09-oauth-challenges
Open

HugoRCD wants to merge 1 commit into
v1-08-toolkit-typesfrom
v1-09-oauth-challenges

Conversation

@HugoRCD

@HugoRCD HugoRCD commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Written by an AI agent on behalf of @HugoRCD. @HugoRCD has reviewed it.

Testing the 1.0 OAuth path in a real consumer (nuxt/nuxt.com#2567) turned up three gaps against the MCP 2025-11-25 authorization spec. The 401 challenge never named any scopes, so clients fell back to requesting everything. A call refused for a missing scope returned a 403 with no WWW-Authenticate, so clients could not step up; the official TypeScript SDK client just failed the call. And there was no supported way to accept a static token for a non-interactive caller (an agent, a cron job) next to OAuth tokens: auth and oauth cannot coexist, and jwt and verify must both pass. This PR adds scope to the 401 challenge, sends RFC 6750's insufficient_scope challenge when a scoped definition refuses a modern request, and adds createMcpOAuth({ serviceTokens }).

To try it, use the snippet below with the PR build (npm i https://pkg.pr.new/nitro-mcp-toolkit@421, available once the preview job publishes):

import { createMcpHandler, createMcpOAuth, defineMcpTool } from 'nitro-mcp-toolkit'

export default createMcpHandler({
  oauth: createMcpOAuth({
    resource: 'https://example.com/mcp',
    authorizationServers: ['https://auth.example.com'],
    jwt: { jwks: 'https://auth.example.com/.well-known/jwks.json' },
    scopesSupported: ['todos:read', 'todos:write'],
    serviceTokens: [{
      token: () => process.env.AGENT_TOKEN,
      claims: { sub: 'agent', scope: 'todos:read todos:write' },
    }],
  }),
  tools: [defineMcpTool({ name: 'remove-todo', scopes: ['todos:write'], handler: () => 'removed' })],
})

Before and after

The same two modern tools/call requests to the same handler (scopesSupported: ['todos:read', 'todos:write'], tool scoped to todos:write), first without a token, then with a token holding only todos:read. Before is #420's head 987571d.

== before (987571d)
no token: HTTP 401
  www-authenticate: Bearer realm="mcp", resource_metadata="https://example.com/.well-known/oauth-protected-resource/mcp"
token with todos:read only: HTTP 403
  www-authenticate: (none)
exit 0
== after
no token: HTTP 401
  www-authenticate: Bearer realm="mcp", resource_metadata="https://example.com/.well-known/oauth-protected-resource/mcp", scope="todos:read todos:write"
token with todos:read only: HTTP 403
  www-authenticate: Bearer error="insufficient_scope", scope="todos:read todos:write", resource_metadata="https://example.com/.well-known/oauth-protected-resource/mcp"
exit 0

The insufficient_scope challenge asks for the scopes the token already holds plus the ones the call needs, the spec's recommended approach, so a client does not lose access it had. A denial from authorize stays a bare 403, since asking for more scopes cannot fix it. Legacy requests are unchanged: they report the same JSON-RPC -32003 inside a 200 stream, which has nowhere to carry a challenge.

Service tokens are read on each request, so they can come from private runtime configuration; an empty value matches nothing. They are compared in constant time, skip jwt and verify, and set fixed claims on event.context.oauth, so scopes and authorize still apply. An entry without claims.sub throws when the OAuth instance is created. The docs recommend OAuth for people, since a service token never expires.

Removing the 403 challenge or the service-token match each makes one of the new tests in test/oauth-runtime.test.ts fail (checked by hand). The flow has not been run against Claude, Cursor or VS Code with this build; the nuxt.com consumer will switch to serviceTokens once this preview publishes.

ELI5

When an app knocks on an MCP server without the right key, the server now says exactly which permissions to ask for, instead of just saying "no". It also lets a trusted robot use a fixed password while people keep signing in normally.

@vercel

vercel Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
nuxt-mcp-toolkit-docs Ready Ready Preview Nov 25, 2026 1:54pm UTC

Request Review

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for following the naming conventions! 🙏

@pkg-pr-new

pkg-pr-new Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
npm i https://pkg.pr.new/nitro-mcp-toolkit@421
npm i https://pkg.pr.new/@nuxtjs/mcp-toolkit@421

commit: 3df6edb

This branch was successfully deployed

1 active deployment
Preview — 3df6edb6 Deployed Nov 25, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant