Skip to content

feat(agent): guide cloud agents to use the PostHog memory MCP tool#2599

Draft
andrewm4894 wants to merge 2 commits into
mainfrom
posthog-code/use-memory-mcp-tool-in-cloud-runs
Draft

feat(agent): guide cloud agents to use the PostHog memory MCP tool#2599
andrewm4894 wants to merge 2 commits into
mainfrom
posthog-code/use-memory-mcp-tool-in-cloud-runs

Conversation

@andrewm4894

Copy link
Copy Markdown
Member

Problem

Claude-style agent memories produced during PostHog Code cloud runs went nowhere — there was no persistent, shared memory store wired into cloud agents, so they couldn't recall durable context across runs or carry learnings forward.

Changes

Appends a short Persistent Memory section to every cloud-run system prompt (buildSessionSystemPrompt → applied across all buildCloudSystemPrompt branches) telling cloud agents to use the PostHog memory MCP tool to recall durable facts before non-trivial work and to store new learnings pre-emptively.

The memory MCP tool is added server-side in PostHog/posthog (PostHog/posthog#62875), backed by the same AgentMemory / document_embeddings store Max/PostHog AI uses. No tool-allowlist change is needed here: cloud agents forward every PostHog MCP tool verbatim (newSession({ mcpServers })) under bypass-permissions, so the tool becomes available automatically — this PR only encourages its use.

How did you test this?

I'm an agent, so I did not manually run the app. Automated checks I actually ran:

  • pnpm --filter @posthog/agent typecheck — clean (after building workspace deps).
  • The existing agent-server.test.ts cloud-prompt assertions use toContain, which this additive change preserves; that suite could not execute in my sandbox because its git-fixture setup calls git commit, which is blocked in this environment (the failure reproduces on unmodified main).

Automatic notifications

  • Publish to changelog?
  • Alert Sales and Marketing teams?

Created with PostHog Code from a Slack thread

Appends persistent-memory guidance to every cloud-run system prompt so cloud agents recall durable context across runs and persist new learnings via the PostHog `memory` MCP tool — the shared, semantically-searchable memory store used by Max/PostHog AI.

The tool itself is added server-side in PostHog/posthog (the cloud agent already forwards every PostHog MCP tool, so no allowlist change is needed here).

Generated-By: PostHog Code
Task-Id: b2ae2f32-aa94-4532-b069-b03875e73a58
@github-actions

github-actions Bot commented Jun 11, 2026

Copy link
Copy Markdown

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit cb84a33.

@greptile-apps

greptile-apps Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
packages/agent/src/server/agent-server.ts:1704-1714
**Memory instructions reach Codex cloud runners too**

`buildMemoryInstructions()` is appended unconditionally inside `buildSessionSystemPrompt`, and `buildCodexInstructions` (line 1651) re-uses that same output verbatim as the `instructions` field for Codex runs. The `memory` MCP tool is listed in `mcpServers` passed to `newSession` for both runtimes, but whether the Codex path actually negotiates and exposes MCP tools to the model is worth confirming — if it does not, Codex agents will reference a tool they cannot call. This is not a blocker given the architectural note in the PR description, but a quick check or comment clarifying that Codex also has `memory` available would make this assumption explicit.

Reviews (1): Last reviewed commit: "feat(agent): guide cloud agents to use t..." | Re-trigger Greptile

Comment on lines +1704 to +1714
private buildMemoryInstructions(): string {
return `
# Persistent Memory
You have access to PostHog's persistent memory through the \`memory\` MCP tool — a shared,
semantically-searchable store of durable facts about this user, their company, product, and
preferences (the same memories used across PostHog AI). Memories persist across runs, so:
- Before non-trivial work, recall context with the \`memory\` tool using \`action: "query"\` (semantic search) to surface durable facts, decisions, and prior discussions.
- When you learn something durable — product setup, conventions, definitions, preferences, decisions — store it with \`action: "create"\`. Do this pre-emptively; you don't need to be asked. Query first to avoid duplicates.
- Use \`action: "update"\`/\`"delete"\` to keep memories accurate, and \`action: "list_metadata_keys"\` to keep metadata tags consistent.
- Memories are for durable facts, not transient task state. New memories become searchable once their embedding is processed.`;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Memory instructions reach Codex cloud runners too

buildMemoryInstructions() is appended unconditionally inside buildSessionSystemPrompt, and buildCodexInstructions (line 1651) re-uses that same output verbatim as the instructions field for Codex runs. The memory MCP tool is listed in mcpServers passed to newSession for both runtimes, but whether the Codex path actually negotiates and exposes MCP tools to the model is worth confirming — if it does not, Codex agents will reference a tool they cannot call. This is not a blocker given the architectural note in the PR description, but a quick check or comment clarifying that Codex also has memory available would make this assumption explicit.

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/agent/src/server/agent-server.ts
Line: 1704-1714

Comment:
**Memory instructions reach Codex cloud runners too**

`buildMemoryInstructions()` is appended unconditionally inside `buildSessionSystemPrompt`, and `buildCodexInstructions` (line 1651) re-uses that same output verbatim as the `instructions` field for Codex runs. The `memory` MCP tool is listed in `mcpServers` passed to `newSession` for both runtimes, but whether the Codex path actually negotiates and exposes MCP tools to the model is worth confirming — if it does not, Codex agents will reference a tool they cannot call. This is not a blocker given the architectural note in the PR description, but a quick check or comment clarifying that Codex also has `memory` available would make this assumption explicit.

How can I resolve this? If you propose a fix, please make it concise.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Clarifies that the memory MCP tool guidance correctly applies to both Claude and Codex cloud runs: newSession passes the PostHog MCP server via mcpServers for both runtimes, and the Codex adapter forwards those servers to the Codex CLI, so buildCodexInstructions reusing this text is intentional. Addresses a P2 review note from greptile-apps.

Generated-By: PostHog Code
Task-Id: b2ae2f32-aa94-4532-b069-b03875e73a58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants