Skip to main content
Glama
baryhuang

MCP Server - Twitter NoAuth

twitter_search_tweets

Search for tweets using a specified query and return results via the Twitter API, without requiring local credential setup. Ideal for extracting tweet data programmatically.

Instructions

Search for tweets using the Twitter API

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
max_resultsNoMaximum number of tweets to return (default: 10)
queryYesThe search query to execute
twitter_access_tokenYesTwitter OAuth2 access token

Implementation Reference

  • Core handler logic for the twitter_search_tweets tool: TwitterClient.search_tweets method performs the actual API request to Twitter's search endpoint.
    def search_tweets(self, query: str, max_results: int = 10) -> str: """Search for tweets using the Twitter API Args: query: The search query to execute max_results: Maximum number of tweets to return (default: 10) Returns: JSON string with search results """ try: if not self.access_token: return json.dumps({ "error": "No valid access token provided. Please refresh your token first.", "status": "error" }) logger.debug(f"Searching tweets with query: {query}, max_results: {max_results}") # Twitter API v2 search recent endpoint url = f"{self.api_base_url}/tweets/search/recent" headers = { "Authorization": f"Bearer {self.access_token}" } params = { "query": query, "max_results": max_results, "tweet.fields": "id,text,created_at,author_id", "expansions": "author_id", "user.fields": "id,name,username" } response = requests.get(url, headers=headers, params=params) response.raise_for_status() # Return the raw JSON response return json.dumps(response.json()) except requests.exceptions.RequestException as e: logger.error(f"API request error: {str(e)}") return json.dumps({"error": str(e), "status": "error"}) except Exception as e: logger.error(f"Exception in search_tweets: {str(e)}") return json.dumps({"error": str(e), "status": "error"})
  • Registration of the twitter_search_tweets tool in the MCP server's list_tools decorator, defining name, description, and input schema.
    types.Tool( name="twitter_search_tweets", description="Search for tweets using the Twitter API", inputSchema={ "type": "object", "properties": { "twitter_access_token": {"type": "string", "description": "Twitter OAuth2 access token"}, "query": {"type": "string", "description": "The search query to execute"}, "max_results": {"type": "integer", "description": "Maximum number of tweets to return (default: 10)"} }, "required": ["twitter_access_token", "query"] }, ),
  • Dispatch handler in @server.call_tool() that extracts arguments and calls TwitterClient.search_tweets for the twitter_search_tweets tool.
    if name == "twitter_search_tweets": query = arguments.get("query") max_results = int(arguments.get("max_results", 10)) if not query: raise ValueError("query is required for twitter_search_tweets") results = twitter.search_tweets(query=query, max_results=max_results) return [types.TextContent(type="text", text=results)]
  • Input schema definition for the twitter_search_tweets tool, specifying required parameters and types.
    "type": "object", "properties": { "twitter_access_token": {"type": "string", "description": "Twitter OAuth2 access token"}, "query": {"type": "string", "description": "The search query to execute"}, "max_results": {"type": "integer", "description": "Maximum number of tweets to return (default: 10)"} }, "required": ["twitter_access_token", "query"]

Other Tools

Related Tools

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/baryhuang/mcp-twitter-noauth'

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