mgbam commited on
Commit
6523621
·
verified ·
1 Parent(s): 5373a3d

Update mcp/mygene.py

Browse files
Files changed (1) hide show
  1. 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/v2/doc/query_service.html (public, key-free) 📄
 
 
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" # 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
 
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