1- import { AuthInfo } from "@modelcontextprotocol/sdk/server/auth/types" ;
2- import { InvalidTokenError , InsufficientScopeError , ServerError } from "@modelcontextprotocol/sdk/server/auth/errors" ;
1+ import { AuthInfo } from "@modelcontextprotocol/sdk/server/auth/types.js" ;
2+ import {
3+ InvalidTokenError ,
4+ InsufficientScopeError ,
5+ ServerError ,
6+ } from "@modelcontextprotocol/sdk/server/auth/errors.js" ;
37import { withAuthContext } from "./auth-context" ;
48
59declare global {
@@ -34,7 +38,7 @@ export function withMcpAuth(
3438 const bearerToken = type ?. toLowerCase ( ) === "bearer" ? token : undefined ;
3539
3640 const authInfo = await verifyToken ( req , bearerToken ) ;
37-
41+
3842 if ( required && ! authInfo ) {
3943 throw new InvalidTokenError ( "No authorization provided" ) ;
4044 }
@@ -45,7 +49,7 @@ export function withMcpAuth(
4549
4650 // Check if token has the required scopes (if any)
4751 if ( requiredScopes ?. length ) {
48- const hasAllScopes = requiredScopes . every ( scope =>
52+ const hasAllScopes = requiredScopes . every ( ( scope ) =>
4953 authInfo . scopes . includes ( scope )
5054 ) ;
5155
@@ -72,36 +76,34 @@ export function withMcpAuth(
7276 status : 401 ,
7377 headers : {
7478 "WWW-Authenticate" : `Bearer error="${ error . errorCode } ", error_description="${ error . message } ", resource_metadata="${ resourceMetadataUrl } "` ,
75- "Content-Type" : "application/json"
76- }
79+ "Content-Type" : "application/json" ,
80+ } ,
7781 } ) ;
7882 } else if ( error instanceof InsufficientScopeError ) {
7983 return new Response ( JSON . stringify ( error . toResponseObject ( ) ) , {
8084 status : 403 ,
8185 headers : {
8286 "WWW-Authenticate" : `Bearer error="${ error . errorCode } ", error_description="${ error . message } ", resource_metadata="${ resourceMetadataUrl } "` ,
83- "Content-Type" : "application/json"
84- }
87+ "Content-Type" : "application/json" ,
88+ } ,
8589 } ) ;
8690 } else if ( error instanceof ServerError ) {
8791 return new Response ( JSON . stringify ( error . toResponseObject ( ) ) , {
8892 status : 500 ,
8993 headers : {
90- "Content-Type" : "application/json"
91- }
94+ "Content-Type" : "application/json" ,
95+ } ,
9296 } ) ;
9397 } else {
9498 console . error ( "Unexpected error authenticating bearer token:" , error ) ;
9599 const serverError = new ServerError ( "Internal Server Error" ) ;
96100 return new Response ( JSON . stringify ( serverError . toResponseObject ( ) ) , {
97101 status : 500 ,
98102 headers : {
99- "Content-Type" : "application/json"
100- }
103+ "Content-Type" : "application/json" ,
104+ } ,
101105 } ) ;
102106 }
103107 }
104108 } ;
105109}
106-
107-
0 commit comments