Skip to main content

ToolFilter

Trait ToolFilter 

Source
pub trait ToolFilter: Send + Sync {
    // Required method
    fn allow<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        tool: &'life1 Tool,
        context: &'life2 RequestContext<RoleServer>,
    ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
}
Expand description

Trait for dynamically filtering tools based on request context.

Implement this to control which tools are visible and callable based on user permissions, scopes, or other runtime context.

§Usage

use std::sync::Arc;
use rmcp_openapi::{Server, ToolFilter};

let server = Server::builder()
    .openapi_spec(spec)
    .base_url(url)
    .tool_filter(Arc::new(MyFilter::new()))
    .build();

§Behavior

  • list_tools: Only returns tools where allow returns true
  • call_tool: Returns “tool not found” error if filter rejects the tool

Required Methods§

Source

fn allow<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, tool: &'life1 Tool, context: &'life2 RequestContext<RoleServer>, ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Returns true if the tool should be accessible in this context.

Called for both list_tools (to filter visible tools) and call_tool (to enforce access control).

§Arguments
  • tool - The tool to check access for
  • context - The request context containing extensions (e.g., user scopes)
§Returns

true if the tool should be accessible, false to hide/block it

Implementors§