fastmcp-rust 0.7.0

Asupersync-based MCP framework for Rust
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
//! Example: Notes Server
//!
//! An in-memory note-taking MCP server demonstrating:
//! - CRUD operations (Create, Read, Update, Delete)
//! - State management with thread-safe collections
//! - Complex data structures
//! - Dynamic resource URIs
//! - Search and filtering
//!
//! Run with:
//! ```bash
//! cargo run -p fastmcp-rust --example notes_server
//! ```
//!
//! Test with MCP Inspector:
//! ```bash
//! npx @modelcontextprotocol/inspector cargo run -p fastmcp-rust --example notes_server
//! ```

#![allow(clippy::needless_pass_by_value)]
#![allow(clippy::cast_sign_loss)]

use std::collections::HashMap;
use std::sync::Mutex;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};

use fastmcp_rust::modern::ServerBuilder;
use fastmcp_rust::prelude::*;

// ============================================================================
// Data Structures
// ============================================================================

/// A single note.
#[derive(Clone, serde::Serialize, serde::Deserialize)]
struct Note {
    id: u64,
    title: String,
    content: String,
    tags: Vec<String>,
    created_at: u64,
    updated_at: u64,
}

/// Global note storage.
static NOTE_ID_COUNTER: AtomicU64 = AtomicU64::new(1);
static NOTES: Mutex<Option<HashMap<u64, Note>>> = Mutex::new(None);

fn get_notes() -> std::sync::MutexGuard<'static, Option<HashMap<u64, Note>>> {
    let mut guard = NOTES.lock().unwrap();
    if guard.is_none() {
        *guard = Some(HashMap::new());
    }
    guard
}

fn current_timestamp() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

// ============================================================================
// CRUD Tools
// ============================================================================

/// Create a new note.
#[tool(description = "Create a new note with title, content, and optional tags (comma-separated)")]
fn create_note(_ctx: &McpContext, title: String, content: String, tags: String) -> String {
    let id = NOTE_ID_COUNTER.fetch_add(1, Ordering::SeqCst);
    let timestamp = current_timestamp();

    let tag_list: Vec<String> = if tags.is_empty() {
        vec![]
    } else {
        tags.split(',').map(|s| s.trim().to_string()).collect()
    };

    let note = Note {
        id,
        title,
        content,
        tags: tag_list,
        created_at: timestamp,
        updated_at: timestamp,
    };

    let mut notes = get_notes();
    notes.as_mut().unwrap().insert(id, note.clone());

    serde_json::to_string_pretty(&note).unwrap_or_else(|_| format!("Created note with ID: {id}"))
}

/// Get a note by ID.
#[tool(description = "Retrieve a note by its ID")]
fn get_note(_ctx: &McpContext, id: i64) -> String {
    if id < 0 {
        return "Error: ID must be non-negative".to_string();
    }

    let notes = get_notes();
    match notes.as_ref().unwrap().get(&(id as u64)) {
        Some(note) => {
            serde_json::to_string_pretty(note).unwrap_or_else(|_| "Error serializing note".into())
        }
        None => format!("Error: Note with ID {id} not found"),
    }
}

/// Update an existing note.
#[tool(description = "Update a note's title, content, or tags. Empty values keep existing data.")]
fn update_note(_ctx: &McpContext, id: i64, title: String, content: String, tags: String) -> String {
    if id < 0 {
        return "Error: ID must be non-negative".to_string();
    }

    let mut notes = get_notes();
    let notes_map = notes.as_mut().unwrap();

    match notes_map.get_mut(&(id as u64)) {
        Some(note) => {
            if !title.is_empty() {
                note.title = title;
            }
            if !content.is_empty() {
                note.content = content;
            }
            if !tags.is_empty() {
                note.tags = tags.split(',').map(|s| s.trim().to_string()).collect();
            }
            note.updated_at = current_timestamp();

            serde_json::to_string_pretty(note).unwrap_or_else(|_| format!("Updated note {id}"))
        }
        None => format!("Error: Note with ID {id} not found"),
    }
}

/// Delete a note by ID.
#[tool(description = "Delete a note by its ID")]
fn delete_note(_ctx: &McpContext, id: i64) -> String {
    if id < 0 {
        return "Error: ID must be non-negative".to_string();
    }

    let mut notes = get_notes();
    match notes.as_mut().unwrap().remove(&(id as u64)) {
        Some(_) => format!("Successfully deleted note {id}"),
        None => format!("Error: Note with ID {id} not found"),
    }
}

// ============================================================================
// Query Tools
// ============================================================================

/// List all notes.
#[tool(description = "List all notes (returns ID, title, and tags for each)")]
fn list_notes(_ctx: &McpContext) -> String {
    let notes = get_notes();
    let notes_map = notes.as_ref().unwrap();

    if notes_map.is_empty() {
        return "No notes found".to_string();
    }

    let summaries: Vec<serde_json::Value> = notes_map
        .values()
        .map(|note| {
            serde_json::json!({
                "id": note.id,
                "title": note.title,
                "tags": note.tags,
                "updated_at": note.updated_at
            })
        })
        .collect();

    serde_json::to_string_pretty(&summaries).unwrap_or_else(|_| "Error listing notes".into())
}

/// Search notes by keyword.
#[tool(description = "Search notes by keyword in title or content")]
fn search_notes(_ctx: &McpContext, query: String) -> String {
    let query_lower = query.to_lowercase();
    let notes = get_notes();
    let notes_map = notes.as_ref().unwrap();

    let matches: Vec<&Note> = notes_map
        .values()
        .filter(|note| {
            note.title.to_lowercase().contains(&query_lower)
                || note.content.to_lowercase().contains(&query_lower)
        })
        .collect();

    if matches.is_empty() {
        return format!("No notes found matching '{query}'");
    }

    serde_json::to_string_pretty(&matches).unwrap_or_else(|_| "Error searching notes".into())
}

/// Find notes by tag.
#[tool(description = "Find all notes with a specific tag")]
fn notes_by_tag(_ctx: &McpContext, tag: String) -> String {
    let tag_lower = tag.to_lowercase();
    let notes = get_notes();
    let notes_map = notes.as_ref().unwrap();

    let matches: Vec<&Note> = notes_map
        .values()
        .filter(|note| note.tags.iter().any(|t| t.to_lowercase() == tag_lower))
        .collect();

    if matches.is_empty() {
        return format!("No notes found with tag '{tag}'");
    }

    serde_json::to_string_pretty(&matches).unwrap_or_else(|_| "Error finding notes by tag".into())
}

/// Get all unique tags.
#[tool(description = "List all unique tags across all notes")]
fn list_tags(_ctx: &McpContext) -> String {
    let notes = get_notes();
    let notes_map = notes.as_ref().unwrap();

    let mut all_tags: Vec<String> = notes_map
        .values()
        .flat_map(|note| note.tags.iter().cloned())
        .collect();

    all_tags.sort();
    all_tags.dedup();

    if all_tags.is_empty() {
        return "No tags found".to_string();
    }

    serde_json::to_string_pretty(&all_tags).unwrap_or_else(|_| "Error listing tags".into())
}

/// Get statistics about the notes collection.
#[tool(description = "Get statistics about the notes collection")]
fn notes_stats(_ctx: &McpContext) -> String {
    let notes = get_notes();
    let notes_map = notes.as_ref().unwrap();

    let total_notes = notes_map.len();
    let total_content_length: usize = notes_map.values().map(|n| n.content.len()).sum();

    let mut tag_counts: HashMap<String, usize> = HashMap::new();
    for note in notes_map.values() {
        for tag in &note.tags {
            *tag_counts.entry(tag.clone()).or_insert(0) += 1;
        }
    }

    let stats = serde_json::json!({
        "total_notes": total_notes,
        "total_content_length": total_content_length,
        "average_content_length": total_content_length.checked_div(total_notes).unwrap_or(0),
        "unique_tags": tag_counts.len(),
        "tag_counts": tag_counts
    });

    serde_json::to_string_pretty(&stats).unwrap_or_else(|_| "Error computing stats".into())
}

// ============================================================================
// Resources
// ============================================================================

/// Returns help documentation.
#[resource(
    uri = "notes://help",
    name = "Notes Help",
    description = "Documentation for the notes server"
)]
fn notes_help(_ctx: &McpContext) -> String {
    r#"{
    "description": "An in-memory note-taking server",
    "tools": {
        "create_note": "Create a new note with title, content, and optional tags",
        "get_note": "Retrieve a specific note by ID",
        "update_note": "Update an existing note (empty fields preserve existing values)",
        "delete_note": "Delete a note by ID",
        "list_notes": "List all notes with summaries",
        "search_notes": "Search notes by keyword in title or content",
        "notes_by_tag": "Find notes with a specific tag",
        "list_tags": "List all unique tags",
        "notes_stats": "Get collection statistics"
    },
    "example_workflow": [
        "1. Create a note: create_note('Shopping List', 'Milk, Eggs, Bread', 'shopping,household')",
        "2. List all notes: list_notes()",
        "3. Search: search_notes('milk')",
        "4. Filter by tag: notes_by_tag('shopping')",
        "5. Update: update_note(1, '', 'Milk, Eggs, Bread, Butter', '')",
        "6. Delete: delete_note(1)"
    ]
}"#
    .to_string()
}

/// Returns sample data for demo purposes.
#[resource(
    uri = "notes://samples",
    name = "Sample Notes",
    description = "Pre-populated sample notes for demonstration"
)]
fn sample_notes(_ctx: &McpContext) -> String {
    r#"[
    {
        "title": "Meeting Notes",
        "content": "Discussed Q1 roadmap. Key decisions: prioritize mobile app.",
        "tags": ["work", "meetings"]
    },
    {
        "title": "Book Recommendations",
        "content": "1. The Pragmatic Programmer\n2. Clean Code\n3. Design Patterns",
        "tags": ["books", "learning"]
    },
    {
        "title": "Recipe: Pasta",
        "content": "Ingredients: pasta, olive oil, garlic, parmesan.\nBoil pasta, sauté garlic in oil, combine with cheese.",
        "tags": ["recipes", "cooking"]
    }
]"#
    .to_string()
}

// ============================================================================
// Prompts
// ============================================================================

/// A prompt for organizing notes.
#[prompt(description = "Generate suggestions for organizing notes")]
fn organize_notes(_ctx: &McpContext, current_tags: String) -> Vec<PromptMessage> {
    vec![PromptMessage {
        role: Role::User,
        content: Content::Text {
            text: format!(
                "I have a note-taking system with the following tags: {current_tags}\n\n\
                 Please suggest:\n\
                 1. A better tag taxonomy or hierarchy\n\
                 2. Tags that could be merged or split\n\
                 3. New tags that might be useful\n\
                 4. Tips for maintaining note organization"
            ),
        },
    }]
}

/// A prompt for summarizing notes.
#[prompt(description = "Generate a summary of note contents")]
fn summarize_notes(_ctx: &McpContext, notes_content: String) -> Vec<PromptMessage> {
    vec![PromptMessage {
        role: Role::User,
        content: Content::Text {
            text: format!(
                "Please summarize the following notes into key points and action items:\n\n{notes_content}"
            ),
        },
    }]
}

// ============================================================================
// Main
// ============================================================================

fn main() {
    ServerBuilder::new("notes-server", "1.0.0")
        // CRUD operations
        .tool(CreateNote)
        .tool(GetNote)
        .tool(UpdateNote)
        .tool(DeleteNote)
        // Query operations
        .tool(ListNotes)
        .tool(SearchNotes)
        .tool(NotesByTag)
        .tool(ListTags)
        .tool(NotesStats)
        // Resources
        .resource(NotesHelpResource)
        .resource(SampleNotesResource)
        // Prompts
        .prompt(OrganizeNotesPrompt)
        .prompt(SummarizeNotesPrompt)
        // Config
        .request_timeout(30)
        .instructions(
            "A note-taking server with full CRUD support. Start with 'create_note' to add notes, \
             use 'list_notes' to see all, 'search_notes' to find specific content, and \
             'notes_by_tag' to filter by tag. Check 'notes://help' resource for full documentation.",
        )
        .build()
        .run_stdio();
}