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.

chore(phrocs): shrink MCP log payloads to cut agent token usage (#71428)

Sam Penningtondc82fac743f4
diff --git a/tools/phrocs/mcp_server.py b/tools/phrocs/mcp_server.py
index 409759e42922..13f0b202d2b2 100755
--- a/tools/phrocs/mcp_server.py
+++ b/tools/phrocs/mcp_server.py
@@ -12,6 +12,7 @@ Run via Claude Code's .mcp.json (invoked automatically by the MCP client):
from __future__ import annotations
import os
+import re
import json
import socket
import hashlib
@@ -60,6 +61,29 @@ def _query_phrocs(cmd: dict) -> dict | None:
_NOT_RUNNING = {"error": "phrocs is not running. Start the dev environment with: ./bin/start"}
+# Log lines come from a VT emulator render: they can carry ANSI escapes and
+# screen-width padding (the emulator hard-wraps at terminal width, so length
+# is already bounded). Clean them so MCP clients don't pay tokens for noise.
+_ANSI_RE = re.compile(r"\x1b\[[0-?]*[ -/]*[@-~]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)")
+
+
+def _clean_log_lines(lines: list[str]) -> list[str]:
+ return _collapse_repeats([_ANSI_RE.sub("", line).rstrip() for line in lines])
+
+
+def _collapse_repeats(lines: list[str]) -> list[str]:
+ out: list[str] = []
+ i = 0
+ while i < len(lines):
+ j = i + 1
+ while j < len(lines) and lines[j] == lines[i]:
+ j += 1
+ out.append(lines[i])
+ if j - i > 1:
+ out.append(f"[repeated x{j - i}]")
+ i = j
+ return out
+
@mcp.tool()
def get_process_status(process: str = "") -> dict[str, Any]:
@@ -91,6 +115,8 @@ def get_process_status(process: str = "") -> dict[str, Any]:
def get_process_logs(process: str, lines: int = 100, grep: str = "") -> dict[str, Any]:
"""Get recent log output from a dev environment process.
Reads from phrocs' in-memory scrollback buffer (10,000 lines per process).
+ Prefer a grep pattern over a large line count when hunting for something
+ specific — it filters server-side across the whole buffer.
Args:
process: Process name (e.g. 'backend', 'frontend', 'celery-worker').
lines: Number of recent lines to return (default 100, max 500).
@@ -107,7 +133,7 @@ def get_process_logs(process: str, lines: int = 100, grep: str = "") -> dict[str
resp: dict[str, Any] = {
"process": process,
"returned_lines": len(result["lines"]),
- "logs": result["lines"],
+ "logs": "\n".join(_clean_log_lines(result["lines"])),
}
if grep:
resp["grep"] = grep