: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(mcp): keep shared blob cache keys in one Redis Cluster slot (#97653)
Jake Ruth461b6043c595
diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex a87a29423ffc..6ab2272ec33f 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -5253,6 +5253,9 @@ importers:chokidar:specifier: ^3.6.0version: 3.6.0+ cluster-key-slot:+ specifier: ^1.1.2+ version: 1.1.2dotenv:specifier: ^16.4.7version: 16.4.7diff --git a/services/mcp/package.json b/services/mcp/package.jsonindex c7d581dff4fd..aa47aeb9920a 100644--- a/services/mcp/package.json+++ b/services/mcp/package.json@@ -87,6 +87,7 @@"ajv": "^8.20.0","ajv-formats": "^3.0.1","chokidar": "^3.6.0",+ "cluster-key-slot": "^1.1.2","dotenv": "^16.4.7","msw": "^2.14.6","oxfmt": "^0.33.0",diff --git a/services/mcp/src/hono/cache/SharedBlobCache.ts b/services/mcp/src/hono/cache/SharedBlobCache.tsindex baa9149517cd..08573e45e28e 100644--- a/services/mcp/src/hono/cache/SharedBlobCache.ts+++ b/services/mcp/src/hono/cache/SharedBlobCache.ts@@ -79,7 +79,9 @@ export class SharedBlobCache {opts: SharedBlobCacheOptions = {}) {// Separate layouts let old and new processes coexist during a rolling deploy.- const prefix = `mcp:shared-blob:${namespace}:v2`+ // The hash tag keeps the pointer, blob, and lock in one Redis Cluster slot, so+ // a script over two of them is accepted.+ const prefix = `mcp:shared-blob:{${namespace}}:v3`this.currentKey = `${prefix}:current`this.blobKeyPrefix = `${prefix}:blob`this.lockKey = `${prefix}:lock`diff --git a/services/mcp/tests/hono/context-mill-resource-cache.test.ts b/services/mcp/tests/hono/context-mill-resource-cache.test.tsindex f2ed54e865ba..0ad71a9f03a7 100644--- a/services/mcp/tests/hono/context-mill-resource-cache.test.ts+++ b/services/mcp/tests/hono/context-mill-resource-cache.test.ts@@ -74,8 +74,8 @@ function createMockRedis(): MockRedis {}}-const MANIFEST_CURRENT_KEY = 'mcp:shared-blob:context-mill:manifest:v2:current'-const MANIFEST_LOCK_KEY = 'mcp:shared-blob:context-mill:manifest:v2:lock'+const MANIFEST_CURRENT_KEY = 'mcp:shared-blob:{context-mill:manifest}:v3:current'+const MANIFEST_LOCK_KEY = 'mcp:shared-blob:{context-mill:manifest}:v3:lock'function bodyKey(uri: string): string {const hash = createHash('sha256').update(uri).digest('hex')diff --git a/services/mcp/tests/hono/helpers/shared-blob-redis-stubs.ts b/services/mcp/tests/hono/helpers/shared-blob-redis-stubs.tsindex b9fe5faa98a7..a0acdb88347a 100644--- a/services/mcp/tests/hono/helpers/shared-blob-redis-stubs.ts+++ b/services/mcp/tests/hono/helpers/shared-blob-redis-stubs.ts@@ -1,3 +1,4 @@+import calculateSlot from 'cluster-key-slot'import { vi } from 'vitest'import type { RedisLike } from '@/hono/cache/RedisCache'@@ -7,6 +8,7 @@ export function makeSharedBlobRedisStubs(store: Map<string, string>): Pick<Redisttl: vi.fn(async (key: string) => (store.has(key) ? 60 : -2)),expire: vi.fn(async (key: string) => (store.has(key) ? 1 : 0)),eval: vi.fn(async (_script: string, numberOfKeys: number, ...args: (string | number)[]) => {+ assertSingleSlot(args.slice(0, numberOfKeys).map(String))if (numberOfKeys === 1) {const [key, token] = args.map(String)return store.get(key!) === token ? Number(store.delete(key!)) : 0@@ -20,3 +22,10 @@ export function makeSharedBlobRedisStubs(store: Map<string, string>): Pick<Redis}),}}++// Redis Cluster rejects a multi-key command unless every key hashes to one slot.+function assertSingleSlot(keys: string[]): void {+ if (new Set(keys.map((key) => calculateSlot(key))).size > 1) {+ throw new Error("CROSSSLOT Keys in request don't hash to the same slot")+ }+}diff --git a/services/mcp/tests/hono/resource-catalog.test.ts b/services/mcp/tests/hono/resource-catalog.test.tsindex 51b1f189e093..4b6beac41b67 100644--- a/services/mcp/tests/hono/resource-catalog.test.ts+++ b/services/mcp/tests/hono/resource-catalog.test.ts@@ -97,7 +97,7 @@ function makeEntry(suffix: string): ContextMillResource {}}-const MANIFEST_CURRENT_KEY = 'mcp:shared-blob:context-mill:manifest:v2:current'+const MANIFEST_CURRENT_KEY = 'mcp:shared-blob:{context-mill:manifest}:v3:current'describe('ResourceCatalog', () => {let redis: MockRedisdiff --git a/services/mcp/tests/hono/shared-blob-cache.test.ts b/services/mcp/tests/hono/shared-blob-cache.test.tsindex 24e4c235ea40..bad9a30c93d7 100644--- a/services/mcp/tests/hono/shared-blob-cache.test.ts+++ b/services/mcp/tests/hono/shared-blob-cache.test.ts@@ -69,8 +69,8 @@ function createMockRedis(): MockRedis {}const NAMESPACE = 'test-blob'-const CURRENT_KEY = `mcp:shared-blob:${NAMESPACE}:v2:current`-const LOCK_KEY = `mcp:shared-blob:${NAMESPACE}:v2:lock`+const CURRENT_KEY = `mcp:shared-blob:{${NAMESPACE}}:v3:current`+const LOCK_KEY = `mcp:shared-blob:{${NAMESPACE}}:v3:lock`describe('SharedBlobCache', () => {let redis: MockRedisdiff --git a/services/mcp/tests/unit/skill-archive-cache.test.ts b/services/mcp/tests/unit/skill-archive-cache.test.tsindex 9b671b0dc85e..889eda787266 100644--- a/services/mcp/tests/unit/skill-archive-cache.test.ts+++ b/services/mcp/tests/unit/skill-archive-cache.test.ts@@ -46,15 +46,15 @@ function downloaded(bytes: Uint8Array = makeArchive(), etag?: string): SkillArchreturn { status: 'downloaded', bytes, etag }}-const CURRENT_KEY = 'mcp:shared-blob:product-skills:v2:current'-const LOCK_KEY = 'mcp:shared-blob:product-skills:v2:lock'+const CURRENT_KEY = 'mcp:shared-blob:{product-skills}:v3:current'+const LOCK_KEY = 'mcp:shared-blob:{product-skills}:v3:lock'function current(redis: MockRedis): { sha: string; etag?: string; validatedAt: number } {return JSON.parse(redis.store.get(CURRENT_KEY)!)}function bytesKey(redis: MockRedis): string {- return `mcp:shared-blob:product-skills:v2:blob:${current(redis).sha}`+ return `mcp:shared-blob:{product-skills}:v3:blob:${current(redis).sha}`}function expireSharedCopy(redis: MockRedis): void {