research-master 0.1.40

MCP server for searching and downloading academic papers from multiple research sources
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//! PubMed research source implementation using E-utilities API.

use async_trait::async_trait;
use quick_xml::de::from_str;
use serde::Deserialize;
use std::sync::Arc;

use crate::models::{Paper, PaperBuilder, SearchQuery, SearchResponse, SourceType};
use crate::sources::{Source, SourceCapabilities, SourceError};
use crate::utils::{api_retry_config, with_retry, HttpClient};

/// PubMed E-utilities API base URLs
const PUBMED_ESEARCH_URL: &str = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi";
const PUBMED_EFETCH_URL: &str = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi";

/// PubMed research source
///
/// Uses NCBI E-utilities API for searching and fetching PubMed records.
#[derive(Debug, Clone)]
pub struct PubMedSource {
    client: Arc<HttpClient>,
}

impl PubMedSource {
    /// Create a new PubMed source
    pub fn new() -> Result<Self, SourceError> {
        Ok(Self {
            client: Arc::new(HttpClient::new()?),
        })
    }

    /// Create with a custom HTTP client (for testing)
    #[allow(dead_code)]
    pub fn with_client(client: Arc<HttpClient>) -> Self {
        Self { client }
    }

    /// Build E-utilities search URL
    fn build_search_url(&self, query: &SearchQuery) -> String {
        let mut params = vec![
            ("db".to_string(), "pubmed".to_string()),
            ("term".to_string(), query.query.clone()),
            ("retmax".to_string(), query.max_results.to_string()),
            ("retmode".to_string(), "xml".to_string()),
        ];

        // Add year filter if specified
        if let Some(year) = &query.year {
            // PubMed uses various date formats
            if let Some(start) = year.strip_prefix('-') {
                // Until year: PDAT less than or equal to YYYY
                params.push(("datetype".to_string(), "pdat".to_string()));
                params.push(("reldate".to_string(), format!("{}-01-01:9999", start)));
            } else if let Some(end) = year.strip_suffix('-') {
                // From year: PDAT greater than or equal to YYYY
                params.push(("datetype".to_string(), "pdat".to_string()));
                params.push(("reldate".to_string(), format!("{}-01-01:9999", end)));
            } else if year.contains('-') {
                // Range
                let parts: Vec<&str> = year.splitn(2, '-').collect();
                if parts.len() == 2 {
                    params.push(("mindate".to_string(), format!("{}-01-01", parts[0])));
                    params.push(("maxdate".to_string(), format!("{}-12-31", parts[1])));
                }
            } else if year.len() == 4 {
                // Single year
                params.push(("mindate".to_string(), format!("{}-01-01", year)));
                params.push(("maxdate".to_string(), format!("{}-12-31", year)));
            }
        }

        // Add author filter
        if let Some(author) = &query.author {
            params.push(("term".to_string(), format!("{}[AUTH]", author)));
        }

        params
            .iter()
            .map(|(k, v)| format!("{}={}", k, urlencoding::encode(v)))
            .collect::<Vec<_>>()
            .join("&")
    }

    /// Parse E-utilities search response XML
    fn parse_search_response(xml: &str) -> Result<Vec<String>, SourceError> {
        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct ESearchResult {
            IdList: IdList,
        }

        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct IdList {
            #[serde(rename = "Id", default)]
            ids: Vec<String>,
        }

        let result: ESearchResult = from_str(xml)
            .map_err(|e| SourceError::Parse(format!("Failed to parse PubMed search XML: {}", e)))?;

        Ok(result.IdList.ids)
    }

    /// Build E-utilities fetch URL for specific PubMed IDs
    fn build_fetch_url(ids: &[String]) -> String {
        format!(
            "{}?db=pubmed&id={}&retmode=xml",
            PUBMED_EFETCH_URL,
            ids.join(",")
        )
    }

    /// Parse E-utilities fetch response XML
    fn parse_fetch_response(xml: &str) -> Result<Vec<Paper>, SourceError> {
        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct PubmedArticleSet {
            #[serde(rename = "PubmedArticle", default)]
            articles: Vec<PubmedArticle>,
        }

        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct PubmedArticle {
            MedlineCitation: Option<MedlineCitation>,
            PubmedData: Option<PubmedData>,
        }

        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct MedlineCitation {
            PMID: Option<Pmid>,
            Article: Option<Article>,
        }

        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct Pmid {
            #[serde(rename = "$text")]
            id: String,
        }

        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct Article {
            Journal: Option<Journal>,
            ArticleTitle: Option<ArticleTitle>,
            Abstract: Option<Abstract>,
            AuthorList: Option<AuthorList>,
        }

        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct Journal {
            JournalIssue: Option<JournalIssue>,
        }

        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct JournalIssue {
            PubDate: Option<PubDate>,
        }

        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct PubDate {
            Year: Option<String>,
            #[serde(rename = "MedlineDate")]
            medline_date: Option<String>,
        }

        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct ArticleTitle {
            #[serde(rename = "$text")]
            title: String,
        }

        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct Abstract {
            #[serde(rename = "AbstractText", default)]
            abstract_texts: Vec<AbstractText>,
        }

        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct AbstractText {
            #[serde(rename = "$text")]
            text: String,
        }

        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct AuthorList {
            #[serde(rename = "Author", default)]
            authors: Vec<Author>,
        }

        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct Author {
            LastName: Option<LastName>,
            ForeName: Option<ForeName>,
            Initials: Option<Initials>,
            CollectiveName: Option<CollectiveName>,
        }

        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct LastName {
            #[serde(rename = "$text")]
            name: String,
        }

        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct ForeName {
            #[serde(rename = "$text")]
            name: String,
        }

        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct Initials {
            #[serde(rename = "$text")]
            initials: String,
        }

        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct CollectiveName {
            #[serde(rename = "$text")]
            name: String,
        }

        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct PubmedData {
            ArticleIdList: Option<ArticleIdList>,
        }

        #[derive(Debug, Deserialize)]
        #[allow(non_snake_case)]
        struct ArticleIdList {
            #[serde(rename = "ArticleId", default)]
            ids: Vec<ArticleId>,
        }

        #[derive(Debug, Deserialize)]
        struct ArticleId {
            #[serde(rename = "IdType")]
            id_type: String,
            #[serde(rename = "$text")]
            value: String,
        }

        let result: PubmedArticleSet = from_str(xml)
            .map_err(|e| SourceError::Parse(format!("Failed to parse PubMed fetch XML: {}", e)))?;

        let mut papers = Vec::new();

        for article in result.articles {
            let pmid = article
                .MedlineCitation
                .as_ref()
                .and_then(|m| m.PMID.as_ref())
                .map(|p| p.id.clone())
                .unwrap_or_default();

            let title = article
                .MedlineCitation
                .as_ref()
                .and_then(|m| m.Article.as_ref())
                .and_then(|a| a.ArticleTitle.as_ref())
                .map(|t| t.title.clone())
                .unwrap_or_default();

            let authors = article
                .MedlineCitation
                .as_ref()
                .and_then(|m| m.Article.as_ref())
                .and_then(|a| a.AuthorList.as_ref())
                .map(|al| {
                    al.authors
                        .iter()
                        .map(|author| {
                            if let Some(collective) = &author.CollectiveName {
                                collective.name.clone()
                            } else {
                                let first = author
                                    .ForeName
                                    .as_ref()
                                    .map(|f| f.name.as_str())
                                    .unwrap_or("");
                                let last = author
                                    .LastName
                                    .as_ref()
                                    .map(|l| l.name.as_str())
                                    .unwrap_or("");
                                let initials = author
                                    .Initials
                                    .as_ref()
                                    .map(|i| i.initials.as_str())
                                    .unwrap_or("");
                                format!("{} {} {}", first, last, initials)
                                    .trim()
                                    .to_string()
                            }
                        })
                        .collect::<Vec<_>>()
                        .join("; ")
                })
                .unwrap_or_default();

            let abstract_text = article
                .MedlineCitation
                .as_ref()
                .and_then(|m| m.Article.as_ref())
                .and_then(|a| a.Abstract.as_ref())
                .map(|ab| {
                    ab.abstract_texts
                        .iter()
                        .map(|at| at.text.clone())
                        .collect::<Vec<_>>()
                        .join(" ")
                })
                .unwrap_or_default();

            let published_date = article
                .MedlineCitation
                .as_ref()
                .and_then(|m| m.Article.as_ref())
                .and_then(|a| a.Journal.as_ref())
                .and_then(|j| j.JournalIssue.as_ref())
                .and_then(|ji| ji.PubDate.as_ref())
                .and_then(|pd| pd.Year.as_ref().or(pd.medline_date.as_ref()))
                .cloned();

            let doi = article
                .PubmedData
                .as_ref()
                .and_then(|pd| pd.ArticleIdList.as_ref())
                .and_then(|ail| ail.ids.iter().find(|id| id.id_type == "doi"))
                .map(|id| id.value.clone());

            let url = format!("https://pubmed.ncbi.nlm.nih.gov/{}/", pmid);

            papers.push(
                PaperBuilder::new(pmid, title, url, SourceType::PubMed)
                    .authors(authors)
                    .abstract_text(abstract_text)
                    .doi(doi.unwrap_or_default())
                    .published_date(published_date.unwrap_or_default())
                    .build(),
            );
        }

        Ok(papers)
    }
}

impl Default for PubMedSource {
    fn default() -> Self {
        Self::new().expect("Failed to create PubMedSource")
    }
}

#[async_trait]
impl Source for PubMedSource {
    fn id(&self) -> &str {
        "pubmed"
    }

    fn name(&self) -> &str {
        "PubMed"
    }

    fn capabilities(&self) -> SourceCapabilities {
        SourceCapabilities::SEARCH
    }

    async fn search(&self, query: &SearchQuery) -> Result<SearchResponse, SourceError> {
        let search_url = format!("{}?{}", PUBMED_ESEARCH_URL, self.build_search_url(query));

        // Clone values for retry closure
        let client = Arc::clone(&self.client);
        let search_url_for_retry = search_url.clone();

        let xml =
            with_retry(api_retry_config(), || {
                let client = Arc::clone(&client);
                let url = search_url_for_retry.clone();
                async move {
                    let response = client.get(&url).send().await.map_err(|e| {
                        SourceError::Network(format!("Failed to search PubMed: {}", e))
                    })?;

                    if !response.status().is_success() {
                        let status = response.status();
                        // Handle rate limiting
                        if status == reqwest::StatusCode::TOO_MANY_REQUESTS {
                            tracing::debug!("PubMed API rate-limited - returning empty results");
                            return Err(SourceError::Api("PubMed rate-limited".to_string()));
                        }
                        // Check for service unavailable
                        if status == reqwest::StatusCode::SERVICE_UNAVAILABLE {
                            tracing::debug!("PubMed API unavailable - returning empty results");
                            return Err(SourceError::Api("PubMed unavailable".to_string()));
                        }
                        return Err(SourceError::Api(format!(
                            "PubMed API returned status: {}",
                            response.status()
                        )));
                    }

                    response.text().await.map_err(|e| {
                        SourceError::Network(format!("Failed to read response: {}", e))
                    })
                }
            })
            .await;

        // Handle rate limiting gracefully
        let xml = match xml {
            Ok(x) => x,
            Err(SourceError::Api(msg)) if msg.contains("rate-limited") => {
                tracing::debug!("PubMed rate-limited - returning empty results");
                return Ok(SearchResponse::new(vec![], "PubMed", &query.query));
            }
            Err(SourceError::Api(msg)) if msg.contains("unavailable") => {
                tracing::debug!("PubMed unavailable - returning empty results");
                return Ok(SearchResponse::new(vec![], "PubMed", &query.query));
            }
            Err(e) => return Err(e),
        };

        let ids = Self::parse_search_response(&xml)?;

        if ids.is_empty() {
            return Ok(SearchResponse::new(vec![], "PubMed", &query.query));
        }

        // Fetch details for each paper (batch request)
        let fetch_url = Self::build_fetch_url(&ids);

        let client = Arc::clone(&self.client);
        let fetch_url_for_retry = fetch_url.clone();

        let fetch_xml = with_retry(api_retry_config(), || {
            let client = Arc::clone(&client);
            let url = fetch_url_for_retry.clone();
            async move {
                let response = client.get(&url).send().await.map_err(|e| {
                    SourceError::Network(format!("Failed to fetch PubMed details: {}", e))
                })?;

                if !response.status().is_success() {
                    return Err(SourceError::Api(format!(
                        "PubMed API returned status: {}",
                        response.status()
                    )));
                }

                response
                    .text()
                    .await
                    .map_err(|e| SourceError::Network(format!("Failed to read response: {}", e)))
            }
        })
        .await?;

        let papers = Self::parse_fetch_response(&fetch_xml)?;

        Ok(SearchResponse::new(papers, "PubMed", &query.query))
    }
}

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

    #[test]
    fn test_build_search_url() {
        let source = PubMedSource::new().unwrap();
        let query = SearchQuery::new("machine learning").max_results(10);
        let url = source.build_search_url(&query);

        assert!(url.contains("db=pubmed"));
        assert!(url.contains("term=machine%20learning"));
        assert!(url.contains("retmax=10"));
        assert!(url.contains("retmode=xml"));
    }

    #[test]
    fn test_build_search_url_with_year() {
        let source = PubMedSource::new().unwrap();
        let query = SearchQuery::new("cancer").year("2020");
        let url = source.build_search_url(&query);

        assert!(url.contains("2020-01-01"));
        assert!(url.contains("2020-12-31"));
    }

    #[test]
    fn test_build_search_url_with_year_range() {
        let source = PubMedSource::new().unwrap();
        let query = SearchQuery::new("cancer").year("2015-2020");
        let url = source.build_search_url(&query);

        assert!(url.contains("2015-01-01"));
        assert!(url.contains("2020-12-31"));
    }

    #[test]
    fn test_build_search_url_with_year_from() {
        let source = PubMedSource::new().unwrap();
        let query = SearchQuery::new("cancer").year("2020-");
        let url = source.build_search_url(&query);

        assert!(url.contains("2020-01-01"));
    }

    #[test]
    fn test_build_search_url_with_year_until() {
        let source = PubMedSource::new().unwrap();
        let query = SearchQuery::new("cancer").year("-2020");
        let url = source.build_search_url(&query);

        // The implementation uses reldate format for until year
        assert!(url.contains("2020-01-01"));
    }

    #[test]
    fn test_build_search_url_with_author() {
        let source = PubMedSource::new().unwrap();
        let query = SearchQuery::new("cancer").author("Smith J");
        let url = source.build_search_url(&query);

        assert!(url.contains("Smith%20J%5BAUTH%5D"));
    }

    #[test]
    fn test_build_search_url_complex() {
        let source = PubMedSource::new().unwrap();
        let query = SearchQuery::new("cancer")
            .year("2019-2021")
            .author("Smith J")
            .max_results(50);
        let url = source.build_search_url(&query);

        assert!(url.contains("term=cancer"));
        assert!(url.contains("term=Smith%20J%5BAUTH%5D"));
        assert!(url.contains("retmax=50"));
        assert!(url.contains("2019-01-01"));
        assert!(url.contains("2021-12-31"));
    }
}