[v2] requireBearerAuth: drop error="invalid_token" when no credentials were sent and resource_metadata is set (RFC 6750 §3.1)
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
- Ambito
- api, authentication
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
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
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di modelcontextprotocol/typescript-sdk
-
The `maxToolInputElements` refusal lacks the `Input validation error:` prefix that the schema refusal carriesForse già presa @barry166 l’ha presa 3 giorni fa. Apertav1 v2
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
modelcontextprotocol/typescript-sdk#3111 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
server: registeredResource.update and registeredResourceTemplate.update desync title and metadata.title when updating metadata aloneForse già presa Una pull request collegata a questa issue è aperta o già unita. Apertav1 v2
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
modelcontextprotocol/typescript-sdk#3103 · 5 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
[v1.x] Protocol.request keeps its abort listener after the request resolved, so a later abort sends notifications/cancelled for an answered request (1.30.0)Forse già presa Una pull request collegata a questa issue è aperta o già unita. Apertav1
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
modelcontextprotocol/typescript-sdk#3090 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
[v2] e2e `protocol:timeout:max-total` is flaky over SSE: fake timers advance before the request reaches the serverForse già presa @Singmin l’ha presa 12 giorni fa. Apertav1 v2
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
modelcontextprotocol/typescript-sdk#3045 ·
I maintainer di solito rispondono entro 1 giorno
-
[v1.x] Backport #2654: `notifications/cancelled` with `requestId: 0` is ignored, so the first server→client request cannot be cancelled (still in 1.32.1)Forse già presa @CloudCorpRecords l’ha presa 13 giorni fa. Apertav1
Difficoltà 2/5 1-3 ore Idoneità per principianti 77/100
modelcontextprotocol/typescript-sdk#3043 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di modelcontextprotocol/typescript-sdk
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
components-web-app/docs#307 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
localstack/localstack-mcp-server#112 ·
I maintainer di solito rispondono entro 1 giorno
-
area:frontend kind:bug priority:p1 ready
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
I maintainer di solito rispondono entro 1 giorno
-
bug ready-for-agent
Difficoltà 2/5 1-3 ore Idoneità per principianti 73/100
PhyberApex/kuroshiro#1687 ·
I maintainer di solito rispondono entro 1 giorno
-
Performance (Web Vitals) collector: LCP/FCP from background tabs are not discarded, inflating p75Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
umami-software/umami#4720 ·
I maintainer di solito rispondono entro 3 giorni