Skip to content
You are reading the docs for Wanaku next, the unreleased development version. Read the docs for the latest release.

Authenticate MCP clients with oauth2-proxy and Keycloak ​

Wanaku uses Keycloak to issue tokens. oauth2-proxy validates browser sessions and bearer tokens before it forwards requests to Wanaku.

text
Browser/CLI ──► oauth2-proxy-mgmt (:4180) ──► Wanaku Management API (:8080)
            └─► oauth2-proxy-mcp (:4181) ──► Wanaku MCP (:8081)

Both proxies share a cookie secret. A browser login through either proxy then works on both ports. CLI requests use a Keycloak access token.

Local Docker Compose setup ​

Follow the authenticated local deployment guide. It starts Keycloak, Wanaku, and both proxies. It also creates a test user and demonstrates the wanaku and wanaku-keycloak-admin commands.

The bundled realm has three separate clients:

  • admin-cli lets wanaku-keycloak-admin manage Keycloak. Its token does not authorize Wanaku API requests.
  • mcp-client lets wanaku log in as a user. Its default scope adds the audience accepted by the proxies.
  • wanaku-mcp-router lets oauth2-proxy authenticate browser sessions. It is a confidential client with a generated secret.

The CLI logs in with the public mcp-client, which does not need a client secret. The wanaku-mcp-router secret belongs to the oauth2-proxy instances.

The local realm enables the password grant for mcp-client. Use this grant only for local development. Production deployments should use an interactive OAuth flow and production Keycloak clients.

Log in with the CLI ​

After setup, log in with the local CLI client and use one token with both APIs:

bash
wanaku auth login --auth-server http://localhost:8543 --realm wanaku \
  --client-id mcp-client --username alice --password
TOKEN=$(wanaku auth token --get --plain --unmask)
wanaku mcp tool list --verbose --uri http://localhost:4181/default/mcp --token "$TOKEN"
wanaku tools list --verbose --host http://localhost:4180 --token "$TOKEN"

Set the namespace in the MCP endpoint path. Replace default with the namespace that contains the tools you want to access. The login command prompts for the user's password.

Issuer addresses ​

Set WANAKU_AUTH_ISSUER to the issuer URL that clients can reach. Set WANAKU_AUTH_UPSTREAM_ISSUER when the server must use a different URL to reach Keycloak. The upstream variable defaults to WANAKU_AUTH_ISSUER.

VariableDefaultPurpose
WANAKU_AUTH_ISSUER(unset = disabled)Public OIDC issuer URL (for example, http://localhost:8543/realms/wanaku)
WANAKU_AUTH_UPSTREAM_ISSUERWANAKU_AUTH_ISSUERInternal OIDC issuer URL for token requests (for example, http://keycloak:8080/realms/wanaku)

The public issuer URL must be accessible to clients and browsers. Wanaku uses this URL in discovery metadata, authorization redirects, registration redirects, and the JWKS URL. Set WANAKU_AUTH_UPSTREAM_ISSUER when Wanaku must use a different address to reach the same issuer. Only the /token proxy uses this internal address. An unset or empty upstream issuer uses the public issuer URL.

For the Compose setup, clients use http://localhost:8543/realms/wanaku; the server container uses http://keycloak:8080/realms/wanaku for token requests. This keeps public discovery metadata and browser redirects usable from the host.

Wanaku always serves OAuth protected-resource metadata for MCP endpoints at /.well-known/oauth-protected-resource/{namespace}/mcp. When WANAKU_AUTH_ISSUER is set, the authorization_servers list contains the WANAKU_AUTH_ISSUER value. When it is unset, the route returns HTTP 200 with an empty authorization_servers list. The OpenID discovery, /authorize, /token, and /register routes are active only when WANAKU_AUTH_ISSUER is set.

OAuth discovery flow ​

An MCP client discovers the authorization server in these steps:

  1. The client sends a request to the MCP endpoint without a token. The proxy returns HTTP 401.
  2. The client gets the protected-resource metadata from /.well-known/oauth-protected-resource/{namespace}/mcp.
  3. The client reads the issuer URL from the authorization_servers list.
  4. The client gets the authorization server metadata from the issuer. Keycloak serves it at {issuer}/.well-known/openid-configuration.
  5. The client uses the authorization, token, and registration endpoints of the issuer.

The issuer value in the Keycloak metadata is the same URL as the authorization_servers entry. Clients that validate this match (RFC 8414, section 3.3) accept the metadata.

A browser-based MCP client sends the token request to Keycloak directly. Keycloak adds CORS headers only for the origins in the Web Origins setting of the client. Add the origin of the browser client to the Keycloak client. A client that Keycloak creates through dynamic registration has no web origins, so set them on that client after registration. Native and command-line clients do not need CORS.

Wanaku still serves /.well-known/openid-configuration. The wanaku auth login --auth-server <mcp-origin> --realm "" command reads the issuer from this document.

Discovery base URL ​

The resource value in the protected-resource metadata must be the URL that the client uses to connect (RFC 9728, section 3.3). The Wanaku OpenID discovery document also contains /authorize, /token, and /register URLs. Wanaku selects the base URL of these values in this order:

  1. WANAKU_MCP_PUBLIC_URL, when it is set.
  2. The X-Forwarded-Proto and X-Forwarded-Host headers, when the request contains them.
  3. http:// and the Host header.

A TLS-terminating proxy, such as an OpenShift Route, an Ingress, or a load balancer, usually sets the X-Forwarded-* headers. Wanaku uses the first value of each header. Wanaku accepts only http and https in X-Forwarded-Proto. Wanaku ignores an X-Forwarded-Host value that contains /, \, @, ?, #, or a space. The responses contain Vary: x-forwarded-proto, x-forwarded-host, so that caches keep a separate copy for each header value.

Wanaku does not check the source of the X-Forwarded-* headers. A client can set them, but the values change only the response to that client. Set WANAKU_MCP_PUBLIC_URL when the proxy does not set these headers, or when you do not want Wanaku to use them.

Protected-resource challenge ​

When oauth2-proxy rejects an MCP request without a token, it returns HTTP 401 without a WWW-Authenticate header. MCP clients that follow the MCP authorization specification then request the well-known address for the endpoint. For /default/mcp, this address is /.well-known/oauth-protected-resource/default/mcp. Wanaku serves the metadata at this address, and the proxy permits it without a token.

Some MCP clients find the metadata only from a resource_metadata value in the WWW-Authenticate header. For these clients, add the header in a reverse proxy in front of oauth2-proxy. The NGINX example below adds the header only to 401 responses. Replace https://wanaku.example.com with the public address of the MCP proxy.

nginx
map $status $wanaku_mcp_challenge {
    401     'Bearer resource_metadata="https://wanaku.example.com/.well-known/oauth-protected-resource$uri"';
    default "";
}

server {
    listen 443 ssl;
    server_name wanaku.example.com;

    location / {
        proxy_pass http://oauth2-proxy-mcp:4181;
        add_header WWW-Authenticate $wanaku_mcp_challenge always;
    }
}

NGINX does not send a header with an empty value. Thus, other responses do not get the header.

Proxy behavior ​

The MCP proxy accepts bearer tokens with the configured Wanaku MCP audience. MCP endpoints use /{namespace}/mcp, such as /default/mcp. Pass the full endpoint to the CLI. The CLI does not infer or select a namespace from a bare server origin.

The management proxy accepts the same bearer tokens for /api/ requests. It protects the admin UI with a browser session. Set the proxy's allowed roles only when the realm maps the selected administrator role into access tokens.

Every namespace endpoint requires a token. Wanaku has no unauthenticated public namespace. A namespace named public requires a token, like all other namespaces. The proxy permits requests without a token only for OAuth discovery (/.well-known/), /authorize, /token, /register, and CORS preflight (OPTIONS) requests.

Logging out ​

wanaku auth logout clears the CLI's saved credentials. It does not end a browser session or revoke a token that was already issued.

The admin UI Logout button opens /oauth2/sign_out?rd=/admin/. An oauth2-proxy in front of the management API must serve this path. Set OAUTH2_PROXY_BACKEND_LOGOUT_URL to the identity provider logout endpoint to also end the provider session. Without it, the next login can complete without a password prompt.