getLikedTweets
Retrieve tweets liked by a specific Twitter user to analyze preferences, monitor engagement, or curate content based on user interests.
Instructions
Get a list of tweets liked by a user
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| userId | Yes | The ID of the user whose likes to fetch | |
| maxResults | No | The maximum number of results to return (default: 100, max: 100) | |
| tweetFields | No | Additional tweet fields to include in the response |
Implementation Reference
- The core handler function that implements the getLikedTweets tool logic, fetching liked tweets via Twitter API v2.userLikedTweets with pagination and error handling.export const handleGetLikedTweets: TwitterHandler<GetLikedTweetsArgs> = async ( client: TwitterClient | null, { userId, maxResults = 100, tweetFields }: GetLikedTweetsArgs ): Promise<HandlerResponse> => { if (!client) { return createMissingTwitterApiKeyResponse('getLikedTweets'); } try { const likedTweets = await client.v2.userLikedTweets(userId, { max_results: maxResults, 'tweet.fields': tweetFields?.join(',') || 'created_at,public_metrics,author_id' }); // The paginator returns data nested: { data: [tweets], meta: {...} } const tweetData = likedTweets.data?.data; const metaData = likedTweets.data?.meta || likedTweets.meta; if (!tweetData || !Array.isArray(tweetData) || tweetData.length === 0) { return createResponse(`No liked tweets found for user: ${userId}`); } const responseData = { likedTweets: tweetData, meta: metaData }; return createResponse(`Liked tweets: ${JSON.stringify(responseData, null, 2)}`); } catch (error) { if (error instanceof Error) { if (error.message.includes('400') && error.message.includes('Invalid Request')) { throw new Error(`Get liked tweets functionality may require elevated permissions or Pro tier access. Current Basic tier ($200/month) has limited access to user engagement data. Consider upgrading to Pro tier ($5,000/month) at https://developer.x.com/en/portal/products/pro or use alternative methods to track user engagement.`); } throw new Error(formatTwitterError(error, 'getting liked tweets')); } throw error; } };
- src/tools.ts:55-78 (schema)MCP tool schema definition including description, input schema with properties for userId, maxResults, and tweetFields.getLikedTweets: { description: 'Get a list of tweets liked by a user', inputSchema: { type: 'object', properties: { userId: { type: 'string', description: 'The ID of the user whose likes to fetch' }, maxResults: { type: 'number', description: 'The maximum number of results to return (default: 100, max: 100)', minimum: 1, maximum: 100 }, tweetFields: { type: 'array', items: { type: 'string', enum: ['created_at', 'author_id', 'conversation_id', 'public_metrics', 'entities', 'context_annotations'] }, description: 'Additional tweet fields to include in the response' }, }, required: ['userId'], }, },
- src/index.ts:207-210 (registration)Registration and dispatch logic in the MCP CallToolRequestSchema handler switch statement that routes getLikedTweets calls to the handleGetLikedTweets function.case 'getLikedTweets': { const { userId, maxResults } = request.params.arguments as { userId: string; maxResults?: number }; response = await handleGetLikedTweets(client, { userId, maxResults }); break;
- TypeScript interface defining the input arguments for the getLikedTweets handler.interface GetLikedTweetsArgs { userId: string; maxResults?: number; tweetFields?: string[]; }
- src/index.ts:30-30 (registration)Import statement registering the handler function for use in the tool dispatch.handleGetLikedTweets