getConversation
Retrieve the full history of a Twitter conversation by providing the conversation ID, with options to specify message fields, result limits, and pagination for extensive threads.
Instructions
Get full conversation history for a specific conversation
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| conversationId | Yes | The ID of the conversation to retrieve | |
| dmEventFields | No | Fields to include in the DM event objects | |
| maxResults | No | Maximum number of messages to return (default: 100, max: 100) | |
| paginationToken | No | Pagination token for retrieving next page of results |
Implementation Reference
- The handler function that implements the core logic for the 'getConversation' tool, fetching direct message conversation history from Twitter API v2, filtering by conversation ID, and returning formatted response.export const handleGetConversation: TwitterHandler<GetConversationArgs> = async ( client: TwitterClient | null, { conversationId, maxResults = 100, paginationToken, dmEventFields }: GetConversationArgs ): Promise<HandlerResponse> => { if (!client) { return createMissingTwitterApiKeyResponse('getConversation'); } try { const options: any = { max_results: Math.min(maxResults, 100) }; if (paginationToken) { options.pagination_token = paginationToken; } if (dmEventFields && dmEventFields.length > 0) { options['dm_event.fields'] = dmEventFields.join(','); } else { options['dm_event.fields'] = 'id,text,created_at,sender_id,dm_conversation_id,referenced_tweet,attachments'; } // Get conversation messages using the conversation ID endpoint // Note: This would typically use a specific conversation endpoint // For now, we'll use the general listDmEvents and filter by conversation ID const conversation = await client.v2.listDmEvents({ ...options, // Note: The actual API might have a different method for getting conversation-specific events }); if (!conversation.data || !Array.isArray(conversation.data) || conversation.data.length === 0) { return createResponse(`No messages found in conversation ${conversationId}.`); } // Filter by conversation ID if needed (depending on API implementation) const filteredMessages = conversation.data.filter((event: any) => event.dm_conversation_id === conversationId ); const responseData = { conversationId, messages: filteredMessages.length > 0 ? filteredMessages : conversation.data, meta: conversation.meta }; return createResponse(`Retrieved ${responseData.messages.length} messages from conversation ${conversationId}: ${JSON.stringify(responseData, null, 2)}`); } catch (error) { if (error instanceof Error) { if (error.message.includes('404')) { throw new Error(`Failed to get conversation: Conversation ${conversationId} not found.`); } throw new Error(formatTwitterError(error, 'getting conversation')); } throw error; } };
- src/index.ts:358-366 (registration)MCP server request handler registration: switch case that invokes handleGetConversation for tool calls named 'getConversation'.case 'getConversation': { const { conversationId, maxResults, paginationToken, dmEventFields } = request.params.arguments as { conversationId: string; maxResults?: number; paginationToken?: string; dmEventFields?: string[]; }; response = await handleGetConversation(client, { conversationId, maxResults, paginationToken, dmEventFields }); break;
- src/tools.ts:534-564 (schema)Tool registration object defining the 'getConversation' tool's description and inputSchema for MCP's ListToolsRequest.getConversation: { description: 'Get full conversation history for a specific conversation', inputSchema: { type: 'object', properties: { conversationId: { type: 'string', description: 'The ID of the conversation to retrieve' }, maxResults: { type: 'number', description: 'Maximum number of messages to return (default: 100, max: 100)', minimum: 1, maximum: 100 }, paginationToken: { type: 'string', description: 'Pagination token for retrieving next page of results' }, dmEventFields: { type: 'array', items: { type: 'string', enum: ['id', 'text', 'created_at', 'sender_id', 'dm_conversation_id', 'referenced_tweet', 'attachments'] }, description: 'Fields to include in the DM event objects' } }, required: ['conversationId'] } },
- src/types/handlers.ts:94-99 (schema)TypeScript interface defining input arguments for the getConversation handler.export interface GetConversationArgs { conversationId: string; maxResults?: number; paginationToken?: string; dmEventFields?: string[]; }