Phase 4: Query tools implementation#12
Conversation
- pyproject.toml with FastMCP, uvicorn, and dev dependencies - Makefile with check, fmt, lint, test, install, uninstall targets - LaunchAgent plist and install/uninstall scripts for auto-start - dev.sh script for development mode with auto-reload - Basic FastMCP server with placeholder tools: - get_status: Returns server status - ingest_logs: Placeholder for log ingestion - query_tool_frequency: Placeholder for frequency queries - Usage guide as MCP resource at session-analytics://guide - Tests for the placeholder tools - README with installation and usage instructions Server runs on port 8081 (to not conflict with event-bus on 8080). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- storage.py with SQLiteStorage class: - Events table with denormalized fields for fast queries - Sessions table for session metadata - Ingestion state tracking for incremental updates - Patterns table for pre-computed insights - Indexes on timestamp, session_id, tool_name, project_path - Data classes: Event, Session, IngestionState, Pattern - CRUD operations for all entities with batch insert support - get_db_stats() for monitoring database health - Updated server.py to use storage for get_status() - Comprehensive test suite (16 tests) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Implements log file discovery and parsing: - find_log_files(): Discovers JSONL files within date range - parse_tool_use(): Extracts tool info (command, file_path, skill_name) - parse_entry(): Parses entries into Event objects - ingest_file(): Incremental ingestion with mtime/size tracking - ingest_logs(): Full ingestion orchestration - update_session_stats(): Aggregates session statistics Integrates with server.py to provide real data for ingest_logs tool. Closes #3 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Implements all query MCP tools: - query_tool_frequency: Tool usage counts with project filter - query_timeline: Events in time window with filtering - query_commands: Bash command breakdown with prefix filter - query_sessions: Session metadata and token totals - query_tokens: Token usage grouped by day/session/model Also adds: - ensure_fresh_data(): Auto-refresh mechanism (5 min staleness) - Comprehensive tests for all queries (18 new tests) Closes #4 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Pull Request Review: Phase 4 Query Tools ImplementationOverviewThis PR implements comprehensive query tools for the session analytics MCP server with good architecture and solid test coverage. The implementation is well-structured and follows Python best practices. ✅ Code Quality & Best PracticesStrengths
Security Review✅ No SQL Injection Vulnerabilities All queries properly use parameterized SQL. User-supplied values (project, prefix, tool, etc.) are passed via params list, never interpolated into SQL strings. ✅ Path Traversal Protection Database path uses Path.home() with hardcoded subdirectories. Log file discovery is constrained to ~/.claude/projects/ by default. 🐛 Potential Issues & Edge Cases1. Race Condition in ensure_fresh_data() (Low severity)Location: src/session_analytics/queries.py:33-40 If multiple queries run concurrently on stale data, they'll all trigger ingestion simultaneously. This could cause duplicate work and SQLite lock contention. For an MCP server handling sequential requests, this is low priority. 2. Timezone Handling (Low severity)Location: src/session_analytics/ingest.py:133-135 Converting to naive datetime assumes local timezone. Document that all timestamps are stored in local timezone. 3. Empty UUID Handling (Low severity)Location: src/session_analytics/ingest.py:270 For summary entries with missing uuid and leafUuid, creates "summary:unknown" which could collide. Recommend adding timestamp suffix for uniqueness. 4. F-string Query Construction (Documentation needed)While SAFE in current implementation (where_clause is built from hardcoded strings only), add comments explaining this to prevent future bugs. 🚀 Performance ConsiderationsGood Practices
Potential Optimizations
📋 Test Coverage✅ Excellent Coverage
Suggestions for additional tests:
📝 Documentation✅ Good Documentation
Minor suggestions:
🎯 Summary & RecommendationsCritical IssuesNone - Code is production-ready. High Priority
Medium Priority
Low Priority
✅ ApprovalExcellent work! Implementation is clean, secure, well-tested, and follows best practices. Issues identified are minor and don't block merging. Recommendation: ✅ Approve and merge The codebase demonstrates:
Great job! 🚀 |
Summary
Implements all query MCP tools for session analytics:
query_tool_frequency- Tool usage counts with project filterquery_timeline- Events in time window with filtering by tool/projectquery_commands- Bash command breakdown with prefix filterquery_sessions- Session metadata with token totalsquery_tokens- Token usage grouped by day/session/modelAlso adds:
ensure_fresh_data()- Auto-refresh mechanism that checks data staleness (>5 min old) and refreshes transparently before queriesTest plan
make check- all 53 tests passCloses #4
🤖 Generated with Claude Code