pub struct PluginRegistry { /* private fields */ }
Expand description
Registry for managing client plugins
The registry maintains an ordered list of plugins and provides methods for:
- Plugin registration and validation
- Middleware chain execution
- Custom method routing
- Plugin lifecycle management
§Examples
use turbomcp_client::plugins::{PluginRegistry, MetricsPlugin, PluginConfig};
use std::sync::Arc;
let mut registry = PluginRegistry::new();
// Register a metrics plugin
let metrics = Arc::new(MetricsPlugin::new(PluginConfig::Metrics));
registry.register_plugin(metrics).await?;
// Execute middleware chain
// let mut request_context = RequestContext::new(...);
// registry.execute_before_request(&mut request_context).await?;
Implementations§
Source§impl PluginRegistry
impl PluginRegistry
Sourcepub fn set_client_context(&mut self, context: PluginContext)
pub fn set_client_context(&mut self, context: PluginContext)
Set the client context for plugin initialization
This should be called once when the client is initialized to provide context information to plugins during registration.
Sourcepub async fn register_plugin(
&mut self,
plugin: Arc<dyn ClientPlugin>,
) -> PluginResult<()>
pub async fn register_plugin( &mut self, plugin: Arc<dyn ClientPlugin>, ) -> PluginResult<()>
Register a new plugin
Validates the plugin, checks dependencies, and initializes it. Plugins are executed in registration order.
§Arguments
plugin
- The plugin to register
§Returns
Returns Ok(())
if registration succeeds, or PluginError
if it fails.
§Errors
- Plugin name already registered
- Plugin dependencies not met
- Plugin initialization failure
Sourcepub async fn unregister_plugin(&mut self, plugin_name: &str) -> PluginResult<()>
pub async fn unregister_plugin(&mut self, plugin_name: &str) -> PluginResult<()>
Sourcepub fn has_plugin(&self, plugin_name: &str) -> bool
pub fn has_plugin(&self, plugin_name: &str) -> bool
Check if a plugin is registered
Sourcepub fn get_plugin(&self, plugin_name: &str) -> Option<Arc<dyn ClientPlugin>>
pub fn get_plugin(&self, plugin_name: &str) -> Option<Arc<dyn ClientPlugin>>
Get a plugin by name
Sourcepub fn get_plugin_names(&self) -> Vec<String>
pub fn get_plugin_names(&self) -> Vec<String>
Get all registered plugin names in execution order
Sourcepub fn plugin_count(&self) -> usize
pub fn plugin_count(&self) -> usize
Get the number of registered plugins
Sourcepub async fn execute_before_request(
&self,
context: &mut RequestContext,
) -> PluginResult<()>
pub async fn execute_before_request( &self, context: &mut RequestContext, ) -> PluginResult<()>
Execute before_request middleware chain
Calls before_request
on all registered plugins in order.
If any plugin returns an error, the chain is aborted and the error is returned.
§Arguments
context
- Mutable request context that can be modified by plugins
§Returns
Returns Ok(())
if all plugins succeed, or the first PluginError
encountered.
Sourcepub async fn execute_after_response(
&self,
context: &mut ResponseContext,
) -> PluginResult<()>
pub async fn execute_after_response( &self, context: &mut ResponseContext, ) -> PluginResult<()>
Execute after_response middleware chain
Calls after_response
on all registered plugins in order.
Unlike before_request, this continues execution even if a plugin fails,
logging errors but not aborting the chain.
§Arguments
context
- Mutable response context that can be modified by plugins
§Returns
Returns Ok(())
unless all plugins fail, in which case returns the last error.
Sourcepub async fn handle_custom_method(
&self,
method: &str,
params: Option<Value>,
) -> PluginResult<Option<Value>>
pub async fn handle_custom_method( &self, method: &str, params: Option<Value>, ) -> PluginResult<Option<Value>>
Handle custom method by routing to appropriate plugin
Attempts to handle the custom method by calling handle_custom
on each
plugin in order until one returns Some(Value)
.
§Arguments
method
- The custom method nameparams
- Optional parameters for the method
§Returns
Returns Some(Value)
if a plugin handled the method, None
if no plugin handled it,
or PluginError
if handling failed.
Sourcepub fn get_plugin_info(&self) -> Vec<(String, String, Option<String>)>
pub fn get_plugin_info(&self) -> Vec<(String, String, Option<String>)>
Get plugin information for debugging
Sourcepub fn validate_dependencies(&self) -> Result<(), Vec<String>>
pub fn validate_dependencies(&self) -> Result<(), Vec<String>>
Validate plugin dependencies
Checks that all registered plugins have their dependencies satisfied. This is useful for debugging plugin configuration issues.
Sourcepub async fn clear(&mut self) -> PluginResult<()>
pub async fn clear(&mut self) -> PluginResult<()>
Clear all registered plugins
Calls cleanup on all plugins and removes them from the registry. This is primarily useful for testing and shutdown scenarios.