MCP_Res / mcp /mygene.py
mgbam's picture
Update mcp/mygene.py
41bca22 verified
raw
history blame
880 Bytes
#!/usr/bin/env python3
"""Async wrapper for MyGene.info API (https://mygene.info)
Provides a single helper:
• `fetch_gene_info(query)` → returns the first matching hit dict or `{}`.
No API‑key needed; community tier allows 500 QPS.
"""
from __future__ import annotations
import httpx
from functools import lru_cache
_BASE = "https://mygene.info/v3"
@lru_cache(maxsize=128)
async def fetch_gene_info(query: str) -> dict: # noqa: D401
"""Return first hit for *query* or an empty dict."""
params = {
"q": query,
"fields": "symbol,name,summary,alias,entrezgene,clinvar,location,go",
"size": 1,
}
async with httpx.AsyncClient(timeout=10) as client:
resp = await client.get(f"{_BASE}/query", params=params)
resp.raise_for_status()
hits = resp.json().get("hits", [])
return hits[0] if hits else {}