search_by_topic
Locate academic papers by topic with optional date range and result limit. Retrieve structured search results for integration with AI models.
Instructions
Search for papers by topic with optional date range.
Note: Query length is limited to 300 characters. Longer queries will be automatically truncated.
Args:
topic (str): Search query (max 300 chars)
year_start (int, optional): Start year for date range
year_end (int, optional): End year for date range
limit (int, optional): Maximum number of results to return (default 10)
Returns:
str: Formatted search results or error message
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | ||
| topic | Yes | ||
| year_end | No | ||
| year_start | No |
Input Schema (JSON Schema)
{
"properties": {
"limit": {
"default": 10,
"title": "Limit",
"type": "integer"
},
"topic": {
"title": "Topic",
"type": "string"
},
"year_end": {
"default": null,
"title": "Year End",
"type": "integer"
},
"year_start": {
"default": null,
"title": "Year Start",
"type": "integer"
}
},
"required": [
"topic"
],
"title": "search_by_topicArguments",
"type": "object"
}
Implementation Reference
- server.py:160-210 (handler)The handler function decorated with @mcp.tool() that implements the search_by_topic tool. It queries the Semantic Scholar API for papers matching the topic within the specified year range, formats the results, and falls back to Crossref via search_papers if needed.@mcp.tool() async def search_by_topic(topic: str, year_start: int = None, year_end: int = None, limit: int = 10) -> str: """Search for papers by topic with optional date range. Note: Query length is limited to 300 characters. Longer queries will be automatically truncated. Args: topic (str): Search query (max 300 chars) year_start (int, optional): Start year for date range year_end (int, optional): End year for date range limit (int, optional): Maximum number of results to return (default 10) Returns: str: Formatted search results or error message """ try: # Truncate long queries to prevent API errors MAX_QUERY_LENGTH = 300 if len(topic) > MAX_QUERY_LENGTH: original_length = len(topic) topic = topic[:MAX_QUERY_LENGTH] + "..." # Try Semantic Scholar API first semantic_url = f"{SEMANTIC_SCHOLAR_API}/paper/search" params = { "query": topic.encode('utf-8').decode('utf-8'), "limit": limit, "fields": "title,authors,year,paperId,externalIds,abstract,venue,isOpenAccess,openAccessPdf,tldr" } if year_start and year_end: params["year"] = f"{year_start}-{year_end}" headers = { "Accept": "application/json", "Content-Type": "application/json; charset=utf-8" } data = await make_api_request(semantic_url, headers=headers, params=params) if data and 'data' in data: results = ["=== Search Results ==="] for paper in data['data']: results.append(format_paper_data(paper, "semantic_scholar")) return "\n".join(results) # Fallback to Crossref if Semantic Scholar fails return await search_papers(topic, limit) except Exception as e: return f"Error searching papers!"