-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAuthenticationError.ts
More file actions
92 lines (81 loc) · 2.5 KB
/
AuthenticationError.ts
File metadata and controls
92 lines (81 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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",
},
};
}
}