search_tweets
Find relevant tweets by providing a search query and specifying the number of results (10-100).
Instructions
Search for tweets on Twitter
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Search query | |
| count | Yes | Number of tweets to return (10-100) |
Implementation Reference
- src/index.ts:149-175 (handler)The handleSearchTweets method validates args via SearchTweetsSchema, calls TwitterClient.searchTweets(), formats the response using ResponseFormatter, and returns MCP TextContent.
private async handleSearchTweets(args: unknown) { const result = SearchTweetsSchema.safeParse(args); if (!result.success) { throw new McpError( ErrorCode.InvalidParams, `Invalid parameters: ${result.error.message}` ); } const { tweets, users } = await this.client.searchTweets( result.data.query, result.data.count ); const formattedResponse = ResponseFormatter.formatSearchResponse( result.data.query, tweets, users ); return { content: [{ type: 'text', text: ResponseFormatter.toMcpResponse(formattedResponse) }] as TextContent[] }; } - src/types.ts:21-27 (schema)Zod schema for search_tweets input validation: requires 'query' (non-empty string) and 'count' (integer between 10 and 100).
export const SearchTweetsSchema = z.object({ query: z.string().min(1, 'Search query cannot be empty'), count: z.number() .int('Count must be an integer') .min(10, 'Minimum count is 10') .max(100, 'Maximum count is 100') }); - src/index.ts:85-106 (registration)Registration of 'search_tweets' as a named tool in the ListToolsRequestSchema handler, with inputSchema specifying query (string) and count (number, 10-100).
{ name: 'search_tweets', description: 'Search for tweets on Twitter', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query' }, count: { type: 'number', description: 'Number of tweets to return (10-100)', minimum: 10, maximum: 100 } }, required: ['query', 'count'] } } as Tool ] })); - src/index.ts:117-128 (registration)Dispatch in the CallToolRequestSchema handler that routes 'search_tweets' calls to handleSearchTweets.
case 'search_tweets': return await this.handleSearchTweets(args); default: throw new McpError( ErrorCode.MethodNotFound, `Unknown tool: ${name}` ); } } catch (error) { return this.handleError(error); } }); - src/twitter-api.ts:42-80 (helper)TwitterClient.searchTweets() method that calls Twitter API v2 search with expansions, maps response to Tweet/TwitterUser objects.
async searchTweets(query: string, count: number): Promise<{ tweets: Tweet[], users: TwitterUser[] }> { try { const endpoint = 'tweets/search'; await this.checkRateLimit(endpoint); const response = await this.client.v2.search(query, { max_results: count, expansions: ['author_id'], 'tweet.fields': ['public_metrics', 'created_at'], 'user.fields': ['username', 'name', 'verified'] }); console.error(`Fetched ${response.tweets.length} tweets for query: "${query}"`); const tweets = response.tweets.map(tweet => ({ id: tweet.id, text: tweet.text, authorId: tweet.author_id ?? '', metrics: { likes: tweet.public_metrics?.like_count ?? 0, retweets: tweet.public_metrics?.retweet_count ?? 0, replies: tweet.public_metrics?.reply_count ?? 0, quotes: tweet.public_metrics?.quote_count ?? 0 }, createdAt: tweet.created_at ?? '' })); const users = response.includes.users.map(user => ({ id: user.id, username: user.username, name: user.name, verified: user.verified ?? false })); return { tweets, users }; } catch (error) { this.handleApiError(error); } }