Skip to main content
Glama

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
NameRequiredDescriptionDefault
textYesThe text content of the tweet (max 280 characters)
reply_to_tweet_idNoOptional: ID of the tweet to reply to

Implementation Reference

  • 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}`);
      }
    }
  • 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);
Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/krishna-paulraj/x-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server