Skip to main content

prism_mcp_rs/plugin/
types.rs

1//! Plugin system types and traits
2
3use crate::core::error::McpResult;
4use crate::core::prompt::Prompt;
5use crate::core::resource::Resource;
6use crate::core::tool::Tool;
7use async_trait::async_trait;
8use serde::{Deserialize, Serialize};
9use serde_json::Value;
10use std::collections::HashMap;
11
12/// Result of plugin loading operations
13#[derive(Debug, Clone)]
14pub struct LoadResult {
15    /// Number of successfully loaded plugins
16    pub count: usize,
17    /// Information about loaded plugins
18    pub plugins: Vec<LoadedPluginInfo>,
19    /// Errors encountered during loading
20    pub errors: Vec<(String, crate::plugin::PluginError)>,
21}
22
23/// Information about a loaded plugin for LoadResult
24#[derive(Debug, Clone)]
25pub struct LoadedPluginInfo {
26    /// Plugin name
27    pub name: String,
28    /// Plugin version
29    pub version: String,
30    /// Plugin path
31    pub path: std::path::PathBuf,
32    /// Whether the plugin is enabled
33    pub enabled: bool,
34}
35use std::path::PathBuf;
36use std::sync::Arc;
37
38/// Main plugin trait that all plugins must implement
39#[async_trait]
40pub trait Plugin: Send + Sync {
41    /// Get the plugin name
42    fn name(&self) -> &str;
43
44    /// Get the plugin version
45    fn version(&self) -> &str;
46
47    /// Get the plugin description
48    fn description(&self) -> &str;
49
50    /// Initialize the plugin
51    async fn initialize(&mut self) -> McpResult<()>;
52
53    /// Shutdown the plugin
54    async fn shutdown(&mut self) -> McpResult<()>;
55
56    /// Configure the plugin with custom settings
57    async fn configure(&mut self, config: Value) -> McpResult<()>;
58
59    /// Get the plugin capabilities
60    fn capabilities(&self) -> PluginCapabilities;
61
62    /// Get plugin metadata
63    fn metadata(&self) -> PluginMetadata;
64}
65
66/// Plugin capabilities
67#[derive(Debug, Clone, Default, Serialize, Deserialize)]
68pub struct PluginCapabilities {
69    /// Whether the plugin provides tools
70    pub provides_tools: bool,
71
72    /// Whether the plugin provides resources
73    pub provides_resources: bool,
74
75    /// Whether the plugin provides prompts
76    pub provides_prompts: bool,
77
78    /// Whether the plugin supports hot reload
79    pub supports_hot_reload: bool,
80}
81
82/// Plugin metadata
83#[derive(Debug, Clone, Default, Serialize, Deserialize)]
84pub struct PluginMetadata {
85    /// Plugin author
86    pub author: Option<String>,
87
88    /// Plugin license
89    pub license: Option<String>,
90
91    /// Plugin homepage
92    pub homepage: Option<String>,
93
94    /// Plugin repository
95    pub repository: Option<String>,
96
97    /// Plugin keywords
98    pub keywords: Vec<String>,
99
100    /// Plugin categories
101    pub categories: Vec<String>,
102}
103
104/// Plugin information
105#[derive(Debug, Clone, Serialize, Deserialize)]
106pub struct PluginInfo {
107    /// Plugin name
108    pub name: String,
109
110    /// Plugin version
111    pub version: String,
112
113    /// Plugin file path
114    pub path: PathBuf,
115
116    /// Whether the plugin is enabled
117    pub enabled: bool,
118
119    /// Plugin capabilities
120    pub capabilities: PluginCapabilities,
121
122    /// Plugin metadata
123    pub metadata: PluginMetadata,
124}
125
126/// Plugin configuration
127#[derive(Debug, Clone, Serialize, Deserialize)]
128pub struct PluginConfig {
129    /// Whether the plugin is enabled
130    pub enabled: bool,
131
132    /// Whether to auto-reload on changes
133    pub auto_reload: bool,
134
135    /// Path to plugin configuration file
136    pub config_path: Option<PathBuf>,
137
138    /// Plugin-specific settings
139    pub settings: Value,
140}
141
142impl Default for PluginConfig {
143    fn default() -> Self {
144        Self {
145            enabled: true,
146            auto_reload: false,
147            config_path: None,
148            settings: Value::Null,
149        }
150    }
151}
152
153/// Loaded plugin container
154pub struct LoadedPlugin {
155    /// The plugin instance
156    pub plugin: Box<dyn Plugin>,
157
158    /// Plugin configuration
159    pub config: PluginConfig,
160
161    /// Registered tools from this plugin
162    pub tools: HashMap<String, Arc<Tool>>,
163
164    /// Registered resources from this plugin
165    pub resources: HashMap<String, Arc<Resource>>,
166
167    /// Registered prompts from this plugin
168    pub prompts: HashMap<String, Arc<Prompt>>,
169}