Skip to main content
Glama
crazyrabbitLTC

Twitter MCP Server

postTweetWithMedia

Post tweets with attached media files to Twitter, including images or videos with accessibility text, through the MCP server.

Instructions

Post a tweet with media attachment to Twitter

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
textYesThe text of the tweet
mediaPathYesLocal file path to the media to upload
mediaTypeYesMIME type of the media file
altTextNoAlternative text for the media (accessibility)

Implementation Reference

  • The core handler function that implements the postTweetWithMedia tool logic: checks for client, uploads media, optionally sets alt text, posts the tweet with media using Twitter API v1/v2, and returns success or formatted error.
    export async function handlePostTweetWithMedia( client: TwitterClient | null, { text, mediaPath, mediaType, altText }: MediaTweetHandlerArgs ): Promise<HandlerResponse> { if (!client) { return createMissingTwitterApiKeyResponse('Post Tweet with Media'); } try { // Upload media const mediaId = await client.v1.uploadMedia(mediaPath, { type: mediaType }); // Set alt text if provided if (altText) { await client.v1.createMediaMetadata(mediaId, { alt_text: { text: altText } }); } // Post tweet with media const tweet = await client.v2.tweet(text, { media: { media_ids: [mediaId] } }); return createResponse(`Successfully posted tweet with media: ${tweet.data.id}`); } catch (error) { if (error instanceof Error) { throw new Error(formatTwitterError(error, 'posting tweet with media')); } throw new Error('Failed to post tweet with media: Unknown error occurred'); } }
  • Defines the MCP tool 'postTweetWithMedia' including its description and input schema (JSON schema) used for validation and tool listing.
    postTweetWithMedia: { description: 'Post a tweet with media attachment to Twitter', inputSchema: { type: 'object', properties: { text: { type: 'string', description: 'The text of the tweet' }, mediaPath: { type: 'string', description: 'Local file path to the media to upload' }, mediaType: { type: 'string', enum: ['image/jpeg', 'image/png', 'image/gif', 'video/mp4'], description: 'MIME type of the media file' }, altText: { type: 'string', description: 'Alternative text for the media (accessibility)' } }, required: ['text', 'mediaPath', 'mediaType'], }, },
  • src/index.ts:157-165 (registration)
    In the MCP CallToolRequest handler switch statement, registers and dispatches 'postTweetWithMedia' tool calls to the handlePostTweetWithMedia function.
    case 'postTweetWithMedia': { const { text, mediaPath, mediaType, altText } = request.params.arguments as { text: string; mediaPath: string; mediaType: string; altText?: string; }; response = await handlePostTweetWithMedia(client, { text, mediaPath, mediaType, altText }); break;
  • TypeScript interface defining the input arguments for postTweetWithMedia, used for type safety in handlers.
    export interface PostTweetWithMediaArgs { text: string; mediaPath: string; mediaType: 'image/jpeg' | 'image/png' | 'image/gif' | 'video/mp4'; altText?: string; }

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

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