Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

[v2] requireBearerAuth: drop error="invalid_token" when no credentials were sent and resource_metadata is set (RFC 6750 §3.1)

Aperta Adatta ai principianti
#3,124 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
typescript

Direzione di ricerca

In packages/server, trova requireBearerAuth e la bearerAuthChallengeResponse condivisa usata dall’adattatore Express; inizia leggendo come costruiscono la challenge e i test vicini. Aggiungi o aggiorna i test per credenziali mancanti con resource_metadata o scope, credenziali mancanti senza altri parametri e un token non valido. Il lavoro è completato quando il primo caso omette invalid_token, il secondo mantiene la challenge attuale e la risposta al token non valido resta invariata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

v2
What happened?

When a request has no Authorization header, requireBearerAuth answers:

WWW-Authenticate: Bearer error="invalid_token", error_description="Missing Authorization header", scope="a:read", resource_metadata="https://mcp.example/.well-known/oauth-protected-resource/mcp"

RFC 6750 §3.1 says that when a request has no authentication information, the server SHOULD NOT include an error code.

This was tried in #785 (v1) and closed. The blocker there was §3: a Bearer challenge MUST carry at least one auth-param, so removing the error code meant adding a realm, which didn't fit.

That's different for MCP servers now. resource_metadata (RFC 9728) is the param MCP clients need to start sign-in, and the SDK already adds it when resourceMetadataUrl is set (and scope when requiredScopes is set).

Why it matters: the first request from every MCP client has no token, so this is the challenge every client sees first. invalid_token says "your token went bad" to a client that hasn't signed in yet. We ran into this building the MCP server for Bachs (a payments API), and write the challenge ourselves for now.

Would you take a PR for this? It's a small change plus tests in packages/server (the Express adapter shares bearerAuthChallengeResponse, so it would follow), and a v1.x backport if you want one.

What did you expect?

Drop the error code only when another param remains:

  • no credentials, resource_metadata or scope present → Bearer scope="a:read", resource_metadata="…"
  • no credentials, no other params → unchanged, so the challenge is never a bare "Bearer"
  • a token was sent but is bad → unchanged
Code to reproduce
import { requireBearerAuth } from '@modelcontextprotocol/server';

const gate = requireBearerAuth({
  verifier: { verifyAccessToken: async () => { throw new Error('unused'); } },
  requiredScopes: ['a:read'],
  resourceMetadataUrl: 'https://mcp.example/.well-known/oauth-protected-resource/mcp',
});
const res = await gate(new Request('https://mcp.example/mcp', { method: 'POST' }));
console.log(res.status, res.headers.get('www-authenticate'));
SDK version

@modelcontextprotocol/server 2.4.0

Area

Auth

Lingua principale
TypeScript
Stelle
13.7k
Fork
2.3k
Merge medio
1g 5h
PR unite (30g)
51

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di modelcontextprotocol/typescript-sdk

Tutte le issue di modelcontextprotocol/typescript-sdk

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.