feat: implement expand-task MCP command

- Create direct function wrapper in expand-task.js with error handling

- Add MCP tool integration for breaking down tasks into subtasks

- Update task-master-core.js to expose expandTaskDirect function

- Update changeset to document the new command

- Parameter support for subtask generation options (num, research, prompt, force)
This commit is contained in:
Eyal Toledano
2025-03-31 12:06:23 -04:00
parent 20d04b243b
commit b2b1a1ef8f
7 changed files with 219 additions and 4 deletions

View File

@@ -0,0 +1,152 @@
/**
* expand-task.js
* Direct function implementation for expanding a task into subtasks
*/
import { expandTask } from '../../../../scripts/modules/task-manager.js';
import { readJSON, writeJSON } from '../../../../scripts/modules/utils.js';
import { findTasksJsonPath } from '../utils/path-utils.js';
import path from 'path';
import fs from 'fs';
/**
* Direct function wrapper for expanding a task into subtasks with error handling.
*
* @param {Object} args - Command arguments
* @param {Object} log - Logger object
* @returns {Promise<Object>} - Task expansion result { success: boolean, data?: any, error?: { code: string, message: string }, fromCache: boolean }
*/
export async function expandTaskDirect(args, log) {
let tasksPath;
try {
// Find the tasks path first
tasksPath = findTasksJsonPath(args, log);
} catch (error) {
log.error(`Tasks file not found: ${error.message}`);
return {
success: false,
error: {
code: 'FILE_NOT_FOUND_ERROR',
message: error.message
},
fromCache: false
};
}
// Validate task ID
const taskId = args.id ? parseInt(args.id, 10) : null;
if (!taskId) {
log.error('Task ID is required');
return {
success: false,
error: {
code: 'INPUT_VALIDATION_ERROR',
message: 'Task ID is required'
},
fromCache: false
};
}
// Process other parameters
const numSubtasks = args.num ? parseInt(args.num, 10) : undefined;
const useResearch = args.research === true;
const additionalContext = args.prompt || '';
const force = args.force === true;
try {
log.info(`Expanding task ${taskId} into ${numSubtasks || 'default'} subtasks. Research: ${useResearch}, Force: ${force}`);
// Read tasks data
const data = readJSON(tasksPath);
if (!data || !data.tasks) {
return {
success: false,
error: {
code: 'INVALID_TASKS_FILE',
message: `No valid tasks found in ${tasksPath}`
},
fromCache: false
};
}
// Find the specific task
const task = data.tasks.find(t => t.id === taskId);
if (!task) {
return {
success: false,
error: {
code: 'TASK_NOT_FOUND',
message: `Task with ID ${taskId} not found`
},
fromCache: false
};
}
// Check if task is completed
if (task.status === 'done' || task.status === 'completed') {
return {
success: false,
error: {
code: 'TASK_COMPLETED',
message: `Task ${taskId} is already marked as ${task.status} and cannot be expanded`
},
fromCache: false
};
}
// Check for existing subtasks
const hasExistingSubtasks = task.subtasks && task.subtasks.length > 0;
// Keep a copy of the task before modification
const originalTask = JSON.parse(JSON.stringify(task));
// Tracking subtasks count before expansion
const subtasksCountBefore = task.subtasks ? task.subtasks.length : 0;
// Create a backup of the tasks.json file
const backupPath = path.join(path.dirname(tasksPath), 'tasks.json.bak');
fs.copyFileSync(tasksPath, backupPath);
// Directly modify the data instead of calling the CLI function
if (!task.subtasks) {
task.subtasks = [];
}
// Save tasks.json with potentially empty subtasks array
writeJSON(tasksPath, data);
// Call the core expandTask function
await expandTask(taskId, numSubtasks, useResearch, additionalContext);
// Read the updated data
const updatedData = readJSON(tasksPath);
const updatedTask = updatedData.tasks.find(t => t.id === taskId);
// Calculate how many subtasks were added
const subtasksAdded = updatedTask.subtasks ?
updatedTask.subtasks.length - subtasksCountBefore : 0;
// Return the result
log.info(`Successfully expanded task ${taskId} with ${subtasksAdded} new subtasks`);
return {
success: true,
data: {
task: updatedTask,
subtasksAdded,
hasExistingSubtasks
},
fromCache: false
};
} catch (error) {
log.error(`Error expanding task: ${error.message}`);
return {
success: false,
error: {
code: 'CORE_FUNCTION_ERROR',
message: error.message || 'Failed to expand task'
},
fromCache: false
};
}
}

View File

@@ -15,6 +15,7 @@ import { generateTaskFilesDirect } from './direct-functions/generate-task-files.
import { setTaskStatusDirect } from './direct-functions/set-task-status.js';
import { showTaskDirect } from './direct-functions/show-task.js';
import { nextTaskDirect } from './direct-functions/next-task.js';
import { expandTaskDirect } from './direct-functions/expand-task.js';
// Re-export utility functions
export { findTasksJsonPath } from './utils/path-utils.js';
@@ -31,6 +32,7 @@ export {
setTaskStatusDirect,
showTaskDirect,
nextTaskDirect,
expandTaskDirect,
};
/**
@@ -48,5 +50,6 @@ export const directFunctions = {
setStatus: setTaskStatusDirect,
showTask: showTaskDirect,
nextTask: nextTaskDirect,
expandTask: expandTaskDirect,
// Add more functions as we implement them
};

View File

@@ -0,0 +1,57 @@
/**
* tools/expand-task.js
* Tool to expand a task into subtasks
*/
import { z } from "zod";
import {
handleApiResult,
createErrorResponse
} from "./utils.js";
import { expandTaskDirect } from "../core/task-master-core.js";
/**
* Register the expand-task tool with the MCP server
* @param {Object} server - FastMCP server instance
*/
export function registerExpandTaskTool(server) {
server.addTool({
name: "expand_task",
description: "Expand a task into subtasks for detailed implementation",
parameters: z.object({
id: z.string().describe("ID of task to expand"),
num: z.union([z.number(), z.string()]).optional().describe("Number of subtasks to generate"),
research: z.boolean().optional().describe("Use Perplexity AI for research-backed generation"),
prompt: z.string().optional().describe("Additional context for subtask generation"),
force: z.boolean().optional().describe("Force regeneration even for tasks that already have subtasks"),
file: z.string().optional().describe("Path to the tasks file"),
projectRoot: z
.string()
.optional()
.describe(
"Root directory of the project (default: current working directory)"
),
}),
execute: async (args, { log }) => {
try {
log.info(`Expanding task with args: ${JSON.stringify(args)}`);
// Call the direct function wrapper
const result = await expandTaskDirect(args, log);
// Log result
if (result.success) {
log.info(`Successfully expanded task ID: ${args.id} with ${result.data.subtasksAdded} new subtasks${result.data.hasExistingSubtasks ? ' (appended to existing subtasks)' : ''}`);
} else {
log.error(`Failed to expand task: ${result.error.message}`);
}
// Use handleApiResult to format the response
return handleApiResult(result, log, 'Error expanding task');
} catch (error) {
log.error(`Error in expand-task tool: ${error.message}`);
return createErrorResponse(`Failed to expand task: ${error.message}`);
}
},
});
}

View File

@@ -13,6 +13,7 @@ import { registerUpdateSubtaskTool } from "./update-subtask.js";
import { registerGenerateTool } from "./generate.js";
import { registerShowTaskTool } from "./show-task.js";
import { registerNextTaskTool } from "./next-task.js";
import { registerExpandTaskTool } from "./expand-task.js";
/**
* Register all Task Master tools with the MCP server
@@ -28,6 +29,7 @@ export function registerTaskMasterTools(server) {
registerGenerateTool(server);
registerShowTaskTool(server);
registerNextTaskTool(server);
registerExpandTaskTool(server);
}
export default {