Update mcp/mygene.py
Browse files- mcp/mygene.py +35 -9
mcp/mygene.py
CHANGED
@@ -1,11 +1,37 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from functools import lru_cache
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
@lru_cache(maxsize=512)
|
5 |
-
async def fetch_gene_info(
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
MyGene.info async helper
|
3 |
+
Docs: https://docs.mygene.info/en/v2/doc/query_service.html (public, key-free) 📄
|
4 |
+
"""
|
5 |
+
|
6 |
+
from __future__ import annotations
|
7 |
+
import os, asyncio, httpx
|
8 |
from functools import lru_cache
|
9 |
+
from typing import Dict
|
10 |
+
|
11 |
+
_URL = "https://mygene.info/v3/query" # official endpoint :contentReference[oaicite:1]{index=1}
|
12 |
+
_KEY = os.getenv("MYGENE_KEY") # optional; see FAQ :contentReference[oaicite:2]{index=2}
|
13 |
+
_FIELDS = "symbol,name,summary,alias,entrezgene,location"
|
14 |
+
_HDR = {"Accept": "application/json"}
|
15 |
+
_TIMEOUT = 8
|
16 |
+
_MAX_RETRY = 2
|
17 |
+
|
18 |
@lru_cache(maxsize=512)
|
19 |
+
async def fetch_gene_info(symbol: str) -> Dict:
|
20 |
+
"""
|
21 |
+
Return first MyGene.info hit or {} (never raises).
|
22 |
+
"""
|
23 |
+
params = {"q": symbol, "fields": _FIELDS, "size": 1}
|
24 |
+
if _KEY: params["api_key"] = _KEY
|
25 |
+
|
26 |
+
delay = 0.0
|
27 |
+
for _ in range(_MAX_RETRY):
|
28 |
+
try:
|
29 |
+
async with httpx.AsyncClient(timeout=_TIMEOUT, headers=_HDR) as cli:
|
30 |
+
r = await cli.get(_URL, params=params)
|
31 |
+
r.raise_for_status()
|
32 |
+
return r.json().get("hits", [{}])[0]
|
33 |
+
except (httpx.HTTPStatusError, httpx.ReadTimeout):
|
34 |
+
await asyncio.sleep(delay)
|
35 |
+
delay = 0.8 if delay == 0 else 0 # retry once
|
36 |
+
|
37 |
+
return {} # graceful fallback
|