Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(mcp): log redacted token on rate-limit block (#61816)

Joshua Snyder2ad4ecb7b937
diff --git a/services/mcp/src/hono/rate-limit-telemetry.ts b/services/mcp/src/hono/rate-limit-telemetry.ts
index 480d26344c17..c3f0f57cb927 100644
--- a/services/mcp/src/hono/rate-limit-telemetry.ts
+++ b/services/mcp/src/hono/rate-limit-telemetry.ts
@@ -1,4 +1,5 @@
import type { RequestProperties } from '@/lib/request-properties'
+import { redactToken } from '@/lib/utils'
import type { State } from '@/tools/types'
import { RedisCache, type RedisLike } from './cache/RedisCache'
@@ -44,6 +45,13 @@ export async function recordRateLimitBlock(
}
}
rateLimitBlockedByTeam.inc({ scope: result.scope, team_id: normalizeTeamId(teamId) })
+
+ // Log so we can trace which token is hitting the limit; the token is redacted
+ // to its last 4 chars so the line is useless to anyone who shouldn't have it.
+ console.warn(
+ '[RateLimiter] rate limited',
+ JSON.stringify({ scope: result.scope, projectId: teamId ?? null, token: redactToken(props.apiToken) })
+ )
}
// test-only: reset the process-lifetime cardinality cap between cases
diff --git a/services/mcp/src/lib/utils.ts b/services/mcp/src/lib/utils.ts
index 523f9448f9ad..2c13d92191a3 100644
--- a/services/mcp/src/lib/utils.ts
+++ b/services/mcp/src/lib/utils.ts
@@ -7,6 +7,15 @@ export function hash(data: string): string {
return crypto.pbkdf2Sync(data, salt, 100000, 32, 'sha256').toString('hex')
}
+// Redact an API token for logs: keep only the last 4 chars, mask the rest.
+// Tokens of 4 chars or fewer (or empty) are fully masked so nothing useful leaks.
+export function redactToken(token: string): string {
+ if (token.length <= 4) {
+ return '****'
+ }
+ return `****${token.slice(-4)}`
+}
+
export function formatPrompt(template: string, vars: Record<string, string>): string {
// Use a function replacement so `$` sequences (`$&`, `$$`, `` $` ``, `$'`) inside a
// value are NOT interpreted as replacement-pattern escapes. Otherwise values containing
diff --git a/services/mcp/tests/hono/rate-limit-telemetry.test.ts b/services/mcp/tests/hono/rate-limit-telemetry.test.ts
index 93fc65996a78..df3a79a76b75 100644
--- a/services/mcp/tests/hono/rate-limit-telemetry.test.ts
+++ b/services/mcp/tests/hono/rate-limit-telemetry.test.ts
@@ -56,6 +56,21 @@ describe('recordRateLimitBlock', () => {
expect(await teamValue('unresolved')).toBe(1)
})
+ it('logs the block with the project id and a token redacted to its last 4 chars', async () => {
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
+ const get = vi.fn()
+ await recordRateLimitBlock(
+ { get } as never,
+ makeProps({ projectId: '123', apiToken: 'phx_secrettoken9876' }),
+ blocked('mcp_burst')
+ )
+ expect(warn).toHaveBeenCalledWith(
+ '[RateLimiter] rate limited',
+ JSON.stringify({ scope: 'mcp_burst', projectId: '123', token: '****9876' })
+ )
+ warn.mockRestore()
+ })
+
it.each([['not-a-number'], ['12345678901234567890'], ['1; DROP']])(
'buckets a non-numeric or oversized project id %j as unresolved',
async (projectId) => {
diff --git a/services/mcp/tests/unit/utils.test.ts b/services/mcp/tests/unit/utils.test.ts
index 912ccb133b10..a01da1017d0a 100644
--- a/services/mcp/tests/unit/utils.test.ts
+++ b/services/mcp/tests/unit/utils.test.ts
@@ -1,10 +1,25 @@
import { describe, expect, it } from 'vitest'
-import { formatPrompt, sanitizeHeaderValue } from '@/lib/utils'
+import { formatPrompt, redactToken, sanitizeHeaderValue } from '@/lib/utils'
import { omitResponseFields, pickResponseFields, withPostHogUrl } from '@/tools/tool-utils'
import type { Context } from '@/tools/types'
describe('utils', () => {
+ describe('redactToken', () => {
+ it('keeps only the last 4 chars and masks the rest', () => {
+ expect(redactToken('phx_abcdefgh1234')).toBe('****1234')
+ })
+
+ it('fully masks tokens of 4 chars or fewer', () => {
+ expect(redactToken('1234')).toBe('****')
+ expect(redactToken('ab')).toBe('****')
+ })
+
+ it('fully masks an empty token', () => {
+ expect(redactToken('')).toBe('****')
+ })
+ })
+
describe('formatPrompt', () => {
it('substitutes placeholders with values', () => {
expect(formatPrompt('Hello {name}, welcome to {place}', { name: 'world', place: 'earth' })).toBe(