@casys/mcp-server 0.26.1 → 0.27.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +10 -627
- package/esm/mod.d.ts +1 -0
- package/esm/mod.js +1 -0
- package/package.json +28 -25
- package/mod.ts +0 -302
- package/src/auth/as-metadata-proxy.ts +0 -288
- package/src/auth/config.ts +0 -368
- package/src/auth/jwt-provider.ts +0 -327
- package/src/auth/middleware.ts +0 -257
- package/src/auth/mod.ts +0 -89
- package/src/auth/multi-tenant-middleware.ts +0 -236
- package/src/auth/presets.ts +0 -351
- package/src/auth/provider.ts +0 -49
- package/src/auth/scope-middleware.ts +0 -59
- package/src/auth/static-token-provider.ts +0 -197
- package/src/auth/types.ts +0 -207
- package/src/client-auth/callback-server.ts +0 -166
- package/src/client-auth/client-id-metadata.ts +0 -260
- package/src/client-auth/connect.ts +0 -72
- package/src/client-auth/mod.ts +0 -44
- package/src/client-auth/provider.ts +0 -300
- package/src/client-auth/token-store/file-store.ts +0 -73
- package/src/client-auth/token-store/memory-store.ts +0 -32
- package/src/client-auth/types.ts +0 -95
- package/src/concurrency/rate-limiter.ts +0 -190
- package/src/concurrency/request-queue.ts +0 -140
- package/src/http/body.ts +0 -72
- package/src/http/request-guards.ts +0 -137
- package/src/http/wire.ts +0 -41
- package/src/inspector/launcher.ts +0 -131
- package/src/mcp-app.ts +0 -4595
- package/src/middleware/backpressure.ts +0 -36
- package/src/middleware/mod.ts +0 -21
- package/src/middleware/rate-limit.ts +0 -45
- package/src/middleware/runner.ts +0 -63
- package/src/middleware/types.ts +0 -104
- package/src/middleware/validation.ts +0 -28
- package/src/mrtr/capability-check.ts +0 -303
- package/src/mrtr/mod.ts +0 -54
- package/src/mrtr/replay-store.ts +0 -103
- package/src/mrtr/request-state.ts +0 -435
- package/src/mrtr/retry-guard.ts +0 -110
- package/src/mrtr/types.ts +0 -107
- package/src/observability/metrics.ts +0 -336
- package/src/observability/mod.ts +0 -21
- package/src/observability/otel.ts +0 -330
- package/src/runtime/runtime.deno.ts +0 -149
- package/src/runtime/runtime.node.ts +0 -324
- package/src/runtime/runtime.ts +0 -50
- package/src/runtime/types.ts +0 -130
- package/src/security/channel-hmac.ts +0 -140
- package/src/security/csp.ts +0 -89
- package/src/security/message-signer.ts +0 -223
- package/src/subscriptions/subscription-registry.ts +0 -558
- package/src/tasks/mod.ts +0 -33
- package/src/tasks/task-store.ts +0 -1014
- package/src/transport/request-headers.ts +0 -661
- package/src/types.ts +0 -908
- package/src/ui/viewer-utils.ts +0 -117
- package/src/validation/schema-validator.ts +0 -307
package/src/client-auth/mod.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OAuth client-auth helpers for MCP clients.
|
|
3
|
-
*
|
|
4
|
-
* Includes the local callback server, OAuth client provider wiring, token
|
|
5
|
-
* stores, and Client ID Metadata Document (CIMD) helpers.
|
|
6
|
-
*
|
|
7
|
-
* @module lib/server/client-auth
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
// OAuth provider wiring
|
|
11
|
-
export { prepareOAuthProvider } from "./connect.js";
|
|
12
|
-
export type { PrepareOAuthResult } from "./connect.js";
|
|
13
|
-
|
|
14
|
-
// OAuthClientProvider implementation
|
|
15
|
-
export { OAuthClientProviderImpl } from "./provider.js";
|
|
16
|
-
|
|
17
|
-
// Local callback server
|
|
18
|
-
export { CallbackServer } from "./callback-server.js";
|
|
19
|
-
export type { CallbackServerOptions } from "./callback-server.js";
|
|
20
|
-
|
|
21
|
-
// Types
|
|
22
|
-
export type {
|
|
23
|
-
CimdClientConfig,
|
|
24
|
-
OAuthClientConfig,
|
|
25
|
-
OAuthClientConfigBase,
|
|
26
|
-
StaticClientConfig,
|
|
27
|
-
StoredCredentials,
|
|
28
|
-
TokenStore,
|
|
29
|
-
} from "./types.js";
|
|
30
|
-
|
|
31
|
-
// Token stores
|
|
32
|
-
export { MemoryTokenStore } from "./token-store/memory-store.js";
|
|
33
|
-
export { FileTokenStore } from "./token-store/file-store.js";
|
|
34
|
-
|
|
35
|
-
// Client ID Metadata Documents (CIMD)
|
|
36
|
-
export {
|
|
37
|
-
buildClientIdMetadataDocument,
|
|
38
|
-
CimdConfigError,
|
|
39
|
-
isCimdConfig,
|
|
40
|
-
} from "./client-id-metadata.js";
|
|
41
|
-
export type {
|
|
42
|
-
CimdConfigErrorCode,
|
|
43
|
-
ClientIdMetadataDocument,
|
|
44
|
-
} from "./client-id-metadata.js";
|
|
@@ -1,300 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OAuth client provider implementation.
|
|
3
|
-
*
|
|
4
|
-
* Implements OAuthClientProvider from the MCP SDK for authenticating
|
|
5
|
-
* against OAuth-protected MCP servers. Handles token storage,
|
|
6
|
-
* PKCE flow, and browser-based authorization.
|
|
7
|
-
*
|
|
8
|
-
* @module lib/server/client-auth/provider
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import type {
|
|
12
|
-
OAuthClientInformationMixed,
|
|
13
|
-
OAuthClientMetadata,
|
|
14
|
-
OAuthTokens,
|
|
15
|
-
} from "@modelcontextprotocol/sdk/shared/auth.js";
|
|
16
|
-
import type {
|
|
17
|
-
OAuthClientProvider,
|
|
18
|
-
OAuthDiscoveryState,
|
|
19
|
-
} from "@modelcontextprotocol/sdk/client/auth.js";
|
|
20
|
-
import type { OAuthClientConfig } from "./types.js";
|
|
21
|
-
import {
|
|
22
|
-
CimdConfigError,
|
|
23
|
-
isCimdConfig,
|
|
24
|
-
resolveClientMode,
|
|
25
|
-
validateCimdClientConfig,
|
|
26
|
-
} from "./client-id-metadata.js";
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Normalise an authorization server identifier before comparing it.
|
|
30
|
-
*
|
|
31
|
-
* `https://as.example.com` and `https://as.example.com/` name the same server,
|
|
32
|
-
* and so do `HTTPS://AS.Example.com` and an explicit `:443`. Comparing raw
|
|
33
|
-
* strings would read those as a server change and wipe valid credentials on
|
|
34
|
-
* every call — a self-inflicted login loop. Falls back to the raw string if it
|
|
35
|
-
* does not parse, which then simply never matches.
|
|
36
|
-
*/
|
|
37
|
-
function canonicalIssuer(value: string): string {
|
|
38
|
-
try {
|
|
39
|
-
return new URL(value).href;
|
|
40
|
-
} catch {
|
|
41
|
-
return value;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* An authorization server URL, safe to put in a log line.
|
|
47
|
-
*
|
|
48
|
-
* A URL may carry `userinfo` credentials, and a query string may carry a token
|
|
49
|
-
* or an internal identifier. Origin and path are enough to tell an operator
|
|
50
|
-
* which server is involved; the rest is only a way for secrets to reach a log
|
|
51
|
-
* aggregator.
|
|
52
|
-
*/
|
|
53
|
-
function redactUrl(value: string): string {
|
|
54
|
-
try {
|
|
55
|
-
const url = new URL(value);
|
|
56
|
-
return `${url.origin}${url.pathname}`;
|
|
57
|
-
} catch {
|
|
58
|
-
return "<unparseable url>";
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export class OAuthClientProviderImpl implements OAuthClientProvider {
|
|
63
|
-
private serverUrl: string;
|
|
64
|
-
private config: OAuthClientConfig;
|
|
65
|
-
private _codeVerifier = "";
|
|
66
|
-
private _clientInfo: OAuthClientInformationMixed | undefined;
|
|
67
|
-
private _redirectUrl: string | undefined;
|
|
68
|
-
/**
|
|
69
|
-
* Issuer the SDK resolved for the request currently being authorized
|
|
70
|
-
* (SEP-2352). Set by {@link saveDiscoveryState}, which `auth()` calls after
|
|
71
|
-
* discovery and before it asks for tokens.
|
|
72
|
-
*/
|
|
73
|
-
private _currentIssuer: string | undefined;
|
|
74
|
-
|
|
75
|
-
constructor(serverUrl: string, config: OAuthClientConfig) {
|
|
76
|
-
const mode = resolveClientMode(config);
|
|
77
|
-
if (mode === "client_id_metadata") {
|
|
78
|
-
validateCimdClientConfig(config);
|
|
79
|
-
}
|
|
80
|
-
this.serverUrl = serverUrl;
|
|
81
|
-
this.config = config;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
get redirectUrl(): string | URL {
|
|
85
|
-
if (isCimdConfig(this.config)) {
|
|
86
|
-
return this.config.clientRegistration.redirectUri;
|
|
87
|
-
}
|
|
88
|
-
return this._redirectUrl ?? "http://localhost:0/callback";
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/** Set the redirect URL (called after CallbackServer binds to a port). */
|
|
92
|
-
setRedirectUrl(url: string): void {
|
|
93
|
-
if (isCimdConfig(this.config)) {
|
|
94
|
-
if (url !== this.config.clientRegistration.redirectUri) {
|
|
95
|
-
throw new CimdConfigError(
|
|
96
|
-
"cimd_redirect_mismatch",
|
|
97
|
-
"CIMD redirectUrl cannot differ from configured redirectUri",
|
|
98
|
-
{
|
|
99
|
-
redirectUrl: url,
|
|
100
|
-
configuredRedirectUri: this.config.clientRegistration.redirectUri,
|
|
101
|
-
},
|
|
102
|
-
"Use clientRegistration.redirectUri as the runtime redirect URL.",
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
this._redirectUrl = url;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
get clientMetadata(): OAuthClientMetadata {
|
|
111
|
-
return {
|
|
112
|
-
client_name: this.config.clientName ?? "PML Client",
|
|
113
|
-
redirect_uris: [String(this.redirectUrl)],
|
|
114
|
-
grant_types: ["authorization_code", "refresh_token"],
|
|
115
|
-
response_types: ["code"],
|
|
116
|
-
token_endpoint_auth_method: "none", // public client, PKCE only
|
|
117
|
-
application_type: "native", // loopback CLI/desktop — prevents AS rejection of http://127.0.0.1 redirects
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
clientInformation(): Promise<OAuthClientInformationMixed | undefined> {
|
|
122
|
-
if (isCimdConfig(this.config)) {
|
|
123
|
-
return Promise.resolve({
|
|
124
|
-
client_id: this.config.clientRegistration.clientIdMetadataUrl,
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
return Promise.resolve(
|
|
128
|
-
this._clientInfo ?? {
|
|
129
|
-
client_id: this.config.clientId,
|
|
130
|
-
},
|
|
131
|
-
);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
saveClientInformation(
|
|
135
|
-
info: OAuthClientInformationMixed,
|
|
136
|
-
): Promise<void> {
|
|
137
|
-
this._clientInfo = info;
|
|
138
|
-
return Promise.resolve();
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Record the authorization server the SDK just resolved (SEP-2352).
|
|
143
|
-
*
|
|
144
|
-
* `auth()` calls this after RFC 9728 discovery and before {@link tokens},
|
|
145
|
-
* which is what makes the check there possible.
|
|
146
|
-
*
|
|
147
|
-
* The binding is the **discovered URL**, not `metadata.issuer`. That
|
|
148
|
-
* distinction is the whole protection: `issuer` is a field the authorization
|
|
149
|
-
* server publishes about itself, so an attacker's server reached at
|
|
150
|
-
* `https://attacker.example.com` can claim
|
|
151
|
-
* `issuer: "https://as-one.example.com"` and, since SDK 1.29 validates the
|
|
152
|
-
* metadata's shape without checking it against the URL it was fetched from,
|
|
153
|
-
* would be handed the credentials minted by the real AS-one. The URL the
|
|
154
|
-
* client actually resolved is the one thing in this exchange the server
|
|
155
|
-
* cannot forge.
|
|
156
|
-
*
|
|
157
|
-
* RFC 8414 does require `issuer` to match the discovery URL. A mismatch is
|
|
158
|
-
* therefore a broken or hostile authorization server, and it is logged —
|
|
159
|
-
* but nothing here depends on the claim being honest.
|
|
160
|
-
*/
|
|
161
|
-
saveDiscoveryState(state: OAuthDiscoveryState): Promise<void> {
|
|
162
|
-
const resolved = canonicalIssuer(state.authorizationServerUrl);
|
|
163
|
-
const claimed = state.authorizationServerMetadata?.issuer;
|
|
164
|
-
|
|
165
|
-
if (claimed !== undefined && canonicalIssuer(claimed) !== resolved) {
|
|
166
|
-
this.log(
|
|
167
|
-
`authorization server at ${redactUrl(state.authorizationServerUrl)} ` +
|
|
168
|
-
`claims issuer "${redactUrl(claimed)}" ` +
|
|
169
|
-
"(RFC 8414 requires them to match); " +
|
|
170
|
-
"binding credentials to the discovered URL",
|
|
171
|
-
);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
this._currentIssuer = resolved;
|
|
175
|
-
return Promise.resolve();
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Stored tokens, unless they were issued by a different authorization server.
|
|
180
|
-
*
|
|
181
|
-
* SEP-2352 makes this a MUST, and the reason is concrete rather than
|
|
182
|
-
* bureaucratic. `auth()` hands whatever this returns to
|
|
183
|
-
* `refreshAuthorization(authorizationServerUrl, …)`, where the URL comes from
|
|
184
|
-
* the MCP server's own protected-resource metadata. A server that changes —
|
|
185
|
-
* or is made to advertise — a different authorization server would therefore
|
|
186
|
-
* receive a refresh token minted by the previous one. The client must not let
|
|
187
|
-
* the resource server pick who sees its credentials.
|
|
188
|
-
*
|
|
189
|
-
* The check only runs once an issuer is known, which is the case inside
|
|
190
|
-
* `auth()`. A transport asking for the bearer to attach to an ordinary
|
|
191
|
-
* request has no issuer in hand, and that path is not the exposure: those
|
|
192
|
-
* tokens go to the MCP server they were minted for, not to an authorization
|
|
193
|
-
* server.
|
|
194
|
-
*/
|
|
195
|
-
async tokens(): Promise<OAuthTokens | undefined> {
|
|
196
|
-
const stored = await this.config.tokenStore.get(this.serverUrl);
|
|
197
|
-
if (!stored) return undefined;
|
|
198
|
-
if (this._currentIssuer === undefined) return stored.tokens;
|
|
199
|
-
|
|
200
|
-
if (stored.authServerUrl === undefined) {
|
|
201
|
-
// Written before this binding existed: nothing proves which authorization
|
|
202
|
-
// server minted them. Treated as unusable rather than trusted once —
|
|
203
|
-
// the whole point is to stop handing credentials to an unverified party,
|
|
204
|
-
// and "probably fine" is the assumption this check exists to remove. The
|
|
205
|
-
// cost is one re-authorization per stored credential, at upgrade time.
|
|
206
|
-
await this.config.tokenStore.delete(this.serverUrl);
|
|
207
|
-
this.log(
|
|
208
|
-
"discarding stored credentials with no recorded issuer (SEP-2352); re-authorization required",
|
|
209
|
-
);
|
|
210
|
-
return undefined;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
if (canonicalIssuer(stored.authServerUrl) !== this._currentIssuer) {
|
|
214
|
-
// A legitimate migration lands here too, and is handled the same way:
|
|
215
|
-
// drop the old credentials and let the flow re-authorize against the new
|
|
216
|
-
// server. What must not happen is the old refresh token travelling there.
|
|
217
|
-
await this.config.tokenStore.delete(this.serverUrl);
|
|
218
|
-
this.log(
|
|
219
|
-
`authorization server changed for ${redactUrl(this.serverUrl)} ` +
|
|
220
|
-
`(${redactUrl(stored.authServerUrl)} → ` +
|
|
221
|
-
`${redactUrl(this._currentIssuer)}); ` +
|
|
222
|
-
"stored credentials discarded, re-authorization required",
|
|
223
|
-
);
|
|
224
|
-
return undefined;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
return stored.tokens;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
async saveTokens(tokens: OAuthTokens): Promise<void> {
|
|
231
|
-
// Sequential guard only: keep an existing refresh_token when a later save omits it.
|
|
232
|
-
// This get/set is not atomic; concurrent saves for one serverUrl are unsupported
|
|
233
|
-
// because refresh is serialized by the caller.
|
|
234
|
-
const stored = await this.config.tokenStore.get(this.serverUrl);
|
|
235
|
-
|
|
236
|
-
// …but only from the same authorization server. `auth({ authorizationCode })`
|
|
237
|
-
// reaches here without going through `tokens()`, so this is the one path
|
|
238
|
-
// where a credential from a previous issuer can survive: if the code
|
|
239
|
-
// exchange returns no refresh token, carrying the old one over would
|
|
240
|
-
// re-label another server's secret with the new issuer and hand it over on
|
|
241
|
-
// the next refresh. A legacy record has no recorded issuer, so it cannot
|
|
242
|
-
// clear this bar either.
|
|
243
|
-
const inheritable = stored?.authServerUrl !== undefined &&
|
|
244
|
-
this._currentIssuer !== undefined &&
|
|
245
|
-
canonicalIssuer(stored.authServerUrl) === this._currentIssuer;
|
|
246
|
-
|
|
247
|
-
const tokensToStore = tokens.refresh_token === undefined &&
|
|
248
|
-
inheritable && stored?.tokens.refresh_token !== undefined
|
|
249
|
-
? { ...tokens, refresh_token: stored.tokens.refresh_token }
|
|
250
|
-
: tokens;
|
|
251
|
-
|
|
252
|
-
await this.config.tokenStore.set(this.serverUrl, {
|
|
253
|
-
serverUrl: this.serverUrl,
|
|
254
|
-
tokens: tokensToStore,
|
|
255
|
-
obtainedAt: Date.now(),
|
|
256
|
-
// Binds the credentials to their minter. Without it every save produces
|
|
257
|
-
// exactly the legacy record `tokens()` has to throw away.
|
|
258
|
-
...(this._currentIssuer !== undefined
|
|
259
|
-
? { authServerUrl: this._currentIssuer }
|
|
260
|
-
: {}),
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
private log(message: string): void {
|
|
265
|
-
this.config.logger?.(`[client-auth] ${message}`);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
async redirectToAuthorization(authorizationUrl: URL): Promise<void> {
|
|
269
|
-
await this.config.openBrowser(authorizationUrl.toString());
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
saveCodeVerifier(codeVerifier: string): Promise<void> {
|
|
273
|
-
this._codeVerifier = codeVerifier;
|
|
274
|
-
return Promise.resolve();
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
codeVerifier(): Promise<string> {
|
|
278
|
-
return Promise.resolve(this._codeVerifier);
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
async invalidateCredentials(
|
|
282
|
-
scope: "all" | "client" | "tokens" | "verifier" | "discovery",
|
|
283
|
-
): Promise<void> {
|
|
284
|
-
if (scope === "all" || scope === "tokens") {
|
|
285
|
-
await this.config.tokenStore.delete(this.serverUrl);
|
|
286
|
-
}
|
|
287
|
-
if (scope === "all" || scope === "verifier") {
|
|
288
|
-
this._codeVerifier = "";
|
|
289
|
-
}
|
|
290
|
-
if (scope === "all" || scope === "client") {
|
|
291
|
-
this._clientInfo = undefined;
|
|
292
|
-
}
|
|
293
|
-
if (scope === "all" || scope === "discovery") {
|
|
294
|
-
// Keeping a stale issuer would make the next `tokens()` compare against
|
|
295
|
-
// an authorization server the SDK has already given up on, and discard
|
|
296
|
-
// credentials that are in fact still valid.
|
|
297
|
-
this._currentIssuer = undefined;
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* File-based token store.
|
|
3
|
-
*
|
|
4
|
-
* Stores OAuth credentials as JSON files with restrictive permissions (0o600).
|
|
5
|
-
* One file per MCP server, keyed by SHA-256 hash of the server URL.
|
|
6
|
-
*
|
|
7
|
-
* @module lib/server/client-auth/token-store/file-store
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import type { StoredCredentials, TokenStore } from "../types.js";
|
|
11
|
-
import {
|
|
12
|
-
mkdir,
|
|
13
|
-
readDir,
|
|
14
|
-
readTextFile,
|
|
15
|
-
remove,
|
|
16
|
-
writeTextFile,
|
|
17
|
-
} from "../../runtime/runtime.js";
|
|
18
|
-
|
|
19
|
-
async function sha256hex(input: string): Promise<string> {
|
|
20
|
-
const data = new TextEncoder().encode(input);
|
|
21
|
-
const hash = await crypto.subtle.digest("SHA-256", data);
|
|
22
|
-
return Array.from(new Uint8Array(hash))
|
|
23
|
-
.map((b) => b.toString(16).padStart(2, "0"))
|
|
24
|
-
.join("");
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export class FileTokenStore implements TokenStore {
|
|
28
|
-
constructor(private baseDir: string) {}
|
|
29
|
-
|
|
30
|
-
private async filePath(serverUrl: string): Promise<string> {
|
|
31
|
-
const hash = await sha256hex(serverUrl);
|
|
32
|
-
return `${this.baseDir}/${hash}.json`;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
private async ensureDir(): Promise<void> {
|
|
36
|
-
await mkdir(this.baseDir, { recursive: true, mode: 0o700 });
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
async get(serverUrl: string): Promise<StoredCredentials | null> {
|
|
40
|
-
const path = await this.filePath(serverUrl);
|
|
41
|
-
const content = await readTextFile(path);
|
|
42
|
-
return content === null ? null : JSON.parse(content) as StoredCredentials;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
async set(serverUrl: string, credentials: StoredCredentials): Promise<void> {
|
|
46
|
-
await this.ensureDir();
|
|
47
|
-
const path = await this.filePath(serverUrl);
|
|
48
|
-
const content = JSON.stringify(credentials, null, 2);
|
|
49
|
-
await writeTextFile(path, content, { mode: 0o600 });
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async delete(serverUrl: string): Promise<void> {
|
|
53
|
-
const path = await this.filePath(serverUrl);
|
|
54
|
-
await remove(path);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
async list(): Promise<string[]> {
|
|
58
|
-
const urls: string[] = [];
|
|
59
|
-
for (const name of await readDir(this.baseDir)) {
|
|
60
|
-
if (name.endsWith(".json")) {
|
|
61
|
-
try {
|
|
62
|
-
const content = await readTextFile(`${this.baseDir}/${name}`);
|
|
63
|
-
if (content === null) continue;
|
|
64
|
-
const creds = JSON.parse(content) as StoredCredentials;
|
|
65
|
-
urls.push(creds.serverUrl);
|
|
66
|
-
} catch {
|
|
67
|
-
// Corrupted file, skip
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
return urls;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* In-memory token store for testing and ephemeral use.
|
|
3
|
-
*
|
|
4
|
-
* @module lib/server/client-auth/token-store/memory-store
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import type { StoredCredentials, TokenStore } from "../types.js";
|
|
8
|
-
|
|
9
|
-
export class MemoryTokenStore implements TokenStore {
|
|
10
|
-
private store = new Map<string, StoredCredentials>();
|
|
11
|
-
|
|
12
|
-
// Not `async`: the Map operations are synchronous. The interface returns
|
|
13
|
-
// Promises so a real store can be I/O-backed; `Promise.resolve` satisfies it
|
|
14
|
-
// without claiming an await point that does not exist.
|
|
15
|
-
get(serverUrl: string): Promise<StoredCredentials | null> {
|
|
16
|
-
return Promise.resolve(this.store.get(serverUrl) ?? null);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
set(serverUrl: string, credentials: StoredCredentials): Promise<void> {
|
|
20
|
-
this.store.set(serverUrl, credentials);
|
|
21
|
-
return Promise.resolve();
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
delete(serverUrl: string): Promise<void> {
|
|
25
|
-
this.store.delete(serverUrl);
|
|
26
|
-
return Promise.resolve();
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
list(): Promise<string[]> {
|
|
30
|
-
return Promise.resolve([...this.store.keys()]);
|
|
31
|
-
}
|
|
32
|
-
}
|
package/src/client-auth/types.ts
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OAuth client auth types.
|
|
3
|
-
*
|
|
4
|
-
* Interfaces for token storage and OAuth client configuration.
|
|
5
|
-
* Used by OAuthClientProviderImpl to authenticate against
|
|
6
|
-
* OAuth-protected MCP servers.
|
|
7
|
-
*
|
|
8
|
-
* @module lib/server/client-auth/types
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import type { OAuthTokens } from "@modelcontextprotocol/sdk/shared/auth.js";
|
|
12
|
-
|
|
13
|
-
/** Stored OAuth credentials for one MCP server */
|
|
14
|
-
export interface StoredCredentials {
|
|
15
|
-
/** The MCP server URL these credentials are for */
|
|
16
|
-
serverUrl: string;
|
|
17
|
-
/** OAuth tokens (access_token, refresh_token, etc.) */
|
|
18
|
-
tokens: OAuthTokens;
|
|
19
|
-
/** When the tokens were obtained (Unix epoch ms) */
|
|
20
|
-
obtainedAt: number;
|
|
21
|
-
/**
|
|
22
|
-
* Issuer that minted these credentials (SEP-2352).
|
|
23
|
-
*
|
|
24
|
-
* Absent on records written before the binding existed; `tokens()` treats
|
|
25
|
-
* that as unusable, since nothing proves who minted them.
|
|
26
|
-
*/
|
|
27
|
-
authServerUrl?: string;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** Abstract token storage — consumers provide the implementation */
|
|
31
|
-
export interface TokenStore {
|
|
32
|
-
/** Retrieve stored credentials for a server, or null if none */
|
|
33
|
-
get(serverUrl: string): Promise<StoredCredentials | null>;
|
|
34
|
-
/** Store credentials for a server */
|
|
35
|
-
set(serverUrl: string, credentials: StoredCredentials): Promise<void>;
|
|
36
|
-
/** Delete credentials for a server */
|
|
37
|
-
delete(serverUrl: string): Promise<void>;
|
|
38
|
-
/** List all server URLs with stored credentials */
|
|
39
|
-
list(): Promise<string[]>;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** Fields shared by all OAuth client registration modes. */
|
|
43
|
-
export interface OAuthClientConfigBase {
|
|
44
|
-
/** Client name shown in consent screen */
|
|
45
|
-
clientName?: string;
|
|
46
|
-
/** Scopes to request (space-separated in OAuth, array here) */
|
|
47
|
-
scopes?: string[];
|
|
48
|
-
/** Token storage backend */
|
|
49
|
-
tokenStore: TokenStore;
|
|
50
|
-
/** Callback to open browser — injected by consumer (platform-specific) */
|
|
51
|
-
openBrowser: (url: string) => Promise<void>;
|
|
52
|
-
/** Callback server port (0 = OS auto-assign, default: 0) */
|
|
53
|
-
callbackPort?: number;
|
|
54
|
-
/** Timeout for user to complete OAuth flow (ms, default: 120_000) */
|
|
55
|
-
authTimeout?: number;
|
|
56
|
-
/**
|
|
57
|
-
* Optional sink for credential-lifecycle events.
|
|
58
|
-
*
|
|
59
|
-
* Notably an authorization-server change (SEP-2352): the client silently
|
|
60
|
-
* drops its stored credentials and re-authorizes, and an operator debugging
|
|
61
|
-
* an unexpected login prompt has no other way to see why.
|
|
62
|
-
*/
|
|
63
|
-
logger?: (message: string) => void;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** Static OAuth client registration (default, backwards-compatible mode). */
|
|
67
|
-
export interface StaticClientConfig extends OAuthClientConfigBase {
|
|
68
|
-
/** OAuth Client ID (public — embedded in consumer binary) */
|
|
69
|
-
clientId: string;
|
|
70
|
-
/** Absence of clientRegistration selects static client-id behavior. */
|
|
71
|
-
clientRegistration?: undefined;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/** CIMD client registration (explicit opt-in). */
|
|
75
|
-
export interface CimdClientConfig extends OAuthClientConfigBase {
|
|
76
|
-
/** In CIMD mode, client_id is derived from clientIdMetadataUrl. */
|
|
77
|
-
clientId?: never;
|
|
78
|
-
clientRegistration: {
|
|
79
|
-
method: "client_id_metadata";
|
|
80
|
-
/** HTTPS URL of the hosted Client ID Metadata Document; also the client_id. */
|
|
81
|
-
clientIdMetadataUrl: string;
|
|
82
|
-
/** Runtime redirect URI published in the metadata document. */
|
|
83
|
-
redirectUri: string;
|
|
84
|
-
/** Optional display and extension metadata for the hosted document. */
|
|
85
|
-
metadata?: {
|
|
86
|
-
client_uri?: string;
|
|
87
|
-
logo_uri?: string;
|
|
88
|
-
contacts?: string[];
|
|
89
|
-
extra?: Record<string, unknown>;
|
|
90
|
-
};
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** Configuration for creating an OAuthClientProvider */
|
|
95
|
-
export type OAuthClientConfig = StaticClientConfig | CimdClientConfig;
|