@casys/mcp-server 0.17.3 → 0.17.5

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@casys/mcp-server",
3
- "version": "0.17.3",
3
+ "version": "0.17.5",
4
4
  "description": "Production-ready MCP server framework with concurrency control, auth, and observability",
5
5
  "type": "module",
6
6
  "main": "mod.ts",
@@ -281,7 +281,11 @@ export class JwtAuthProvider extends AuthProvider {
281
281
  }
282
282
 
283
283
  return authInfo;
284
- } catch {
284
+ } catch (err) {
285
+ // Log the actual rejection reason so JWKS fetch failures, audience
286
+ // mismatches, and expiration errors are distinguishable in logs.
287
+ const msg = err instanceof Error ? err.message : String(err);
288
+ console.error(`[JwtAuthProvider] Token rejected: ${msg}`);
285
289
  return null;
286
290
  }
287
291
  }
@@ -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<Response | null> => {
1186
- if (!this.authProvider) return null;
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 createUnauthorizedResponse(
1192
- metadata.resource_metadata_url,
1193
- "missing_token",
1194
- "Authorization header with Bearer token required",
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 createUnauthorizedResponse(
1202
- metadata.resource_metadata_url,
1203
- "invalid_token",
1204
- "Invalid or expired token",
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 null;
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 authDeniedSse = await verifyHttpAuth(c.req.raw);
1305
- if (authDeniedSse) return authDeniedSse;
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 authDenied = await verifyHttpAuth(c.req.raw);
1443
- if (authDenied) return authDenied;
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",