Update mcp/mygene.py
Browse files- mcp/mygene.py +14 -16
mcp/mygene.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
"""
|
2 |
MyGene.info async helper
|
3 |
-
Docs: https://docs.mygene.info/en/
|
|
|
|
|
4 |
"""
|
5 |
|
6 |
from __future__ import annotations
|
@@ -8,30 +10,26 @@ import os, asyncio, httpx
|
|
8 |
from functools import lru_cache
|
9 |
from typing import Dict
|
10 |
|
11 |
-
_URL = "https://mygene.info/v3/query"
|
12 |
-
_KEY = os.getenv("MYGENE_KEY")
|
13 |
_FIELDS = "symbol,name,summary,alias,entrezgene,location"
|
14 |
-
|
15 |
-
|
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:
|
|
|
25 |
|
26 |
delay = 0.0
|
27 |
-
for _ in range(
|
28 |
try:
|
29 |
-
async with httpx.AsyncClient(timeout=_TIMEOUT
|
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.
|
36 |
-
|
37 |
-
return {} # graceful fallback
|
|
|
1 |
"""
|
2 |
MyGene.info async helper
|
3 |
+
Docs: https://docs.mygene.info/en/latest/api_service.html
|
4 |
+
• Key is OPTIONAL – set MYGENE_KEY only for >500 QPS quota.
|
5 |
+
• One retry on 5xx/timeout then returns {} so UI never crashes.
|
6 |
"""
|
7 |
|
8 |
from __future__ import annotations
|
|
|
10 |
from functools import lru_cache
|
11 |
from typing import Dict
|
12 |
|
13 |
+
_URL = "https://mygene.info/v3/query"
|
14 |
+
_KEY = os.getenv("MYGENE_KEY") # optional
|
15 |
_FIELDS = "symbol,name,summary,alias,entrezgene,location"
|
16 |
+
_TIMEOUT = 8
|
17 |
+
_RETRIES = 2 # initial + one retry
|
|
|
18 |
|
19 |
@lru_cache(maxsize=512)
|
20 |
async def fetch_gene_info(symbol: str) -> Dict:
|
|
|
|
|
|
|
21 |
params = {"q": symbol, "fields": _FIELDS, "size": 1}
|
22 |
+
if _KEY:
|
23 |
+
params["api_key"] = _KEY
|
24 |
|
25 |
delay = 0.0
|
26 |
+
for _ in range(_RETRIES):
|
27 |
try:
|
28 |
+
async with httpx.AsyncClient(timeout=_TIMEOUT) as cli:
|
29 |
r = await cli.get(_URL, params=params)
|
30 |
r.raise_for_status()
|
31 |
return r.json().get("hits", [{}])[0]
|
32 |
except (httpx.HTTPStatusError, httpx.ReadTimeout):
|
33 |
+
await asyncio.sleep(delay or 0.6) # back-off once
|
34 |
+
delay = 0.0 # only retry once
|
35 |
+
return {} # graceful fallback
|
|