search_tweets
Find tweets matching a search query and retrieve up to 100 results. Use this tool to analyze or monitor social media content on X (Twitter) with precision.
Instructions
Search for tweets using a query
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| count | No | Number of tweets to retrieve (max 100) | |
| query | Yes | Search query for tweets |
Implementation Reference
- src/index.ts:232-263 (handler)The handler function that executes the search_tweets tool. It validates input, searches tweets using Twitter API v2, formats results, and returns them.private async searchTweets(args: any) { if (!this.twitterClient) { throw new Error("Twitter client not initialized"); } const { query, count = 10 } = args; if (!query || typeof query !== "string") { throw new Error("Query is required and must be a string"); } const searchCount = Math.min(count, 100); const tweets = await this.twitterClient.v2.search(query, { max_results: searchCount, "tweet.fields": ["created_at", "author_id"], }); const tweetList = tweets.data.data || []; const formattedTweets = tweetList .map((tweet) => `- ${tweet.text} (by @${tweet.author_id})`) .join("\n"); return { content: [ { type: "text", text: `Search results for "${query}":\n${formattedTweets}`, }, ], }; }
- src/index.ts:64-82 (schema)The input schema definition for the search_tweets tool, defining the expected parameters: query (required string) and optional count.{ name: "search_tweets", description: "Search for tweets using a query", inputSchema: { type: "object", properties: { query: { type: "string", description: "Search query for tweets", }, count: { type: "number", description: "Number of tweets to retrieve (max 100)", default: 10, }, }, required: ["query"], }, },
- src/index.ts:113-114 (registration)The registration/dispatch point in the tool handler switch statement that routes calls to search_tweets to the searchTweets method.case "search_tweets": return await this.searchTweets(args);