sendDirectMessage
Send a direct message to a specified Twitter user, including optional text and media attachments, via the Twitter MCP Server for streamlined communication.
Instructions
Send a direct message to a specified user
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| attachments | No | Optional array of media IDs to attach to the message | |
| mediaId | No | Optional media ID for attaching media to the message | |
| recipientId | Yes | The ID of the user to send the message to | |
| text | Yes | The text content of the direct message |
Implementation Reference
- The handler function that implements the core logic for sending a direct message using the Twitter v1 API. It constructs DM parameters, calls client.v1.sendDm, handles errors, and returns a formatted response.export const handleSendDirectMessage: TwitterHandler<SendDirectMessageArgs> = async ( client: TwitterClient | null, { recipientId, text, mediaId }: SendDirectMessageArgs ): Promise<HandlerResponse> => { if (!client) { return createMissingTwitterApiKeyResponse('sendDirectMessage'); } try { const dmParams: any = { recipient_id: recipientId, text }; // Add media if provided if (mediaId) { dmParams.media_id = mediaId; } const result = await client.v1.sendDm(dmParams); return createResponse(`Direct message sent successfully to user ${recipientId}. Response: ${JSON.stringify(result, null, 2)}`); } catch (error) { if (error instanceof Error) { if (error.message.includes('404')) { throw new Error(`Failed to send direct message: User ${recipientId} not found or cannot receive messages.`); } throw new Error(formatTwitterError(error, 'sending direct message')); } throw error; } };
- src/tools.ts:438-463 (schema)MCP tool schema definition including description and inputSchema for validating arguments like recipientId, text, optional mediaId and attachments.sendDirectMessage: { description: 'Send a direct message to a specified user', inputSchema: { type: 'object', properties: { recipientId: { type: 'string', description: 'The ID of the user to send the message to' }, text: { type: 'string', description: 'The text content of the direct message' }, mediaId: { type: 'string', description: 'Optional media ID for attaching media to the message' }, attachments: { type: 'array', items: { type: 'string' }, description: 'Optional array of media IDs to attach to the message' } }, required: ['recipientId', 'text'] } },
- src/index.ts:328-336 (registration)Tool registration in the MCP server request handler switch statement. Extracts arguments and calls the handleSendDirectMessage function.case 'sendDirectMessage': { const { recipientId, text, mediaId, attachments } = request.params.arguments as { recipientId: string; text: string; mediaId?: string; attachments?: string[]; }; response = await handleSendDirectMessage(client, { recipientId, text, mediaId, attachments }); break;
- src/types/handlers.ts:73-78 (schema)TypeScript interface defining the input arguments for the sendDirectMessage handler.export interface SendDirectMessageArgs { recipientId: string; text: string; mediaId?: string; attachments?: string[]; }
- src/index.ts:52-52 (registration)Import of the handleSendDirectMessage handler function from directmessage.handlers.ts.handleSendDirectMessage,