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.

fix(mcp): run the mcp server on node 24.22.1 with a fixed undici (#122294)

Tom Piccirello3e621108875c
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index bff368363ca4..7a356073008a 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -5878,6 +5878,9 @@ importers:
prosemirror-transform:
specifier: ^1.10.4
version: 1.10.4
+ undici:
+ specifier: ^8.12.2
+ version: 8.12.2
uuid:
specifier: ^11.1.1
version: 11.1.1
@@ -23221,6 +23224,10 @@ packages:
resolution: {integrity: sha512-YQ3WlbqjYMmNpdvDH64jAgLjxuAR9+649calDWhbshYaeQGO2bR4nI94ORJmwI3J9YhoKQnpyGOK+0zlWS5N5Q==}
engines: {node: '>=22.19.0'}
+ resolution: {integrity: sha512-XtuDWVdqTLr3NAxm2ly9/hU6kUsJOgGh1RASzMBbsw6aWCOaQzHdW+QQDfdERjfJ2V5vL5pRPa4RetO9UfivDg==}
+ engines: {node: '>=22.19.0'}
+
resolution: {integrity: sha512-i7qRCmY42zmCwnYlh9H2SvLEypEFGye5iRmEMKjcGi7zk9UquigRjFtTLz0TYqr0ZGLZhaMHl/foy1bZR+Cwlw==}
@@ -46062,6 +46069,8 @@ snapshots:
+
dependencies:
pathe: 2.0.3
diff --git a/services/mcp/Dockerfile b/services/mcp/Dockerfile
index 94267b319674..70b6c14de477 100644
--- a/services/mcp/Dockerfile
+++ b/services/mcp/Dockerfile
@@ -8,8 +8,8 @@
# The legacy `Dockerfile.proxy` in this directory is for the `mcp-remote` CLI
# proxy distribution, not the server — different artifact, don't confuse them.
-# Held below .nvmrc: Node 24.22.1's bundled undici crashes the server on assert(!this.paused).
-ARG NODE_VERSION=24.13.0
+# Pin the same Node version as the rest of the monorepo (.nvmrc).
+ARG NODE_VERSION=24.22.1
ARG NODE_IMAGE=node:${NODE_VERSION}-bookworm-slim
#
@@ -70,13 +70,6 @@ WORKDIR /code
ARG COMMIT_HASH
RUN echo "${COMMIT_HASH:-unknown}" > /code/commit.txt
-# Node 24.13.0's bundled CA store still trusts the compromised eMudhra roots.
-# --use-openssl-ca in CMD makes Node read this Debian bundle instead, which
-# ca-certificates 20250419~deb12u2 and later keep current.
-RUN apt-get update && \
- apt-get install -y --no-install-recommends ca-certificates && \
- rm -rf /var/lib/apt/lists/*
-
# Bundle is fully self-contained — ioredis and everything else is inlined.
COPY --from=build --chown=node:node /code/services/mcp/dist/hono-server.mjs ./hono-server.mjs
COPY --from=build --chown=node:node /code/services/mcp/dist/hono-server.mjs.map ./hono-server.mjs.map
@@ -95,6 +88,4 @@ EXPOSE 3001
USER node
-# The flag lives here, not in NODE_OPTIONS, because the deploy chart sets
-# NODE_OPTIONS and would replace it.
-CMD ["node", "--use-openssl-ca", "hono-server.mjs"]
+CMD ["node", "hono-server.mjs"]
diff --git a/services/mcp/package.json b/services/mcp/package.json
index b569978e1713..a7197cc4cc36 100644
--- a/services/mcp/package.json
+++ b/services/mcp/package.json
@@ -70,6 +70,7 @@
"prosemirror-collab": "^1.3.1",
"prosemirror-model": "^1.25.2",
"prosemirror-transform": "^1.10.4",
+ "undici": "^8.12.2",
"uuid": "^11.1.1",
"yaml": "^2.9.0",
"zod": "^4.3.6"
diff --git a/services/mcp/src/hono/http-dispatcher.ts b/services/mcp/src/hono/http-dispatcher.ts
new file mode 100644
index 000000000000..a6eed025f449
--- /dev/null
+++ b/services/mcp/src/hono/http-dispatcher.ts
@@ -0,0 +1,17 @@
+import { Agent, setGlobalDispatcher } from 'undici'
+
+/**
+ * Routes global `fetch` through this package's undici instead of the copy bundled with Node.
+ *
+ * Node 24.22.1 bundles undici 7.30.1. When a `Connection: close` response ends while backpressure
+ * has paused the HTTP/1 parser, that version throws `assert(!this.paused)` from the socket 'end'
+ * handler, and the throw exits the process (nodejs/undici#5360). undici 8.6.0 and later finish
+ * the paused parser instead. Remove this module when the Node release in services/mcp/Dockerfile
+ * bundles an undici 7.x that contains nodejs/undici#5553.
+ *
+ * `allowH2: false` keeps HTTP/1.1, the protocol the bundled dispatcher uses, because undici 8
+ * offers HTTP/2 over ALPN by default.
+ */
+export function installHttpDispatcher(): void {
+ setGlobalDispatcher(new Agent({ allowH2: false }))
+}
diff --git a/services/mcp/src/hono/index.ts b/services/mcp/src/hono/index.ts
index 7ffab8929020..a907ce074255 100644
--- a/services/mcp/src/hono/index.ts
+++ b/services/mcp/src/hono/index.ts
@@ -4,10 +4,13 @@ import Redis from 'ioredis'
import { getCustomApiBaseUrl, isLocalApi } from '@/lib/constants'
import { createApp } from './app'
+import { installHttpDispatcher } from './http-dispatcher'
import { redisOperationsTotal } from './metrics'
import { connectRedis } from './redis-connection'
import { registerShutdownHandlers } from './shutdown'
+installHttpDispatcher()
+
const PORT = parseInt(process.env.PORT || '3001', 10)
const HOST = process.env.HOST || '0.0.0.0'
diff --git a/services/mcp/tests/hono/api-client-connection-close.test.ts b/services/mcp/tests/hono/api-client-connection-close.test.ts
new file mode 100644
index 000000000000..c7c7a7791e83
--- /dev/null
+++ b/services/mcp/tests/hono/api-client-connection-close.test.ts
@@ -0,0 +1,73 @@
+import { createHash } from 'node:crypto'
+import { once } from 'node:events'
+import { type Server, createServer } from 'node:http'
+import type { AddressInfo } from 'node:net'
+import { gzipSync } from 'node:zlib'
+import { afterAll, beforeAll, describe, expect, it } from 'vitest'
+
+import { ApiClient } from '@/api/client'
+import { installHttpDispatcher } from '@/hono/http-dispatcher'
+
+// Django gzips POST /query/ responses, and a server that closes each connection sends FIN right
+// after the body. Node 24.22.1's bundled undici paused its HTTP/1 parser while the gzip decoder
+// caught up, then threw assert(!this.paused) from the socket 'end' handler and exited the process
+// (nodejs/undici#5360).
+// The body must stay large and hard to compress, because a small body never pauses the parser.
+const queryResult = {
+ results: Array.from({ length: 16_384 }, (_, i) => [createHash('sha256').update(String(i)).digest('hex')]),
+}
+const gzippedQueryResult = gzipSync(JSON.stringify(queryResult))
+
+describe('ApiClient against a server that closes every connection', () => {
+ let server: Server
+ let client: ApiClient
+ let connectionClosed: Promise<unknown>
+
+ beforeAll(async () => {
+ installHttpDispatcher()
+ server = createServer((_req, res) => {
+ res.writeHead(200, {
+ 'Content-Type': 'application/json',
+ 'Content-Encoding': 'gzip',
+ 'Content-Length': gzippedQueryResult.length,
+ Connection: 'close',
+ })
+ res.end(gzippedQueryResult)
+ })
+ server.on('connection', (socket) => {
+ connectionClosed = once(socket, 'close')
+ })
+ await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve))
+ const { port } = server.address() as AddressInfo
+ client = new ApiClient({ apiToken: 'test-token', baseUrl: `http://127.0.0.1:${port}` })
+ })
+
+ afterAll(async () => {
+ await new Promise((resolve) => server.close(resolve))
+ })
+
+ it('reads a large gzip query result without an uncaught exception', { timeout: 5000 }, async () => {
+ // In production an uncaught exception exits the process. Vitest only reports it, and the
+ // throw can come after the body is complete, so the test records it to fail on it.
+ const uncaught: unknown[] = []
+ const recordUncaught = (error: unknown): void => {
+ uncaught.push(error)
+ }
+ process.on('uncaughtException', recordUncaught)
+ try {
+ const response = await client.request({
+ method: 'POST',
+ path: '/api/projects/1/query/',
+ body: { query: { kind: 'HogQLQuery', query: 'select 1' } },
+ })
+ // The client runs its socket 'end' handler on a tick before the server can see the
+ // connection close, so after this wait a throw from that handler is in `uncaught`.
+ await connectionClosed
+
+ expect(response).toEqual(queryResult)
+ expect(uncaught).toEqual([])
+ } finally {
+ process.off('uncaughtException', recordUncaught)
+ }
+ })
+})