chore: add prettier config and prettify
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import TaskMasterMCPServer from "./src/index.js";
|
||||
import dotenv from "dotenv";
|
||||
import logger from "./src/logger.js";
|
||||
import TaskMasterMCPServer from './src/index.js';
|
||||
import dotenv from 'dotenv';
|
||||
import logger from './src/logger.js';
|
||||
|
||||
// Load environment variables
|
||||
dotenv.config();
|
||||
@@ -11,25 +11,25 @@ dotenv.config();
|
||||
* Start the MCP server
|
||||
*/
|
||||
async function startServer() {
|
||||
const server = new TaskMasterMCPServer();
|
||||
const server = new TaskMasterMCPServer();
|
||||
|
||||
// Handle graceful shutdown
|
||||
process.on("SIGINT", async () => {
|
||||
await server.stop();
|
||||
process.exit(0);
|
||||
});
|
||||
// Handle graceful shutdown
|
||||
process.on('SIGINT', async () => {
|
||||
await server.stop();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
process.on("SIGTERM", async () => {
|
||||
await server.stop();
|
||||
process.exit(0);
|
||||
});
|
||||
process.on('SIGTERM', async () => {
|
||||
await server.stop();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
try {
|
||||
await server.start();
|
||||
} catch (error) {
|
||||
logger.error(`Failed to start MCP server: ${error.message}`);
|
||||
process.exit(1);
|
||||
}
|
||||
try {
|
||||
await server.start();
|
||||
} catch (error) {
|
||||
logger.error(`Failed to start MCP server: ${error.message}`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Start the server
|
||||
|
||||
@@ -2,84 +2,90 @@ import { jest } from '@jest/globals';
|
||||
import { ContextManager } from '../context-manager.js';
|
||||
|
||||
describe('ContextManager', () => {
|
||||
let contextManager;
|
||||
let contextManager;
|
||||
|
||||
beforeEach(() => {
|
||||
contextManager = new ContextManager({
|
||||
maxCacheSize: 10,
|
||||
ttl: 1000, // 1 second for testing
|
||||
maxContextSize: 1000
|
||||
});
|
||||
});
|
||||
beforeEach(() => {
|
||||
contextManager = new ContextManager({
|
||||
maxCacheSize: 10,
|
||||
ttl: 1000, // 1 second for testing
|
||||
maxContextSize: 1000
|
||||
});
|
||||
});
|
||||
|
||||
describe('getContext', () => {
|
||||
it('should create a new context when not in cache', async () => {
|
||||
const context = await contextManager.getContext('test-id', { test: true });
|
||||
expect(context.id).toBe('test-id');
|
||||
expect(context.metadata.test).toBe(true);
|
||||
expect(contextManager.stats.misses).toBe(1);
|
||||
expect(contextManager.stats.hits).toBe(0);
|
||||
});
|
||||
describe('getContext', () => {
|
||||
it('should create a new context when not in cache', async () => {
|
||||
const context = await contextManager.getContext('test-id', {
|
||||
test: true
|
||||
});
|
||||
expect(context.id).toBe('test-id');
|
||||
expect(context.metadata.test).toBe(true);
|
||||
expect(contextManager.stats.misses).toBe(1);
|
||||
expect(contextManager.stats.hits).toBe(0);
|
||||
});
|
||||
|
||||
it('should return cached context when available', async () => {
|
||||
// First call creates the context
|
||||
await contextManager.getContext('test-id', { test: true });
|
||||
|
||||
// Second call should hit cache
|
||||
const context = await contextManager.getContext('test-id', { test: true });
|
||||
expect(context.id).toBe('test-id');
|
||||
expect(context.metadata.test).toBe(true);
|
||||
expect(contextManager.stats.hits).toBe(1);
|
||||
expect(contextManager.stats.misses).toBe(1);
|
||||
});
|
||||
it('should return cached context when available', async () => {
|
||||
// First call creates the context
|
||||
await contextManager.getContext('test-id', { test: true });
|
||||
|
||||
it('should respect TTL settings', async () => {
|
||||
// Create context
|
||||
await contextManager.getContext('test-id', { test: true });
|
||||
|
||||
// Wait for TTL to expire
|
||||
await new Promise(resolve => setTimeout(resolve, 1100));
|
||||
|
||||
// Should create new context
|
||||
await contextManager.getContext('test-id', { test: true });
|
||||
expect(contextManager.stats.misses).toBe(2);
|
||||
expect(contextManager.stats.hits).toBe(0);
|
||||
});
|
||||
});
|
||||
// Second call should hit cache
|
||||
const context = await contextManager.getContext('test-id', {
|
||||
test: true
|
||||
});
|
||||
expect(context.id).toBe('test-id');
|
||||
expect(context.metadata.test).toBe(true);
|
||||
expect(contextManager.stats.hits).toBe(1);
|
||||
expect(contextManager.stats.misses).toBe(1);
|
||||
});
|
||||
|
||||
describe('updateContext', () => {
|
||||
it('should update existing context metadata', async () => {
|
||||
await contextManager.getContext('test-id', { initial: true });
|
||||
const updated = await contextManager.updateContext('test-id', { updated: true });
|
||||
|
||||
expect(updated.metadata.initial).toBe(true);
|
||||
expect(updated.metadata.updated).toBe(true);
|
||||
});
|
||||
});
|
||||
it('should respect TTL settings', async () => {
|
||||
// Create context
|
||||
await contextManager.getContext('test-id', { test: true });
|
||||
|
||||
describe('invalidateContext', () => {
|
||||
it('should remove context from cache', async () => {
|
||||
await contextManager.getContext('test-id', { test: true });
|
||||
contextManager.invalidateContext('test-id', { test: true });
|
||||
|
||||
// Should be a cache miss
|
||||
await contextManager.getContext('test-id', { test: true });
|
||||
expect(contextManager.stats.invalidations).toBe(1);
|
||||
expect(contextManager.stats.misses).toBe(2);
|
||||
});
|
||||
});
|
||||
// Wait for TTL to expire
|
||||
await new Promise((resolve) => setTimeout(resolve, 1100));
|
||||
|
||||
describe('getStats', () => {
|
||||
it('should return current cache statistics', async () => {
|
||||
await contextManager.getContext('test-id', { test: true });
|
||||
const stats = contextManager.getStats();
|
||||
|
||||
expect(stats.hits).toBe(0);
|
||||
expect(stats.misses).toBe(1);
|
||||
expect(stats.invalidations).toBe(0);
|
||||
expect(stats.size).toBe(1);
|
||||
expect(stats.maxSize).toBe(10);
|
||||
expect(stats.ttl).toBe(1000);
|
||||
});
|
||||
});
|
||||
});
|
||||
// Should create new context
|
||||
await contextManager.getContext('test-id', { test: true });
|
||||
expect(contextManager.stats.misses).toBe(2);
|
||||
expect(contextManager.stats.hits).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateContext', () => {
|
||||
it('should update existing context metadata', async () => {
|
||||
await contextManager.getContext('test-id', { initial: true });
|
||||
const updated = await contextManager.updateContext('test-id', {
|
||||
updated: true
|
||||
});
|
||||
|
||||
expect(updated.metadata.initial).toBe(true);
|
||||
expect(updated.metadata.updated).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('invalidateContext', () => {
|
||||
it('should remove context from cache', async () => {
|
||||
await contextManager.getContext('test-id', { test: true });
|
||||
contextManager.invalidateContext('test-id', { test: true });
|
||||
|
||||
// Should be a cache miss
|
||||
await contextManager.getContext('test-id', { test: true });
|
||||
expect(contextManager.stats.invalidations).toBe(1);
|
||||
expect(contextManager.stats.misses).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getStats', () => {
|
||||
it('should return current cache statistics', async () => {
|
||||
await contextManager.getContext('test-id', { test: true });
|
||||
const stats = contextManager.getStats();
|
||||
|
||||
expect(stats.hits).toBe(0);
|
||||
expect(stats.misses).toBe(1);
|
||||
expect(stats.invalidations).toBe(0);
|
||||
expect(stats.size).toBe(1);
|
||||
expect(stats.maxSize).toBe(10);
|
||||
expect(stats.ttl).toBe(1000);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,156 +15,157 @@ import { LRUCache } from 'lru-cache';
|
||||
*/
|
||||
|
||||
export class ContextManager {
|
||||
/**
|
||||
* Create a new ContextManager instance
|
||||
* @param {ContextManagerConfig} config - Configuration options
|
||||
*/
|
||||
constructor(config = {}) {
|
||||
this.config = {
|
||||
maxCacheSize: config.maxCacheSize || 1000,
|
||||
ttl: config.ttl || 1000 * 60 * 5, // 5 minutes default
|
||||
maxContextSize: config.maxContextSize || 4000
|
||||
};
|
||||
/**
|
||||
* Create a new ContextManager instance
|
||||
* @param {ContextManagerConfig} config - Configuration options
|
||||
*/
|
||||
constructor(config = {}) {
|
||||
this.config = {
|
||||
maxCacheSize: config.maxCacheSize || 1000,
|
||||
ttl: config.ttl || 1000 * 60 * 5, // 5 minutes default
|
||||
maxContextSize: config.maxContextSize || 4000
|
||||
};
|
||||
|
||||
// Initialize LRU cache for context data
|
||||
this.cache = new LRUCache({
|
||||
max: this.config.maxCacheSize,
|
||||
ttl: this.config.ttl,
|
||||
updateAgeOnGet: true
|
||||
});
|
||||
// Initialize LRU cache for context data
|
||||
this.cache = new LRUCache({
|
||||
max: this.config.maxCacheSize,
|
||||
ttl: this.config.ttl,
|
||||
updateAgeOnGet: true
|
||||
});
|
||||
|
||||
// Cache statistics
|
||||
this.stats = {
|
||||
hits: 0,
|
||||
misses: 0,
|
||||
invalidations: 0
|
||||
};
|
||||
}
|
||||
// Cache statistics
|
||||
this.stats = {
|
||||
hits: 0,
|
||||
misses: 0,
|
||||
invalidations: 0
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new context or retrieve from cache
|
||||
* @param {string} contextId - Unique identifier for the context
|
||||
* @param {Object} metadata - Additional metadata for the context
|
||||
* @returns {Object} Context object with metadata
|
||||
*/
|
||||
async getContext(contextId, metadata = {}) {
|
||||
const cacheKey = this._getCacheKey(contextId, metadata);
|
||||
|
||||
// Try to get from cache first
|
||||
const cached = this.cache.get(cacheKey);
|
||||
if (cached) {
|
||||
this.stats.hits++;
|
||||
return cached;
|
||||
}
|
||||
/**
|
||||
* Create a new context or retrieve from cache
|
||||
* @param {string} contextId - Unique identifier for the context
|
||||
* @param {Object} metadata - Additional metadata for the context
|
||||
* @returns {Object} Context object with metadata
|
||||
*/
|
||||
async getContext(contextId, metadata = {}) {
|
||||
const cacheKey = this._getCacheKey(contextId, metadata);
|
||||
|
||||
this.stats.misses++;
|
||||
|
||||
// Create new context if not in cache
|
||||
const context = {
|
||||
id: contextId,
|
||||
metadata: {
|
||||
...metadata,
|
||||
created: new Date().toISOString()
|
||||
}
|
||||
};
|
||||
// Try to get from cache first
|
||||
const cached = this.cache.get(cacheKey);
|
||||
if (cached) {
|
||||
this.stats.hits++;
|
||||
return cached;
|
||||
}
|
||||
|
||||
// Cache the new context
|
||||
this.cache.set(cacheKey, context);
|
||||
|
||||
return context;
|
||||
}
|
||||
this.stats.misses++;
|
||||
|
||||
/**
|
||||
* Update an existing context
|
||||
* @param {string} contextId - Context identifier
|
||||
* @param {Object} updates - Updates to apply to the context
|
||||
* @returns {Object} Updated context
|
||||
*/
|
||||
async updateContext(contextId, updates) {
|
||||
const context = await this.getContext(contextId);
|
||||
|
||||
// Apply updates to context
|
||||
Object.assign(context.metadata, updates);
|
||||
|
||||
// Update cache
|
||||
const cacheKey = this._getCacheKey(contextId, context.metadata);
|
||||
this.cache.set(cacheKey, context);
|
||||
|
||||
return context;
|
||||
}
|
||||
// Create new context if not in cache
|
||||
const context = {
|
||||
id: contextId,
|
||||
metadata: {
|
||||
...metadata,
|
||||
created: new Date().toISOString()
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Invalidate a context in the cache
|
||||
* @param {string} contextId - Context identifier
|
||||
* @param {Object} metadata - Metadata used in the cache key
|
||||
*/
|
||||
invalidateContext(contextId, metadata = {}) {
|
||||
const cacheKey = this._getCacheKey(contextId, metadata);
|
||||
this.cache.delete(cacheKey);
|
||||
this.stats.invalidations++;
|
||||
}
|
||||
// Cache the new context
|
||||
this.cache.set(cacheKey, context);
|
||||
|
||||
/**
|
||||
* Get cached data associated with a specific key.
|
||||
* Increments cache hit stats if found.
|
||||
* @param {string} key - The cache key.
|
||||
* @returns {any | undefined} The cached data or undefined if not found/expired.
|
||||
*/
|
||||
getCachedData(key) {
|
||||
const cached = this.cache.get(key);
|
||||
if (cached !== undefined) { // Check for undefined specifically, as null/false might be valid cached values
|
||||
this.stats.hits++;
|
||||
return cached;
|
||||
}
|
||||
this.stats.misses++;
|
||||
return undefined;
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set data in the cache with a specific key.
|
||||
* @param {string} key - The cache key.
|
||||
* @param {any} data - The data to cache.
|
||||
*/
|
||||
setCachedData(key, data) {
|
||||
this.cache.set(key, data);
|
||||
}
|
||||
/**
|
||||
* Update an existing context
|
||||
* @param {string} contextId - Context identifier
|
||||
* @param {Object} updates - Updates to apply to the context
|
||||
* @returns {Object} Updated context
|
||||
*/
|
||||
async updateContext(contextId, updates) {
|
||||
const context = await this.getContext(contextId);
|
||||
|
||||
/**
|
||||
* Invalidate a specific cache key.
|
||||
* Increments invalidation stats.
|
||||
* @param {string} key - The cache key to invalidate.
|
||||
*/
|
||||
invalidateCacheKey(key) {
|
||||
this.cache.delete(key);
|
||||
this.stats.invalidations++;
|
||||
}
|
||||
// Apply updates to context
|
||||
Object.assign(context.metadata, updates);
|
||||
|
||||
/**
|
||||
* Get cache statistics
|
||||
* @returns {Object} Cache statistics
|
||||
*/
|
||||
getStats() {
|
||||
return {
|
||||
hits: this.stats.hits,
|
||||
misses: this.stats.misses,
|
||||
invalidations: this.stats.invalidations,
|
||||
size: this.cache.size,
|
||||
maxSize: this.config.maxCacheSize,
|
||||
ttl: this.config.ttl
|
||||
};
|
||||
}
|
||||
// Update cache
|
||||
const cacheKey = this._getCacheKey(contextId, context.metadata);
|
||||
this.cache.set(cacheKey, context);
|
||||
|
||||
/**
|
||||
* Generate a cache key from context ID and metadata
|
||||
* @private
|
||||
* @deprecated No longer used for direct cache key generation outside the manager.
|
||||
* Prefer generating specific keys in calling functions.
|
||||
*/
|
||||
_getCacheKey(contextId, metadata) {
|
||||
// Kept for potential backward compatibility or internal use if needed later.
|
||||
return `${contextId}:${JSON.stringify(metadata)}`;
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidate a context in the cache
|
||||
* @param {string} contextId - Context identifier
|
||||
* @param {Object} metadata - Metadata used in the cache key
|
||||
*/
|
||||
invalidateContext(contextId, metadata = {}) {
|
||||
const cacheKey = this._getCacheKey(contextId, metadata);
|
||||
this.cache.delete(cacheKey);
|
||||
this.stats.invalidations++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached data associated with a specific key.
|
||||
* Increments cache hit stats if found.
|
||||
* @param {string} key - The cache key.
|
||||
* @returns {any | undefined} The cached data or undefined if not found/expired.
|
||||
*/
|
||||
getCachedData(key) {
|
||||
const cached = this.cache.get(key);
|
||||
if (cached !== undefined) {
|
||||
// Check for undefined specifically, as null/false might be valid cached values
|
||||
this.stats.hits++;
|
||||
return cached;
|
||||
}
|
||||
this.stats.misses++;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set data in the cache with a specific key.
|
||||
* @param {string} key - The cache key.
|
||||
* @param {any} data - The data to cache.
|
||||
*/
|
||||
setCachedData(key, data) {
|
||||
this.cache.set(key, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidate a specific cache key.
|
||||
* Increments invalidation stats.
|
||||
* @param {string} key - The cache key to invalidate.
|
||||
*/
|
||||
invalidateCacheKey(key) {
|
||||
this.cache.delete(key);
|
||||
this.stats.invalidations++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache statistics
|
||||
* @returns {Object} Cache statistics
|
||||
*/
|
||||
getStats() {
|
||||
return {
|
||||
hits: this.stats.hits,
|
||||
misses: this.stats.misses,
|
||||
invalidations: this.stats.invalidations,
|
||||
size: this.cache.size,
|
||||
maxSize: this.config.maxCacheSize,
|
||||
ttl: this.config.ttl
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a cache key from context ID and metadata
|
||||
* @private
|
||||
* @deprecated No longer used for direct cache key generation outside the manager.
|
||||
* Prefer generating specific keys in calling functions.
|
||||
*/
|
||||
_getCacheKey(contextId, metadata) {
|
||||
// Kept for potential backward compatibility or internal use if needed later.
|
||||
return `${contextId}:${JSON.stringify(metadata)}`;
|
||||
}
|
||||
}
|
||||
|
||||
// Export a singleton instance with default config
|
||||
export const contextManager = new ContextManager();
|
||||
export const contextManager = new ContextManager();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* task-master-core.js
|
||||
* Direct function imports from Task Master modules
|
||||
*
|
||||
*
|
||||
* This module provides direct access to Task Master core functions
|
||||
* for improved performance and error handling compared to CLI execution.
|
||||
*/
|
||||
@@ -16,9 +16,9 @@ const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
|
||||
// Import Task Master modules
|
||||
import {
|
||||
listTasks,
|
||||
// We'll import more functions as we continue implementation
|
||||
import {
|
||||
listTasks
|
||||
// We'll import more functions as we continue implementation
|
||||
} from '../../../scripts/modules/task-manager.js';
|
||||
|
||||
// Import context manager
|
||||
@@ -33,38 +33,40 @@ import { getCachedOrExecute } from '../tools/utils.js'; // Import the utility he
|
||||
* @throws {Error} - If tasks.json cannot be found.
|
||||
*/
|
||||
function findTasksJsonPath(args, log) {
|
||||
// Assume projectRoot is already normalized absolute path if passed in args
|
||||
// Or use getProjectRoot if we decide to centralize that logic
|
||||
const projectRoot = args.projectRoot || process.cwd();
|
||||
log.info(`Searching for tasks.json within project root: ${projectRoot}`);
|
||||
// Assume projectRoot is already normalized absolute path if passed in args
|
||||
// Or use getProjectRoot if we decide to centralize that logic
|
||||
const projectRoot = args.projectRoot || process.cwd();
|
||||
log.info(`Searching for tasks.json within project root: ${projectRoot}`);
|
||||
|
||||
const possiblePaths = [];
|
||||
const possiblePaths = [];
|
||||
|
||||
// 1. If a file is explicitly provided relative to projectRoot
|
||||
if (args.file) {
|
||||
possiblePaths.push(path.resolve(projectRoot, args.file));
|
||||
}
|
||||
// 1. If a file is explicitly provided relative to projectRoot
|
||||
if (args.file) {
|
||||
possiblePaths.push(path.resolve(projectRoot, args.file));
|
||||
}
|
||||
|
||||
// 2. Check the standard locations relative to projectRoot
|
||||
possiblePaths.push(
|
||||
path.join(projectRoot, 'tasks.json'),
|
||||
path.join(projectRoot, 'tasks', 'tasks.json')
|
||||
);
|
||||
// 2. Check the standard locations relative to projectRoot
|
||||
possiblePaths.push(
|
||||
path.join(projectRoot, 'tasks.json'),
|
||||
path.join(projectRoot, 'tasks', 'tasks.json')
|
||||
);
|
||||
|
||||
log.info(`Checking potential task file paths: ${possiblePaths.join(', ')}`);
|
||||
log.info(`Checking potential task file paths: ${possiblePaths.join(', ')}`);
|
||||
|
||||
// Find the first existing path
|
||||
for (const p of possiblePaths) {
|
||||
if (fs.existsSync(p)) {
|
||||
log.info(`Found tasks file at: ${p}`);
|
||||
return p;
|
||||
}
|
||||
}
|
||||
// Find the first existing path
|
||||
for (const p of possiblePaths) {
|
||||
if (fs.existsSync(p)) {
|
||||
log.info(`Found tasks file at: ${p}`);
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
// If no file was found, throw an error
|
||||
const error = new Error(`Tasks file not found in any of the expected locations relative to ${projectRoot}: ${possiblePaths.join(', ')}`);
|
||||
error.code = 'TASKS_FILE_NOT_FOUND';
|
||||
throw error;
|
||||
// If no file was found, throw an error
|
||||
const error = new Error(
|
||||
`Tasks file not found in any of the expected locations relative to ${projectRoot}: ${possiblePaths.join(', ')}`
|
||||
);
|
||||
error.code = 'TASKS_FILE_NOT_FOUND';
|
||||
throw error;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,60 +77,94 @@ function findTasksJsonPath(args, log) {
|
||||
* @returns {Promise<Object>} - Task list result { success: boolean, data?: any, error?: { code: string, message: string }, fromCache: boolean }.
|
||||
*/
|
||||
export async function listTasksDirect(args, log) {
|
||||
let tasksPath;
|
||||
try {
|
||||
// Find the tasks path first - needed for cache key and execution
|
||||
tasksPath = findTasksJsonPath(args, log);
|
||||
} catch (error) {
|
||||
if (error.code === 'TASKS_FILE_NOT_FOUND') {
|
||||
log.error(`Tasks file not found: ${error.message}`);
|
||||
// Return the error structure expected by the calling tool/handler
|
||||
return { success: false, error: { code: error.code, message: error.message }, fromCache: false };
|
||||
}
|
||||
log.error(`Unexpected error finding tasks file: ${error.message}`);
|
||||
// Re-throw for outer catch or return structured error
|
||||
return { success: false, error: { code: 'FIND_TASKS_PATH_ERROR', message: error.message }, fromCache: false };
|
||||
}
|
||||
let tasksPath;
|
||||
try {
|
||||
// Find the tasks path first - needed for cache key and execution
|
||||
tasksPath = findTasksJsonPath(args, log);
|
||||
} catch (error) {
|
||||
if (error.code === 'TASKS_FILE_NOT_FOUND') {
|
||||
log.error(`Tasks file not found: ${error.message}`);
|
||||
// Return the error structure expected by the calling tool/handler
|
||||
return {
|
||||
success: false,
|
||||
error: { code: error.code, message: error.message },
|
||||
fromCache: false
|
||||
};
|
||||
}
|
||||
log.error(`Unexpected error finding tasks file: ${error.message}`);
|
||||
// Re-throw for outer catch or return structured error
|
||||
return {
|
||||
success: false,
|
||||
error: { code: 'FIND_TASKS_PATH_ERROR', message: error.message },
|
||||
fromCache: false
|
||||
};
|
||||
}
|
||||
|
||||
// Generate cache key *after* finding tasksPath
|
||||
const statusFilter = args.status || 'all';
|
||||
const withSubtasks = args.withSubtasks || false;
|
||||
const cacheKey = `listTasks:${tasksPath}:${statusFilter}:${withSubtasks}`;
|
||||
|
||||
// Define the action function to be executed on cache miss
|
||||
const coreListTasksAction = async () => {
|
||||
try {
|
||||
log.info(`Executing core listTasks function for path: ${tasksPath}, filter: ${statusFilter}, subtasks: ${withSubtasks}`);
|
||||
const resultData = listTasks(tasksPath, statusFilter, withSubtasks, 'json');
|
||||
// Generate cache key *after* finding tasksPath
|
||||
const statusFilter = args.status || 'all';
|
||||
const withSubtasks = args.withSubtasks || false;
|
||||
const cacheKey = `listTasks:${tasksPath}:${statusFilter}:${withSubtasks}`;
|
||||
|
||||
if (!resultData || !resultData.tasks) {
|
||||
log.error('Invalid or empty response from listTasks core function');
|
||||
return { success: false, error: { code: 'INVALID_CORE_RESPONSE', message: 'Invalid or empty response from listTasks core function' } };
|
||||
}
|
||||
log.info(`Core listTasks function retrieved ${resultData.tasks.length} tasks`);
|
||||
return { success: true, data: resultData };
|
||||
// Define the action function to be executed on cache miss
|
||||
const coreListTasksAction = async () => {
|
||||
try {
|
||||
log.info(
|
||||
`Executing core listTasks function for path: ${tasksPath}, filter: ${statusFilter}, subtasks: ${withSubtasks}`
|
||||
);
|
||||
const resultData = listTasks(
|
||||
tasksPath,
|
||||
statusFilter,
|
||||
withSubtasks,
|
||||
'json'
|
||||
);
|
||||
|
||||
} catch (error) {
|
||||
log.error(`Core listTasks function failed: ${error.message}`);
|
||||
return { success: false, error: { code: 'LIST_TASKS_CORE_ERROR', message: error.message || 'Failed to list tasks' } };
|
||||
}
|
||||
};
|
||||
if (!resultData || !resultData.tasks) {
|
||||
log.error('Invalid or empty response from listTasks core function');
|
||||
return {
|
||||
success: false,
|
||||
error: {
|
||||
code: 'INVALID_CORE_RESPONSE',
|
||||
message: 'Invalid or empty response from listTasks core function'
|
||||
}
|
||||
};
|
||||
}
|
||||
log.info(
|
||||
`Core listTasks function retrieved ${resultData.tasks.length} tasks`
|
||||
);
|
||||
return { success: true, data: resultData };
|
||||
} catch (error) {
|
||||
log.error(`Core listTasks function failed: ${error.message}`);
|
||||
return {
|
||||
success: false,
|
||||
error: {
|
||||
code: 'LIST_TASKS_CORE_ERROR',
|
||||
message: error.message || 'Failed to list tasks'
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
// Use the caching utility
|
||||
try {
|
||||
const result = await getCachedOrExecute({
|
||||
cacheKey,
|
||||
actionFn: coreListTasksAction,
|
||||
log
|
||||
});
|
||||
log.info(`listTasksDirect completed. From cache: ${result.fromCache}`);
|
||||
return result; // Returns { success, data/error, fromCache }
|
||||
} catch(error) {
|
||||
// Catch unexpected errors from getCachedOrExecute itself (though unlikely)
|
||||
log.error(`Unexpected error during getCachedOrExecute for listTasks: ${error.message}`);
|
||||
console.error(error.stack);
|
||||
return { success: false, error: { code: 'CACHE_UTIL_ERROR', message: error.message }, fromCache: false };
|
||||
}
|
||||
// Use the caching utility
|
||||
try {
|
||||
const result = await getCachedOrExecute({
|
||||
cacheKey,
|
||||
actionFn: coreListTasksAction,
|
||||
log
|
||||
});
|
||||
log.info(`listTasksDirect completed. From cache: ${result.fromCache}`);
|
||||
return result; // Returns { success, data/error, fromCache }
|
||||
} catch (error) {
|
||||
// Catch unexpected errors from getCachedOrExecute itself (though unlikely)
|
||||
log.error(
|
||||
`Unexpected error during getCachedOrExecute for listTasks: ${error.message}`
|
||||
);
|
||||
console.error(error.stack);
|
||||
return {
|
||||
success: false,
|
||||
error: { code: 'CACHE_UTIL_ERROR', message: error.message },
|
||||
fromCache: false
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,30 +174,30 @@ export async function listTasksDirect(args, log) {
|
||||
* @returns {Object} - Cache statistics
|
||||
*/
|
||||
export async function getCacheStatsDirect(args, log) {
|
||||
try {
|
||||
log.info('Retrieving cache statistics');
|
||||
const stats = contextManager.getStats();
|
||||
return {
|
||||
success: true,
|
||||
data: stats
|
||||
};
|
||||
} catch (error) {
|
||||
log.error(`Error getting cache stats: ${error.message}`);
|
||||
return {
|
||||
success: false,
|
||||
error: {
|
||||
code: 'CACHE_STATS_ERROR',
|
||||
message: error.message || 'Unknown error occurred'
|
||||
}
|
||||
};
|
||||
}
|
||||
try {
|
||||
log.info('Retrieving cache statistics');
|
||||
const stats = contextManager.getStats();
|
||||
return {
|
||||
success: true,
|
||||
data: stats
|
||||
};
|
||||
} catch (error) {
|
||||
log.error(`Error getting cache stats: ${error.message}`);
|
||||
return {
|
||||
success: false,
|
||||
error: {
|
||||
code: 'CACHE_STATS_ERROR',
|
||||
message: error.message || 'Unknown error occurred'
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps Task Master functions to their direct implementation
|
||||
*/
|
||||
export const directFunctions = {
|
||||
list: listTasksDirect,
|
||||
cacheStats: getCacheStatsDirect,
|
||||
// Add more functions as we implement them
|
||||
};
|
||||
list: listTasksDirect,
|
||||
cacheStats: getCacheStatsDirect
|
||||
// Add more functions as we implement them
|
||||
};
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { FastMCP } from "fastmcp";
|
||||
import path from "path";
|
||||
import dotenv from "dotenv";
|
||||
import { fileURLToPath } from "url";
|
||||
import fs from "fs";
|
||||
import logger from "./logger.js";
|
||||
import { registerTaskMasterTools } from "./tools/index.js";
|
||||
import { FastMCP } from 'fastmcp';
|
||||
import path from 'path';
|
||||
import dotenv from 'dotenv';
|
||||
import { fileURLToPath } from 'url';
|
||||
import fs from 'fs';
|
||||
import logger from './logger.js';
|
||||
import { registerTaskMasterTools } from './tools/index.js';
|
||||
|
||||
// Load environment variables
|
||||
dotenv.config();
|
||||
@@ -17,70 +17,70 @@ const __dirname = path.dirname(__filename);
|
||||
* Main MCP server class that integrates with Task Master
|
||||
*/
|
||||
class TaskMasterMCPServer {
|
||||
constructor() {
|
||||
// Get version from package.json using synchronous fs
|
||||
const packagePath = path.join(__dirname, "../../package.json");
|
||||
const packageJson = JSON.parse(fs.readFileSync(packagePath, "utf8"));
|
||||
constructor() {
|
||||
// Get version from package.json using synchronous fs
|
||||
const packagePath = path.join(__dirname, '../../package.json');
|
||||
const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
|
||||
|
||||
this.options = {
|
||||
name: "Task Master MCP Server",
|
||||
version: packageJson.version,
|
||||
};
|
||||
this.options = {
|
||||
name: 'Task Master MCP Server',
|
||||
version: packageJson.version
|
||||
};
|
||||
|
||||
this.server = new FastMCP(this.options);
|
||||
this.initialized = false;
|
||||
this.server = new FastMCP(this.options);
|
||||
this.initialized = false;
|
||||
|
||||
// this.server.addResource({});
|
||||
// this.server.addResource({});
|
||||
|
||||
// this.server.addResourceTemplate({});
|
||||
// this.server.addResourceTemplate({});
|
||||
|
||||
// Bind methods
|
||||
this.init = this.init.bind(this);
|
||||
this.start = this.start.bind(this);
|
||||
this.stop = this.stop.bind(this);
|
||||
// Bind methods
|
||||
this.init = this.init.bind(this);
|
||||
this.start = this.start.bind(this);
|
||||
this.stop = this.stop.bind(this);
|
||||
|
||||
// Setup logging
|
||||
this.logger = logger;
|
||||
}
|
||||
// Setup logging
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the MCP server with necessary tools and routes
|
||||
*/
|
||||
async init() {
|
||||
if (this.initialized) return;
|
||||
/**
|
||||
* Initialize the MCP server with necessary tools and routes
|
||||
*/
|
||||
async init() {
|
||||
if (this.initialized) return;
|
||||
|
||||
// Register Task Master tools
|
||||
registerTaskMasterTools(this.server);
|
||||
// Register Task Master tools
|
||||
registerTaskMasterTools(this.server);
|
||||
|
||||
this.initialized = true;
|
||||
this.initialized = true;
|
||||
|
||||
return this;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the MCP server
|
||||
*/
|
||||
async start() {
|
||||
if (!this.initialized) {
|
||||
await this.init();
|
||||
}
|
||||
/**
|
||||
* Start the MCP server
|
||||
*/
|
||||
async start() {
|
||||
if (!this.initialized) {
|
||||
await this.init();
|
||||
}
|
||||
|
||||
// Start the FastMCP server
|
||||
await this.server.start({
|
||||
transportType: "stdio",
|
||||
});
|
||||
// Start the FastMCP server
|
||||
await this.server.start({
|
||||
transportType: 'stdio'
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the MCP server
|
||||
*/
|
||||
async stop() {
|
||||
if (this.server) {
|
||||
await this.server.stop();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Stop the MCP server
|
||||
*/
|
||||
async stop() {
|
||||
if (this.server) {
|
||||
await this.server.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default TaskMasterMCPServer;
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
import chalk from "chalk";
|
||||
import chalk from 'chalk';
|
||||
|
||||
// Define log levels
|
||||
const LOG_LEVELS = {
|
||||
debug: 0,
|
||||
info: 1,
|
||||
warn: 2,
|
||||
error: 3,
|
||||
success: 4,
|
||||
debug: 0,
|
||||
info: 1,
|
||||
warn: 2,
|
||||
error: 3,
|
||||
success: 4
|
||||
};
|
||||
|
||||
// Get log level from environment or default to info
|
||||
const LOG_LEVEL = process.env.LOG_LEVEL
|
||||
? LOG_LEVELS[process.env.LOG_LEVEL.toLowerCase()]
|
||||
: LOG_LEVELS.info;
|
||||
? LOG_LEVELS[process.env.LOG_LEVEL.toLowerCase()]
|
||||
: LOG_LEVELS.info;
|
||||
|
||||
/**
|
||||
* Logs a message with the specified level
|
||||
@@ -20,29 +20,29 @@ const LOG_LEVEL = process.env.LOG_LEVEL
|
||||
* @param {...any} args - Arguments to log
|
||||
*/
|
||||
function log(level, ...args) {
|
||||
const icons = {
|
||||
debug: chalk.gray("🔍"),
|
||||
info: chalk.blue("ℹ️"),
|
||||
warn: chalk.yellow("⚠️"),
|
||||
error: chalk.red("❌"),
|
||||
success: chalk.green("✅"),
|
||||
};
|
||||
const icons = {
|
||||
debug: chalk.gray('🔍'),
|
||||
info: chalk.blue('ℹ️'),
|
||||
warn: chalk.yellow('⚠️'),
|
||||
error: chalk.red('❌'),
|
||||
success: chalk.green('✅')
|
||||
};
|
||||
|
||||
if (LOG_LEVELS[level] >= LOG_LEVEL) {
|
||||
const icon = icons[level] || "";
|
||||
if (LOG_LEVELS[level] >= LOG_LEVEL) {
|
||||
const icon = icons[level] || '';
|
||||
|
||||
if (level === "error") {
|
||||
console.error(icon, chalk.red(...args));
|
||||
} else if (level === "warn") {
|
||||
console.warn(icon, chalk.yellow(...args));
|
||||
} else if (level === "success") {
|
||||
console.log(icon, chalk.green(...args));
|
||||
} else if (level === "info") {
|
||||
console.log(icon, chalk.blue(...args));
|
||||
} else {
|
||||
console.log(icon, ...args);
|
||||
}
|
||||
}
|
||||
if (level === 'error') {
|
||||
console.error(icon, chalk.red(...args));
|
||||
} else if (level === 'warn') {
|
||||
console.warn(icon, chalk.yellow(...args));
|
||||
} else if (level === 'success') {
|
||||
console.log(icon, chalk.green(...args));
|
||||
} else if (level === 'info') {
|
||||
console.log(icon, chalk.blue(...args));
|
||||
} else {
|
||||
console.log(icon, ...args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,14 +51,14 @@ function log(level, ...args) {
|
||||
* @returns {Object} Logger object with info, error, debug, warn, and success methods
|
||||
*/
|
||||
export function createLogger() {
|
||||
return {
|
||||
debug: (message) => log("debug", message),
|
||||
info: (message) => log("info", message),
|
||||
warn: (message) => log("warn", message),
|
||||
error: (message) => log("error", message),
|
||||
success: (message) => log("success", message),
|
||||
log: log, // Also expose the raw log function
|
||||
};
|
||||
return {
|
||||
debug: (message) => log('debug', message),
|
||||
info: (message) => log('info', message),
|
||||
warn: (message) => log('warn', message),
|
||||
error: (message) => log('error', message),
|
||||
success: (message) => log('success', message),
|
||||
log: log // Also expose the raw log function
|
||||
};
|
||||
}
|
||||
|
||||
// Export a default logger instance
|
||||
|
||||
@@ -3,64 +3,64 @@
|
||||
* Tool to add a new task using AI
|
||||
*/
|
||||
|
||||
import { z } from "zod";
|
||||
import { z } from 'zod';
|
||||
import {
|
||||
executeTaskMasterCommand,
|
||||
createContentResponse,
|
||||
createErrorResponse,
|
||||
} from "./utils.js";
|
||||
executeTaskMasterCommand,
|
||||
createContentResponse,
|
||||
createErrorResponse
|
||||
} from './utils.js';
|
||||
|
||||
/**
|
||||
* Register the addTask tool with the MCP server
|
||||
* @param {FastMCP} server - FastMCP server instance
|
||||
*/
|
||||
export function registerAddTaskTool(server) {
|
||||
server.addTool({
|
||||
name: "addTask",
|
||||
description: "Add a new task using AI",
|
||||
parameters: z.object({
|
||||
prompt: z.string().describe("Description of the task to add"),
|
||||
dependencies: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe("Comma-separated list of task IDs this task depends on"),
|
||||
priority: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe("Task priority (high, medium, low)"),
|
||||
file: z.string().optional().describe("Path to the tasks file"),
|
||||
projectRoot: z
|
||||
.string()
|
||||
.describe(
|
||||
"Root directory of the project (default: current working directory)"
|
||||
),
|
||||
}),
|
||||
execute: async (args, { log }) => {
|
||||
try {
|
||||
log.info(`Adding new task: ${args.prompt}`);
|
||||
server.addTool({
|
||||
name: 'addTask',
|
||||
description: 'Add a new task using AI',
|
||||
parameters: z.object({
|
||||
prompt: z.string().describe('Description of the task to add'),
|
||||
dependencies: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe('Comma-separated list of task IDs this task depends on'),
|
||||
priority: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe('Task priority (high, medium, low)'),
|
||||
file: z.string().optional().describe('Path to the tasks file'),
|
||||
projectRoot: z
|
||||
.string()
|
||||
.describe(
|
||||
'Root directory of the project (default: current working directory)'
|
||||
)
|
||||
}),
|
||||
execute: async (args, { log }) => {
|
||||
try {
|
||||
log.info(`Adding new task: ${args.prompt}`);
|
||||
|
||||
const cmdArgs = [`--prompt="${args.prompt}"`];
|
||||
if (args.dependencies)
|
||||
cmdArgs.push(`--dependencies=${args.dependencies}`);
|
||||
if (args.priority) cmdArgs.push(`--priority=${args.priority}`);
|
||||
if (args.file) cmdArgs.push(`--file=${args.file}`);
|
||||
const cmdArgs = [`--prompt="${args.prompt}"`];
|
||||
if (args.dependencies)
|
||||
cmdArgs.push(`--dependencies=${args.dependencies}`);
|
||||
if (args.priority) cmdArgs.push(`--priority=${args.priority}`);
|
||||
if (args.file) cmdArgs.push(`--file=${args.file}`);
|
||||
|
||||
const result = executeTaskMasterCommand(
|
||||
"add-task",
|
||||
log,
|
||||
cmdArgs,
|
||||
projectRoot
|
||||
);
|
||||
const result = executeTaskMasterCommand(
|
||||
'add-task',
|
||||
log,
|
||||
cmdArgs,
|
||||
projectRoot
|
||||
);
|
||||
|
||||
if (!result.success) {
|
||||
throw new Error(result.error);
|
||||
}
|
||||
if (!result.success) {
|
||||
throw new Error(result.error);
|
||||
}
|
||||
|
||||
return createContentResponse(result.stdout);
|
||||
} catch (error) {
|
||||
log.error(`Error adding task: ${error.message}`);
|
||||
return createErrorResponse(`Error adding task: ${error.message}`);
|
||||
}
|
||||
},
|
||||
});
|
||||
return createContentResponse(result.stdout);
|
||||
} catch (error) {
|
||||
log.error(`Error adding task: ${error.message}`);
|
||||
return createErrorResponse(`Error adding task: ${error.message}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,76 +3,76 @@
|
||||
* Tool to break down a task into detailed subtasks
|
||||
*/
|
||||
|
||||
import { z } from "zod";
|
||||
import { z } from 'zod';
|
||||
import {
|
||||
executeTaskMasterCommand,
|
||||
createContentResponse,
|
||||
createErrorResponse,
|
||||
} from "./utils.js";
|
||||
executeTaskMasterCommand,
|
||||
createContentResponse,
|
||||
createErrorResponse
|
||||
} from './utils.js';
|
||||
|
||||
/**
|
||||
* Register the expandTask tool with the MCP server
|
||||
* @param {Object} server - FastMCP server instance
|
||||
*/
|
||||
export function registerExpandTaskTool(server) {
|
||||
server.addTool({
|
||||
name: "expandTask",
|
||||
description: "Break down a task into detailed subtasks",
|
||||
parameters: z.object({
|
||||
id: z.string().describe("Task ID to expand"),
|
||||
num: z.number().optional().describe("Number of subtasks to generate"),
|
||||
research: z
|
||||
.boolean()
|
||||
.optional()
|
||||
.describe(
|
||||
"Enable Perplexity AI for research-backed subtask generation"
|
||||
),
|
||||
prompt: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe("Additional context to guide subtask generation"),
|
||||
force: z
|
||||
.boolean()
|
||||
.optional()
|
||||
.describe(
|
||||
"Force regeneration of subtasks for tasks that already have them"
|
||||
),
|
||||
file: z.string().optional().describe("Path to the tasks file"),
|
||||
projectRoot: z
|
||||
.string()
|
||||
.describe(
|
||||
"Root directory of the project (default: current working directory)"
|
||||
),
|
||||
}),
|
||||
execute: async (args, { log }) => {
|
||||
try {
|
||||
log.info(`Expanding task ${args.id}`);
|
||||
server.addTool({
|
||||
name: 'expandTask',
|
||||
description: 'Break down a task into detailed subtasks',
|
||||
parameters: z.object({
|
||||
id: z.string().describe('Task ID to expand'),
|
||||
num: z.number().optional().describe('Number of subtasks to generate'),
|
||||
research: z
|
||||
.boolean()
|
||||
.optional()
|
||||
.describe(
|
||||
'Enable Perplexity AI for research-backed subtask generation'
|
||||
),
|
||||
prompt: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe('Additional context to guide subtask generation'),
|
||||
force: z
|
||||
.boolean()
|
||||
.optional()
|
||||
.describe(
|
||||
'Force regeneration of subtasks for tasks that already have them'
|
||||
),
|
||||
file: z.string().optional().describe('Path to the tasks file'),
|
||||
projectRoot: z
|
||||
.string()
|
||||
.describe(
|
||||
'Root directory of the project (default: current working directory)'
|
||||
)
|
||||
}),
|
||||
execute: async (args, { log }) => {
|
||||
try {
|
||||
log.info(`Expanding task ${args.id}`);
|
||||
|
||||
const cmdArgs = [`--id=${args.id}`];
|
||||
if (args.num) cmdArgs.push(`--num=${args.num}`);
|
||||
if (args.research) cmdArgs.push("--research");
|
||||
if (args.prompt) cmdArgs.push(`--prompt="${args.prompt}"`);
|
||||
if (args.force) cmdArgs.push("--force");
|
||||
if (args.file) cmdArgs.push(`--file=${args.file}`);
|
||||
const cmdArgs = [`--id=${args.id}`];
|
||||
if (args.num) cmdArgs.push(`--num=${args.num}`);
|
||||
if (args.research) cmdArgs.push('--research');
|
||||
if (args.prompt) cmdArgs.push(`--prompt="${args.prompt}"`);
|
||||
if (args.force) cmdArgs.push('--force');
|
||||
if (args.file) cmdArgs.push(`--file=${args.file}`);
|
||||
|
||||
const projectRoot = args.projectRoot;
|
||||
const projectRoot = args.projectRoot;
|
||||
|
||||
const result = executeTaskMasterCommand(
|
||||
"expand",
|
||||
log,
|
||||
cmdArgs,
|
||||
projectRoot
|
||||
);
|
||||
const result = executeTaskMasterCommand(
|
||||
'expand',
|
||||
log,
|
||||
cmdArgs,
|
||||
projectRoot
|
||||
);
|
||||
|
||||
if (!result.success) {
|
||||
throw new Error(result.error);
|
||||
}
|
||||
if (!result.success) {
|
||||
throw new Error(result.error);
|
||||
}
|
||||
|
||||
return createContentResponse(result.stdout);
|
||||
} catch (error) {
|
||||
log.error(`Error expanding task: ${error.message}`);
|
||||
return createErrorResponse(`Error expanding task: ${error.message}`);
|
||||
}
|
||||
},
|
||||
});
|
||||
return createContentResponse(result.stdout);
|
||||
} catch (error) {
|
||||
log.error(`Error expanding task: ${error.message}`);
|
||||
return createErrorResponse(`Error expanding task: ${error.message}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,27 +3,27 @@
|
||||
* Export all Task Master CLI tools for MCP server
|
||||
*/
|
||||
|
||||
import logger from "../logger.js";
|
||||
import { registerListTasksTool } from "./listTasks.js";
|
||||
import { registerShowTaskTool } from "./showTask.js";
|
||||
import { registerSetTaskStatusTool } from "./setTaskStatus.js";
|
||||
import { registerExpandTaskTool } from "./expandTask.js";
|
||||
import { registerNextTaskTool } from "./nextTask.js";
|
||||
import { registerAddTaskTool } from "./addTask.js";
|
||||
import logger from '../logger.js';
|
||||
import { registerListTasksTool } from './listTasks.js';
|
||||
import { registerShowTaskTool } from './showTask.js';
|
||||
import { registerSetTaskStatusTool } from './setTaskStatus.js';
|
||||
import { registerExpandTaskTool } from './expandTask.js';
|
||||
import { registerNextTaskTool } from './nextTask.js';
|
||||
import { registerAddTaskTool } from './addTask.js';
|
||||
|
||||
/**
|
||||
* Register all Task Master tools with the MCP server
|
||||
* @param {Object} server - FastMCP server instance
|
||||
*/
|
||||
export function registerTaskMasterTools(server) {
|
||||
registerListTasksTool(server);
|
||||
registerShowTaskTool(server);
|
||||
registerSetTaskStatusTool(server);
|
||||
registerExpandTaskTool(server);
|
||||
registerNextTaskTool(server);
|
||||
registerAddTaskTool(server);
|
||||
registerListTasksTool(server);
|
||||
registerShowTaskTool(server);
|
||||
registerSetTaskStatusTool(server);
|
||||
registerExpandTaskTool(server);
|
||||
registerNextTaskTool(server);
|
||||
registerAddTaskTool(server);
|
||||
}
|
||||
|
||||
export default {
|
||||
registerTaskMasterTools,
|
||||
registerTaskMasterTools
|
||||
};
|
||||
|
||||
@@ -3,51 +3,50 @@
|
||||
* Tool to list all tasks from Task Master
|
||||
*/
|
||||
|
||||
import { z } from "zod";
|
||||
import {
|
||||
createErrorResponse,
|
||||
handleApiResult
|
||||
} from "./utils.js";
|
||||
import { listTasksDirect } from "../core/task-master-core.js";
|
||||
import { z } from 'zod';
|
||||
import { createErrorResponse, handleApiResult } from './utils.js';
|
||||
import { listTasksDirect } from '../core/task-master-core.js';
|
||||
|
||||
/**
|
||||
* Register the listTasks tool with the MCP server
|
||||
* @param {Object} server - FastMCP server instance
|
||||
*/
|
||||
export function registerListTasksTool(server) {
|
||||
server.addTool({
|
||||
name: "listTasks",
|
||||
description: "List all tasks from Task Master",
|
||||
parameters: z.object({
|
||||
status: z.string().optional().describe("Filter tasks by status"),
|
||||
withSubtasks: z
|
||||
.boolean()
|
||||
.optional()
|
||||
.describe("Include subtasks in the response"),
|
||||
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(`Listing tasks with filters: ${JSON.stringify(args)}`);
|
||||
|
||||
// Call core function - args contains projectRoot which is handled internally
|
||||
const result = await listTasksDirect(args, log);
|
||||
|
||||
// Log result and use handleApiResult utility
|
||||
log.info(`Retrieved ${result.success ? (result.data?.tasks?.length || 0) : 0} tasks`);
|
||||
return handleApiResult(result, log, 'Error listing tasks');
|
||||
} catch (error) {
|
||||
log.error(`Error listing tasks: ${error.message}`);
|
||||
return createErrorResponse(error.message);
|
||||
}
|
||||
},
|
||||
});
|
||||
server.addTool({
|
||||
name: 'listTasks',
|
||||
description: 'List all tasks from Task Master',
|
||||
parameters: z.object({
|
||||
status: z.string().optional().describe('Filter tasks by status'),
|
||||
withSubtasks: z
|
||||
.boolean()
|
||||
.optional()
|
||||
.describe('Include subtasks in the response'),
|
||||
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(`Listing tasks with filters: ${JSON.stringify(args)}`);
|
||||
|
||||
// Call core function - args contains projectRoot which is handled internally
|
||||
const result = await listTasksDirect(args, log);
|
||||
|
||||
// Log result and use handleApiResult utility
|
||||
log.info(
|
||||
`Retrieved ${result.success ? result.data?.tasks?.length || 0 : 0} tasks`
|
||||
);
|
||||
return handleApiResult(result, log, 'Error listing tasks');
|
||||
} catch (error) {
|
||||
log.error(`Error listing tasks: ${error.message}`);
|
||||
return createErrorResponse(error.message);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// We no longer need the formatTasksResponse function as we're returning raw JSON data
|
||||
|
||||
@@ -3,55 +3,55 @@
|
||||
* Tool to show the next task to work on based on dependencies and status
|
||||
*/
|
||||
|
||||
import { z } from "zod";
|
||||
import { z } from 'zod';
|
||||
import {
|
||||
executeTaskMasterCommand,
|
||||
createContentResponse,
|
||||
createErrorResponse,
|
||||
} from "./utils.js";
|
||||
executeTaskMasterCommand,
|
||||
createContentResponse,
|
||||
createErrorResponse
|
||||
} from './utils.js';
|
||||
|
||||
/**
|
||||
* Register the nextTask tool with the MCP server
|
||||
* @param {Object} server - FastMCP server instance
|
||||
*/
|
||||
export function registerNextTaskTool(server) {
|
||||
server.addTool({
|
||||
name: "nextTask",
|
||||
description:
|
||||
"Show 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()
|
||||
.describe(
|
||||
"Root directory of the project (default: current working directory)"
|
||||
),
|
||||
}),
|
||||
execute: async (args, { log }) => {
|
||||
try {
|
||||
log.info(`Finding next task to work on`);
|
||||
server.addTool({
|
||||
name: 'nextTask',
|
||||
description:
|
||||
'Show 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()
|
||||
.describe(
|
||||
'Root directory of the project (default: current working directory)'
|
||||
)
|
||||
}),
|
||||
execute: async (args, { log }) => {
|
||||
try {
|
||||
log.info(`Finding next task to work on`);
|
||||
|
||||
const cmdArgs = [];
|
||||
if (args.file) cmdArgs.push(`--file=${args.file}`);
|
||||
const cmdArgs = [];
|
||||
if (args.file) cmdArgs.push(`--file=${args.file}`);
|
||||
|
||||
const projectRoot = args.projectRoot;
|
||||
const projectRoot = args.projectRoot;
|
||||
|
||||
const result = executeTaskMasterCommand(
|
||||
"next",
|
||||
log,
|
||||
cmdArgs,
|
||||
projectRoot
|
||||
);
|
||||
const result = executeTaskMasterCommand(
|
||||
'next',
|
||||
log,
|
||||
cmdArgs,
|
||||
projectRoot
|
||||
);
|
||||
|
||||
if (!result.success) {
|
||||
throw new Error(result.error);
|
||||
}
|
||||
if (!result.success) {
|
||||
throw new Error(result.error);
|
||||
}
|
||||
|
||||
return createContentResponse(result.stdout);
|
||||
} catch (error) {
|
||||
log.error(`Error finding next task: ${error.message}`);
|
||||
return createErrorResponse(`Error finding next task: ${error.message}`);
|
||||
}
|
||||
},
|
||||
});
|
||||
return createContentResponse(result.stdout);
|
||||
} catch (error) {
|
||||
log.error(`Error finding next task: ${error.message}`);
|
||||
return createErrorResponse(`Error finding next task: ${error.message}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,62 +3,62 @@
|
||||
* Tool to set the status of a task
|
||||
*/
|
||||
|
||||
import { z } from "zod";
|
||||
import { z } from 'zod';
|
||||
import {
|
||||
executeTaskMasterCommand,
|
||||
createContentResponse,
|
||||
createErrorResponse,
|
||||
} from "./utils.js";
|
||||
executeTaskMasterCommand,
|
||||
createContentResponse,
|
||||
createErrorResponse
|
||||
} from './utils.js';
|
||||
|
||||
/**
|
||||
* Register the setTaskStatus tool with the MCP server
|
||||
* @param {Object} server - FastMCP server instance
|
||||
*/
|
||||
export function registerSetTaskStatusTool(server) {
|
||||
server.addTool({
|
||||
name: "setTaskStatus",
|
||||
description: "Set the status of a task",
|
||||
parameters: z.object({
|
||||
id: z
|
||||
.string()
|
||||
.describe("Task ID (can be comma-separated for multiple tasks)"),
|
||||
status: z
|
||||
.string()
|
||||
.describe("New status (todo, in-progress, review, done)"),
|
||||
file: z.string().optional().describe("Path to the tasks file"),
|
||||
projectRoot: z
|
||||
.string()
|
||||
.describe(
|
||||
"Root directory of the project (default: current working directory)"
|
||||
),
|
||||
}),
|
||||
execute: async (args, { log }) => {
|
||||
try {
|
||||
log.info(`Setting status of task(s) ${args.id} to: ${args.status}`);
|
||||
server.addTool({
|
||||
name: 'setTaskStatus',
|
||||
description: 'Set the status of a task',
|
||||
parameters: z.object({
|
||||
id: z
|
||||
.string()
|
||||
.describe('Task ID (can be comma-separated for multiple tasks)'),
|
||||
status: z
|
||||
.string()
|
||||
.describe('New status (todo, in-progress, review, done)'),
|
||||
file: z.string().optional().describe('Path to the tasks file'),
|
||||
projectRoot: z
|
||||
.string()
|
||||
.describe(
|
||||
'Root directory of the project (default: current working directory)'
|
||||
)
|
||||
}),
|
||||
execute: async (args, { log }) => {
|
||||
try {
|
||||
log.info(`Setting status of task(s) ${args.id} to: ${args.status}`);
|
||||
|
||||
const cmdArgs = [`--id=${args.id}`, `--status=${args.status}`];
|
||||
if (args.file) cmdArgs.push(`--file=${args.file}`);
|
||||
const cmdArgs = [`--id=${args.id}`, `--status=${args.status}`];
|
||||
if (args.file) cmdArgs.push(`--file=${args.file}`);
|
||||
|
||||
const projectRoot = args.projectRoot;
|
||||
const projectRoot = args.projectRoot;
|
||||
|
||||
const result = executeTaskMasterCommand(
|
||||
"set-status",
|
||||
log,
|
||||
cmdArgs,
|
||||
projectRoot
|
||||
);
|
||||
const result = executeTaskMasterCommand(
|
||||
'set-status',
|
||||
log,
|
||||
cmdArgs,
|
||||
projectRoot
|
||||
);
|
||||
|
||||
if (!result.success) {
|
||||
throw new Error(result.error);
|
||||
}
|
||||
if (!result.success) {
|
||||
throw new Error(result.error);
|
||||
}
|
||||
|
||||
return createContentResponse(result.stdout);
|
||||
} catch (error) {
|
||||
log.error(`Error setting task status: ${error.message}`);
|
||||
return createErrorResponse(
|
||||
`Error setting task status: ${error.message}`
|
||||
);
|
||||
}
|
||||
},
|
||||
});
|
||||
return createContentResponse(result.stdout);
|
||||
} catch (error) {
|
||||
log.error(`Error setting task status: ${error.message}`);
|
||||
return createErrorResponse(
|
||||
`Error setting task status: ${error.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,76 +3,80 @@
|
||||
* Tool to show detailed information about a specific task
|
||||
*/
|
||||
|
||||
import { z } from "zod";
|
||||
import { z } from 'zod';
|
||||
import {
|
||||
executeTaskMasterCommand,
|
||||
createErrorResponse,
|
||||
handleApiResult
|
||||
} from "./utils.js";
|
||||
executeTaskMasterCommand,
|
||||
createErrorResponse,
|
||||
handleApiResult
|
||||
} from './utils.js';
|
||||
|
||||
/**
|
||||
* Register the showTask tool with the MCP server
|
||||
* @param {Object} server - FastMCP server instance
|
||||
*/
|
||||
export function registerShowTaskTool(server) {
|
||||
server.addTool({
|
||||
name: "showTask",
|
||||
description: "Show detailed information about a specific task",
|
||||
parameters: z.object({
|
||||
id: z.string().describe("Task ID to show"),
|
||||
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(`Showing task details for ID: ${args.id}`);
|
||||
server.addTool({
|
||||
name: 'showTask',
|
||||
description: 'Show detailed information about a specific task',
|
||||
parameters: z.object({
|
||||
id: z.string().describe('Task ID to show'),
|
||||
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(`Showing task details for ID: ${args.id}`);
|
||||
|
||||
// Prepare arguments for CLI command
|
||||
const cmdArgs = [`--id=${args.id}`];
|
||||
if (args.file) cmdArgs.push(`--file=${args.file}`);
|
||||
// Prepare arguments for CLI command
|
||||
const cmdArgs = [`--id=${args.id}`];
|
||||
if (args.file) cmdArgs.push(`--file=${args.file}`);
|
||||
|
||||
// Execute the command - function now handles project root internally
|
||||
const result = executeTaskMasterCommand(
|
||||
"show",
|
||||
log,
|
||||
cmdArgs,
|
||||
args.projectRoot // Pass raw project root, function will normalize it
|
||||
);
|
||||
// Execute the command - function now handles project root internally
|
||||
const result = executeTaskMasterCommand(
|
||||
'show',
|
||||
log,
|
||||
cmdArgs,
|
||||
args.projectRoot // Pass raw project root, function will normalize it
|
||||
);
|
||||
|
||||
// Process CLI result into API result format for handleApiResult
|
||||
if (result.success) {
|
||||
try {
|
||||
// Try to parse response as JSON
|
||||
const data = JSON.parse(result.stdout);
|
||||
// Return equivalent of a successful API call with data
|
||||
return handleApiResult({ success: true, data }, log, 'Error showing task');
|
||||
} catch (e) {
|
||||
// If parsing fails, still return success but with raw string data
|
||||
return handleApiResult(
|
||||
{ success: true, data: result.stdout },
|
||||
log,
|
||||
'Error showing task',
|
||||
// Skip data processing for string data
|
||||
null
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// Return equivalent of a failed API call
|
||||
return handleApiResult(
|
||||
{ success: false, error: { message: result.error } },
|
||||
log,
|
||||
'Error showing task'
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
log.error(`Error showing task: ${error.message}`);
|
||||
return createErrorResponse(error.message);
|
||||
}
|
||||
},
|
||||
});
|
||||
// Process CLI result into API result format for handleApiResult
|
||||
if (result.success) {
|
||||
try {
|
||||
// Try to parse response as JSON
|
||||
const data = JSON.parse(result.stdout);
|
||||
// Return equivalent of a successful API call with data
|
||||
return handleApiResult(
|
||||
{ success: true, data },
|
||||
log,
|
||||
'Error showing task'
|
||||
);
|
||||
} catch (e) {
|
||||
// If parsing fails, still return success but with raw string data
|
||||
return handleApiResult(
|
||||
{ success: true, data: result.stdout },
|
||||
log,
|
||||
'Error showing task',
|
||||
// Skip data processing for string data
|
||||
null
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// Return equivalent of a failed API call
|
||||
return handleApiResult(
|
||||
{ success: false, error: { message: result.error } },
|
||||
log,
|
||||
'Error showing task'
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
log.error(`Error showing task: ${error.message}`);
|
||||
return createErrorResponse(error.message);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,27 +3,27 @@
|
||||
* Utility functions for Task Master CLI integration
|
||||
*/
|
||||
|
||||
import { spawnSync } from "child_process";
|
||||
import path from "path";
|
||||
import { spawnSync } from 'child_process';
|
||||
import path from 'path';
|
||||
import { contextManager } from '../core/context-manager.js'; // Import the singleton
|
||||
|
||||
/**
|
||||
* Get normalized project root path
|
||||
* Get normalized project root path
|
||||
* @param {string|undefined} projectRootRaw - Raw project root from arguments
|
||||
* @param {Object} log - Logger object
|
||||
* @returns {string} - Normalized absolute path to project root
|
||||
*/
|
||||
export function getProjectRoot(projectRootRaw, log) {
|
||||
// Make sure projectRoot is set
|
||||
const rootPath = projectRootRaw || process.cwd();
|
||||
|
||||
// Ensure projectRoot is absolute
|
||||
const projectRoot = path.isAbsolute(rootPath)
|
||||
? rootPath
|
||||
: path.resolve(process.cwd(), rootPath);
|
||||
|
||||
log.info(`Using project root: ${projectRoot}`);
|
||||
return projectRoot;
|
||||
// Make sure projectRoot is set
|
||||
const rootPath = projectRootRaw || process.cwd();
|
||||
|
||||
// Ensure projectRoot is absolute
|
||||
const projectRoot = path.isAbsolute(rootPath)
|
||||
? rootPath
|
||||
: path.resolve(process.cwd(), rootPath);
|
||||
|
||||
log.info(`Using project root: ${projectRoot}`);
|
||||
return projectRoot;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,28 +34,35 @@ export function getProjectRoot(projectRootRaw, log) {
|
||||
* @param {Function} processFunction - Optional function to process successful result data
|
||||
* @returns {Object} - Standardized MCP response object
|
||||
*/
|
||||
export function handleApiResult(result, log, errorPrefix = 'API error', processFunction = processMCPResponseData) {
|
||||
if (!result.success) {
|
||||
const errorMsg = result.error?.message || `Unknown ${errorPrefix}`;
|
||||
// Include cache status in error logs
|
||||
log.error(`${errorPrefix}: ${errorMsg}. From cache: ${result.fromCache}`); // Keep logging cache status on error
|
||||
return createErrorResponse(errorMsg);
|
||||
}
|
||||
|
||||
// Process the result data if needed
|
||||
const processedData = processFunction ? processFunction(result.data) : result.data;
|
||||
|
||||
// Log success including cache status
|
||||
log.info(`Successfully completed operation. From cache: ${result.fromCache}`); // Add success log with cache status
|
||||
export function handleApiResult(
|
||||
result,
|
||||
log,
|
||||
errorPrefix = 'API error',
|
||||
processFunction = processMCPResponseData
|
||||
) {
|
||||
if (!result.success) {
|
||||
const errorMsg = result.error?.message || `Unknown ${errorPrefix}`;
|
||||
// Include cache status in error logs
|
||||
log.error(`${errorPrefix}: ${errorMsg}. From cache: ${result.fromCache}`); // Keep logging cache status on error
|
||||
return createErrorResponse(errorMsg);
|
||||
}
|
||||
|
||||
// Create the response payload including the fromCache flag
|
||||
const responsePayload = {
|
||||
fromCache: result.fromCache, // Get the flag from the original 'result'
|
||||
data: processedData // Nest the processed data under a 'data' key
|
||||
};
|
||||
|
||||
// Pass this combined payload to createContentResponse
|
||||
return createContentResponse(responsePayload);
|
||||
// Process the result data if needed
|
||||
const processedData = processFunction
|
||||
? processFunction(result.data)
|
||||
: result.data;
|
||||
|
||||
// Log success including cache status
|
||||
log.info(`Successfully completed operation. From cache: ${result.fromCache}`); // Add success log with cache status
|
||||
|
||||
// Create the response payload including the fromCache flag
|
||||
const responsePayload = {
|
||||
fromCache: result.fromCache, // Get the flag from the original 'result'
|
||||
data: processedData // Nest the processed data under a 'data' key
|
||||
};
|
||||
|
||||
// Pass this combined payload to createContentResponse
|
||||
return createContentResponse(responsePayload);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,68 +74,68 @@ export function handleApiResult(result, log, errorPrefix = 'API error', processF
|
||||
* @returns {Object} - The result of the command execution
|
||||
*/
|
||||
export function executeTaskMasterCommand(
|
||||
command,
|
||||
log,
|
||||
args = [],
|
||||
projectRootRaw = null
|
||||
command,
|
||||
log,
|
||||
args = [],
|
||||
projectRootRaw = null
|
||||
) {
|
||||
try {
|
||||
// Normalize project root internally using the getProjectRoot utility
|
||||
const cwd = getProjectRoot(projectRootRaw, log);
|
||||
try {
|
||||
// Normalize project root internally using the getProjectRoot utility
|
||||
const cwd = getProjectRoot(projectRootRaw, log);
|
||||
|
||||
log.info(
|
||||
`Executing task-master ${command} with args: ${JSON.stringify(
|
||||
args
|
||||
)} in directory: ${cwd}`
|
||||
);
|
||||
log.info(
|
||||
`Executing task-master ${command} with args: ${JSON.stringify(
|
||||
args
|
||||
)} in directory: ${cwd}`
|
||||
);
|
||||
|
||||
// Prepare full arguments array
|
||||
const fullArgs = [command, ...args];
|
||||
// Prepare full arguments array
|
||||
const fullArgs = [command, ...args];
|
||||
|
||||
// Common options for spawn
|
||||
const spawnOptions = {
|
||||
encoding: "utf8",
|
||||
cwd: cwd,
|
||||
};
|
||||
// Common options for spawn
|
||||
const spawnOptions = {
|
||||
encoding: 'utf8',
|
||||
cwd: cwd
|
||||
};
|
||||
|
||||
// Execute the command using the global task-master CLI or local script
|
||||
// Try the global CLI first
|
||||
let result = spawnSync("task-master", fullArgs, spawnOptions);
|
||||
// Execute the command using the global task-master CLI or local script
|
||||
// Try the global CLI first
|
||||
let result = spawnSync('task-master', fullArgs, spawnOptions);
|
||||
|
||||
// If global CLI is not available, try fallback to the local script
|
||||
if (result.error && result.error.code === "ENOENT") {
|
||||
log.info("Global task-master not found, falling back to local script");
|
||||
result = spawnSync("node", ["scripts/dev.js", ...fullArgs], spawnOptions);
|
||||
}
|
||||
// If global CLI is not available, try fallback to the local script
|
||||
if (result.error && result.error.code === 'ENOENT') {
|
||||
log.info('Global task-master not found, falling back to local script');
|
||||
result = spawnSync('node', ['scripts/dev.js', ...fullArgs], spawnOptions);
|
||||
}
|
||||
|
||||
if (result.error) {
|
||||
throw new Error(`Command execution error: ${result.error.message}`);
|
||||
}
|
||||
if (result.error) {
|
||||
throw new Error(`Command execution error: ${result.error.message}`);
|
||||
}
|
||||
|
||||
if (result.status !== 0) {
|
||||
// Improve error handling by combining stderr and stdout if stderr is empty
|
||||
const errorOutput = result.stderr
|
||||
? result.stderr.trim()
|
||||
: result.stdout
|
||||
? result.stdout.trim()
|
||||
: "Unknown error";
|
||||
throw new Error(
|
||||
`Command failed with exit code ${result.status}: ${errorOutput}`
|
||||
);
|
||||
}
|
||||
if (result.status !== 0) {
|
||||
// Improve error handling by combining stderr and stdout if stderr is empty
|
||||
const errorOutput = result.stderr
|
||||
? result.stderr.trim()
|
||||
: result.stdout
|
||||
? result.stdout.trim()
|
||||
: 'Unknown error';
|
||||
throw new Error(
|
||||
`Command failed with exit code ${result.status}: ${errorOutput}`
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
stdout: result.stdout,
|
||||
stderr: result.stderr,
|
||||
};
|
||||
} catch (error) {
|
||||
log.error(`Error executing task-master command: ${error.message}`);
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
return {
|
||||
success: true,
|
||||
stdout: result.stdout,
|
||||
stderr: result.stderr
|
||||
};
|
||||
} catch (error) {
|
||||
log.error(`Error executing task-master command: ${error.message}`);
|
||||
return {
|
||||
success: false,
|
||||
error: error.message
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,40 +151,44 @@ export function executeTaskMasterCommand(
|
||||
* Format: { success: boolean, data?: any, error?: { code: string, message: string }, fromCache: boolean }
|
||||
*/
|
||||
export async function getCachedOrExecute({ cacheKey, actionFn, log }) {
|
||||
// Check cache first
|
||||
const cachedResult = contextManager.getCachedData(cacheKey);
|
||||
|
||||
if (cachedResult !== undefined) {
|
||||
log.info(`Cache hit for key: ${cacheKey}`);
|
||||
// Return the cached data in the same structure as a fresh result
|
||||
return {
|
||||
...cachedResult, // Spread the cached result to maintain its structure
|
||||
fromCache: true // Just add the fromCache flag
|
||||
};
|
||||
}
|
||||
// Check cache first
|
||||
const cachedResult = contextManager.getCachedData(cacheKey);
|
||||
|
||||
log.info(`Cache miss for key: ${cacheKey}. Executing action function.`);
|
||||
|
||||
// Execute the action function if cache missed
|
||||
const result = await actionFn();
|
||||
|
||||
// If the action was successful, cache the result (but without fromCache flag)
|
||||
if (result.success && result.data !== undefined) {
|
||||
log.info(`Action successful. Caching result for key: ${cacheKey}`);
|
||||
// Cache the entire result structure (minus the fromCache flag)
|
||||
const { fromCache, ...resultToCache } = result;
|
||||
contextManager.setCachedData(cacheKey, resultToCache);
|
||||
} else if (!result.success) {
|
||||
log.warn(`Action failed for cache key ${cacheKey}. Result not cached. Error: ${result.error?.message}`);
|
||||
} else {
|
||||
log.warn(`Action for cache key ${cacheKey} succeeded but returned no data. Result not cached.`);
|
||||
}
|
||||
|
||||
// Return the fresh result, indicating it wasn't from cache
|
||||
return {
|
||||
...result,
|
||||
fromCache: false
|
||||
};
|
||||
if (cachedResult !== undefined) {
|
||||
log.info(`Cache hit for key: ${cacheKey}`);
|
||||
// Return the cached data in the same structure as a fresh result
|
||||
return {
|
||||
...cachedResult, // Spread the cached result to maintain its structure
|
||||
fromCache: true // Just add the fromCache flag
|
||||
};
|
||||
}
|
||||
|
||||
log.info(`Cache miss for key: ${cacheKey}. Executing action function.`);
|
||||
|
||||
// Execute the action function if cache missed
|
||||
const result = await actionFn();
|
||||
|
||||
// If the action was successful, cache the result (but without fromCache flag)
|
||||
if (result.success && result.data !== undefined) {
|
||||
log.info(`Action successful. Caching result for key: ${cacheKey}`);
|
||||
// Cache the entire result structure (minus the fromCache flag)
|
||||
const { fromCache, ...resultToCache } = result;
|
||||
contextManager.setCachedData(cacheKey, resultToCache);
|
||||
} else if (!result.success) {
|
||||
log.warn(
|
||||
`Action failed for cache key ${cacheKey}. Result not cached. Error: ${result.error?.message}`
|
||||
);
|
||||
} else {
|
||||
log.warn(
|
||||
`Action for cache key ${cacheKey} succeeded but returned no data. Result not cached.`
|
||||
);
|
||||
}
|
||||
|
||||
// Return the fresh result, indicating it wasn't from cache
|
||||
return {
|
||||
...result,
|
||||
fromCache: false
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -194,79 +205,92 @@ export async function getCachedOrExecute({ cacheKey, actionFn, log }) {
|
||||
* @returns {Promise<Object>} - Standardized response for FastMCP.
|
||||
*/
|
||||
export async function executeMCPToolAction({
|
||||
actionFn,
|
||||
args,
|
||||
log,
|
||||
actionName,
|
||||
cacheKeyGenerator, // Note: We decided not to use this for listTasks for now
|
||||
processResult = processMCPResponseData
|
||||
actionFn,
|
||||
args,
|
||||
log,
|
||||
actionName,
|
||||
cacheKeyGenerator, // Note: We decided not to use this for listTasks for now
|
||||
processResult = processMCPResponseData
|
||||
}) {
|
||||
try {
|
||||
// Log the action start
|
||||
log.info(`${actionName} with args: ${JSON.stringify(args)}`);
|
||||
try {
|
||||
// Log the action start
|
||||
log.info(`${actionName} with args: ${JSON.stringify(args)}`);
|
||||
|
||||
// Normalize project root path - common to almost all tools
|
||||
const projectRootRaw = args.projectRoot || process.cwd();
|
||||
const projectRoot = path.isAbsolute(projectRootRaw)
|
||||
? projectRootRaw
|
||||
: path.resolve(process.cwd(), projectRootRaw);
|
||||
// Normalize project root path - common to almost all tools
|
||||
const projectRootRaw = args.projectRoot || process.cwd();
|
||||
const projectRoot = path.isAbsolute(projectRootRaw)
|
||||
? projectRootRaw
|
||||
: path.resolve(process.cwd(), projectRootRaw);
|
||||
|
||||
log.info(`Using project root: ${projectRoot}`);
|
||||
const executionArgs = { ...args, projectRoot };
|
||||
log.info(`Using project root: ${projectRoot}`);
|
||||
const executionArgs = { ...args, projectRoot };
|
||||
|
||||
let result;
|
||||
const cacheKey = cacheKeyGenerator ? cacheKeyGenerator(executionArgs) : null;
|
||||
let result;
|
||||
const cacheKey = cacheKeyGenerator
|
||||
? cacheKeyGenerator(executionArgs)
|
||||
: null;
|
||||
|
||||
if (cacheKey) {
|
||||
// Use caching utility
|
||||
log.info(`Caching enabled for ${actionName} with key: ${cacheKey}`);
|
||||
const cacheWrappedAction = async () => await actionFn(executionArgs, log);
|
||||
result = await getCachedOrExecute({
|
||||
cacheKey,
|
||||
actionFn: cacheWrappedAction,
|
||||
log
|
||||
});
|
||||
} else {
|
||||
// Execute directly without caching
|
||||
log.info(`Caching disabled for ${actionName}. Executing directly.`);
|
||||
// We need to ensure the result from actionFn has a fromCache field
|
||||
// Let's assume actionFn now consistently returns { success, data/error, fromCache }
|
||||
// The current listTasksDirect does this if it calls getCachedOrExecute internally.
|
||||
result = await actionFn(executionArgs, log);
|
||||
// If the action function itself doesn't determine caching (like our original listTasksDirect refactor attempt),
|
||||
// we'd set it here:
|
||||
// result.fromCache = false;
|
||||
}
|
||||
if (cacheKey) {
|
||||
// Use caching utility
|
||||
log.info(`Caching enabled for ${actionName} with key: ${cacheKey}`);
|
||||
const cacheWrappedAction = async () => await actionFn(executionArgs, log);
|
||||
result = await getCachedOrExecute({
|
||||
cacheKey,
|
||||
actionFn: cacheWrappedAction,
|
||||
log
|
||||
});
|
||||
} else {
|
||||
// Execute directly without caching
|
||||
log.info(`Caching disabled for ${actionName}. Executing directly.`);
|
||||
// We need to ensure the result from actionFn has a fromCache field
|
||||
// Let's assume actionFn now consistently returns { success, data/error, fromCache }
|
||||
// The current listTasksDirect does this if it calls getCachedOrExecute internally.
|
||||
result = await actionFn(executionArgs, log);
|
||||
// If the action function itself doesn't determine caching (like our original listTasksDirect refactor attempt),
|
||||
// we'd set it here:
|
||||
// result.fromCache = false;
|
||||
}
|
||||
|
||||
// Handle error case
|
||||
if (!result.success) {
|
||||
const errorMsg = result.error?.message || `Unknown error during ${actionName.toLowerCase()}`;
|
||||
// Include fromCache in error logs too, might be useful
|
||||
log.error(`Error during ${actionName.toLowerCase()}: ${errorMsg}. From cache: ${result.fromCache}`);
|
||||
return createErrorResponse(errorMsg);
|
||||
}
|
||||
// Handle error case
|
||||
if (!result.success) {
|
||||
const errorMsg =
|
||||
result.error?.message ||
|
||||
`Unknown error during ${actionName.toLowerCase()}`;
|
||||
// Include fromCache in error logs too, might be useful
|
||||
log.error(
|
||||
`Error during ${actionName.toLowerCase()}: ${errorMsg}. From cache: ${result.fromCache}`
|
||||
);
|
||||
return createErrorResponse(errorMsg);
|
||||
}
|
||||
|
||||
// Log success
|
||||
log.info(`Successfully completed ${actionName.toLowerCase()}. From cache: ${result.fromCache}`);
|
||||
// Log success
|
||||
log.info(
|
||||
`Successfully completed ${actionName.toLowerCase()}. From cache: ${result.fromCache}`
|
||||
);
|
||||
|
||||
// Process the result data if needed
|
||||
const processedData = processResult ? processResult(result.data) : result.data;
|
||||
// Process the result data if needed
|
||||
const processedData = processResult
|
||||
? processResult(result.data)
|
||||
: result.data;
|
||||
|
||||
// Create a new object that includes both the processed data and the fromCache flag
|
||||
const responsePayload = {
|
||||
fromCache: result.fromCache, // Include the flag here
|
||||
data: processedData // Embed the actual data under a 'data' key
|
||||
};
|
||||
|
||||
// Pass this combined payload to createContentResponse
|
||||
return createContentResponse(responsePayload);
|
||||
// Create a new object that includes both the processed data and the fromCache flag
|
||||
const responsePayload = {
|
||||
fromCache: result.fromCache, // Include the flag here
|
||||
data: processedData // Embed the actual data under a 'data' key
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
// Handle unexpected errors during the execution wrapper itself
|
||||
log.error(`Unexpected error during ${actionName.toLowerCase()} execution wrapper: ${error.message}`);
|
||||
console.error(error.stack); // Log stack for debugging wrapper errors
|
||||
return createErrorResponse(`Internal server error during ${actionName.toLowerCase()}: ${error.message}`);
|
||||
}
|
||||
// Pass this combined payload to createContentResponse
|
||||
return createContentResponse(responsePayload);
|
||||
} catch (error) {
|
||||
// Handle unexpected errors during the execution wrapper itself
|
||||
log.error(
|
||||
`Unexpected error during ${actionName.toLowerCase()} execution wrapper: ${error.message}`
|
||||
);
|
||||
console.error(error.stack); // Log stack for debugging wrapper errors
|
||||
return createErrorResponse(
|
||||
`Internal server error during ${actionName.toLowerCase()}: ${error.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -276,56 +300,68 @@ export async function executeMCPToolAction({
|
||||
* @param {string[]} fieldsToRemove - An array of field names to remove.
|
||||
* @returns {Object|Array} - The processed data with specified fields removed.
|
||||
*/
|
||||
export function processMCPResponseData(taskOrData, fieldsToRemove = ['details', 'testStrategy']) {
|
||||
if (!taskOrData) {
|
||||
return taskOrData;
|
||||
}
|
||||
export function processMCPResponseData(
|
||||
taskOrData,
|
||||
fieldsToRemove = ['details', 'testStrategy']
|
||||
) {
|
||||
if (!taskOrData) {
|
||||
return taskOrData;
|
||||
}
|
||||
|
||||
// Helper function to process a single task object
|
||||
const processSingleTask = (task) => {
|
||||
if (typeof task !== 'object' || task === null) {
|
||||
return task;
|
||||
}
|
||||
|
||||
const processedTask = { ...task };
|
||||
|
||||
// Remove specified fields from the task
|
||||
fieldsToRemove.forEach(field => {
|
||||
delete processedTask[field];
|
||||
});
|
||||
// Helper function to process a single task object
|
||||
const processSingleTask = (task) => {
|
||||
if (typeof task !== 'object' || task === null) {
|
||||
return task;
|
||||
}
|
||||
|
||||
// Recursively process subtasks if they exist and are an array
|
||||
if (processedTask.subtasks && Array.isArray(processedTask.subtasks)) {
|
||||
// Use processArrayOfTasks to handle the subtasks array
|
||||
processedTask.subtasks = processArrayOfTasks(processedTask.subtasks);
|
||||
}
|
||||
|
||||
return processedTask;
|
||||
};
|
||||
|
||||
// Helper function to process an array of tasks
|
||||
const processArrayOfTasks = (tasks) => {
|
||||
return tasks.map(processSingleTask);
|
||||
};
|
||||
const processedTask = { ...task };
|
||||
|
||||
// Check if the input is a data structure containing a 'tasks' array (like from listTasks)
|
||||
if (typeof taskOrData === 'object' && taskOrData !== null && Array.isArray(taskOrData.tasks)) {
|
||||
return {
|
||||
...taskOrData, // Keep other potential fields like 'stats', 'filter'
|
||||
tasks: processArrayOfTasks(taskOrData.tasks),
|
||||
};
|
||||
}
|
||||
// Check if the input is likely a single task object (add more checks if needed)
|
||||
else if (typeof taskOrData === 'object' && taskOrData !== null && 'id' in taskOrData && 'title' in taskOrData) {
|
||||
return processSingleTask(taskOrData);
|
||||
}
|
||||
// Check if the input is an array of tasks directly (less common but possible)
|
||||
else if (Array.isArray(taskOrData)) {
|
||||
return processArrayOfTasks(taskOrData);
|
||||
}
|
||||
|
||||
// If it doesn't match known task structures, return it as is
|
||||
return taskOrData;
|
||||
// Remove specified fields from the task
|
||||
fieldsToRemove.forEach((field) => {
|
||||
delete processedTask[field];
|
||||
});
|
||||
|
||||
// Recursively process subtasks if they exist and are an array
|
||||
if (processedTask.subtasks && Array.isArray(processedTask.subtasks)) {
|
||||
// Use processArrayOfTasks to handle the subtasks array
|
||||
processedTask.subtasks = processArrayOfTasks(processedTask.subtasks);
|
||||
}
|
||||
|
||||
return processedTask;
|
||||
};
|
||||
|
||||
// Helper function to process an array of tasks
|
||||
const processArrayOfTasks = (tasks) => {
|
||||
return tasks.map(processSingleTask);
|
||||
};
|
||||
|
||||
// Check if the input is a data structure containing a 'tasks' array (like from listTasks)
|
||||
if (
|
||||
typeof taskOrData === 'object' &&
|
||||
taskOrData !== null &&
|
||||
Array.isArray(taskOrData.tasks)
|
||||
) {
|
||||
return {
|
||||
...taskOrData, // Keep other potential fields like 'stats', 'filter'
|
||||
tasks: processArrayOfTasks(taskOrData.tasks)
|
||||
};
|
||||
}
|
||||
// Check if the input is likely a single task object (add more checks if needed)
|
||||
else if (
|
||||
typeof taskOrData === 'object' &&
|
||||
taskOrData !== null &&
|
||||
'id' in taskOrData &&
|
||||
'title' in taskOrData
|
||||
) {
|
||||
return processSingleTask(taskOrData);
|
||||
}
|
||||
// Check if the input is an array of tasks directly (less common but possible)
|
||||
else if (Array.isArray(taskOrData)) {
|
||||
return processArrayOfTasks(taskOrData);
|
||||
}
|
||||
|
||||
// If it doesn't match known task structures, return it as is
|
||||
return taskOrData;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -334,19 +370,20 @@ export function processMCPResponseData(taskOrData, fieldsToRemove = ['details',
|
||||
* @returns {Object} - Content response object in FastMCP format
|
||||
*/
|
||||
export function createContentResponse(content) {
|
||||
// FastMCP requires text type, so we format objects as JSON strings
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: typeof content === 'object' ?
|
||||
// Format JSON nicely with indentation
|
||||
JSON.stringify(content, null, 2) :
|
||||
// Keep other content types as-is
|
||||
String(content)
|
||||
}
|
||||
]
|
||||
};
|
||||
// FastMCP requires text type, so we format objects as JSON strings
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text:
|
||||
typeof content === 'object'
|
||||
? // Format JSON nicely with indentation
|
||||
JSON.stringify(content, null, 2)
|
||||
: // Keep other content types as-is
|
||||
String(content)
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -355,13 +392,13 @@ export function createContentResponse(content) {
|
||||
* @returns {Object} - Error content response object in FastMCP format
|
||||
*/
|
||||
export function createErrorResponse(errorMessage) {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: `Error: ${errorMessage}`
|
||||
}
|
||||
],
|
||||
isError: true
|
||||
};
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Error: ${errorMessage}`
|
||||
}
|
||||
],
|
||||
isError: true
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user