security(mcp): implement tools/list_changed refresh path with sanitization#1762
Merged
security(mcp): implement tools/list_changed refresh path with sanitization#1762
Conversation
…ation Implement MCP tools/list_changed notification handler in McpManager so that tool list refreshes pushed by servers are sanitized before entering the system prompt (closes #1746). Key changes: - Add ToolListChangedHandler implementing rmcp::ClientHandler; handler applies rate-limiting (5 s per server) and MAX_TOOLS_PER_SERVER cap (100) before calling sanitize_tools(), then sends a ToolRefreshEvent via mpsc - McpManager gains a background refresh task (spawn_refresh_task) that receives ToolRefreshEvents and broadcasts via watch::Sender<Vec<McpTool>> - subscribe_tool_changes() exposes the watch receiver to callers - remove_server() cleans up server_tools HashMap and last_refresh DashMap - shutdown_all_shared() drops refresh_tx, terminating the refresh task cleanly - Agent checks watch::Receiver between turns via check_tool_refresh(), guarded against consuming the empty initial value Security invariants: sanitize_tools() is always called before tools reach the channel or the agent — same guarantee as the connect_all()/add_server() paths.
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.
Summary
Fixes #1746. MCP servers can push tool list updates via
tools/list_changednotifications. This PR implements the handler so thatsanitize_tools()is always called before refreshed tools enter the registry or system prompt.ToolListChangedHandlerimplementingrmcp::ClientHandler; applies rate-limiting (5 s/server) andMAX_TOOLS_PER_SERVER=100cap beforesanitize_tools(), then sends aToolRefreshEventvia mpscMcpManager::spawn_refresh_task()receives events and broadcasts viawatch::Sender<Vec<McpTool>>McpManager::subscribe_tool_changes()exposes the watch receiver to callersremove_server()cleans upserver_toolsHashMap andlast_refreshDashMapshutdown_all_shared()dropsrefresh_tx, terminating the refresh task cleanlywatch::Receiverbetween turns viacheck_tool_refresh(), guarded against consuming the empty initial valueSecurity invariants:
sanitize_tools()is unconditionally called before tools reach the channel or the agent — same guarantee as theconnect_all()andadd_server()paths.Test plan
cargo nextest run --workspace --features full --lib --bins)cargo clippy --workspace --features full -- -D warningspasses (0 warnings)cargo +nightly fmt --checkpasses