post_tweet
Post new tweets to Twitter with optional replies, enabling content sharing and social media interaction through the Twitter MCP Server.
Instructions
Post a new tweet to Twitter
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | The content of your tweet | |
| reply_to_tweet_id | No | Optional: ID of the tweet to reply to |
Implementation Reference
- src/index.ts:131-147 (handler)MCP tool handler for 'post_tweet': validates input with PostTweetSchema, calls TwitterClient.postTweet, and returns success response with tweet URL.private async handlePostTweet(args: unknown) { const result = PostTweetSchema.safeParse(args); if (!result.success) { throw new McpError( ErrorCode.InvalidParams, `Invalid parameters: ${result.error.message}` ); } const tweet = await this.client.postTweet(result.data.text, result.data.reply_to_tweet_id); return { content: [{ type: 'text', text: `Tweet posted successfully!\nURL: https://twitter.com/status/${tweet.id}` }] as TextContent[] }; }
- src/index.ts:66-84 (registration)Tool registration metadata for 'post_tweet' returned in ListToolsRequest, including name, description, and input schema.{ name: 'post_tweet', description: 'Post a new tweet to Twitter', inputSchema: { type: 'object', properties: { text: { type: 'string', description: 'The content of your tweet', maxLength: 280 }, reply_to_tweet_id: { type: 'string', description: 'Optional: ID of the tweet to reply to' } }, required: ['text'] } } as Tool,
- src/types.ts:14-19 (schema)Zod schema for post_tweet input validation, used in the handler.export const PostTweetSchema = z.object({ text: z.string() .min(1, 'Tweet text cannot be empty') .max(280, 'Tweet cannot exceed 280 characters'), reply_to_tweet_id: z.string().optional() });
- src/twitter-api.ts:19-40 (helper)Core Twitter API posting logic in TwitterClient.postTweet, called by the MCP handler. Handles rate limiting and error handling.async postTweet(text: string, replyToTweetId?: string): Promise<PostedTweet> { try { const endpoint = 'tweets/create'; await this.checkRateLimit(endpoint); const tweetOptions: any = { text }; if (replyToTweetId) { tweetOptions.reply = { in_reply_to_tweet_id: replyToTweetId }; } const response = await this.client.v2.tweet(tweetOptions); console.error(`Tweet posted successfully with ID: ${response.data.id}${replyToTweetId ? ` (reply to ${replyToTweetId})` : ''}`); return { id: response.data.id, text: response.data.text }; } catch (error) { this.handleApiError(error); } }