401 and error for a request with no authorization header (for a request with no token) - as per spec no error should be added

Offen Anfängerfreundlich
#3,769 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
68/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
python

Rechercherichtung

Lies zuerst RequireAuthMiddleware._send_auth_error und die Tests für die Auth-Middleware; im Issue wird die Methode genannt und eine Reproduktion mit AuthSettings.required_scopes bereitgestellt. Vergleiche den WWW-Authenticate-Header für ein fehlendes Token mit dem zitierten RFC 6750 und den MCP-Hinweisen zu Scopes. Fertig ist die Arbeit, wenn die Antwort bei einem fehlenden Token die Fehlerfelder weglässt und den konfigurierten Scope enthält und Tests diesen Fall abdecken.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

bug v2
What happened?

Title: Auth spec conformance (minor): 401 challenge contents,
Environment: mcp 2.3.0 · Python 3.12.3 · spec revision 2026-07-28
A small difference from the spec text, only shows up in a corner case.

1. The 401 for a request with no token

RequireAuthMiddleware._send_auth_error sends error="invalid_token" both for a missing token and for an invalid one, and never adds scope, even when required_scopes is set.

Output: 401 Bearer error="invalid_token", error_description="Authentication required", resource_metadata="http://localhost:8001/.well-known/oauth-protected-resource/mcp"

What did you expect?
1. The 401 for a request with no token

RFC 6750 §3.1: when a request lacks any authentication, the resource server "SHOULD NOT include an error code or other error information". MCP Scope Selection Strategy: servers "SHOULD include a scope parameter in the WWW-Authenticate header". The spec's own 401 example carries resource_metadata and scope, and no error.

Expected (no token): Bearer resource_metadata="…", scope="files:read"

Code to reproduce
### 1. The 401 for a request with no token
import asyncio, httpx2
from mcp.server.auth.provider import AccessToken
from mcp.server.auth.settings import AuthSettings
from mcp.server.mcpserver import MCPServer

class RejectAll:
    async def verify_token(self, token: str) -> AccessToken | None: return None

async def main() -> None:
    server = MCPServer("s", token_verifier=RejectAll(), auth=AuthSettings(
        issuer_url="https://as.example", resource_server_url="http://localhost:8001/mcp",
        required_scopes=["files:read"], validate_token_resource=True))
    app = server.streamable_http_app(stateless_http=True)
    async with app.router.lifespan_context(app):
        async with httpx2.AsyncClient(transport=httpx2.ASGITransport(app=app), base_url="http://localhost:8001") as c:
            r = await c.post("/mcp", json={"jsonrpc": "2.0", "id": 1, "method": "tools/list"},
                             headers={"Accept": "application/json, text/event-stream"})
            print(r.status_code, r.headers["www-authenticate"])
asyncio.run(main())
SDK version

2.3.0

Area

Server

Vorherrschende Sprache
Python
Sterne
24.7k
Forks
4.1k
Ø Merge
9 Std. 45 Min.
Gemergte PRs (30 T.)
16

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus modelcontextprotocol/python-sdk

Alle Issues in modelcontextprotocol/python-sdk

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.