This is an automated email from the ASF dual-hosted git repository.
bharos pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino-mcp-gateway.git
The following commit(s) were added to refs/heads/main by this push:
new d5e4cbc [MINOR] fix: Let MCP clients that ask for a refresh token
sign in (#13)
d5e4cbc is described below
commit d5e4cbc091db8aae4e6f9a320742dc5cc9f8e365
Author: Bharath Krishna <[email protected]>
AuthorDate: Wed Nov 18 21:23:29 2026 -0800
[MINOR] fix: Let MCP clients that ask for a refresh token sign in (#13)
MCP clients such as Claude Code register themselves with the scopes the
gateway advertises, then also ask for offline_access to get a refresh
token. Keycloak refuses a self-registered client any scope it didn't
register with, so sign-in failed with "invalid_scope". The docs and the
dev setup now list offline_access in GATEWAY_API_SCOPES, and the
gateway's own sign-in asks for it only once.
The integration test for a self-registering client now asks for a
refresh token the way Claude Code does, and accepts Keycloak's consent
page.
---
README.md | 5 +++--
docs/gravitino-mcp-gateway.md | 2 +-
mcp_gateway/config.py | 2 +-
tests/integration/test_keycloak.py | 32 +++++++++++++++++++++++++++-----
tests/test_auth_routes.py | 16 +++++++++++++++-
5 files changed, 47 insertions(+), 10 deletions(-)
diff --git a/README.md b/README.md
index ae30029..33e2474 100644
--- a/README.md
+++ b/README.md
@@ -140,7 +140,7 @@ export GATEWAY_OAUTH_CLIENT_ID=mcp-gateway
export GATEWAY_OAUTH_CLIENT_SECRET=mcp-gateway-secret
export GATEWAY_OAUTH_ACCEPT_ANY_CLIENT=true
export JWT_AUDIENCE=gravitino
-export GATEWAY_API_SCOPES=gravitino
+export GATEWAY_API_SCOPES="gravitino offline_access"
export GRAVITINO_MCP_URL=http://localhost:8100/mcp
export GRAVITINO_URI=http://localhost:8090 # alice, Gravitino's
service admin, is an admin
export USER_ID_CLAIM=preferred_username # the first of Gravitino's
principalFields
@@ -150,8 +150,9 @@ uvicorn mcp_gateway.main:app --port 8000
Point an MCP client at http://localhost:8000/mcp/gravitino: it registers itself
with Keycloak, signs you in, and the gateway forwards your token to Gravitino.
+`offline_access` lets clients such as Claude Code get a refresh token.
To have the gateway exchange tokens instead (RFC 8693), set
-`JWT_AUDIENCE=mcp-gateway`, `GATEWAY_API_SCOPES=mcp-gateway` and
+`JWT_AUDIENCE=mcp-gateway`, `GATEWAY_API_SCOPES="mcp-gateway offline_access"`
and
`GRAVITINO_MCP_AUDIENCE=gravitino`.
[setup-realm.sh](dev/keycloak/setup-realm.sh)
shows the Keycloak settings each needs.
diff --git a/docs/gravitino-mcp-gateway.md b/docs/gravitino-mcp-gateway.md
index ae0f47d..089647e 100644
--- a/docs/gravitino-mcp-gateway.md
+++ b/docs/gravitino-mcp-gateway.md
@@ -98,7 +98,7 @@ The main settings are:
| `GATEWAY_OAUTH_CLIENT_ID`, `GATEWAY_OAUTH_CLIENT_SECRET` | The gateway's
OAuth client, for browser sign-in and token exchange. |
| `TOKEN_EXCHANGE_STYLE` | `rfc8693`, or `entra_obo` for Entra's on-behalf-of
flow, which is the default with `OIDC_PROVIDER=azure`. |
| `JWT_AUDIENCE` | The audience that people's tokens must carry. Required. |
-| `GATEWAY_API_SCOPES` | Scopes that put `JWT_AUDIENCE` in a token,
space-separated. MCP clients read them from the gateway's metadata, and the
gateway's own sign-in asks for them too. With `OIDC_PROVIDER=azure`,
`user_access` by default. |
+| `GATEWAY_API_SCOPES` | Scopes that put `JWT_AUDIENCE` in a token,
space-separated. MCP clients read them from the gateway's metadata, and the
gateway's own sign-in asks for them too. With Keycloak, also list
`offline_access`, so that clients that ask for a refresh token, such as Claude
Code, can sign in: Keycloak refuses a client that registered itself any scope
it didn't register with. With `OIDC_PROVIDER=azure`, `user_access` by default. |
| `GATEWAY_LOGIN_SCOPES` | Replaces the scopes of the gateway's own sign-in,
space-separated: by default `openid profile email offline_access` and
`GATEWAY_API_SCOPES`, or `<client ID>/.default` with `OIDC_PROVIDER=azure`. |
| `GATEWAY_OAUTH_ADDITIONAL_CLIENT_IDS` | Other OAuth clients whose tokens the
gateway accepts. Comma-separated. |
| `GATEWAY_OAUTH_ACCEPT_ANY_CLIENT` | Accept people's tokens for
`JWT_AUDIENCE` from any client of the identity provider, such as MCP clients
that register themselves (RFC 7591). They must still come from the configured
issuer and carry a verified email. |
diff --git a/mcp_gateway/config.py b/mcp_gateway/config.py
index 965de76..f0f1730 100644
--- a/mcp_gateway/config.py
+++ b/mcp_gateway/config.py
@@ -458,7 +458,7 @@ class Settings(BaseSettings):
else:
# The scopes MCP clients ask for, such as one that puts the gateway
# in the token's audience, so the stored token is like theirs.
- scopes += self.gateway_api_scopes
+ scopes += [s for s in self.gateway_api_scopes if s not in scopes]
return " ".join(scopes)
@property
diff --git a/tests/integration/test_keycloak.py
b/tests/integration/test_keycloak.py
index 3614735..e4b4367 100644
--- a/tests/integration/test_keycloak.py
+++ b/tests/integration/test_keycloak.py
@@ -29,7 +29,7 @@ import json
import os
import re
import secrets
-from urllib.parse import parse_qs, urlencode, urlsplit
+from urllib.parse import parse_qs, urlencode, urljoin, urlsplit
import httpx
import pytest
@@ -82,11 +82,30 @@ def _sign_in(authorize_url: str, user: str) -> str:
for cookie in browser.cookies.jar:
cookie.secure = False
action = re.search(r'id="kc-form-login"[^>]*action="([^"]+)"', page.text)
+ assert action, f"No login page: {page.status_code}
{page.headers.get('location')}"
resp = browser.post(
html.unescape(action.group(1)),
data={"username": user, "password": user, "credentialId": ""},
)
assert resp.status_code == 302, resp.text
+ location = resp.headers["location"]
+ if "execution=OAUTH_GRANT" in location:
+ # Keycloak asks people to consent to clients that registered
themselves.
+ location = _consent(browser, location)
+ return location
+
+
+def _consent(browser: httpx.Client, url: str) -> str:
+ """Accept Keycloak's consent page; return where Keycloak redirects next."""
+ page = browser.get(url)
+ action = re.search(r'action="([^"]*login-actions/consent[^"]*)"',
page.text)
+ code = re.search(r'name="code" value="([^"]+)"', page.text)
+ assert action and code, f"No consent page: {page.status_code}"
+ resp = browser.post(
+ urljoin(url, html.unescape(action.group(1))),
+ data={"code": code.group(1), "accept": "Yes"},
+ )
+ assert resp.status_code == 302, resp.text
return resp.headers["location"]
@@ -169,7 +188,7 @@ def fixture_forwarding_gateway(client, db_session,
monkeypatch):
db_session,
monkeypatch,
JWT_AUDIENCE="gravitino",
- GATEWAY_API_SCOPES="gravitino",
+ GATEWAY_API_SCOPES="gravitino offline_access",
)
@@ -181,7 +200,7 @@ def fixture_exchanging_gateway(client, db_session,
monkeypatch):
db_session,
monkeypatch,
JWT_AUDIENCE="mcp-gateway",
- GATEWAY_API_SCOPES="mcp-gateway",
+ GATEWAY_API_SCOPES="mcp-gateway offline_access",
GRAVITINO_MCP_AUDIENCE="gravitino",
)
@@ -218,6 +237,8 @@ def
test_a_token_for_the_gateway_is_exchanged_for_gravitino(exchanging_gateway):
def
test_an_mcp_client_that_registers_itself_reaches_gravitino(forwarding_gateway):
+ # Like Claude Code: register with the advertised scopes, then ask for a
+ # refresh token too. Keycloak refuses a scope the client didn't register.
resource =
forwarding_gateway.get("/.well-known/oauth-protected-resource").json()
[issuer] = resource["authorization_servers"]
provider = httpx.get(f"{issuer}/.well-known/openid-configuration").json()
@@ -226,7 +247,7 @@ def
test_an_mcp_client_that_registers_itself_reaches_gravitino(forwarding_gatewa
json={
"client_name": "Integration test",
"redirect_uris": [CLIENT_REDIRECT],
- "grant_types": ["authorization_code"],
+ "grant_types": ["authorization_code", "refresh_token"],
"response_types": ["code"],
"token_endpoint_auth_method": "none",
"scope": " ".join(resource["scopes_supported"]),
@@ -242,7 +263,7 @@ def
test_an_mcp_client_that_registers_itself_reaches_gravitino(forwarding_gatewa
"client_id": registered["client_id"],
"response_type": "code",
"redirect_uri": CLIENT_REDIRECT,
- "scope": " ".join(resource["scopes_supported"]),
+ "scope": " ".join([*resource["scopes_supported"], "offline_access"]),
"code_challenge": challenge,
"code_challenge_method": "S256",
}
@@ -260,6 +281,7 @@ def
test_an_mcp_client_that_registers_itself_reaches_gravitino(forwarding_gatewa
},
).json()
+ assert "refresh_token" in tokens, tokens
result = _call(
forwarding_gateway, tokens["access_token"], "tools/call", LIST_CATALOGS
)["result"]
diff --git a/tests/test_auth_routes.py b/tests/test_auth_routes.py
index ba1513c..93757e3 100644
--- a/tests/test_auth_routes.py
+++ b/tests/test_auth_routes.py
@@ -288,8 +288,22 @@ def
test_signing_in_sends_the_browser_to_the_provider_with_a_state_tied_to_it(
{"OIDC_PROVIDER": "keycloak", "GATEWAY_API_SCOPES": "mcp-gateway"},
"openid profile email offline_access mcp-gateway",
),
+ # Listed for MCP clients that ask for a refresh token; asked for once.
+ (
+ {
+ "OIDC_PROVIDER": "keycloak",
+ "GATEWAY_API_SCOPES": "gravitino offline_access",
+ },
+ "openid profile email offline_access gravitino",
+ ),
+ ],
+ ids=[
+ "default",
+ "entra",
+ "configured",
+ "mcp clients' scopes",
+ "offline_access listed",
],
- ids=["default", "entra", "configured", "mcp clients' scopes"],
)
def test_signing_in_asks_for_the_scopes_the_gateway_needs(
client, oauth_client, monkeypatch, configured, scope