solidmcp 0.4.0

A high-level Rust toolkit for building Model Context Protocol (MCP) servers with type safety and minimal boilerplate. Supports tools, resources, and prompts with automatic JSON schema generation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//! High-level MCP Server API
//!
//! This module provides a high-level API for building MCP servers with custom functionality.

use {
    crate::{
        core::McpServer,
        handler::{
            McpContext, McpHandler, McpNotification, PromptContent, PromptInfo, ResourceContent,
            ResourceInfo, ToolDefinition,
        },
    },
    anyhow::Result,
    async_trait::async_trait,
    serde_json::{json, Value},
    std::{collections::HashMap, sync::Arc},
    tokio::sync::{mpsc, RwLock},
    tracing::info,
};

/// Context provided to tools for accessing server functionality
pub struct ToolContext {
    /// Sender for notifications
    pub notification_sender: Option<mpsc::UnboundedSender<McpNotification>>,
}

/// Trait for implementing custom MCP tools
#[async_trait]
pub trait McpTool: Send + Sync {
    /// Get the tool definition for tools/list
    fn definition(&self) -> ExtendedToolDefinition;

    /// Execute the tool with given arguments
    async fn execute(&self, arguments: Value, context: &ToolContext) -> Result<Value>;
}

/// Extended tool definition for MCP (with output_schema)
#[derive(Debug, Clone)]
pub struct ExtendedToolDefinition {
    pub name: String,
    pub description: String,
    pub input_schema: Value,
    pub output_schema: Value,
}

/// Trait for implementing MCP resources
#[async_trait]
pub trait McpResourceProvider: Send + Sync {
    /// List available resources
    async fn list_resources(&self) -> Result<Vec<ResourceInfo>>;

    /// Read a specific resource by URI
    async fn read_resource(&self, uri: &str) -> Result<ResourceContent>;
}

/// Trait for implementing MCP prompts
#[async_trait]
pub trait McpPromptProvider: Send + Sync {
    /// List available prompts
    async fn list_prompts(&self) -> Result<Vec<PromptInfo>>;

    /// Get a specific prompt by name
    async fn get_prompt(&self, name: &str, arguments: Option<Value>) -> Result<PromptContent>;
}

/// High-level MCP Server that can be customized with tools, resources, and prompts
pub struct McpServerBuilder {
    tools: HashMap<String, Arc<dyn McpTool>>,
    resource_provider: Option<Arc<dyn McpResourceProvider>>,
    prompt_provider: Option<Arc<dyn McpPromptProvider>>,
    capabilities: ServerCapabilities,
}

/// Server capabilities configuration
#[derive(Debug, Clone, Default)]
pub struct ServerCapabilities {
    pub experimental: HashMap<String, Value>,
}

impl Default for McpServerBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl McpServerBuilder {
    /// Create a new MCP server builder
    pub fn new() -> Self {
        Self {
            tools: HashMap::new(),
            resource_provider: None,
            prompt_provider: None,
            capabilities: ServerCapabilities::default(),
        }
    }

    /// Add a custom tool to the server
    pub fn add_tool(mut self, tool: impl McpTool + 'static) -> Self {
        let definition = tool.definition();
        self.tools.insert(definition.name.clone(), Arc::new(tool));
        self
    }

    /// Set the resource provider
    pub fn with_resources(mut self, provider: impl McpResourceProvider + 'static) -> Self {
        self.resource_provider = Some(Arc::new(provider));
        self
    }

    /// Set the prompt provider
    pub fn with_prompts(mut self, provider: impl McpPromptProvider + 'static) -> Self {
        self.prompt_provider = Some(Arc::new(provider));
        self
    }

    /// Set experimental capabilities
    pub fn with_experimental_capability(mut self, name: String, value: Value) -> Self {
        self.capabilities.experimental.insert(name, value);
        self
    }

    /// Build and start the MCP server
    pub async fn build(self) -> Result<HighLevelMcpServer> {
        let (notification_tx, notification_rx) = mpsc::unbounded_channel();

        let custom_handler = Arc::new(CustomMcpHandler {
            tools: Arc::new(RwLock::new(self.tools)),
            resource_provider: self.resource_provider,
            prompt_provider: self.prompt_provider,
            capabilities: self.capabilities,
            notification_sender: Some(notification_tx),
        });

        Ok(HighLevelMcpServer {
            custom_handler,
            notification_receiver: notification_rx,
        })
    }
}

/// High-level MCP Server
pub struct HighLevelMcpServer {
    custom_handler: Arc<CustomMcpHandler>,
    notification_receiver: mpsc::UnboundedReceiver<McpNotification>,
}

impl HighLevelMcpServer {
    /// Add a tool to the server after construction
    pub async fn add_tool(&self, tool: impl McpTool + 'static) -> Result<()> {
        let definition = tool.definition();
        self.custom_handler
            .tools
            .write()
            .await
            .insert(definition.name.clone(), Arc::new(tool));
        Ok(())
    }

    /// Get a notification sender for tools that need to send notifications
    pub fn notification_sender(&self) -> Option<mpsc::UnboundedSender<McpNotification>> {
        self.custom_handler.notification_sender.clone()
    }

    /// Start the server on the specified port
    pub async fn start(self, port: u16) -> Result<()> {
        // Create core server with our custom handler
        let mut core_server = McpServer::with_handler(self.custom_handler).await?;

        info!("🚀 Starting MCP server with custom tools on port {}", port);
        core_server.start(port).await
    }
}

/// Custom handler that implements the MCP protocol with user-provided tools, resources, and prompts
struct CustomMcpHandler {
    tools: Arc<RwLock<HashMap<String, Arc<dyn McpTool>>>>,
    resource_provider: Option<Arc<dyn McpResourceProvider>>,
    prompt_provider: Option<Arc<dyn McpPromptProvider>>,
    capabilities: ServerCapabilities,
    notification_sender: Option<mpsc::UnboundedSender<McpNotification>>,
}

impl CustomMcpHandler {
    /// Send a notification
    pub fn send_notification(&self, notification: McpNotification) -> Result<()> {
        if let Some(sender) = &self.notification_sender {
            sender.send(notification)?;
        }
        Ok(())
    }

    /// Get capabilities based on what's configured
    fn get_capabilities(&self) -> Value {
        let mut capabilities = json!({});

        // Add tools capability - we always have tools since they're added during build
        // This avoids the need for blocking_read() in async context
        capabilities["tools"] = json!({
            "listChanged": false
        });

        // Add resources capability if we have a resource provider
        if self.resource_provider.is_some() {
            capabilities["resources"] = json!({
                "listChanged": false
            });
        }

        // Add prompts capability if we have a prompt provider
        if self.prompt_provider.is_some() {
            capabilities["prompts"] = json!({
                "listChanged": false
            });
        }

        // Add experimental capabilities
        if !self.capabilities.experimental.is_empty() {
            capabilities["experimental"] = json!(self.capabilities.experimental);
        }

        capabilities
    }

    /// Handle tools/list request
    async fn handle_tools_list(&self) -> Result<Value> {
        let tools = self.tools.read().await;
        let tool_definitions: Vec<Value> = tools
            .values()
            .map(|tool| {
                let def = tool.definition();
                json!({
                    "name": def.name,
                    "description": def.description,
                    "inputSchema": def.input_schema,
                })
            })
            .collect();

        Ok(json!({
            "tools": tool_definitions
        }))
    }

    /// Handle tools/call request
    async fn handle_tool_call(&self, name: &str, arguments: Value) -> Result<Value> {
        let tools = self.tools.read().await;

        if let Some(tool) = tools.get(name) {
            let context = ToolContext {
                notification_sender: self.notification_sender.clone(),
            };
            let result = tool.execute(arguments, &context).await?;
            Ok(json!({
                "content": [
                    {
                        "type": "text",
                        "text": serde_json::to_string(&result)?
                    }
                ]
            }))
        } else {
            Err(anyhow::anyhow!("Unknown tool: {}", name))
        }
    }

    /// Handle resources/list request
    async fn handle_resources_list(&self) -> Result<Value> {
        if let Some(provider) = &self.resource_provider {
            let resources = provider.list_resources().await?;
            let resource_list: Vec<Value> = resources
                .into_iter()
                .map(|r| {
                    let mut resource = json!({
                        "uri": r.uri,
                        "name": r.name,
                    });
                    if let Some(desc) = r.description {
                        resource["description"] = json!(desc);
                    }
                    if let Some(mime) = r.mime_type {
                        resource["mimeType"] = json!(mime);
                    }
                    resource
                })
                .collect();

            Ok(json!({
                "resources": resource_list
            }))
        } else {
            Err(anyhow::anyhow!("Resources not supported"))
        }
    }

    /// Handle resources/read request
    async fn handle_resource_read(&self, uri: &str) -> Result<Value> {
        if let Some(provider) = &self.resource_provider {
            let content = provider.read_resource(uri).await?;
            Ok(json!({
                "contents": [
                    {
                        "uri": content.uri,
                        "mimeType": content.mime_type,
                        "text": content.content,
                    }
                ]
            }))
        } else {
            Err(anyhow::anyhow!("Resources not supported"))
        }
    }

    /// Handle prompts/list request
    async fn handle_prompts_list(&self) -> Result<Value> {
        if let Some(provider) = &self.prompt_provider {
            let prompts = provider.list_prompts().await?;
            let prompt_list: Vec<Value> = prompts
                .into_iter()
                .map(|p| {
                    let mut prompt = json!({
                        "name": p.name,
                    });
                    if let Some(desc) = p.description {
                        prompt["description"] = json!(desc);
                    }
                    if !p.arguments.is_empty() {
                        let args: Vec<Value> = p
                            .arguments
                            .into_iter()
                            .map(|a| {
                                let mut arg = json!({
                                    "name": a.name,
                                    "required": a.required,
                                });
                                if let Some(desc) = a.description {
                                    arg["description"] = json!(desc);
                                }
                                arg
                            })
                            .collect();
                        prompt["arguments"] = json!(args);
                    }
                    prompt
                })
                .collect();

            Ok(json!({
                "prompts": prompt_list
            }))
        } else {
            Err(anyhow::anyhow!("Prompts not supported"))
        }
    }

    /// Handle prompts/get request
    async fn handle_prompt_get(&self, name: &str, arguments: Option<Value>) -> Result<Value> {
        if let Some(provider) = &self.prompt_provider {
            let content = provider.get_prompt(name, arguments).await?;
            let messages: Vec<Value> = content
                .messages
                .into_iter()
                .map(|m| {
                    json!({
                        "role": m.role,
                        "content": {
                            "type": "text",
                            "text": m.content,
                        }
                    })
                })
                .collect();

            Ok(json!({
                "messages": messages
            }))
        } else {
            Err(anyhow::anyhow!("Prompts not supported"))
        }
    }
}

#[async_trait]
impl McpHandler for CustomMcpHandler {
    async fn initialize(&self, _params: Value, _context: &McpContext) -> Result<Value> {
        Ok(json!({
            "protocolVersion": "2025-06-18",
            "capabilities": self.get_capabilities(),
            "serverInfo": {
                "name": "toy-notes-server",
                "version": "0.1.0"
            }
        }))
    }

    async fn list_tools(&self, _context: &McpContext) -> Result<Vec<ToolDefinition>> {
        let tools = self.tools.read().await;
        let tool_definitions: Vec<ToolDefinition> = tools
            .values()
            .map(|tool| {
                let def = tool.definition();
                ToolDefinition {
                    name: def.name,
                    description: def.description,
                    input_schema: def.input_schema,
                }
            })
            .collect();
        Ok(tool_definitions)
    }

    async fn call_tool(
        &self,
        name: &str,
        arguments: Value,
        _context: &McpContext,
    ) -> Result<Value> {
        let tools = self.tools.read().await;

        if let Some(tool) = tools.get(name) {
            let context = ToolContext {
                notification_sender: self.notification_sender.clone(),
            };
            let result = tool.execute(arguments, &context).await?;
            Ok(json!({
                "content": [
                    {
                        "type": "text",
                        "text": serde_json::to_string(&result)?
                    }
                ]
            }))
        } else {
            Err(anyhow::anyhow!("Unknown tool: {}", name))
        }
    }

    async fn list_resources(&self, _context: &McpContext) -> Result<Vec<ResourceInfo>> {
        if let Some(provider) = &self.resource_provider {
            provider.list_resources().await
        } else {
            Ok(vec![])
        }
    }

    async fn read_resource(&self, uri: &str, _context: &McpContext) -> Result<ResourceContent> {
        if let Some(provider) = &self.resource_provider {
            provider.read_resource(uri).await
        } else {
            Err(anyhow::anyhow!("Resource not found: {}", uri))
        }
    }

    async fn list_prompts(&self, _context: &McpContext) -> Result<Vec<PromptInfo>> {
        if let Some(provider) = &self.prompt_provider {
            provider.list_prompts().await
        } else {
            Ok(vec![])
        }
    }

    async fn get_prompt(
        &self,
        name: &str,
        arguments: Option<Value>,
        _context: &McpContext,
    ) -> Result<PromptContent> {
        if let Some(provider) = &self.prompt_provider {
            provider.get_prompt(name, arguments).await
        } else {
            Err(anyhow::anyhow!("Prompt not found: {}", name))
        }
    }
}