get_compound_info
Retrieve compound properties like CID, molecular formula, and weight from PubChem for chemical safety research and analysis.
Instructions
获取化合物基础信息
Args: name: 化合物名称
Returns: 包含CID、分子式、分子量等基础信息的字典
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes |
Implementation Reference
- pubchem_mcp/mcp_server.py:21-69 (handler)The main handler function for the `get_compound_info` tool in the MCP server. It manages cache checking, PubChem API interaction via `PubChemClient`, and result parsing.
@app.tool() async def get_compound_info(ctx: Context, name: str) -> Dict[str, Any]: """ 获取化合物基础信息 Args: name: 化合物名称 Returns: 包含CID、分子式、分子量等基础信息的字典 """ try: # 初始化缓存服务 cache_service = CacheService() await cache_service.initialize() # 检查缓存 cached_data = await cache_service.get_compound_info(name) if cached_data: await cache_service.close() return cached_data # 从PubChem获取数据 async with PubChemClient() as client: # 获取CID cid = await client.get_compound_cid(name) if not cid: await cache_service.close() return {"name": name, "error": "Compound not found"} # 获取基础信息 compound_data = await client.get_compound_by_name(name) if "error" in compound_data: await cache_service.close() return {"name": name, "error": compound_data["error"]} # 解析数据 info = _parse_compound_data(name, cid, compound_data) # 缓存结果 await cache_service.set_compound_info(name, info.model_dump()) await cache_service.close() return info.model_dump() except Exception as e: logger.error(f"Error getting compound info for {name}: {e}") return {"name": name, "error": str(e)} - pubchem_mcp/mcp_server.py:21-21 (registration)The `get_compound_info` tool is registered here using the `@app.tool()` decorator from the `FastMCP` framework.
@app.tool() - pubchem_mcp/mcp_server.py:156-177 (helper)A helper function `_parse_compound_data` used by `get_compound_info` to transform raw PubChem API responses into the structured `CompoundInfo` model.
def _parse_compound_data(name: str, cid: int, data: Dict[str, Any]) -> CompoundInfo: """解析化合物数据""" try: properties = data.get("PropertyTable", {}).get("Properties", []) if not properties: return CompoundInfo(name=name, cid=cid) props = properties[0] return CompoundInfo( cid=cid, name=name, molecular_formula=props.get("MolecularFormula"), molecular_weight=props.get("MolecularWeight"), iupac_name=props.get("IUPACName"), smiles=props.get("IsomericSMILES"), inchi_key=props.get("InChIKey"), synonyms=[] # 需要额外API调用获取 ) except Exception as e: logger.error(f"Error parsing compound data: {e}") return CompoundInfo(name=name, cid=cid)