Skip to content

MCP server: 401 invalid_token “Authentication required”

MCPtested on Python-SDK mcp 2.3.0, MCP 2026-07-28, Keycloak 26.8

An MCP server with OAuth 2.1 checks every request for a valid access token before it even reads the message. Without a token it answers 401 with a WWW-Authenticate header that points to the Protected Resource Metadata.

Symptom

curl -si http://worker1:8004/mcp -X POST -H "Content-Type: application/json" -H "Accept: application/json, text/event-stream" -H "MCP-Protocol-Version: 2026-07-28" -H "Mcp-Method: tools/list" -d '{}' | grep -i -E "^HTTP|^www-authenticate"
HTTP/1.1 401 Unauthorized
www-authenticate: Bearer error="invalid_token", error_description="Authentication required", resource_metadata="http://worker1:8004/.well-known/oauth-protected-resource/mcp"

The content of the request does not matter – the token check comes before everything else.

Diagnosis

Follow resource_metadata. The metadata names the authorization server and the scopes a token must have.

curl -s http://worker1:8004/.well-known/oauth-protected-resource/mcp | jq .
{
  "resource": "http://worker1:8004/mcp",
  "authorization_servers": [
    "http://worker2:8080/realms/mcpa"
  ],
  "scopes_supported": [
    "mcp:tools"
  ],
  "bearer_methods_supported": [
    "header"
  ]
}

Causes

no tokenAuthorization header missing

The SDK writes error="invalid_token" even when no token was sent at all. MCP over HTTP has no session – the token belongs on every single request.

expiredToken expired

Access tokens are short-lived. After expiry the server rejects requests until the client fetches a new token (refresh).

wrong audienceToken for a different resource

The server checks issuer, expiry and audience. A token issued for another service is not valid here.

Fix

Get an access token from the authorization server (in the course by hand with PKCE) and send it in the Authorization header.
curl -s http://worker1:8004/mcp "${KOPF[@]}" -H "Mcp-Method: tools/call" -H "Mcp-Name: wer_bin_ich" -H "Authorization: Bearer $TOKEN" --data @wer.json | jq -r '.result.content[0].text'
sub=fa146718-661d-420d-b8ee-7e05e74d48f3 client=leitstand-host scopes=mcp:tools email profile

Don't: Put the token in the URL. bearer_methods_supported says header – URLs end up in log files and browser history.

▶
Break it yourself and find it – free
MCPA hands-on, Day 12: authorization I – Keycloak as authorization server, Protected Resource Metadata, PKCE by hand and a protected MCP server.
Go to day 12 →

FAQ

Is error="invalid_token" without a token correct?

Strictly by RFC 6750, there should be no error without a token. The SDK sets it anyway; clients do not mind.

Why is the metadata not protected?

Otherwise no client could find out which authorization server issues its token.

Related error patterns

More