-
Notifications
You must be signed in to change notification settings - Fork 3
feat: implement multi-client API key support for MCP server #41
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /** | ||
| * Authentication-specific error handling | ||
| */ | ||
|
|
||
| import { McpError, ErrorCode } from "@modelcontextprotocol/sdk/types.js"; | ||
|
|
||
| export enum AuthErrorType { | ||
| MISSING_API_KEY = "MISSING_API_KEY", | ||
| INVALID_API_KEY = "INVALID_API_KEY", | ||
| EXPIRED_API_KEY = "EXPIRED_API_KEY", | ||
| RATE_LIMITED = "RATE_LIMITED", | ||
| VALIDATION_FAILED = "VALIDATION_FAILED", | ||
| } | ||
|
|
||
| export class AuthenticationError extends McpError { | ||
| public readonly type: AuthErrorType; | ||
| public readonly keyHash?: string; | ||
| public readonly retryAfter?: number; | ||
|
|
||
| constructor(type: AuthErrorType, message: string, keyHash?: string, retryAfter?: number) { | ||
| super(ErrorCode.InvalidRequest, message); | ||
| this.name = "AuthenticationError"; | ||
| this.type = type; | ||
| this.keyHash = keyHash; | ||
| this.retryAfter = retryAfter; | ||
| } | ||
|
|
||
| static missingApiKey(): AuthenticationError { | ||
| return new AuthenticationError( | ||
| AuthErrorType.MISSING_API_KEY, | ||
| "API key is required. Provide apiKey parameter or configure server default.", | ||
| ); | ||
| } | ||
|
|
||
| static invalidApiKey(keyHash: string): AuthenticationError { | ||
| return new AuthenticationError( | ||
| AuthErrorType.INVALID_API_KEY, | ||
| "Invalid API key provided. Please check your credentials.", | ||
| keyHash, | ||
| ); | ||
| } | ||
|
|
||
| static expiredApiKey(keyHash: string): AuthenticationError { | ||
| return new AuthenticationError( | ||
| AuthErrorType.EXPIRED_API_KEY, | ||
| "API key has expired. Please obtain a new key.", | ||
| keyHash, | ||
| ); | ||
| } | ||
|
|
||
| static rateLimited(keyHash: string, retryAfter: number): AuthenticationError { | ||
| return new AuthenticationError( | ||
| AuthErrorType.RATE_LIMITED, | ||
| `Rate limit exceeded. Try again in ${retryAfter} seconds.`, | ||
| keyHash, | ||
| retryAfter, | ||
| ); | ||
| } | ||
|
|
||
| static validationFailed(keyHash: string, reason?: string): AuthenticationError { | ||
| const message = reason | ||
| ? `API key validation failed: ${reason}` | ||
| : "API key validation failed. Please check your credentials."; | ||
|
|
||
| return new AuthenticationError(AuthErrorType.VALIDATION_FAILED, message, keyHash); | ||
| } | ||
|
|
||
| /** | ||
| * Convert to MCP error response format | ||
| */ | ||
| toMcpError(): { | ||
| error: { | ||
| code: number; | ||
| message: string; | ||
| type: AuthErrorType; | ||
| keyHash?: string; | ||
| retryAfter?: number; | ||
| documentation?: string; | ||
| }; | ||
| } { | ||
| return { | ||
| error: { | ||
| code: this.code, | ||
| message: this.message, | ||
| type: this.type, | ||
| keyHash: this.keyHash, | ||
| retryAfter: this.retryAfter, | ||
| documentation: "https://docs.lighthouse.storage/mcp-server#authentication", | ||
| }, | ||
| }; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| /** | ||
| * Error exports | ||
| */ | ||
|
|
||
| export { AuthenticationError, AuthErrorType } from "./AuthenticationError.js"; |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚨 issue (security): Key validation logic is hardcoded for testing.
Make sure this test key acceptance logic is excluded from production, or strictly controlled by environment settings, to avoid security risks.