Skip to content

Commit 5fbf7ed

Browse files
committed
🤖 feat: add MCP servers section to system prompt
Shows configured servers (name + command) when at least one exists. Explains they're configured in user's local project's .mux/mcp.jsonc.
1 parent 83f4ff0 commit 5fbf7ed

File tree

5 files changed

+71
-2
lines changed

5 files changed

+71
-2
lines changed

.mux/mcp.jsonc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"servers": {
3+
"chrome": "npx -y chrome-devtools-mcp@latest --headless --chromeArg='--no-sandbox'"
4+
}
5+
}

docs/system-prompt.mdx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,27 @@ You are in a git worktree at ${workspacePath}
5959
</environment>
6060
`;
6161
}
62+
63+
/**
64+
* Build MCP servers context XML block.
65+
* Only included when at least one MCP server is configured.
66+
*/
67+
function buildMCPContext(mcpServers: MCPServerMap): string {
68+
const entries = Object.entries(mcpServers);
69+
if (entries.length === 0) return "";
70+
71+
const serverList = entries.map(([name, command]) => `- ${name}: \`${command}\``).join("\n");
72+
73+
return `
74+
<mcp>
75+
MCP (Model Context Protocol) servers provide additional tools. Configured in user's local project's .mux/mcp.jsonc:
76+
77+
${serverList}
78+
79+
Use /mcp add|edit|remove or Settings → Projects to manage servers.
80+
</mcp>
81+
`;
82+
}
6283
```
6384

6485
{/* END SYSTEM_PROMPT_DOCS */}

src/node/services/aiService.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,14 +982,20 @@ export class AIService extends EventEmitter {
982982
? metadata.projectPath
983983
: runtime.getWorkspacePath(metadata.projectPath, metadata.name);
984984

985+
// Fetch MCP server config for system prompt (before building message)
986+
const mcpServers = this.mcpServerManager
987+
? await this.mcpServerManager.listServers(metadata.projectPath)
988+
: undefined;
989+
985990
// Build system message from workspace metadata
986991
const systemMessage = await buildSystemMessage(
987992
metadata,
988993
runtime,
989994
workspacePath,
990995
mode,
991996
additionalSystemInstructions,
992-
modelString
997+
modelString,
998+
mcpServers
993999
);
9941000

9951001
// Count system message tokens for cost tracking

src/node/services/mcpServerManager.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ export class MCPServerManager {
2828

2929
constructor(private readonly configService: MCPConfigService) {}
3030

31+
/**
32+
* List configured MCP servers for a project (name -> command).
33+
* Used to show server info in the system prompt.
34+
*/
35+
async listServers(projectPath: string): Promise<MCPServerMap> {
36+
return this.configService.listServers(projectPath);
37+
}
38+
3139
async getToolsForWorkspace(options: {
3240
workspaceId: string;
3341
projectPath: string;

src/node/services/systemMessage.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { WorkspaceMetadata } from "@/common/types/workspace";
2+
import type { MCPServerMap } from "@/common/types/mcp";
23
import {
34
readInstructionSet,
45
readInstructionSetFromRuntime,
@@ -77,6 +78,27 @@ You are in a git worktree at ${workspacePath}
7778
</environment>
7879
`;
7980
}
81+
82+
/**
83+
* Build MCP servers context XML block.
84+
* Only included when at least one MCP server is configured.
85+
*/
86+
function buildMCPContext(mcpServers: MCPServerMap): string {
87+
const entries = Object.entries(mcpServers);
88+
if (entries.length === 0) return "";
89+
90+
const serverList = entries.map(([name, command]) => `- ${name}: \`${command}\``).join("\n");
91+
92+
return `
93+
<mcp>
94+
MCP (Model Context Protocol) servers provide additional tools. Configured in user's local project's .mux/mcp.jsonc:
95+
96+
${serverList}
97+
98+
Use /mcp add|edit|remove or Settings → Projects to manage servers.
99+
</mcp>
100+
`;
101+
}
80102
// #endregion SYSTEM_PROMPT_DOCS
81103

82104
/**
@@ -183,6 +205,7 @@ async function readInstructionSources(
183205
* @param mode - Optional mode name (e.g., "plan", "exec")
184206
* @param additionalSystemInstructions - Optional instructions appended last
185207
* @param modelString - Active model identifier used for Model-specific sections
208+
* @param mcpServers - Optional MCP server configuration (name -> command)
186209
* @throws Error if metadata or workspacePath invalid
187210
*/
188211
export async function buildSystemMessage(
@@ -191,7 +214,8 @@ export async function buildSystemMessage(
191214
workspacePath: string,
192215
mode?: string,
193216
additionalSystemInstructions?: string,
194-
modelString?: string
217+
modelString?: string,
218+
mcpServers?: MCPServerMap
195219
): Promise<string> {
196220
if (!metadata) throw new Error("Invalid workspace metadata: metadata is required");
197221
if (!workspacePath) throw new Error("Invalid workspace path: workspacePath is required");
@@ -237,6 +261,11 @@ export async function buildSystemMessage(
237261
// Build system message
238262
let systemMessage = `${PRELUDE.trim()}\n\n${buildEnvironmentContext(workspacePath)}`;
239263

264+
// Add MCP context if servers are configured
265+
if (mcpServers && Object.keys(mcpServers).length > 0) {
266+
systemMessage += buildMCPContext(mcpServers);
267+
}
268+
240269
if (customInstructions) {
241270
systemMessage += `\n<custom-instructions>\n${customInstructions}\n</custom-instructions>`;
242271
}

0 commit comments

Comments
 (0)