import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { MealieClient } from "../client.js";
import { formatErrorResponse } from "../utils.js";
const MealPlanEntrySchema = z.object({
date: z.string().describe("Date for the mealplan in ISO format (YYYY-MM-DD)"),
recipe_id: z
.string()
.optional()
.describe("UUID of the recipe to add to the mealplan"),
title: z
.string()
.optional()
.describe("Title for the mealplan entry if not using a recipe"),
entry_type: z
.string()
.default("breakfast")
.describe("Type of mealplan entry (breakfast, lunch, dinner, etc.)"),
});
export function registerMealplanTools(
server: McpServer,
client: MealieClient
): void {
server.tool(
"get_all_mealplans",
"Get all meal plans for the current household with pagination.",
{
start_date: z
.string()
.optional()
.describe("Start date for filtering meal plans (ISO format YYYY-MM-DD)"),
end_date: z
.string()
.optional()
.describe("End date for filtering meal plans (ISO format YYYY-MM-DD)"),
page: z.number().optional().describe("Page number to retrieve"),
per_page: z.number().optional().describe("Number of items per page"),
},
async ({ start_date, end_date, page, per_page }) => {
try {
const result = await client.getMealplans({
startDate: start_date,
endDate: end_date,
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 mealplans: ${message}`)),
},
],
};
}
}
);
server.tool(
"create_mealplan",
"Create a new meal plan entry.",
{
entry: MealPlanEntrySchema.describe(
"MealPlanEntry object containing date, recipe_id, title, and entry_type"
),
},
async ({ entry }) => {
try {
const result = await client.createMealplan({
date: entry.date,
recipeId: entry.recipe_id,
title: entry.title,
entryType: entry.entry_type,
});
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 mealplan entry: ${message}`)
),
},
],
};
}
}
);
server.tool(
"create_mealplan_bulk",
"Create multiple meal plan entries in bulk.",
{
entries: z
.array(MealPlanEntrySchema)
.describe("List of MealPlanEntry objects containing date, recipe_id, title, and entry_type"),
},
async ({ entries }) => {
try {
for (const entry of entries) {
await client.createMealplan({
date: entry.date,
recipeId: entry.recipe_id,
title: entry.title,
entryType: entry.entry_type,
});
}
return {
content: [
{
type: "text" as const,
text: JSON.stringify({ message: "Bulk mealplan entries created successfully" }),
},
],
};
} catch (error) {
const message =
error instanceof Error ? error.message : "Unknown error";
return {
content: [
{
type: "text" as const,
text: JSON.stringify(
formatErrorResponse(`Error creating bulk mealplan entries: ${message}`)
),
},
],
};
}
}
);
server.tool(
"get_todays_mealplan",
"Get the mealplan entries for today.",
{},
async () => {
try {
const result = await client.getTodaysMealplan();
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 today's mealplan: ${message}`)
),
},
],
};
}
}
);
}