@casys/mcp-server 0.18.0 → 0.20.0
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/mod.ts +24 -0
- package/package.json +2 -2
- package/src/auth/as-metadata-proxy.ts +27 -11
- package/src/client-auth/callback-server.ts +79 -43
- package/src/client-auth/client-id-metadata.ts +260 -0
- package/src/client-auth/connect.ts +26 -2
- package/src/client-auth/mod.ts +44 -0
- package/src/client-auth/provider.ts +45 -8
- package/src/client-auth/token-store/file-store.ts +23 -32
- package/src/client-auth/types.ts +33 -4
- package/src/mcp-app.ts +165 -18
- package/src/runtime/runtime.ts +111 -7
- package/src/runtime/types.ts +41 -1
- package/src/sampling/sampling-bridge.ts +2 -0
- package/src/types.ts +30 -2
package/mod.ts
CHANGED
|
@@ -185,6 +185,30 @@ export {
|
|
|
185
185
|
} from "./src/auth/mod.js";
|
|
186
186
|
export type { AuthConfig, AuthProviderName } from "./src/auth/mod.js";
|
|
187
187
|
|
|
188
|
+
// Client Auth — OAuth client flow + CIMD
|
|
189
|
+
export {
|
|
190
|
+
buildClientIdMetadataDocument,
|
|
191
|
+
CallbackServer,
|
|
192
|
+
CimdConfigError,
|
|
193
|
+
FileTokenStore,
|
|
194
|
+
isCimdConfig,
|
|
195
|
+
MemoryTokenStore,
|
|
196
|
+
OAuthClientProviderImpl,
|
|
197
|
+
prepareOAuthProvider,
|
|
198
|
+
} from "./src/client-auth/mod.js";
|
|
199
|
+
export type {
|
|
200
|
+
CallbackServerOptions,
|
|
201
|
+
CimdClientConfig,
|
|
202
|
+
CimdConfigErrorCode,
|
|
203
|
+
ClientIdMetadataDocument,
|
|
204
|
+
OAuthClientConfig,
|
|
205
|
+
OAuthClientConfigBase,
|
|
206
|
+
PrepareOAuthResult,
|
|
207
|
+
StaticClientConfig,
|
|
208
|
+
StoredCredentials,
|
|
209
|
+
TokenStore,
|
|
210
|
+
} from "./src/client-auth/mod.js";
|
|
211
|
+
|
|
188
212
|
// Observability
|
|
189
213
|
export {
|
|
190
214
|
endToolCallSpan,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@casys/mcp-server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"description": "Production-ready MCP server framework with concurrency control, auth, and observability",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "mod.ts",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
14
|
-
"@modelcontextprotocol/ext-apps": "^1.7.
|
|
14
|
+
"@modelcontextprotocol/ext-apps": "^1.7.4",
|
|
15
15
|
"hono": "^4.0.0",
|
|
16
16
|
"ajv": "^8.17.1",
|
|
17
17
|
"jose": "^6.0.0",
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* AS Metadata Proxy for DCR
|
|
2
|
+
* AS Metadata Proxy for DCR compatibility discovery.
|
|
3
3
|
*
|
|
4
4
|
* Creates a Web Standard request handler that serves an RFC 8414
|
|
5
5
|
* Authorization Server Metadata document enriched with
|
|
6
|
-
* `registration_endpoint` (RFC 7591).
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
6
|
+
* `registration_endpoint` (RFC 7591). This is a compatibility path for
|
|
7
|
+
* MCP clients and IdPs that still rely on Dynamic Client Registration
|
|
8
|
+
* (DCR), which is now optional (`MAY`) in the 2025-11-25 MCP spec.
|
|
9
|
+
*
|
|
10
|
+
* For new OAuth client integrations, prefer the spec-preferred Client ID
|
|
11
|
+
* Metadata Documents (CIMD) on the client side. See
|
|
12
|
+
* `src/client-auth/client-id-metadata.ts`.
|
|
10
13
|
*
|
|
11
14
|
* **Issuer field note (RFC 8414 §3.2)**:
|
|
12
15
|
* The `issuer` field in the served metadata will be the upstream IdP's
|
|
@@ -15,7 +18,8 @@
|
|
|
15
18
|
* overriding `issuer` would cause JWT `iss` claim mismatches — the IdP
|
|
16
19
|
* still issues tokens with its own `iss`, and clients verify
|
|
17
20
|
* `token.iss === metadata.issuer`. The metadata-URL mismatch is the
|
|
18
|
-
* lesser evil
|
|
21
|
+
* lesser evil for this compatibility path and is not needed when clients
|
|
22
|
+
* use CIMD or the upstream IdP ships native registration discovery.
|
|
19
23
|
*
|
|
20
24
|
* @module lib/server/auth/as-metadata-proxy
|
|
21
25
|
*/
|
|
@@ -140,7 +144,9 @@ function resolveMetadataUrl(options: AsMetadataProxyOptions): string {
|
|
|
140
144
|
}
|
|
141
145
|
|
|
142
146
|
validateUrl(upstreamIssuer!, "upstreamIssuer");
|
|
143
|
-
return `${
|
|
147
|
+
return `${
|
|
148
|
+
stripTrailingSlash(upstreamIssuer!)
|
|
149
|
+
}/.well-known/openid-configuration`;
|
|
144
150
|
}
|
|
145
151
|
|
|
146
152
|
/**
|
|
@@ -173,8 +179,13 @@ export function createAsMetadataHandler(
|
|
|
173
179
|
// ── Fail-fast validation at construction time ──
|
|
174
180
|
const metadataUrl = resolveMetadataUrl(options);
|
|
175
181
|
validateUrl(options.registrationEndpoint, "registrationEndpoint");
|
|
176
|
-
if (
|
|
177
|
-
|
|
182
|
+
if (
|
|
183
|
+
options.cacheTtlMs !== undefined &&
|
|
184
|
+
(options.cacheTtlMs < 0 || !Number.isFinite(options.cacheTtlMs))
|
|
185
|
+
) {
|
|
186
|
+
throw new Error(
|
|
187
|
+
"AsMetadataProxyOptions.cacheTtlMs must be a non-negative finite number",
|
|
188
|
+
);
|
|
178
189
|
}
|
|
179
190
|
|
|
180
191
|
const {
|
|
@@ -200,7 +211,9 @@ export function createAsMetadataHandler(
|
|
|
200
211
|
const body = await res.json();
|
|
201
212
|
if (body === null || typeof body !== "object" || Array.isArray(body)) {
|
|
202
213
|
throw new Error(
|
|
203
|
-
`AS metadata from ${metadataUrl} is not a JSON object (got ${
|
|
214
|
+
`AS metadata from ${metadataUrl} is not a JSON object (got ${
|
|
215
|
+
Array.isArray(body) ? "array" : typeof body
|
|
216
|
+
})`,
|
|
204
217
|
);
|
|
205
218
|
}
|
|
206
219
|
if (typeof body.issuer !== "string") {
|
|
@@ -245,7 +258,10 @@ export function createAsMetadataHandler(
|
|
|
245
258
|
|
|
246
259
|
return async (req: Request): Promise<Response> => {
|
|
247
260
|
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
248
|
-
return new Response(null, {
|
|
261
|
+
return new Response(null, {
|
|
262
|
+
status: 405,
|
|
263
|
+
headers: { "Allow": "GET, HEAD" },
|
|
264
|
+
});
|
|
249
265
|
}
|
|
250
266
|
try {
|
|
251
267
|
const upstream = await getMetadata();
|
|
@@ -8,9 +8,13 @@
|
|
|
8
8
|
* @module lib/server/client-auth/callback-server
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
+
import { serve, type ServeHandle } from "../runtime/runtime.js";
|
|
12
|
+
|
|
11
13
|
export interface CallbackServerOptions {
|
|
12
14
|
/** Port to listen on (0 = auto-assign, default: 0) */
|
|
13
15
|
port?: number;
|
|
16
|
+
/** Hostname to bind (default: 127.0.0.1) */
|
|
17
|
+
hostname?: string;
|
|
14
18
|
/** Timeout in ms (default: 120_000) */
|
|
15
19
|
timeout?: number;
|
|
16
20
|
}
|
|
@@ -35,26 +39,33 @@ body{font-family:system-ui;display:flex;justify-content:center;align-items:cente
|
|
|
35
39
|
|
|
36
40
|
export class CallbackServer {
|
|
37
41
|
private port: number;
|
|
42
|
+
private hostname: string;
|
|
38
43
|
private timeout: number;
|
|
39
|
-
private
|
|
40
|
-
private server: Deno.HttpServer | null = null;
|
|
44
|
+
private server: ServeHandle | null = null;
|
|
41
45
|
private timerId: ReturnType<typeof setTimeout> | null = null;
|
|
42
46
|
private closeTimerId: ReturnType<typeof setTimeout> | null = null;
|
|
43
47
|
|
|
44
48
|
constructor(options?: CallbackServerOptions) {
|
|
45
49
|
this.port = options?.port ?? 0;
|
|
50
|
+
this.hostname = options?.hostname ?? "127.0.0.1";
|
|
46
51
|
this.timeout = options?.timeout ?? 120_000;
|
|
47
52
|
}
|
|
48
53
|
|
|
49
54
|
async start(): Promise<{ port: number; codePromise: Promise<string> }> {
|
|
50
|
-
this.abortController = new AbortController();
|
|
51
|
-
|
|
52
55
|
let resolveCode!: (code: string) => void;
|
|
53
56
|
let rejectCode!: (err: Error) => void;
|
|
54
57
|
const codePromise = new Promise<string>((resolve, reject) => {
|
|
55
58
|
resolveCode = resolve;
|
|
56
59
|
rejectCode = reject;
|
|
57
60
|
});
|
|
61
|
+
let resolveListen!: (port: number) => void;
|
|
62
|
+
let rejectListen!: (err: Error) => void;
|
|
63
|
+
const listenPromise = new Promise<number>((resolve) => {
|
|
64
|
+
resolveListen = resolve;
|
|
65
|
+
});
|
|
66
|
+
const bindPromise = new Promise<never>((_, reject) => {
|
|
67
|
+
rejectListen = reject;
|
|
68
|
+
});
|
|
58
69
|
|
|
59
70
|
this.timerId = setTimeout(() => {
|
|
60
71
|
this.timerId = null;
|
|
@@ -66,46 +77,71 @@ export class CallbackServer {
|
|
|
66
77
|
this.close();
|
|
67
78
|
}, this.timeout);
|
|
68
79
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
try {
|
|
81
|
+
this.server = serve(
|
|
82
|
+
{
|
|
83
|
+
port: this.port,
|
|
84
|
+
hostname: this.hostname,
|
|
85
|
+
onListen: ({ port }) => {
|
|
86
|
+
this.port = port;
|
|
87
|
+
resolveListen(port);
|
|
88
|
+
},
|
|
89
|
+
onError: (err) => {
|
|
90
|
+
rejectListen(err);
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
(req) => {
|
|
94
|
+
const url = new URL(req.url);
|
|
95
|
+
if (url.pathname === "/callback") {
|
|
96
|
+
const code = url.searchParams.get("code");
|
|
97
|
+
if (!code) {
|
|
98
|
+
return new Response(ERROR_HTML, {
|
|
99
|
+
status: 400,
|
|
100
|
+
headers: { "Content-Type": "text/html" },
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
if (this.timerId) {
|
|
104
|
+
clearTimeout(this.timerId);
|
|
105
|
+
this.timerId = null;
|
|
106
|
+
}
|
|
107
|
+
resolveCode(code);
|
|
108
|
+
// Schedule close after response is sent
|
|
109
|
+
this.closeTimerId = setTimeout(() => {
|
|
110
|
+
this.closeTimerId = null;
|
|
111
|
+
this.close();
|
|
112
|
+
}, 100);
|
|
113
|
+
return new Response(SUCCESS_HTML, {
|
|
114
|
+
status: 200,
|
|
82
115
|
headers: { "Content-Type": "text/html" },
|
|
83
116
|
});
|
|
84
117
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
this.closeTimerId = null;
|
|
93
|
-
this.close();
|
|
94
|
-
}, 100);
|
|
95
|
-
return new Response(SUCCESS_HTML, {
|
|
96
|
-
status: 200,
|
|
97
|
-
headers: { "Content-Type": "text/html" },
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
return new Response("Not Found", { status: 404 });
|
|
101
|
-
},
|
|
102
|
-
);
|
|
118
|
+
return new Response("Not Found", { status: 404 });
|
|
119
|
+
},
|
|
120
|
+
);
|
|
121
|
+
} catch (err) {
|
|
122
|
+
await this.cleanupAfterStartFailure();
|
|
123
|
+
throw err;
|
|
124
|
+
}
|
|
103
125
|
|
|
104
|
-
|
|
105
|
-
|
|
126
|
+
let port: number;
|
|
127
|
+
try {
|
|
128
|
+
port = await Promise.race([listenPromise, bindPromise]);
|
|
129
|
+
} catch (err) {
|
|
130
|
+
await this.cleanupAfterStartFailure();
|
|
131
|
+
throw err;
|
|
132
|
+
}
|
|
133
|
+
return { port, codePromise };
|
|
106
134
|
}
|
|
107
135
|
|
|
108
136
|
async close(): Promise<void> {
|
|
137
|
+
this.clearTimers();
|
|
138
|
+
if (this.server) {
|
|
139
|
+
await this.server.shutdown();
|
|
140
|
+
this.server = null;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private clearTimers(): void {
|
|
109
145
|
if (this.timerId) {
|
|
110
146
|
clearTimeout(this.timerId);
|
|
111
147
|
this.timerId = null;
|
|
@@ -114,15 +150,15 @@ export class CallbackServer {
|
|
|
114
150
|
clearTimeout(this.closeTimerId);
|
|
115
151
|
this.closeTimerId = null;
|
|
116
152
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private async cleanupAfterStartFailure(): Promise<void> {
|
|
156
|
+
this.clearTimers();
|
|
121
157
|
if (this.server) {
|
|
122
158
|
try {
|
|
123
|
-
await this.server.
|
|
159
|
+
await this.server.shutdown();
|
|
124
160
|
} catch {
|
|
125
|
-
//
|
|
161
|
+
// Ignore shutdown failures after a failed bind.
|
|
126
162
|
}
|
|
127
163
|
this.server = null;
|
|
128
164
|
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import type { CimdClientConfig, OAuthClientConfig } from "./types.js";
|
|
2
|
+
|
|
3
|
+
export type CimdConfigErrorCode =
|
|
4
|
+
| "cimd_config_conflict"
|
|
5
|
+
| "cimd_registration_missing"
|
|
6
|
+
| "cimd_method_invalid"
|
|
7
|
+
| "cimd_url_invalid"
|
|
8
|
+
| "cimd_port_unfixed"
|
|
9
|
+
| "cimd_redirect_mismatch"
|
|
10
|
+
| "cimd_name_missing"
|
|
11
|
+
| "cimd_reserved_metadata_key";
|
|
12
|
+
|
|
13
|
+
export type OAuthClientMode = "static" | "client_id_metadata";
|
|
14
|
+
|
|
15
|
+
export class CimdConfigError extends Error {
|
|
16
|
+
constructor(
|
|
17
|
+
public readonly code: CimdConfigErrorCode,
|
|
18
|
+
message: string,
|
|
19
|
+
public readonly context: Record<string, unknown>,
|
|
20
|
+
public readonly recovery: string,
|
|
21
|
+
options?: ErrorOptions,
|
|
22
|
+
) {
|
|
23
|
+
super(message, options);
|
|
24
|
+
this.name = "CimdConfigError";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ClientIdMetadataDocument {
|
|
29
|
+
client_id: string;
|
|
30
|
+
client_name: string;
|
|
31
|
+
redirect_uris: string[];
|
|
32
|
+
grant_types: ["authorization_code"];
|
|
33
|
+
response_types: ["code"];
|
|
34
|
+
token_endpoint_auth_method: "none";
|
|
35
|
+
/** RFC 7591 application type. Always "native" for loopback CLI/desktop clients. */
|
|
36
|
+
application_type: "native" | "web";
|
|
37
|
+
scope?: string;
|
|
38
|
+
client_uri?: string;
|
|
39
|
+
logo_uri?: string;
|
|
40
|
+
contacts?: string[];
|
|
41
|
+
[key: string]: unknown;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function isCimdConfig(
|
|
45
|
+
config: OAuthClientConfig,
|
|
46
|
+
): config is CimdClientConfig {
|
|
47
|
+
return config.clientRegistration?.method === "client_id_metadata";
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function resolveClientMode(config: OAuthClientConfig): OAuthClientMode {
|
|
51
|
+
const raw = config as unknown as Record<string, unknown>;
|
|
52
|
+
const hasClientId = raw.clientId !== undefined;
|
|
53
|
+
const hasClientRegistration = raw.clientRegistration !== undefined;
|
|
54
|
+
|
|
55
|
+
if (hasClientId && hasClientRegistration) {
|
|
56
|
+
throw new CimdConfigError(
|
|
57
|
+
"cimd_config_conflict",
|
|
58
|
+
"OAuth client config cannot include both clientId and clientRegistration",
|
|
59
|
+
{ hasClientId: true, hasClientRegistration: true },
|
|
60
|
+
"Choose either static clientId mode or client_id_metadata mode.",
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (hasClientRegistration) {
|
|
65
|
+
const registration = raw.clientRegistration;
|
|
66
|
+
if (
|
|
67
|
+
registration === null || typeof registration !== "object" ||
|
|
68
|
+
Array.isArray(registration)
|
|
69
|
+
) {
|
|
70
|
+
throw new CimdConfigError(
|
|
71
|
+
"cimd_registration_missing",
|
|
72
|
+
"clientRegistration must be an object with a registration method",
|
|
73
|
+
{ clientRegistration: registration },
|
|
74
|
+
'Provide clientRegistration.method = "client_id_metadata".',
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
const method = (registration as Record<string, unknown>).method;
|
|
78
|
+
if (method === undefined) {
|
|
79
|
+
throw new CimdConfigError(
|
|
80
|
+
"cimd_registration_missing",
|
|
81
|
+
"clientRegistration.method is required",
|
|
82
|
+
{ method },
|
|
83
|
+
'Provide clientRegistration.method = "client_id_metadata".',
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
if (method !== "client_id_metadata") {
|
|
87
|
+
throw new CimdConfigError(
|
|
88
|
+
"cimd_method_invalid",
|
|
89
|
+
'Unsupported clientRegistration.method; expected "client_id_metadata"',
|
|
90
|
+
{ method },
|
|
91
|
+
'Set clientRegistration.method to "client_id_metadata".',
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
return "client_id_metadata";
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (typeof raw.clientId !== "string") {
|
|
98
|
+
throw new CimdConfigError(
|
|
99
|
+
"cimd_registration_missing",
|
|
100
|
+
"OAuth client config requires either clientId or clientRegistration",
|
|
101
|
+
{ hasClientId: false, hasClientRegistration: false },
|
|
102
|
+
"Provide clientId for static mode or clientRegistration for CIMD mode.",
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return "static";
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function buildClientIdMetadataDocument(
|
|
110
|
+
config: CimdClientConfig,
|
|
111
|
+
): ClientIdMetadataDocument {
|
|
112
|
+
validateCimdClientConfig(config);
|
|
113
|
+
|
|
114
|
+
const { clientRegistration } = config;
|
|
115
|
+
const document = {
|
|
116
|
+
...clientRegistration.metadata?.extra,
|
|
117
|
+
client_uri: clientRegistration.metadata?.client_uri,
|
|
118
|
+
logo_uri: clientRegistration.metadata?.logo_uri,
|
|
119
|
+
contacts: clientRegistration.metadata?.contacts,
|
|
120
|
+
client_id: clientRegistration.clientIdMetadataUrl,
|
|
121
|
+
client_name: config.clientName!,
|
|
122
|
+
redirect_uris: [clientRegistration.redirectUri],
|
|
123
|
+
grant_types: ["authorization_code"],
|
|
124
|
+
response_types: ["code"],
|
|
125
|
+
token_endpoint_auth_method: "none",
|
|
126
|
+
application_type: "native",
|
|
127
|
+
} as ClientIdMetadataDocument;
|
|
128
|
+
|
|
129
|
+
if (config.scopes?.length) {
|
|
130
|
+
document.scope = config.scopes.join(" ");
|
|
131
|
+
}
|
|
132
|
+
for (const key of ["client_uri", "logo_uri", "contacts"] as const) {
|
|
133
|
+
if (document[key] === undefined) {
|
|
134
|
+
delete document[key];
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return document;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function validateCimdClientConfig(
|
|
142
|
+
config: OAuthClientConfig,
|
|
143
|
+
): asserts config is CimdClientConfig {
|
|
144
|
+
const mode = resolveClientMode(config);
|
|
145
|
+
if (mode !== "client_id_metadata") {
|
|
146
|
+
throw new CimdConfigError(
|
|
147
|
+
"cimd_registration_missing",
|
|
148
|
+
"CIMD config requires clientRegistration",
|
|
149
|
+
{ mode },
|
|
150
|
+
'Provide clientRegistration.method = "client_id_metadata".',
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const raw = config as unknown as Record<string, unknown>;
|
|
155
|
+
const registration = raw.clientRegistration as Record<string, unknown>;
|
|
156
|
+
const metadataUrl = registration.clientIdMetadataUrl;
|
|
157
|
+
try {
|
|
158
|
+
if (typeof metadataUrl !== "string") {
|
|
159
|
+
throw new Error("expected string");
|
|
160
|
+
}
|
|
161
|
+
const parsed = new URL(metadataUrl);
|
|
162
|
+
if (parsed.protocol !== "https:") {
|
|
163
|
+
throw new Error("expected https scheme");
|
|
164
|
+
}
|
|
165
|
+
} catch (cause) {
|
|
166
|
+
throw new CimdConfigError(
|
|
167
|
+
"cimd_url_invalid",
|
|
168
|
+
"clientIdMetadataUrl must be an absolute HTTPS URL",
|
|
169
|
+
{ clientIdMetadataUrl: metadataUrl },
|
|
170
|
+
"Provide an absolute https:// URL for clientRegistration.clientIdMetadataUrl.",
|
|
171
|
+
{ cause },
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
if (
|
|
175
|
+
typeof raw.callbackPort !== "number" || raw.callbackPort === 0 ||
|
|
176
|
+
!Number.isFinite(raw.callbackPort)
|
|
177
|
+
) {
|
|
178
|
+
throw new CimdConfigError(
|
|
179
|
+
"cimd_port_unfixed",
|
|
180
|
+
"CIMD mode requires a fixed callbackPort",
|
|
181
|
+
{ callbackPort: raw.callbackPort },
|
|
182
|
+
"Set callbackPort to a fixed non-zero port and publish the same port in redirectUri.",
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
if (
|
|
186
|
+
typeof raw.clientName !== "string" || raw.clientName.trim().length === 0
|
|
187
|
+
) {
|
|
188
|
+
throw new CimdConfigError(
|
|
189
|
+
"cimd_name_missing",
|
|
190
|
+
"CIMD mode requires an explicit clientName",
|
|
191
|
+
{ clientName: raw.clientName },
|
|
192
|
+
"Set clientName to the user-facing application name for the metadata document.",
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
const redirectUri = registration.redirectUri;
|
|
196
|
+
try {
|
|
197
|
+
if (typeof redirectUri !== "string") {
|
|
198
|
+
throw new Error("expected string");
|
|
199
|
+
}
|
|
200
|
+
const parsedRedirect = new URL(redirectUri);
|
|
201
|
+
if (parsedRedirect.protocol !== "http:") {
|
|
202
|
+
throw new Error("redirect scheme must be http");
|
|
203
|
+
}
|
|
204
|
+
if (parsedRedirect.hostname !== "127.0.0.1") {
|
|
205
|
+
throw new Error("redirect host must be 127.0.0.1");
|
|
206
|
+
}
|
|
207
|
+
if (parsedRedirect.pathname !== "/callback") {
|
|
208
|
+
throw new Error("redirect path must be /callback");
|
|
209
|
+
}
|
|
210
|
+
if (parsedRedirect.port !== String(raw.callbackPort)) {
|
|
211
|
+
throw new Error("redirect port differs from callbackPort");
|
|
212
|
+
}
|
|
213
|
+
} catch (cause) {
|
|
214
|
+
throw new CimdConfigError(
|
|
215
|
+
"cimd_redirect_mismatch",
|
|
216
|
+
"redirectUri must match the fixed callbackPort",
|
|
217
|
+
{ redirectUri, callbackPort: raw.callbackPort },
|
|
218
|
+
"Set clientRegistration.redirectUri to the exact loopback callback URL that will be used at runtime.",
|
|
219
|
+
{ cause },
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
validateExtraMetadataKeys(registration.metadata);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const RESERVED_METADATA_KEYS = new Set([
|
|
226
|
+
"scope",
|
|
227
|
+
"client_id",
|
|
228
|
+
"client_name",
|
|
229
|
+
"client_uri",
|
|
230
|
+
"logo_uri",
|
|
231
|
+
"contacts",
|
|
232
|
+
"redirect_uris",
|
|
233
|
+
"grant_types",
|
|
234
|
+
"response_types",
|
|
235
|
+
"token_endpoint_auth_method",
|
|
236
|
+
"application_type",
|
|
237
|
+
]);
|
|
238
|
+
|
|
239
|
+
function validateExtraMetadataKeys(metadata: unknown): void {
|
|
240
|
+
if (metadata === null || typeof metadata !== "object") {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
const extra = (metadata as { extra?: unknown }).extra;
|
|
244
|
+
if (extra === undefined) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
if (extra === null || typeof extra !== "object" || Array.isArray(extra)) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
for (const key of Object.keys(extra)) {
|
|
251
|
+
if (RESERVED_METADATA_KEYS.has(key)) {
|
|
252
|
+
throw new CimdConfigError(
|
|
253
|
+
"cimd_reserved_metadata_key",
|
|
254
|
+
"clientRegistration.metadata.extra cannot contain reserved metadata keys",
|
|
255
|
+
{ key },
|
|
256
|
+
"Move reserved OAuth client metadata fields to their dedicated config fields.",
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
@@ -11,6 +11,11 @@
|
|
|
11
11
|
import { OAuthClientProviderImpl } from "./provider.js";
|
|
12
12
|
import type { OAuthClientConfig } from "./types.js";
|
|
13
13
|
import { CallbackServer } from "./callback-server.js";
|
|
14
|
+
import {
|
|
15
|
+
CimdConfigError,
|
|
16
|
+
isCimdConfig,
|
|
17
|
+
resolveClientMode,
|
|
18
|
+
} from "./client-id-metadata.js";
|
|
14
19
|
|
|
15
20
|
export interface PrepareOAuthResult {
|
|
16
21
|
/** The configured provider — pass as authProvider to StreamableHTTPClientTransport */
|
|
@@ -35,14 +40,33 @@ export async function prepareOAuthProvider(
|
|
|
35
40
|
serverUrl: string,
|
|
36
41
|
config: OAuthClientConfig,
|
|
37
42
|
): Promise<PrepareOAuthResult> {
|
|
43
|
+
const mode = resolveClientMode(config);
|
|
44
|
+
const provider = new OAuthClientProviderImpl(serverUrl, config);
|
|
38
45
|
const callbackServer = new CallbackServer({
|
|
39
46
|
port: config.callbackPort ?? 0,
|
|
47
|
+
hostname: mode === "client_id_metadata" ? "127.0.0.1" : "localhost",
|
|
40
48
|
timeout: config.authTimeout ?? 120_000,
|
|
41
49
|
});
|
|
42
50
|
const { port } = await callbackServer.start();
|
|
43
51
|
|
|
44
|
-
|
|
45
|
-
|
|
52
|
+
if (isCimdConfig(config)) {
|
|
53
|
+
if (port !== config.callbackPort) {
|
|
54
|
+
await callbackServer.close();
|
|
55
|
+
throw new CimdConfigError(
|
|
56
|
+
"cimd_redirect_mismatch",
|
|
57
|
+
"CIMD callback server bound to a different port than callbackPort",
|
|
58
|
+
{
|
|
59
|
+
callbackPort: config.callbackPort,
|
|
60
|
+
boundPort: port,
|
|
61
|
+
redirectUri: config.clientRegistration.redirectUri,
|
|
62
|
+
},
|
|
63
|
+
"Set callbackPort to the fixed port used by the callback server and publish the same port in redirectUri.",
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
provider.setRedirectUrl(config.clientRegistration.redirectUri);
|
|
67
|
+
} else {
|
|
68
|
+
provider.setRedirectUrl(`http://localhost:${port}/callback`);
|
|
69
|
+
}
|
|
46
70
|
|
|
47
71
|
return { provider, callbackServer, callbackPort: port };
|
|
48
72
|
}
|
|
@@ -0,0 +1,44 @@
|
|
|
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";
|