getTweetById
Retrieve specific tweets using their unique ID to access content, metadata, and user information from Twitter's platform.
Instructions
Get a tweet by its ID
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| tweetId | Yes | The ID of the tweet | |
| tweetFields | No | Fields to include in the tweet object |
Implementation Reference
- src/handlers/tweet.handlers.ts:62-81 (handler)The main handler function that implements the getTweetById tool logic. It uses the TwitterClient to fetch a single tweet by ID via the v2 API, including fields like created_at, public_metrics, and text. Handles missing client and errors appropriately.export async function handleGetTweetById( client: TwitterClient | null, { tweetId }: { tweetId: string } ): Promise<HandlerResponse> { if (!client) { return createMissingTwitterApiKeyResponse('Get Tweet by ID'); } try { const tweet = await client.v2.singleTweet(tweetId, { 'tweet.fields': 'created_at,public_metrics,text' }); return createResponse(`Tweet details: ${JSON.stringify(tweet.data, null, 2)}`); } catch (error) { if (error instanceof Error) { throw new Error(formatTwitterError(error, 'getting tweet')); } throw new Error('Failed to get tweet: Unknown error occurred'); } }
- src/index.ts:167-170 (registration)Registration and dispatch point in the MCP server request handler. Matches the tool name and calls the handleGetTweetById function with parsed arguments.case 'getTweetById': { const { tweetId } = request.params.arguments as { tweetId: string }; response = await handleGetTweetById(client, { tweetId }); break;
- src/tools.ts:142-161 (schema)Tool schema definition used for listing tools and input validation. Defines the input schema with required tweetId string and optional tweetFields array.getTweetById: { description: 'Get a tweet by its ID', inputSchema: { type: 'object', properties: { tweetId: { type: 'string', description: 'The ID of the tweet' }, tweetFields: { type: 'array', items: { type: 'string' }, description: 'Fields to include in the tweet object' } }, required: ['tweetId'] } },
- src/types.ts:28-30 (schema)TypeScript interface defining the arguments for getTweetById.export interface GetTweetByIdArgs { tweetId: string; }
- src/types.ts:200-207 (schema)Runtime assertion function to validate getTweetById arguments.export function assertGetTweetByIdArgs(args: unknown): asserts args is GetTweetByIdArgs { if (typeof args !== 'object' || args === null) { throw new Error('Invalid arguments: expected object'); } if (!('tweetId' in args) || typeof (args as any).tweetId !== 'string') { throw new Error('Invalid arguments: expected tweetId string'); } }