Skip to main content
Glama
rafaljanicki

X (Twitter) MCP server

by rafaljanicki

post_tweet

Publish tweets with text, media, replies, or hashtags using the X (Twitter) MCP server for streamlined social media posting.

Instructions

Post a tweet with optional media, reply, and tags

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
media_pathsNo
reply_toNo
tagsNo
textYes

Implementation Reference

  • The main handler function for the 'post_tweet' tool. It handles rate limiting, initializes Twitter clients, processes text, media uploads (using v1 API), replies, tags, and posts the tweet using the v2 client. Returns the tweet data.
    @server.tool(name="post_tweet", description="Post a tweet with optional media, reply, and tags") async def post_tweet(text: str, media_paths: Optional[List[str]] = None, reply_to: Optional[str] = None, tags: Optional[List[str]] = None) -> Dict: """Posts a tweet. Args: text (str): The text content of the tweet. Max 280 characters. media_paths (Optional[List[str]]): A list of local file paths to media (images, videos) to be uploaded and attached. reply_to (Optional[str]): The ID of the tweet to reply to. tags (Optional[List[str]]): A list of hashtags (without '#') to append to the tweet. """ if not check_rate_limit("tweet_actions"): raise Exception("Tweet action rate limit exceeded") client, v1_api = initialize_twitter_clients() tweet_data = {"text": text} if reply_to: tweet_data["in_reply_to_tweet_id"] = reply_to if tags: tweet_data["text"] += " " + " ".join(f"#{tag}" for tag in tags) if media_paths: media_ids = [] for path in media_paths: media = v1_api.media_upload(filename=path) media_ids.append(media.media_id_string) tweet_data["media_ids"] = media_ids tweet = client.create_tweet(**tweet_data) logger.info(f"Type of response from client.create_tweet: {type(tweet)}; Content: {tweet}") return tweet.data
  • The decorator that registers the 'post_tweet' function as an MCP tool with name 'post_tweet' and its description.
    @server.tool(name="post_tweet", description="Post a tweet with optional media, reply, and tags")
  • The function signature with type annotations defining the input schema (parameters: text (str), media_paths (Optional[List[str]]), reply_to (Optional[str]), tags (Optional[List[str]]) ) and output (Dict), along with detailed docstring descriptions.
    async def post_tweet(text: str, media_paths: Optional[List[str]] = None, reply_to: Optional[str] = None, tags: Optional[List[str]] = None) -> Dict: """Posts a tweet. Args: text (str): The text content of the tweet. Max 280 characters. media_paths (Optional[List[str]]): A list of local file paths to media (images, videos) to be uploaded and attached. reply_to (Optional[str]): The ID of the tweet to reply to. tags (Optional[List[str]]): A list of hashtags (without '#') to append to the tweet. """

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/rafaljanicki/x-twitter-mcp-server'

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