: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
chore(mcp): debug logs (#70280)
Georgiy Tarasov8823c9ed8b9f
diff --git a/services/mcp/src/hono/app.ts b/services/mcp/src/hono/app.tsindex 8262572f307b..95ea4042880e 100644--- a/services/mcp/src/hono/app.ts+++ b/services/mcp/src/hono/app.ts@@ -1,10 +1,11 @@import { Hono } from 'hono'+import { env } from '@/lib/env'import { loadSigningKeyFromEnv, NonceLedger, SignedStateCodec } from '@/lib/signed-state'import { setConfirmedActionRuntime } from '@/tools/confirmed-action-registry'import { confirmedActionRuntimeInstalled } from './metrics'-import { httpMetrics, securityHeaders } from './middleware'+import { devRequestLogger, httpMetrics, securityHeaders } from './middleware'import { registerPublicRoutes } from './public-routes'import { StreamableMcpHandler } from './streamable-handler'import type { HonoCtx, RedisWithPing } from './types'@@ -54,6 +55,11 @@ export function createApp(redis: RedisWithPing): App {)}+ // Positive allowlist so the wire tap fails closed when NODE_ENV is unset.+ if (env.NODE_ENV === 'development') {+ app.use('*', devRequestLogger)+ }+app.use('*', securityHeaders)app.use('*', httpMetrics)diff --git a/services/mcp/src/hono/middleware.ts b/services/mcp/src/hono/middleware.tsindex dbcb7d06ebd4..dc1403ca86a4 100644--- a/services/mcp/src/hono/middleware.ts+++ b/services/mcp/src/hono/middleware.ts@@ -2,6 +2,36 @@ import { type MiddlewareHandler } from 'hono'import { httpRequestDurationSeconds, httpRequestsTotal, inflightRequests, routeLabel } from './metrics'+const DEV_LOG_VISIBLE_HEADERS = new Set([+ 'accept',+ 'content-length',+ 'content-type',+ 'mcp-protocol-version',+ 'user-agent',+])++export const devRequestLogger: MiddlewareHandler = async (c, next) => {+ const headers: Record<string, string> = {}+ c.req.raw.headers.forEach((value, key) => {+ headers[key] = DEV_LOG_VISIBLE_HEADERS.has(key.toLowerCase()) ? value : '[REDACTED]'+ })++ const url = new URL(c.req.url)+ console.info('[MCP dev]', {+ method: c.req.method,+ pathname: url.pathname,+ queryParameters: [...new Set(url.searchParams.keys())],+ headers,+ body: {+ present: c.req.raw.body !== null,+ contentLength: c.req.header('content-length') ?? 'unknown',+ contentType: c.req.header('content-type') ?? 'unknown',+ },+ })++ await next()+}+export const securityHeaders: MiddlewareHandler = async (c, next) => {await next()c.header('X-Content-Type-Options', 'nosniff')diff --git a/services/mcp/tests/hono/dev-request-logger.test.ts b/services/mcp/tests/hono/dev-request-logger.test.tsnew file mode 100644index 000000000000..865329705e38--- /dev/null+++ b/services/mcp/tests/hono/dev-request-logger.test.ts@@ -0,0 +1,49 @@+import { Hono } from 'hono'+import { afterEach, describe, expect, it, vi } from 'vitest'++import { devRequestLogger } from '@/hono/middleware'++describe('devRequestLogger', () => {+ afterEach(() => {+ vi.restoreAllMocks()+ })++ it('logs request metadata without exposing credentials or consuming the body', async () => {+ const consoleInfo = vi.spyOn(console, 'info').mockImplementation(() => undefined)+ const app = new Hono()+ app.use('*', devRequestLogger)+ app.post('/mcp', async (c) => c.json(await c.req.json()))++ const response = await app.request('/mcp?token=query-secret&project=123', {+ method: 'POST',+ headers: {+ accept: 'application/json',+ authorization: 'Bearer header-secret',+ 'content-type': 'application/json',+ 'x-csrftoken': 'csrf-secret',+ 'x-posthog-session-id': 'session-secret',+ 'x-tool-credential': 'tool-secret',+ },+ body: JSON.stringify({ password: 'body-secret' }),+ })++ expect(await response.json()).toEqual({ password: 'body-secret' })+ expect(consoleInfo).toHaveBeenCalledOnce()++ const logOutput = JSON.stringify(consoleInfo.mock.calls[0])+ expect(logOutput).toContain('"pathname":"/mcp"')+ expect(logOutput).toContain('"queryParameters":["token","project"]')+ expect(logOutput).toContain('"accept":"application/json"')+ expect(logOutput).toContain('"present":true')+ expect(logOutput).toContain('"contentType":"application/json"')+ expect(logOutput).toContain('"x-csrftoken":"[REDACTED]"')+ expect(logOutput).toContain('"x-posthog-session-id":"[REDACTED]"')+ expect(logOutput).toContain('"x-tool-credential":"[REDACTED]"')+ expect(logOutput).not.toContain('query-secret')+ expect(logOutput).not.toContain('header-secret')+ expect(logOutput).not.toContain('csrf-secret')+ expect(logOutput).not.toContain('session-secret')+ expect(logOutput).not.toContain('tool-secret')+ expect(logOutput).not.toContain('body-secret')+ })+})