Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spicy-dragons-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vercel/mcp-adapter": patch
---

Update auth logic to not throw error is missing bearerToken
26 changes: 19 additions & 7 deletions src/next/auth-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { withAuthContext } from "./auth-context";
export function withMcpAuth(
handler: (req: Request) => Response | Promise<Response>,
verifyToken: (
req: Request
req: Request,
bearerToken?: string
) => AuthInfo | undefined | Promise<AuthInfo | undefined>,
{
required = false,
Expand All @@ -17,13 +18,21 @@ export function withMcpAuth(
return async (req: Request) => {
const origin = new URL(req.url).origin;

const authInfo = await verifyToken(req);
const authHeader = req.headers.get("Authorization");
const [type, token] = authHeader?.split(" ") || [];

// Only support bearer token as per the MCP spec
// https://modelcontextprotocol.io/specification/2025-03-26/basic/authorization#2-6-1-token-requirements
const bearerToken = type?.toLowerCase() === "bearer" ? token : undefined;

const authInfo = await verifyToken(req, bearerToken);

if (required && !authInfo) {
return Response.json(
{
return new Response(
JSON.stringify({
error: "unauthorized_client",
error_description: "No authorization provided",
},
}),
{
status: 401,
headers: {
Expand All @@ -38,8 +47,11 @@ export function withMcpAuth(
}

if (authInfo.expiresAt && authInfo.expiresAt < Date.now() / 1000) {
return Response.json(
{ error: "invalid_token", error_description: "Authorization expired" },
return new Response(
JSON.stringify({
error: "invalid_token",
error_description: "Authorization expired",
}),
{
status: 401,
headers: {
Expand Down