get_random_gif
Retrieve a random GIF from Giphy's library, with optional filtering by tag and content rating for appropriate use.
Instructions
Get a random GIF from Giphy, optionally filtered by tag
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| tag | No | Tag to limit random results (optional) | |
| rating | No | Content rating (g, pg, pg-13, r) |
Implementation Reference
- src/service.ts:41-72 (handler)The main handler function that implements the logic to fetch a random GIF from the Giphy API, handling parameters like tag and rating, constructing the API URL, making the request with axios, formatting the response, and error handling.export async function getRandomGif(params: { tag?: string; rating?: "g" | "pg" | "pg-13" | "r"; }) { const { tag, rating = "g" } = params; const searchParams: Record<string, string | number> = { rating, }; if (tag) { searchParams.tag = tag; } const url = buildUrl("random", searchParams); try { const response = await axios.get(url); const responseData = response.data as GiphyRandomResponse; return formatGif(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:28-45 (schema)The schema definition for the get_random_gif tool, including name, description, and input schema specifying optional tag and rating parameters.export const getRandomGifTool: Tool = { name: "get_random_gif", description: "Get a random GIF from Giphy, optionally filtered by tag", inputSchema: { type: "object", properties: { tag: { type: "string", description: "Tag to limit random results (optional)", }, rating: { type: "string", enum: ["g", "pg", "pg-13", "r"], description: "Content rating (g, pg, pg-13, r)", }, }, }, };
- src/server.ts:107-111 (registration)Registration of the getRandomGifTool in the MCP server's list of available tools via the ListToolsRequestHandler.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [searchGifsTool, getRandomGifTool, getTrendingGifsTool], }; });
- src/server.ts:52-67 (handler)The dispatch handler in the MCP server's CallToolRequestHandler that routes get_random_gif calls to the getRandomGif service function and formats the response.case "get_random_gif": { const randomParams = args as { tag?: string; rating?: "g" | "pg" | "pg-13" | "r"; }; const gif = await getRandomGif(randomParams); return { content: [ { type: "text", text: JSON.stringify({ gif }), }, ], }; }