Skip to content

An AI-powered task-management system you can drop into Cursor, Lovable, Windsurf, Roo, and others.

feat: implement expand-task MCP command

Eyal Toledanod0092a6e6fe9
diff --git a/.changeset/two-bats-smoke.md b/.changeset/two-bats-smoke.md
index fde657a7..2c40b9eb 100644
--- a/.changeset/two-bats-smoke.md
+++ b/.changeset/two-bats-smoke.md
@@ -9,4 +9,5 @@
- Implement set-status MCP command for updating task status
- Implement show-task MCP command for displaying detailed task information
- Implement next-task MCP command for finding the next task to work on
+- Implement expand-task MCP command for breaking down tasks into subtasks
- Document MCP server naming conventions in architecture.mdc and mcp.mdc files (file names use kebab-case, direct functions use camelCase with Direct suffix, tool registration functions use camelCase with Tool suffix, and MCP tool names use snake_case)
diff --git a/mcp-server/src/core/direct-functions/expand-task.js b/mcp-server/src/core/direct-functions/expand-task.js
new file mode 100644
index 00000000..0642e7e2
--- /dev/null
+++ b/mcp-server/src/core/direct-functions/expand-task.js
@@ -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
+ };
+ }
+}
\ No newline at end of file
diff --git a/mcp-server/src/core/task-master-core.js b/mcp-server/src/core/task-master-core.js
index 3b509c66..5ba4a812 100644
--- a/mcp-server/src/core/task-master-core.js
+++ b/mcp-server/src/core/task-master-core.js
@@ -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
};
\ No newline at end of file
diff --git a/mcp-server/src/tools/expand-task.js b/mcp-server/src/tools/expand-task.js
new file mode 100644
index 00000000..598f9f4a
--- /dev/null
+++ b/mcp-server/src/tools/expand-task.js
@@ -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}`);
+ }
+ },
+ });
+}
\ No newline at end of file
diff --git a/mcp-server/src/tools/index.js b/mcp-server/src/tools/index.js
index b9e00ae7..de7c0cb2 100644
--- a/mcp-server/src/tools/index.js
+++ b/mcp-server/src/tools/index.js
@@ -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 {
diff --git a/tasks/task_023.txt b/tasks/task_023.txt
index ac56b9ef..54afb5d3 100644
--- a/tasks/task_023.txt
+++ b/tasks/task_023.txt
@@ -538,7 +538,7 @@ Following MCP implementation standards:
- Unit test for showTaskDirect.js
- Integration test for MCP tool
-## 23. Implement next-task MCP command [in-progress]
+## 23. Implement next-task MCP command [done]
### Dependencies: None
### Description: Create direct function wrapper and MCP tool for finding the next task to work on.
### Details:
@@ -571,7 +571,7 @@ Following MCP implementation standards:
- Unit test for nextTaskDirect.js
- Integration test for MCP tool
-## 24. Implement expand-task MCP command [pending]
+## 24. Implement expand-task MCP command [done]
### Dependencies: None
### Description: Create direct function wrapper and MCP tool for expanding a task into subtasks.
### Details:
diff --git a/tasks/tasks.json b/tasks/tasks.json
index 89dbedda..c415a1b0 100644
--- a/tasks/tasks.json
+++ b/tasks/tasks.json
@@ -1555,7 +1555,7 @@
"title": "Implement next-task MCP command",
"description": "Create direct function wrapper and MCP tool for finding the next task to work on.",
"details": "Following MCP implementation standards:\n\n1. Create nextTaskDirect.js in mcp-server/src/core/direct-functions/:\n - Import nextTask from task-manager.js\n - Handle file paths using findTasksJsonPath utility\n - Process arguments (no specific args needed except projectRoot/file)\n - Handle errors with try/catch\n - Return standardized { success, data/error } object\n\n2. Export from task-master-core.js:\n - Import the function from its file\n - Add to directFunctions map\n\n3. Create next-task.js MCP tool in mcp-server/src/tools/:\n - Import z from zod for parameter schema\n - Import executeMCPToolAction from ./utils.js\n - Import nextTaskDirect from task-master-core.js\n - Define parameters matching CLI options using zod schema\n - Implement registerNextTaskTool(server) with server.addTool\n - Use executeMCPToolAction in execute method\n\n4. Register in tools/index.js with tool name 'next_task'\n\n5. Add to .cursor/mcp.json with appropriate schema\n\n6. Write tests following testing guidelines:\n - Unit test for nextTaskDirect.js\n - Integration test for MCP tool",
- "status": "in-progress",
+ "status": "done",
"dependencies": [],
"parentTaskId": 23
},
@@ -1564,7 +1564,7 @@
"title": "Implement expand-task MCP command",
"description": "Create direct function wrapper and MCP tool for expanding a task into subtasks.",
"details": "Following MCP implementation standards:\n\n1. Create expandTaskDirect.js in mcp-server/src/core/direct-functions/:\n - Import expandTask from task-manager.js\n - Handle file paths using findTasksJsonPath utility\n - Process arguments: taskId, prompt, num, force, research\n - Validate inputs and handle errors with try/catch\n - Return standardized { success, data/error } object\n\n2. Export from task-master-core.js:\n - Import the function from its file\n - Add to directFunctions map\n\n3. Create expand-task.js MCP tool in mcp-server/src/tools/:\n - Import z from zod for parameter schema\n - Import executeMCPToolAction from ./utils.js\n - Import expandTaskDirect from task-master-core.js\n - Define parameters matching CLI options using zod schema\n - Implement registerExpandTaskTool(server) with server.addTool\n - Use executeMCPToolAction in execute method\n\n4. Register in tools/index.js with tool name 'expand_task'\n\n5. Add to .cursor/mcp.json with appropriate schema\n\n6. Write tests following testing guidelines:\n - Unit test for expandTaskDirect.js\n - Integration test for MCP tool",
- "status": "pending",
+ "status": "done",
"dependencies": [],
"parentTaskId": 23
},