Skip to content

spike: local legreffier MCP server with AxLearn self-improvement #418

@legreffier

Description

@legreffier

Summary

Build a local MCP server for legreffier that wraps AxLearn (@ax-llm/ax) with diary-backed storage. The server runs alongside Claude Code, keeps an AxLearn instance in memory with mode: 'continuous', and exposes MCP tools for asking questions, giving feedback, and triggering optimization.

This is the next evolution of legreffier: diary entries become the training signal for self-improvement. The agent gets better at answering codebase questions over time, and the entire learning chain is verifiable via diary provenance (CIDs, signatures, createdBy).

Motivation

  • Diary entries record what happened but aren't used to improve agent behavior
  • AxLearn's teacher-student pattern (opus teaches, sonnet runs) fits naturally with diary-backed storage
  • MoltNet adds provenance, trust-weighted training, and cross-agent learning on top of raw AxLearn
  • Local MCP server means native Claude Code integration — no custom skills or HTTP clients

Architecture

┌──────────────────────────────────────┐
│  legreffier local MCP server         │
│  (Fastify + @getlarge/fastify-mcp)   │
│                                      │
│  AxLearn instance (in memory)        │
│  ├── checkpoint from diary           │
│  ├── accumulated traces              │
│  ├── mode: 'continuous'              │
│  └── session: <uuid>                 │
│                                      │
│  AxStorage → diary entries via API   │
│  AxAIClaudeAgentSDK as LLM          │
│  Auto-kills after 2h idle            │
└──────────┬───────────────────────────┘
           │ MCP (stdio)
           │
  Claude Code (native MCP client)

MCP Tools

Tool Input Description
legreffier_ask { question, codeContext? } agent.forward() — answer question, trace saved to diary
legreffier_feedback { traceIndex?, score, label?, comment? } agent.addFeedback() + agent.applyUpdate() (continuous)
legreffier_traces { limit? } Recent traces with short index numbers
legreffier_optimize { budget? } Batch optimization with teacher model (opus)
legreffier_status {} Checkpoint version, trace count, avg score

AxStorage → Diary Mapping

AxStorage op Diary API call Tags
save trace POST /diaries/:id/entries axlearn:trace, axlearn:session:<uuid>, entry type: procedural
save checkpoint POST /diaries/:id/entries axlearn:checkpoint, axlearn:v:<N>, entry type: reflection
load traces POST /diaries/search Filter by axlearn:trace tags + date/limit
load checkpoints GET /diaries/:id/entries Filter by axlearn:checkpoint tags

What MoltNet adds beyond plain DB

  1. Provenance chain — CID from improved output → checkpoint → training traces → original experiences
  2. Trust-weighted training — vouch graph scores could weight traces during optimization
  3. Cross-agent learning — diary shares let other agents' traces become training data
  4. Signed checkpoints — learned behavior is content-addressed and verifiable
  5. Semantic search — pgvector enables meaning-based trace retrieval, not just tag queries

Implementation plan

Phase 0: Spike (this issue)

  • AxStorage adapter backed by diary entries (tools/src/legreffier-local/)
  • Minimal Fastify + fastify-mcp server with 5 tools
  • AxLearn wiring with AxAIClaudeAgentSDK (opus teacher, sonnet student)
  • Register in .claude/settings.json for testing
  • Verify roundtrip: ask → trace in diary → feedback → optimize → better answer

Phase 1: Polish

  • SSE transport with idle auto-kill (2h)
  • Session UUID + trace tagging
  • Trace index numbering for feedback targeting
  • Graceful shutdown + state persistence

Phase 2: Integration

  • Connect to existing legreffier skill (trigger tools from skill context)
  • axRAG adapter for on-demand deep retrieval (entries_search as overfetch)
  • Seed with prod diary data for meaningful optimization

Future

Key dependencies

  • @ax-llm/ax (AxLearn, AxStorage, ax) — already in workspace
  • @getlarge/fastify-mcp — already in workspace
  • @moltnet/api-client — diary CRUD + search
  • libs/context-evals/src/ax-claude-agent-sdk.ts — AxAIClaudeAgentSDK adapter

Research doc

Full brainstorming and design details: docs/research/local-context-packs-axrag-self-improvement.md

Related

Metadata

Metadata

Assignees

Labels

context-flywheelContext flywheel, evals, tiles, packs, and observation loop workmcp-serverMCP server componentspikeResearch or exploration task

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions