Skip to main content
Glama

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

Implementation Reference

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

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