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
2 changed files with 59 additions and 2 deletions
|
|
@ -187,7 +187,9 @@ where
|
|||
}
|
||||
|
||||
pub fn list_all(&self) -> Vec<crate::model::Prompt> {
|
||||
self.map.values().map(|item| item.attr.clone()).collect()
|
||||
let mut prompts: Vec<_> = self.map.values().map(|item| item.attr.clone()).collect();
|
||||
prompts.sort_by(|a, b| a.name.cmp(&b.name));
|
||||
prompts
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -252,7 +252,9 @@ where
|
|||
}
|
||||
|
||||
pub fn list_all(&self) -> Vec<crate::model::Tool> {
|
||||
self.map.values().map(|item| item.attr.clone()).collect()
|
||||
let mut tools: Vec<_> = self.map.values().map(|item| item.attr.clone()).collect();
|
||||
tools.sort_by(|a, b| a.name.cmp(&b.name));
|
||||
tools
|
||||
}
|
||||
|
||||
/// Get a tool definition by name.
|
||||
|
|
|
|||
Loading…
Reference in a new issue