post_tweet
Publish a tweet on Twitter, with an optional reply to an existing tweet.
Instructions
Post a new tweet to Twitter
Input 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)The main handler function for the 'post_tweet' tool. It validates args using PostTweetSchema, calls the Twitter client, and returns a success response with the 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/twitter-api.ts:19-40 (handler)The underlying Twitter API client method that actually posts the tweet via the twitter-api-v2 library. Handles rate limiting, optional reply-to, and returns the posted tweet's id and text.
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); } } - src/types.ts:14-19 (schema)Zod schema defining input validation for post_tweet: requires 'text' (1-280 chars) and optional 'reply_to_tweet_id' string.
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/index.ts:67-84 (registration)Registration of the 'post_tweet' tool in the ListToolsRequestSchema handler, defining its name, description, and inputSchema for MCP discovery.
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/index.ts:115-116 (registration)Dispatch routing for the 'post_tweet' tool name in the CallToolRequestSchema handler, directing to handlePostTweet.
case 'post_tweet': return await this.handlePostTweet(args);