Skip to content

feat(mcp): inject MCP server InitializeResult.instructions into system prompt - #4262

Open
luyao10282 wants to merge 1 commit into
MoonshotAI:mainfrom
luyao10282:feat/mcp-server-instructions
Open

luyao10282 wants to merge 1 commit into
MoonshotAI:mainfrom
luyao10282:feat/mcp-server-instructions

Conversation

@luyao10282

Copy link
Copy Markdown

Summary

MCP servers can return server-level usage guidance via InitializeResult.instructions during handshake (Client.getInstructions() in @modelcontextprotocol/sdk), which was previously ignored after connection.

  • Expose getInstructions() on StdioMcpClient, SseMcpClient, and HttpMcpClient and store non-empty handshake instructions on McpConnectionManager entries.
  • Append connected baseline MCP server instructions under # MCP Server Instructions in AgentMcpService before each step begins.
  • Add unit test coverage in test/agent/mcp/mcp.test.ts.

Verification

  • node scripts/check-no-comments.mjs
  • pnpm --filter @moonshot-ai/agent-core-v2 exec vitest run test/agent/mcp/mcp.test.ts

…m prompt

- Expose getInstructions() on stdio, SSE, and HTTP MCP clients and store handshake instructions on McpConnectionManager entries.
- Append connected baseline MCP server instructions under '# MCP Server Instructions' in AgentMcpService before each step begins.
Copilot AI balanced review requested due to automatic review settings October 17, 2026 01:55
@changeset-bot

changeset-bot Bot commented Oct 17, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 2053be5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-17T01:59:53.765194Z 2053be5 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Prompt synchronization must account for turn snapshots, remove obsolete instructions, and preserve user-authored guidance.

3 open issues
What changed in this PR

Adds MCP server handshake instructions to agent system prompts so the model receives server-provided usage guidance.

Changes:

  • Exposes instructions through all three MCP transports and stores non-empty values.
  • Adds per-step prompt synchronization for connected baseline servers.
  • Adds a prompt-injection unit test.
File Description
packages/​agent-core-v2/​test/​agent/​mcp/​mcp.test.ts Tests instruction injection into profile state.
packages/​agent-core-v2/​src/​mcpCore/​types.ts Adds an optional instruction accessor.
packages/​agent-core-v2/​src/​mcpCore/​connection-manager.ts Stores and exposes handshake instructions.
packages/​agent-core-v2/​src/​mcpCore/​client-stdio.ts Exposes stdio server instructions.
packages/​agent-core-v2/​src/​mcpCore/​client-sse.ts Exposes SSE server instructions.
packages/​agent-core-v2/​src/​mcpCore/​client-http.ts Exposes HTTP server instructions.
packages/​agent-core-v2/​src/​agent/​mcp/​mcpService.ts Synchronizes instructions before each step.

Review effort: Balanced

this.attachMcpTools();
loop.hooks.onWillBeginStep.register('mcp', async (ctx, next) => {
await this.waitForInitialLoad(ctx.signal);
this.syncMcpInstructions();
sections.push(`## ${entry.name}\n${instructions}`);
}
}
if (sections.length === 0) return;
Comment on lines +391 to +392
const index = current.indexOf(marker);
const base = index === -1 ? current : current.slice(0, index);

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2053be564d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

this.attachMcpTools();
loop.hooks.onWillBeginStep.register('mcp', async (ctx, next) => {
await this.waitForInitialLoad(ctx.signal);
this.syncMcpInstructions();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Sync MCP instructions before freezing the turn prompt

beginActiveTurn() calls prepareTurnConfig() at loopService.ts:1196, and getOrCreateTurnConfig() snapshots profile.getSystemPrompt() at llmRequesterService.ts:765-770; this hook is only run later by gate() at loopService.ts:1024. Consequently, the first turn sends no MCP instructions at all, and any later instruction change is delayed until the following turn because every request in the current turn uses the frozen prompt. Perform this synchronization before prepareTurnConfig() or include the contribution while resolving the effective request.

AGENTS.md reference: AGENTS.md:L122-L124

Useful? React with 👍 / 👎.

sections.push(`## ${entry.name}\n${instructions}`);
}
}
if (sections.length === 0) return;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Remove stale instructions when no server contributes

When the last instruction-bearing server is disabled, removed, fails, or reconnects without instructions, sections is empty while the profile still contains the block injected on an earlier step. Returning here leaves those obsolete directives in the system prompt indefinitely, so subsequent turns can be told to use a server or tools that are no longer available. Strip the managed block when the current section set is empty.

AGENTS.md reference: AGENTS.md:L122-L124

Useful? React with 👍 / 👎.

'The following MCP servers have provided instructions for how to use their tools and resources:\n\n' +
sections.join('\n\n');
const current = this.profile.getSystemPrompt();
if (current.length === 0) return;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Preserve instructions for empty base prompts

A profile is allowed to render an empty system prompt, but with such a profile this return prevents the generated MCP block from ever being installed even when connected servers provide instructions. In that configuration every turn silently omits the only MCP guidance; use the generated block itself when the base prompt is empty instead of returning.

AGENTS.md reference: AGENTS.md:L102-L104

Useful? React with 👍 / 👎.

}
if (sections.length === 0) return;
const block =
'# MCP Server Instructions\n\n' +

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Add the required changeset for MCP instruction injection

This commit introduces user-visible system-prompt behavior, but its seven changed paths contain no .changeset entry and the repository's existing changesets contain no matching MCP-instructions release note. Add the required short user-facing changeset so the release workflow records this behavior change.

AGENTS.md reference: AGENTS.md:L89-L90

Useful? React with 👍 / 👎.

This branch has not been deployed

No deployments
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