@casys/mcp-server 0.17.2 → 0.17.4
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/package.json +1 -1
- package/src/auth/middleware.ts +7 -0
- package/src/mcp-app.ts +35 -18
- package/src/ui/viewer-utils.ts +16 -0
package/package.json
CHANGED
package/src/auth/middleware.ts
CHANGED
|
@@ -123,6 +123,13 @@ export function createAuthMiddleware(provider: AuthProvider): Middleware {
|
|
|
123
123
|
return next();
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
+
// Already authenticated by the HTTP-level gate (verifyHttpAuth).
|
|
127
|
+
// Skip the redundant verifyToken call to avoid JWKS race conditions
|
|
128
|
+
// when concurrent requests arrive on a cold cache.
|
|
129
|
+
if (ctx.authInfo) {
|
|
130
|
+
return next();
|
|
131
|
+
}
|
|
132
|
+
|
|
126
133
|
// 0.15.0+: provider's getResourceMetadata() always returns a valid
|
|
127
134
|
// absolute URL in resource_metadata_url (type system guarantees it).
|
|
128
135
|
// No derivation needed at the middleware level anymore.
|
package/src/mcp-app.ts
CHANGED
|
@@ -46,6 +46,7 @@ import {
|
|
|
46
46
|
import { createScopeMiddleware } from "./auth/scope-middleware.js";
|
|
47
47
|
import { createAuthProviderFromConfig, loadAuthConfig } from "./auth/config.js";
|
|
48
48
|
import type { AuthProvider } from "./auth/provider.js";
|
|
49
|
+
import type { AuthInfo } from "./auth/types.js";
|
|
49
50
|
import type {
|
|
50
51
|
FetchHandler,
|
|
51
52
|
HttpRateLimitContext,
|
|
@@ -618,6 +619,7 @@ export class McpApp {
|
|
|
618
619
|
args: Record<string, unknown>,
|
|
619
620
|
request?: Request,
|
|
620
621
|
sessionId?: string,
|
|
622
|
+
preVerifiedAuthInfo?: AuthInfo,
|
|
621
623
|
): Promise<MiddlewareResult> {
|
|
622
624
|
if (!this.middlewareRunner) {
|
|
623
625
|
throw new Error(
|
|
@@ -630,6 +632,10 @@ export class McpApp {
|
|
|
630
632
|
args,
|
|
631
633
|
request,
|
|
632
634
|
sessionId,
|
|
635
|
+
// Pass pre-verified authInfo from the HTTP gate so the auth
|
|
636
|
+
// middleware skips the redundant verifyToken call (avoids JWKS
|
|
637
|
+
// race conditions on concurrent cold-cache requests).
|
|
638
|
+
authInfo: preVerifiedAuthInfo,
|
|
633
639
|
};
|
|
634
640
|
|
|
635
641
|
// OTel span + metrics
|
|
@@ -1180,32 +1186,39 @@ export class McpApp {
|
|
|
1180
1186
|
// (see `buildMetadataUrl` helper, removed 0.15.0), which produced a
|
|
1181
1187
|
// broken URL when `resource` was an opaque URI per RFC 9728 § 2
|
|
1182
1188
|
// (e.g., an OIDC project ID used as JWT audience).
|
|
1189
|
+
// Auth verification for HTTP endpoints. Returns the verified AuthInfo
|
|
1190
|
+
// on success, or an error Response if auth fails. The caller stores
|
|
1191
|
+
// the AuthInfo so the middleware pipeline can skip re-verification.
|
|
1183
1192
|
const verifyHttpAuth = async (
|
|
1184
1193
|
request: Request,
|
|
1185
|
-
): Promise<
|
|
1186
|
-
if (!this.authProvider) return
|
|
1194
|
+
): Promise<{ authInfo: AuthInfo } | { denied: Response }> => {
|
|
1195
|
+
if (!this.authProvider) return { authInfo: {} as AuthInfo };
|
|
1187
1196
|
|
|
1188
1197
|
const token = extractBearerToken(request);
|
|
1189
1198
|
if (!token) {
|
|
1190
1199
|
const metadata = this.authProvider.getResourceMetadata();
|
|
1191
|
-
return
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1200
|
+
return {
|
|
1201
|
+
denied: createUnauthorizedResponse(
|
|
1202
|
+
metadata.resource_metadata_url,
|
|
1203
|
+
"missing_token",
|
|
1204
|
+
"Authorization header with Bearer token required",
|
|
1205
|
+
),
|
|
1206
|
+
};
|
|
1196
1207
|
}
|
|
1197
1208
|
|
|
1198
1209
|
const authInfo = await this.authProvider.verifyToken(token);
|
|
1199
1210
|
if (!authInfo) {
|
|
1200
1211
|
const metadata = this.authProvider.getResourceMetadata();
|
|
1201
|
-
return
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1212
|
+
return {
|
|
1213
|
+
denied: createUnauthorizedResponse(
|
|
1214
|
+
metadata.resource_metadata_url,
|
|
1215
|
+
"invalid_token",
|
|
1216
|
+
"Invalid or expired token",
|
|
1217
|
+
),
|
|
1218
|
+
};
|
|
1206
1219
|
}
|
|
1207
1220
|
|
|
1208
|
-
return
|
|
1221
|
+
return { authInfo };
|
|
1209
1222
|
};
|
|
1210
1223
|
|
|
1211
1224
|
const checkHttpRateLimit = async (
|
|
@@ -1301,8 +1314,8 @@ export class McpApp {
|
|
|
1301
1314
|
}
|
|
1302
1315
|
|
|
1303
1316
|
// Auth gate: SSE connections require valid token when auth is configured
|
|
1304
|
-
const
|
|
1305
|
-
if (
|
|
1317
|
+
const sseAuthResult = await verifyHttpAuth(c.req.raw);
|
|
1318
|
+
if ("denied" in sseAuthResult) return sseAuthResult.denied;
|
|
1306
1319
|
|
|
1307
1320
|
// Validate session if provided
|
|
1308
1321
|
if (sessionId && !this.sessions.has(sessionId)) {
|
|
@@ -1438,9 +1451,11 @@ export class McpApp {
|
|
|
1438
1451
|
// response with WWW-Authenticate triggers OAuth discovery in MCP
|
|
1439
1452
|
// clients (Claude.ai, Cursor). Without this, clients receive a 200 on
|
|
1440
1453
|
// initialize and never attempt the OAuth/DCR flow.
|
|
1454
|
+
let httpAuthInfo: AuthInfo | undefined;
|
|
1441
1455
|
if (this.authProvider) {
|
|
1442
|
-
const
|
|
1443
|
-
if (
|
|
1456
|
+
const authResult = await verifyHttpAuth(c.req.raw);
|
|
1457
|
+
if ("denied" in authResult) return authResult.denied;
|
|
1458
|
+
httpAuthInfo = authResult.authInfo;
|
|
1444
1459
|
}
|
|
1445
1460
|
|
|
1446
1461
|
// Initialize - create session and return session ID (now auth-verified)
|
|
@@ -1529,6 +1544,7 @@ export class McpApp {
|
|
|
1529
1544
|
args,
|
|
1530
1545
|
c.req.raw,
|
|
1531
1546
|
reqSessionId,
|
|
1547
|
+
httpAuthInfo,
|
|
1532
1548
|
);
|
|
1533
1549
|
return c.json({
|
|
1534
1550
|
jsonrpc: "2.0",
|
|
@@ -2297,7 +2313,8 @@ export interface RegisterViewersConfig {
|
|
|
2297
2313
|
prefix: string;
|
|
2298
2314
|
/** import.meta.url of the consumer's server.ts (for resolving dist paths) */
|
|
2299
2315
|
moduleUrl: string;
|
|
2300
|
-
/** Check if a filesystem path
|
|
2316
|
+
/** Check if a path or URL exists. Receives a filesystem path for local
|
|
2317
|
+
* modules (file://) or an HTTPS URL for remote modules (JSR, CDN). */
|
|
2301
2318
|
exists: (path: string) => boolean;
|
|
2302
2319
|
/** Read a file and return its content as string */
|
|
2303
2320
|
readFile: (path: string) => string | Promise<string>;
|
package/src/ui/viewer-utils.ts
CHANGED
|
@@ -46,6 +46,22 @@ export function resolveViewerDistPath(
|
|
|
46
46
|
viewerName: string,
|
|
47
47
|
exists: (path: string) => boolean,
|
|
48
48
|
): string | null {
|
|
49
|
+
// When consumed from JSR or any remote registry, import.meta.url is an
|
|
50
|
+
// HTTPS URL. fileUrlToPath() only handles file:// — passing an HTTPS URL
|
|
51
|
+
// produces a broken UNC-like path ("//jsr.io/..."). Return the raw resolved
|
|
52
|
+
// URL string instead; the caller's exists/readFile callbacks are responsible
|
|
53
|
+
// for handling remote URLs (e.g., via fetch()).
|
|
54
|
+
if (moduleUrl.startsWith("https://") || moduleUrl.startsWith("http://")) {
|
|
55
|
+
const candidates = [
|
|
56
|
+
new URL(`./src/ui/dist/${viewerName}/index.html`, moduleUrl).href,
|
|
57
|
+
new URL(`./ui-dist/${viewerName}/index.html`, moduleUrl).href,
|
|
58
|
+
];
|
|
59
|
+
for (const candidate of candidates) {
|
|
60
|
+
if (exists(candidate)) return candidate;
|
|
61
|
+
}
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
|
|
49
65
|
const candidates = [
|
|
50
66
|
fileUrlToPath(new URL(`./src/ui/dist/${viewerName}/index.html`, moduleUrl)),
|
|
51
67
|
fileUrlToPath(new URL(`./ui-dist/${viewerName}/index.html`, moduleUrl)),
|