post_tweet
Post tweets to X (Twitter) with text content and optional replies using the X MCP Server's API integration.
Instructions
Post a tweet to X (Twitter)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | The text content of the tweet (max 280 characters) | |
| reply_to_tweet_id | No | Optional: ID of the tweet to reply to |
Implementation Reference
- src/index.ts:151-188 (handler)The handler function that implements the post_tweet tool logic. It validates the input arguments, ensures the Twitter client is initialized, posts the tweet using TwitterApi.v2.tweet(), handles replies, and returns a success message with the tweet ID or throws an error.private async postTweet(args: any) { if (!this.twitterClient) { throw new Error("Twitter client not initialized"); } const { text, reply_to_tweet_id } = args; if (!text || typeof text !== "string") { throw new Error("Text is required and must be a string"); } if (text.length > 280) { throw new Error("Tweet text cannot exceed 280 characters"); } try { const tweetOptions: any = { text }; if (reply_to_tweet_id) { tweetOptions.reply = { in_reply_to_tweet_id: reply_to_tweet_id }; } const tweet = await this.twitterClient.v2.tweet(tweetOptions); return { content: [ { type: "text", text: `Tweet posted successfully! Tweet ID: ${tweet.data.id}`, }, ], }; } catch (error) { // Provide more detailed error information const errorMessage = error instanceof Error ? error.message : String(error); throw new Error(`Failed to post tweet: ${errorMessage}`); } }
- src/index.ts:32-49 (schema)The tool specification for post_tweet, including its name, description, and input schema defining required 'text' field and optional 'reply_to_tweet_id'.{ name: "post_tweet", description: "Post a tweet to X (Twitter)", inputSchema: { type: "object", properties: { text: { type: "string", description: "The text content of the tweet (max 280 characters)", }, reply_to_tweet_id: { type: "string", description: "Optional: ID of the tweet to reply to", }, }, required: ["text"], }, },
- src/index.ts:109-110 (registration)The dispatch case in the CallToolRequestSchema handler that registers and routes calls to the post_tweet tool to its handler method.case "post_tweet": return await this.postTweet(args);