Skip to main content
Glama

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
NameRequiredDescriptionDefault
ratingNoContent rating (g, pg, pg-13, r)
tagNoTag to limit random results (optional)

Implementation Reference

  • 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); } }
  • 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], }; });
  • 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 }), }, ], }; }
  • 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, }; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/magarcia/mcp-server-giphy'

If you have feedback or need assistance with the MCP directory API, please join our Discord server