Skip to main content
Glama
crazyrabbitLTC

Twitter MCP Server

postTweetWithMedia

Upload and share tweets with media attachments like images, videos, or GIFs using a local file path. Include text and alt text for accessibility, ensuring content is accessible and engaging on Twitter.

Instructions

Post a tweet with media attachment to Twitter

Input Schema

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

Implementation Reference

  • Core handler function that uploads media using Twitter v1 API, optionally sets alt text, and posts the tweet with media using v2 API.
    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 schema including description, input properties, and required fields for postTweetWithMedia.
    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)
    Registers and dispatches tool calls for 'postTweetWithMedia' to the handler function in the MCP server request handler.
    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.
    export interface PostTweetWithMediaArgs { text: string; mediaPath: string; mediaType: 'image/jpeg' | 'image/png' | 'image/gif' | 'video/mp4'; altText?: string; }
  • Runtime assertion function to validate input arguments match PostTweetWithMediaArgs interface.
    export function assertPostTweetWithMediaArgs(args: unknown): asserts args is PostTweetWithMediaArgs { if (typeof args !== 'object' || args === null) { throw new Error('Invalid arguments: expected object'); } if (!('text' in args) || typeof (args as any).text !== 'string') { throw new Error('Invalid arguments: expected text string'); } if (!('mediaPath' in args) || typeof (args as any).mediaPath !== 'string') { throw new Error('Invalid arguments: expected mediaPath string'); } if (!('mediaType' in args) || typeof (args as any).mediaType !== 'string') { throw new Error('Invalid arguments: expected mediaType string'); } const validMediaTypes = ['image/jpeg', 'image/png', 'image/gif', 'video/mp4']; if (!validMediaTypes.includes((args as any).mediaType)) { throw new Error(`Invalid arguments: mediaType must be one of: ${validMediaTypes.join(', ')}`); } if ('altText' in args && typeof (args as any).altText !== 'string') { throw new Error('Invalid arguments: expected altText to be a 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