get_random_gif
Retrieve a random GIF from Giphy with optional tag filtering and content rating controls for appropriate usage.
Instructions
Get a random GIF from Giphy, optionally filtered by tag
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| rating | No | Content rating (g, pg, pg-13, r) | |
| tag | No | Tag to limit random results (optional) |
Implementation Reference
- src/service.ts:41-72 (handler)The core handler function that fetches a random GIF from the Giphy API using optional tag and rating parameters. It constructs the API URL, makes an HTTP request with axios, formats the response using formatGif, and handles errors appropriately.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)Defines the Tool object for get_random_gif, including name, description, and input schema for validation of parameters (optional tag and rating).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)Registers the getRandomGifTool in the list of available tools returned by the MCP server's ListTools handler.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [searchGifsTool, getRandomGifTool, getTrendingGifsTool], }; });
- src/server.ts:52-67 (handler)Dispatch handler in the MCP CallToolRequestHandler that matches the tool name, casts arguments, calls the getRandomGif service function, and formats the response as MCP content.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 }), }, ], }; }
- src/service.ts:107-127 (helper)Helper function used by getRandomGif to format the raw Giphy GIF data into a standardized response object.function formatGif(gif: GiphyGif) { return { id: gif.id, title: gif.title, url: gif.url, images: { original: gif.images.original, downsized: gif.images.downsized, preview: gif.images.preview_gif, }, source: gif.source, import_datetime: gif.import_datetime, user: gif.user ? { username: gif.user.username, display_name: gif.user.display_name, profile_url: gif.user.profile_url, } : null, }; }