get_trending_gifs
Retrieve trending GIFs from Giphy with options to filter by content rating and control result quantity.
Instructions
Get currently trending GIFs on Giphy
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Maximum number of objects to return (default: 10, max: 50) | |
| offset | No | Results offset (default: 0) | |
| rating | No | Content rating (g, pg, pg-13, r) |
Implementation Reference
- src/service.ts:74-104 (handler)Main handler function implementing the core logic for fetching trending GIFs from Giphy API using axios, handling parameters, building URL, error handling, and formatting the response.export async function getTrendingGifs(params: { limit?: number; offset?: number; rating?: "g" | "pg" | "pg-13" | "r"; }) { const { limit = 10, offset = 0, rating = "g" } = params; const searchParams = { limit, offset, rating, }; const url = buildUrl("trending", searchParams); try { const response = await axios.get(url); const responseData = response.data as GiphyResponse; return formatGifs(responseData.data); } catch (error) { let errorMsg = "Giphy API error"; if (axios.isAxiosError(error) && error.response) { errorMsg = `${errorMsg}: ${error.response.status} ${error.response.statusText}`; } else if (error instanceof Error) { errorMsg = `${errorMsg}: ${error.message}`; } throw new Error(errorMsg); } }
- src/tools.ts:47-66 (schema)Tool schema definition including name, description, and input schema for validating parameters like limit, offset, rating.export const getTrendingGifsTool: Tool = { name: "get_trending_gifs", description: "Get currently trending GIFs on Giphy", inputSchema: { type: "object", properties: { limit: { type: "number", description: "Maximum number of objects to return (default: 10, max: 50)", }, offset: { type: "number", description: "Results offset (default: 0)" }, rating: { type: "string", enum: ["g", "pg", "pg-13", "r"], description: "Content rating (g, pg, pg-13, r)", }, }, }, };
- src/server.ts:69-85 (handler)MCP server tool call handler that dispatches to the getTrendingGifs service function and formats the MCP response.case "get_trending_gifs": { const trendingParams = args as { limit?: number; offset?: number; rating?: "g" | "pg" | "pg-13" | "r"; }; const gifs = await getTrendingGifs(trendingParams); return { content: [ { type: "text", text: JSON.stringify({ gifs }), }, ], }; }
- src/server.ts:107-111 (registration)Registration of the tool in the MCP server's listTools handler, making it discoverable.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [searchGifsTool, getRandomGifTool, getTrendingGifsTool], }; });
- src/service.ts:130-132 (helper)Helper function to format array of GIFs for the response.function formatGifs(gifs: GiphyGif[]) { return gifs.map(formatGif); }