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:
parent
53cd5ed84a
commit
61ffba84b5
1 changed files with 59 additions and 2 deletions
|
|
@ -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"
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue