mgbam commited on
Commit
686ea1e
·
verified ·
1 Parent(s): c30e46a

Update mcp/mygene.py

Browse files
Files changed (1) hide show
  1. 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
- _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
 
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]