crates-docs 1.0.0

High-performance Rust crate documentation query MCP server, supports Stdio/HTTP/SSE transport and OAuth authentication
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
//! Cache key generation and validation for document cache

use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

/// Check if a byte is a valid crate name character
#[inline]
fn is_valid_crate_name_char(b: u8) -> bool {
    b.is_ascii_alphanumeric() || b == b'_' || b == b'-'
}

/// Check if a byte is a valid item path character
#[inline]
fn is_valid_item_path_char(b: u8) -> bool {
    b.is_ascii_alphanumeric() || b == b'_' || b == b'-' || b == b':'
}

/// Check if crate name is valid (non-empty and all valid chars)
#[inline]
fn is_valid_crate_name(name: &str) -> bool {
    !name.is_empty() && name.bytes().all(is_valid_crate_name_char)
}

/// Check if item path is valid (non-empty, valid chars, colons only as `::`)
///
/// Rust paths separate segments with `::` (a double colon). A lone `:` is not a
/// valid path separator, and allowing it would make the item cache key
/// ambiguous: `item:a:1.0:Serialize` could mean either path `"1.0:Serialize"`
/// (no version) or version `"1.0"` + path `"Serialize"`. Rejecting single
/// colons routes such inputs through the hashed-key branch, keeping keys
/// injective and matching the tool-level `validate_item_path` guard.
#[inline]
fn is_valid_item_path(path: &str) -> bool {
    !path.is_empty()
        && path.bytes().all(is_valid_item_path_char)
        && !path.replace("::", "").contains(':')
}

/// Reversibly escape `:` and `%` in a free-form cache-key segment.
///
/// Cache keys are colon-delimited. Free-form segments such as the search query
/// or sort string may themselves contain `:`, which would otherwise let two
/// distinct (query, sort, limit) inputs map to the same key (for example
/// `query="a", sort="b:c"` and `query="a:b", sort="c"` both yield
/// `search:a:b:c:{limit}`). Percent-encoding `%` first and then `:` keeps the
/// mapping injective while leaving colon-free inputs unchanged.
#[inline]
fn escape_key_segment(segment: &str) -> String {
    if segment.contains('%') || segment.contains(':') {
        segment.replace('%', "%25").replace(':', "%3a")
    } else {
        segment.to_string()
    }
}

/// Normalize an optional version string for cache-key generation.
///
/// Trims surrounding whitespace and lowercases the version (versions are
/// treated case-insensitively). An empty/whitespace-only version and the
/// `"latest"` sentinel both normalize to `None` so they share a cache entry
/// with an unversioned (`None`) lookup. This mirrors the URL builders: for
/// std crates `rust_lang_docs_base` maps `None`/`"latest"` to the same
/// unversioned docs, the non-std crate overview at `docs.rs/<crate>/` and
/// `docs.rs/<crate>/latest/` resolve to identical content, and the item URL
/// builder maps `None` to `latest`. Keying them separately would duplicate
/// entries and trigger redundant network fetches.
#[inline]
fn normalize_cache_version(version: Option<&str>) -> Option<String> {
    version
        .map(|v| v.trim().to_lowercase())
        .filter(|v| !v.is_empty() && v != "latest")
}

/// Cache key generator for document cache
pub struct CacheKeyGenerator;

impl CacheKeyGenerator {
    /// Build a raw crate HTML cache key with normalization.
    ///
    /// This key stores the fetched docs.rs HTML artifact shared across
    /// markdown, text, and html responses for the same crate lookup.
    ///
    /// Key format: `htmlraw:crate:{name}` or `htmlraw:crate:{name}:{version}`
    ///
    /// The `htmlraw:` namespace prefix keeps raw HTML artifacts in a separate
    /// keyspace from rendered documentation keys (`crate:...`). Without it, a
    /// rendered lookup for version literal `"html"` (e.g.
    /// `crate_cache_key("serde", Some("html"))` => `crate:serde:html`) would
    /// collide with the HTML artifact key for `crate_html_cache_key("serde",
    /// None)`, cross-contaminating rendered text and raw HTML.
    #[must_use]
    pub fn crate_html_cache_key(crate_name: &str, version: Option<&str>) -> String {
        let base_key = Self::crate_cache_key(crate_name, version);
        format!("htmlraw:{base_key}")
    }

    /// Build crate cache key with normalization
    ///
    /// # Normalization rules
    ///
    /// - `crate_name`: lowercase, trimmed
    ///   (crate names are case-insensitive on crates.io)
    /// - `version`: lowercase, trimmed
    /// - Invalid characters in `crate_name` (non-alphanumeric, non-underscore, non-hyphen)
    ///   will result in a hashed key to prevent injection
    #[must_use]
    pub fn crate_cache_key(crate_name: &str, version: Option<&str>) -> String {
        // Inline normalization to avoid intermediate allocations
        let normalized_name = crate_name.trim().to_lowercase();
        let normalized_ver = normalize_cache_version(version);

        if !is_valid_crate_name(&normalized_name) {
            let mut hasher = DefaultHasher::new();
            normalized_name.hash(&mut hasher);
            let hash = hasher.finish();
            return match normalized_ver {
                Some(ver) => format!("crate:hash:{hash}:{ver}"),
                None => format!("crate:hash:{hash}"),
            };
        }

        match normalized_ver {
            Some(ver) => format!("crate:{normalized_name}:{ver}"),
            None => format!("crate:{normalized_name}"),
        }
    }

    /// Build search cache key with normalization
    ///
    /// # Normalization rules
    ///
    /// - query: lowercase, trimmed (search is case-insensitive)
    /// - sort: lowercase, trimmed
    #[must_use]
    pub fn search_cache_key(query: &str, limit: u32, sort: Option<&str>) -> String {
        let normalized_query = escape_key_segment(&query.trim().to_lowercase());
        let normalized_sort =
            escape_key_segment(&sort.unwrap_or("relevance").trim().to_lowercase());
        format!("search:{normalized_query}:{normalized_sort}:{limit}")
    }

    /// Build item cache key with normalization
    ///
    /// # Normalization rules
    ///
    /// - `crate_name`: lowercase, trimmed
    ///   (crate names are case-insensitive on crates.io)
    /// - `item_path`: trimmed but case-sensitive (Rust paths are case-sensitive)
    /// - `version`: lowercase, trimmed
    #[must_use]
    pub fn item_cache_key(crate_name: &str, item_path: &str, version: Option<&str>) -> String {
        let normalized_name = crate_name.trim().to_lowercase();
        let normalized_path = item_path.trim();
        let normalized_ver = normalize_cache_version(version);

        if !is_valid_crate_name(&normalized_name) || !is_valid_item_path(normalized_path) {
            let mut hasher = DefaultHasher::new();
            normalized_name.hash(&mut hasher);
            normalized_path.hash(&mut hasher);
            let hash = hasher.finish();
            return match normalized_ver {
                Some(ver) => {
                    format!("item:{normalized_name}:{ver}:hash:{hash}")
                }
                None => format!("item:{normalized_name}:hash:{hash}"),
            };
        }

        match normalized_ver {
            Some(ver) => {
                format!("item:{normalized_name}:{ver}:{normalized_path}")
            }
            None => format!("item:{normalized_name}:{normalized_path}"),
        }
    }

    /// Build a raw item HTML cache key with normalization.
    ///
    /// This key stores the fetched docs.rs search-result HTML artifact shared
    /// across markdown, text, and html responses for the same item lookup.
    ///
    /// Key format: `htmlraw:item:{crate}:{path}` (see [`Self::crate_html_cache_key`]
    /// for why the `htmlraw:` namespace is used to avoid collisions).
    #[must_use]
    pub fn item_html_cache_key(crate_name: &str, item_path: &str, version: Option<&str>) -> String {
        let base_key = Self::item_cache_key(crate_name, item_path, version);
        format!("htmlraw:{base_key}")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cache_key_generation() {
        assert_eq!(
            CacheKeyGenerator::crate_cache_key("serde", None),
            "crate:serde"
        );
        assert_eq!(
            CacheKeyGenerator::crate_cache_key("serde", Some("1.0")),
            "crate:serde:1.0"
        );
        assert_eq!(
            CacheKeyGenerator::crate_html_cache_key("serde", Some("1.0")),
            "htmlraw:crate:serde:1.0"
        );

        assert_eq!(
            CacheKeyGenerator::search_cache_key("web framework", 10, None),
            "search:web framework:relevance:10"
        );
        assert_eq!(
            CacheKeyGenerator::search_cache_key("web framework", 10, Some("downloads")),
            "search:web framework:downloads:10"
        );

        assert_eq!(
            CacheKeyGenerator::item_cache_key("serde", "Serialize", None),
            "item:serde:Serialize"
        );
        assert_eq!(
            CacheKeyGenerator::item_cache_key("serde", "Serialize", Some("1.0")),
            "item:serde:1.0:Serialize"
        );
        assert_eq!(
            CacheKeyGenerator::item_html_cache_key("serde", "Serialize", Some("1.0")),
            "htmlraw:item:serde:1.0:Serialize"
        );
    }

    #[test]
    fn test_cache_key_normalization_case_insensitivity() {
        assert_eq!(
            CacheKeyGenerator::crate_cache_key("Serde", None),
            CacheKeyGenerator::crate_cache_key("serde", None)
        );
        assert_eq!(
            CacheKeyGenerator::crate_cache_key("SERDE", None),
            CacheKeyGenerator::crate_cache_key("serde", None)
        );

        assert_eq!(
            CacheKeyGenerator::crate_cache_key("Tokio", Some("1.0")),
            CacheKeyGenerator::crate_cache_key("tokio", Some("1.0"))
        );

        assert_eq!(
            CacheKeyGenerator::search_cache_key("Web Framework", 10, Some("Relevance")),
            CacheKeyGenerator::search_cache_key("web framework", 10, Some("relevance"))
        );

        assert_eq!(
            CacheKeyGenerator::item_cache_key("Serde", "Serialize", None),
            CacheKeyGenerator::item_cache_key("serde", "Serialize", None)
        );
    }

    #[test]
    fn test_cache_key_normalization_whitespace() {
        assert_eq!(
            CacheKeyGenerator::crate_cache_key("serde", Some(" 1.0 ")),
            "crate:serde:1.0"
        );

        assert_eq!(
            CacheKeyGenerator::search_cache_key("  web framework  ", 10, Some(" downloads ")),
            "search:web framework:downloads:10"
        );

        assert_eq!(
            CacheKeyGenerator::item_cache_key("serde", "  Serialize  ", None),
            "item:serde:Serialize"
        );
    }

    #[test]
    fn test_html_artifact_keys_do_not_collide_with_rendered_keys() {
        // A rendered lookup for the (pathological) version literal "html" must
        // not collide with the raw HTML artifact keyspace.
        let rendered = CacheKeyGenerator::crate_cache_key("serde", Some("html"));
        let artifact = CacheKeyGenerator::crate_html_cache_key("serde", None);
        assert_ne!(rendered, artifact);
        assert_eq!(rendered, "crate:serde:html");
        assert_eq!(artifact, "htmlraw:crate:serde");

        let rendered_item = CacheKeyGenerator::item_cache_key("serde", "Serialize", Some("html"));
        let artifact_item = CacheKeyGenerator::item_html_cache_key("serde", "Serialize", None);
        assert_ne!(rendered_item, artifact_item);
    }

    #[test]
    fn test_cache_key_normalization_version_case() {
        assert_eq!(
            CacheKeyGenerator::crate_cache_key("serde", Some("1.0-RC1")),
            "crate:serde:1.0-rc1"
        );
        assert_eq!(
            CacheKeyGenerator::item_cache_key("serde", "Serialize", Some("V1.0")),
            "item:serde:v1.0:Serialize"
        );
    }

    #[test]
    fn test_cache_key_injection_prevention() {
        let malicious_key = CacheKeyGenerator::crate_cache_key("serde:malicious", None);
        assert!(malicious_key.starts_with("crate:hash:"));
        assert!(!malicious_key.contains("serde:malicious"));

        let malicious_key_with_version =
            CacheKeyGenerator::crate_cache_key("crate:evil", Some("1.0"));
        assert!(malicious_key_with_version.starts_with("crate:hash:"));
        assert!(!malicious_key_with_version.contains("crate:evil"));

        let valid_key = CacheKeyGenerator::crate_cache_key("serde-json", None);
        assert_eq!(valid_key, "crate:serde-json");

        let valid_key_underscore = CacheKeyGenerator::crate_cache_key("my_crate", None);
        assert_eq!(valid_key_underscore, "crate:my_crate");
    }

    #[test]
    fn test_search_cache_key_no_colon_collision() {
        // Two distinct (query, sort) inputs that previously collapsed to the
        // same colon-delimited key must now produce distinct keys.
        let a = CacheKeyGenerator::search_cache_key("a", 5, Some("b:c"));
        let b = CacheKeyGenerator::search_cache_key("a:b", 5, Some("c"));
        assert_ne!(a, b);

        // Colon-free inputs stay human-readable and unescaped.
        assert_eq!(
            CacheKeyGenerator::search_cache_key("web framework", 10, Some("downloads")),
            "search:web framework:downloads:10"
        );

        // A literal percent in the query must not be confused with an escape.
        let pct = CacheKeyGenerator::search_cache_key("100%", 10, None);
        let escaped = CacheKeyGenerator::search_cache_key("100%3a", 10, None);
        assert_ne!(pct, escaped);
    }

    #[test]
    fn test_item_cache_key_no_version_path_collision() {
        // path "1.0:Serialize" (no version) must NOT collide with
        // version "1.0" + path "Serialize".
        let a = CacheKeyGenerator::item_cache_key("serde", "1.0:Serialize", None);
        let b = CacheKeyGenerator::item_cache_key("serde", "Serialize", Some("1.0"));
        assert_ne!(a, b);
        // Legitimate `::` paths stay readable/unhashed.
        assert_eq!(
            CacheKeyGenerator::item_cache_key("serde", "de::Deserialize", None),
            "item:serde:de::Deserialize"
        );
    }

    #[test]
    fn test_cache_key_version_latest_normalizes_to_none() {
        // `None`, `Some("latest")`, and whitespace/empty versions all fetch the
        // same content, so they must share a single cache key.
        let none = CacheKeyGenerator::crate_cache_key("serde", None);
        assert_eq!(none, "crate:serde");
        assert_eq!(
            CacheKeyGenerator::crate_cache_key("serde", Some("latest")),
            none
        );
        assert_eq!(
            CacheKeyGenerator::crate_cache_key("serde", Some("LATEST")),
            none
        );
        assert_eq!(
            CacheKeyGenerator::crate_cache_key("serde", Some(" latest ")),
            none
        );
        assert_eq!(CacheKeyGenerator::crate_cache_key("serde", Some("")), none);
        assert_eq!(
            CacheKeyGenerator::crate_cache_key("serde", Some("   ")),
            none
        );

        // The HTML artifact keyspace inherits the same normalization.
        assert_eq!(
            CacheKeyGenerator::crate_html_cache_key("serde", Some("latest")),
            CacheKeyGenerator::crate_html_cache_key("serde", None)
        );

        // Item keys normalize identically.
        let item_none = CacheKeyGenerator::item_cache_key("serde", "Serialize", None);
        assert_eq!(item_none, "item:serde:Serialize");
        assert_eq!(
            CacheKeyGenerator::item_cache_key("serde", "Serialize", Some("latest")),
            item_none
        );
        assert_eq!(
            CacheKeyGenerator::item_cache_key("serde", "Serialize", Some("")),
            item_none
        );

        // A real version is still preserved verbatim (lowercased/trimmed).
        assert_eq!(
            CacheKeyGenerator::crate_cache_key("serde", Some("1.0")),
            "crate:serde:1.0"
        );
    }

    #[test]
    fn test_item_path_case_sensitivity() {
        assert_ne!(
            CacheKeyGenerator::item_cache_key("serde", "Serialize", None),
            CacheKeyGenerator::item_cache_key("serde", "serialize", None)
        );
    }

    #[test]
    fn test_cache_key_edge_cases() {
        let empty_key = CacheKeyGenerator::crate_cache_key("", None);
        assert!(empty_key.starts_with("crate:hash:"));

        let whitespace_key = CacheKeyGenerator::crate_cache_key("   ", None);
        assert!(whitespace_key.starts_with("crate:hash:"));

        // Empty/whitespace-only versions normalize to the unversioned key
        // (same as `None`) so they do not create duplicate cache entries.
        assert_eq!(
            CacheKeyGenerator::crate_cache_key("serde", Some("")),
            "crate:serde"
        );

        let unicode_key = CacheKeyGenerator::crate_cache_key("serde测试", None);
        assert!(unicode_key.starts_with("crate:hash:"));
        assert!(!unicode_key.contains("测试"));

        let malicious_item_path =
            CacheKeyGenerator::item_cache_key("serde", "Serialize\nmalicious", None);
        assert!(malicious_item_path.contains("hash:"));
        assert!(!malicious_item_path.contains('\n'));

        // Single (non-`::`) colons are ambiguous separators and must be hashed,
        // not embedded verbatim, to avoid version/path key collisions.
        let malicious_item_colon =
            CacheKeyGenerator::item_cache_key("serde", "Serialize:extra:colons", None);
        assert!(malicious_item_colon.starts_with("item:serde:hash:"));
        assert!(!malicious_item_colon.contains("Serialize:extra:colons"));

        let valid_item_path = CacheKeyGenerator::item_cache_key("serde", "serde::Serialize", None);
        assert_eq!(valid_item_path, "item:serde:serde::Serialize");

        let empty_item_key = CacheKeyGenerator::item_cache_key("serde", "", None);
        assert!(empty_item_key.contains("hash:"));

        let empty_item_crate = CacheKeyGenerator::item_cache_key("", "Crate", None);
        assert!(empty_item_crate.contains("hash:"));
    }
}