@prunellejs/mcp 0.7.2 → 0.7.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 +2 -2
- package/src/index.ts +51 -46
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prunellejs/mcp",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.4",
|
|
4
4
|
"description": "Zero-dependency MCP Streamable HTTP (2025-03-26) plugin exposing Prunelle server actions as AI agent tools",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -21,6 +21,6 @@
|
|
|
21
21
|
"directory": "packages/plugin-mcp"
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|
|
24
|
-
"prunelle": "^0.7.
|
|
24
|
+
"prunelle": "^0.7.4"
|
|
25
25
|
}
|
|
26
26
|
}
|
package/src/index.ts
CHANGED
|
@@ -433,7 +433,8 @@ function extractBearerToken(request: Request): string | undefined {
|
|
|
433
433
|
/** The shortest bearer token the endpoint accepts: 32 bytes, the size of a random key. */
|
|
434
434
|
const MIN_TOKEN_BYTES = 32
|
|
435
435
|
/** Wrong or missing bearer tokens one client may send per window before the endpoint answers 429. */
|
|
436
|
-
const
|
|
436
|
+
const FAILED_AUTH_MAX = 10
|
|
437
|
+
const FAILED_AUTH_WINDOW_MS = 60_000
|
|
437
438
|
|
|
438
439
|
function mcpTooManyRequests(retryAfter: number): Response {
|
|
439
440
|
return Response.json(
|
|
@@ -442,6 +443,33 @@ function mcpTooManyRequests(retryAfter: number): Response {
|
|
|
442
443
|
)
|
|
443
444
|
}
|
|
444
445
|
|
|
446
|
+
function createAuthorizer(
|
|
447
|
+
tokenSource: McpTokenSource | undefined,
|
|
448
|
+
readTokenFile: (filePath: string) => Promise<string | undefined>,
|
|
449
|
+
failedAuthLimit: RateLimit<RequestContext>,
|
|
450
|
+
failures: RateLimiter,
|
|
451
|
+
): (ctx: RequestContext) => Promise<Response | undefined> {
|
|
452
|
+
return async (ctx) => {
|
|
453
|
+
const configured = await resolveToken(tokenSource, readTokenFile)
|
|
454
|
+
if (configured === undefined || Buffer.byteLength(configured) < MIN_TOKEN_BYTES)
|
|
455
|
+
return new Response('Not Found', { status: 404 })
|
|
456
|
+
|
|
457
|
+
const key = rateLimitKey(failedAuthLimit, ctx)
|
|
458
|
+
const retryAfter = failures.peek(key)
|
|
459
|
+
if (retryAfter > 0)
|
|
460
|
+
return mcpTooManyRequests(retryAfter)
|
|
461
|
+
const provided = extractBearerToken(ctx.request)
|
|
462
|
+
if (provided === undefined || !safeEqual(provided, configured)) {
|
|
463
|
+
failures.hit(key)
|
|
464
|
+
return Response.json(
|
|
465
|
+
{ error: 'Unauthorized' },
|
|
466
|
+
{ status: 401, headers: { 'www-authenticate': 'Bearer' } },
|
|
467
|
+
)
|
|
468
|
+
}
|
|
469
|
+
return undefined
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
445
473
|
export function mcp<T extends Record<string, unknown>>(
|
|
446
474
|
options: Omit<McpOptions, 'tools'> & { tools: McpToolsRecord<T> },
|
|
447
475
|
): PrunellePlugin
|
|
@@ -466,44 +494,6 @@ export function mcp(options: McpOptions): PrunellePlugin {
|
|
|
466
494
|
const warnSink = customWarn ?? ((msg: string) => console.warn(msg))
|
|
467
495
|
const readTokenFile = createTokenFileReader()
|
|
468
496
|
|
|
469
|
-
const rpc = createRpcDispatcher({
|
|
470
|
-
serverName,
|
|
471
|
-
serverVersion,
|
|
472
|
-
instructions: options.instructions,
|
|
473
|
-
listedTools,
|
|
474
|
-
toolMap,
|
|
475
|
-
resourceMap,
|
|
476
|
-
listedResources,
|
|
477
|
-
formatResult: options.formatResult,
|
|
478
|
-
mapError: options.mapError,
|
|
479
|
-
limiterFor: limit => actionLimiter?.(limit),
|
|
480
|
-
})
|
|
481
|
-
|
|
482
|
-
// Set by server(): failed bearer attempts per client, counted apart from the call limit.
|
|
483
|
-
let failures: RateLimiter | undefined
|
|
484
|
-
// Set by server(): counts a tool's calls against the `rateLimit` of the action behind it.
|
|
485
|
-
let actionLimiter: ((limit: RateLimit<ActionContext>) => RateLimiter) | undefined
|
|
486
|
-
|
|
487
|
-
async function authorize(ctx: RequestContext): Promise<Response | undefined> {
|
|
488
|
-
const configured = await resolveToken(options.token, readTokenFile)
|
|
489
|
-
if (configured === undefined || Buffer.byteLength(configured) < MIN_TOKEN_BYTES)
|
|
490
|
-
return new Response('Not Found', { status: 404 })
|
|
491
|
-
|
|
492
|
-
const key = rateLimitKey(FAILED_AUTH_LIMIT, ctx)
|
|
493
|
-
const retryAfter = failures?.peek(key) ?? 0
|
|
494
|
-
if (retryAfter > 0)
|
|
495
|
-
return mcpTooManyRequests(retryAfter)
|
|
496
|
-
const provided = extractBearerToken(ctx.request)
|
|
497
|
-
if (provided === undefined || !safeEqual(provided, configured)) {
|
|
498
|
-
failures?.hit(key)
|
|
499
|
-
return Response.json(
|
|
500
|
-
{ error: 'Unauthorized' },
|
|
501
|
-
{ status: 401, headers: { 'www-authenticate': 'Bearer' } },
|
|
502
|
-
)
|
|
503
|
-
}
|
|
504
|
-
return undefined
|
|
505
|
-
}
|
|
506
|
-
|
|
507
497
|
return definePlugin({
|
|
508
498
|
name: '@prunellejs/mcp',
|
|
509
499
|
|
|
@@ -532,12 +522,27 @@ export function mcp(options: McpOptions): PrunellePlugin {
|
|
|
532
522
|
return Response.json({ error: `Forbidden: untrusted origin ${origin}` }, { status: 403 })
|
|
533
523
|
}
|
|
534
524
|
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
const
|
|
538
|
-
const
|
|
525
|
+
const failedAuthLimit: RateLimit<RequestContext> = { max: FAILED_AUTH_MAX, windowMs: FAILED_AUTH_WINDOW_MS }
|
|
526
|
+
const failures = api.rateLimiter(failedAuthLimit)
|
|
527
|
+
const authorize = createAuthorizer(options.token, readTokenFile, failedAuthLimit, failures)
|
|
528
|
+
const rpc = createRpcDispatcher({
|
|
529
|
+
serverName,
|
|
530
|
+
serverVersion,
|
|
531
|
+
instructions: options.instructions,
|
|
532
|
+
listedTools,
|
|
533
|
+
toolMap,
|
|
534
|
+
resourceMap,
|
|
535
|
+
listedResources,
|
|
536
|
+
formatResult: options.formatResult,
|
|
537
|
+
mapError: options.mapError,
|
|
538
|
+
limiterFor: limit => api.rateLimiter(limit),
|
|
539
|
+
})
|
|
540
|
+
|
|
541
|
+
const serverRateLimit = rateLimit !== undefined ? { ...rateLimit } : undefined
|
|
542
|
+
const postLimiter = serverRateLimit !== undefined ? api.rateLimiter(serverRateLimit) : undefined
|
|
543
|
+
const consumePostTokens = postLimiter !== undefined && serverRateLimit !== undefined
|
|
539
544
|
? (ctx: RequestContext, count: number): Response | undefined => {
|
|
540
|
-
const key = rateLimitKey(
|
|
545
|
+
const key = rateLimitKey(serverRateLimit, ctx)
|
|
541
546
|
for (let i = 0; i < count; i++) {
|
|
542
547
|
const retryAfter = postLimiter.hit(key)
|
|
543
548
|
if (retryAfter > 0)
|
|
@@ -568,7 +573,7 @@ export function mcp(options: McpOptions): PrunellePlugin {
|
|
|
568
573
|
if (denied !== undefined)
|
|
569
574
|
return denied
|
|
570
575
|
return new Response('Method Not Allowed', { status: 405, headers: { allow: 'POST, DELETE' } })
|
|
571
|
-
}, { rateLimit })
|
|
576
|
+
}, { rateLimit: serverRateLimit })
|
|
572
577
|
|
|
573
578
|
api.route('DELETE', endpoint, async (ctx) => {
|
|
574
579
|
const originDenied = checkOrigin(ctx)
|
|
@@ -578,7 +583,7 @@ export function mcp(options: McpOptions): PrunellePlugin {
|
|
|
578
583
|
if (denied !== undefined)
|
|
579
584
|
return denied
|
|
580
585
|
return new Response(null, { status: 204 })
|
|
581
|
-
}, { csrf: false, rateLimit })
|
|
586
|
+
}, { csrf: false, rateLimit: serverRateLimit })
|
|
582
587
|
},
|
|
583
588
|
|
|
584
589
|
check(ctx) {
|