createTask
Create and manage tasks with customizable attributes like category, priority, and status using the MCP Task API Server. Ideal for integrating task management into applications or workflows.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| category | Yes | Task category (e.g., 'Development', 'Documentation') | |
| priority | No | Task priority level (defaults to 'medium' if not specified) | |
| status | No | Initial task status (defaults to 'not_started' if not specified) | |
| task | Yes | The task description or title |
Implementation Reference
- src/index.ts:384-434 (handler)The handler function for the createTask tool. It constructs the request body from input parameters and makes a POST request to the /tasks API endpoint using makeApiRequest. Returns formatted success response with task details or error message.async ({ task, category, priority, status }: { task: string; category: string; priority?: string; status?: string }) => { try { const requestBody: any = { task, category, }; if (priority) requestBody.priority = priority; if (status) requestBody.status = status; const newTask = await makeApiRequest("POST", "/tasks", requestBody); logDebug(`Created new task with ID ${newTask.id}`); return { content: [ { type: "text", text: `Task created successfully with ID: ${newTask.id}` }, { type: "text", text: JSON.stringify({ id: newTask.id, task: newTask.task || task, category: newTask.category || category, priority: newTask.priority || priority || "medium", status: newTask.status || status || "not_started", create_time: newTask.create_time || new Date().toISOString() }, null, 2) } ] }; } catch (error: any) { logError(`Error in createTask: ${error.message}`); return { content: [ { type: "text", text: `Error creating task: ${error.message}` } ] }; } }
- src/index.ts:375-383 (schema)Zod input schema for the createTask tool, defining required task and category fields, optional priority and status with validation and descriptions.task: z.string().min(1, "Task description is required") .describe("The task description or title"), category: z.string().min(1, "Category is required") .describe("Task category (e.g., 'Development', 'Documentation')"), priority: z.enum(["low", "medium", "high"]).optional() .describe("Task priority level (defaults to 'medium' if not specified)"), status: z.enum(["not_started", "started", "done"]).optional() .describe("Initial task status (defaults to 'not_started' if not specified)") },
- src/index.ts:373-435 (registration)MCP server.tool call registering the createTask tool, including input schema and handler function."createTask", { task: z.string().min(1, "Task description is required") .describe("The task description or title"), category: z.string().min(1, "Category is required") .describe("Task category (e.g., 'Development', 'Documentation')"), priority: z.enum(["low", "medium", "high"]).optional() .describe("Task priority level (defaults to 'medium' if not specified)"), status: z.enum(["not_started", "started", "done"]).optional() .describe("Initial task status (defaults to 'not_started' if not specified)") }, async ({ task, category, priority, status }: { task: string; category: string; priority?: string; status?: string }) => { try { const requestBody: any = { task, category, }; if (priority) requestBody.priority = priority; if (status) requestBody.status = status; const newTask = await makeApiRequest("POST", "/tasks", requestBody); logDebug(`Created new task with ID ${newTask.id}`); return { content: [ { type: "text", text: `Task created successfully with ID: ${newTask.id}` }, { type: "text", text: JSON.stringify({ id: newTask.id, task: newTask.task || task, category: newTask.category || category, priority: newTask.priority || priority || "medium", status: newTask.status || status || "not_started", create_time: newTask.create_time || new Date().toISOString() }, null, 2) } ] }; } catch (error: any) { logError(`Error in createTask: ${error.message}`); return { content: [ { type: "text", text: `Error creating task: ${error.message}` } ] }; } } );
- src/index.ts:68-168 (helper)Helper function makeApiRequest used by createTask handler to make authenticated POST requests to the task API.async function makeApiRequest(method: string, endpoint: string, data: any = null, params: any = null): Promise<any> { const url = `${API_BASE_URL}${endpoint}`; // Validate that API_KEY is defined if (!API_KEY) { throw new Error("TASK_MANAGER_API_KEY environment variable is not defined. Please check your .env file."); } logDebug(`API Request: ${method} ${url}`); // Standard headers const headers = { "X-API-Key": API_KEY, "Content-Type": "application/json; charset=utf-8", "Accept": "application/json, text/plain, */*", "User-Agent": "TaskMcpServer/1.0", "Connection": "close", "Cache-Control": "no-cache" }; try { // Log request details const logEntry = `Timestamp: ${new Date().toISOString()}\nMethod: ${method}\nURL: ${url}\nParams: ${JSON.stringify(params)}\nData: ${JSON.stringify(data)}\nHeaders: ${JSON.stringify(headers)}\n\n`; fs.appendFileSync("api_debug.log", logEntry); // Configure axios request options const requestConfig: any = { method, url, headers, data, params, maxRedirects: 0, timeout: 20000, decompress: false, validateStatus: function (status: number) { return status < 500; // Don't reject if status code is less than 500 } }; // Ensure proper data encoding for all requests if (data) { requestConfig.data = JSON.stringify(data); } // Add transform request for properly handling all requests requestConfig.transformRequest = [(data: any, headers: any) => { // Force proper content type headers['Content-Type'] = 'application/json; charset=utf-8'; return typeof data === 'string' ? data : JSON.stringify(data); }]; // Add specific URL handling for individual task endpoints if (endpoint.startsWith('/tasks/') && method === 'GET') { // Fix to retrieve individual task by adding specific query parameters requestConfig.params = { ...params, id: endpoint.split('/')[2] }; } const response = await axios(requestConfig); // Check for HTTP error status codes we didn't automatically reject if (response.status >= 400 && response.status < 500) { logError(`HTTP error ${response.status} from API`, response.data); // Enhanced error logging const errorLogEntry = `Timestamp: ${new Date().toISOString()}\nError: HTTP ${response.status}\nURL: ${url}\nMethod: ${method}\nResponse: ${JSON.stringify(response.data)}\n\n`; fs.appendFileSync("api_error.log", errorLogEntry); throw new Error(`API Error (${response.status}): ${JSON.stringify(response.data)}`); } // Check if response has expected format if ((method === "POST" && endpoint === "/tasks/list") || (method === "GET" && endpoint === "/tasks")) { logDebug(`listTasks response`, response.data.tasks || []); if (!response.data || !response.data.tasks || response.data.tasks.length === 0) { logDebug("API returned empty tasks array"); } } return response.data; } catch (error: any) { logError(`API Error: ${error.message}`); // Enhanced error logging with more details const errorDetails = error.response ? `Status: ${error.response.status}, Data: ${JSON.stringify(error.response.data || 'No response data')}` : (error.request ? 'No response received' : error.message); const errorLogEntry = `Timestamp: ${new Date().toISOString()}\nError: ${error.message}\nDetails: ${errorDetails}\nURL: ${url}\nMethod: ${method}\n\n`; fs.appendFileSync("api_error.log", errorLogEntry); if (error.response) { throw new Error( `API Error (${error.response.status}): ${JSON.stringify(error.response.data || 'No response data')}`, ); } else if (error.request) { throw new Error(`API Request Error: No response received (possible network issue)`); } throw error; } }