get_user_timeline
Retrieve a user’s most recent tweets from X (Twitter) by specifying the number of tweets to fetch, up to a maximum of 100. Streamline timeline access for analysis or integration.
Instructions
Get the user's recent tweets
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| count | No | Number of tweets to retrieve (max 100) |
Implementation Reference
- src/index.ts:190-230 (handler)The core handler function that implements the get_user_timeline tool logic: fetches the user's own timeline using Twitter API v2.userTimeline, handles errors by falling back to user info, and formats tweets as a text list.private async getUserTimeline(args: any) { if (!this.twitterClient) { throw new Error("Twitter client not initialized"); } const count = Math.min(args.count || 10, 100); try { const me = await this.twitterClient.v2.me(); const tweets = await this.twitterClient.v2.userTimeline(me.data.id, { max_results: count, "tweet.fields": ["created_at", "public_metrics"], expansions: ["author_id"], }); const tweetList = tweets.data.data || []; const formattedTweets = tweetList .map((tweet) => `- ${tweet.text} (${tweet.id})`) .join("\n"); return { content: [ { type: "text", text: `Your recent tweets:\n${formattedTweets}`, }, ], }; } catch (error) { // If timeline fails, just return user info const me = await this.twitterClient.v2.me(); return { content: [ { type: "text", text: `User: @${me.data.username} (${me.data.name})\nTimeline access may be restricted.`, }, ], }; } }
- src/index.ts:50-64 (schema)The tool schema definition in the getTools() method, including name, description, and inputSchema with optional 'count' parameter.{ name: "get_user_timeline", description: "Get the user's recent tweets", inputSchema: { type: "object", properties: { count: { type: "number", description: "Number of tweets to retrieve (max 100)", default: 10, }, }, }, }, {
- src/index.ts:111-112 (registration)Registration in the CallToolRequestSchema handler: switch case that routes calls to the getUserTimeline method.case "get_user_timeline": return await this.getUserTimeline(args);