fix: sort list_all() output in ToolRouter and PromptRouter for deterministic ordering (#665)

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 commit is contained in:
Peter 2026-02-17 15:37:17 +01:00 • committed by GitHub
commit 61ffba84b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -66,3 +66,20 @@ where
H: CallToolHandler<S, A>,
{
}
#[test]
fn test_tool_router_list_all_is_sorted() {
let router: ToolRouter<TestHandler<()>> = ToolRouter::<TestHandler<()>>::new()
.with_route((async_function_tool_attr(), async_function))
.with_route((async_function2_tool_attr(), async_function2))
+ TestHandler::<()>::test_router_1()
+ TestHandler::<()>::test_router_2();
let tools = router.list_all();
let names: Vec<&str> = tools.iter().map(|t| t.name.as_ref()).collect();
let mut sorted = names.clone();
sorted.sort();
assert_eq!(
names, sorted,
"list_all() should return tools sorted alphabetically by name"
);
}