158 lines
5.7 KiB
TypeScript
158 lines
5.7 KiB
TypeScript
import { createFixedWindowRateLimiter, type FixedWindowDecision, type FixedWindowRateLimiter, type SharedStore } from "@hivegrid/shared-store";
|
|
|
|
import { getMcpSharedStore } from "./shared-store.js";
|
|
|
|
export const MCP_KEY_RATE_LIMIT_PER_HOUR = 1_000;
|
|
export const MCP_TENANT_RATE_LIMIT_PER_HOUR = 10_000;
|
|
export const MCP_RATE_LIMIT_WINDOW_SECONDS = 60 * 60;
|
|
|
|
// Pre-authentication per-IP shield. The per-key/per-tenant limits above only
|
|
// apply after a token resolves, so requests bearing an invalid/absent token
|
|
// were never throttled yet still forced DB token lookups. This sheds an
|
|
// unauthenticated flood cheaply, keyed on the trusted client IP.
|
|
export const MCP_PREAUTH_IP_RATE_LIMIT_PER_MINUTE = 300;
|
|
export const MCP_PREAUTH_IP_WINDOW_SECONDS = 60;
|
|
|
|
// A single Streamable-HTTP POST may carry a JSON-RPC batch (array) that the SDK
|
|
// dispatches message-by-message. Cap how many operations one request may carry
|
|
// so a batch cannot amplify a single rate-limit token into thousands of tool
|
|
// invocations. Legitimate clients send one request at a time or tiny batches.
|
|
export const MCP_MAX_BATCH_OPERATIONS = 50;
|
|
|
|
type Limiters = {
|
|
key: FixedWindowRateLimiter;
|
|
tenant: FixedWindowRateLimiter;
|
|
ip: FixedWindowRateLimiter;
|
|
};
|
|
|
|
let limiters: Limiters | null = null;
|
|
let storeFactory: () => SharedStore = () => getMcpSharedStore();
|
|
|
|
export type McpRateLimitAllowed = {
|
|
allowed: true;
|
|
headers: Record<string, string>;
|
|
};
|
|
|
|
export type McpRateLimitBlocked = {
|
|
allowed: false;
|
|
status: 429;
|
|
code: "rate_limit_exceeded";
|
|
message: string;
|
|
headers: Record<string, string>;
|
|
};
|
|
|
|
export type McpRateLimitResult = McpRateLimitAllowed | McpRateLimitBlocked;
|
|
|
|
export async function checkMcpRateLimit(input: { apiKeyId: string; tenantId: string; weight?: number }): Promise<McpRateLimitResult> {
|
|
const { key, tenant } = getLimiters();
|
|
// Consume one token per JSON-RPC operation the request carries (weight), not
|
|
// one per HTTP request — otherwise a batch bypasses the per-key/per-tenant cap.
|
|
const weight = Math.max(1, Math.min(input.weight ?? 1, MCP_MAX_BATCH_OPERATIONS));
|
|
const [keyDecision, tenantDecision] = await Promise.all([
|
|
consumeWeighted(key, input.apiKeyId, weight),
|
|
consumeWeighted(tenant, input.tenantId, weight),
|
|
]);
|
|
const blockedScope = !keyDecision.allowed ? "key" : !tenantDecision.allowed ? "tenant" : null;
|
|
const decision = blockedScope === "key" ? keyDecision : tenantDecision;
|
|
const limit = blockedScope === "key" ? MCP_KEY_RATE_LIMIT_PER_HOUR : blockedScope === "tenant" ? MCP_TENANT_RATE_LIMIT_PER_HOUR : Math.min(MCP_KEY_RATE_LIMIT_PER_HOUR, MCP_TENANT_RATE_LIMIT_PER_HOUR);
|
|
const headers = createRateLimitHeaders({
|
|
limit,
|
|
remaining: Math.min(keyDecision.remaining, tenantDecision.remaining),
|
|
resetAt: keyDecision.resetAt.getTime() >= tenantDecision.resetAt.getTime() ? keyDecision.resetAt : tenantDecision.resetAt,
|
|
scope: blockedScope ?? "combined",
|
|
});
|
|
|
|
if (blockedScope && decision) {
|
|
return {
|
|
allowed: false,
|
|
status: 429,
|
|
code: "rate_limit_exceeded",
|
|
message: `MCP ${blockedScope} rate limit exceeded. Try again after the reset time.`,
|
|
headers,
|
|
};
|
|
}
|
|
|
|
return { allowed: true, headers };
|
|
}
|
|
|
|
// Pre-auth per-IP shield, run before the token lookups so invalid/absent
|
|
// tokens cannot force unbounded DB work. Keyed on the trusted client IP.
|
|
export async function checkMcpPreAuthIpRateLimit(ip: string): Promise<McpRateLimitResult> {
|
|
const { ip: limiter } = getLimiters();
|
|
const decision = await limiter.consume(ip);
|
|
const headers = createRateLimitHeaders({
|
|
limit: MCP_PREAUTH_IP_RATE_LIMIT_PER_MINUTE,
|
|
remaining: decision.remaining,
|
|
resetAt: decision.resetAt,
|
|
scope: "ip",
|
|
});
|
|
if (!decision.allowed) {
|
|
return {
|
|
allowed: false,
|
|
status: 429,
|
|
code: "rate_limit_exceeded",
|
|
message: "MCP ip rate limit exceeded. Try again after the reset time.",
|
|
headers,
|
|
};
|
|
}
|
|
return { allowed: true, headers };
|
|
}
|
|
|
|
export function __setMcpRateLimitStoreForTests(store: SharedStore): void {
|
|
limiters = null;
|
|
storeFactory = () => store;
|
|
}
|
|
|
|
export function __resetMcpRateLimitStoreForTests(): void {
|
|
limiters = null;
|
|
storeFactory = () => getMcpSharedStore();
|
|
}
|
|
|
|
// Consume `weight` tokens from a fixed-window limiter. The underlying store
|
|
// increments atomically, so the last decision reflects the cumulative count;
|
|
// `allowed` is false once the window budget is exceeded by any of the tokens.
|
|
async function consumeWeighted(limiter: FixedWindowRateLimiter, key: string, weight: number): Promise<FixedWindowDecision> {
|
|
let decision = await limiter.consume(key);
|
|
for (let i = 1; i < weight; i += 1) {
|
|
decision = await limiter.consume(key);
|
|
}
|
|
return decision;
|
|
}
|
|
|
|
function getLimiters(): Limiters {
|
|
if (limiters) {
|
|
return limiters;
|
|
}
|
|
const store = storeFactory();
|
|
limiters = {
|
|
key: createFixedWindowRateLimiter({
|
|
store,
|
|
prefix: "mcp:key",
|
|
windowSeconds: MCP_RATE_LIMIT_WINDOW_SECONDS,
|
|
max: MCP_KEY_RATE_LIMIT_PER_HOUR,
|
|
}),
|
|
tenant: createFixedWindowRateLimiter({
|
|
store,
|
|
prefix: "mcp:tenant",
|
|
windowSeconds: MCP_RATE_LIMIT_WINDOW_SECONDS,
|
|
max: MCP_TENANT_RATE_LIMIT_PER_HOUR,
|
|
}),
|
|
ip: createFixedWindowRateLimiter({
|
|
store,
|
|
prefix: "mcp:ip",
|
|
windowSeconds: MCP_PREAUTH_IP_WINDOW_SECONDS,
|
|
max: MCP_PREAUTH_IP_RATE_LIMIT_PER_MINUTE,
|
|
}),
|
|
};
|
|
return limiters;
|
|
}
|
|
|
|
function createRateLimitHeaders(input: { limit: number; remaining: number; resetAt: Date; scope: string }): Record<string, string> {
|
|
return {
|
|
"X-RateLimit-Limit": String(input.limit),
|
|
"X-RateLimit-Remaining": String(Math.max(0, input.remaining)),
|
|
"X-RateLimit-Reset": String(Math.ceil(input.resetAt.getTime() / 1000)),
|
|
"X-RateLimit-Scope": input.scope,
|
|
};
|
|
}
|