rust-docs-mcp 0.1.1

MCP server providing comprehensive Rust crate analysis: documentation search, source code access, dependency trees, and module structure visualization with multi-source caching
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
use anyhow::{Context, Result};
use rmcp::schemars;
use rustdoc_types::{Crate, Id, Item, ItemEnum};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// Query interface for rustdoc JSON data
#[derive(Debug)]
pub struct DocQuery {
    crate_data: Crate,
}

/// Simplified item information for API responses
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct ItemInfo {
    pub id: String,
    pub name: String,
    pub kind: String,
    pub path: Vec<String>,
    pub docs: Option<String>,
    pub visibility: String,
}

/// Source location information
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct SourceLocation {
    pub filename: String,
    pub line_start: usize,
    pub column_start: usize,
    pub line_end: usize,
    pub column_end: usize,
}

/// Source code information for an item
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct SourceInfo {
    pub location: SourceLocation,
    pub code: String,
    pub context_lines: Option<usize>,
}

/// Detailed item information including signatures
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct DetailedItem {
    pub info: ItemInfo,
    pub signature: Option<String>,
    pub generics: Option<serde_json::Value>,
    pub fields: Option<Vec<ItemInfo>>,
    pub variants: Option<Vec<ItemInfo>>,
    pub methods: Option<Vec<ItemInfo>>,
    pub source_location: Option<SourceLocation>,
}

impl DocQuery {
    /// Create a new query interface for a crate's documentation
    pub fn new(crate_data: Crate) -> Self {
        Self { crate_data }
    }

    /// List all items in the crate, optionally filtered by kind
    pub fn list_items(&self, kind_filter: Option<&str>) -> Vec<ItemInfo> {
        let mut items = Vec::new();

        for (id, item) in &self.crate_data.index {
            if let Some(filter) = &kind_filter
                && self.get_item_kind_string(&item.inner) != *filter
            {
                continue;
            }

            if let Some(info) = self.item_to_info(id, item) {
                items.push(info);
            }
        }

        // Sort by path and name for consistent output
        items.sort_by(|a, b| a.path.cmp(&b.path).then_with(|| a.name.cmp(&b.name)));
        items
    }

    /// Search for items by name pattern
    pub fn search_items(&self, pattern: &str) -> Vec<ItemInfo> {
        let pattern_lower = pattern.to_lowercase();
        let mut items = Vec::new();

        for (id, item) in &self.crate_data.index {
            // First check if item has a direct name
            let item_name = if let Some(name) = &item.name {
                Some(name.clone())
            } else if let Some(path_summary) = self.crate_data.paths.get(id) {
                // Fall back to using the last component of the path
                path_summary.path.last().cloned()
            } else {
                None
            };

            if let Some(name) = item_name
                && name.to_lowercase().contains(&pattern_lower)
                && let Some(info) = self.item_to_info(id, item)
            {
                items.push(info);
            }
        }

        items.sort_by(|a, b| {
            // Sort by relevance (exact match first, then prefix match, then contains)
            let a_exact = a.name.to_lowercase() == pattern_lower;
            let b_exact = b.name.to_lowercase() == pattern_lower;
            let a_prefix = a.name.to_lowercase().starts_with(&pattern_lower);
            let b_prefix = b.name.to_lowercase().starts_with(&pattern_lower);

            b_exact
                .cmp(&a_exact)
                .then_with(|| b_prefix.cmp(&a_prefix))
                .then_with(|| a.name.len().cmp(&b.name.len()))
                .then_with(|| a.name.cmp(&b.name))
        });

        items
    }

    /// Get detailed information about a specific item by ID
    pub fn get_item_details(&self, item_id: u32) -> Result<DetailedItem> {
        let id = Id(item_id);
        let item = self.crate_data.index.get(&id).context("Item not found")?;

        let info = self
            .item_to_info(&id, item)
            .context("Failed to convert item to info")?;

        let mut details = DetailedItem {
            info,
            signature: self.get_item_signature(item),
            generics: None,
            fields: None,
            variants: None,
            methods: None,
            source_location: self.get_item_source_location(item),
        };

        // Add type-specific information
        match &item.inner {
            ItemEnum::Struct(s) => {
                details.generics = serde_json::to_value(&s.generics).ok();
                details.fields = Some(self.get_struct_fields(s));
            }
            ItemEnum::Enum(e) => {
                details.generics = serde_json::to_value(&e.generics).ok();
                details.variants = Some(self.get_enum_variants(e));
            }
            ItemEnum::Trait(t) => {
                details.generics = serde_json::to_value(&t.generics).ok();
                details.methods = Some(self.get_trait_items(&t.items));
            }
            ItemEnum::Impl(i) => {
                details.generics = serde_json::to_value(&i.generics).ok();
                details.methods = Some(self.get_impl_items(&i.items));
            }
            ItemEnum::Function(f) => {
                details.generics = serde_json::to_value(&f.generics).ok();
            }
            _ => {}
        }

        Ok(details)
    }

    /// Get documentation for a specific item
    pub fn get_item_docs(&self, item_id: u32) -> Result<Option<String>> {
        let id = Id(item_id);
        let item = self.crate_data.index.get(&id).context("Item not found")?;

        Ok(item.docs.clone())
    }

    /// Helper to convert an Item to ItemInfo
    fn item_to_info(&self, id: &Id, item: &Item) -> Option<ItemInfo> {
        // Get name from item or from paths
        let name = if let Some(name) = &item.name {
            name.clone()
        } else if let Some(path_summary) = self.crate_data.paths.get(id) {
            path_summary.path.last()?.clone()
        } else {
            return None;
        };

        let kind = self.get_item_kind_string(&item.inner);
        let path = self.get_item_path(id);
        let visibility = self.get_visibility_string(&item.visibility);

        Some(ItemInfo {
            id: id.0.to_string(),
            name,
            kind,
            path,
            docs: item.docs.clone(),
            visibility,
        })
    }

    /// Get the kind of an item as a string
    fn get_item_kind_string(&self, inner: &ItemEnum) -> String {
        use ItemEnum::*;
        match inner {
            Module(_) => "module",
            Struct(_) => "struct",
            Enum(_) => "enum",
            Function(_) => "function",
            Trait(_) => "trait",
            Impl(_) => "impl",
            TypeAlias(_) => "type_alias",
            Constant { .. } => "constant",
            Static(_) => "static",
            Macro(_) => "macro",
            ExternCrate { .. } => "extern_crate",
            Use(_) => "use",
            Union(_) => "union",
            StructField(_) => "field",
            Variant(_) => "variant",
            TraitAlias(_) => "trait_alias",
            ProcMacro(_) => "proc_macro",
            Primitive(_) => "primitive",
            AssocConst { .. } => "assoc_const",
            AssocType { .. } => "assoc_type",
            ExternType => "extern_type",
        }
        .to_string()
    }

    /// Get the full path of an item
    fn get_item_path(&self, id: &Id) -> Vec<String> {
        if let Some(summary) = self.crate_data.paths.get(id) {
            summary.path.clone()
        } else {
            Vec::new()
        }
    }

    /// Get visibility as a string
    fn get_visibility_string(&self, vis: &rustdoc_types::Visibility) -> String {
        use rustdoc_types::Visibility::*;
        match vis {
            Public => "public".to_string(),
            Default => "default".to_string(),
            Crate => "crate".to_string(),
            Restricted { parent, .. } => format!("restricted({})", parent.0),
        }
    }

    /// Get a signature representation for an item
    fn get_item_signature(&self, item: &Item) -> Option<String> {
        use ItemEnum::*;
        match &item.inner {
            Function(f) => {
                let name = item.name.as_ref()?;
                let generics = self.format_generics(&f.generics);
                let params = self.format_fn_params(&f.sig.inputs);
                let output = self.format_fn_output(&f.sig.output);
                Some(format!("fn {name}{generics}{params}{output}"))
            }
            _ => None,
        }
    }

    /// Format generic parameters
    fn format_generics(&self, generics: &rustdoc_types::Generics) -> String {
        // Simplified generic formatting
        if generics.params.is_empty() {
            String::new()
        } else {
            "<...>".to_string()
        }
    }

    /// Format function parameters
    fn format_fn_params(&self, params: &[(String, rustdoc_types::Type)]) -> String {
        let param_strs: Vec<String> = params.iter().map(|(name, _)| name.clone()).collect();
        format!("({})", param_strs.join(", "))
    }

    /// Format function output
    fn format_fn_output(&self, output: &Option<rustdoc_types::Type>) -> String {
        output
            .as_ref()
            .map(|_| " -> ...".to_string())
            .unwrap_or_default()
    }

    /// Get struct fields as ItemInfo
    fn get_struct_fields(&self, s: &rustdoc_types::Struct) -> Vec<ItemInfo> {
        use rustdoc_types::StructKind;
        match &s.kind {
            StructKind::Unit => vec![],
            StructKind::Tuple(fields) => fields
                .iter()
                .enumerate()
                .filter_map(|(i, field_id)| {
                    if let Some(field_id) = field_id {
                        let item = self.crate_data.index.get(field_id)?;
                        let mut info = self.item_to_info(field_id, item)?;
                        if info.name.is_empty() {
                            info.name = i.to_string();
                        }
                        Some(info)
                    } else {
                        Some(ItemInfo {
                            id: String::new(),
                            name: format!("(field {i} stripped)"),
                            kind: "field".to_string(),
                            path: Vec::new(),
                            docs: None,
                            visibility: "private".to_string(),
                        })
                    }
                })
                .collect(),
            StructKind::Plain {
                fields,
                has_stripped_fields,
            } => {
                let mut field_infos: Vec<ItemInfo> = fields
                    .iter()
                    .filter_map(|field_id| {
                        let item = self.crate_data.index.get(field_id)?;
                        self.item_to_info(field_id, item)
                    })
                    .collect();

                if *has_stripped_fields {
                    field_infos.push(ItemInfo {
                        id: String::new(),
                        name: "(some fields stripped)".to_string(),
                        kind: "note".to_string(),
                        path: Vec::new(),
                        docs: None,
                        visibility: "private".to_string(),
                    });
                }

                field_infos
            }
        }
    }

    /// Get enum variants as ItemInfo
    fn get_enum_variants(&self, e: &rustdoc_types::Enum) -> Vec<ItemInfo> {
        let mut variant_infos: Vec<ItemInfo> = e
            .variants
            .iter()
            .filter_map(|variant_id| {
                let item = self.crate_data.index.get(variant_id)?;
                self.item_to_info(variant_id, item)
            })
            .collect();

        if e.has_stripped_variants {
            variant_infos.push(ItemInfo {
                id: String::new(),
                name: "(some variants stripped)".to_string(),
                kind: "note".to_string(),
                path: Vec::new(),
                docs: None,
                visibility: "private".to_string(),
            });
        }

        variant_infos
    }

    /// Get trait items as ItemInfo
    fn get_trait_items(&self, items: &[Id]) -> Vec<ItemInfo> {
        items
            .iter()
            .filter_map(|item_id| {
                let item = self.crate_data.index.get(item_id)?;
                self.item_to_info(item_id, item)
            })
            .collect()
    }

    /// Get impl items as ItemInfo
    fn get_impl_items(&self, items: &[Id]) -> Vec<ItemInfo> {
        items
            .iter()
            .filter_map(|item_id| {
                let item = self.crate_data.index.get(item_id)?;
                self.item_to_info(item_id, item)
            })
            .collect()
    }

    /// Get source location information for an item
    fn get_item_source_location(&self, item: &Item) -> Option<SourceLocation> {
        let span = item.span.as_ref()?;
        Some(SourceLocation {
            filename: span.filename.to_string_lossy().to_string(),
            line_start: span.begin.0,
            column_start: span.begin.1,
            line_end: span.end.0,
            column_end: span.end.1,
        })
    }

    /// Get source code for a specific item by ID
    pub fn get_item_source(
        &self,
        item_id: u32,
        base_path: &std::path::Path,
        context_lines: usize,
    ) -> Result<SourceInfo> {
        let id = Id(item_id);
        let item = self.crate_data.index.get(&id).context("Item not found")?;

        let span = item.span.as_ref().context("Item has no source span")?;
        let source_path = base_path.join(&span.filename);

        if !source_path.exists() {
            anyhow::bail!("Source file not found: {}", source_path.display());
        }

        let content = std::fs::read_to_string(&source_path)
            .with_context(|| format!("Failed to read source file: {}", source_path.display()))?;

        let lines: Vec<&str> = content.lines().collect();

        // Calculate line range with context
        let start_line = span.begin.0.saturating_sub(1).saturating_sub(context_lines);
        let end_line = std::cmp::min(span.end.0 + context_lines, lines.len());

        // Extract the relevant lines
        let code_lines: Vec<String> = lines[start_line..end_line]
            .iter()
            .map(|line| line.to_string())
            .collect();

        Ok(SourceInfo {
            location: SourceLocation {
                filename: span.filename.to_string_lossy().to_string(),
                line_start: span.begin.0,
                column_start: span.begin.1,
                line_end: span.end.0,
                column_end: span.end.1,
            },
            code: code_lines.join("\n"),
            context_lines: Some(context_lines),
        })
    }
}