download_article
Download arXiv articles as PDFs by title. The tool fetches the article, saves it to a specified location, and uses the arXiv ID as the filename.
Instructions
Download the article hosted on arXiv.org as a PDF file. This tool searches for the article based on its title, retrieves the article's PDF, and saves it to a specified download location using the arXiv ID as the filename.
Args: title: Article title.
Returns: Success or error message.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| title | Yes |
Implementation Reference
- src/arxiv_server/server.py:127-153 (handler)The handler function for the 'download_article' tool, decorated with @mcp.tool() which also serves as registration. It orchestrates article lookup, PDF download, and local file saving.@mcp.tool() async def download_article(title: str) -> str: """ Download the article hosted on arXiv.org as a PDF file. This tool searches for the article based on its title, retrieves the article's PDF, and saves it to a specified download location using the arXiv ID as the filename. Args: title: Article title. Returns: Success or error message. """ result = await get_url_and_arxiv_id(title) if isinstance(result, str): return result article_url, arxiv_id = result pdf_doc = await get_pdf(article_url) if pdf_doc is None: return "Unable to retrieve the article from arXiv.org." file_path = os.path.join(DOWNLOAD_PATH, f"{arxiv_id}.pdf") try: with open(file_path, "wb") as file: file.write(pdf_doc) return f"Download successful. Find the PDF at {DOWNLOAD_PATH}" except Exception: return f"Unable to save the article to local directory."
- src/arxiv_server/server.py:85-93 (helper)Helper that fetches article information and constructs the direct PDF URL and arXiv ID from the title.async def get_url_and_arxiv_id(title: str) -> tuple[str, str] | str: """Get URL of the article hosted on arXiv.org.""" info = await fetch_information(title) if isinstance(info, str): return info arxiv_id = info.id.split("/abs/")[-1] direct_pdf_url = f"https://arxiv.org/pdf/{arxiv_id}" return (direct_pdf_url, arxiv_id)
- src/arxiv_server/server.py:31-43 (helper)Helper function to download PDF bytes from the arXiv URL.async def get_pdf(url: str) -> bytes | None: """Get PDF document as bytes from arXiv.org.""" headers = { "User-Agent": USER_AGENT, "Accept": "application/pdf" } async with httpx.AsyncClient() as client: try: response = await client.get(url, headers=headers, timeout=30.0) response.raise_for_status() return response.content except Exception: return None