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
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | The text of the tweet | |
| mediaPath | Yes | Local file path to the media to upload | |
| mediaType | Yes | MIME type of the media file | |
| altText | No | Alternative text for the media (accessibility) |
Implementation Reference
- src/handlers/tweet.handlers.ts:34-60 (handler)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'); } }
- src/tools.ts:15-34 (schema)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;
- src/types.ts:5-10 (schema)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; }