- Step 202: docs/AGENT_API.md — comprehensive JSON-RPC API reference with 23 methods - Step 203: schemas/ — 20 JSON Schema files (8 types, 12 methods) for validation - Step 204: Expose ContextAPI (getInScopeSymbols, getCallHierarchy, getDependencyGraph) and BatchMutationAPI (applyBatch) via WebSocket RPC - Step 205: Expose Pipeline operations (runPipeline, parseSource, generateFromAST, projectLanguage) via WebSocket RPC - Step 206: API schema validation tests — 51/51 assertions pass - Updated PROGRESS.md with Sprint 5+6 completion, corrected summary table - Updated FEATURE_REQUESTS.md to reflect implemented items - Created sprint7_plan.md with 33 steps across 6 phases Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
26 KiB
Sprint 7: MCP Bridge & Agent Tooling — Plan
Goal: Make Whetstone's agent API accessible to LLMs (Claude, Codex, open-source models) via the Model Context Protocol (MCP). Document the existing JSON-RPC API, build an MCP server that wraps it, generate synthetic interaction traces for evaluation and fine-tuning, and create an evaluation harness to measure LLM tool-use accuracy against Whetstone.
Prerequisites: Sprint 6 complete (201 steps). All agent API methods wired through WebSocket JSON-RPC. WorkflowRecorder captures sessions.
Key themes:
- API documentation and JSON schemas
- MCP server exposing editor operations as tools/resources
- Synthetic trace generation for training data
- Evaluation harness for LLM tool-use accuracy
- Model-specific tool definitions (Claude, Codex)
- Session recording pipeline
Existing Agent API Surface (Reference)
Before planning steps, here's what already exists:
WebSocket JSON-RPC Methods (~25 methods):
| Category | Methods |
|---|---|
| Session | ping, getSessionInfo, setAgentName, listSessions |
| AST Access | getAST |
| Code Generation | generateCode |
| Mutations | applyMutation (subtypes: setProperty, updateNode, deleteNode, insertNode) |
| Annotations | getAnnotationSuggestions, applyAnnotationSuggestion, recordAnnotationFeedback |
| Workflow | startWorkflowRecording, stopWorkflowRecording, getWorkflowRecording, replayWorkflow |
| Role Management | setAgentRole |
C++ API Only (not yet exposed via RPC):
ContextAPI:getInScopeSymbols,getCallHierarchy,getDependencyGraphBatchMutationAPI:applySequencePipeline:run,parse,generate
Orchestrator-Only RPC (separate process):
setNodeProperty,insertNode,undo,redo,getLocks- Emacs bridge:
sendToEmacs,loadFile,saveFile
Phase 7a: API Documentation & Schemas (Steps 202–206)
Formal documentation so LLMs can understand and use the API correctly.
-
Step 202: JSON-RPC API reference document Create
docs/AGENT_API.mdwith complete API reference:- Every method: name, description, parameter schema, response schema, examples
- Grouped by category (Session, Query, Mutation, Annotation, Workflow, Generation)
- Error codes and error response format
- Authentication/role requirements noted per method
- "Quick Start" section: connect → set role → get AST → mutate → verify
New:
docs/AGENT_API.md
-
Step 203: JSON Schema definitions for all RPC methods Create machine-readable JSON Schema files for request/response validation:
schemas/methods/getAST.json— request params + response formatschemas/methods/applyMutation.json— all 4 subtypes with discriminated unionschemas/methods/generateCode.json— spec + preferImports paramsschemas/methods/getAnnotationSuggestions.json— nodeId or line/col params- One schema file per method, plus shared
schemas/types/for reusable types:ASTNode.schema.json,Suggestion.schema.json,Diagnostic.schema.json,MutationResult.schema.json,Symbol.schema.json,CallInfo.schema.json - Schemas validate against actual API behavior (tested in Step 206)
New:
schemas/directory with ~30 schema files
-
Step 204: Expose ContextAPI and BatchMutationAPI via RPC Wire the remaining C++ APIs into the WebSocket JSON-RPC handler:
getInScopeSymbols— params:{nodeId}→ Symbol[]getCallHierarchy— params:{functionId}→ CallInfogetDependencyGraph— params:{nodeId}→ string[] (node IDs)applyBatch— params:{mutations: Mutation[]}→ BatchResult- All respect
AgentPermissionPolicy(queries = all roles, batch = Refactor/Generator) Modifies:EditorState.h(processAgentRequest handler)
-
Step 205: Expose Pipeline operations via RPC Add RPC methods for the end-to-end pipeline:
runPipeline— params:{source, sourceLanguage, targetLanguage}→ PipelineResultparseSource— params:{source, language}→{ast, diagnostics[]}generateFromAST— params:{language}→{code}(uses current AST)projectLanguage— params:{targetLanguage}→{ast, annotationChanges[]}These are high-level operations LLMs can use without manual AST manipulation. Modifies:EditorState.h(processAgentRequest handler)
-
Step 206: API schema validation tests Tests verifying schemas match actual API behavior:
- Each method's request schema validates against test request payloads
- Each method's response schema validates against actual responses
- Invalid requests rejected with correct error codes
- Role-gated methods return permission errors for wrong roles
- Batch mutation rollback verified via schema-validated responses
- Pipeline methods return schema-compliant PipelineResult
New:
step206_test.cpp
Phase 7b: MCP Server (Steps 207–213)
Wrap the Whetstone agent API as an MCP server so Claude, Codex, and other MCP-compatible clients can interact with the editor natively.
-
Step 207: MCP server core with stdio transport Create
MCPServer.himplementing the MCP protocol:- JSON-RPC 2.0 over stdin/stdout (MCP stdio transport)
initialize/initializedhandshake with capabilities declaration- Server info: name="whetstone-mcp", version from build
- Capabilities:
tools,resources,prompts - Connection lifecycle: initialize → ready → serve → shutdown
- Error handling: MCP-compliant error responses
New:
editor/src/MCPServer.h
-
Step 208: MCP tools — AST query and mutation Register MCP tools that map to existing JSON-RPC methods:
whetstone_get_ast— Returns the current AST as JSON- Input schema:
{}(no params) or{nodeId}for subtree - Returns: AST JSON with annotation counts and diagnostics
- Input schema:
whetstone_mutate— Apply a mutation to the AST- Input schema:
{type, nodeId, property, value, ...}(same as applyMutation) - Returns:
{success, warning}
- Input schema:
whetstone_batch_mutate— Apply multiple mutations atomically- Input schema:
{mutations: [{type, ...}]} - Returns:
{success, appliedCount, error}
- Input schema:
whetstone_get_scope— Get symbols in scope at a node- Input schema:
{nodeId} - Returns: Symbol[]
- Input schema:
whetstone_get_call_hierarchy— Call hierarchy for a function- Input schema:
{functionId} - Returns: CallInfo
Each tool has a complete JSON Schema for its input, and a description
string tuned for LLM comprehension.
Modifies:
MCPServer.h
- Input schema:
-
Step 209: MCP tools — annotation and generation Additional MCP tools for higher-level operations:
whetstone_suggest_annotations— Get annotation suggestions for a code region- Input:
{nodeId}or{line, col} - Returns: suggestions with confidence scores + diagnostics
- Input:
whetstone_apply_annotation— Apply a suggested annotation- Input: suggestion object
- Returns:
{success, warning}
whetstone_generate_code— Generate code from a natural language spec- Input:
{spec, preferImports?} - Returns:
{node, usedSymbols, language}
- Input:
whetstone_run_pipeline— Full parse→infer→validate→optimize→generate- Input:
{source, sourceLanguage, targetLanguage} - Returns: PipelineResult (generated code + diagnostics)
- Input:
whetstone_project_language— Cross-language projection- Input:
{targetLanguage} - Returns: projected AST + annotation adaptation notes
Modifies:
MCPServer.h
- Input:
-
Step 210: MCP resources — editor state and project info Expose read-only editor state as MCP resources:
whetstone://ast— Current AST as JSON (subscribable)whetstone://diagnostics— Current diagnostics listwhetstone://libraries— Imported libraries with available symbolswhetstone://annotations— All annotations in current modulewhetstone://buffer/{path}— Buffer content by file pathwhetstone://settings— Editor settings (read-only) Resources support MCPresources/listandresources/read. Optional:resources/subscribefor AST and diagnostics (change notifications). Modifies:MCPServer.h
-
Step 211: MCP prompts — guided workflows Pre-built MCP prompts that guide LLMs through complex operations:
annotate_module— "Analyze this module and suggest memory annotations for all unannotated functions. Show confidence scores."cross_language_projection— "Project this {sourceLanguage} code to {targetLanguage}, adapting annotations appropriately."security_audit— "Check all dependencies for known vulnerabilities and suggest safe alternatives."refactor_memory— "Analyze memory strategy annotations and suggest improvements for safety and performance." Each prompt includes argument schemas and returns structured messages. Modifies:MCPServer.h
-
Step 212: MCP server entry point and transport wiring Create the MCP server executable and wire it to the editor:
editor/src/mcp_main.cpp— Standalone MCP server process- Reads stdin, writes stdout (MCP stdio transport)
- Connects to the editor's WebSocket server as an internal client
- Translates MCP tool calls → JSON-RPC requests → MCP responses
- Configuration:
mcp_config.jsonspecifying tool/resource visibility - Startup:
whetstone_mcp.exe(orwhetstone_mcpon Linux) - Can also run embedded within the editor process (in-process mode)
New:
editor/src/mcp_main.cpp,editor/src/MCPBridge.h
-
Step 213: MCP server tests End-to-end tests for the MCP server:
- Initialize handshake returns correct capabilities
tools/listreturns all registered tools with schemastools/callfor each tool produces correct resultsresources/listreturns all resourcesresources/readfor each resource returns valid dataprompts/listreturns all prompts with argument schemas- Invalid tool calls return MCP-compliant errors
- Role enforcement: tools respect agent permissions
New:
step213_test.cpp
Phase 7c: Synthetic Trace Generation (Steps 214–219)
Generate realistic interaction traces for fine-tuning LLMs on Whetstone tool use. Traces should look like real Claude/Codex sessions working with the editor.
-
Step 214: Trace data model and format Define the trace format in
TraceGenerator.h:Tracestruct:{id, scenario, steps: TraceStep[]}TraceStep:{role: "user"|"assistant"|"tool_call"|"tool_result", content}- Tool calls include: tool name, input JSON, output JSON
- Scenarios tagged with difficulty: basic, intermediate, advanced
- Export formats: JSONL (one trace per line), OpenAI chat format, Anthropic messages format
- Trace metadata: language, annotation types used, tools invoked, success/failure
New:
editor/src/TraceGenerator.h
-
Step 215: Scenario templates Define parameterized scenario templates that can generate many traces:
- Read & Understand: get AST → navigate structure → describe annotations
- Add Annotations: get AST → identify unannotated functions → suggest → apply annotations → verify
- Cross-Language: get AST → run pipeline Python→C++ → review generated code → fix annotation issues
- Refactor: get AST → find anti-patterns → batch mutate → validate → verify improvements
- Security Audit: get libraries → check vulnerabilities → suggest safe alternatives → update dependencies
- Multi-Step Debugging: get AST → run validation → find errors →
fix annotations → re-validate → confirm clean
Each template parameterized by: language, code complexity, number of functions,
annotation density, error injection rate.
New: scenario template data structures in
TraceGenerator.h
-
Step 216: Code corpus for trace generation Build a corpus of diverse code samples for trace generation:
- 20 Python modules (data processing, web handlers, ML pipelines, utils)
- 20 C++ modules (data structures, algorithms, RAII patterns, templates)
- 10 JavaScript modules (React components, Node.js servers, async patterns)
- 10 Rust modules (ownership patterns, trait implementations, error handling)
- 5 Go modules (goroutines, channels, interfaces)
- 5 Java modules (classes, generics, streams)
- Each module: 3–10 functions with varied complexity
- Some modules pre-annotated, some intentionally unannotated
- Some modules with annotation errors (for debugging scenarios)
Corpus stored as JSON-serialized ASTs in
traces/corpus/. New:traces/corpus/directory with ~70 sample modules
-
Step 217: Trace generator engine Engine that combines templates + corpus to produce traces:
TraceGenerator::generate(scenario, params)→ Trace- Simulates tool calls with real API responses (uses Pipeline, ContextAPI, etc.)
- Injects realistic "thinking" steps for the assistant role
- Handles error paths: tool call fails → assistant recovers → retries
- Configurable: noise level (typos in specs), difficulty, verbosity
- Deterministic with seed for reproducibility
- Batch generation:
generateBatch(count, distribution)→ Trace[] Modifies:TraceGenerator.h
-
Step 218: Trace export pipeline Export traces in formats suitable for fine-tuning and evaluation:
- Anthropic Messages format:
{role, content}with tool_use/tool_result blocks - OpenAI Chat format:
{role, content, tool_calls, tool_call_id} - JSONL: one trace per line, streaming-friendly
- Markdown: human-readable trace for review/documentation
- Filtering: by scenario type, difficulty, language, success/failure
- Statistics: tool call distribution, average trace length, success rate
- CLI:
whetstone_traces --count 1000 --format anthropic --output traces/New:editor/src/TraceExporter.h, export logic inTraceGenerator.h
- Anthropic Messages format:
-
Step 219: Trace generation tests Tests for trace generation:
- Generated traces conform to schema (valid JSON, correct roles)
- Tool calls in traces produce valid responses when replayed
- Each scenario template generates distinct traces with different seeds
- Export formats are well-formed (Anthropic/OpenAI/JSONL)
- Batch generation produces correct count with expected distribution
- Error-path traces include recovery sequences
New:
step219_test.cpp
Phase 7d: Evaluation Harness (Steps 220–224)
Test suites that measure how accurately an LLM can use Whetstone tools.
-
Step 220: Evaluation framework Create
EvalHarness.hwith the evaluation infrastructure:EvalTaskstruct:{id, description, setupAST, expectedOutcome, tools, maxSteps}EvalResultstruct:{taskId, passed, steps, toolCallCount, errors, duration}EvalRunner: loads tasks, executes agent traces, compares outcomes- Outcome comparison: AST diff (structural equality modulo IDs), annotation presence, generated code equivalence
- Scoring: binary pass/fail + partial credit for intermediate progress
New:
editor/src/EvalHarness.h
-
Step 221: Evaluation task suite — basic operations 30 basic tasks testing individual tool use:
- 5 tasks: read AST and answer questions about structure
- 5 tasks: apply a single mutation (rename function, change type, etc.)
- 5 tasks: add a specific annotation to a specific function
- 5 tasks: get annotation suggestions and apply the best one
- 5 tasks: generate code from a simple spec
- 5 tasks: run pipeline and report diagnostics
Each task has: setup AST, natural language instruction, expected outcome.
New:
eval/basic/directory with 30 task definitions
-
Step 222: Evaluation task suite — multi-step workflows 20 advanced tasks requiring multiple tool calls:
- 5 tasks: annotate all functions in a module (iterate, suggest, apply)
- 5 tasks: cross-language projection with annotation fixes
- 5 tasks: refactor (find pattern → batch mutate → validate)
- 5 tasks: debug annotation errors (validate → identify → fix → re-validate)
Each task has multiple acceptable solution paths.
New:
eval/workflows/directory with 20 task definitions
-
Step 223: Evaluation runner CLI Command-line tool to run evaluations:
whetstone_eval --tasks eval/basic/ --trace agent_trace.jsonl --report report.json- Accepts pre-recorded traces (for offline evaluation) or live agent connection
- Produces: per-task pass/fail, aggregate scores, tool-call statistics
- Comparison mode: evaluate multiple traces against same tasks
- Leaderboard output: rank agents by accuracy, efficiency (fewer tool calls = better)
New:
editor/src/eval_main.cpp
-
Step 224: Evaluation harness tests Tests for the evaluation framework itself:
- Known-good traces score 100% on basic tasks
- Known-bad traces (wrong mutations) score 0%
- Partial-credit scoring works for multi-step tasks
- AST diff correctly identifies structural changes
- Runner handles malformed traces gracefully
- Statistics aggregation is correct
New:
step224_test.cpp
Phase 7e: Model-Specific Tool Definitions (Steps 225–229)
Optimized tool definitions and prompts for specific LLM families.
-
Step 225: Claude tool definitions Anthropic-format tool definitions optimized for Claude:
- Each Whetstone MCP tool as a Claude
tool_usedefinition - Descriptions tuned for Claude's strengths (detailed, structured)
- Input schemas with examples and constraints
- System prompt template: "You are a Whetstone coding assistant with access to a semantic annotation editor. You can read and modify ASTs, suggest memory annotations, and project code across languages."
- Few-shot examples embedded in tool descriptions
New:
tools/claude/directory with tool definitions + system prompt
- Each Whetstone MCP tool as a Claude
-
Step 226: Codex / GPT tool definitions OpenAI-format function definitions for Codex and GPT models:
- Each tool as an OpenAI
functiondefinition - Descriptions tuned for OpenAI models (concise, example-heavy)
strict: trueschemas where applicable- System prompt template adapted for OpenAI message format
- Parallel tool calling hints for independent operations
New:
tools/openai/directory with function definitions + system prompt
- Each tool as an OpenAI
-
Step 227: Open-source model tool definitions Generic tool definitions for open-source models (Llama, Mistral, etc.):
- ReAct-style prompting with tool descriptions in system prompt
- XML-tagged tool call format (for models without native tool calling)
- Simplified schemas (fewer optional fields, more defaults)
- Adapter that parses model output into structured tool calls
New:
tools/generic/directory with definitions + adapter
-
Step 228: Prompt engineering templates Reusable prompt templates for common Whetstone tasks:
annotate_module.prompt— System + user messages for annotation workflowcross_language.prompt— System + user for cross-language projectioncode_review.prompt— System + user for reviewing annotationsrefactor.prompt— System + user for refactoring workflowssecurity.prompt— System + user for security audit Each template has: system prompt, user prompt template with{{variables}}, expected tool sequence, success criteria. New:tools/prompts/directory with 5+ prompt templates
-
Step 229: Tool definition tests Tests verifying tool definitions are correct and complete:
- All MCP tools have corresponding Claude/OpenAI/generic definitions
- Tool input schemas match MCP tool schemas
- System prompts reference all available tools
- Few-shot examples in tool descriptions produce valid tool calls
- Prompt templates have all required variables
New:
step229_test.cpp
Phase 7f: Session Recording Pipeline (Steps 230–234)
Capture real editing sessions, anonymize them, and export as training data.
-
Step 230: Enhanced session recorder Extend
WorkflowRecorderfor full session capture:- Record all JSON-RPC traffic (not just agent-initiated)
- Capture editor events: file open, buffer switch, theme change, etc.
- Timestamp all events with millisecond precision
- Session metadata: editor version, OS, language, project type
- Start/stop recording from UI (status bar indicator)
- Auto-recording mode: always capture (configurable in settings)
Modifies:
WorkflowRecorder.h,panels/StatusBarPanel.h
-
Step 231: Session anonymizer Strip PII and sensitive data from recorded sessions:
- Replace file paths with generic paths (
/project/src/module.py) - Replace variable/function names with synthetic names (preserve structure)
- Strip comments and string literal contents
- Remove user-specific settings (paths, API keys)
- Configurable: anonymization level (light/medium/full)
- Deterministic: same input produces same anonymized output (with seed)
New:
editor/src/SessionAnonymizer.h
- Replace file paths with generic paths (
-
Step 232: Session-to-trace converter Convert recorded sessions into training trace format:
- Map editor events → user messages ("I opened file X and want to annotate it")
- Map RPC requests → tool calls
- Map RPC responses → tool results
- Insert synthetic assistant "thinking" between tool calls
- Handle session gaps (user pauses → split into separate traces)
- Quality filter: discard sessions with too many errors or no meaningful work
New:
editor/src/SessionToTrace.h
-
Step 233: Training data pipeline CLI End-to-end pipeline from raw sessions to training data:
whetstone_pipeline --input sessions/ --anonymize medium --format anthropic --output training/- Steps: load sessions → anonymize → convert to traces → filter → export
- Statistics: sessions processed, traces generated, quality distribution
- Deduplication: detect near-duplicate traces
- Validation: all output traces pass schema validation
New:
editor/src/pipeline_main.cpp
-
Step 234: Session pipeline tests Tests for the recording and conversion pipeline:
- Recorded session round-trips through anonymizer preserving structure
- Anonymized sessions have no file paths or identifiable content
- Session-to-trace conversion produces valid traces
- Pipeline CLI produces correct output count
- Quality filter removes degenerate sessions
- Deduplication identifies similar traces
New:
step234_test.cpp
Summary
| Phase | Steps | Description |
|---|---|---|
| 7a | 202–206 | API documentation & JSON schemas |
| 7b | 207–213 | MCP server (tools, resources, prompts, transport) |
| 7c | 214–219 | Synthetic trace generation (templates, corpus, export) |
| 7d | 220–224 | Evaluation harness (tasks, runner, scoring) |
| 7e | 225–229 | Model-specific tool definitions (Claude, Codex, open-source) |
| 7f | 230–234 | Session recording pipeline (capture, anonymize, convert) |
Total: 33 steps across 6 phases.
Context for Agents
What exists after Sprint 6 (prerequisites):
- 80+ header files in editor/src/ including panels/, state/, ast/
- ~25 JSON-RPC methods via WebSocket agent server
- WorkflowRecorder for session capture/replay
- AgentPermissionPolicy with Linter/Refactor/Generator roles
- Full pipeline: parse→infer→validate→optimize→generate for 8 languages
- ContextAPI, BatchMutationAPI (C++ only, not yet RPC-exposed)
- 347+ passing test executables
Architecture principles (from Sprint 6, still apply):
- 600-line header limit — split files that exceed this
- Panel extraction pattern —
void renderXxx(EditorState&)free functions - Event-driven updates — use UIEventBus, not polling
- Theme-aware rendering — use
ThemeEngine::getColor(), not hardcoded colors - Test-first — minimum 2 tests per step, real assertions only
- Header-only — all components in
.hfiles (only main/orchestrator/mcp are.cpp)
Key new files introduced in Sprint 7:
| File | What it does |
|---|---|
docs/AGENT_API.md |
Complete JSON-RPC API reference |
schemas/ |
Machine-readable JSON Schema for all methods |
MCPServer.h |
MCP protocol implementation |
MCPBridge.h |
MCP ↔ WebSocket JSON-RPC translation |
mcp_main.cpp |
Standalone MCP server executable |
TraceGenerator.h |
Synthetic interaction trace generation |
TraceExporter.h |
Multi-format trace export (Anthropic/OpenAI/JSONL) |
EvalHarness.h |
LLM tool-use evaluation framework |
eval_main.cpp |
Evaluation runner CLI |
SessionAnonymizer.h |
PII removal from recorded sessions |
SessionToTrace.h |
Session → training trace conversion |
pipeline_main.cpp |
Training data pipeline CLI |
tools/claude/ |
Claude-optimized tool definitions |
tools/openai/ |
OpenAI-format function definitions |
tools/generic/ |
Open-source model tool definitions |
tools/prompts/ |
Reusable prompt engineering templates |
traces/corpus/ |
Code sample corpus for trace generation |
eval/basic/ |
30 basic evaluation tasks |
eval/workflows/ |
20 multi-step evaluation tasks |
Build notes:
- No new external C++ dependencies required for Phases 7a–7b (MCP is JSON-RPC 2.0 over stdio, implemented with nlohmann-json)
- Phase 7c corpus files are JSON data, no code changes to add more
- Evaluation tasks are JSON definitions, extensible without recompilation
- Tool definitions (Phase 7e) are JSON/Markdown files, not compiled code
MCP Protocol Reference:
- MCP spec: JSON-RPC 2.0 over stdio (stdin/stdout)
- Three primitives: tools (model-invoked), resources (context/data), prompts (user-facing templates)
- Lifecycle:
initialize→initialized→ serve →shutdown - Capabilities negotiated during handshake
Sprint 8 Preview (for planning)
Sprint 8: Real-World Integration & Production Hardening
Planned themes (to be fully specified after Sprint 7):
- End-to-end testing with real LLM agents (Claude, GPT-4, Llama)
- Performance profiling and optimization with real workloads
- Multi-file project support (project-wide AST, cross-file references)
- Git integration (diff view, blame annotations, branch-aware sessions)
- Collaborative editing (multiple users/agents on same project)
- Production packaging and distribution (stable release)