Skip to main content
Glama
milkosten

Task API Server

by milkosten

listTasks

Retrieve and filter tasks by status or priority to manage workloads efficiently using the Task API Server.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
statusNoFilter tasks by status (optional)
priorityNoFilter tasks by priority level (optional)

Implementation Reference

  • Handler function for listTasks tool: fetches tasks from external API with optional status and priority filters, handles various response formats, formats tasks, and returns formatted list or error.
    async ({ status, priority }: { status?: string, priority?: string }) => {
      try {
        const params: any = {};
        if (status) params.status = status;
        if (priority) params.priority = priority;
    
        const tasksResponse = await makeApiRequest("POST", "/tasks/list", { status, priority });
        
        // More flexible validation for tasks data structure
        let tasks: any[] = [];
        
        // Handle various response formats that might come from the API
        if (tasksResponse) {
          if (Array.isArray(tasksResponse.tasks)) {
            // Standard format: { tasks: [...] }
            tasks = tasksResponse.tasks;
            logDebug("Found tasks array in standard format");
          } else if (Array.isArray(tasksResponse)) {
            // Direct array format: [...]
            tasks = tasksResponse;
            logDebug("Found tasks in direct array format");
          } else if (typeof tasksResponse === 'object' && tasksResponse !== null) {
            // Try to extract tasks from any available property
            const possibleTasksProperties = Object.entries(tasksResponse)
              .filter(([_, value]) => Array.isArray(value))
              .map(([key, value]) => ({ key, value }));
              
            if (possibleTasksProperties.length > 0) {
              // Use the first array property as tasks
              const tasksProp = possibleTasksProperties[0];
              tasks = tasksProp.value as any[];
              logDebug(`Found tasks array in property: ${tasksProp.key}`);
            } else {
              logError(`No tasks array found in response`, tasksResponse);
            }
          }
        }
        
        // If we still couldn't find tasks, log error and return empty array
        if (tasks.length === 0) {
          logError(`Invalid or empty tasks data structure`, tasksResponse);
        }
        
        // Format response in a way that's useful for AI to parse
        const formattedTasks = tasks.map(task => ({
          id: task.id,
          task: task.task || "No description",
          category: task.category,
          priority: task.priority || "medium",
          status: task.status || "not_started",
          createTime: task.create_time || task.created_at || task.createTime || new Date().toISOString()
        }));
        
        // Log the formatted response for debugging
        logDebug(`listTasks formatted response`, formattedTasks);
    
        return {
          content: [
            {
              type: "text",
              text: `Found ${tasks.length} tasks${status ? ` with status '${status}'` : ''}${priority ? ` and priority '${priority}'` : ''}.`
            },
            {
              type: "text",
              text: JSON.stringify(formattedTasks, null, 2)
            }
          ]
        };
      } catch (error: any) {
        return {
          content: [
            {
              type: "text",
              text: `Error listing tasks: ${error.message}`
            }
          ]
        };
      }
    }
  • Input schema for listTasks tool, defining optional status and priority parameters using Zod.
    { 
      status: z.enum(["not_started", "started", "done"]).optional()
        .describe("Filter tasks by status (optional)"),
      priority: z.enum(["low", "medium", "high"]).optional()
        .describe("Filter tasks by priority level (optional)")
    }, 
  • src/index.ts:282-369 (registration)
    Registration of the listTasks tool on the MCP server, including name, input schema, and handler function.
    server.tool(
      "listTasks", 
      { 
        status: z.enum(["not_started", "started", "done"]).optional()
          .describe("Filter tasks by status (optional)"),
        priority: z.enum(["low", "medium", "high"]).optional()
          .describe("Filter tasks by priority level (optional)")
      }, 
      async ({ status, priority }: { status?: string, priority?: string }) => {
        try {
          const params: any = {};
          if (status) params.status = status;
          if (priority) params.priority = priority;
    
          const tasksResponse = await makeApiRequest("POST", "/tasks/list", { status, priority });
          
          // More flexible validation for tasks data structure
          let tasks: any[] = [];
          
          // Handle various response formats that might come from the API
          if (tasksResponse) {
            if (Array.isArray(tasksResponse.tasks)) {
              // Standard format: { tasks: [...] }
              tasks = tasksResponse.tasks;
              logDebug("Found tasks array in standard format");
            } else if (Array.isArray(tasksResponse)) {
              // Direct array format: [...]
              tasks = tasksResponse;
              logDebug("Found tasks in direct array format");
            } else if (typeof tasksResponse === 'object' && tasksResponse !== null) {
              // Try to extract tasks from any available property
              const possibleTasksProperties = Object.entries(tasksResponse)
                .filter(([_, value]) => Array.isArray(value))
                .map(([key, value]) => ({ key, value }));
                
              if (possibleTasksProperties.length > 0) {
                // Use the first array property as tasks
                const tasksProp = possibleTasksProperties[0];
                tasks = tasksProp.value as any[];
                logDebug(`Found tasks array in property: ${tasksProp.key}`);
              } else {
                logError(`No tasks array found in response`, tasksResponse);
              }
            }
          }
          
          // If we still couldn't find tasks, log error and return empty array
          if (tasks.length === 0) {
            logError(`Invalid or empty tasks data structure`, tasksResponse);
          }
          
          // Format response in a way that's useful for AI to parse
          const formattedTasks = tasks.map(task => ({
            id: task.id,
            task: task.task || "No description",
            category: task.category,
            priority: task.priority || "medium",
            status: task.status || "not_started",
            createTime: task.create_time || task.created_at || task.createTime || new Date().toISOString()
          }));
          
          // Log the formatted response for debugging
          logDebug(`listTasks formatted response`, formattedTasks);
    
          return {
            content: [
              {
                type: "text",
                text: `Found ${tasks.length} tasks${status ? ` with status '${status}'` : ''}${priority ? ` and priority '${priority}'` : ''}.`
              },
              {
                type: "text",
                text: JSON.stringify(formattedTasks, null, 2)
              }
            ]
          };
        } catch (error: any) {
          return {
            content: [
              {
                type: "text",
                text: `Error listing tasks: ${error.message}`
              }
            ]
          };
        }
      }
    );
  • Handler function for listTasks tool (duplicate implementation in HTTP server version): fetches tasks from external API with optional filters, handles response formats, formats and returns task list.
    async ({ status, priority }: { status?: string, priority?: string }) => {
      try {
        const params: any = {};
        if (status) params.status = status;
        if (priority) params.priority = priority;
    
        const tasksResponse = await makeApiRequest("POST", "/tasks/list", { status, priority });
        
        // More flexible validation for tasks data structure
        let tasks: any[] = [];
        
        // Handle various response formats that might come from the API
        if (tasksResponse) {
          if (Array.isArray(tasksResponse.tasks)) {
            // Standard format: { tasks: [...] }
            tasks = tasksResponse.tasks;
            logDebug("Found tasks array in standard format");
          } else if (Array.isArray(tasksResponse)) {
            // Direct array format: [...]
            tasks = tasksResponse;
            logDebug("Found tasks in direct array format");
          } else if (typeof tasksResponse === 'object' && tasksResponse !== null) {
            // Try to extract tasks from any available property
            const possibleTasksProperties = Object.entries(tasksResponse)
              .filter(([_, value]) => Array.isArray(value))
              .map(([key, value]) => ({ key, value }));
              
            if (possibleTasksProperties.length > 0) {
              // Use the first array property as tasks
              const tasksProp = possibleTasksProperties[0];
              tasks = tasksProp.value as any[];
              logDebug(`Found tasks array in property: ${tasksProp.key}`);
            } else {
              logError(`No tasks array found in response`, tasksResponse);
            }
          }
        }
        
        // If we still couldn't find tasks, log error and return empty array
        if (tasks.length === 0) {
          logError(`Invalid or empty tasks data structure`, tasksResponse);
        }
        
        // Format response in a way that's useful for AI to parse
        const formattedTasks = tasks.map(task => ({
          id: task.id,
          task: task.task || "No description",
          category: task.category,
          priority: task.priority || "medium",
          status: task.status || "not_started",
          createTime: task.create_time || task.created_at || task.createTime || new Date().toISOString()
        }));
        
        // Log the formatted response for debugging
        logDebug(`listTasks formatted response`, formattedTasks);
    
        return {
          content: [
            {
              type: "text",
              text: `Found ${tasks.length} tasks${status ? ` with status '${status}'` : ''}${priority ? ` and priority '${priority}'` : ''}.`
            },
            {
              type: "text",
              text: JSON.stringify(formattedTasks, null, 2)
            }
          ]
        };
      } catch (error: any) {
        return {
          content: [
            {
              type: "text",
              text: `Error listing tasks: ${error.message}`
            }
          ]
        };
      }
    }
  • Input schema for listTasks tool (HTTP server version), defining optional status and priority filters using Zod.
    { 
      status: z.enum(["not_started", "started", "done"]).optional()
        .describe("Filter tasks by status (optional)"),
      priority: z.enum(["low", "medium", "high"]).optional()
        .describe("Filter tasks by priority level (optional)")
    }, 
Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/milkosten/task-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server