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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! Custom crates.io API client
//!
//! Async client for the crates.io REST API, built on reqwest with built-in
//! rate limiting. Supports both anonymous and authenticated access.

pub mod docsrs;
pub mod error;
pub mod osv;
pub mod query;
pub mod types;
pub(crate) mod wire;

mod categories;
mod crates;
mod keywords;
mod metadata;
mod owners;
mod publish;
mod teams;
mod tokens;
mod trusted;
mod users;
mod versions;

#[cfg(test)]
mod tests;

use std::sync::Arc;
use std::time::Duration;

use serde::Serialize;
use serde::de::DeserializeOwned;
use tokio::sync::Mutex;
use tokio::time::Instant;

pub use error::Error;
pub use query::{CratesQuery, CratesQueryBuilder, Sort};
pub use types::*;

// ── Auth ────────────────────────────────────────────────────────────────────

/// Authentication credentials for the crates.io API.
struct Auth {
    token: String,
}

impl std::fmt::Debug for Auth {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Auth")
            .field("token", &"[REDACTED]")
            .finish()
    }
}

// ── Client ──────────────────────────────────────────────────────────────────

/// Async client for the crates.io REST API.
///
/// Includes built-in rate limiting to respect the crates.io crawling policy.
/// Supports optional authentication via API token for write operations.
/// Retries transient failures (429, 5xx) with exponential backoff.
pub struct CratesIoClient {
    http: reqwest::Client,
    base_url: String,
    rate_limit: Duration,
    last_request: Arc<Mutex<Option<Instant>>>,
    auth: Option<Auth>,
    max_retries: u32,
    initial_backoff: Duration,
}

impl CratesIoClient {
    /// Create a new client with the given user agent and rate limit.
    pub fn new(user_agent: &str, rate_limit: Duration) -> Result<Self, Error> {
        Self::with_base_url(user_agent, rate_limit, "https://crates.io/api/v1")
    }

    /// Create a new client with a custom base URL (for testing).
    pub fn with_base_url(
        user_agent: &str,
        rate_limit: Duration,
        base_url: &str,
    ) -> Result<Self, Error> {
        let http = reqwest::Client::builder().user_agent(user_agent).build()?;
        Ok(Self {
            http,
            base_url: base_url.trim_end_matches('/').to_string(),
            rate_limit,
            last_request: Arc::new(Mutex::new(None)),
            auth: None,
            max_retries: 3,
            initial_backoff: Duration::from_millis(500),
        })
    }

    /// Set the maximum number of retries for transient failures.
    ///
    /// Returns `self` for builder-style chaining.
    pub fn with_max_retries(mut self, max_retries: u32) -> Self {
        self.max_retries = max_retries;
        self
    }

    /// Set the initial backoff duration for exponential retry backoff.
    ///
    /// Returns `self` for builder-style chaining.
    pub fn with_initial_backoff(mut self, backoff: Duration) -> Self {
        self.initial_backoff = backoff;
        self
    }

    /// Enable authentication with an API token.
    ///
    /// Returns `self` for builder-style chaining.
    pub fn with_auth(mut self, token: impl Into<String>) -> Self {
        self.auth = Some(Auth {
            token: token.into(),
        });
        self
    }

    /// Returns the auth token or `Error::AuthRequired`.
    pub(crate) fn require_auth(&self) -> Result<&str, Error> {
        self.auth
            .as_ref()
            .map(|a| a.token.as_str())
            .ok_or(Error::AuthRequired)
    }

    // ── Unauthenticated HTTP helpers ────────────────────────────────────

    /// Enforce rate limiting between requests.
    pub(crate) async fn throttle(&self) {
        let mut last = self.last_request.lock().await;
        if let Some(last_time) = *last {
            let elapsed = last_time.elapsed();
            if elapsed < self.rate_limit {
                tokio::time::sleep(self.rate_limit - elapsed).await;
            }
        }
        *last = Some(Instant::now());
    }

    /// Execute an HTTP request with exponential backoff retry on 429 and 5xx responses.
    ///
    /// `make_request` is called once per attempt. Retries are only performed for
    /// transient failures (429 Too Many Requests, 5xx Server Error). Client errors
    /// (4xx other than 429) are returned immediately without retrying.
    async fn send_with_retry<F, Fut>(
        &self,
        path: &str,
        make_request: F,
    ) -> Result<reqwest::Response, Error>
    where
        F: Fn() -> Fut,
        Fut: std::future::Future<Output = Result<reqwest::Response, reqwest::Error>>,
    {
        let mut attempt = 0u32;
        loop {
            self.throttle().await;
            let resp = make_request().await?;
            let status = resp.status();

            let retryable =
                status == reqwest::StatusCode::TOO_MANY_REQUESTS || status.is_server_error();

            if retryable && attempt < self.max_retries {
                let backoff = self.initial_backoff * 2u32.pow(attempt);
                tracing::warn!(
                    attempt = attempt + 1,
                    max_retries = self.max_retries,
                    status = status.as_u16(),
                    path,
                    backoff_ms = backoff.as_millis(),
                    "retrying crates.io request"
                );
                tokio::time::sleep(backoff).await;
                attempt += 1;
                continue;
            }

            return Self::check_status(resp, path).await;
        }
    }

    /// Send a GET request and check the response status.
    pub(crate) async fn send(&self, path: &str) -> Result<reqwest::Response, Error> {
        let url = format!("{}{}", self.base_url, path);
        self.send_with_retry(path, || self.http.get(&url).send())
            .await
    }

    /// Send a GET request with query parameters and check the response status.
    pub(crate) async fn send_query(
        &self,
        path: &str,
        query: &[(String, String)],
    ) -> Result<reqwest::Response, Error> {
        let url = format!("{}{}", self.base_url, path);
        self.send_with_retry(path, || self.http.get(&url).query(query).send())
            .await
    }

    /// Map non-success HTTP status codes to typed errors.
    pub(crate) async fn check_status(
        resp: reqwest::Response,
        path: &str,
    ) -> Result<reqwest::Response, Error> {
        let status = resp.status();
        if status.is_success() {
            Ok(resp)
        } else if status == reqwest::StatusCode::NOT_FOUND {
            Err(Error::NotFound(path.to_string()))
        } else if status == reqwest::StatusCode::UNAUTHORIZED {
            Err(Error::Unauthorized)
        } else if status == reqwest::StatusCode::FORBIDDEN {
            Err(Error::PermissionDenied)
        } else if status == reqwest::StatusCode::TOO_MANY_REQUESTS {
            Err(Error::RateLimited)
        } else {
            let text = resp.text().await.unwrap_or_default();
            Err(Error::Api {
                status: status.as_u16(),
                message: text,
            })
        }
    }

    /// GET a JSON resource.
    pub(crate) async fn get_json<T: DeserializeOwned>(&self, path: &str) -> Result<T, Error> {
        let resp = self.send(path).await?;
        Ok(resp.json().await?)
    }

    /// GET a JSON resource with query parameters.
    pub(crate) async fn get_json_query<T: DeserializeOwned>(
        &self,
        path: &str,
        query: &[(String, String)],
    ) -> Result<T, Error> {
        let resp = self.send_query(path, query).await?;
        Ok(resp.json().await?)
    }

    /// GET a text resource (e.g. readme).
    pub(crate) async fn get_text(&self, path: &str) -> Result<String, Error> {
        let resp = self.send(path).await?;
        Ok(resp.text().await?)
    }

    // ── Authenticated HTTP helpers ──────────────────────────────────────

    /// Send an authenticated GET request.
    pub(crate) async fn send_auth(&self, path: &str) -> Result<reqwest::Response, Error> {
        let token = self.require_auth()?.to_string();
        let url = format!("{}{}", self.base_url, path);
        self.send_with_retry(path, || {
            self.http.get(&url).header("Authorization", &token).send()
        })
        .await
    }

    /// Send an authenticated GET request with query parameters.
    pub(crate) async fn send_query_auth(
        &self,
        path: &str,
        query: &[(String, String)],
    ) -> Result<reqwest::Response, Error> {
        let token = self.require_auth()?;
        self.throttle().await;
        let url = format!("{}{}", self.base_url, path);
        let resp = self
            .http
            .get(&url)
            .header("Authorization", token)
            .query(query)
            .send()
            .await?;
        Self::check_status(resp, path).await
    }

    /// GET a JSON resource with authentication.
    pub(crate) async fn get_json_auth<T: DeserializeOwned>(&self, path: &str) -> Result<T, Error> {
        let resp = self.send_auth(path).await?;
        Ok(resp.json().await?)
    }

    /// GET a JSON resource with query params and authentication.
    pub(crate) async fn get_json_query_auth<T: DeserializeOwned>(
        &self,
        path: &str,
        query: &[(String, String)],
    ) -> Result<T, Error> {
        let resp = self.send_query_auth(path, query).await?;
        Ok(resp.json().await?)
    }

    /// PUT a JSON body and return a deserialized response. Requires auth.
    pub(crate) async fn put_json<T: DeserializeOwned, B: Serialize>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<T, Error> {
        let token = self.require_auth()?;
        self.throttle().await;
        let url = format!("{}{}", self.base_url, path);
        let resp = self
            .http
            .put(&url)
            .header("Authorization", token)
            .json(body)
            .send()
            .await?;
        let resp = Self::check_status(resp, path).await?;
        Ok(resp.json().await?)
    }

    /// PUT a JSON body, expecting no meaningful response body. Requires auth.
    pub(crate) async fn put_json_ok<B: Serialize>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<(), Error> {
        let token = self.require_auth()?;
        self.throttle().await;
        let url = format!("{}{}", self.base_url, path);
        let resp = self
            .http
            .put(&url)
            .header("Authorization", token)
            .json(body)
            .send()
            .await?;
        Self::check_status(resp, path).await?;
        Ok(())
    }

    /// PUT with no body, returning a deserialized JSON response. Requires auth.
    pub(crate) async fn put_empty<T: DeserializeOwned>(&self, path: &str) -> Result<T, Error> {
        let token = self.require_auth()?;
        self.throttle().await;
        let url = format!("{}{}", self.base_url, path);
        let resp = self
            .http
            .put(&url)
            .header("Authorization", token)
            .send()
            .await?;
        let resp = Self::check_status(resp, path).await?;
        Ok(resp.json().await?)
    }

    /// PUT with no body, returning deserialized JSON. No auth.
    pub(crate) async fn put_empty_json<T: DeserializeOwned>(&self, path: &str) -> Result<T, Error> {
        self.throttle().await;
        let url = format!("{}{}", self.base_url, path);
        let resp = self.http.put(&url).send().await?;
        let resp = Self::check_status(resp, path).await?;
        Ok(resp.json().await?)
    }

    /// DELETE and return a deserialized JSON response. Requires auth.
    pub(crate) async fn delete_json<T: DeserializeOwned>(&self, path: &str) -> Result<T, Error> {
        let token = self.require_auth()?;
        self.throttle().await;
        let url = format!("{}{}", self.base_url, path);
        let resp = self
            .http
            .delete(&url)
            .header("Authorization", token)
            .send()
            .await?;
        let resp = Self::check_status(resp, path).await?;
        Ok(resp.json().await?)
    }

    /// DELETE with a JSON body and return deserialized response. Requires auth.
    pub(crate) async fn delete_json_with_body<T: DeserializeOwned, B: Serialize>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<T, Error> {
        let token = self.require_auth()?;
        self.throttle().await;
        let url = format!("{}{}", self.base_url, path);
        let resp = self
            .http
            .delete(&url)
            .header("Authorization", token)
            .json(body)
            .send()
            .await?;
        let resp = Self::check_status(resp, path).await?;
        Ok(resp.json().await?)
    }

    /// DELETE expecting no response body (just check status). Requires auth.
    pub(crate) async fn delete_ok(&self, path: &str) -> Result<(), Error> {
        let token = self.require_auth()?;
        self.throttle().await;
        let url = format!("{}{}", self.base_url, path);
        let resp = self
            .http
            .delete(&url)
            .header("Authorization", token)
            .send()
            .await?;
        Self::check_status(resp, path).await?;
        Ok(())
    }

    /// PATCH a JSON body and return deserialized response. Requires auth.
    pub(crate) async fn patch_json<T: DeserializeOwned, B: Serialize>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<T, Error> {
        let token = self.require_auth()?;
        self.throttle().await;
        let url = format!("{}{}", self.base_url, path);
        let resp = self
            .http
            .patch(&url)
            .header("Authorization", token)
            .json(body)
            .send()
            .await?;
        let resp = Self::check_status(resp, path).await?;
        Ok(resp.json().await?)
    }

    /// POST a JSON body and return deserialized response. Requires auth.
    pub(crate) async fn post_json<T: DeserializeOwned, B: Serialize>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<T, Error> {
        let token = self.require_auth()?;
        self.throttle().await;
        let url = format!("{}{}", self.base_url, path);
        let resp = self
            .http
            .post(&url)
            .header("Authorization", token)
            .json(body)
            .send()
            .await?;
        let resp = Self::check_status(resp, path).await?;
        Ok(resp.json().await?)
    }

    /// POST a JSON body without authentication and return deserialized response.
    pub(crate) async fn post_json_unauth<T: DeserializeOwned, B: Serialize>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<T, Error> {
        self.throttle().await;
        let url = format!("{}{}", self.base_url, path);
        let resp = self.http.post(&url).json(body).send().await?;
        let resp = Self::check_status(resp, path).await?;
        Ok(resp.json().await?)
    }

    /// PUT raw bytes with a custom content type and return deserialized JSON. Requires auth.
    pub(crate) async fn put_bytes_json<T: DeserializeOwned>(
        &self,
        path: &str,
        body: Vec<u8>,
        content_type: &str,
    ) -> Result<T, Error> {
        let token = self.require_auth()?;
        self.throttle().await;
        let url = format!("{}{}", self.base_url, path);
        let resp = self
            .http
            .put(&url)
            .header("Authorization", token)
            .header("Content-Type", content_type)
            .body(body)
            .send()
            .await?;
        let resp = Self::check_status(resp, path).await?;
        Ok(resp.json().await?)
    }
}