Skip to content

fix(mcp): carry server instructions into embedded agent prompts - #174342

Merged
obviyus merged 18 commits into
openclaw:mainfrom
xiaopan-xydt:fix/mcp-bridge-server-instructions-174272
Oct 20, 2026
Merged

obviyus merged 18 commits into
openclaw:mainfrom
xiaopan-xydt:fix/mcp-bridge-server-instructions-174272

Conversation

@xiaopan-xydt

@xiaopan-xydt xiaopan-xydt commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #174336

What Problem This Solves

Fixes: embedded agents receive an MCP server's tools without its initialization guidance about sequencing, confirmations, quotas, or retries.

User Impact

Embedded agents receive guidance for available MCP tools in their direct prompt or deferred Tool Search / Code Mode metadata. Each server contributes at most a 2,000-character prefix plus a truncation marker. Servers without instructions keep their existing surface. Claude CLI loopback guidance remains whole and unchanged.

Why This Change Was Made

Reuse the initialization read and raw catalog fields landed in #172026 rather than maintaining a second reader or duplicate fields. The cap belongs only to embedded tool-metadata materialization, leaving #172026's native Claude contract intact. Prepare each server's bounded metadata once, cut without splitting a Unicode surrogate pair, append …[truncated: server instructions exceed 2000 characters] when needed, then neutralize prompt delimiters and model control tokens. All three embedded delivery paths consume that same text; omitted trailing guidance cannot be recovered through discovery.

This bounds prompt cost for model-facing descriptive metadata, consistent with Codex surfacing server instructions as a namespace description; it does not claim Codex uses this cap.

The original contributor's commits and credit are preserved. The overlapping contributor PRs #174425, #174508, and #174396 can be closed as superseded once this lands.

Evidence

Built head 0ec710a2ec4cdcaaeb97f1cd99150e9339659987, local stdio MCP server, recording OpenAI-compatible provider, and a shipped openclaw agent turn through an isolated Gateway:

short: request=1, MCP tool=present, guidance=present, truncated=false
long:  request=1, MCP tool=present, cappedTextPresent=true,
       tailAbsent=true, truncationMarkerCount=1

The long server sends more than 3,500 characters. The recorded provider request contains exactly the retained 2,000-character prefix, with prompt delimiters neutralized afterward, plus one truncation marker; the trailing rule is absent. The regression covers a surrogate-pair boundary and verifies that the raw catalog still preserves the complete text. The existing short-instruction regression is unchanged.

Native preservation: #172026's configured-MCP loopback tests now use guidance longer than 2,500 characters and require the complete text and trailing rule across ordinary/completion turns. Its allowed, denied, and requester-capped cases pass, as do existing-session, completion-tool, and configured-MCP HTTP authority tests.

100 tests pass across nine focused files in 112.32 seconds, covering embedded delivery/revocation, materialization, Code Mode, Tool Search metadata, prefix stability, and the native boundaries above. Targeted formatting/lint pass. Prompt fixtures were regenerated with the canonical generator. A fresh openai/gpt-5.4-mini turn completes successfully and returns the requested marker.

Original missing-instructions proof is retained: main 7a628b0adaa omits the guidance in two recorded provider requests; candidate 4d46784da23 includes it in both. A server without instructions has identical tool definitions and an identical system prompt after normalizing only the separate proof workspace/repository paths.

Merge danger: two-way door; reverting removes embedded prompt/metadata additions and writes no new persistent state or config. Blast radius: embedded agents using MCP tools, including Tool Search and Code Mode. Native Claude CLI's complete-guidance contract is preserved.

@clawsweeper

clawsweeper Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime agents Agent runtime and tooling size: M labels Oct 16, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Oct 16, 2026
@clawsweeper

clawsweeper Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

Carries bounded MCP server instructions into embedded prompts, Tool Search results, and Code Mode metadata.

Example: The research server exposes search and instructs clients to confirm before paid calls.

  • Before: The embedded agent receives search without the server-wide confirmation guidance.
  • After: The agent receives that guidance in its direct prompt or deferred discovery metadata.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) Useful, focused repair with recorded shipped-entry-point proof, distinct delivery-boundary tests, and no remaining actionable finding.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (logs): The exact-head body records shipped openclaw agent turns through an isolated Gateway with a local stdio MCP server and recording provider: short instructions appear, long instructions retain the capped prefix and marker, and the trailing rule is absent; a real model turn also completes.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Embedded agents miss server-wide sequencing, confirmation, quota, and retry guidance despite receiving the server's tools.
Reason: Restores a demonstrated MCP guidance contract through existing owners; the area contributor's repair explicitly bounds embedded presentation while preserving complete native guidance.

Merge readiness

✅ Ready for maintainer review

This PR fixes a verified embedded MCP guidance gap; the previous integration blocker is resolved, and no actionable introduced defect remains.

Priority: P2
Reviewed head: 0ec710a2ec4cdcaaeb97f1cd99150e9339659987

Before merge

None.

Findings

None.

Agent review details

How this fits together

MCP initialization supplies raw server guidance to the catalog; tool materialization prepares embedded metadata consumed by prompts and discovery, while the native CLI bridge retains complete guidance.

flowchart TD
  A[MCP initialization] --> B[Raw server catalog]
  B --> C[Embedded metadata materialization]
  C --> D[Direct system prompt]
  C --> E[Tool Search and Code Mode]
  B --> F[Native CLI loopback]
Loading

Technical review

Best possible solution:

Land the shared embedded metadata projection while keeping raw catalog guidance complete for native consumers.

Do we have a high-confidence way to reproduce the issue?

Current main reads raw server instructions but omits them from embedded tool metadata and prompts; the linked report and recorded baseline provider requests demonstrate the resulting missing guidance.

Is this the best way to solve the issue?

The smallest complete fix attaches prepared guidance at the existing materialization owner and carries it through existing prompt and discovery owners, preserving native consumers and execution policy.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against a69d352fdc59.

Provenance checked

  • Native raw instruction catalog keeps the original intent (77d9f42: The merged Claude CLI repair preserves initialization guidance previously received through native MCP connections.)
  • Tool Search descriptor and describe ownership keeps the original intent (050475b: The behavior-neutral extraction separated catalog lifecycle and runtime execution into cohesive owners; this PR moves description assembly into the shared catalog owner rather than adding another reader.)
  • Code Mode namespace documentation keeps the original intent (86b68bc: Prepare one run-owned namespace model and virtual API representation instead of rebuilding equivalent descriptions.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

  • Pinned introduction reviewed: Reviewed the merge-base-to-head hunks separately from current-main context; the checkout matches the supplied PR head. (0ec710a2ec4c)
  • Shared embedded presentation: Materialization prepares one Unicode-safe prefix per server, marks truncation, and neutralizes application delimiters and model control tokens before attaching guidance to ordinary and utility tools. (src/agents/agent-bundle-mcp-materialize.ts:185, 0ec710a2ec4c)
  • Effective-tool prompt ownership: The configured prompt builder collects guidance from the effective concrete tools and deduplicates it per server; deferred paths use the existing catalog and namespace owners. (src/agents/system-prompt-config.ts:30, 0ec710a2ec4c)
  • Previous blocker resolved: The GitHub comparison against the previously reviewed head shows reconciliation with fix(claude-cli): stop cache-prefix churn after child completion #172026: one raw instruction capture and declaration remain, and the cap now belongs downstream in embedded materialization. (src/agents/agent-bundle-mcp-runtime.ts:596, 0ec710a2ec4c)
  • Native guidance contract preserved: The loopback obtains complete guidance directly from the raw catalog; the strengthened configured-MCP test requires its trailing rule beyond 2,500 characters. (src/agents/cli-runner/loopback-bundle-mcp.ts:106, 0ec710a2ec4c)
  • Recorded shipped-entry-point proof: The full PR body records an isolated Gateway and shipped openclaw agent turn on the exact head: short guidance reaches the provider request; long guidance retains the bounded prefix and one marker while omitting the tail. Earlier recorded baseline requests omit guidance. (0ec710a2ec4c)

Likely related people:

  • Peter Steinberger: Raw commit ec8cb8b adds src/agents/code-mode-namespaces.ts:456 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: ec8cb8bcbfae; files: src/agents/code-mode-namespaces.ts)

PR surface

Source +167, Tests +483, Docs +10. Total +660 across 24 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 14 188 21 +167
Tests 9 491 8 +483
Docs 1 10 0 +10
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 24 689 29 +660

Labels

Label changes:

  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • remove rating: 🧂 unranked krab: Current PR rating is rating: 🐚 platinum hermit, so this older rating label is no longer current.
  • remove merge-risk: 🚨 availability: Current PR review selected no merge-risk labels.
  • remove status: ⏳ waiting on author: Current PR status label is status: 👀 ready for maintainer look.

Label justifications:

  • P2: Restores missing MCP usage guidance for embedded agents without changing execution policy or persistent state.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (13 earlier review cycles; latest 8 shown)
  • reviewed 2026-10-17T05:39:52.578Z sha 943d89ccde9d4e4e4f58c739ef257910219df262 :: needs changes before merge. :: [P2] Keep server guidance from truncating scalar discovery and schemas
  • reviewed 2026-10-17T06:01:28.545Z sha 38a2a43 :: needs maintainer review before merge. :: none
  • reviewed 2026-10-18T13:07:23.936Z sha 41e6828 :: needs changes before merge. :: [P2] Preserve complete guidance or direct its retrieval during discovery
  • reviewed 2026-10-18T17:06:06.833Z sha 4517d58 :: needs changes before merge. :: [P2] Preserve guidance retrieval hints on Code Mode search handles | [P2] Account for the describe response limit in retrieval hints | [P2] Regenerate the prompt snapshot for the changed descriptions
  • reviewed 2026-10-18T17:36:04.540Z sha 53f1a5e :: needs maintainer review before merge. :: none
  • reviewed 2026-10-20T00:18:06.038Z sha 4d46784 :: needs maintainer review before merge. :: none
  • reviewed 2026-10-20T12:37:23.536Z sha c84fcf8 :: needs changes before merge. :: none
  • reviewed 2026-10-20T13:18:12.730Z sha 751ce81 :: needs changes before merge. :: [P1] Reconcile instruction declarations with the newly merged main change

Reviewed October 20, 2026, 9:44 AM ET / 13:44 UTC (Revision 14).

@xiaopan-xydt
xiaopan-xydt force-pushed the fix/mcp-bridge-server-instructions-174272 branch 2 times, most recently from c61e357 to 0105157 Compare October 16, 2026 15:39
@clawsweeper clawsweeper Bot removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Oct 16, 2026
@xiaopan-xydt
xiaopan-xydt force-pushed the fix/mcp-bridge-server-instructions-174272 branch from 0105157 to d33ac22 Compare October 17, 2026 02:45
@xiaopan-xydt xiaopan-xydt changed the title fix(mcp): forward server instructions through the loopback bridge fix(mcp): carry server instructions into embedded agent prompts Oct 17, 2026
@openclaw-barnacle openclaw-barnacle Bot removed docs Improvements or additions to documentation gateway Gateway runtime labels Oct 17, 2026
@xiaopan-xydt
xiaopan-xydt force-pushed the fix/mcp-bridge-server-instructions-174272 branch from d33ac22 to a4ada1d Compare October 17, 2026 02:57
@xiaopan-xydt

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 17, 2026
@xiaopan-xydt

Copy link
Copy Markdown
Contributor Author

Addressed the three new P2 findings on 53f1a5e4408.

  • Code Mode hints: search handles now carry a Code Mode-specific instructionsHint telling the caller to use handle.describe() before invocation. The hint is preserved in both the search result and handle.toJSON() metadata, and describe() still returns the full server instructions.
  • Describe budget honesty: search hints now distinguish guidance that tool_describe can return from guidance exceeding its 20,000-character delivery budget. The latter uses an explicit “only the visible excerpt can be returned” hint instead of promising complete instructions. The test checks the model-visible search hint and the model-visible [truncated] describe response, not only internal details.
  • Prompt snapshots: regenerated test/fixtures/agents/prompt-snapshots/local-ollama/default-tool-surface.json; prompt:snapshots:check reports all 8 files current.

Validation: tool-search.metadata.test.ts 8/8, code-mode.mcp.test.ts 15/15 across interactive/headless, tool-search-runtime.test.ts 45/45, pnpm tsgo:core, oxfmt, oxlint, and git diff --check pass.

Live provider behavior proof remains unavailable in this environment; I did not fabricate it.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@xiaopan-xydt

Copy link
Copy Markdown
Contributor Author

Pushed 18467629c18 to fix check-test-types-core-3: the metadata assertion now types instructionsHint on deferred candidates. Focused suite passed (8 tests).

@xiaopan-xydt

Copy link
Copy Markdown
Contributor Author

The branch now includes the latest main merge at 43562fdbb00, preserving the MCP server-instructions implementation. The focused agents-core suite passed 72/72 and the instructions prompt test passed 7/7. @clawsweeper re-review

@obviyus

obviyus commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@obviyus

obviyus commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

Maintainer override:
The only failing product lane on head 4d46784da23c1342d0ca38cea663317e26baec84 is checks-ui-e2e (1/2): boot-module-boundaries.e2e.test.ts times out clicking the disabled “Retry now” button in both sidebar-download recovery cases (390px and 1280px).

This PR's job: https://github.com/openclaw/openclaw/actions/runs/39345991063/job/118379129242

The identical two cases and 30-second disabled-button timeout occur on unrelated PR #177875, head 712c0e769c26c45783b69790f897b3a5625e234e: https://github.com/openclaw/openclaw/actions/runs/39346810348/job/118381812534

This diff changes MCP instruction delivery in the embedded agent, not Control UI module loading, connection recovery, or the mocked sidebar fixture. The agent/MCP focused lanes, typechecks, prompt snapshots, security checks, and build proof pass. The CI gate failure is the aggregate consequence of this UI failure. Cancelled jobs: 0. No jobs were rerun and no extra push was made to obtain green.

@xiaopan-xydt

xiaopan-xydt commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Merged current main into the branch as ec8841a4dce, picking up 92bd521fabf (test(ui): settle failed reconnects before retrying module recovery), which repairs the boot-module-boundaries Retry timeout seen here and on #177875.

Fresh CI on the new head now passes both checks-ui-e2e (1/2) and checks-ui-e2e (2/2); the remaining lint/typecheck jobs are queued. Local worktree validation was unavailable because the partial clone could not supply a required object, so the remote CI run is the validation source. No PR product code was changed.

@obviyus

obviyus commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

# Conflicts:
#	src/agents/tool-search.metadata.test.ts
#	src/agents/tool-search.ts
#	test/fixtures/agents/prompt-snapshots/local-ollama/default-tool-surface.json
@obviyus

obviyus commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@obviyus

obviyus commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@obviyus

obviyus commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

Landed in f874f24. Thanks @xiaopan-xydt. Embedded agents now get MCP server instructions (capped at 2,000 characters per server).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling docs Improvements or additions to documentation P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: L status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Embedded runtime drops MCP server instructions; the MCP catalog never reads them

2 participants