|
| 1 | +import type { FeedbackImportance } from '@colony/core'; |
| 2 | +import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; |
| 3 | +import { z } from 'zod'; |
| 4 | +import { type ToolContext, defaultWrapHandler } from './context.js'; |
| 5 | +import { mcpErrorResponse } from './shared.js'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Feedback lane (ICM slice 2 — docs/icm-integration-plan.md). Records the |
| 9 | + * "AI predicted X, real answer was Y" pairs that surface in code review, |
| 10 | + * test failures, and human corrections so a later agent can search prior |
| 11 | + * mistakes by topic. |
| 12 | + * |
| 13 | + * Progressive disclosure mirrors the observation surface: |
| 14 | + * feedback_record → row id only |
| 15 | + * feedback_search → compact hits (id, topic, score, snippet) |
| 16 | + * feedback_stats → counts per topic |
| 17 | + * |
| 18 | + * Compression invariant: prediction/correction/context flow through |
| 19 | + * `MemoryStore.recordFeedback`, which runs each body through the same |
| 20 | + * `prepareMemoryText` path observations use. Tool handlers never write |
| 21 | + * raw prose to storage directly. |
| 22 | + * |
| 23 | + * Note: this PR does not register a pre-tool-use hook that surfaces prior |
| 24 | + * corrections on inbound prompts. That belongs to a follow-up PR so this |
| 25 | + * slice can ship behind a search surface first. |
| 26 | + */ |
| 27 | +export function register(server: McpServer, ctx: ToolContext): void { |
| 28 | + const wrapHandler = ctx.wrapHandler ?? defaultWrapHandler; |
| 29 | + const { store } = ctx; |
| 30 | + |
| 31 | + const importanceSchema = z |
| 32 | + .enum(['critical', 'high', 'medium', 'low']) |
| 33 | + .describe('how strongly this correction should weigh against repeating the prediction'); |
| 34 | + |
| 35 | + server.tool( |
| 36 | + 'feedback_record', |
| 37 | + "Record an 'AI predicted X, real answer was Y' correction. Bodies are compressed via the same path observations use; returns only the new row id.", |
| 38 | + { |
| 39 | + topic: z |
| 40 | + .string() |
| 41 | + .min(1) |
| 42 | + .max(200) |
| 43 | + .describe('a short, stable label callers can pivot on (e.g. "frontend.routing")'), |
| 44 | + prediction: z.string().min(1).describe('what the AI predicted / asserted'), |
| 45 | + correction: z.string().min(1).describe('what the real answer turned out to be'), |
| 46 | + context: z |
| 47 | + .string() |
| 48 | + .min(1) |
| 49 | + .optional() |
| 50 | + .describe('optional surrounding context (where the prediction was made)'), |
| 51 | + importance: importanceSchema.optional(), |
| 52 | + created_by: z.string().min(1).optional().describe('agent or human author for audit'), |
| 53 | + }, |
| 54 | + wrapHandler('feedback_record', async (args) => { |
| 55 | + const topic = args.topic.trim(); |
| 56 | + if (!topic) { |
| 57 | + return mcpErrorResponse('INTERNAL_ERROR', 'feedback_record: topic must be non-empty'); |
| 58 | + } |
| 59 | + const id = store.recordFeedback({ |
| 60 | + topic, |
| 61 | + prediction: args.prediction, |
| 62 | + correction: args.correction, |
| 63 | + ...(args.context !== undefined ? { context: args.context } : {}), |
| 64 | + ...(args.importance !== undefined |
| 65 | + ? { importance: args.importance as FeedbackImportance } |
| 66 | + : {}), |
| 67 | + ...(args.created_by !== undefined ? { created_by: args.created_by } : {}), |
| 68 | + }); |
| 69 | + if (id < 0) { |
| 70 | + return mcpErrorResponse( |
| 71 | + 'INTERNAL_ERROR', |
| 72 | + 'feedback_record: prediction/correction collapsed to empty after privacy redaction', |
| 73 | + ); |
| 74 | + } |
| 75 | + return { content: [{ type: 'text', text: JSON.stringify({ id }) }] }; |
| 76 | + }), |
| 77 | + ); |
| 78 | + |
| 79 | + server.tool( |
| 80 | + 'feedback_search', |
| 81 | + 'Search prior corrections. Returns compact hits (id, topic, importance, score, snippet); use feedback_record output ids and a follow-up read if you need the full bodies.', |
| 82 | + { |
| 83 | + query: z |
| 84 | + .string() |
| 85 | + .min(1) |
| 86 | + .describe('FTS5 query across topic + prediction + correction + context'), |
| 87 | + topic: z |
| 88 | + .string() |
| 89 | + .min(1) |
| 90 | + .optional() |
| 91 | + .describe('optional exact-match filter on the feedback topic'), |
| 92 | + limit: z.number().int().positive().max(100).optional(), |
| 93 | + }, |
| 94 | + wrapHandler('feedback_search', async (args) => { |
| 95 | + const hits = store.searchFeedback({ |
| 96 | + query: args.query, |
| 97 | + ...(args.topic !== undefined ? { topic: args.topic } : {}), |
| 98 | + ...(args.limit !== undefined ? { limit: args.limit } : {}), |
| 99 | + }); |
| 100 | + return { content: [{ type: 'text', text: JSON.stringify({ hits }) }] }; |
| 101 | + }), |
| 102 | + ); |
| 103 | + |
| 104 | + server.tool( |
| 105 | + 'feedback_stats', |
| 106 | + 'Per-topic counts of recorded corrections, sorted by most recent first. Pass a topic to scope to a single bucket.', |
| 107 | + { |
| 108 | + topic: z.string().min(1).optional(), |
| 109 | + }, |
| 110 | + wrapHandler('feedback_stats', async (args) => { |
| 111 | + const stats = store.feedbackStats(args.topic !== undefined ? { topic: args.topic } : {}); |
| 112 | + return { content: [{ type: 'text', text: JSON.stringify({ stats }) }] }; |
| 113 | + }), |
| 114 | + ); |
| 115 | +} |
0 commit comments