UNPKG

@hauptsache.net/clickup-mcp

Version:

Transform your AI assistant into a powerful ClickUp integration for both agentic coding and productivity management. Enables seamless task context sharing, intelligent search, time tracking, and complete project management workflows.

404 lines (403 loc) 15.2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.downloadImages = void 0; exports.isTaskId = isTaskId; exports.getCurrentUser = getCurrentUser; exports.getSpaceDetails = getSpaceDetails; exports.getTaskSearchIndex = getTaskSearchIndex; exports.generateTaskUrl = generateTaskUrl; exports.generateListUrl = generateListUrl; exports.generateSpaceUrl = generateSpaceUrl; exports.generateFolderUrl = generateFolderUrl; exports.formatTaskLink = formatTaskLink; exports.formatListLink = formatListLink; exports.formatSpaceLink = formatSpaceLink; exports.extractTaskIdFromUrl = extractTaskIdFromUrl; exports.isClickUpUrl = isClickUpUrl; exports.formatLinksSection = formatLinksSection; exports.getSpaceSearchIndex = getSpaceSearchIndex; exports.getSpaceContent = getSpaceContent; exports.getAllTeamMembers = getAllTeamMembers; const config_1 = require("./config"); const fuse_js_1 = __importDefault(require("fuse.js")); const GLOBAL_REFRESH_INTERVAL = 60000; // 60 seconds - that is the rate limit time frame /** * Checks if a string looks like a valid ClickUp task ID * Valid task IDs are 6-9 characters long and contain only alphanumeric characters */ function isTaskId(str) { // Task IDs are 6-9 characters long and contain only alphanumeric characters return /^[a-z0-9]{6,9}$/i.test(str); } // Cache for current user info to avoid repeated API calls and race conditions let cachedUserPromise = null; /** * Get current authenticated user information from ClickUp API * Caches the promise to prevent race conditions on concurrent calls */ async function getCurrentUser() { // Return cached promise if available if (cachedUserPromise) { return cachedUserPromise; } // Create the fetch promise const fetchPromise = (async () => { const userResponse = await fetch("https://api.clickup.com/api/v2/user", { headers: { Authorization: config_1.CONFIG.apiKey }, }); if (!userResponse.ok) { throw new Error(`Error fetching user info: ${userResponse.status} ${userResponse.statusText}`); } return await userResponse.json(); })(); // Cache the promise cachedUserPromise = fetchPromise; // Auto-cleanup after 60 seconds setTimeout(() => { cachedUserPromise = null; console.error(`Auto-cleaned user data cache`); }, GLOBAL_REFRESH_INTERVAL); return fetchPromise; } // Re-export image processing functions for backward compatibility var image_processing_1 = require("./image-processing"); Object.defineProperty(exports, "downloadImages", { enumerable: true, get: function () { return image_processing_1.downloadImages; } }); const spaceCache = new Map(); // Global cache for space details promises /** * Function to get space details, using a cache to avoid redundant fetches */ function getSpaceDetails(spaceId) { if (!spaceId) { return Promise.reject(new Error('Invalid space ID')); } const cachedSpace = spaceCache.get(spaceId); if (cachedSpace) { return cachedSpace; } const fetchPromise = fetch(`https://api.clickup.com/api/v2/space/${spaceId}`, { headers: { Authorization: config_1.CONFIG.apiKey } }) .then(res => { if (!res.ok) { throw new Error(`Error fetching space ${spaceId}: ${res.status}`); } return res.json(); }) .catch(error => { console.error(`Network error fetching space ${spaceId}:`, error); throw new Error(`Error fetching space ${spaceId}: ${error}`); }); spaceCache.set(spaceId, fetchPromise); return fetchPromise; } // Task search index management - cache promises to prevent race conditions const taskIndices = new Map(); /** * Get or create a task search index with specified filters * Caches promises to prevent race conditions on concurrent calls */ async function getTaskSearchIndex(space_ids, list_ids, assignees) { // Create cache key from sorted filter arrays const key = JSON.stringify({ space_ids: space_ids?.sort(), list_ids: list_ids?.sort(), assignees: assignees?.sort() }); // Check for existing valid index promise const cachedPromise = taskIndices.get(key); if (cachedPromise) { return cachedPromise; } // Create the fetch promise const fetchPromise = (async () => { console.error(`Refreshing task index for filters: ${key}`); const tasks = await fetchTasks(space_ids, list_ids, assignees); const index = createFuseIndex(tasks); console.error(`Task index created with ${tasks.length} tasks`); return index; })(); // Store promise with auto-cleanup taskIndices.set(key, fetchPromise); setTimeout(() => { taskIndices.delete(key); console.error(`Auto-cleaned index for filters: ${key}`); }, GLOBAL_REFRESH_INTERVAL); return fetchPromise; } /** * Fetch tasks using team endpoint with dynamic filters */ async function fetchTasks(space_ids, list_ids, assignees) { const queryParams = ['order_by=updated', 'subtasks=true']; // Add filter parameters if (space_ids?.length) { space_ids.forEach(id => queryParams.push(`space_ids[]=${id}`)); } if (list_ids?.length) { list_ids.forEach(id => queryParams.push(`list_ids[]=${id}`)); } if (assignees?.length) { assignees.forEach(id => queryParams.push(`assignees[]=${id}`)); } const queryString = queryParams.join('&'); // Fetch multiple pages in parallel const maxPages = space_ids?.length || list_ids?.length || assignees?.length ? 10 : 30; // Fewer pages for filtered searches const taskListsPromises = [...Array(maxPages)].map(async (_, i) => { const url = `https://api.clickup.com/api/v2/team/${config_1.CONFIG.teamId}/task?${queryString}&page=${i}`; try { const res = await fetch(url, { headers: { Authorization: config_1.CONFIG.apiKey } }); return await res.json(); } catch (e) { console.error(`Error fetching page ${i}:`, e); return { tasks: [] }; } }); const taskLists = await Promise.all(taskListsPromises); return taskLists.flatMap(taskList => taskList.tasks || []); } /** * Create a Fuse index from tasks array */ function createFuseIndex(tasks) { return new fuse_js_1.default(tasks, { keys: [ { name: 'name', weight: 0.7 }, { name: 'id', weight: 0.6 }, { name: 'text_content', weight: 0.5 }, { name: 'tags.name', weight: 0.4 }, { name: 'assignees.username', weight: 0.4 }, { name: 'list.name', weight: 0.3 }, { name: 'folder.name', weight: 0.2 }, { name: 'space.name', weight: 0.1 } ], includeScore: true, threshold: 0.4, minMatchCharLength: 2, }); } // ===== LINK UTILITIES ===== /** * Generate a ClickUp task URL from a task ID */ function generateTaskUrl(taskId) { return `https://app.clickup.com/t/${taskId}`; } /** * Generate a ClickUp list URL from a list ID */ function generateListUrl(listId) { return `https://app.clickup.com/v/l/${listId}`; } /** * Generate a ClickUp space URL from a space ID */ function generateSpaceUrl(spaceId) { return `https://app.clickup.com/v/s/${spaceId}`; } /** * Generate a ClickUp folder URL from a folder ID */ function generateFolderUrl(folderId) { return `https://app.clickup.com/v/f/${folderId}`; } /** * Format a ClickUp task link as markdown */ function formatTaskLink(taskId, taskName) { const url = generateTaskUrl(taskId); const displayText = taskName ? `${taskName} (${taskId})` : taskId; return `[${displayText}](${url})`; } /** * Format a ClickUp list link as markdown */ function formatListLink(listId, listName) { const url = generateListUrl(listId); const displayText = listName ? `${listName} (${listId})` : listId; return `[${displayText}](${url})`; } /** * Format a ClickUp space link as markdown */ function formatSpaceLink(spaceId, spaceName) { const url = generateSpaceUrl(spaceId); const displayText = spaceName ? `${spaceName} (${spaceId})` : spaceId; return `[${displayText}](${url})`; } /** * Extract task ID from a ClickUp URL */ function extractTaskIdFromUrl(url) { const match = url.match(/https?:\/\/app\.clickup\.com\/t\/([a-z0-9]{6,9})/i); return match ? match[1] : null; } /** * Validate if a string is a valid ClickUp URL */ function isClickUpUrl(url) { return /^https?:\/\/app\.clickup\.com\//.test(url); } /** * Format a prominent link section for responses */ function formatLinksSection(links) { if (links.length === 0) return ''; const linkLines = links.map(link => `🔗 [${link.text}](${link.url})`); return `\n\n**📌 Quick Links:**\n${linkLines.join('\n')}`; } // Space search index cache - cache promise to prevent race conditions let spaceSearchIndexPromise = null; /** * Get or refresh the space search index * Caches promise to prevent race conditions on concurrent calls */ async function getSpaceSearchIndex() { // Return cached promise if available if (spaceSearchIndexPromise) { return spaceSearchIndexPromise; } // Create the fetch promise const fetchPromise = (async () => { try { const url = `https://api.clickup.com/api/v2/team/${config_1.CONFIG.teamId}/space`; const response = await fetch(url, { headers: { Authorization: config_1.CONFIG.apiKey }, }); if (!response.ok) { throw new Error(`Error fetching spaces: ${response.status} ${response.statusText}`); } const data = await response.json(); const spacesData = data.spaces || []; // Create Fuse search index return new fuse_js_1.default(spacesData, { keys: [ { name: 'name', weight: 0.7 }, { name: 'id', weight: 0.6 } ], includeScore: true, threshold: 0.4, minMatchCharLength: 1, }); } catch (error) { console.error('Error creating space search index:', error); return null; } })(); // Cache the promise spaceSearchIndexPromise = fetchPromise; // Auto-cleanup after 60 seconds setTimeout(() => { spaceSearchIndexPromise = null; console.error('Auto-cleaned space search index'); }, GLOBAL_REFRESH_INTERVAL); return fetchPromise; } const listCache = new Map(); // Cache for space lists/folders /** * Get lists and folders for a specific space with caching */ async function getSpaceContent(spaceId) { const cacheKey = `space-content-${spaceId}`; // Check cache first const cachedContent = listCache.get(cacheKey); if (cachedContent) { return cachedContent; } // Fetch content with parallel requests const fetchPromise = (async () => { try { const [foldersResponse, listsResponse] = await Promise.all([ fetch(`https://api.clickup.com/api/v2/space/${spaceId}/folder`, { headers: { Authorization: config_1.CONFIG.apiKey }, }), fetch(`https://api.clickup.com/api/v2/space/${spaceId}/list`, { headers: { Authorization: config_1.CONFIG.apiKey }, }) ]); const folders = foldersResponse.ok ? (await foldersResponse.json()).folders || [] : []; const lists = listsResponse.ok ? (await listsResponse.json()).lists || [] : []; // For each folder, also fetch its lists const folderListPromises = folders.map(async (folder) => { try { const folderListResponse = await fetch(`https://api.clickup.com/api/v2/folder/${folder.id}/list`, { headers: { Authorization: config_1.CONFIG.apiKey } }); if (folderListResponse.ok) { const folderListData = await folderListResponse.json(); folder.lists = folderListData.lists || []; } return folder; } catch (error) { console.error(`Error fetching lists for folder ${folder.id}:`, error); folder.lists = []; return folder; } }); const foldersWithLists = await Promise.all(folderListPromises); return { lists, folders: foldersWithLists }; } catch (error) { console.error(`Error fetching space content for ${spaceId}:`, error); return { lists: [], folders: [] }; } })(); // Cache the promise listCache.set(cacheKey, fetchPromise); // Auto-cleanup after 60 seconds setTimeout(() => { listCache.delete(cacheKey); console.error(`Auto-cleaned space content cache for ${spaceId}`); }, GLOBAL_REFRESH_INTERVAL); return fetchPromise; } // Cache for team members to avoid repeated API calls and race conditions let cachedTeamMembersPromise = null; /** * Gets all team members from ClickUp API with caching */ async function getAllTeamMembers() { // Return cached promise if available if (cachedTeamMembersPromise) { return cachedTeamMembersPromise; } // Create the fetch promise const fetchPromise = (async () => { try { const response = await fetch(`https://api.clickup.com/api/v2/team`, { headers: { Authorization: config_1.CONFIG.apiKey }, }); if (!response.ok) { console.error(`Error fetching teams: ${response.status} ${response.statusText}`); return []; } const data = await response.json(); if (!data.teams || !Array.isArray(data.teams)) { return []; } // Find the team that matches our configured team ID and extract all user IDs const currentTeam = data.teams.find((team) => team.id === config_1.CONFIG.teamId); if (!currentTeam || !currentTeam.members || !Array.isArray(currentTeam.members)) { console.error(`Team ${config_1.CONFIG.teamId} not found or has no members`); return []; } return currentTeam.members.map((member) => member.user?.id).filter(Boolean); } catch (error) { console.error('Error fetching team members:', error); return []; } })(); // Cache the promise cachedTeamMembersPromise = fetchPromise; // Auto-cleanup after 60 seconds setTimeout(() => { cachedTeamMembersPromise = null; console.error(`Auto-cleaned team members cache`); }, GLOBAL_REFRESH_INTERVAL); return fetchPromise; }