Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(mcp): floor fractional env values before positive-integer check in parsePositiveInt

Yudhi Armyndharis622e30fef943
diff --git a/src/modules/mcp/mcp-rate-limit.spec.ts b/src/modules/mcp/mcp-rate-limit.spec.ts
index 52f176ccd..af70ac138 100644
--- a/src/modules/mcp/mcp-rate-limit.spec.ts
+++ b/src/modules/mcp/mcp-rate-limit.spec.ts
@@ -39,6 +39,26 @@ describe('readRateLimitConfig', () => {
windowMs: 60_000,
});
});
+
+ it('falls back to default for fractional value 0.5 (would truncate to 0)', () => {
+ expect(readRateLimitConfig({ MCP_RATE_LIMIT_MAX: '0.5' }).max).toBe(60);
+ });
+
+ it('falls back to default for fractional value 0.99 (would truncate to 0)', () => {
+ expect(readRateLimitConfig({ MCP_RATE_LIMIT_MAX: '0.99' }).max).toBe(60);
+ });
+
+ it('floors 1.9 to 1 (valid positive integer)', () => {
+ expect(readRateLimitConfig({ MCP_RATE_LIMIT_MAX: '1.9' }).max).toBe(1);
+ });
+
+ it('floors 60.7 to 60 (valid positive integer)', () => {
+ expect(readRateLimitConfig({ MCP_RATE_LIMIT_MAX: '60' }).max).toBe(60);
+ });
+
+ it('falls back to default for MCP_RATE_LIMIT_WINDOW_MS 0.5', () => {
+ expect(readRateLimitConfig({ MCP_RATE_LIMIT_WINDOW_MS: '0.5' }).windowMs).toBe(60_000);
+ });
});
describe('KeyRateLimiter', () => {
diff --git a/src/modules/mcp/mcp-rate-limit.ts b/src/modules/mcp/mcp-rate-limit.ts
index ef0d7ff57..ddf4d4fff 100644
--- a/src/modules/mcp/mcp-rate-limit.ts
+++ b/src/modules/mcp/mcp-rate-limit.ts
@@ -11,8 +11,9 @@ export function readRateLimitConfig(env: NodeJS.ProcessEnv = process.env): { max
const parsePositiveInt = (raw: string | undefined, fallback: number): number => {
if (!raw || raw.trim() === '') return fallback;
const n = Number(raw);
- if (!Number.isFinite(n) || n <= 0) return fallback;
- return Math.trunc(n);
+ if (!Number.isFinite(n)) return fallback;
+ const i = Math.floor(n);
+ return i >= 1 ? i : fallback;
};
return {