-
Notifications
You must be signed in to change notification settings - Fork 16
Add support for dynamic Bearer token via custom JS module #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add support for dynamic Bearer token via custom JS module #17
Conversation
Motivation: In real life bearer token is temporary and generated via API call. Setting it each time in MCP configuration is inconvenient. The aim of this new feature is to allow to provide a function in MCP configuration that the server will use to acquire a new token when it is needed. This function can implement custom authentication mechanism.
|
@dkmaker is there a way this can be merged (even if with some modification). We really need a way to implement authentication and some other improvements later. If you don't have a capacity to work on this, we can publish a forked version |
|
I will review it today seems like a cool addition! |
|
PR Review: Add support for dynamic Bearer token via custom JS module Thanks for this contribution! This is a useful feature that addresses a real need for dynamic token acquisition. The implementation is generally solid with good JWT handling and proper caching/deduplication. Issues to Address1. Missing 401 retry logic (Important)The Suggested fix: Add retry logic that invalidates the token and retries once on 401: // After making the request, check for 401 and retry with fresh token
if (response.status === 401 && this.tokenProvider) {
this.tokenProvider.invalidate();
const freshToken = await this.tokenProvider.getToken(true);
config.headers = {
...config.headers,
'Authorization': `Bearer ${freshToken}`
};
// Retry the request once
response = await this.axiosInstance.request(config);
}2. Security documentationLoading and executing user-provided JavaScript modules is a trust boundary. Consider adding a note to the README/docs warning users to only use trusted modules, e.g.:
Minor Items3. Whitespace changes in READMELines 1-2 show whitespace-only changes (possibly CRLF line ending differences). Consider reverting these to keep the diff clean. 4. Optional: TTL for non-JWT tokensFor tokens without an 5. Optional: Example auth modulesIt would be great to include an
This would help users understand how to implement their own modules and serve as working reference implementations. If you have any examples from your own use case that could be generalized, that would be very helpful! Overall this is good work - the |
…om authorization contexts. Problem: Many APIs and applications require multiple parallel sessions (e.g., multiple users, multiple roles). Current dynamic approach hardcodes single credentials pair for all requests. Solution: Allow LLM to provide MCP with override credentials. This allows having multiple users (in additional to default ones).
|
@dkmaker thank you for your review. I've just added yet another commit - sorry for late update, I made these changes earlier, but wasn't sure if you still monitor this project. These changes are more controversial because they add yet another tool parameter so I'm open to you suggestion on this too. I'll review your current suggestions over the weekend. Thanks again. |
|
The current implementation works fine and my Claude Code agent is able to use default user and then switch to another user (one he create beforehand) and then switch back to default |
Motivation:
In real life bearer token is temporary and generated via API call. Setting it each time in MCP configuration is inconvenient. The aim of this new feature is to allow to provide a function in MCP configuration that the server will use to acquire a new token when it is needed. This function can implement custom authentication mechanism.