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): always close server (#59248)

Joshua Snyder22618970e5f8
diff --git a/services/mcp/src/hono/streamable-handler.ts b/services/mcp/src/hono/streamable-handler.ts
index 8b15eadaa0e1..c4b262e32eee 100644
--- a/services/mcp/src/hono/streamable-handler.ts
+++ b/services/mcp/src/hono/streamable-handler.ts
@@ -53,17 +53,20 @@ export class StreamableMcpHandler {
return auth.error
}
+ let mcpServer: HonoMcpServer | undefined
try {
- const mcpServer = new HonoMcpServer(this.redis, auth.props, {
+ mcpServer = new HonoMcpServer(this.redis, auth.props, {
catalog: this.catalog,
resourceEntries: this._resourceEntries,
})
await mcpServer.init()
- const transport = new WebStandardStreamableHTTPServerTransport({})
+ const transport = new WebStandardStreamableHTTPServerTransport({ enableJsonResponse: true })
await mcpServer.server.connect(transport)
return passThrough(await transport.handleRequest(c.req.raw))
} catch (error) {
return handleCatchError(error, auth.props)
+ } finally {
+ await mcpServer?.server.close().catch(() => {})
}
}
}
diff --git a/services/mcp/src/resources/internals.ts b/services/mcp/src/resources/internals.ts
index 91f21f3f5f96..bf744f42f17d 100644
--- a/services/mcp/src/resources/internals.ts
+++ b/services/mcp/src/resources/internals.ts
@@ -3,8 +3,7 @@ import { type Unzipped, strFromU8, unzipSync } from 'fflate'
import type { ContextMillManifest, ContextMillResource } from './manifest-types'
import { loadContextMillManifest } from './manifest-loader'
-const CONTEXT_MILL_URL =
- 'https://github.com/PostHog/context-mill/releases/latest/download/skills-mcp-resources.zip'
+const CONTEXT_MILL_URL = 'https://github.com/PostHog/context-mill/releases/latest/download/skills-mcp-resources.zip'
let cachedResources: Unzipped | null = null
diff --git a/services/mcp/tests/unit/StateManager.test.ts b/services/mcp/tests/unit/StateManager.test.ts
index f876689a646a..5ddc76d1bf0d 100644
--- a/services/mcp/tests/unit/StateManager.test.ts
+++ b/services/mcp/tests/unit/StateManager.test.ts
@@ -481,7 +481,8 @@ describe('StateManager', () => {
it('should return undefined (not stale data) when fetch succeeds then later fails', async () => {
const mockGroupTypes = [{ group_type: 'company', group_type_index: 0 }]
- const getGroupTypes = vi.fn()
+ const getGroupTypes = vi
+ .fn()
.mockResolvedValueOnce(mockGroupTypes)
.mockRejectedValueOnce(new Error('API error'))
const mockApi = stateManager as any
diff --git a/services/mcp/tests/unit/api-client.test.ts b/services/mcp/tests/unit/api-client.test.ts
index 679b2fe946e6..33a84b674641 100644
--- a/services/mcp/tests/unit/api-client.test.ts
+++ b/services/mcp/tests/unit/api-client.test.ts
@@ -132,18 +132,13 @@ describe('ApiClient', () => {
[
'both ids set',
{ mcpSessionId: 'abc123session', mcpConversationId: '01984ad9-bda4-7000-8000-abcdef012345' },
- { 'x-posthog-mcp-session-id': 'abc123session', 'x-posthog-mcp-conversation-id': '01984ad9-bda4-7000-8000-abcdef012345' },
- ],
- [
- 'only session id set',
- { mcpSessionId: 'only-session' },
- { 'x-posthog-mcp-session-id': 'only-session' },
- ],
- [
- 'neither id set',
- {},
- {},
+ {
+ 'x-posthog-mcp-session-id': 'abc123session',
+ 'x-posthog-mcp-conversation-id': '01984ad9-bda4-7000-8000-abcdef012345',
+ },
],
+ ['only session id set', { mcpSessionId: 'only-session' }, { 'x-posthog-mcp-session-id': 'only-session' }],
+ ['neither id set', {}, {}],
] as const)('forwards mcp id headers — %s', async (_label, extraConfig, expectedHeaders) => {
const mockFetch = vi.fn().mockResolvedValue(new Response(JSON.stringify({}), { status: 200 }))
vi.stubGlobal('fetch', mockFetch)