cratesio-mcp 0.1.4

MCP server for querying crates.io - the Rust package registry
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
//! Find alternative crates tool

use std::sync::Arc;

use schemars::JsonSchema;
use serde::Deserialize;
use tower_mcp::{
    CallToolResult, ResultExt, Tool, ToolBuilder,
    extract::{Json, State},
};

use crate::client::{CratesQuery, Sort};
use crate::state::{AppState, format_number};

/// Input for finding alternative crates
#[derive(Debug, Deserialize, JsonSchema)]
pub struct FindAlternativesInput {
    /// Name of the crate to find alternatives for
    name: String,
    /// Maximum number of alternatives to return (default: 5)
    #[serde(default = "default_max_results")]
    max_results: usize,
}

fn default_max_results() -> usize {
    5
}

pub fn build(state: Arc<AppState>) -> Tool {
    ToolBuilder::new("find_alternatives")
        .title("Find Alternatives")
        .description(
            "Find and compare alternative crates for a given crate. Uses the crate's keywords \
             to search for related crates, then returns a comparison table showing downloads, \
             recent activity, and descriptions.",
        )
        .read_only()
        .idempotent()
        .icon("https://crates.io/assets/cargo.png")
        .extractor_handler(
            state,
            |State(state): State<Arc<AppState>>, Json(input): Json<FindAlternativesInput>| async move {
                // 1. Get target crate info to extract keywords
                let target = state
                    .client
                    .get_crate(&input.name)
                    .await
                    .tool_context("Crates.io API error")?;

                let crate_data = &target.crate_data;

                // 2. Build search query from first few keywords
                let keywords: Vec<String> = crate_data
                    .keywords
                    .as_deref()
                    .unwrap_or(&[])
                    .iter()
                    .take(3)
                    .cloned()
                    .collect();

                if keywords.is_empty() {
                    return Ok(CallToolResult::text(format!(
                        "No keywords found for '{}'. Cannot search for alternatives.",
                        input.name
                    )));
                }

                let search_term = keywords.join(" ");

                // 3. Search for related crates by keyword
                let query = CratesQuery::builder()
                    .search(&search_term)
                    .sort(Sort::Downloads)
                    .per_page(25)
                    .build();

                let search_results = state
                    .client
                    .crates(query)
                    .await
                    .tool_context("Crates.io API error")?;

                // 4. Filter out the original crate and collect candidates
                let candidates: Vec<_> = search_results
                    .crates
                    .iter()
                    .filter(|c| c.name.to_lowercase() != input.name.to_lowercase())
                    .take(input.max_results)
                    .collect();

                if candidates.is_empty() {
                    return Ok(CallToolResult::text(format!(
                        "No alternatives found for '{}' using keywords: {}",
                        input.name, search_term
                    )));
                }

                // 5. Format output as markdown comparison table
                let mut output = format!(
                    "# Alternatives to `{}`\n\n",
                    input.name
                );

                // Target crate summary
                output.push_str(&format!(
                    "**Target**: {} v{} — {}\n",
                    crate_data.name,
                    crate_data.max_version,
                    crate_data.description.as_deref().unwrap_or("No description"),
                ));
                output.push_str(&format!(
                    "**Keywords searched**: {}\n\n",
                    search_term
                ));

                // Comparison table header
                output.push_str(
                    "| Crate | Version | Description | Downloads | Recent | Last Updated |\n",
                );
                output.push_str("|---|---|---|---|---|---|\n");

                // Target crate row
                output.push_str(&format!(
                    "| **{}** *(target)* | {} | {} | {} | {} | {} |\n",
                    crate_data.name,
                    crate_data.max_version,
                    crate_data
                        .description
                        .as_deref()
                        .unwrap_or("-")
                        .trim()
                        .chars()
                        .take(60)
                        .collect::<String>(),
                    format_number(crate_data.downloads),
                    crate_data
                        .recent_downloads
                        .map(format_number)
                        .unwrap_or_else(|| "-".to_string()),
                    crate_data.updated_at.date_naive(),
                ));

                // 5. Get basic info for each alternative and add to table
                for candidate in &candidates {
                    match state.client.get_crate(&candidate.name).await {
                        Ok(alt) => {
                            let c = &alt.crate_data;
                            output.push_str(&format!(
                                "| {} | {} | {} | {} | {} | {} |\n",
                                c.name,
                                c.max_version,
                                c.description
                                    .as_deref()
                                    .unwrap_or("-")
                                    .trim()
                                    .chars()
                                    .take(60)
                                    .collect::<String>(),
                                format_number(c.downloads),
                                c.recent_downloads
                                    .map(format_number)
                                    .unwrap_or_else(|| "-".to_string()),
                                c.updated_at.date_naive(),
                            ));
                        }
                        Err(_) => {
                            // Fall back to search result data if detailed fetch fails
                            output.push_str(&format!(
                                "| {} | {} | {} | {} | {} | - |\n",
                                candidate.name,
                                candidate.max_version,
                                candidate
                                    .description
                                    .as_deref()
                                    .unwrap_or("-")
                                    .trim()
                                    .chars()
                                    .take(60)
                                    .collect::<String>(),
                                format_number(candidate.downloads),
                                candidate
                                    .recent_downloads
                                    .map(format_number)
                                    .unwrap_or_else(|| "-".to_string()),
                            ));
                        }
                    }
                }

                Ok(CallToolResult::text(output))
            },
        )
        .build()
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;
    use std::time::Duration;

    use tokio::sync::RwLock;
    use wiremock::matchers::{method, path, query_param};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    use crate::client::CratesIoClient;
    use crate::client::docsrs::DocsRsClient;
    use crate::client::osv::OsvClient;
    use crate::docs::cache::DocsCache;
    use crate::state::AppState;

    fn test_state(crates_url: &str) -> Arc<AppState> {
        let osv_url = "http://localhost:1";
        Arc::new(AppState {
            client: CratesIoClient::with_base_url("test", Duration::from_millis(0), crates_url)
                .unwrap(),
            docsrs_client: DocsRsClient::with_base_url("test", crates_url).unwrap(),
            osv_client: OsvClient::with_base_url("test", osv_url).unwrap(),
            docs_cache: DocsCache::new(10, Duration::from_secs(3600)),
            recent_searches: RwLock::new(Vec::new()),
        })
    }

    #[tokio::test]
    async fn find_alternatives_basic() {
        let server = MockServer::start().await;

        // Target crate info
        Mock::given(method("GET"))
            .and(path("/crates/serde"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "crate": {
                    "name": "serde",
                    "max_version": "1.0.210",
                    "description": "A generic serialization/deserialization framework",
                    "downloads": 500000000,
                    "recent_downloads": 50000000,
                    "created_at": "2015-01-01T00:00:00.000000Z",
                    "updated_at": "2026-01-01T00:00:00.000000Z",
                    "keywords": ["serialization", "serde", "encoding"],
                    "categories": ["encoding"]
                },
                "versions": [
                    {"num": "1.0.210", "yanked": false, "created_at": "2026-01-01T00:00:00.000000Z", "downloads": 1000000}
                ]
            })))
            .mount(&server)
            .await;

        // Search results for the keywords
        Mock::given(method("GET"))
            .and(path("/crates"))
            .and(query_param("q", "serialization serde encoding"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "crates": [
                    {
                        "name": "serde",
                        "max_version": "1.0.210",
                        "description": "A generic serialization/deserialization framework",
                        "downloads": 500000000,
                        "recent_downloads": 50000000,
                        "created_at": "2015-01-01T00:00:00.000000Z",
                        "updated_at": "2026-01-01T00:00:00.000000Z"
                    },
                    {
                        "name": "bincode",
                        "max_version": "1.3.3",
                        "description": "A binary serialization / deserialization strategy",
                        "downloads": 80000000,
                        "recent_downloads": 8000000,
                        "created_at": "2017-01-01T00:00:00.000000Z",
                        "updated_at": "2025-06-01T00:00:00.000000Z"
                    },
                    {
                        "name": "postcard",
                        "max_version": "1.0.8",
                        "description": "A compact serializer for embedded targets",
                        "downloads": 10000000,
                        "recent_downloads": 1000000,
                        "created_at": "2020-01-01T00:00:00.000000Z",
                        "updated_at": "2025-11-01T00:00:00.000000Z"
                    }
                ],
                "meta": {"total": 3}
            })))
            .mount(&server)
            .await;

        // Alternative crate: bincode
        Mock::given(method("GET"))
            .and(path("/crates/bincode"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "crate": {
                    "name": "bincode",
                    "max_version": "1.3.3",
                    "description": "A binary serialization / deserialization strategy",
                    "downloads": 80000000,
                    "recent_downloads": 8000000,
                    "created_at": "2017-01-01T00:00:00.000000Z",
                    "updated_at": "2025-06-01T00:00:00.000000Z"
                },
                "versions": [
                    {"num": "1.3.3", "yanked": false, "created_at": "2025-06-01T00:00:00.000000Z", "downloads": 5000000}
                ]
            })))
            .mount(&server)
            .await;

        // Alternative crate: postcard
        Mock::given(method("GET"))
            .and(path("/crates/postcard"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "crate": {
                    "name": "postcard",
                    "max_version": "1.0.8",
                    "description": "A compact serializer for embedded targets",
                    "downloads": 10000000,
                    "recent_downloads": 1000000,
                    "created_at": "2020-01-01T00:00:00.000000Z",
                    "updated_at": "2025-11-01T00:00:00.000000Z"
                },
                "versions": [
                    {"num": "1.0.8", "yanked": false, "created_at": "2025-11-01T00:00:00.000000Z", "downloads": 500000}
                ]
            })))
            .mount(&server)
            .await;

        let state = test_state(&server.uri());
        let tool = super::build(state);
        let result = tool.call(serde_json::json!({"name": "serde"})).await;

        let text = result.all_text();
        assert!(text.contains("Alternatives to `serde`"));
        assert!(text.contains("serialization serde encoding"));
        assert!(text.contains("bincode"));
        assert!(text.contains("postcard"));
        // Target should appear in table as well
        assert!(text.contains("*(target)*"));
        // serde should not appear as an alternative
        assert!(!text.contains("serde | 1.0.210"));
    }

    #[tokio::test]
    async fn find_alternatives_no_keywords() {
        let server = MockServer::start().await;

        Mock::given(method("GET"))
            .and(path("/crates/no-keywords-crate"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "crate": {
                    "name": "no-keywords-crate",
                    "max_version": "0.1.0",
                    "description": "A crate with no keywords",
                    "downloads": 100,
                    "created_at": "2025-01-01T00:00:00.000000Z",
                    "updated_at": "2025-01-01T00:00:00.000000Z",
                    "keywords": []
                },
                "versions": [
                    {"num": "0.1.0", "yanked": false, "created_at": "2025-01-01T00:00:00.000000Z", "downloads": 100}
                ]
            })))
            .mount(&server)
            .await;

        let state = test_state(&server.uri());
        let tool = super::build(state);
        let result = tool
            .call(serde_json::json!({"name": "no-keywords-crate"}))
            .await;

        let text = result.all_text();
        assert!(text.contains("No keywords found"));
    }

    #[tokio::test]
    async fn find_alternatives_custom_max_results() {
        let server = MockServer::start().await;

        // Target crate
        Mock::given(method("GET"))
            .and(path("/crates/tokio"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "crate": {
                    "name": "tokio",
                    "max_version": "1.40.0",
                    "description": "An event-driven, non-blocking I/O platform",
                    "downloads": 300000000,
                    "recent_downloads": 30000000,
                    "created_at": "2016-01-01T00:00:00.000000Z",
                    "updated_at": "2026-01-01T00:00:00.000000Z",
                    "keywords": ["async", "futures", "io"]
                },
                "versions": [
                    {"num": "1.40.0", "yanked": false, "created_at": "2026-01-01T00:00:00.000000Z", "downloads": 5000000}
                ]
            })))
            .mount(&server)
            .await;

        // Search results
        Mock::given(method("GET"))
            .and(path("/crates"))
            .and(query_param("q", "async futures io"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "crates": [
                    {
                        "name": "tokio",
                        "max_version": "1.40.0",
                        "description": "An event-driven, non-blocking I/O platform",
                        "downloads": 300000000,
                        "recent_downloads": 30000000,
                        "created_at": "2016-01-01T00:00:00.000000Z",
                        "updated_at": "2026-01-01T00:00:00.000000Z"
                    },
                    {
                        "name": "async-std",
                        "max_version": "1.12.0",
                        "description": "Async version of the Rust standard library",
                        "downloads": 50000000,
                        "recent_downloads": 2000000,
                        "created_at": "2019-01-01T00:00:00.000000Z",
                        "updated_at": "2024-01-01T00:00:00.000000Z"
                    }
                ],
                "meta": {"total": 2}
            })))
            .mount(&server)
            .await;

        // async-std detail
        Mock::given(method("GET"))
            .and(path("/crates/async-std"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "crate": {
                    "name": "async-std",
                    "max_version": "1.12.0",
                    "description": "Async version of the Rust standard library",
                    "downloads": 50000000,
                    "recent_downloads": 2000000,
                    "created_at": "2019-01-01T00:00:00.000000Z",
                    "updated_at": "2024-01-01T00:00:00.000000Z"
                },
                "versions": [
                    {"num": "1.12.0", "yanked": false, "created_at": "2024-01-01T00:00:00.000000Z", "downloads": 1000000}
                ]
            })))
            .mount(&server)
            .await;

        let state = test_state(&server.uri());
        let tool = super::build(state);
        let result = tool
            .call(serde_json::json!({"name": "tokio", "max_results": 1}))
            .await;

        let text = result.all_text();
        assert!(text.contains("Alternatives to `tokio`"));
        assert!(text.contains("async-std"));
    }
}