fix: set X-Metabase-Session header on current request in interceptor for username/password auth#32
Open
KrasnovidKE wants to merge 1 commit intoCognitionAI:mainfrom
Conversation
…for username/password auth When using username/password auth, the session token was written to defaults.headers.common inside the request interceptor. However, axios merges defaults into the per-request config.headers before interceptors run, so the first request was always dispatched without the X-Metabase-Session header, resulting in HTTP 401. Fix: explicitly assign the token to config.headers after ensureAuthenticated() resolves, ensuring it is present on the very first request. Co-Authored-By: Claude Sonnet 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.
Problem
When using username/password authentication (i.e.
METABASE_USERNAME+METABASE_PASSWORDwithoutMETABASE_API_KEY), every API call fails with HTTP 401 on the first invocation of any MCP tool.Root cause
MetabaseClientuses an axios request interceptor to lazily authenticate before each request:After
ensureAuthenticated()resolves, the session token is written tothis.axiosInstance.defaults.headers.common["X-Metabase-Session"].However, axios merges
defaults.headers.commoninto the per-requestconfig.headersobject before request interceptors run — not after. This means that by the time the interceptor sets the default header, the current request's header map is already frozen and theX-Metabase-Sessionheader is absent. The request is dispatched unauthenticated → 401.Subsequent calls succeed because
defaults.headers.commonis already populated from the previous run, so the merge at the start of the next request picks it up correctly.The
METABASE_API_KEYpath is unaffected because the header is set in the constructor (before any request is created):Fix
After
ensureAuthenticated()resolves, explicitly assign the session token directly onto the current request'sconfig.headersobject:This guarantees the header is present on the very first request regardless of when axios performs header merging internally.
Testing
Reproduced and verified against a self-hosted Metabase instance:
Affected file
src/client/metabase-client.ts— request interceptor inside theMetabaseClientconstructor.