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): include sql guidelines (#61891)

Georgiy Tarasov28d2683b2bcc
diff --git a/services/mcp/scripts/build-hono.ts b/services/mcp/scripts/build-hono.ts
index 50257edb321d..301903be408d 100644
--- a/services/mcp/scripts/build-hono.ts
+++ b/services/mcp/scripts/build-hono.ts
@@ -1,7 +1,11 @@
import { build } from 'esbuild'
+import { copyInstructions } from './copy-instructions'
import { honoEsbuildOptions, honoOutfile } from './hono-esbuild-config'
+// Populate `shared/guidelines.md` so esbuild can inline it via `@shared/*`.
+copyInstructions()
+
build(honoEsbuildOptions())
.then(() => {
console.info(`Built MCP server → ${honoOutfile}`)
diff --git a/services/mcp/scripts/copy-instructions.ts b/services/mcp/scripts/copy-instructions.ts
index 9d852875ccc5..09a5338002e4 100644
--- a/services/mcp/scripts/copy-instructions.ts
+++ b/services/mcp/scripts/copy-instructions.ts
@@ -1,6 +1,8 @@
#!/usr/bin/env tsx
/**
- * Copies shared prompt files.
+ * Copies shared prompt files into `shared/` (gitignored) so esbuild can inline
+ * them via the `@shared/*` tsconfig alias at bundle time. Called from
+ * build-hono.ts / dev-hono.ts before bundling, and standalone in CI.
*/
import { cpSync, mkdirSync } from 'fs'
import { dirname, resolve } from 'path'
@@ -15,10 +17,17 @@ const PROMPTS = [
},
]
-for (const prompt of PROMPTS) {
- const src = resolve(REPO_ROOT, prompt.src)
- const dest = resolve(ROOT_DIR, prompt.dest)
- mkdirSync(dirname(dest), { recursive: true })
- // `force: true` so watch-mode rebuilds don't EEXIST when the dest already exists.
- cpSync(src, dest, { recursive: true, force: true })
+export function copyInstructions(): void {
+ for (const prompt of PROMPTS) {
+ const src = resolve(REPO_ROOT, prompt.src)
+ const dest = resolve(ROOT_DIR, prompt.dest)
+ mkdirSync(dirname(dest), { recursive: true })
+ // `force: true` so watch-mode rebuilds don't EEXIST when the dest already exists.
+ cpSync(src, dest, { recursive: true, force: true })
+ }
+}
+
+// Standalone execution: `tsx scripts/copy-instructions.ts` (used in CI).
+if (require.main === module) {
+ copyInstructions()
}
diff --git a/services/mcp/scripts/dev-hono.ts b/services/mcp/scripts/dev-hono.ts
index bd19438905e0..34ed961606fd 100644
--- a/services/mcp/scripts/dev-hono.ts
+++ b/services/mcp/scripts/dev-hono.ts
@@ -6,8 +6,12 @@ import { context, type Plugin } from 'esbuild'
import { existsSync } from 'fs'
import { resolve } from 'path'
+import { copyInstructions } from './copy-instructions'
import { honoEsbuildOptions, honoOutfile } from './hono-esbuild-config'
+// Populate `shared/guidelines.md` so esbuild can inline it via `@shared/*`.
+copyInstructions()
+
if (existsSync(resolve(process.cwd(), '.env'))) {
process.loadEnvFile(resolve(process.cwd(), '.env'))
}
diff --git a/services/mcp/src/hono/dispatcher.ts b/services/mcp/src/hono/dispatcher.ts
index 880b933175a4..d1bf5af20655 100644
--- a/services/mcp/src/hono/dispatcher.ts
+++ b/services/mcp/src/hono/dispatcher.ts
@@ -17,6 +17,7 @@ import type {
PingRequest,
ReadResourceRequest,
} from '@modelcontextprotocol/sdk/types.js'
+import GUIDELINES from '@shared/guidelines.md'
import { randomUUID } from 'node:crypto'
import { MCP_SERVER_NAME, MCP_SERVER_VERSION } from '@/lib/constants'
@@ -35,24 +36,6 @@ import { ToolExecutor } from './tool-executor'
export { McpDispatcher }
export type { ResolvedState } from './request-state-resolver'
-function loadGuidelines(): string {
- try {
- // eslint-disable-next-line @typescript-eslint/no-require-imports
- const mod = require('@shared/guidelines.md')
- return typeof mod === 'string' ? mod : (mod?.default ?? '')
- } catch {
- // @shared alias only resolves in the esbuild production bundle.
- // Fall back to reading from disk (works in Vitest/test contexts).
- }
- try {
- const fs = require('node:fs')
- const path = require('node:path')
- return fs.readFileSync(path.resolve(process.cwd(), 'shared/guidelines.md'), 'utf-8')
- } catch {
- return ''
- }
-}
-
const MAX_BATCH_SIZE = 100
const MAX_BODY_BYTES = 1_048_576
@@ -115,7 +98,7 @@ class McpDispatcher {
this.catalog = catalog
this.resourceCatalog = new ResourceCatalog(env, redis)
this.stateResolver = new RequestStateResolver(catalog, redis, env)
- this.instructionsBuilder = new InstructionsBuilder(loadGuidelines())
+ this.instructionsBuilder = new InstructionsBuilder(GUIDELINES)
this.toolExecutor = new ToolExecutor(catalog, this.instructionsBuilder)
}