|
| 1 | +"""MCP server implementation using fastmcp.""" |
| 2 | + |
| 3 | +from datetime import date |
| 4 | + |
| 5 | +from fastmcp import FastMCP |
| 6 | + |
| 7 | +from ..config import config |
| 8 | +from .client import PaperoniAPIClient |
| 9 | + |
| 10 | + |
| 11 | +def create_mcp(endpoint: str = None): |
| 12 | + api_client = PaperoniAPIClient( |
| 13 | + endpoint or config.mcp.api_client.endpoint, |
| 14 | + token=config.mcp.api_client.token, |
| 15 | + fetch=config.mcp.api_client.fetch, |
| 16 | + ) |
| 17 | + |
| 18 | + mcp = FastMCP(name="Paperoni") |
| 19 | + |
| 20 | + @mcp.tool |
| 21 | + def search_papers( |
| 22 | + paper_id: int = None, |
| 23 | + title: str = None, |
| 24 | + institution: str = None, |
| 25 | + author: str = None, |
| 26 | + venue: str = None, |
| 27 | + start_date: date = None, |
| 28 | + end_date: date = None, |
| 29 | + include_flags: list[str] = None, |
| 30 | + exclude_flags: list[str] = None, |
| 31 | + query: str = None, |
| 32 | + similarity_threshold: float = 0.75, |
| 33 | + offset: int = 0, |
| 34 | + limit: int = 100, |
| 35 | + ): |
| 36 | + """Search for papers in the paperoni collection. |
| 37 | +
|
| 38 | + This tool allows searching for papers using various filters including |
| 39 | + semantic search, institution, venue, author, and date ranges. Results |
| 40 | + include paper metadata (title, abstract, authors, venues, topics) but |
| 41 | + exclude PDF content. |
| 42 | +
|
| 43 | + If a query is provided, the results will be sorted by similarity score. |
| 44 | + Note that unrelated papers may still be returned with a low similarity |
| 45 | + score. |
| 46 | +
|
| 47 | + Args: |
| 48 | + paper_id: Paper ID |
| 49 | + title: Title of the paper |
| 50 | + institution: Institution of an author |
| 51 | + author: Author of the paper |
| 52 | + venue: Venue name (long or short) |
| 53 | + start_date: Start date to consider |
| 54 | + end_date: End date to consider |
| 55 | + include_flags: Flags that must be present |
| 56 | + exclude_flags: Flags that must not be present |
| 57 | + query: Semantic search query |
| 58 | + similarity_threshold: Similarity threshold (default: 0.75) |
| 59 | + offset: Pagination offset (default: 0) |
| 60 | + limit: Maximum number of results to return (default: 100) |
| 61 | +
|
| 62 | + Returns: |
| 63 | + Dictionary containing: |
| 64 | + - results: List of paper objects with metadata |
| 65 | + - similarities: List of similarity scores (None if no query was provided) |
| 66 | + - count: Number of results in this page |
| 67 | + - next_offset: Offset for next page (None if no more pages) |
| 68 | + """ |
| 69 | + |
| 70 | + return api_client.search_papers( |
| 71 | + paper_id=paper_id, |
| 72 | + title=title, |
| 73 | + institution=institution, |
| 74 | + author=author, |
| 75 | + venue=venue, |
| 76 | + start_date=start_date, |
| 77 | + end_date=end_date, |
| 78 | + include_flags=include_flags, |
| 79 | + exclude_flags=exclude_flags, |
| 80 | + query=query, |
| 81 | + similarity_threshold=similarity_threshold, |
| 82 | + offset=offset, |
| 83 | + limit=limit, |
| 84 | + ) |
| 85 | + |
| 86 | + @mcp.tool |
| 87 | + def count_papers( |
| 88 | + paper_id: int = None, |
| 89 | + title: str = None, |
| 90 | + institution: str = None, |
| 91 | + author: str = None, |
| 92 | + venue: str = None, |
| 93 | + start_date: date = None, |
| 94 | + end_date: date = None, |
| 95 | + include_flags: list[str] = None, |
| 96 | + exclude_flags: list[str] = None, |
| 97 | + ) -> int: |
| 98 | + """Count papers matching criteria without fetching all results. |
| 99 | +
|
| 100 | + Args: |
| 101 | + paper_id: Paper ID |
| 102 | + title: Title of the paper |
| 103 | + institution: Institution of an author |
| 104 | + author: Author of the paper |
| 105 | + venue: Venue name (long or short) |
| 106 | + start_date: Start date to consider |
| 107 | + end_date: End date to consider |
| 108 | + include_flags: Flags that must be present |
| 109 | + exclude_flags: Flags that must not be present |
| 110 | +
|
| 111 | + Returns: |
| 112 | + Total count of matching papers |
| 113 | + """ |
| 114 | + |
| 115 | + return api_client.count_papers( |
| 116 | + paper_id=paper_id, |
| 117 | + title=title, |
| 118 | + institution=institution, |
| 119 | + author=author, |
| 120 | + venue=venue, |
| 121 | + start_date=start_date, |
| 122 | + end_date=end_date, |
| 123 | + include_flags=include_flags, |
| 124 | + exclude_flags=exclude_flags, |
| 125 | + ) |
| 126 | + |
| 127 | + return mcp |
0 commit comments