MCP endpoints: answer insufficient_scope and invalid_token with the challenge their 401 already carries
Summary
Follow-up to !260668 (merged), agreed in review there.
The two MCP endpoints, /api/v4/mcp and /api/v4/orbit/mcp, answer a request with no token, or with a token GitLab does not know, with a 401 whose challenge API::Helpers::Mcp::AuthChallenge builds: realm="GitLab", the endpoint's own scope from Gitlab::Auth::MCP_RESOURCE_SCOPES (mcp or mcp_orbit), and resource_metadata. The other refusals of a token on these endpoints are built elsewhere and say something different:
- The
insufficient_scope403 comes from the shared API guard (lib/api/api_guard.rb:217-223). Onmasterit carries noWWW-Authenticateheader (theFIXMEthere). !260668 (merged) adds the guard's challenge, which has the guard's realm, noresource_metadata, and inscopethe scopes the guard's check accepts for the request rather than the endpoint's scope:mcp apifor aPOSTto/api/v4/mcp. - On
/api/v4/mcp, a token that hasapiand notmcpgets a plain 403.API::APIregistersapiwith no condition (lib/api/api.rb:58), so the guard's check accepts the token, and the endpoint's own check refuses it afterwards withforbidden!(lib/api/mcp/base.rb:68-69):{"message":"403 Forbidden"}and no challenge, onmasterand with !260668 (merged) alike. Soapi, which the 403 above names, is a scope this endpoint refuses, and the refusal gives a client nothing to step up from. - An expired or revoked token, or an impersonation token while impersonation is disabled, gets the guard's
invalid_token401 (lib/api/api_guard.rb:202-215), with the guard's realm and noresource_metadataorscope. #604654 (closed) asked forresource_metadatathere and was closed after !243210 (merged) and !243212 (merged). Those add the challenge inunauthorized!, which answers a request with no token or an unknown one; none of these tokens reaches it.
The MCP authorization specification asks for error="insufficient_scope", the scopes needed and resource_metadata on that 403, "for consistency with 401 responses" (2025-11-25, Runtime Insufficient Scope Errors), and its step-up flow has the client read that challenge and request the scopes it names.
Steps to reproduce
-
On an instance with access to the MCP server turned on, create personal access tokens with only
read_userand with onlyapi, and a third that you then revoke. -
Send an MCP
initializerequest to/api/v4/mcpwith each of them, and with none:curl --include --request POST --header "Authorization: Bearer <token>" \ --header "Content-Type: application/json" \ --data '{"jsonrpc":"2.0","method":"initialize","id":1,"params":{"protocolVersion":"2025-06-18"}}' \ "https://gitlab.example.com/api/v4/mcp" -
Send the same requests to
/api/v4/orbit/mcp, except theapione, which that endpoint accepts.
Example Project
Not needed: the responses do not depend on any project.
What is the current bug behavior?
On /api/v4/mcp, with !260668 (merged) applied:
| Request | Status | WWW-Authenticate |
|---|---|---|
| No token, or an unknown one | 401 |
Bearer realm="GitLab", scope="mcp", resource_metadata="<url>/.well-known/oauth-protected-resource/api/v4/mcp" |
| Expired or revoked token, or an impersonation token while impersonation is disabled | 401 |
Bearer realm="Protected by OAuth 2.0", error="invalid_token", error_description="Token is expired. You can either do re-authorization or token refresh." (or "Token was revoked. ...", "Token is an impersonation token but impersonation was disabled.") |
Token with only read_user |
403 |
Bearer realm="Protected by OAuth 2.0", error="insufficient_scope", error_description="The request requires higher privileges than provided by the access token.", scope="mcp api", and none on master |
Token with api and not mcp |
403 |
none, and the body is {"message":"403 Forbidden"} |
/api/v4/orbit/mcp answers the first three rows the same, with mcp_orbit and its own metadata URL in the first and scope="mcp_orbit mcp read_api ai_workflows api" in the third, and it accepts a token with api. Every scope in that list is one the Orbit endpoint accepts.
What is the expected correct behavior?
On both endpoints, the insufficient_scope 403 and the invalid_token 401 carry the parameters of the challenge the 401 already carries, with error and error_description added, and the body's scope matches the header's:
WWW-Authenticate: Bearer realm="GitLab", error="insufficient_scope", error_description="The request requires higher privileges than provided by the access token.", scope="mcp", resource_metadata="https://gitlab.example.com/.well-known/oauth-protected-resource/api/v4/mcp"On /api/v4/mcp the scope is mcp: it is what the 401 there already names, and the scope GitLab sets on an authorization request for that resource and for a dynamically registered client limited to mcp (Oauth::AuthorizationsController#pre_auth_params). A token that passes the guard only through api gets this 403 instead of the plain one. On /api/v4/orbit/mcp, the 401 names mcp_orbit, which the same method sets for that resource and for a dynamically registered client limited to mcp_orbit. Whether its 403 names only that scope or keeps the list of every scope the endpoint accepts is open, since all of them work there.
The insufficient_granular_scope 403 that fine-grained tokens get is out of scope here.
Relevant logs and/or screenshots
Responses from a throwaway request spec on !260668 (merged)'s branch
POST /api/v4/mcp, no token
401
WWW-Authenticate: Bearer realm="GitLab", scope="mcp", resource_metadata="http://localhost/.well-known/oauth-protected-resource/api/v4/mcp"
{"message":"401 Unauthorized"}
POST /api/v4/mcp, unknown personal access token
401
WWW-Authenticate: Bearer realm="GitLab", scope="mcp", resource_metadata="http://localhost/.well-known/oauth-protected-resource/api/v4/mcp"
{"message":"401 Unauthorized"}
POST /api/v4/mcp, expired OAuth token
401
WWW-Authenticate: Bearer realm="Protected by OAuth 2.0", error="invalid_token", error_description="Token is expired. You can either do re-authorization or token refresh."
{"error":"invalid_token","error_description":"Token is expired. You can either do re-authorization or token refresh."}
POST /api/v4/mcp, revoked personal access token
401
WWW-Authenticate: Bearer realm="Protected by OAuth 2.0", error="invalid_token", error_description="Token was revoked. You have to re-authorize from the user."
{"error":"invalid_token","error_description":"Token was revoked. You have to re-authorize from the user."}
POST /api/v4/mcp, impersonation token with mcp, impersonation disabled
401
WWW-Authenticate: Bearer realm="Protected by OAuth 2.0", error="invalid_token", error_description="Token is an impersonation token but impersonation was disabled."
{"error":"invalid_token","error_description":"Token is an impersonation token but impersonation was disabled."}
POST /api/v4/mcp, OAuth token with read_user
403
WWW-Authenticate: Bearer realm="Protected by OAuth 2.0", error="insufficient_scope", error_description="The request requires higher privileges than provided by the access token.", scope="mcp api"
{"error":"insufficient_scope","error_description":"The request requires higher privileges than provided by the access token.","scope":"mcp api"}
POST /api/v4/mcp, personal access token with api
403
(no WWW-Authenticate)
{"message":"403 Forbidden"}
POST /api/v4/orbit/mcp, no token
401
WWW-Authenticate: Bearer realm="GitLab", scope="mcp_orbit", resource_metadata="http://localhost/.well-known/oauth-protected-resource/api/v4/orbit/mcp"
{"message":"401 Unauthorized"}
POST /api/v4/orbit/mcp, expired OAuth token
401
WWW-Authenticate: Bearer realm="Protected by OAuth 2.0", error="invalid_token", error_description="Token is expired. You can either do re-authorization or token refresh."
{"error":"invalid_token","error_description":"Token is expired. You can either do re-authorization or token refresh."}
POST /api/v4/orbit/mcp, OAuth token with read_user
403
WWW-Authenticate: Bearer realm="Protected by OAuth 2.0", error="insufficient_scope", error_description="The request requires higher privileges than provided by the access token.", scope="mcp_orbit mcp read_api ai_workflows api"
{"error":"insufficient_scope","error_description":"The request requires higher privileges than provided by the access token.","scope":"mcp_orbit mcp read_api ai_workflows api"}An OAuth token and a personal access token with the same scopes, or in the same state, got the same answer in every case.
Output of checks
Results of GitLab environment info
The code is cited from master at c567a00380f. The responses above come from a throwaway request spec on !260668 (merged)'s branch at 197a5063 (master at cc09e01a plus that MR), where the guard's handling of expired, revoked and impersonation tokens, AuthChallenge and both endpoints' before blocks are the same as on master.
Possible fixes
- Build every challenge on these endpoints in
AuthChallenge, takingerroranderror_descriptionas well, and have the guard's error handler use it when the endpoint includesAuthChallenge. The handler runs in the endpoint's context, sorequestand the resource path are there. The body'sscopeshould come from the same value, so the two keep agreeing as they do after !260668 (merged). - On
/api/v4/mcp, raiseGitlab::Auth::InsufficientScopeErrorwith[Gitlab::Auth::MCP_SCOPE]instead of callingforbidden!atlib/api/mcp/base.rb:68-69, so the token with onlyapigets the same answer from the same handler.