fix: sort list_all() output in ToolRouter and PromptRouter for deterministic ordering#665
Open
p4fg wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
Conversation
…inistic ordering ToolRouter::list_all() and PromptRouter::list_all() iterate over a HashMap, which returns items in non-deterministic order. Since list_all() backs the tools/list and prompts/list MCP protocol responses, this causes MCP clients to receive differently-ordered results across calls and process restarts, leading to intermittent tool discovery failures. Sort the output alphabetically by name to guarantee stable ordering.
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.
ToolRouter::list_all()andPromptRouter::list_all()iterate over aHashMap, which returns items in non-deterministic order. Sincelist_all()backs thetools/listandprompts/listMCP protocolresponses, this causes MCP clients to receive differently-ordered results across calls and process restarts, leading to suspected intermittent tool discovery failures.
This pull request sorts the output alphabetically by name to guarantee stable ordering.
Motivation and Context
MCP servers with more than a handful of tools can experience intermittent tool discovery failures in clients (e.g. Claude Code, Claude Desktop). The root cause is that
HashMapiteration order israndomized across process restarts (and potentially within a process), so every
tools/listorprompts/listresponse returns the same set of items but in a different order.This causes:
Related: anthropics/claude-code#2682
How Has This Been Tested?
test_tool_router_list_all_is_sortedandtest_prompt_router_list_all_is_sortedtestsBreaking Changes
None. The output type and contents of
list_all()are unchanged — only the ordering is now guaranteed to be sorted alphabetically by name.Types of changes
Checklist
Additional context
The fix sorts the collected
Vecby name before returning. This is preferred over switching toIndexMap(insertion-order) because it provides a canonical alphabetical ordering regardless of howtools/prompts are registered, with negligible performance cost for typical tool counts.
The
IntoIteratorimpl on both routers also usesHashMapiteration, but it is not used in any protocol-facing context, so it was left unchanged to avoid a breaking API change.