Skip to content

MCP server: 401 "Audience doesn't match" although resource= was sent

MCPtested on Ubuntu 24.04, k3s v1.37.2+k3s1, Keycloak 26.8.2

The MCP specification requires clients to send resource. Anna does – but logs in at the AI assistant's client. The MCP server rejects her token with 401: its address is not in aud.

Symptom

curl -s -i http://172.16.0.126:8091/mcp -H @$HOME/kurs/13/kopf-falsch.txt -H "Content-Type: application/json" -H "Accept: application/json, text/event-stream" -H "MCP-Protocol-Version: 2026-07-28" -d @$HOME/kurs/13/liste.json
HTTP/1.0 401 Unauthorized
WWW-Authenticate: Bearer resource_metadata="http://172.16.0.126:8091/.well-known/oauth-protected-resource/mcp", error="invalid_token", error_description="Audience doesn't match"

{"fehler": "Audience doesn't match"}

The token comes from a device login at assistent-cli – with resource=http://172.16.0.126:8091/mcp.

Diagnosis

Look into the token – what is in aud?

jq -r .access_token ~/kurs/13/anmeldung-anna-assistent.json > ~/kurs/13/anna-assistent.jwt
step crypto jwt inspect --insecure < ~/kurs/13/anna-assistent.jwt > ~/kurs/13/anna-assistent-inhalt.json
jq '.payload | {aud, azp, scope}' ~/kurs/13/anna-assistent-inhalt.json
{
  "aud": [
    "assistent",
    "account"
  ],
  "azp": "assistent-cli",
  "scope": "openid profile email"
}

Causes

resourceKeycloak 26.8 does not evaluate resource

The resource parameter had no effect. Keycloak 26.8 ignores it for this login.

MapperThe audience comes from the client

aud is determined solely by the audience mapper of the client you log in at – for assistent-cli that is fuer-assistent. For the MCP server, the mapper fuer-mcp on mcp-cli adds its address.

/opt/keycloak/bin/kcadm.sh create clients/0d93bf1b-bfee-4b2a-8643-016fc8dfe0f5/protocol-mappers/models -r gebaeude -s name=fuer-mcp -s protocol=openid-connect -s protocolMapper=oidc-audience-mapper -s 'config."included.client.audience"=http://172.16.0.126:8091/mcp' -s 'config."access.token.claim"=true'

aud checkThe MCP server does everything right

It only accepts tokens with its own address in aud. Otherwise any service Anna gave a token to could use it to call tools as well.

schein, schluessel.key, algorithms=["RS256"], audience=ICH, issuer=AUSSTELLER

Fix

Anna logs in at mcp-cli. Keeping resource in the request does no harm and takes effect as soon as Keycloak supports it.
curl -s http://172.16.0.126:8080/realms/gebaeude/protocol/openid-connect/token -d client_id=mcp-cli -d grant_type=urn:ietf:params:oauth:grant-type:device_code --data-urlencode device_code@$HOME/kurs/13/geraet-anna-code.txt --data-urlencode resource=http://172.16.0.126:8091/mcp -o ~/kurs/13/anmeldung-anna.json -w "%{http_code}\n"
jq -r .access_token ~/kurs/13/anmeldung-anna.json > ~/kurs/13/anna.jwt
step crypto jwt inspect --insecure < ~/kurs/13/anna.jwt
    "aud": [
      "http://172.16.0.126:8091/mcp",
      "account"
    ],
    "azp": "mcp-cli",

Don't: Loosen the aud check in the MCP server or pass tokens for other audiences through. The MCP specification forbids that for good reason.

▶
Break it yourself and find it – free
Workload and AI agent identity hands-on, day 13: securing MCP servers according to the specification – protected resource metadata, audience and token exchange.
Go to day 13 →

FAQ

Does Keycloak support RFC 8707 (resource indicators)?

In the course with Keycloak 26.8, resource had no effect on aud in the device login. The audience came from the client's mapper alone.

Should the MCP client send resource anyway?

Yes. The MCP specification requires it, it does no harm and takes effect as soon as the authorization server supports it.

Related error patterns

More