|
| 1 | +/** |
| 2 | + * MCP Response Formatter |
| 3 | + * |
| 4 | + * Compact mode reduces token usage ~60% by: |
| 5 | + * - Shortening verbose JSON keys (filePath → fp, qualifiedName → qn, etc.) |
| 6 | + * - Removing null/undefined values |
| 7 | + * - Dropping rarely-useful fields (id hashes, visibility, pagination echoes) |
| 8 | + * - Hoisting shared repositoryId/language to envelope level |
| 9 | + * - Minifying JSON (no indentation) |
| 10 | + * |
| 11 | + * Full mode: passthrough with pretty-print (existing behavior). |
| 12 | + */ |
| 13 | + |
| 14 | +import type { ResponseMode } from '../core/types.js'; |
| 15 | + |
| 16 | +const KEY_MAP: Record<string, string> = { |
| 17 | + results: 'r', |
| 18 | + callers: 'r', |
| 19 | + callees: 'r', |
| 20 | + name: 'n', |
| 21 | + qualifiedName: 'qn', |
| 22 | + kind: 'k', |
| 23 | + filePath: 'fp', |
| 24 | + lineStart: 'ls', |
| 25 | + lineEnd: 'le', |
| 26 | + repositoryId: 'rid', |
| 27 | + language: 'lang', |
| 28 | + nodeId: 'nid', |
| 29 | + score: 's', |
| 30 | + isExported: 'x', |
| 31 | + complexity: 'cx', |
| 32 | + total: 't', |
| 33 | + hasMore: 'm', |
| 34 | + symbol: 'sym', |
| 35 | + affectedNodes: 'nodes', |
| 36 | + affectedFiles: 'files', |
| 37 | + entryPoints: 'entry', |
| 38 | + modules: 'mods', |
| 39 | + keyAbstractions: 'abs', |
| 40 | + searchType: 'st', |
| 41 | +}; |
| 42 | + |
| 43 | +// Fields stripped entirely in compact mode |
| 44 | +const STRIP_FIELDS = new Set(['id', 'visibility', 'limit', 'offset', 'columnStart', 'columnEnd']); |
| 45 | + |
| 46 | +/** |
| 47 | + * Recursively rename keys, strip null/undefined/stripped fields. |
| 48 | + */ |
| 49 | +function compactValue(value: unknown): unknown { |
| 50 | + if (value === null || value === undefined) return undefined; |
| 51 | + if (Array.isArray(value)) { |
| 52 | + return value.map(compactValue).filter((v) => v !== undefined); |
| 53 | + } |
| 54 | + if (typeof value === 'object') { |
| 55 | + return compactObject(value as Record<string, unknown>); |
| 56 | + } |
| 57 | + return value; |
| 58 | +} |
| 59 | + |
| 60 | +function compactObject(obj: Record<string, unknown>): Record<string, unknown> { |
| 61 | + const out: Record<string, unknown> = {}; |
| 62 | + for (const [key, val] of Object.entries(obj)) { |
| 63 | + if (STRIP_FIELDS.has(key)) continue; |
| 64 | + if (val === null || val === undefined) continue; |
| 65 | + |
| 66 | + const compacted = compactValue(val); |
| 67 | + if (compacted === undefined) continue; |
| 68 | + |
| 69 | + const mappedKey = KEY_MAP[key] ?? key; |
| 70 | + out[mappedKey] = compacted; |
| 71 | + } |
| 72 | + return out; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Hoist repositoryId and language to envelope when all items in a results |
| 77 | + * array share the same value, then remove from individual items. |
| 78 | + */ |
| 79 | +function hoistSharedFields(obj: Record<string, unknown>): Record<string, unknown> { |
| 80 | + const resultsKey = Object.keys(obj).find((k) => Array.isArray(obj[k]) && k === 'r'); |
| 81 | + if (!resultsKey) return obj; |
| 82 | + |
| 83 | + const items = obj[resultsKey] as Record<string, unknown>[]; |
| 84 | + if (items.length === 0) return obj; |
| 85 | + |
| 86 | + const sharedFields: Array<{ original: string; compact: string }> = [ |
| 87 | + { original: 'repositoryId', compact: 'rid' }, |
| 88 | + { original: 'language', compact: 'lang' }, |
| 89 | + ]; |
| 90 | + |
| 91 | + const hoisted: Record<string, unknown> = {}; |
| 92 | + |
| 93 | + for (const { original, compact } of sharedFields) { |
| 94 | + const compactKey = KEY_MAP[original] ?? original; |
| 95 | + const values = items.map((item) => item[compactKey] ?? item[original]); |
| 96 | + const allSame = values.length > 0 && values.every((v) => v === values[0]); |
| 97 | + if (allSame && values[0] !== undefined) { |
| 98 | + hoisted[compact] = values[0]; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (Object.keys(hoisted).length === 0) return obj; |
| 103 | + |
| 104 | + // Remove hoisted fields from each item |
| 105 | + const hoistedKeys = new Set(Object.keys(hoisted)); |
| 106 | + const strippedItems = items.map((item) => { |
| 107 | + const copy = { ...item }; |
| 108 | + for (const k of hoistedKeys) { |
| 109 | + delete copy[k]; |
| 110 | + } |
| 111 | + return copy; |
| 112 | + }); |
| 113 | + |
| 114 | + return { ...obj, ...hoisted, [resultsKey]: strippedItems }; |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Transform a response object into compact form. |
| 119 | + */ |
| 120 | +export function compactResponse(result: unknown): unknown { |
| 121 | + if (typeof result !== 'object' || result === null || Array.isArray(result)) { |
| 122 | + return result; |
| 123 | + } |
| 124 | + const compacted = compactObject(result as Record<string, unknown>); |
| 125 | + return hoistSharedFields(compacted); |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Serialize an MCP tool result to string. |
| 130 | + * compact: short keys + minified JSON |
| 131 | + * full: original keys + pretty-printed JSON |
| 132 | + */ |
| 133 | +export function formatMCPResponse(result: unknown, mode: ResponseMode): string { |
| 134 | + if (mode === 'compact') { |
| 135 | + return JSON.stringify(compactResponse(result)); |
| 136 | + } |
| 137 | + return JSON.stringify(result, null, 2); |
| 138 | +} |
0 commit comments