Skip to content

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

feat: implement next-task MCP command

Eyal Toledano729ae4d2d5b6
diff --git a/.changeset/two-bats-smoke.md b/.changeset/two-bats-smoke.md
index 1cc69f92..fde657a7 100644
--- a/.changeset/two-bats-smoke.md
+++ b/.changeset/two-bats-smoke.md
@@ -8,4 +8,5 @@
- Implement generate MCP command for creating individual task files from tasks.json
- 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
- 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/next-task.js b/mcp-server/src/core/direct-functions/next-task.js
new file mode 100644
index 00000000..4fb05be7
--- /dev/null
+++ b/mcp-server/src/core/direct-functions/next-task.js
@@ -0,0 +1,112 @@
+/**
+ * next-task.js
+ * Direct function implementation for finding the next task to work on
+ */
+
+import { findNextTask } from '../../../../scripts/modules/task-manager.js';
+import { readJSON } from '../../../../scripts/modules/utils.js';
+import { getCachedOrExecute } from '../../tools/utils.js';
+import { findTasksJsonPath } from '../utils/path-utils.js';
+
+/**
+ * Direct function wrapper for finding the next task to work on with error handling and caching.
+ *
+ * @param {Object} args - Command arguments
+ * @param {Object} log - Logger object
+ * @returns {Promise<Object>} - Next task result { success: boolean, data?: any, error?: { code: string, message: string }, fromCache: boolean }
+ */
+export async function nextTaskDirect(args, log) {
+ let tasksPath;
+ try {
+ // Find the tasks path first - needed for cache key and execution
+ 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
+ };
+ }
+
+ // Generate cache key using task path
+ const cacheKey = `nextTask:${tasksPath}`;
+
+ // Define the action function to be executed on cache miss
+ const coreNextTaskAction = async () => {
+ try {
+ log.info(`Finding next task from ${tasksPath}`);
+
+ // 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}`
+ }
+ };
+ }
+
+ // Find the next task
+ const nextTask = findNextTask(data.tasks);
+
+ if (!nextTask) {
+ log.info('No eligible next task found. All tasks are either completed or have unsatisfied dependencies');
+ return {
+ success: true,
+ data: {
+ message: 'No eligible next task found. All tasks are either completed or have unsatisfied dependencies',
+ nextTask: null,
+ allTasks: data.tasks
+ }
+ };
+ }
+
+ // Return the next task data with the full tasks array for reference
+ log.info(`Successfully found next task ${nextTask.id}: ${nextTask.title}`);
+ return {
+ success: true,
+ data: {
+ nextTask,
+ allTasks: data.tasks
+ }
+ };
+ } catch (error) {
+ log.error(`Error finding next task: ${error.message}`);
+ return {
+ success: false,
+ error: {
+ code: 'CORE_FUNCTION_ERROR',
+ message: error.message || 'Failed to find next task'
+ }
+ };
+ }
+ };
+
+ // Use the caching utility
+ try {
+ const result = await getCachedOrExecute({
+ cacheKey,
+ actionFn: coreNextTaskAction,
+ log
+ });
+ log.info(`nextTaskDirect completed. From cache: ${result.fromCache}`);
+ return result; // Returns { success, data/error, fromCache }
+ } catch (error) {
+ // Catch unexpected errors from getCachedOrExecute itself
+ log.error(`Unexpected error during getCachedOrExecute for nextTask: ${error.message}`);
+ return {
+ success: false,
+ error: {
+ code: 'UNEXPECTED_ERROR',
+ message: error.message
+ },
+ 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 14a24fe8..3b509c66 100644
--- a/mcp-server/src/core/task-master-core.js
+++ b/mcp-server/src/core/task-master-core.js
@@ -14,6 +14,7 @@ import { updateSubtaskByIdDirect } from './direct-functions/update-subtask-by-id
import { generateTaskFilesDirect } from './direct-functions/generate-task-files.js';
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';
// Re-export utility functions
export { findTasksJsonPath } from './utils/path-utils.js';
@@ -29,6 +30,7 @@ export {
generateTaskFilesDirect,
setTaskStatusDirect,
showTaskDirect,
+ nextTaskDirect,
};
/**
@@ -45,5 +47,6 @@ export const directFunctions = {
generate: generateTaskFilesDirect,
setStatus: setTaskStatusDirect,
showTask: showTaskDirect,
+ nextTask: nextTaskDirect,
// Add more functions as we implement them
};
\ No newline at end of file
diff --git a/mcp-server/src/tools/index.js b/mcp-server/src/tools/index.js
index 748530bd..b9e00ae7 100644
--- a/mcp-server/src/tools/index.js
+++ b/mcp-server/src/tools/index.js
@@ -12,6 +12,7 @@ import { registerUpdateTaskTool } from "./update-task.js";
import { registerUpdateSubtaskTool } from "./update-subtask.js";
import { registerGenerateTool } from "./generate.js";
import { registerShowTaskTool } from "./show-task.js";
+import { registerNextTaskTool } from "./next-task.js";
/**
* Register all Task Master tools with the MCP server
@@ -26,6 +27,7 @@ export function registerTaskMasterTools(server) {
registerUpdateSubtaskTool(server);
registerGenerateTool(server);
registerShowTaskTool(server);
+ registerNextTaskTool(server);
}
export default {
diff --git a/mcp-server/src/tools/next-task.js b/mcp-server/src/tools/next-task.js
new file mode 100644
index 00000000..6d4c40f2
--- /dev/null
+++ b/mcp-server/src/tools/next-task.js
@@ -0,0 +1,56 @@
+/**
+ * tools/next-task.js
+ * Tool to find the next task to work on
+ */
+
+import { z } from "zod";
+import {
+ handleApiResult,
+ createErrorResponse
+} from "./utils.js";
+import { nextTaskDirect } from "../core/task-master-core.js";
+
+/**
+ * Register the next-task tool with the MCP server
+ * @param {Object} server - FastMCP server instance
+ */
+export function registerNextTaskTool(server) {
+ server.addTool({
+ name: "next_task",
+ description: "Find the next task to work on based on dependencies and status",
+ parameters: z.object({
+ 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(`Finding next task with args: ${JSON.stringify(args)}`);
+
+ // Call the direct function wrapper
+ const result = await nextTaskDirect(args, log);
+
+ // Log result
+ if (result.success) {
+ if (result.data.nextTask) {
+ log.info(`Successfully found next task ID: ${result.data.nextTask.id}${result.fromCache ? ' (from cache)' : ''}`);
+ } else {
+ log.info(`No eligible next task found${result.fromCache ? ' (from cache)' : ''}`);
+ }
+ } else {
+ log.error(`Failed to find next task: ${result.error.message}`);
+ }
+
+ // Use handleApiResult to format the response
+ return handleApiResult(result, log, 'Error finding next task');
+ } catch (error) {
+ log.error(`Error in next-task tool: ${error.message}`);
+ return createErrorResponse(`Failed to find next task: ${error.message}`);
+ }
+ },
+ });
+}
\ No newline at end of file
diff --git a/tasks/task_023.txt b/tasks/task_023.txt
index 1438857e..ac56b9ef 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 [pending]
+## 23. Implement next-task MCP command [in-progress]
### Dependencies: None
### Description: Create direct function wrapper and MCP tool for finding the next task to work on.
### Details:
diff --git a/tasks/tasks.json b/tasks/tasks.json
index edc88682..89dbedda 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": "pending",
+ "status": "in-progress",
"dependencies": [],
"parentTaskId": 23
},