import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { MealieClient } from "../client.js";
import { formatErrorResponse } from "../utils.js";
export function registerFoodsTools(
server: McpServer,
client: MealieClient
): void {
server.tool(
"get_foods",
"List and search food items in the database. Foods are used as structured ingredients in recipes.",
{
search: z
.string()
.optional()
.describe("Search term to filter foods by name"),
page: z.number().optional().describe("Page number for pagination"),
per_page: z.number().optional().describe("Number of items per page"),
},
async ({ search, page, per_page }) => {
try {
const result = await client.getFoods({
search,
page,
perPage: per_page,
});
return {
content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }],
};
} catch (error) {
const message =
error instanceof Error ? error.message : "Unknown error";
return {
content: [
{
type: "text" as const,
text: JSON.stringify(formatErrorResponse(`Error fetching foods: ${message}`)),
},
],
};
}
}
);
server.tool(
"create_food",
"Create a new food item in the database. Foods can be used as structured ingredients in recipes.",
{
name: z.string().describe("The name of the food item (e.g., 'chicken breast', 'flour')"),
plural_name: z
.string()
.optional()
.describe("The plural form of the food name (e.g., 'chicken breasts')"),
description: z
.string()
.optional()
.describe("Optional description of the food item"),
},
async ({ name, plural_name, description }) => {
try {
const result = await client.createFood({
name,
pluralName: plural_name,
description,
});
return {
content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }],
};
} catch (error) {
const message =
error instanceof Error ? error.message : "Unknown error";
return {
content: [
{
type: "text" as const,
text: JSON.stringify(formatErrorResponse(`Error creating food '${name}': ${message}`)),
},
],
};
}
}
);
server.tool(
"get_food",
"Get a specific food item by its ID.",
{
id: z.string().describe("The unique ID of the food item"),
},
async ({ id }) => {
try {
const result = await client.getFood(id);
return {
content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }],
};
} catch (error) {
const message =
error instanceof Error ? error.message : "Unknown error";
return {
content: [
{
type: "text" as const,
text: JSON.stringify(formatErrorResponse(`Error fetching food '${id}': ${message}`)),
},
],
};
}
}
);
server.tool(
"delete_food",
"Delete a food item from the database by its ID.",
{
id: z.string().describe("The unique ID of the food item to delete"),
},
async ({ id }) => {
try {
const result = await client.deleteFood(id);
return {
content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }],
};
} catch (error) {
const message =
error instanceof Error ? error.message : "Unknown error";
return {
content: [
{
type: "text" as const,
text: JSON.stringify(formatErrorResponse(`Error deleting food '${id}': ${message}`)),
},
],
};
}
}
);
}