fix(cache): sort MCP tools deterministically to stabilize prompt cache#58037
Merged
Conversation
Contributor
Greptile SummaryThis PR makes the tools array returned by
Confidence Score: 5/5
Reviews (1): Last reviewed commit: "fix(cache): sort MCP tools deterministic..." | Re-trigger Greptile |
This comment was marked as spam.
This comment was marked as spam.
odysseus0
force-pushed
the
cache/mcp-tools-sort
branch
from
April 4, 2026 00:19
e134420 to
2ca9eed
Compare
Contributor
|
Landed via manual rebase-port onto main (original base was stale after upstream MCP runtime refactor).
Thanks @bcherny! |
1 task
KimGLee
pushed a commit
to KimGLee/openclaw
that referenced
this pull request
Apr 4, 2026
openclaw#58037) Co-authored-by: George Zhang <[email protected]>
27 tasks
MonkeyLeeT
added a commit
to MonkeyLeeT/openclaw
that referenced
this pull request
Apr 4, 2026
vincentkoc
pushed a commit
that referenced
this pull request
Apr 5, 2026
(#60633) * docs: correct overstated prompt-cache comments from #58036 #58037 #58038 * docs: restore purpose context in MCP tool sort comment * docs: drop misleading 'legacy' framing from image-prune comments * docs: restore useful context stripped from image-prune comments * docs: restore 'deterministically' in MCP tool sort comment * docs: restore 'idempotent' at attempt.ts callsite * docs: restore 'provider prompt cache' in context-guard comment
1 task
mdrxy
added a commit
to langchain-ai/deepagents
that referenced
this pull request
Apr 7, 2026
…#2497) MCP's `list_tools()` does not guarantee tool ordering. If tool order differs between turns — from paginated responses, server restarts, or SDK changes — the tools array in the API request changes, busting the prompt cache at the tools block. Sort the collected tools by name in `_load_tools_from_config` to make the tools block deterministic. Inspired by [openclaw/openclaw#58037](openclaw/openclaw#58037).
lovewanwan
pushed a commit
to lovewanwan/openclaw
that referenced
this pull request
Apr 28, 2026
openclaw#58037) Co-authored-by: George Zhang <[email protected]>
lovewanwan
pushed a commit
to lovewanwan/openclaw
that referenced
this pull request
Apr 28, 2026
…enclaw#58037 openclaw#58038 (openclaw#60633) * docs: correct overstated prompt-cache comments from openclaw#58036 openclaw#58037 openclaw#58038 * docs: restore purpose context in MCP tool sort comment * docs: drop misleading 'legacy' framing from image-prune comments * docs: restore useful context stripped from image-prune comments * docs: restore 'deterministically' in MCP tool sort comment * docs: restore 'idempotent' at attempt.ts callsite * docs: restore 'provider prompt cache' in context-guard comment
james8814
pushed a commit
to james8814/deepagents
that referenced
this pull request
May 1, 2026
…langchain-ai#2497) MCP's `list_tools()` does not guarantee tool ordering. If tool order differs between turns — from paginated responses, server restarts, or SDK changes — the tools array in the API request changes, busting the prompt cache at the tools block. Sort the collected tools by name in `_load_tools_from_config` to make the tools block deterministic. Inspired by [openclaw/openclaw#58037](openclaw/openclaw#58037).
ogt-redknie
pushed a commit
to ogt-redknie/OPENX
that referenced
this pull request
May 2, 2026
openclaw#58037) Co-authored-by: George Zhang <[email protected]>
ogt-redknie
pushed a commit
to ogt-redknie/OPENX
that referenced
this pull request
May 2, 2026
…enclaw#58037 openclaw#58038 (openclaw#60633) * docs: correct overstated prompt-cache comments from openclaw#58036 openclaw#58037 openclaw#58038 * docs: restore purpose context in MCP tool sort comment * docs: drop misleading 'legacy' framing from image-prune comments * docs: restore useful context stripped from image-prune comments * docs: restore 'deterministically' in MCP tool sort comment * docs: restore 'idempotent' at attempt.ts callsite * docs: restore 'provider prompt cache' in context-guard comment
12 tasks
github-actions Bot
pushed a commit
to Desicool/openclaw
that referenced
this pull request
May 9, 2026
openclaw#58037) Co-authored-by: George Zhang <[email protected]>
github-actions Bot
pushed a commit
to Desicool/openclaw
that referenced
this pull request
May 9, 2026
…enclaw#58037 openclaw#58038 (openclaw#60633) * docs: correct overstated prompt-cache comments from openclaw#58036 openclaw#58037 openclaw#58038 * docs: restore purpose context in MCP tool sort comment * docs: drop misleading 'legacy' framing from image-prune comments * docs: restore useful context stripped from image-prune comments * docs: restore 'deterministically' in MCP tool sort comment * docs: restore 'idempotent' at attempt.ts callsite * docs: restore 'provider prompt cache' in context-guard comment
github-actions Bot
pushed a commit
to Desicool/openclaw
that referenced
this pull request
May 24, 2026
openclaw#58037) Co-authored-by: George Zhang <[email protected]>
github-actions Bot
pushed a commit
to Desicool/openclaw
that referenced
this pull request
May 24, 2026
…enclaw#58037 openclaw#58038 (openclaw#60633) * docs: correct overstated prompt-cache comments from openclaw#58036 openclaw#58037 openclaw#58038 * docs: restore purpose context in MCP tool sort comment * docs: drop misleading 'legacy' framing from image-prune comments * docs: restore useful context stripped from image-prune comments * docs: restore 'deterministically' in MCP tool sort comment * docs: restore 'idempotent' at attempt.ts callsite * docs: restore 'provider prompt cache' in context-guard comment
Nachx639
pushed a commit
to Nachx639/clawdbot
that referenced
this pull request
Jun 17, 2026
openclaw#58037) Co-authored-by: George Zhang <[email protected]>
Nachx639
pushed a commit
to Nachx639/clawdbot
that referenced
this pull request
Jun 17, 2026
…enclaw#58037 openclaw#58038 (openclaw#60633) * docs: correct overstated prompt-cache comments from openclaw#58036 openclaw#58037 openclaw#58038 * docs: restore purpose context in MCP tool sort comment * docs: drop misleading 'legacy' framing from image-prune comments * docs: restore useful context stripped from image-prune comments * docs: restore 'deterministically' in MCP tool sort comment * docs: restore 'idempotent' at attempt.ts callsite * docs: restore 'provider prompt cache' in context-guard comment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
createBundleMcpToolRuntime(src/agents/pi-bundle-mcp-tools.ts:122) is called on every turn fromattempt.ts:1842. It collects tools viaclient.listTools()without sorting, then returns them in collection order.The MCP spec doesn't guarantee
listTools()order. If tool order differs between turns — from paginated responses, third-party server implementations, or future SDK changes — the tools array in the API request changes, busting the prompt cache at the tools block.Fix
Sort the collected tools alphabetically by name before returning:
This makes the tools block deterministic regardless of
listTools()iteration order or server-config iteration order.Semantics unchanged: collision resolution (first-server-wins via
reservedNamesset) happens during collection, before the sort.Verification
zeta, alpha, mu— asserts runtime returns[alpha, mu, zeta]attempt.tsare name-based (allowlist, prefix-stripping at :620), not position-basedFollow-up (not in this PR)
Hoist runtime to session scope.
createBundleMcpToolRuntimecurrently spawns stdio transports +listTools()on every turn and disposes in thefinallyatattempt.ts:3227. Bigger win: cache the runtime keyed on a hash ofloaded.mcpServersconfig, dispose on session end. Stops re-spawning MCP servers per turn. More invasive — needs session-lifecycle wiring.🤖 Generated with Claude Code