Conversation
Add support for authenticating with Grafana instances using Cloud access policy tokens as an alternative to service account tokens or API keys. The token is read from the GRAFANA_CLOUD_ACCESS_POLICY_TOKEN environment variable and sent as a Bearer token in the X-Access-Token header. For SSE/streamable HTTP transports, the token can also be provided via the X-Cloud-Access-Policy-Token request header, which takes precedence over the environment variable. The implementation covers all transport paths: BuildTransport for direct clients, NewGrafanaClient for reflection-based client setup, and NewProxiedClient for datasource proxy connections. Closes #561 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
…ctive Both CloudAccessPolicyToken and on-behalf-of AccessToken use the X-Access-Token header. When OBO auth is active, the cloud token's ExtraHeadersRoundTripper would clone the request and overwrite the header set by authRoundTripper, silently breaking OBO auth. Skip adding the cloud access policy token header in BuildTransport and NewGrafanaClient when AccessToken is set. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This comment has been minimized.
This comment has been minimized.
|
Bugbot Autofix prepared fixes for 1 of the 1 bugs found in the latest run.
Or push these changes by commenting: Preview (b7baec6864)diff --git a/proxied_client.go b/proxied_client.go
--- a/proxied_client.go
+++ b/proxied_client.go
@@ -36,8 +36,10 @@
headers["Authorization"] = "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
}
- // Add cloud access policy token header if configured
- if config.CloudAccessPolicyToken != "" {
+ // Add cloud access policy token header only when on-behalf-of auth is not
+ // active. OBO auth also uses X-Access-Token (set by authRoundTripper in
+ // tool-specific clients) and should take precedence.
+ if config.CloudAccessPolicyToken != "" && config.AccessToken == "" {
headers["X-Access-Token"] = "Bearer " + config.CloudAccessPolicyToken
} |
Add the missing `config.AccessToken == ""` guard to the CloudAccessPolicyToken check in proxied_client.go, matching the pattern already used in BuildTransport and NewGrafanaClient. Without this guard, the cloud access policy token would overwrite the OBO token for proxied datasource connections. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
MCP Token Analysis✅ Passed
|
Instead of sending the Cloud Access Policy token directly as X-Access-Token, use authlib's TokenExchangeClient to exchange it for a signed access token via the Auth API's /v1/sign-access-token endpoint. The TokenExchangeClient provides built-in caching (TTL from JWT expiry - 15s), singleflight dedup, and retry logic. New environment variables: GRAFANA_TOKEN_EXCHANGE_URL and GRAFANA_TOKEN_EXCHANGE_NAMESPACE. Corresponding HTTP headers: X-Token-Exchange-URL and X-Token-Exchange-Namespace. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Adds support for authenticating with Grafana instances using Cloud access policy tokens as an alternative to service account tokens or API keys.
mcpgrafana.go: AddedGRAFANA_CLOUD_ACCESS_POLICY_TOKENenv var support,CloudAccessPolicyTokenconfig field, extraction from both env andX-Cloud-Access-Policy-Tokenrequest header (header takes precedence), andX-Access-Tokenheader injection across all transport paths (BuildTransport,NewGrafanaClient)proxied_client.go: AddedX-Access-Tokenheader for proxied MCP datasource connectionsmcpgrafana_test.go: Tests covering env extraction, header extraction with precedence, and transport header injection (with and without extra headers)README.md: Documentation for the new authentication method including configuration exampleCloses #561
Test plan
TestCloudAccessPolicyTokenFromEnvpasses (token set and unset scenarios)TestCloudAccessPolicyTokenFromHeaderspasses (header-only, env-only, and header-takes-precedence scenarios)TestCloudAccessPolicyTokenTransportpasses (header injection viaBuildTransport, no header when unset, coexistence with extra headers)GRAFANA_CLOUD_ACCESS_POLICY_TOKENagainst a Grafana Cloud instance and confirm successful authentication🤖 Generated with Claude Code
Note
Medium Risk
Touches request authentication/header injection across multiple client transports; mistakes could cause auth failures or unintended credential precedence, though the change is gated and covered by unit tests.
Overview
Adds Grafana Cloud access policy token authentication as an alternative to service account/API key auth.
Introduces
GRAFANA_CLOUD_ACCESS_POLICY_TOKEN(andX-Cloud-Access-Policy-Tokenfor SSE/HTTP) to populate a newGrafanaConfig.CloudAccessPolicyToken, and injects the correspondingX-Access-Token: Bearer ...header across client/transport paths (including proxied MCP datasource connections), while explicitly not overriding on-behalf-of auth whenAccessTokenis set. Includes unit tests for env/header precedence and transport header injection, plus README documentation and examples.Written by Cursor Bugbot for commit cc1c96a. This will update automatically on new commits. Configure here.