search_twitter
Query X (Twitter) to retrieve specific tweets, filter results by product, count, or cursor for precise and targeted data collection.
Instructions
Search Twitter with a query
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| count | No | ||
| cursor | No | ||
| product | No | Top | |
| query | Yes |
Implementation Reference
- src/x_twitter_mcp/server.py:365-365 (registration)Registers the 'search_twitter' tool with FastMCP server using the @server.tool decorator, specifying the name and description.@server.tool(name="search_twitter", description="Search Twitter with a query")
- src/x_twitter_mcp/server.py:366-366 (schema)Defines the input schema via type hints: query (str), product (Optional[str]), count (Optional[int]), cursor (Optional[str]); output as List[Dict]. Includes docstring with parameter descriptions.async def search_twitter(query: str, product: Optional[str] = "Top", count: Optional[int] = 100, cursor: Optional[str] = None) -> List[Dict]:
- src/x_twitter_mcp/server.py:367-391 (handler)Executes the tool: determines sort_order from product, clamps count to 10-100, initializes Twitter client, performs search_recent_tweets, returns list of tweet dictionaries."""Searches Twitter for recent tweets. Args: query (str): The search query. Supports operators like #hashtag, from:user, etc. product (Optional[str]): Sorting preference. 'Top' for relevancy (default), 'Latest' for recency. count (Optional[int]): Number of tweets to retrieve. Default 100. Min 10, Max 100 for search_recent_tweets. cursor (Optional[str]): Pagination token (next_token) for fetching the next set of results. """ sort_order = "relevancy" if product == "Top" else "recency" # Ensure count is within the allowed range (10-100) if count is None: effective_count = 100 # Default to 100 if not provided elif count < 10: logger.info(f"Requested count {count} is less than minimum 10. Using 10 instead.") effective_count = 10 elif count > 100: logger.info(f"Requested count {count} is greater than maximum 100. Using 100 instead.") effective_count = 100 else: effective_count = count client, _ = initialize_twitter_clients() tweets = client.search_recent_tweets(query=query, max_results=effective_count, sort_order=sort_order, next_token=cursor, tweet_fields=["id", "text", "created_at"]) return [tweet.data for tweet in tweets.data]