@@ -314,12 +314,12 @@ func (s *Server) WrapHandlerFunc(next http.HandlerFunc) http.HandlerFunc {
314314}
315315
316316// WrapMCPEndpoint wraps an MCP endpoint handler with automatic 401 handling.
317- // Returns 401 with WWW-Authenticate headers if Bearer token is missing.
317+ // Returns 401 with WWW-Authenticate headers if Bearer token is missing or invalid .
318318//
319319// This method provides automatic OAuth discovery for MCP clients by:
320320// - Passing through OPTIONS requests (CORS pre-flight)
321- // - Passing through non-Bearer auth schemes (e.g., Basic auth )
322- // - Returning 401 with proper headers if Bearer token is missing
321+ // - Rejecting non-Bearer auth schemes (OAuth-only endpoint )
322+ // - Returning 401 with proper headers if Bearer token is missing/malformed
323323// - Extracting token to context and passing to wrapped handler
324324//
325325// Usage with mark3labs SDK:
@@ -347,17 +347,15 @@ func (s *Server) WrapMCPEndpoint(handler http.Handler) http.HandlerFunc {
347347 }
348348
349349 // Check if it's a Bearer token (case-insensitive per OAuth 2.0 spec)
350- if strings .HasPrefix (authLower , "bearer" ) {
351- // Malformed Bearer token (no space after "Bearer")
352- if ! strings .HasPrefix (authLower , "bearer " ) {
353- s .Return401InvalidToken (w )
354- return
355- }
356- // Valid Bearer format, extract to context
357- // (validation happens in downstream middleware)
358- } else {
359- // Pass through non-Bearer schemes (e.g., Basic auth)
360- handler .ServeHTTP (w , r )
350+ if ! strings .HasPrefix (authLower , "bearer" ) {
351+ // Reject non-Bearer schemes (OAuth endpoints require Bearer tokens only)
352+ s .Return401 (w )
353+ return
354+ }
355+
356+ // Malformed Bearer token (no space after "Bearer")
357+ if ! strings .HasPrefix (authLower , "bearer " ) {
358+ s .Return401InvalidToken (w )
361359 return
362360 }
363361
0 commit comments