feat(mcp): enhanced audit trail with SQLite backend and rich metadata#43
Merged
Conversation
Expand AuditLogger.log() with optional duration_ms, change_detail, and output_summary parameters (backward compatible). Add SQLite backend alongside existing JSONL for queryable audit history. Timestamps now use millisecond-precision ISO format. New methods: query() for filtered retrieval (by days, profile, tool, result) and purge_old_entries() for retention management. Add MCPAuditConfig with enabled, level, log_search_queries, and retention_days fields.
Track execution time via perf_counter in call_tool handler. Build tool-specific change_detail (note_path, size_bytes, result_count) and output_summary for each tool type. Pass duration_ms, change_detail, and output_summary to all audit log calls (OK, DENIED, ERROR).
25 tests across 5 classes: enhanced logger (7) covering backward compat, duration, change_detail, SQLite writes, timestamps; query filtering (7) by profile/tool/result with ordering and limits; purge retention (2); change detail builders (7) for vault_write, capture, search, unknown tools; MCPAuditConfig validation (2).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Upgrade the MCP audit trail from minimal OK/DENIED/ERROR logging to rich, queryable audit entries with execution duration, tool-specific change details, and output summaries. Adds a SQLite backend alongside existing JSONL for structured queries, inspired by SivaRamSV/paaw's immutable audit trail pattern.
This is the final PR (#43) in the PAAW-inspired enhancement plan.
Before vs After
Before:
{"ts": "2025-03-25T14:32:15", "profile": "planner", "tool": "vault_write", "params": {"path": "02-projects/..."}, "result": "OK"}After:
{ "ts": "2025-03-25T14:32:15.123+00:00", "profile": "planner", "tool": "vault_write", "params": {"path": "02-projects/..."}, "result": "OK", "duration_ms": 342, "change_detail": { "type": "vault_write", "note_path": "02-projects/test.md", "size_bytes": 2847, "was_new": true }, "output_summary": { "status": "ok", "path": "02-projects/test.md" } }Tool-Specific Change Details
vault_writecapture/capture_notevault_searchvault_readgraph_query/graph_pathfind_duplicatessuggest_linksQueryable SQLite Backend
Every audit entry is dual-written to JSONL (streaming, tail-friendly) and SQLite (indexed, queryable). The SQLite
audit_eventstable has indexes ontsandtool.Changes
Modified Files
src/vaultmind/mcp/auth.py— ExpandedAuditLogger.log()with optionalduration_ms,change_detail,output_summaryparams (backward compatible). Added SQLite backend (audit_eventstable with WAL mode). Timestamps upgraded to millisecond-precision ISO format. New methods:query()for filtered retrieval andpurge_old_entries()for retention managementsrc/vaultmind/mcp/server.py— Addedtime.perf_counter()timing incall_toolhandler. New module-level helpers_build_change_detail()and_build_output_summary()extract tool-specific metadata. Duration, change_detail, and output_summary passed to all three audit log calls (OK, DENIED, ERROR)src/vaultmind/config.py— AddedMCPAuditConfigclass withenabled,level(minimal/standard/verbose),log_search_queries,retention_daysfields. Addedauditfield toMCPConfigconfig/default.toml— Added[mcp.audit]section with defaultsNew Files
tests/test_mcp_audit_enhanced.py(273 lines) — 25 tests across 5 classesBackward Compatibility
AuditLogger.log(profile, tool, params, result)still works — new params all optional with defaults.dbsuffix replaces.jsonl)test_mcp_auth.pytests pass unchangedMCPAuditConfignested insideMCPConfigviaauditfield — existingMCPConfigcallers unaffectedTest plan
test_mcp_audit_enhanced.pyacross 5 classes:test_mcp_auth.pytests pass unchangedruff check— cleanmypy --ignore-missing-imports— clean