/**
* LiteFarm MCP Server - Task Management Tools
*/
import { z } from "zod";
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import type { LiteFarmClient } from "../litefarm-client.js";
import { ResponseFormat, type LiteFarmTask } from "../types.js";
import { createToolResponse, createErrorResponse, formatListResponse, formatDate } from "../tool-utils.js";
import { TASK_STATUS, TASK_TYPES } from "../constants.js";
export function registerTaskTools(server: McpServer, client: LiteFarmClient): void {
// List tasks
server.registerTool(
"litefarm_list_tasks",
{
title: "List Farm Tasks",
description: `List tasks for a specific farm, optionally filtered by status.
Args:
- farm_id (string): Farm ID to get tasks for
- status (string): Optional filter: 'planned', 'in_progress', 'completed', 'abandoned'
- response_format ('markdown' | 'json'): Output format (default: 'markdown')
Returns: Array of task objects with task details`,
inputSchema: z.object({
farm_id: z.string().describe("Farm ID"),
status: z.enum(["planned", "in_progress", "completed", "abandoned"]).optional(),
response_format: z.nativeEnum(ResponseFormat).default(ResponseFormat.MARKDOWN)
}).strict(),
annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: true }
},
async (params) => {
try {
const tasks = await client.getTasks({ farm_id: params.farm_id, status: params.status });
if (!tasks || tasks.length === 0) {
return createToolResponse("No tasks found.");
}
const formatter = (task: LiteFarmTask) =>
`**Task #${task.task_id}** - ${task.type}\n- Status: ${task.status}\n- Due: ${formatDate(task.due_date)}`;
const response = formatListResponse(tasks, formatter, params.response_format);
return createToolResponse(response.text, response.structuredContent);
} catch (error) {
return createErrorResponse(error);
}
}
);
// Create task
server.registerTool(
"litefarm_create_task",
{
title: "Create Farm Task",
description: `Create a new task for a farm.
Args:
- farm_id (string): Farm ID
- type (string): Task type (e.g., 'field_work_task', 'cleaning_task', 'harvest_task')
- location_id (string): Location ID where the task will be performed (required)
- due_date (string): ISO date string (optional, defaults to 7 days from now)
- assignee_user_id (string): Optional user to assign
- notes (string): Optional task notes
Returns: Created task object`,
inputSchema: z.object({
farm_id: z.string().describe("Farm ID"),
type: z.string().describe("Task type"),
location_id: z.string().describe("Location ID where task will be performed"),
due_date: z.string().optional(),
assignee_user_id: z.string().optional(),
notes: z.string().optional()
}).strict(),
annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: true }
},
async (params) => {
try {
const task = await client.createTask(params);
return createToolResponse(`✅ Task created: #${task.task_id}`, task);
} catch (error) {
return createErrorResponse(error);
}
}
);
// Complete task
server.registerTool(
"litefarm_complete_task",
{
title: "Complete Task",
description: `Mark a task as completed.
Args:
- task_id (number): Task ID to complete
- happiness (number): Optional happiness rating (1-5)
Returns: Updated task object`,
inputSchema: z.object({
task_id: z.number().int().describe("Task ID"),
happiness: z.number().int().min(1).max(5).optional()
}).strict(),
annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: true }
},
async (params) => {
try {
const task = await client.completeTask(params.task_id, { happiness: params.happiness });
return createToolResponse(`✅ Task #${task.task_id} completed!`, task);
} catch (error) {
return createErrorResponse(error);
}
}
);
}