Skip to main content
Glama
rafaljanicki

X (Twitter) MCP server

by rafaljanicki

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
NameRequiredDescriptionDefault
countNo
cursorNo
productNoTop
queryYes

Implementation Reference

  • 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")
  • 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]:
  • 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]

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/rafaljanicki/x-twitter-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server