Update mcp/mygene.py
Browse files- mcp/mygene.py +15 -29
mcp/mygene.py
CHANGED
|
@@ -1,35 +1,21 @@
|
|
| 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
|
| 9 |
-
import os, asyncio, httpx
|
| 10 |
from functools import lru_cache
|
| 11 |
-
from typing import Dict
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 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(
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
if _KEY:
|
| 23 |
params["api_key"] = _KEY
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 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
|
|
|
|
| 1 |
+
import os, httpx
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from functools import lru_cache
|
|
|
|
| 3 |
|
| 4 |
+
_KEY = os.getenv("MYGENE_KEY")
|
| 5 |
+
_URL = "https://mygene.info/v3/query"
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
@lru_cache(maxsize=512)
|
| 8 |
+
async def fetch_gene_info(sym: str) -> dict:
|
| 9 |
+
if not sym:
|
| 10 |
+
return {}
|
| 11 |
+
params = {
|
| 12 |
+
"q" : sym,
|
| 13 |
+
"fields": "symbol,name,summary,alias,entrezgene,location",
|
| 14 |
+
"size" : 1,
|
| 15 |
+
}
|
| 16 |
if _KEY:
|
| 17 |
params["api_key"] = _KEY
|
| 18 |
+
async with httpx.AsyncClient(timeout=8) as c:
|
| 19 |
+
r = await c.get(_URL, params=params)
|
| 20 |
+
r.raise_for_status()
|
| 21 |
+
return r.json().get("hits", [{}])[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|