feat(router): support runtime disabling of tools (#809)
* feat(router): support runtime disabling of tools Add methods to disable/enable tools at runtime. Disabled tools are hidden from listing, lookup, and execution, including in composed routers. Closes #477 * fix(router): simplify disable tool api * feat(router): auto-send tools/list_changed on disable/enable * refactor(router): simplify disable_route and notifier call
This commit is contained in:
parent
8f696e6788
commit
63583b164f
2 changed files with 750 additions and 15 deletions
|
|
@ -6,7 +6,7 @@ use tool::{IntoToolRoute, ToolRoute};
|
|||
use super::ServerHandler;
|
||||
use crate::{
|
||||
RoleServer, Service,
|
||||
model::{ClientRequest, ListPromptsResult, ListToolsResult, ServerResult},
|
||||
model::{ClientNotification, ClientRequest, ListPromptsResult, ListToolsResult, ServerResult},
|
||||
service::NotificationContext,
|
||||
};
|
||||
|
||||
|
|
@ -18,6 +18,7 @@ pub struct Router<S> {
|
|||
pub tool_router: tool::ToolRouter<S>,
|
||||
pub prompt_router: prompt::PromptRouter<S>,
|
||||
pub service: Arc<S>,
|
||||
peer_slot: Arc<std::sync::OnceLock<crate::service::Peer<RoleServer>>>,
|
||||
}
|
||||
|
||||
impl<S> Router<S>
|
||||
|
|
@ -25,10 +26,14 @@ where
|
|||
S: ServerHandler,
|
||||
{
|
||||
pub fn new(service: S) -> Self {
|
||||
let (notifier, peer_slot) = tool::ToolRouter::<S>::deferred_peer_notifier();
|
||||
let mut tool_router = tool::ToolRouter::new();
|
||||
tool_router.set_notifier(notifier);
|
||||
Self {
|
||||
tool_router: tool::ToolRouter::new(),
|
||||
tool_router,
|
||||
prompt_router: prompt::PromptRouter::new(),
|
||||
service: Arc::new(service),
|
||||
peer_slot,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -72,6 +77,12 @@ where
|
|||
notification: <RoleServer as crate::service::ServiceRole>::PeerNot,
|
||||
context: NotificationContext<RoleServer>,
|
||||
) -> Result<(), crate::ErrorData> {
|
||||
if matches!(
|
||||
¬ification,
|
||||
ClientNotification::InitializedNotification(_)
|
||||
) {
|
||||
let _ = self.peer_slot.set(context.peer.clone());
|
||||
}
|
||||
self.service
|
||||
.handle_notification(notification, context)
|
||||
.await
|
||||
|
|
@ -83,7 +94,10 @@ where
|
|||
) -> Result<<RoleServer as crate::service::ServiceRole>::Resp, crate::ErrorData> {
|
||||
match request {
|
||||
ClientRequest::CallToolRequest(request) => {
|
||||
if self.tool_router.has_route(request.params.name.as_ref())
|
||||
if self
|
||||
.tool_router
|
||||
.map
|
||||
.contains_key(request.params.name.as_ref())
|
||||
|| !self.tool_router.transparent_when_not_found
|
||||
{
|
||||
let tool_call_context = crate::handler::server::tool::ToolCallContext::new(
|
||||
|
|
@ -134,6 +148,81 @@ where
|
|||
}
|
||||
|
||||
fn get_info(&self) -> <RoleServer as crate::service::ServiceRole>::Info {
|
||||
ServerHandler::get_info(&self.service)
|
||||
let mut info = ServerHandler::get_info(&self.service);
|
||||
info.capabilities
|
||||
.tools
|
||||
.get_or_insert_with(Default::default)
|
||||
.list_changed = Some(true);
|
||||
info
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::*;
|
||||
use crate::{
|
||||
model::{CallToolResult, ClientNotification, ServerNotification, Tool},
|
||||
service::{AtomicU32RequestIdProvider, Peer, PeerSinkMessage, RequestIdProvider},
|
||||
};
|
||||
|
||||
struct DummyHandler;
|
||||
impl ServerHandler for DummyHandler {}
|
||||
|
||||
async fn recv_notification(
|
||||
rx: &mut tokio::sync::mpsc::Receiver<PeerSinkMessage<RoleServer>>,
|
||||
) -> ServerNotification {
|
||||
let msg = tokio::time::timeout(std::time::Duration::from_secs(1), rx.recv())
|
||||
.await
|
||||
.expect("timed out")
|
||||
.expect("channel closed");
|
||||
match msg {
|
||||
PeerSinkMessage::Notification {
|
||||
notification,
|
||||
responder,
|
||||
} => {
|
||||
let _ = responder.send(Ok(()));
|
||||
notification
|
||||
}
|
||||
other => panic!("expected notification, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_router_deferred_notifier_e2e() {
|
||||
let mut router = Router::new(DummyHandler).with_tool(tool::ToolRoute::new_dyn(
|
||||
Tool::new("my_tool", "test", Arc::new(Default::default())),
|
||||
|_ctx| Box::pin(async { Ok(CallToolResult::default()) }),
|
||||
));
|
||||
|
||||
let id_provider: Arc<dyn RequestIdProvider> =
|
||||
Arc::new(AtomicU32RequestIdProvider::default());
|
||||
let (peer, mut rx) = Peer::<RoleServer>::new(id_provider, None);
|
||||
|
||||
let context = crate::service::NotificationContext {
|
||||
peer: peer.clone(),
|
||||
meta: Default::default(),
|
||||
extensions: Default::default(),
|
||||
};
|
||||
router
|
||||
.handle_notification(
|
||||
ClientNotification::InitializedNotification(Default::default()),
|
||||
context,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
router.tool_router.disable_route("my_tool");
|
||||
assert!(matches!(
|
||||
recv_notification(&mut rx).await,
|
||||
ServerNotification::ToolListChangedNotification(_)
|
||||
));
|
||||
|
||||
router.tool_router.enable_route("my_tool");
|
||||
assert!(matches!(
|
||||
recv_notification(&mut rx).await,
|
||||
ServerNotification::ToolListChangedNotification(_)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -298,13 +298,30 @@ where
|
|||
self
|
||||
}
|
||||
}
|
||||
#[derive(Debug)]
|
||||
#[non_exhaustive]
|
||||
pub struct ToolRouter<S> {
|
||||
#[allow(clippy::type_complexity)]
|
||||
pub map: std::collections::HashMap<Cow<'static, str>, ToolRoute<S>>,
|
||||
|
||||
pub transparent_when_not_found: bool,
|
||||
|
||||
disabled: std::collections::HashSet<Cow<'static, str>>,
|
||||
|
||||
notifier: Option<Arc<dyn Fn() + Send + Sync>>,
|
||||
}
|
||||
|
||||
impl<S> std::fmt::Debug for ToolRouter<S> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("ToolRouter")
|
||||
.field("map", &self.map)
|
||||
.field(
|
||||
"transparent_when_not_found",
|
||||
&self.transparent_when_not_found,
|
||||
)
|
||||
.field("disabled", &self.disabled)
|
||||
.field("notifier", &self.notifier.as_ref().map(|_| "..."))
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Default for ToolRouter<S> {
|
||||
|
|
@ -312,14 +329,19 @@ impl<S> Default for ToolRouter<S> {
|
|||
Self {
|
||||
map: std::collections::HashMap::new(),
|
||||
transparent_when_not_found: false,
|
||||
disabled: std::collections::HashSet::new(),
|
||||
notifier: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Clone for ToolRouter<S> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
map: self.map.clone(),
|
||||
transparent_when_not_found: self.transparent_when_not_found,
|
||||
disabled: self.disabled.clone(),
|
||||
notifier: self.notifier.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -329,7 +351,11 @@ impl<S> IntoIterator for ToolRouter<S> {
|
|||
type IntoIter = std::collections::hash_map::IntoValues<Cow<'static, str>, ToolRoute<S>>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.map.into_values()
|
||||
let mut map = self.map;
|
||||
for name in &self.disabled {
|
||||
map.remove(name);
|
||||
}
|
||||
map.into_values()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -338,10 +364,7 @@ where
|
|||
S: MaybeSend + 'static,
|
||||
{
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
map: std::collections::HashMap::new(),
|
||||
transparent_when_not_found: false,
|
||||
}
|
||||
Self::default()
|
||||
}
|
||||
pub fn with_route<R, A>(mut self, route: R) -> Self
|
||||
where
|
||||
|
|
@ -394,24 +417,134 @@ where
|
|||
}
|
||||
|
||||
pub fn merge(&mut self, other: ToolRouter<S>) {
|
||||
self.disabled.extend(other.disabled);
|
||||
for item in other.map.into_values() {
|
||||
self.add_route(item);
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove a tool route from the router.
|
||||
///
|
||||
/// The disabled state is **preserved**: if the name was in the disabled
|
||||
/// set, it stays there so that a future [`add_route`](Self::add_route)
|
||||
/// or [`merge`](Self::merge) with the same name will inherit the
|
||||
/// disabled state. To also clear the disabled marker, call
|
||||
/// [`enable_route`](Self::enable_route) afterwards.
|
||||
pub fn remove_route(&mut self, name: &str) {
|
||||
self.map.remove(name);
|
||||
}
|
||||
|
||||
/// Returns `true` if the tool is registered **and** not currently
|
||||
/// disabled.
|
||||
pub fn has_route(&self, name: &str) -> bool {
|
||||
self.map.contains_key(name)
|
||||
self.map.contains_key(name) && !self.disabled.contains(name)
|
||||
}
|
||||
|
||||
/// Disable a tool by name. Hidden from `list_all`, `get`, rejected by
|
||||
/// `call`. Re-enable with [`enable_route`](Self::enable_route).
|
||||
///
|
||||
/// Returns `true` if the name was newly added to the disabled set.
|
||||
/// The name is recorded even if no matching route exists yet, so routes
|
||||
/// added later will inherit the disabled state.
|
||||
pub fn disable_route(&mut self, name: impl Into<Cow<'static, str>>) -> bool {
|
||||
let name = name.into();
|
||||
let was_visible = self.map.contains_key(&name) && !self.disabled.contains(&name);
|
||||
if was_visible {
|
||||
self.notify_if_visible(&name);
|
||||
}
|
||||
self.disabled.insert(name)
|
||||
}
|
||||
|
||||
/// Re-enable a previously disabled tool. Returns `true` if the name
|
||||
/// was in the disabled set.
|
||||
pub fn enable_route(&mut self, name: &str) -> bool {
|
||||
let removed = self.disabled.remove(name);
|
||||
if removed {
|
||||
self.notify_if_visible(name);
|
||||
}
|
||||
removed
|
||||
}
|
||||
|
||||
/// Returns `true` if the tool exists in the router **and** is currently
|
||||
/// disabled. Returns `false` if the tool does not exist or if the name
|
||||
/// was pre-disabled without a matching route.
|
||||
pub fn is_disabled(&self, name: &str) -> bool {
|
||||
self.map.contains_key(name) && self.disabled.contains(name)
|
||||
}
|
||||
|
||||
/// Builder-style variant of [`disable_route`](Self::disable_route).
|
||||
///
|
||||
/// The name is recorded even if no matching route has been added yet,
|
||||
/// so it can be called before [`with_route`](Self::with_route) in a
|
||||
/// builder chain.
|
||||
pub fn with_disabled(mut self, name: impl Into<Cow<'static, str>>) -> Self {
|
||||
self.disabled.insert(name.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Install a callback invoked when the visible tool list changes.
|
||||
pub fn set_notifier(&mut self, f: impl Fn() + Send + Sync + 'static) {
|
||||
self.notifier = Some(Arc::new(f));
|
||||
}
|
||||
|
||||
pub fn clear_notifier(&mut self) {
|
||||
self.notifier = None;
|
||||
}
|
||||
|
||||
/// Install a notifier that sends `notifications/tools/list_changed`
|
||||
/// via the given peer.
|
||||
pub fn bind_peer_notifier(&mut self, peer: &crate::service::Peer<crate::RoleServer>) {
|
||||
let peer = peer.clone();
|
||||
self.set_notifier(move || {
|
||||
let peer = peer.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = peer.notify_tool_list_changed().await {
|
||||
tracing::warn!("failed to send tools/list_changed notification: {e}");
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// Deferred notifier: no-op until the peer slot is filled.
|
||||
pub(crate) fn deferred_peer_notifier() -> (
|
||||
impl Fn() + Send + Sync + 'static,
|
||||
Arc<std::sync::OnceLock<crate::service::Peer<crate::RoleServer>>>,
|
||||
) {
|
||||
let peer_slot =
|
||||
Arc::new(std::sync::OnceLock::<crate::service::Peer<crate::RoleServer>>::new());
|
||||
let slot_clone = peer_slot.clone();
|
||||
let notifier = move || {
|
||||
if let Some(peer) = slot_clone.get() {
|
||||
let peer = peer.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = peer.notify_tool_list_changed().await {
|
||||
tracing::warn!("failed to send tools/list_changed notification: {e}");
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
(notifier, peer_slot)
|
||||
}
|
||||
|
||||
fn notify_if_visible(&self, name: &str) {
|
||||
if self.map.contains_key(name) {
|
||||
if let Some(notifier) = &self.notifier {
|
||||
notifier();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn call(
|
||||
&self,
|
||||
context: ToolCallContext<'_, S>,
|
||||
) -> Result<CallToolResult, crate::ErrorData> {
|
||||
let name = context.name();
|
||||
if self.disabled.contains(name) {
|
||||
return Err(crate::ErrorData::invalid_params("tool not found", None));
|
||||
}
|
||||
let item = self
|
||||
.map
|
||||
.get(context.name())
|
||||
.get(name)
|
||||
.ok_or_else(|| crate::ErrorData::invalid_params("tool not found", None))?;
|
||||
|
||||
let result = (item.call)(context).await?;
|
||||
|
|
@ -420,15 +553,24 @@ where
|
|||
}
|
||||
|
||||
pub fn list_all(&self) -> Vec<crate::model::Tool> {
|
||||
let mut tools: Vec<_> = self.map.values().map(|item| item.attr.clone()).collect();
|
||||
let mut tools: Vec<_> = self
|
||||
.map
|
||||
.values()
|
||||
.filter(|item| !self.disabled.contains(&item.attr.name))
|
||||
.map(|item| item.attr.clone())
|
||||
.collect();
|
||||
tools.sort_by(|a, b| a.name.cmp(&b.name));
|
||||
tools
|
||||
}
|
||||
|
||||
/// Get a tool definition by name.
|
||||
///
|
||||
/// Returns the tool if found, or `None` if no tool with the given name exists.
|
||||
/// Returns the tool if found and enabled, or `None` if the tool does not
|
||||
/// exist or is disabled.
|
||||
pub fn get(&self, name: &str) -> Option<&crate::model::Tool> {
|
||||
if self.disabled.contains(name) {
|
||||
return None;
|
||||
}
|
||||
self.map.get(name).map(|r| &r.attr)
|
||||
}
|
||||
}
|
||||
|
|
@ -453,3 +595,49 @@ where
|
|||
self.merge(other);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::*;
|
||||
use crate::{
|
||||
RoleServer,
|
||||
model::{CallToolRequestParams, ErrorCode, NumberOrString},
|
||||
service::{AtomicU32RequestIdProvider, Peer, RequestContext},
|
||||
};
|
||||
|
||||
struct DummyService;
|
||||
impl crate::handler::server::ServerHandler for DummyService {}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_call_disabled_tool_returns_error() {
|
||||
let service = DummyService;
|
||||
let mut router = ToolRouter::new().with_route(ToolRoute::new_dyn(
|
||||
crate::model::Tool::new("test_tool", "a test tool", Arc::new(Default::default())),
|
||||
|_ctx| Box::pin(async { Ok(CallToolResult::default()) }),
|
||||
));
|
||||
router.disable_route("test_tool");
|
||||
|
||||
let id_provider: Arc<dyn crate::service::RequestIdProvider> =
|
||||
Arc::new(AtomicU32RequestIdProvider::default());
|
||||
let (peer, _rx) = Peer::<RoleServer>::new(id_provider, None);
|
||||
let ctx = crate::handler::server::tool::ToolCallContext::new(
|
||||
&service,
|
||||
CallToolRequestParams {
|
||||
meta: None,
|
||||
name: Cow::Borrowed("test_tool"),
|
||||
arguments: None,
|
||||
task: None,
|
||||
},
|
||||
RequestContext::new(NumberOrString::Number(1), peer),
|
||||
);
|
||||
|
||||
let err = router
|
||||
.call(ctx)
|
||||
.await
|
||||
.expect_err("disabled tool should reject");
|
||||
assert_eq!(err.code, ErrorCode::INVALID_PARAMS);
|
||||
assert_eq!(err.message, "tool not found");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue