fetchMeal.ts•1.11 kB
import { z } from "zod";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
export function registerFetchCookingRecipe(server: McpServer) {
server.tool(
"fetch-cooking-recipe",
"Fetch a cooking recipe by name in English",
{
name: z.string().describe("Name of the recipe in English"),
},
async ({ name }) => {
const ctrl = new AbortController();
const id = setTimeout(() => ctrl.abort(), 8000);
try {
const recipe = await fetch(
`https://www.themealdb.com/api/json/v1/1/search.php?s=${name}`,
{ signal: ctrl.signal }
);
const recipeData = await recipe.json();
return {
content: [
{
type: "text",
text: JSON.stringify(recipeData, null, 2),
},
],
};
} catch (err: any) {
return {
content: [
{
type: "text",
text: JSON.stringify({ error: err.message }),
},
],
};
} finally {
clearTimeout(id);
}
}
);
}