Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

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

Đang mở Phù hợp với người mới
#3,124 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
82/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
typescript
Lĩnh vực
api, authentication

Hướng nghiên cứu

Trong packages/server, tìm requireBearerAuth và bearerAuthChallengeResponse dùng chung được adapter Express sử dụng; trước tiên, đọc cách chúng tạo challenge và các bài kiểm thử gần đó. Thêm hoặc cập nhật bài kiểm thử cho trường hợp thiếu thông tin xác thực có resource_metadata hoặc scope, thiếu thông tin xác thực không có tham số nào khác và token không hợp lệ. Công việc hoàn tất khi trường hợp đầu tiên bỏ qua invalid_token, trường hợp thứ hai giữ nguyên challenge hiện tại và phản hồi cho token không hợp lệ không thay đổi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
TypeScript
Star
13.7k
Fork
2.3k
Merge trung bình
1 ngày 5 giờ
Pull request đã merge (30 ngày)
51

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của modelcontextprotocol/typescript-sdk

Tất cả issue của modelcontextprotocol/typescript-sdk

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.