Skip to main content
Glama
crazyrabbitLTC

Twitter MCP Server

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
NameRequiredDescriptionDefault
attachmentsNoOptional array of media IDs to attach to the message
mediaIdNoOptional media ID for attaching media to the message
recipientIdYesThe ID of the user to send the message to
textYesThe 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; } };
  • 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;
  • 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,

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