Skip to main content
Glama
tools.pyโ€ข53.5 kB
""" MCP tools implementation for the Codebuddy server. Follows Clean Code and SOLID principles: - Single Responsibility: Each tool has one clear purpose - Open/Closed: Easy to add new tools without modifying existing ones - Interface Segregation: Small, focused tool interfaces - Dependency Injection: Storage dependency injected, not hardcoded Enhanced with cognitive scaffolding based on research: - Miller's 7ยฑ2 rule for working memory limits - Chain-of-thought reasoning patterns - Hierarchical task network (HTN) principles - Metacognitive self-reflection loops - Software engineering best practices """ import logging from datetime import datetime, timedelta from typing import List, Dict, Any from uuid import uuid4 from fastmcp import FastMCP from models import ( Task, TaskStatus, TaskCreateRequest, TaskUpdateRequest, LessonsSummary, CognitiveGuidance, ReflectionPrompts, TaskPlanResponse ) from storage import TaskStorage from error_handling import error_handler, ErrorType, ErrorSeverity # ===== COGNITIVE SCAFFOLDING FRAMEWORKS ===== # Research-based templates for better agentic thinking COGNITIVE_PATTERNS = { "discovery_questions": [ "๐ŸŽฏ What exactly am I trying to achieve? (Clear success criteria)", "๐Ÿ” What constraints do I need to work within? (Time, resources, dependencies)", "๐Ÿง  What assumptions am I making that I should validate?", "โš ๏ธ What could go wrong and how can I mitigate it?", "๐Ÿ“ What's the smallest version that would still be valuable? (MVP thinking)" ], "validation_criteria_templates": [ "โœ… **Success looks like**: Specific, measurable outcomes", "โš ๏ธ **Warning signs**: Red flags that indicate problems", "๐Ÿ”„ **When to pivot**: Conditions that suggest changing approach", "๐Ÿ“Š **How to measure progress**: Concrete checkpoints and metrics" ], "reflection_framework": [ "๐Ÿค” **What worked well?** (Patterns to repeat)", "๐Ÿšง **What was harder than expected?** (Learning opportunities)", "๐Ÿ’ก **What would I do differently?** (Process improvements)", "๐Ÿ“ˆ **What patterns can I reuse?** (Knowledge transfer)" ], "software_engineering_reminders": [ "๐Ÿ—๏ธ **Architecture First**: Design before coding - saves rework later", "๐Ÿงช **Test Early**: Write tests as you go, not at the end", "๐Ÿ“ **Document Decisions**: Record why, not just what you built", "๐Ÿ”„ **Iterate Quickly**: Small working versions beat big perfect plans", "๐Ÿ‘ฅ **Code for Humans**: Your future self will thank you for clarity", "๐Ÿ›ก๏ธ **Error Handling**: Plan for failures from the start", "๐Ÿ“ฆ **Modular Design**: Small, focused components are easier to debug" ], "tool_usage_nudges": [ "๐Ÿ’ก **Remember**: You have access to other tools that might help with this task", "๐Ÿ” **Search Pattern**: Use search tools to find similar past solutions", "๐Ÿ“Š **List & Review**: Check your task list to see what's already in progress", "๐ŸŽฏ **Update Frequently**: Keep task status current for better coordination", "๐Ÿ“š **Learn from History**: Review lessons from completed tasks" ] } # Hierarchical step templates following 7ยฑ2 rule (max 6 core phases) COGNITIVE_STEP_TEMPLATES = { "generic_project": [ "### ๐ŸŽฏ **Clarify & Scope** | *Why: Prevents scope creep* | *Check: Clear success criteria written*", "### ๐Ÿ” **Analyze & Design** | *Why: Reduces implementation complexity* | *Check: Architecture decisions documented*", "### ๐Ÿ› ๏ธ **Setup & Foundation** | *Why: Solid base prevents rework* | *Check: Environment ready and tested*", "### ๐Ÿ“‹ **Implement Core Features** | *Why: Deliver value incrementally* | *Check: Each feature fully tested*", "### โœ… **Validate & Test** | *Why: Catch issues early* | *Check: All acceptance criteria met*", "### ๐Ÿ”„ **Review & Iterate** | *Why: Continuous improvement* | *Check: Lessons captured for future use*" ], "debugging_workflow": [ "### ๐Ÿ” **Reproduce the Issue** | *Why: Understand the problem fully* | *Check: Consistent reproduction steps*", "### ๐Ÿงช **Isolate Variables** | *Why: Find root cause faster* | *Check: Minimum failing example created*", "### ๐Ÿ’ก **Hypothesis Formation** | *Why: Structured investigation* | *Check: Testable theories documented*", "### ๐Ÿ› ๏ธ **Test & Fix** | *Why: Verify solutions work* | *Check: Fix addresses root cause*", "### โœ… **Validate Resolution** | *Why: Ensure no regressions* | *Check: All test cases pass*" ], "research_learning": [ "### ๐Ÿ“š **Define Learning Goals** | *Why: Focused effort* | *Check: Clear questions to answer*", "### ๐Ÿ” **Gather Information** | *Why: Broad understanding* | *Check: Multiple sources consulted*", "### ๐Ÿง  **Synthesize Knowledge** | *Why: Connect concepts* | *Check: Can explain to others*", "### ๐Ÿ› ๏ธ **Apply & Experiment** | *Why: Learning by doing* | *Check: Working examples created*", "### ๐Ÿ“ **Document Insights** | *Why: Knowledge retention* | *Check: Key learnings captured*" ] } logger = logging.getLogger(__name__) class TaskService: """ Service layer for task operations. Follows SRP: Handles business logic for task management. Separates concerns between MCP tools and storage operations. """ def __init__(self, storage: TaskStorage): """Inject storage dependency following Dependency Inversion Principle.""" self.storage = storage def generate_task_steps(self, problem: str) -> List[str]: """ Generate cognitively scaffolded steps for a problem. Uses research-based patterns and respects 7ยฑ2 rule for working memory. Provides structured thinking support for agentic users. """ problem_lower = problem.lower() # Select appropriate cognitive template based on problem type if any(word in problem_lower for word in ['debug', 'fix', 'bug', 'error', 'issue', 'problem']): return COGNITIVE_STEP_TEMPLATES["debugging_workflow"] elif any(word in problem_lower for word in ['learn', 'research', 'study', 'understand', 'explore']): return COGNITIVE_STEP_TEMPLATES["research_learning"] else: return COGNITIVE_STEP_TEMPLATES["generic_project"] def assess_complexity(self, problem: str, steps: List[str]) -> str: """ Assess cognitive complexity of a task for load management. Simple heuristic based on problem characteristics. """ complexity_indicators = 0 problem_lower = problem.lower() # Complexity factors if len(problem.split()) > 20: # Long descriptions often indicate complexity complexity_indicators += 1 if any(word in problem_lower for word in ['integrate', 'architecture', 'system', 'complex', 'multiple']): complexity_indicators += 2 if any(word in problem_lower for word in ['new', 'unfamiliar', 'learning', 'research']): complexity_indicators += 1 if len(steps) > 6: # More than 6 steps may overwhelm working memory complexity_indicators += 1 if complexity_indicators >= 3: return "complex" elif complexity_indicators >= 1: return "moderate" else: return "simple" def generate_cognitive_guidance(self, problem: str, complexity: str) -> CognitiveGuidance: """Generate metacognitive scaffolding based on task characteristics.""" complexity_guidance = { "simple": "๐Ÿง  **Cognitive Load**: Light - should feel straightforward. If it feels overwhelming, break down further.", "moderate": "๐Ÿง  **Cognitive Load**: Moderate - manageable in focused sessions. Take breaks between phases.", "complex": "๐Ÿง  **Cognitive Load**: High - consider splitting into multiple tasks. Use the 7ยฑ2 rule for each phase." } return CognitiveGuidance( complexity_check=complexity_guidance.get(complexity, complexity_guidance["moderate"]), progress_monitor="๐Ÿ“Š **Self-Check**: After each step, ask: 'Am I closer to my goal?' and 'What did I learn?'", adaptation_prompt="๐Ÿ”„ **If Stuck**: Step back, reassess scope, or break current step into smaller pieces. Consider using other available tools.", success_pattern="โœ… **Success Signals**: Each step feels achievable, you're building momentum, and can explain progress clearly.", tool_suggestions=[ "๐Ÿ’ก Use `search_tasks` to find similar past solutions and patterns", "๐Ÿ“‹ Check `list_tasks` to see what else is in progress and avoid conflicts", "๐ŸŽฏ Use `update_task` frequently to track progress and insights", "๐Ÿ“š Try `summarize_lessons` to learn from completed tasks" ] ) def generate_reflection_prompts(self, problem: str) -> ReflectionPrompts: """Generate metacognitive reflection questions.""" return ReflectionPrompts( assumption_check="๐Ÿค” What assumptions am I making about this problem that I should validate first?", simplification_prompt="๐Ÿ“ What's the smallest version of this that would still deliver value? (MVP thinking)", explanation_test="๐Ÿ‘ฅ If I had to explain this approach to a colleague, what would I say? (Clarity check)" ) def analyze_lessons_from_tasks(self, tasks: List[Task]) -> LessonsSummary: """ Analyze patterns in completed and blocked tasks. Extracts insights for improving future task execution. """ completed_tasks = [t for t in tasks if t.status == TaskStatus.COMPLETED] blocked_tasks = [t for t in tasks if t.status == TaskStatus.BLOCKED] # Analyze success patterns success_patterns = [] if completed_tasks: # Simple keyword analysis for successful tasks common_success_words = self._extract_common_keywords( [f"{t.problem} {t.notes}" for t in completed_tasks] ) for word in common_success_words[:3]: # Top 3 patterns success_patterns.append(f"Tasks mentioning '{word}' tend to succeed") # Analyze common blockers common_blockers = [] if blocked_tasks: blocker_keywords = self._extract_common_keywords( [t.notes for t in blocked_tasks if t.notes] ) for word in blocker_keywords[:3]: # Top 3 blockers common_blockers.append(f"'{word}' is a common blocking issue") # Generate recommendations recommendations = [] if len(completed_tasks) > 0: avg_steps = sum(len(t.steps) for t in completed_tasks) / len(completed_tasks) if avg_steps < 3: recommendations.append("Consider breaking problems into more detailed steps") elif avg_steps > 8: recommendations.append("Try to simplify tasks with fewer, broader steps") if len(blocked_tasks) / max(len(tasks), 1) > 0.3: recommendations.append("High blocking rate - consider smaller, incremental tasks") if not recommendations: recommendations.append("Continue current approach - good task completion patterns") return LessonsSummary( success_patterns=success_patterns, common_blockers=common_blockers, recommendations=recommendations, tasks_analyzed=len(tasks) ) def _extract_common_keywords(self, texts: List[str]) -> List[str]: """ Simple keyword extraction from text list. Returns most frequent meaningful words. """ if not texts: return [] # Simple word frequency analysis word_counts = {} stop_words = {'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with', 'by', 'is', 'are', 'was', 'were', 'be', 'been', 'have', 'has', 'had', 'will', 'would', 'could', 'should'} for text in texts: words = text.lower().split() for word in words: word = word.strip('.,!?;:"()[]{}') if len(word) > 2 and word not in stop_words: word_counts[word] = word_counts.get(word, 0) + 1 # Return top words by frequency return sorted(word_counts.keys(), key=lambda w: word_counts[w], reverse=True) # === HELPER FUNCTIONS FOR COGNITIVE SCAFFOLDING === def _get_status_transition_guidance(old_status: TaskStatus, new_status: TaskStatus) -> str: """Provide guidance for status transitions.""" transitions = { (TaskStatus.PLANNED, TaskStatus.IN_PROGRESS): "๐Ÿš€ **Starting work** - Focus on the first step, ignore the rest for now", (TaskStatus.IN_PROGRESS, TaskStatus.COMPLETED): "๐ŸŽ‰ **Task completed** - Great work! Capture what you learned", (TaskStatus.IN_PROGRESS, TaskStatus.BLOCKED): "๐Ÿšง **Encountered blocker** - Step back, reassess, consider alternative approaches", (TaskStatus.BLOCKED, TaskStatus.IN_PROGRESS): "โœ… **Blocker resolved** - Resume with fresh perspective and new insights", (TaskStatus.PLANNED, TaskStatus.BLOCKED): "โš ๏ธ **Blocked before starting** - May need more clarity or different approach", (TaskStatus.COMPLETED, TaskStatus.IN_PROGRESS): "๐Ÿ”„ **Reopening task** - New requirements or issues discovered" } key = (old_status, new_status) return transitions.get(key, f"๐Ÿ“ Status changed from {old_status.value} to {new_status.value}") def _generate_progress_feedback(old_status: TaskStatus, new_status: TaskStatus) -> str: """Generate encouraging progress feedback.""" if new_status == TaskStatus.COMPLETED: return "๐ŸŽฏ **Excellent!** Task completion builds momentum and confidence" elif old_status == TaskStatus.PLANNED and new_status == TaskStatus.IN_PROGRESS: return "โšก **Momentum started!** The hardest part is often just beginning" elif old_status == TaskStatus.BLOCKED and new_status == TaskStatus.IN_PROGRESS: return "๐Ÿ’ช **Breakthrough achieved!** Overcoming blocks builds problem-solving skills" else: return "๐Ÿ“ˆ **Progress tracked** - Every update helps build better working habits" def _get_next_action_suggestions(status: TaskStatus, task: Task) -> List[str]: """Suggest next actions based on current status.""" if status == TaskStatus.PLANNED: return [ "๐Ÿ“‹ Review the task steps and success criteria", "๐ŸŽฏ Mark as 'in_progress' when you start the first step", "๐Ÿ” Search for similar past tasks for insights", "๐Ÿ“ Consider breaking down complex steps further" ] elif status == TaskStatus.IN_PROGRESS: return [ "๐ŸŽฏ Focus on completing the current step fully", "๐Ÿ“Š Update notes with progress and insights", "โš ๏ธ Mark as 'blocked' if you encounter obstacles", "โœ… Mark as 'completed' when all steps are done" ] elif status == TaskStatus.COMPLETED: return [ "๐Ÿ“š Use `summarize_lessons` to extract insights from completed tasks", "๐Ÿ” Look for patterns in your successful approaches", "๐ŸŽฏ Plan any follow-up tasks that emerged from this work", "๐Ÿ“ Document key learnings while they're fresh" ] elif status == TaskStatus.BLOCKED: return [ "๐Ÿค” Break down the blocker into smaller, specific issues", "๐Ÿ” Search for similar problems you've solved before", "๐Ÿ’ก Consider alternative approaches or simpler versions", "๐Ÿ‘ฅ Reach out for help or fresh perspective if needed" ] else: return ["๐Ÿ“‹ Review task status and plan next steps"] def _get_reflection_for_status(status: TaskStatus) -> str: """Get appropriate reflection question for the status.""" reflections = { TaskStatus.PLANNED: "๐Ÿค” **Before starting**: Am I clear on what success looks like for this task?", TaskStatus.IN_PROGRESS: "๐Ÿ“Š **Progress check**: What's working well, and what needs adjustment?", TaskStatus.COMPLETED: "๐ŸŽฏ **Looking back**: What did I learn that I can apply to future tasks?", TaskStatus.BLOCKED: "๐Ÿšง **Problem solving**: What's the real blocker here, and what are my options?" } return reflections.get(status, "๐Ÿค” What insights can I capture about this task?") def _get_se_reminders_for_status(status: TaskStatus) -> List[str]: """Get software engineering reminders based on task status.""" if status == TaskStatus.IN_PROGRESS: return [ "๐Ÿ’พ **Commit frequently** - Save your progress with descriptive messages", "๐Ÿงช **Test as you go** - Don't leave testing until the end", "๐Ÿ“ **Document decisions** - Record why you chose specific approaches" ] elif status == TaskStatus.COMPLETED: return [ "๐Ÿ” **Code review** - Review your own code before marking truly complete", "๐Ÿ“š **Knowledge sharing** - Document lessons for your future self", "๐Ÿ›ก๏ธ **Security check** - Ensure no sensitive data or vulnerabilities introduced" ] elif status == TaskStatus.BLOCKED: return [ "๐Ÿ”ฌ **Scientific debugging** - Form hypotheses and test systematically", "๐Ÿ“‹ **Reproduce the issue** - Create minimal failing examples", "๐Ÿค **Seek help effectively** - Prepare clear problem descriptions" ] else: return [ "๐Ÿ—๏ธ **Plan before coding** - Design interfaces and architecture first", "๐Ÿ“Š **Measure twice, cut once** - Validate requirements before implementation" ] def setup_mcp_tools(mcp: FastMCP, task_service: TaskService) -> None: """ Register all MCP tools with the FastMCP server. Follows Open/Closed Principle: Easy to add new tools here. Each tool is a small, focused function following SRP. """ @mcp.tool() def plan_task(problem: str) -> dict: """ ๐Ÿง  **Cognitive Project Planning Tool** **Purpose**: Break down complex problems using proven cognitive scaffolding principles **How it helps your thinking**: - Applies the 7ยฑ2 rule to prevent cognitive overload - Uses hierarchical decomposition to manage complexity - Includes self-reflection prompts for adaptive thinking - Provides clear validation criteria for each step - Incorporates software engineering best practices **Best used when**: You have a complex goal and need structured thinking support **Cognitive pattern**: Discovery โ†’ Planning โ†’ Execution โ†’ Validation โ†’ Reflection Args: problem: The problem description to plan steps for Returns: Dictionary with enhanced cognitive scaffolding and structured guidance """ try: # Validate input if not problem or not problem.strip(): raise ValueError("Problem description cannot be empty") problem_clean = problem.strip() # === COGNITIVE SCAFFOLDING GENERATION === # Apply research-based thinking frameworks # 1. Generate cognitively structured steps (respects 7ยฑ2 rule) steps = task_service.generate_task_steps(problem_clean) # 2. Assess complexity for cognitive load management complexity = task_service.assess_complexity(problem_clean, steps) # 3. Generate metacognitive guidance cognitive_guidance = task_service.generate_cognitive_guidance(problem_clean, complexity) # 4. Create reflection prompts for self-monitoring reflection_prompts = task_service.generate_reflection_prompts(problem_clean) # 5. Select relevant software engineering practices se_practices = [] problem_lower = problem_clean.lower() if any(word in problem_lower for word in ['code', 'implement', 'build', 'develop']): se_practices.extend([ "๐Ÿ—๏ธ **Start with Architecture**: Design interfaces before implementation", "๐Ÿงช **Test-Driven**: Write tests first to clarify requirements", "๐Ÿ“ **Document Decisions**: Record architectural choices and trade-offs" ]) if any(word in problem_lower for word in ['debug', 'fix', 'error']): se_practices.extend([ "๐Ÿ”ฌ **Scientific Method**: Form hypotheses, test systematically", "๐Ÿ“‹ **Reproduce First**: Consistent reproduction before fixing", "๐Ÿ›ก๏ธ **Regression Prevention**: Ensure fix doesn't break other things" ]) if any(word in problem_lower for word in ['refactor', 'improve', 'optimize']): se_practices.extend([ "๐Ÿ“Š **Measure First**: Baseline performance before optimizing", "๐ŸŽฏ **Target Bottlenecks**: Profile to find actual slow points", "โœ… **Maintain Behavior**: Keep existing functionality intact" ]) # Default practices for all projects se_practices.extend([ "๐Ÿ”„ **Version Control**: Commit frequently with clear messages", "๐Ÿ‘ฅ **Code Reviews**: Fresh eyes catch issues you miss", "๐Ÿ“š **Learn Continuously**: Each problem teaches something new" ]) # Create enhanced task with cognitive fields task = Task( problem=problem_clean, steps=steps, status=TaskStatus.PLANNED, complexity_level=complexity, success_criteria=[ "All steps completed with validation checks passed", "Solution meets original problem requirements", "Key learnings documented for future reference" ], dependencies=[], # Can be filled in by user risk_factors=[ f"Complexity level: {complexity} - monitor cognitive load", "Scope creep - stick to defined success criteria", "Technical debt - balance speed vs. maintainability" ] ) saved_task = task_service.storage.save_task(task) logger.info(f"Created cognitively scaffolded task {saved_task.id} with complexity: {complexity}") # === ENHANCED RESPONSE WITH COGNITIVE SCAFFOLDING === return { "task_id": str(saved_task.id), "cognitive_assessment": { "complexity_level": complexity, "working_memory_load": f"{len(steps)} phases (within 7ยฑ2 limit)" if len(steps) <= 7 else f"{len(steps)} phases (consider breaking down further)", "planning_rationale": "Using hierarchical task network (HTN) principles for structured decomposition", "estimated_cognitive_effort": "Low" if complexity == "simple" else "Moderate" if complexity == "moderate" else "High" }, "hierarchical_steps": [ { "step_number": i + 1, "description": step, "cognitive_support": "Focus on this step only - don't worry about later phases yet", "validation_check": "Step complete when you can clearly explain what you accomplished" } for i, step in enumerate(steps) ], "cognitive_guidance": { "complexity_check": cognitive_guidance.complexity_check, "progress_monitor": cognitive_guidance.progress_monitor, "adaptation_prompt": cognitive_guidance.adaptation_prompt, "success_pattern": cognitive_guidance.success_pattern, "tool_suggestions": cognitive_guidance.tool_suggestions }, "reflection_prompts": { "assumption_check": reflection_prompts.assumption_check, "simplification_prompt": reflection_prompts.simplification_prompt, "explanation_test": reflection_prompts.explanation_test }, "software_engineering_guidance": se_practices, "discovery_questions": COGNITIVE_PATTERNS["discovery_questions"], "metacognitive_notes": [ f"๐Ÿ“Š This task breakdown follows cognitive load theory - {len(steps)} steps respects working memory limits", "๐Ÿ”„ Each step builds on the previous one - complete sequentially for best results", "๐Ÿง  If any step feels overwhelming, break it down further using the same principles", "๐Ÿ’ก Remember: You have other tools available - use them to support this task" ], "next_recommended_actions": [ "๐ŸŽฏ Review the discovery questions to clarify requirements", "๐Ÿ“‹ Use `list_tasks` to see how this fits with your other work", "๐Ÿ” Try `search_tasks` to find similar past solutions", "โ–ถ๏ธ When ready, use `update_task` to mark as 'in_progress'" ] } except ValueError as e: error_handler.handle_error(e, ErrorType.VALIDATION, ErrorSeverity.LOW) raise except Exception as e: error_handler.handle_error(e, ErrorType.INTERNAL, ErrorSeverity.MEDIUM) raise @mcp.tool() def update_task(task_id: str, status: str, notes: str = "") -> dict: """ ๐ŸŽฏ **Task Progress Tracking Tool** **Purpose**: Update task status while maintaining cognitive awareness and learning capture **Cognitive Benefits**: - Tracks progress for better self-monitoring - Captures insights for pattern recognition - Provides transition guidance for status changes - Suggests next actions based on current state **Best Practice**: Update frequently to maintain momentum and capture learnings Args: task_id: The ID of the task to update status: New status (planned, in_progress, completed, blocked) notes: Optional notes about the update (encouraged for learning capture) Returns: Dictionary with updated task information and cognitive guidance """ try: # Validate inputs if not task_id or not task_id.strip(): raise ValueError("Task ID cannot be empty") # Validate status try: new_status = TaskStatus(status.lower()) except ValueError: valid_statuses = [s.value for s in TaskStatus] raise ValueError(f"Invalid status '{status}'. Must be one of: {valid_statuses}") # Get existing task existing_task = task_service.storage.get_task_by_id(task_id.strip()) if not existing_task: raise ValueError(f"Task with ID '{task_id}' not found") # Update task with enhanced notes if learning insights provided enhanced_notes = notes.strip() if enhanced_notes and new_status == TaskStatus.COMPLETED: if not any(keyword in enhanced_notes.lower() for keyword in ['learned', 'insight', 'discovered', 'found']): enhanced_notes += " | ๐Ÿ’ก Consider adding: What did you learn? What would you do differently?" updated_task = existing_task.update_progress(new_status, enhanced_notes) saved_task = task_service.storage.save_task(updated_task) logger.info(f"Updated task {saved_task.id} to status {saved_task.status}") # === COGNITIVE GUIDANCE BASED ON STATUS TRANSITION === transition_guidance = _get_status_transition_guidance(existing_task.status, new_status) next_actions = _get_next_action_suggestions(new_status, saved_task) return { "id": str(saved_task.id), "problem": saved_task.problem, "status": saved_task.status.value, "previous_status": existing_task.status.value, "notes": saved_task.notes, "updated_at": saved_task.updated_at.isoformat(), "cognitive_feedback": { "status_transition": transition_guidance, "progress_acknowledgment": _generate_progress_feedback(existing_task.status, new_status), "momentum_check": "๐Ÿ”„ Regular updates build momentum and help track patterns", "learning_capture": "๐Ÿ“ Document insights while they're fresh - your future self will thank you" }, "next_recommended_actions": next_actions, "reflection_prompt": _get_reflection_for_status(new_status), "tool_suggestions": [ "๐Ÿ“‹ Use `list_tasks` to see your overall progress across all tasks", "๐Ÿ” Try `search_tasks` to find related work or similar past solutions", "๐Ÿ“š Use `summarize_lessons` to extract patterns from your completed tasks", "๐ŸŽฏ Consider `plan_task` for any new work that emerged from this update" ], "software_engineering_reminders": _get_se_reminders_for_status(new_status) } except ValueError as e: error_handler.handle_error(e, ErrorType.VALIDATION, ErrorSeverity.LOW) raise except Exception as e: error_handler.handle_error(e, ErrorType.INTERNAL, ErrorSeverity.MEDIUM) raise @mcp.tool() def list_tasks(limit: int = 10) -> dict: """ ๐Ÿ“‹ **Task Overview & Coordination Tool** **Purpose**: Get strategic overview of your work to maintain cognitive coordination **Cognitive Benefits**: - Prevents task conflicts and duplicate effort - Shows progress patterns across multiple projects - Helps prioritize and manage cognitive load - Identifies completion opportunities **Best Practice**: Check regularly to maintain big-picture awareness Args: limit: Maximum number of tasks to return (default: 10, max: 100) Returns: Dictionary with tasks list, cognitive insights, and strategic guidance """ try: # Validate limit if limit < 1: limit = 10 elif limit > 100: limit = 100 # Prevent excessive memory usage tasks = task_service.storage.list_tasks(limit) total_count = task_service.storage.get_total_count() # === COGNITIVE ANALYSIS OF TASK PORTFOLIO === status_distribution = {} complexity_distribution = {} recent_activity = 0 tasks_data = [] for task in tasks: # Count status distribution status = task.status.value status_distribution[status] = status_distribution.get(status, 0) + 1 # Count complexity distribution if task.complexity_level: complexity_distribution[task.complexity_level] = complexity_distribution.get(task.complexity_level, 0) + 1 # Check for recent activity (updated in last 7 days) if (datetime.utcnow() - task.updated_at) < timedelta(days=7): recent_activity += 1 tasks_data.append({ "id": str(task.id), "problem": task.problem, "steps": task.steps, "status": task.status.value, "notes": task.notes, "complexity_level": task.complexity_level, "created_at": task.created_at.isoformat(), "updated_at": task.updated_at.isoformat(), "step_progress": f"{len([s for s in task.steps if 'โœ…' in s or 'done' in s.lower()])}/{len(task.steps)}" if task.steps else "0/0" }) # === COGNITIVE PORTFOLIO INSIGHTS === portfolio_insights = [] in_progress_count = status_distribution.get('in_progress', 0) if in_progress_count > 3: portfolio_insights.append("โš ๏ธ **High parallel work**: Consider focusing on fewer tasks to reduce cognitive switching costs") elif in_progress_count == 0 and status_distribution.get('planned', 0) > 0: portfolio_insights.append("๐Ÿš€ **Ready to start**: Pick your highest priority planned task and begin") if status_distribution.get('blocked', 0) > 0: portfolio_insights.append("๐Ÿšง **Blocked tasks detected**: Address blockers before starting new work") completion_rate = status_distribution.get('completed', 0) / max(len(tasks_data), 1) if completion_rate > 0.7: portfolio_insights.append("๐ŸŽฏ **High completion rate**: Great execution pattern - keep it up!") elif completion_rate < 0.3: portfolio_insights.append("๐Ÿ’ก **Low completion rate**: Consider breaking tasks smaller or addressing common blockers") if recent_activity < len(tasks_data) * 0.3: portfolio_insights.append("๐Ÿ“Š **Stale tasks detected**: Update task statuses to maintain momentum and accurate tracking") return { "tasks": tasks_data, "returned_count": len(tasks_data), "total_count": total_count, "cognitive_portfolio_analysis": { "status_distribution": status_distribution, "complexity_distribution": complexity_distribution, "recent_activity_count": recent_activity, "cognitive_load_assessment": "Light" if in_progress_count <= 2 else "Moderate" if in_progress_count <= 4 else "High", "completion_rate_percentage": round(completion_rate * 100, 1) }, "strategic_insights": portfolio_insights, "recommended_actions": [ "๐ŸŽฏ Focus on completing in-progress tasks before starting new ones", "๐Ÿšง Address any blocked tasks - they consume mental energy even when ignored", "๐Ÿ“Š Update stale tasks to maintain accurate progress tracking", "๐Ÿ” Use `search_tasks` to find related work and avoid duplicates", "๐Ÿ“š Consider `summarize_lessons` to extract patterns from completed tasks" ], "cognitive_guidance": { "focus_recommendation": "Work on 1-2 tasks at a time for optimal cognitive efficiency", "prioritization_tip": "Choose tasks that build momentum - mix quick wins with important work", "completion_strategy": "Small, frequent completions are better than large, delayed ones", "learning_opportunity": "Each completed task teaches something - capture those insights" }, "tool_integration_suggestions": [ "๐Ÿ’ก Use `plan_task` for any new work that needs to be added", "๐ŸŽฏ Use `update_task` to keep progress current and capture insights", "๐Ÿ” Use `search_tasks` to find connections between tasks", "๐Ÿ“š Use `summarize_lessons` after completing several tasks" ] } except Exception as e: error_handler.handle_error(e, ErrorType.INTERNAL, ErrorSeverity.MEDIUM) raise @mcp.tool() def search_tasks(query: str) -> dict: """ ๐Ÿ” **Pattern Recognition & Knowledge Discovery Tool** **Purpose**: Find connections between past and current work for better decision-making **Cognitive Benefits**: - Leverages past solutions to solve current problems - Identifies successful patterns and approaches - Prevents reinventing the wheel - Builds on accumulated knowledge and experience **Best Practice**: Search before starting new work - you may have solved similar problems Args: query: Search query string (keywords related to your current problem) Returns: Dictionary with matching tasks, insights, and pattern analysis """ try: if not query or not query.strip(): raise ValueError("Search query cannot be empty") tasks = task_service.storage.search_tasks(query.strip()) # === COGNITIVE PATTERN ANALYSIS === completed_matches = [t for t in tasks if t.status == TaskStatus.COMPLETED] blocked_matches = [t for t in tasks if t.status == TaskStatus.BLOCKED] in_progress_matches = [t for t in tasks if t.status == TaskStatus.IN_PROGRESS] tasks_data = [] success_patterns = [] warning_patterns = [] for task in tasks: task_data = { "id": str(task.id), "problem": task.problem, "steps": task.steps, "status": task.status.value, "notes": task.notes, "complexity_level": task.complexity_level, "created_at": task.created_at.isoformat(), "updated_at": task.updated_at.isoformat(), "relevance_score": "high" if query.lower() in task.problem.lower() else "medium" } # Extract insights from completed tasks if task.status == TaskStatus.COMPLETED and task.notes: if any(word in task.notes.lower() for word in ['worked', 'success', 'solved', 'effective']): success_patterns.append(f"โœ… From '{task.problem[:50]}...': {task.notes[:100]}...") # Extract warnings from blocked tasks if task.status == TaskStatus.BLOCKED and task.notes: warning_patterns.append(f"โš ๏ธ Blocker in '{task.problem[:50]}...': {task.notes[:100]}...") tasks_data.append(task_data) # === STRATEGIC INSIGHTS GENERATION === cognitive_insights = [] if completed_matches: cognitive_insights.append(f"๐ŸŽฏ **{len(completed_matches)} similar completed tasks found** - review their approaches") if blocked_matches: cognitive_insights.append(f"๐Ÿšง **{len(blocked_matches)} similar blocked tasks found** - learn from their challenges") if in_progress_matches: cognitive_insights.append(f"๐Ÿ”„ **{len(in_progress_matches)} similar ongoing tasks** - consider coordination or consolidation") if not tasks: cognitive_insights.append("๐Ÿ’ก **No similar tasks found** - you're exploring new territory! Document your approach well.") return { "query": query.strip(), "tasks": tasks_data, "results_count": len(tasks_data), "cognitive_insights": cognitive_insights, "pattern_analysis": { "completed_similar": len(completed_matches), "blocked_similar": len(blocked_matches), "in_progress_similar": len(in_progress_matches), "success_rate": round(len(completed_matches) / max(len(tasks), 1) * 100, 1), "novelty_indicator": "High" if len(tasks) == 0 else "Medium" if len(tasks) < 3 else "Low" }, "extracted_success_patterns": success_patterns[:3], # Top 3 "extracted_warning_patterns": warning_patterns[:3], # Top 3 "strategic_recommendations": [ "๐Ÿ“š **Learn from successes**: Study approaches that worked in completed similar tasks", "๐Ÿšง **Avoid known pitfalls**: Review blockers from similar past work", "๐Ÿ”„ **Consider coordination**: If similar work is in progress, explore collaboration", "๐Ÿ“ **Document your approach**: Help future searches by capturing your methods and insights" ], "next_actions": [ "๐Ÿ“‹ Review the most relevant matches for applicable strategies", "๐ŸŽฏ Use `plan_task` to create a new approach incorporating these insights", "๐Ÿ“Š Use `update_task` to capture learnings as you apply these patterns", "๐Ÿ‘ฅ Consider reaching out if similar work is in progress elsewhere" ], "cognitive_guidance": { "pattern_recognition": "Look for recurring themes in successful approaches", "failure_analysis": "Understand why similar tasks got blocked - different context or systemic issues?", "knowledge_building": "Each search builds your problem-solving knowledge base", "transfer_learning": "Adapt successful patterns to your current context rather than copying exactly" } } except ValueError as e: error_handler.handle_error(e, ErrorType.VALIDATION, ErrorSeverity.LOW) raise except Exception as e: error_handler.handle_error(e, ErrorType.INTERNAL, ErrorSeverity.MEDIUM) raise @mcp.tool() def summarize_lessons() -> dict: """ ๐Ÿ“š **Meta-Learning & Pattern Extraction Tool** **Purpose**: Transform your task history into actionable wisdom for better future performance **Cognitive Benefits**: - Builds metacognitive awareness of your working patterns - Identifies successful strategies to repeat - Reveals systemic issues before they become major problems - Accelerates learning through pattern recognition **Best Practice**: Review lessons periodically to compound your problem-solving effectiveness Returns: Dictionary with deep insights, patterns, and strategic recommendations for improvement """ try: # Analyze recent tasks (last 50 for meaningful patterns) recent_tasks = task_service.storage.list_tasks(limit=50) if not recent_tasks: return { "message": "No tasks found to analyze - start by creating and completing some tasks!", "next_steps": [ "๐ŸŽฏ Use `plan_task` to create your first structured task", "๐Ÿ“Š Use `update_task` to track progress and capture insights", "๐Ÿ”„ Return here after completing a few tasks for pattern analysis" ], "tasks_analyzed": 0 } # === DEEP COGNITIVE ANALYSIS === completed_tasks = [t for t in recent_tasks if t.status == TaskStatus.COMPLETED] blocked_tasks = [t for t in recent_tasks if t.status == TaskStatus.BLOCKED] in_progress_tasks = [t for t in recent_tasks if t.status == TaskStatus.IN_PROGRESS] # Analyze completion patterns completion_insights = [] success_strategies = [] failure_patterns = [] cognitive_patterns = [] # Success pattern analysis if completed_tasks: avg_completion_time = sum((t.updated_at - t.created_at).days for t in completed_tasks) / len(completed_tasks) completion_insights.append(f"โฑ๏ธ **Average completion time**: {avg_completion_time:.1f} days") # Analyze successful complexity management complex_completed = [t for t in completed_tasks if t.complexity_level == 'complex'] if complex_completed: success_strategies.append(f"๐Ÿง  **Complex task success**: You've completed {len(complex_completed)} complex tasks - you can handle challenging work!") # Look for successful step patterns successful_step_counts = [len(t.steps) for t in completed_tasks if t.steps] if successful_step_counts: avg_successful_steps = sum(successful_step_counts) / len(successful_step_counts) success_strategies.append(f"๐Ÿ“‹ **Optimal breakdown**: Tasks with ~{avg_successful_steps:.0f} steps tend to get completed") # Failure pattern analysis if blocked_tasks: # Analyze what causes blocks blocked_with_notes = [t for t in blocked_tasks if t.notes] if blocked_with_notes: common_block_words = [] for task in blocked_with_notes: words = task.notes.lower().split() common_block_words.extend([w for w in words if len(w) > 4]) if common_block_words: # Simple frequency analysis word_freq = {} for word in common_block_words: word_freq[word] = word_freq.get(word, 0) + 1 top_block_causes = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)[:3] for word, freq in top_block_causes: if freq > 1: failure_patterns.append(f"๐Ÿšง **Recurring blocker**: '{word}' appears in {freq} blocked tasks") # Analyze complexity vs blocking complex_blocked = [t for t in blocked_tasks if t.complexity_level == 'complex'] if len(complex_blocked) / max(len(blocked_tasks), 1) > 0.7: failure_patterns.append("โš ๏ธ **Complexity correlation**: Most blocks occur in complex tasks - consider smaller breakdowns") # Progress pattern analysis stalled_tasks = [t for t in in_progress_tasks if (datetime.utcnow() - t.updated_at).days > 7] if stalled_tasks: cognitive_patterns.append(f"๐Ÿ“Š **Attention pattern**: {len(stalled_tasks)} tasks haven't been updated in >7 days - may need attention") # Cognitive load analysis current_cognitive_load = len(in_progress_tasks) if current_cognitive_load > 5: cognitive_patterns.append("๐Ÿง  **High cognitive load**: Multiple active tasks may be reducing effectiveness") elif current_cognitive_load == 0 and len(recent_tasks) > 5: cognitive_patterns.append("๐ŸŽฏ **Ready for action**: No active tasks - good time to start something new") # Generate strategic recommendations strategic_recommendations = [] if completed_tasks and blocked_tasks: completion_rate = len(completed_tasks) / (len(completed_tasks) + len(blocked_tasks)) if completion_rate > 0.8: strategic_recommendations.append("๐ŸŽฏ **Excellent execution**: High completion rate - maintain current approach") elif completion_rate < 0.5: strategic_recommendations.append("๐Ÿ’ก **Focus on completion**: Consider smaller tasks or better blocker management") if failure_patterns: strategic_recommendations.append("๐Ÿ” **Pattern detected**: Address recurring blockers systematically - they're costing you momentum") if not success_strategies: strategic_recommendations.append("๐Ÿ“ **Capture insights**: Add more detailed notes to completed tasks to identify success patterns") strategic_recommendations.extend([ "๐Ÿ”„ **Continuous improvement**: Apply these insights to your next task planning", "๐Ÿ“Š **Track progress**: Regular updates help identify patterns and maintain momentum", "๐Ÿง  **Cognitive awareness**: Balance challenging and routine work to optimize performance" ]) return { "analysis_summary": { "tasks_analyzed": len(recent_tasks), "completion_rate_percentage": round(len(completed_tasks) / max(len(recent_tasks), 1) * 100, 1), "current_active_tasks": len(in_progress_tasks), "cognitive_load_assessment": "Light" if current_cognitive_load <= 2 else "Moderate" if current_cognitive_load <= 4 else "High" }, "success_patterns": success_strategies or ["๐ŸŽฏ Complete more tasks with detailed notes to identify success patterns"], "failure_patterns": failure_patterns or ["โœ… No significant blocking patterns detected - good execution!"], "cognitive_insights": cognitive_patterns or ["๐Ÿ“Š Healthy task management patterns - keep it up!"], "strategic_recommendations": strategic_recommendations, "performance_metrics": { "completed_tasks": len(completed_tasks), "blocked_tasks": len(blocked_tasks), "in_progress_tasks": len(in_progress_tasks), "average_completion_days": round(sum((t.updated_at - t.created_at).days for t in completed_tasks) / max(len(completed_tasks), 1), 1) if completed_tasks else None, "stalled_task_count": len(stalled_tasks) if 'stalled_tasks' in locals() else 0 }, "next_optimization_actions": [ "๐ŸŽฏ Apply these insights when using `plan_task` for new work", "๐Ÿ“Š Use `update_task` more frequently to capture real-time insights", "๐Ÿ” Use `search_tasks` to find and replicate successful approaches", "๐Ÿ“‹ Check `list_tasks` regularly to maintain cognitive coordination" ], "meta_learning_guidance": { "pattern_recognition": "Look for themes across multiple tasks, not just individual successes", "feedback_loops": "The more you capture insights, the better these analyses become", "iterative_improvement": "Small consistent improvements compound over time", "cognitive_scaffolding": "Use these insights to structure your future problem-solving approaches" } } except Exception as e: error_handler.handle_error(e, ErrorType.INTERNAL, ErrorSeverity.MEDIUM) raise

Latest Blog Posts

MCP directory API

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

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jacklatrobe/codebuddy-mcp'

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