Skip to content

Commit 34431ec

Browse files
authored
feat: tell MCP which porject it should use (#784)
### TL;DR Added PostHog project context to the system prompt for agent sessions. ### What changed? - Created a new `buildPostHogSystemPrompt` method that generates a system prompt with PostHog project context - Added the PostHog system prompt to both new sessions and resumed sessions - Added support for a `POSTHOG_MCP_URL` environment variable to override the MCP URL - Modified the logic for determining the PostHog MCP URL to prioritize the environment variable ### How to test? 1. Set the `POSTHOG_MCP_URL` environment variable to test the override functionality 2. Start a new agent session and verify that the PostHog project context is included in the system prompt 3. Resume an existing session and confirm the context is maintained ### Why make this change? This change ensures that the agent has proper context about which PostHog project it should operate on. By including the project ID and API host in the system prompt, the agent can make more accurate decisions when using PostHog MCP tools, preventing accidental cross-project operations.
1 parent 92f47ed commit 34431ec

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

apps/twig/src/main/services/agent/service.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,20 @@ export class AgentService extends TypedEventEmitter<AgentServiceEvents> {
347347
return servers;
348348
}
349349

350+
private buildPostHogSystemPrompt(credentials: Credentials): {
351+
append: string;
352+
} {
353+
return {
354+
append: `PostHog context: use project ${credentials.projectId} on ${credentials.apiHost}. When using PostHog MCP tools, operate only on this project.`,
355+
};
356+
}
357+
350358
private getPostHogMcpUrl(apiHost: string): string {
351-
if (
352-
apiHost.includes("localhost") ||
353-
apiHost.includes("127.0.0.1") ||
354-
!app.isPackaged
355-
) {
359+
const overrideUrl = process.env.POSTHOG_MCP_URL;
360+
if (overrideUrl) {
361+
return overrideUrl;
362+
}
363+
if (apiHost.includes("localhost") || apiHost.includes("127.0.0.1")) {
356364
return "http://localhost:8787/mcp";
357365
}
358366
return "https://mcp.posthog.com/mcp";
@@ -475,6 +483,7 @@ export class AgentService extends TypedEventEmitter<AgentServiceEvents> {
475483
let currentModelId: string | undefined;
476484

477485
if (isReconnect) {
486+
const systemPrompt = this.buildPostHogSystemPrompt(credentials);
478487
const resumeResponse = await connection.extMethod(
479488
"_posthog/session/resume",
480489
{
@@ -486,6 +495,7 @@ export class AgentService extends TypedEventEmitter<AgentServiceEvents> {
486495
persistence: { taskId, runId: taskRunId, logUrl },
487496
}),
488497
...(sdkSessionId && { sdkSessionId }),
498+
systemPrompt,
489499
...(additionalDirectories?.length && {
490500
claudeCode: {
491501
options: { additionalDirectories },
@@ -505,12 +515,14 @@ export class AgentService extends TypedEventEmitter<AgentServiceEvents> {
505515
availableModels = resumeMeta?.models?.availableModels;
506516
currentModelId = resumeMeta?.models?.currentModelId;
507517
} else {
518+
const systemPrompt = this.buildPostHogSystemPrompt(credentials);
508519
const newSessionResponse = await connection.newSession({
509520
cwd: repoPath,
510521
mcpServers,
511522
_meta: {
512523
sessionId: taskRunId,
513524
model,
525+
systemPrompt,
514526
...(executionMode && { initialModeId: executionMode }),
515527
...(additionalDirectories?.length && {
516528
claudeCode: {

packages/agent/src/adapters/claude/claude-agent.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ export class ClaudeAcpAgent extends BaseAcpAgent {
207207
cwd: params.cwd,
208208
permissionMode: "default",
209209
mcpServers,
210+
systemPrompt: buildSystemPrompt(meta?.systemPrompt),
210211
userProvidedOptions: meta?.claudeCode?.options,
211212
sdkSessionId: meta?.sdkSessionId,
212213
additionalDirectories: meta?.claudeCode?.options?.additionalDirectories,

0 commit comments

Comments
 (0)