twitter_post_tweet
Publish tweets directly using a Twitter OAuth2 access token and text content. Integrate with the MCP Server - Twitter NoAuth to enable Twitter API operations without local credential setup.
Instructions
Post a new tweet
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | The tweet text content | |
| twitter_access_token | Yes | Twitter OAuth2 access token |
Implementation Reference
- Core handler function in TwitterClient that performs the HTTP POST to Twitter API v2 /tweets endpoint to create a new tweet, using the provided access token and text.def post_tweet(self, text: str) -> str: """Post a new tweet Args: text: The tweet text content """ try: if not self.access_token: return json.dumps({ "error": "No valid access token provided. Please refresh your token first.", "status": "error" }) logger.debug(f"Posting tweet with text: {text[:30]}...") # Twitter API v2 create tweet endpoint url = f"{self.api_base_url}/tweets" headers = { "Authorization": f"Bearer {self.access_token}", "Content-Type": "application/json" } # Create request body data = { "text": text } response = requests.post(url, headers=headers, json=data) response.raise_for_status() # Return the raw JSON response return json.dumps(response.json()) except requests.exceptions.RequestException as e: logger.error(f"API request error: {str(e)}") return json.dumps({"error": str(e), "status": "error"}) except Exception as e: logger.error(f"Exception in post_tweet: {str(e)}") return json.dumps({"error": str(e), "status": "error"})
- src/mcp_server_twitter_noauth/server.py:575-586 (registration)Registers the 'twitter_post_tweet' tool with the MCP server in the list_tools handler, including name, description, and input schema.types.Tool( name="twitter_post_tweet", description="Post a new tweet", inputSchema={ "type": "object", "properties": { "twitter_access_token": {"type": "string", "description": "Twitter OAuth2 access token"}, "text": {"type": "string", "description": "The tweet text content"} }, "required": ["twitter_access_token", "text"] }, ),
- Dispatch logic in the main @server.call_tool() handler that extracts arguments, validates 'text', instantiates TwitterClient, calls post_tweet, and returns the result.elif name == "twitter_post_tweet": text = arguments.get("text") if not text: raise ValueError("text is required for twitter_post_tweet") results = twitter.post_tweet(text=text) return [types.TextContent(type="text", text=results)]
- Pydantic-like input schema definition for the tool, specifying required parameters: twitter_access_token and text.inputSchema={ "type": "object", "properties": { "twitter_access_token": {"type": "string", "description": "Twitter OAuth2 access token"}, "text": {"type": "string", "description": "The tweet text content"} }, "required": ["twitter_access_token", "text"] },