Spaces:
Running
Running
Commit
·
c114650
1
Parent(s):
f5b2917
feat: add MCP tool wrappers for biomedical search tools
Browse files- Introduced a new module `mcp_tools.py` that provides asynchronous search functions for PubMed, ClinicalTrials.gov, and bioRxiv.
- Each function includes detailed Google-style docstrings and type hints, ensuring clarity and usability.
- Implemented a comprehensive search function that aggregates results from all sources, enhancing the search capabilities for biomedical literature.
Files added:
- src/mcp_tools.py
- src/mcp_tools.py +156 -0
src/mcp_tools.py
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""MCP tool wrappers for DeepCritical search tools.
|
| 2 |
+
|
| 3 |
+
These functions expose our search tools via MCP protocol.
|
| 4 |
+
Each function follows the MCP tool contract:
|
| 5 |
+
- Full type hints
|
| 6 |
+
- Google-style docstrings with Args section
|
| 7 |
+
- Formatted string returns
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from src.tools.biorxiv import BioRxivTool
|
| 11 |
+
from src.tools.clinicaltrials import ClinicalTrialsTool
|
| 12 |
+
from src.tools.pubmed import PubMedTool
|
| 13 |
+
|
| 14 |
+
# Singleton instances (avoid recreating on each call)
|
| 15 |
+
_pubmed = PubMedTool()
|
| 16 |
+
_trials = ClinicalTrialsTool()
|
| 17 |
+
_biorxiv = BioRxivTool()
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
async def search_pubmed(query: str, max_results: int = 10) -> str:
|
| 21 |
+
"""Search PubMed for peer-reviewed biomedical literature.
|
| 22 |
+
|
| 23 |
+
Searches NCBI PubMed database for scientific papers matching your query.
|
| 24 |
+
Returns titles, authors, abstracts, and citation information.
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
query: Search query (e.g., "metformin alzheimer", "drug repurposing cancer")
|
| 28 |
+
max_results: Maximum results to return (1-50, default 10)
|
| 29 |
+
|
| 30 |
+
Returns:
|
| 31 |
+
Formatted search results with paper titles, authors, dates, and abstracts
|
| 32 |
+
"""
|
| 33 |
+
max_results = max(1, min(50, max_results)) # Clamp to valid range
|
| 34 |
+
|
| 35 |
+
results = await _pubmed.search(query, max_results)
|
| 36 |
+
|
| 37 |
+
if not results:
|
| 38 |
+
return f"No PubMed results found for: {query}"
|
| 39 |
+
|
| 40 |
+
formatted = [f"## PubMed Results for: {query}\n"]
|
| 41 |
+
for i, evidence in enumerate(results, 1):
|
| 42 |
+
formatted.append(f"### {i}. {evidence.citation.title}")
|
| 43 |
+
formatted.append(f"**Authors**: {', '.join(evidence.citation.authors[:3])}")
|
| 44 |
+
formatted.append(f"**Date**: {evidence.citation.date}")
|
| 45 |
+
formatted.append(f"**URL**: {evidence.citation.url}")
|
| 46 |
+
formatted.append(f"\n{evidence.content}\n")
|
| 47 |
+
|
| 48 |
+
return "\n".join(formatted)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
async def search_clinical_trials(query: str, max_results: int = 10) -> str:
|
| 52 |
+
"""Search ClinicalTrials.gov for clinical trial data.
|
| 53 |
+
|
| 54 |
+
Searches the ClinicalTrials.gov database for trials matching your query.
|
| 55 |
+
Returns trial titles, phases, status, conditions, and interventions.
|
| 56 |
+
|
| 57 |
+
Args:
|
| 58 |
+
query: Search query (e.g., "metformin alzheimer", "diabetes phase 3")
|
| 59 |
+
max_results: Maximum results to return (1-50, default 10)
|
| 60 |
+
|
| 61 |
+
Returns:
|
| 62 |
+
Formatted clinical trial information with NCT IDs, phases, and status
|
| 63 |
+
"""
|
| 64 |
+
max_results = max(1, min(50, max_results))
|
| 65 |
+
|
| 66 |
+
results = await _trials.search(query, max_results)
|
| 67 |
+
|
| 68 |
+
if not results:
|
| 69 |
+
return f"No clinical trials found for: {query}"
|
| 70 |
+
|
| 71 |
+
formatted = [f"## Clinical Trials for: {query}\n"]
|
| 72 |
+
for i, evidence in enumerate(results, 1):
|
| 73 |
+
formatted.append(f"### {i}. {evidence.citation.title}")
|
| 74 |
+
formatted.append(f"**URL**: {evidence.citation.url}")
|
| 75 |
+
formatted.append(f"**Date**: {evidence.citation.date}")
|
| 76 |
+
formatted.append(f"\n{evidence.content}\n")
|
| 77 |
+
|
| 78 |
+
return "\n".join(formatted)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
async def search_biorxiv(query: str, max_results: int = 10) -> str:
|
| 82 |
+
"""Search bioRxiv/medRxiv for preprint research.
|
| 83 |
+
|
| 84 |
+
Searches bioRxiv and medRxiv preprint servers for cutting-edge research.
|
| 85 |
+
Note: Preprints are NOT peer-reviewed but contain the latest findings.
|
| 86 |
+
|
| 87 |
+
Args:
|
| 88 |
+
query: Search query (e.g., "metformin neuroprotection", "long covid treatment")
|
| 89 |
+
max_results: Maximum results to return (1-50, default 10)
|
| 90 |
+
|
| 91 |
+
Returns:
|
| 92 |
+
Formatted preprint results with titles, authors, and abstracts
|
| 93 |
+
"""
|
| 94 |
+
max_results = max(1, min(50, max_results))
|
| 95 |
+
|
| 96 |
+
results = await _biorxiv.search(query, max_results)
|
| 97 |
+
|
| 98 |
+
if not results:
|
| 99 |
+
return f"No bioRxiv/medRxiv preprints found for: {query}"
|
| 100 |
+
|
| 101 |
+
formatted = [f"## Preprint Results for: {query}\n"]
|
| 102 |
+
for i, evidence in enumerate(results, 1):
|
| 103 |
+
formatted.append(f"### {i}. {evidence.citation.title}")
|
| 104 |
+
formatted.append(f"**Authors**: {', '.join(evidence.citation.authors[:3])}")
|
| 105 |
+
formatted.append(f"**Date**: {evidence.citation.date}")
|
| 106 |
+
formatted.append(f"**URL**: {evidence.citation.url}")
|
| 107 |
+
formatted.append(f"\n{evidence.content}\n")
|
| 108 |
+
|
| 109 |
+
return "\n".join(formatted)
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
async def search_all_sources(query: str, max_per_source: int = 5) -> str:
|
| 113 |
+
"""Search all biomedical sources simultaneously.
|
| 114 |
+
|
| 115 |
+
Performs parallel search across PubMed, ClinicalTrials.gov, and bioRxiv.
|
| 116 |
+
This is the most comprehensive search option for drug repurposing research.
|
| 117 |
+
|
| 118 |
+
Args:
|
| 119 |
+
query: Search query (e.g., "metformin alzheimer", "aspirin cancer prevention")
|
| 120 |
+
max_per_source: Maximum results per source (1-20, default 5)
|
| 121 |
+
|
| 122 |
+
Returns:
|
| 123 |
+
Combined results from all sources with source labels
|
| 124 |
+
"""
|
| 125 |
+
import asyncio
|
| 126 |
+
|
| 127 |
+
max_per_source = max(1, min(20, max_per_source))
|
| 128 |
+
|
| 129 |
+
# Run all searches in parallel
|
| 130 |
+
pubmed_task = search_pubmed(query, max_per_source)
|
| 131 |
+
trials_task = search_clinical_trials(query, max_per_source)
|
| 132 |
+
biorxiv_task = search_biorxiv(query, max_per_source)
|
| 133 |
+
|
| 134 |
+
pubmed_results, trials_results, biorxiv_results = await asyncio.gather(
|
| 135 |
+
pubmed_task, trials_task, biorxiv_task, return_exceptions=True
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
formatted = [f"# Comprehensive Search: {query}\n"]
|
| 139 |
+
|
| 140 |
+
# Add each result section (handle exceptions gracefully)
|
| 141 |
+
if isinstance(pubmed_results, str):
|
| 142 |
+
formatted.append(pubmed_results)
|
| 143 |
+
else:
|
| 144 |
+
formatted.append(f"## PubMed\n*Error: {pubmed_results}*\n")
|
| 145 |
+
|
| 146 |
+
if isinstance(trials_results, str):
|
| 147 |
+
formatted.append(trials_results)
|
| 148 |
+
else:
|
| 149 |
+
formatted.append(f"## Clinical Trials\n*Error: {trials_results}*\n")
|
| 150 |
+
|
| 151 |
+
if isinstance(biorxiv_results, str):
|
| 152 |
+
formatted.append(biorxiv_results)
|
| 153 |
+
else:
|
| 154 |
+
formatted.append(f"## Preprints\n*Error: {biorxiv_results}*\n")
|
| 155 |
+
|
| 156 |
+
return "\n---\n".join(formatted)
|