turbomcp_client/client/operations/plugins.rs
1//! Plugin management operations for MCP client
2//!
3//! This module provides methods for registering and managing client plugins
4//! that extend functionality through middleware.
5
6use turbomcp_protocol::{Error, Result};
7
8impl<T: turbomcp_transport::Transport + 'static> super::super::core::Client<T> {
9 /// Register a plugin with the client
10 ///
11 /// # Arguments
12 ///
13 /// * `plugin` - The plugin to register
14 ///
15 /// # Examples
16 ///
17 /// ```rust,no_run
18 /// use turbomcp_client::plugins::{MetricsPlugin, PluginConfig};
19 /// use std::sync::Arc;
20 ///
21 /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
22 /// # let mut client = turbomcp_client::Client::new(turbomcp_transport::stdio::StdioTransport::new());
23 /// let metrics_plugin = Arc::new(MetricsPlugin::new(PluginConfig::Metrics));
24 /// client.register_plugin(metrics_plugin).await?;
25 /// # Ok(())
26 /// # }
27 /// ```
28 pub async fn register_plugin(
29 &self,
30 plugin: std::sync::Arc<dyn crate::plugins::ClientPlugin>,
31 ) -> Result<()> {
32 self.inner
33 .plugin_registry
34 .lock()
35 .await
36 .register_plugin(plugin)
37 .await
38 .map_err(|e| Error::bad_request(format!("Failed to register plugin: {}", e)))
39 }
40
41 /// Check if a plugin is registered
42 ///
43 /// # Arguments
44 ///
45 /// * `name` - The name of the plugin to check
46 pub async fn has_plugin(&self, name: &str) -> bool {
47 self.inner.plugin_registry.lock().await.has_plugin(name)
48 }
49
50 /// Get plugin data for a specific plugin type
51 ///
52 /// # Arguments
53 ///
54 /// * `name` - The name of the plugin
55 ///
56 /// # Examples
57 ///
58 /// ```rust,no_run
59 /// use turbomcp_client::plugins::MetricsPlugin;
60 ///
61 /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
62 /// # let client = turbomcp_client::Client::new(turbomcp_transport::stdio::StdioTransport::new());
63 /// if let Some(plugin) = client.get_plugin("metrics").await {
64 /// // Use plugin data
65 /// }
66 /// # Ok(())
67 /// # }
68 /// ```
69 pub async fn get_plugin(
70 &self,
71 name: &str,
72 ) -> Option<std::sync::Arc<dyn crate::plugins::ClientPlugin>> {
73 self.inner.plugin_registry.lock().await.get_plugin(name)
74 }
75}