Skip to content

Commit 7cb60f6

Browse files
committed
fix: throttle mcp transport reads
1 parent 87abeda commit 7cb60f6

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

apps/web/app/api/mcp/route.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,33 @@ export async function GET(request: Request) {
344344
}
345345

346346
if (isTransportGetRequest(request)) {
347+
const clientIp = getClientIp(request)
348+
const rateLimitResult = await checkRateLimit(clientIp)
349+
350+
if (!rateLimitResult.success) {
351+
const retryAfter = Math.ceil((rateLimitResult.reset - Date.now()) / 1000)
352+
return new Response(
353+
JSON.stringify({
354+
jsonrpc: '2.0',
355+
id: null,
356+
error: {
357+
code: -32000,
358+
message: 'Rate limit exceeded',
359+
data: `Try again in ${retryAfter} seconds`
360+
}
361+
}),
362+
{
363+
status: 429,
364+
headers: {
365+
...createCorsHeaders(request),
366+
...createRateLimitHeaders(rateLimitResult),
367+
'Retry-After': String(retryAfter),
368+
'Content-Type': 'application/json'
369+
}
370+
}
371+
)
372+
}
373+
347374
try {
348375
return withRouteHeaders(
349376
request,
@@ -357,7 +384,8 @@ export async function GET(request: Request) {
357384
: undefined,
358385
telemetryEnabled: !MCP_TELEMETRY_DISABLED
359386
}
360-
)
387+
),
388+
rateLimitResult
361389
)
362390
} catch (error) {
363391
captureServerException(error, {

0 commit comments

Comments
 (0)