paper_ai_analyze
Analyze academic papers using AI to extract core contributions, research methods, and key findings from PDFs or abstracts.
Instructions
使用 AI 分析论文,返回核心贡献、研究方法、关键发现等。
支持任意 OpenAI 兼容 API(通过 AI_API_BASE / AI_API_KEY / AI_MODEL 环境变量配置)。 如果能下载到 PDF,会提取全文进行深度分析;否则退回到 abstract 分析。
Args: doi: 论文的 DOI,例如 "10.1109/tim.2021.3106677"
Returns: AI 分析结果 JSON
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| doi | Yes |
Implementation Reference
- scholar_mcp_server.py:85-204 (handler)The `paper_ai_analyze` tool handler implementation. It uses Crossref to fetch paper metadata, optionally downloads and extracts text from the PDF, and uses an AI API to analyze the content.
def paper_ai_analyze(doi: str) -> str: """使用 AI 分析论文,返回核心贡献、研究方法、关键发现等。 支持任意 OpenAI 兼容 API(通过 AI_API_BASE / AI_API_KEY / AI_MODEL 环境变量配置)。 如果能下载到 PDF,会提取全文进行深度分析;否则退回到 abstract 分析。 Args: doi: 论文的 DOI,例如 "10.1109/tim.2021.3106677" Returns: AI 分析结果 JSON """ if not AI_API_KEY: return json.dumps({"success": False, "error": "AI_API_KEY (or DS_KEY) not set, AI analysis unavailable"}) try: # 获取论文元数据 r = requests.get(f"{CROSSREF}/{requests.utils.quote(doi)}", timeout=10) if r.status_code != 200: return json.dumps({"success": False, "error": f"DOI not found: {doi}"}) item = r.json().get("message", {}) title = (item.get("title") or [""])[0] authors = ", ".join( f"{a.get('given', '')} {a.get('family', '')}" for a in (item.get("author") or []) ) journal = (item.get("container-title") or [""])[0] year = "" if item.get("published") and item["published"].get("date-parts"): year = str(item["published"]["date-parts"][0][0]) abstract = ( (item.get("abstract") or "") .replace("<jats:p>", "").replace("</jats:p>", "") .strip() ) # 尝试获取全文:下载 PDF → 提取文本 full_text = "" analysis_mode = "abstract_only" import tempfile try: tmp_dir = tempfile.mkdtemp(prefix="scholar_mcp_") dl_result = download_paper(doi, tmp_dir) if dl_result.get("success") and dl_result.get("path"): pdf_result = extract_pdf_text(dl_result["path"], max_pages=20, max_chars=12000) if pdf_result.get("success"): full_text = pdf_result["text"] analysis_mode = f"full_text ({pdf_result['pages']} pages, {pdf_result['chars']} chars, {pdf_result['method']})" except Exception: pass # 构建 prompt if full_text: prompt = f"""You are an academic research assistant. Analyze this paper thoroughly in Chinese based on the full text provided. Title: {title} Authors: {authors} Journal: {journal} ({year}) --- FULL TEXT --- {full_text} --- END --- Provide a thorough analysis: 1. **核心贡献**: 一句话概括 2. **研究背景**: 为什么做这个研究(2-3句) 3. **研究方法**: 详细描述使用的方法和技术路线(3-5句) 4. **关键发现**: 3-5个具体的定量或定性结果 5. **创新点**: 与现有研究的具体区别 6. **局限性**: 2-3点 7. **应用前景**: 1-2句""" max_tokens = 1500 else: prompt = f"""You are an academic research assistant. Analyze this paper concisely in Chinese. Title: {title} Authors: {authors} Journal: {journal} ({year}) {"Abstract: " + abstract if abstract else ""} Provide: 1. **核心贡献**: 一句话概括 2. **研究方法**: 2-3句 3. **关键发现**: 2-3个要点 4. **创新点**: 与现有研究的区别 5. **局限性**: 1-2点""" max_tokens = 800 # 调用 AI API api_url = f"{AI_API_BASE.rstrip('/')}/chat/completions" ar = requests.post( api_url, headers={ "Authorization": f"Bearer {AI_API_KEY}", "Content-Type": "application/json", }, json={ "model": AI_MODEL, "messages": [{"role": "user", "content": prompt}], "temperature": 0.3, "max_tokens": max_tokens, }, timeout=120, ) analysis = ar.json()["choices"][0]["message"]["content"] return json.dumps({ "success": True, "doi": doi, "title": title, "authors": authors, "journal": f"{journal} ({year})", "model": AI_MODEL, "analysis_mode": analysis_mode, "analysis": analysis, }, ensure_ascii=False, indent=2) except Exception as e: return json.dumps({"success": False, "error": str(e)}) - scholar_mcp_server.py:84-84 (registration)Registration of the `paper_ai_analyze` tool using the @mcp.tool decorator.
@mcp.tool()