File size: 832 Bytes
b90d413
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# mcp/umls_rel.py
import os, httpx
from functools import lru_cache

UMLS_API_KEY = os.getenv("UMLS_KEY")
REL_URL      = "https://uts-ws.nlm.nih.gov/rest/content/current/CUI/{cui}/relations"

async def _get_ticket() -> str:
    from mcp.umls import _get_ticket as fn
    return await fn()

@lru_cache(maxsize=512)
async def fetch_relations(cui: str) -> list[dict]:
    ticket = await _get_ticket()
    params = {"ticket": ticket, "pageSize": 50}
    async with httpx.AsyncClient(timeout=10) as c:
        r = await c.get(REL_URL.format(cui=cui), params=params)
        r.raise_for_status()
        rels = r.json().get("result", {}).get("relation", [])
        return [
            {"label": rel["relationLabel"],
             "cui2": rel["relatedId"],
             "name2": rel["relatedLabel"]}
            for rel in rels
        ]