search_gifs
Search for GIFs on Giphy using keywords to find animated images for messages, presentations, or social media posts with content filtering options.
Instructions
Search for GIFs on Giphy with a query string
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| lang | No | Language code (default: en) | |
| limit | No | Maximum number of objects to return (default: 10, max: 50) | |
| offset | No | Results offset (default: 0) | |
| query | Yes | Search query term or phrase | |
| rating | No | Content rating (g, pg, pg-13, r) |
Implementation Reference
- src/service.ts:5-39 (handler)Core implementation of the search_gifs tool: async function that takes search parameters, constructs Giphy API URL, fetches data using axios, formats the GIFs, and handles errors.export async function searchGifs(params: { query: string; limit?: number; offset?: number; rating?: "g" | "pg" | "pg-13" | "r"; lang?: string; }) { const { query, limit = 10, offset = 0, rating = "g", lang = "en" } = params; const searchParams = { q: query, limit, offset, rating, lang, }; const url = buildUrl("search", 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:4-26 (schema)Tool definition for search_gifs including name, description, and detailed inputSchema.export const searchGifsTool: Tool = { name: "search_gifs", description: "Search for GIFs on Giphy with a query string", inputSchema: { type: "object", properties: { query: { type: "string", description: "Search query term or phrase" }, 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)", }, lang: { type: "string", description: "Language code (default: en)" }, }, required: ["query"], }, };
- src/server.ts:107-111 (registration)Registers the search_gifs tool (via searchGifsTool) in the MCP server's listTools handler.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [searchGifsTool, getRandomGifTool, getTrendingGifsTool], }; });
- src/server.ts:32-50 (handler)MCP server dispatcher for search_gifs tool call: validates arguments, invokes searchGifs, and formats MCP response.case "search_gifs": { const searchParams = args as { query: string; limit?: number; offset?: number; rating?: "g" | "pg" | "pg-13" | "r"; lang?: string; }; const gifs = await searchGifs(searchParams); return { content: [ { type: "text", text: JSON.stringify({ gifs }), }, ], }; }
- src/service.ts:130-132 (helper)Helper function to format array of GIFs into the response structure.function formatGifs(gifs: GiphyGif[]) { return gifs.map(formatGif); }