search_topics
Search for relevant topics on Weibo using a keyword. Retrieve a list of results with options to specify the result limit and page number for efficient topic discovery.
Instructions
Search for topics on Weibo based on a keyword.
Returns:
list[dict]: List of dictionaries containing search results
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| keyword | Yes | Search term to find content | |
| limit | No | Maximum number of results to return, defaults to 15 | |
| page | No | Page number for pagination, defaults to 1 |
Implementation Reference
- src/mcp_server_weibo/weibo.py:224-256 (handler)Core implementation of the search_topics tool. Performs HTTP request to Weibo search endpoint for topics (containerid=100103type=38), parses JSON response, extracts card group, formats each item using _to_topic_item helper, and returns limited list.async def search_topics(self, keyword: str, limit: int = 15, page: int = 1) -> list[dict]: """ Search Weibo topics by keyword. Args: keyword (str): The search keyword limit (int): Maximum number of topic results to return, defaults to 15 page (int, optional): The starting page number, defaults to 1 Returns: list[dict: List of dict containing topic search results """ async with httpx.AsyncClient() as client: try: params = { 'containerid': f'100103type=38&q={keyword}', 'page_type': 'searchall', 'page': page, } encoded_params = urlencode(params) response = await client.get(f'{SEARCH_URL}?{encoded_params}', headers=DEFAULT_HEADERS) result = response.json() cards = result["data"]["cards"] if len(cards) < 1: return [] else: cardGroup = cards[0]['card_group'] return [self._to_topic_item(item) for item in cardGroup][:limit] except httpx.HTTPError: self.logger.error( f"Unable to search users for keyword '{keyword}'", exc_info=True) return []
- src/mcp_server_weibo/server.py:98-111 (registration)MCP tool registration for search_topics using FastMCP @mcp.tool() decorator. Defines input schema via Annotated Fields with descriptions and defaults. Thin handler that delegates to WeiboCrawler instance.@mcp.tool() async def search_topics( ctx: Context, keyword: Annotated[str, Field(description="Search term to find content")], limit: Annotated[int, Field(description="Maximum number of results to return, defaults to 15", default=15)] = 15, page: Annotated[int, Field(description="Page number for pagination, defaults to 1", default=1)] = 1 ) -> list[dict]: """ Search for topics on Weibo based on a keyword. Returns: list[dict]: List of dictionaries containing search results """ return await crawler.search_topics(keyword, limit, page)
- Supporting helper function used by search_topics to transform raw topic data from Weibo API into a standardized dict with title, desc1, desc2, and url fields.def _to_topic_item(self, item: dict) -> dict: """ Convert raw topic data to a formatted dictionary. Args: item (dict): Raw topic data from Weibo API Returns: dict: Formatted topic information """ return { 'title': item['title_sub'], 'desc1': item.get('desc1', ''), 'desc2': item.get('desc2', ''), 'url': item.get('scheme', '') }