Files
claude-task-master/tasks/task_023.txt
Eyal Toledano d65c76d4cc git commit -m "fix: improve CLI error handling and standardize option flags
This commit fixes several issues with command line interface error handling:

   1. Fix inconsistent behavior between --no-generate and --skip-generate:
      - Standardized on --skip-generate across all commands
      - Updated bin/task-master.js to use --skip-generate instead of --no-generate
      - Modified add-subtask and remove-subtask commands to use --skip-generate

   2. Enhance error handling for unknown options:
      - Removed .allowUnknownOption() from commands to properly detect unknown options
      - Added global error handler in bin/task-master.js for unknown commands/options
      - Added command-specific error handlers with helpful error messages

   3. Improve user experience with better help messages:
      - Added helper functions to display formatted command help on errors
      - Created command-specific help displays for add-subtask and remove-subtask
      - Show available options when encountering unknown options

   4. Update MCP server configuration:
      - Modified .cursor/mcp.json to use node ./mcp-server/server.js directly
      - Removed npx -y usage for more reliable execution

   5. Other minor improvements:
      - Adjusted column width for task ID display in UI
      - Updated version number in package-lock.json to 0.9.30

   This resolves issues where users would see confusing error messages like
   'error: unknown option --generate' when using an incorrect flag."
2025-03-27 13:32:56 -04:00

196 lines
12 KiB
Plaintext

# Task ID: 23
# Title: Complete MCP Server Implementation for Task Master using FastMCP
# Status: in-progress
# Dependencies: 22
# Priority: medium
# Description: Finalize the MCP server functionality for Task Master by leveraging FastMCP's capabilities, transitioning from CLI-based execution to direct function imports, and optimizing performance, authentication, and context management. Ensure the server integrates seamlessly with Cursor via `mcp.json` and supports proper tool registration, efficient context handling, and transport type handling (focusing on stdio). Additionally, ensure the server can be instantiated properly when installed via `npx` or `npm i -g`. Evaluate and address gaps in the current implementation, including function imports, context management, caching, tool registration, and adherence to FastMCP best practices.
# Details:
This task involves completing the Model Context Protocol (MCP) server implementation for Task Master using FastMCP. Key updates include:
1. Transition from CLI-based execution (currently using `child_process.spawnSync`) to direct Task Master function imports for improved performance and reliability.
2. Implement caching mechanisms for frequently accessed contexts to enhance performance, leveraging FastMCP's efficient transport mechanisms (e.g., stdio).
3. Refactor context management to align with best practices for handling large context windows, metadata, and tagging.
4. Refactor tool registration in `tools/index.js` to include clear descriptions and parameter definitions, leveraging FastMCP's decorator-based patterns for better integration.
5. Enhance transport type handling to ensure proper stdio communication and compatibility with FastMCP.
6. Ensure the MCP server can be instantiated and run correctly when installed globally via `npx` or `npm i -g`.
7. Integrate the ModelContextProtocol SDK directly to streamline resource and tool registration, ensuring compatibility with FastMCP's transport mechanisms.
8. Identify and address missing components or functionalities to meet FastMCP best practices, such as robust error handling, monitoring endpoints, and concurrency support.
9. Update documentation to include examples of using the MCP server with FastMCP, detailed setup instructions, and client integration guides.
The implementation must ensure compatibility with existing MCP clients and follow RESTful API design principles, while supporting concurrent requests and maintaining robust error handling.
# Test Strategy:
Testing for the updated MCP server functionality should include:
1. Unit tests:
- Validate direct function imports for Task Master tools, replacing CLI-based execution.
- Test updated authentication and authorization mechanisms.
- Verify context management operations (CRUD, metadata, windowing).
- Test caching mechanisms for frequently accessed contexts.
- Validate proper tool registration with descriptions and parameters.
2. Integration tests:
- Test the MCP server with FastMCP's stdio transport mode.
- Verify end-to-end request/response cycles for each endpoint.
- Ensure compatibility with the ModelContextProtocol SDK.
- Test the tool registration process in `tools/index.js` for correctness and efficiency.
3. Performance tests:
- Benchmark response times for context operations with large datasets.
- Test caching mechanisms and concurrent request handling.
- Measure memory usage and server stability under load.
4. Security tests:
- Validate the robustness of authentication/authorization mechanisms.
- Test for vulnerabilities such as injection attacks, CSRF, and unauthorized access.
5. Deployment tests:
- Verify proper server instantiation and operation when installed via `npx` or `npm i -g`.
- Test configuration loading from `mcp.json`.
6. Documentation validation:
- Ensure all examples in the documentation are accurate and functional.
- Verify manual testing workflows using tools like curl or Postman.
All tests should be automated and integrated into the CI/CD pipeline to ensure consistent quality.
# Subtasks:
## 1. Create Core MCP Server Module and Basic Structure [done]
### Dependencies: None
### Description: Create the foundation for the MCP server implementation by setting up the core module structure, configuration, and server initialization.
### Details:
Implementation steps:
1. Create a new module `mcp-server.js` with the basic server structure
2. Implement configuration options to enable/disable the MCP server
3. Set up Express.js routes for the required MCP endpoints (/context, /models, /execute)
4. Create middleware for request validation and response formatting
5. Implement basic error handling according to MCP specifications
6. Add logging infrastructure for MCP operations
7. Create initialization and shutdown procedures for the MCP server
8. Set up integration with the main Task Master application
Testing approach:
- Unit tests for configuration loading and validation
- Test server initialization and shutdown procedures
- Verify that routes are properly registered
- Test basic error handling with invalid requests
## 2. Implement Context Management System [done]
### Dependencies: 23.1
### Description: Develop a robust context management system that can efficiently store, retrieve, and manipulate context data according to the MCP specification.
### Details:
Implementation steps:
1. Design and implement data structures for context storage
2. Create methods for context creation, retrieval, updating, and deletion
3. Implement context windowing and truncation algorithms for handling size limits
4. Add support for context metadata and tagging
5. Create utilities for context serialization and deserialization
6. Implement efficient indexing for quick context lookups
7. Add support for context versioning and history
8. Develop mechanisms for context persistence (in-memory, disk-based, or database)
Testing approach:
- Unit tests for all context operations (CRUD)
- Performance tests for context retrieval with various sizes
- Test context windowing and truncation with edge cases
- Verify metadata handling and tagging functionality
- Test persistence mechanisms with simulated failures
## 3. Implement MCP Endpoints and API Handlers [done]
### Dependencies: 23.1, 23.2
### Description: Develop the complete API handlers for all required MCP endpoints, ensuring they follow the protocol specification and integrate with the context management system.
### Details:
Implementation steps:
1. Implement the `/context` endpoint for:
- GET: retrieving existing context
- POST: creating new context
- PUT: updating existing context
- DELETE: removing context
2. Implement the `/models` endpoint to list available models
3. Develop the `/execute` endpoint for performing operations with context
4. Create request validators for each endpoint
5. Implement response formatters according to MCP specifications
6. Add detailed error handling for each endpoint
7. Set up proper HTTP status codes for different scenarios
8. Implement pagination for endpoints that return lists
Testing approach:
- Unit tests for each endpoint handler
- Integration tests with mock context data
- Test various request formats and edge cases
- Verify response formats match MCP specifications
- Test error handling with invalid inputs
- Benchmark endpoint performance
## 6. Refactor MCP Server to Leverage ModelContextProtocol SDK [pending]
### Dependencies: 23.1, 23.2, 23.3
### Description: Integrate the ModelContextProtocol SDK directly into the MCP server implementation to streamline tool registration and resource handling.
### Details:
Implementation steps:
1. Replace manual tool registration with ModelContextProtocol SDK methods.
2. Use SDK utilities to simplify resource and template management.
3. Ensure compatibility with FastMCP's transport mechanisms.
4. Update server initialization to include SDK-based configurations.
Testing approach:
- Verify SDK integration with all MCP endpoints.
- Test resource and template registration using SDK methods.
- Validate compatibility with existing MCP clients.
- Benchmark performance improvements from SDK integration.
## 8. Implement Direct Function Imports and Replace CLI-based Execution [pending]
### Dependencies: None
### Description: Refactor the MCP server implementation to use direct Task Master function imports instead of the current CLI-based execution using child_process.spawnSync. This will improve performance, reliability, and enable better error handling.
### Details:
1. Create a new module to import and expose Task Master core functions directly
2. Modify tools/utils.js to remove executeTaskMasterCommand and replace with direct function calls
3. Update each tool implementation (listTasks.js, showTask.js, etc.) to use the direct function imports
4. Implement proper error handling with try/catch blocks and FastMCP's MCPError
5. Add unit tests to verify the function imports work correctly
6. Test performance improvements by comparing response times between CLI and function import approaches
## 9. Implement Context Management and Caching Mechanisms [pending]
### Dependencies: 23.1
### Description: Enhance the MCP server with proper context management and caching to improve performance and user experience, especially for frequently accessed data and contexts.
### Details:
1. Implement a context manager class that leverages FastMCP's Context object
2. Add caching for frequently accessed task data with configurable TTL settings
3. Implement context tagging for better organization of context data
4. Add methods to efficiently handle large context windows
5. Create helper functions for storing and retrieving context data
6. Implement cache invalidation strategies for task updates
7. Add cache statistics for monitoring performance
8. Create unit tests for context management and caching functionality
## 10. Enhance Tool Registration and Resource Management [pending]
### Dependencies: 23.1
### Description: Refactor tool registration to follow FastMCP best practices, using decorators and improving the overall structure. Implement proper resource management for task templates and other shared resources.
### Details:
1. Update registerTaskMasterTools function to use FastMCP's decorator pattern
2. Implement @mcp.tool() decorators for all existing tools
3. Add proper type annotations and documentation for all tools
4. Create resource handlers for task templates using @mcp.resource()
5. Implement resource templates for common task patterns
6. Update the server initialization to properly register all tools and resources
7. Add validation for tool inputs using FastMCP's built-in validation
8. Create comprehensive tests for tool registration and resource access
## 11. Implement Comprehensive Error Handling [pending]
### Dependencies: 23.1, 23.3
### Description: Implement robust error handling using FastMCP's MCPError, including custom error types for different categories and standardized error responses.
### Details:
1. Create custom error types extending MCPError for different categories (validation, auth, etc.)\n2. Implement standardized error responses following MCP protocol\n3. Add error handling middleware for all MCP endpoints\n4. Ensure proper error propagation from tools to client\n5. Add debug mode with detailed error information\n6. Document error types and handling patterns
## 12. Implement Structured Logging System [pending]
### Dependencies: 23.1, 23.3
### Description: Implement a comprehensive logging system for the MCP server with different log levels, structured logging format, and request/response tracking.
### Details:
1. Design structured log format for consistent parsing\n2. Implement different log levels (debug, info, warn, error)\n3. Add request/response logging middleware\n4. Implement correlation IDs for request tracking\n5. Add performance metrics logging\n6. Configure log output destinations (console, file)\n7. Document logging patterns and usage
## 13. Create Testing Framework and Test Suite [pending]
### Dependencies: 23.1, 23.3, 23.8
### Description: Implement a comprehensive testing framework for the MCP server, including unit tests, integration tests, and end-to-end tests.
### Details:
1. Set up Jest testing framework with proper configuration\n2. Create MCPTestClient for testing FastMCP server interaction\n3. Implement unit tests for individual tool functions\n4. Create integration tests for end-to-end request/response cycles\n5. Set up test fixtures and mock data\n6. Implement test coverage reporting\n7. Document testing guidelines and examples