semantic_search_papers_on_huggingface
Find relevant academic papers from HuggingFace using semantic search. Input a query to retrieve a list of papers with titles, summaries, IDs, and upvotes, tailored to your research needs.
Instructions
Search for papers on HuggingFace using semantic search.
Args:
query (str): The query term to search for. It will automatically determine if it should use keywords or a natural language query, so format your queries accordingly.
top_n (int): The number of papers to return. Default is 10, but you can set it to any number.
Returns:
str: A list of papers with the title, summary, ID, and upvotes.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | ||
| top_n | No |
Implementation Reference
- paperpal.py:18-31 (handler)The main handler function decorated with @mcp.tool(), implementing the tool logic by searching HuggingFace papers semantically and formatting the results.@mcp.tool() async def semantic_search_papers_on_huggingface(query: str, top_n: int = 10) -> str: """Search for papers on HuggingFace using semantic search. Args: query (str): The query term to search for. It will automatically determine if it should use keywords or a natural language query, so format your queries accordingly. top_n (int): The number of papers to return. Default is 10, but you can set it to any number. Returns: str: A list of papers with the title, summary, ID, and upvotes. """ papers: list[HuggingFacePaper] = semantic_search_huggingface_papers(query, top_n) return stringify_papers(papers)
- huggingface.py:4-11 (schema)Pydantic BaseModel schema defining the structure of a HuggingFace paper used in the tool's output.class HuggingFacePaper(BaseModel): title: str summary: str arxiv_id: str upvotes: int def __str__(self) -> str: return f"Title: {self.title}\nSummary: {self.summary}\nID: {self.arxiv_id}\nUpvotes: {self.upvotes}"
- huggingface.py:26-40 (helper)Helper function that performs the HTTP request to HuggingFace API for semantic search and parses the papers.def semantic_search_huggingface_papers(query: str, top_n: int) -> list[HuggingFacePaper]: """Search for papers on HuggingFace.""" url = f"https://huggingface.co/api/papers/search?q={query}" try: response = httpx.get(url) response.raise_for_status() papers_json = response.json() papers: list[HuggingFacePaper] = [parse_paper(paper) for paper in papers_json[:top_n]] return papers except Exception as e: return [f"Error fetching papers from HuggingFace. Try again later. {e}"]
- paperpal.py:11-15 (helper)Helper utility to format the list of papers into a readable string output for the tool.def stringify_papers(papers: list[ArxivPaper | HuggingFacePaper]) -> str: """Format a list of papers into a string.""" papers_str = "\n---\n".join([str(paper) for paper in papers]) return f"List of papers:\n---\n{papers_str}\n---\n"