get_task_by_id
Retrieve detailed information about a specific task in OmniFocus using either its ID or name, enabling precise task management and tracking.
Instructions
Get information about a specific task by ID or name
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| taskId | No | The ID of the task to retrieve | |
| taskName | No | The name of the task to retrieve (alternative to taskId) |
Implementation Reference
- The MCP tool handler for 'get_task_by_id'. Validates input, calls the primitive getTaskById, formats the task information into a markdown-like text response, and handles errors.export async function handler(args: z.infer<typeof schema>, extra: RequestHandlerExtra) { try { // Validate that either taskId or taskName is provided if (!args.taskId && !args.taskName) { return { content: [{ type: "text" as const, text: "Error: Either taskId or taskName must be provided." }], isError: true }; } // Call the getTaskById function const result = await getTaskById(args as GetTaskByIdParams); if (result.success && result.task) { const task = result.task; // Format task information for display let infoText = `📋 **Task Information**\n`; infoText += `• **Name**: ${task.name}\n`; infoText += `• **ID**: ${task.id}\n`; if (task.note) { infoText += `• **Note**: ${task.note}\n`; } if (task.parentId && task.parentName) { infoText += `• **Parent Task**: ${task.parentName} (${task.parentId})\n`; } if (task.projectId && task.projectName) { infoText += `• **Project**: ${task.projectName} (${task.projectId})\n`; } infoText += `• **Has Children**: ${task.hasChildren ? `Yes (${task.childrenCount} subtasks)` : 'No'}\n`; return { content: [{ type: "text" as const, text: infoText }] }; } else { // Task retrieval failed return { content: [{ type: "text" as const, text: `Failed to retrieve task: ${result.error}` }], isError: true }; } } catch (err: unknown) { const error = err as Error; console.error(`Tool execution error: ${error.message}`); return { content: [{ type: "text" as const, text: `Error retrieving task: ${error.message}` }], isError: true }; } }
- Zod schema defining the input parameters for the get_task_by_id tool: optional taskId or taskName.export const schema = z.object({ taskId: z.string().optional().describe("The ID of the task to retrieve"), taskName: z.string().optional().describe("The name of the task to retrieve (alternative to taskId)") });
- src/server.ts:84-89 (registration)Registration of the 'get_task_by_id' tool in the MCP server, specifying name, description, schema, and handler.server.tool( "get_task_by_id", "Get information about a specific task by ID or name", getTaskByIdTool.schema.shape, getTaskByIdTool.handler );
- Core helper function that generates and executes AppleScript to retrieve task details from OmniFocus by ID or name, returning structured task information or error.export async function getTaskById(params: GetTaskByIdParams): Promise<{success: boolean, task?: TaskInfo, error?: string}> { try { // Validate parameters if (!params.taskId && !params.taskName) { return { success: false, error: "Either taskId or taskName must be provided" }; } // Generate AppleScript const script = generateGetTaskScript(params); console.error("Executing getTaskById AppleScript..."); // Execute AppleScript const { stdout, stderr } = await execAsync(`osascript -e '${script}'`); if (stderr) { console.error("AppleScript stderr:", stderr); } console.error("AppleScript stdout:", stdout); // Parse the result try { const result = JSON.parse(stdout); if (result.success) { return { success: true, task: result.task as TaskInfo }; } else { return { success: false, error: result.error }; } } catch (parseError) { console.error("Error parsing AppleScript result:", parseError); return { success: false, error: `Failed to parse result: ${stdout}` }; } } catch (error: any) { console.error("Error in getTaskById:", error); return { success: false, error: error?.message || "Unknown error in getTaskById" }; } }