getLikedTweets
Retrieve a user's liked tweets using their user ID. Specify the number of results and include additional tweet fields for detailed insights on Twitter MCP Server.
Instructions
Get a list of tweets liked by a user
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| maxResults | No | The maximum number of results to return (default: 100, max: 100) | |
| tweetFields | No | Additional tweet fields to include in the response | |
| userId | Yes | The ID of the user whose likes to fetch |
Implementation Reference
- Main handler function executing the getLikedTweets tool logic using Twitter API v2's userLikedTweets endpoint, handling pagination, errors, and response formatting.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 registration with input schema definition for validating getLikedTweets arguments.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/types.ts:49-53 (schema)TypeScript interface defining the input arguments for getLikedTweets handler.export interface GetLikedTweetsArgs { userId: string; maxResults?: number; tweetFields?: string[]; }
- src/types.ts:266-289 (schema)Runtime assertion function for validating GetLikedTweetsArgs input.export function assertGetLikedTweetsArgs(args: unknown): asserts args is GetLikedTweetsArgs { if (typeof args !== 'object' || args === null) { throw new Error('Invalid arguments: expected object'); } if (!('userId' in args) || typeof (args as any).userId !== 'string') { throw new Error('Invalid arguments: expected userId string'); } if ('maxResults' in args) { const maxResults = (args as any).maxResults; if (typeof maxResults !== 'number' || maxResults < 1 || maxResults > 100) { throw new Error('Invalid arguments: maxResults must be a number between 1 and 100'); } } if ('tweetFields' in args) { if (!Array.isArray((args as any).tweetFields)) { throw new Error('Invalid arguments: expected tweetFields to be an array'); } for (const field of (args as any).tweetFields) { if (typeof field !== 'string') { throw new Error('Invalid arguments: expected tweetFields to be an array of strings'); } } } }