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