Skip to main content
Glama

analyze_conversations

Analyze conversation patterns to identify trends and generate actionable insights from stored chat data.

Instructions

Analyze conversation patterns and generate insights - AUTO-RUN at session start

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
user_idNo
limitNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • MCP tool handler and registration for 'analyze_conversations'. This is the entry point that receives tool calls and delegates to the ReflectionAgent for analysis.
    @mcp.tool(
        name="analyze_conversations",
        description="Analyze conversation patterns and generate insights - AUTO-RUN at session start",
    )
    async def analyze_conversations(
        user_id: str | None = None, limit: int = 20
    ) -> dict[str, Any]:
        """
        Analyze recent conversations to identify patterns, preferences, and generate actionable insights.
    
        ## AUTONOMOUS EXECUTION TRIGGERS
    
        ### MANDATORY Session Start (Always Execute)
        - **When**: >1 hour since last conversation
        - **Action**: Run analysis automatically, adapt responses immediately
        - **Silent**: Don't announce analysis unless insights are actionable
    
        ### PROACTIVE Mid-Session (Execute When)
        - User asks repetitive questions (3+ similar topics)
        - User shows frustration with recurring issues
        - You notice patterns that suggest knowledge gaps
        - User asks "what should I work on?" or similar
    
        ### IMMEDIATE Usage of Results
        - **Adapt communication style**: Use insights to match user preferences
        - **Proactive suggestions**: Mention relevant patterns without being asked
        - **Context awareness**: Reference ongoing projects and preferences
    
        ## What It Analyzes (Enhanced with Semantic Search)
    
        - **Topic Frequency**: Most discussed subjects and technologies
        - **Question Patterns**: Types of questions frequently asked
        - **Work Style**: Problem-solving approaches and preferences
        - **Project Focus**: Current projects and priorities
        - **Knowledge Gaps**: Areas where user needs more support
        - **Recurring Issues**: Problems that appear multiple times
        - **Incomplete Projects**: Work that seems unfinished
    
        ## Autonomous Integration Example
    
        ```python
        # Session start (user returns after 2 hours)
        # → AUTO: analyze_conversations()
        # → Insights: Focus on React hooks, recurring CORS issues
        # → Immediate adaptation: "Welcome back! I see you've been working on React hooks lately..."
    
        # Mid-conversation pattern detection
        # User asks 3rd question about TypeScript
        # → AUTO: analyze_conversations(limit=10)
        # → Response: "I notice you're asking several TypeScript questions - would a type reference help?"
        ```
    
        ## Response Processing Guidelines
    
        - **High-value insights**: Mention immediately ("I see you prefer functional patterns...")
        - **Actionable patterns**: Offer specific help ("You've had 3 CORS issues - want a permanent fix?")
        - **Learning opportunities**: Suggest resources proactively
        - **Project continuity**: Reference unfinished work naturally
    
        Args:
            user_id: User ID to analyze (optional, defaults to DEFAULT_USER_ID)
            limit: Number of recent memories to analyze (default: 20, max: 100)
                - Use 10-15 for quick mid-session checks
                - Use 20-50 for comprehensive session start analysis
    
        Returns:
            Analysis dictionary containing:
            - status: "analyzed" on success
            - memory_count: Number of memories analyzed
            - recent_count: Memories from chronological analysis
            - relevant_count: Memories from semantic search
            - insights: List of insight objects, each with:
                - type: Category of insight (frequent_questions, focus_area, etc.)
                - description: Human-readable explanation
                - examples: Specific examples when applicable
                - recommendation: Suggested action based on the insight
        """
        try:
            results = await reflection_agent.analyze_recent_conversations(
                user_id=user_id, limit=limit
            )
            logger.info(
                "Conversation analysis completed", insights=len(results.get("insights", []))
            )
            return results
        except Exception as e:
            logger.error("Analysis failed", error=str(e))
            raise RuntimeError(f"Analysis failed: {str(e)}") from e
  • Core helper method in ReflectionAgent class that implements the conversation analysis logic: fetches recent/relevant memories, combines them, analyzes patterns via _analyze_patterns, generates insights, and optionally stores a reflection memory.
    async def analyze_recent_conversations(
        self, user_id: str | None = None, limit: int = 20
    ) -> dict[str, Any]:
        """Analyze recent conversations and generate insights using semantic search.
    
        Args:
            user_id: User to analyze (defaults to settings)
            limit: Number of recent memories to analyze
    
        Returns:
            Analysis results with patterns and suggestions
        """
        user_id = user_id or settings.default_user_id
    
        try:
            # Get a mix of recent and semantically relevant memories
            all_memories = await memory_service.get_all_memories(user_id=user_id)
    
            if not all_memories:
                return {"status": "no_memories", "insights": []}
    
            # Get recent memories for recency bias
            recent_memories = sorted(
                all_memories, key=lambda m: m.get("created_at", ""), reverse=True
            )[: limit // 2]  # Half from recent
    
            # Get semantically relevant memories using pattern-based queries
            relevant_memories = await self._get_relevant_memories_for_analysis(
                user_id=user_id,
                recent_memories=recent_memories,
                remaining_limit=limit - len(recent_memories),
            )
    
            # Combine and deduplicate
            combined_memories = self._deduplicate_memories(
                recent_memories + relevant_memories
            )
    
            insights = await self._analyze_patterns(combined_memories)
    
            if insights:
                await self._store_reflection(insights, user_id)
    
            return {
                "status": "analyzed",
                "memory_count": len(combined_memories),
                "recent_count": len(recent_memories),
                "relevant_count": len(relevant_memories),
                "insights": insights,
            }
    
        except Exception as e:
            self._logger.error("Failed to analyze conversations", error=str(e))
            raise
  • Supporting helper that performs pattern matching on memories to generate insights about frequent questions, focus areas, and problem-solving patterns.
    async def _analyze_patterns(
        self, memories: list[dict[str, Any]]
    ) -> list[dict[str, str]]:
        """Analyze memory patterns and extract insights.
    
        Args:
            memories: List of memories to analyze
    
        Returns:
            List of insights with type and description
        """
        insights = []
    
        # Track topics discussed
        topics = {}
        questions_asked = []
        approaches_tried = []
    
        for memory in memories:
            content = memory.get("memory", memory.get("content", ""))
    
            # Simple pattern matching (could be enhanced with LLM analysis)
            if isinstance(content, str):
                # Track questions
                if "?" in content:
                    questions_asked.append(content)
    
                # Track code-related discussions
                if any(
                    keyword in content.lower()
                    for keyword in ["function", "class", "implement", "code", "debug"]
                ):
                    if "coding" not in topics:
                        topics["coding"] = 0
                    topics["coding"] += 1
    
                # Track problem-solving approaches
                if any(
                    keyword in content.lower()
                    for keyword in ["try", "attempt", "approach", "solution"]
                ):
                    approaches_tried.append(content)
    
        # Generate insights based on patterns
        if len(questions_asked) > 3:
            insights.append({
                "type": "frequent_questions",
                "description": f"User has asked {len(questions_asked)} questions recently. Consider providing more proactive information.",
                "examples": questions_asked[-3:],
            })
    
        if topics:
            most_discussed = max(topics.items(), key=lambda x: x[1])
            insights.append({
                "type": "focus_area",
                "description": f"Primary focus appears to be on {most_discussed[0]} (mentioned {most_discussed[1]} times)",
                "recommendation": f"Consider preparing more detailed resources on {most_discussed[0]}",
            })
    
        if len(approaches_tried) > 2:
            insights.append({
                "type": "problem_solving_pattern",
                "description": "Multiple approaches being tried, suggesting iterative problem solving",
                "recommendation": "Consider suggesting a structured approach or framework",
            })
    
        return insights
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden for behavioral disclosure. It mentions auto-run behavior which is useful, but doesn't describe what the analysis entails, what data sources are used, whether it's read-only or has side effects, what permissions might be needed, or any rate limits. The description is insufficient for a tool with behavioral implications.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is brief and front-loaded with the core purpose. The auto-run information is efficiently appended. However, the dash formatting could be clearer, and the description could be more structured with separate sentences for different aspects of functionality.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool has an output schema (which reduces need to describe return values), no annotations, and simple parameters, the description provides basic purpose and auto-run behavior but lacks crucial details about what analysis is performed, how parameters affect results, and behavioral characteristics. It's minimally adequate but has significant gaps.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters2/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage and 2 parameters, the description provides no information about the 'user_id' or 'limit' parameters. The schema shows defaults (null for user_id, 20 for limit) and types, but the description doesn't explain what user_id filters, what limit applies to, or how these affect the analysis. The description fails to compensate for the complete lack of schema documentation.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose3/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description states the tool 'analyze conversation patterns and generate insights', which provides a general purpose but lacks specificity about what patterns or insights are generated. It doesn't distinguish this analysis tool from potential siblings like 'suggest_next_actions' that might also analyze conversations. The description is vague about the exact nature of the analysis.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description mentions 'AUTO-RUN at session start' which provides some usage context, but doesn't explain when to manually invoke this tool versus relying on auto-run, nor does it differentiate when to use this versus sibling tools like 'search_memories' or 'suggest_next_actions' for conversation analysis. No explicit alternatives or exclusions are provided.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

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

curl -X GET 'https://glama.ai/api/mcp/v1/servers/terrymunro/mcp-mitm-mem0'

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