download_paper
Download PDFs of arXiv papers to a local directory using the paper's arXiv ID for offline access or analysis.
Instructions
Downloads the PDF of a paper to a local directory on the server. NOTE: In a stateless/free hosting environment, this file is temporary and will be deleted when the server restarts or sleeps.
:param arxiv_id: The ArXiv ID of the paper to download (e.g., '2301.12345'). :param directory: The local directory where the paper will be saved.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| arxiv_id | Yes | ||
| directory | No | downloaded_papers |
Implementation Reference
- arxiv_searcher/arxiv_mcp.py:739-778 (handler)Synchronous handler function for the 'download_paper' MCP tool. Downloads arXiv paper PDF to a local directory using the arxiv library.@mcp.tool def download_paper(arxiv_id: str, directory: str = "downloaded_papers") -> dict: """ Downloads the PDF of a paper to a local directory on the server. NOTE: In a stateless/free hosting environment, this file is temporary and will be deleted when the server restarts or sleeps. :param arxiv_id: The ArXiv ID of the paper to download (e.g., '2301.12345'). :param directory: The local directory where the paper will be saved. """ try: # Ensure the download directory exists os.makedirs(directory, exist_ok=True) search = arxiv.Search(id_list=[arxiv_id]) paper = next(search.results()) # Define a clean filename to avoid issues with special characters clean_id = re.sub(r'[^0-9v.]', '_', arxiv_id) filename = f"{clean_id}.pdf" # Download the paper to the specified directory paper.download_pdf(dirpath=directory, filename=filename) filepath = os.path.join(directory, filename) logging.info(f"Paper {arxiv_id} downloaded to {filepath}") return { "success": True, "arxiv_id": arxiv_id, "local_path": filepath, "message": f"Paper is temporarily available at the server path: {filepath}" } except StopIteration: logging.error(f"Paper with ID {arxiv_id} not found.") return {"success": False, "error": f"Paper with ID {arxiv_id} not found."} except Exception as e: logging.error(f"Failed to download paper {arxiv_id}: {e}") return {"success": False, "error": f"An unexpected error occurred: {str(e)}"}
- Asynchronous handler function for the 'download_paper' MCP tool in the remote version. Downloads arXiv paper PDF to a local directory using the arxiv library.@mcp.tool async def download_paper(arxiv_id: str, directory: str = "downloaded_papers") -> dict: """ Downloads the PDF of a paper to a local directory on the server. NOTE: In a stateless/free hosting environment, this file is temporary and will be deleted when the server restarts or sleeps. :param arxiv_id: The ArXiv ID of the paper to download (e.g., '2301.12345'). :param directory: The local directory where the paper will be saved. """ try: # Ensure the download directory exists os.makedirs(directory, exist_ok=True) search = arxiv.Search(id_list=[arxiv_id]) paper = next(search.results()) # Define a clean filename to avoid issues with special characters clean_id = re.sub(r'[^0-9v.]', '_', arxiv_id) filename = f"{clean_id}.pdf" # Download the paper to the specified directory paper.download_pdf(dirpath=directory, filename=filename) filepath = os.path.join(directory, filename) logging.info(f"Paper {arxiv_id} downloaded to {filepath}") return { "success": True, "arxiv_id": arxiv_id, "local_path": filepath, "message": f"Paper is temporarily available at the server path: {filepath}" } except StopIteration: logging.error(f"Paper with ID {arxiv_id} not found.") return {"success": False, "error": f"Paper with ID {arxiv_id} not found."} except Exception as e: logging.error(f"Failed to download paper {arxiv_id}: {e}") return {"success": False, "error": f"An unexpected error occurred: {str(e)}"}