|
| 1 | +# Frontend MCP Integration Fix |
| 2 | + |
| 3 | +## Problem |
| 4 | + |
| 5 | +The frontend was trying to get function diffs by reading files directly from the filesystem using `/api/comparison/diff`, which failed with "Not Found" errors because: |
| 6 | + |
| 7 | +1. The file paths from the comparison were relative, not absolute |
| 8 | +2. The Next.js server couldn't access the files |
| 9 | +3. The frontend wasn't using the MCP server's `get_function_diff` tool |
| 10 | + |
| 11 | +## Solution |
| 12 | + |
| 13 | +Created an MCP-based function diff API that proxies to the MCP server. |
| 14 | + |
| 15 | +### Files Created/Modified |
| 16 | + |
| 17 | +#### 1. New API Route: `nextjs-frontend/src/app/api/mcp/function-diff/route.ts` |
| 18 | + |
| 19 | +This route: |
| 20 | +- Accepts `comparisonId`, `functionName`, and `includeContent` |
| 21 | +- Calls the MCP server's `get_function_diff` tool |
| 22 | +- Parses the MCP response into structured data |
| 23 | +- Returns both parsed data and raw text |
| 24 | + |
| 25 | +**Usage:** |
| 26 | +```typescript |
| 27 | +POST /api/mcp/function-diff |
| 28 | +{ |
| 29 | + "comparisonId": "uuid-here", |
| 30 | + "functionName": "my_function", |
| 31 | + "includeContent": true |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +#### 2. Updated: `nextjs-frontend/src/services/diffService.ts` |
| 36 | + |
| 37 | +Added new method: |
| 38 | +```typescript |
| 39 | +async getFunctionDiffFromMCP( |
| 40 | + comparisonId: string, |
| 41 | + functionName: string, |
| 42 | + includeContent: boolean = true |
| 43 | +): Promise<any> |
| 44 | +``` |
| 45 | + |
| 46 | +#### 3. Updated: `nextjs-frontend/src/services/comparisonService.ts` |
| 47 | + |
| 48 | +Added `comparisonId` field to `ComparisonResult` interface: |
| 49 | +```typescript |
| 50 | +export interface ComparisonResult { |
| 51 | + comparisonId?: string; // MCP comparison ID for getting function diffs |
| 52 | + // ... rest of fields |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +#### 4. Updated: `nextjs-frontend/src/components/graph/FunctionGraphViewer.tsx` |
| 57 | + |
| 58 | +Modified `loadFunctionDiff` to: |
| 59 | +1. Check if `data.comparisonId` is available |
| 60 | +2. If yes, use `diffService.getFunctionDiffFromMCP()` |
| 61 | +3. If no, fall back to file-based diff |
| 62 | +4. Convert MCP diff format to `FileDiff` format for display |
| 63 | + |
| 64 | +Added helper function `parseUnifiedDiffToLines()` to convert unified diff format to line-by-line format. |
| 65 | + |
| 66 | +## How It Works |
| 67 | + |
| 68 | +### Flow |
| 69 | + |
| 70 | +1. User clicks on a function node in the graph |
| 71 | +2. `FunctionGraphViewer` calls `loadFunctionDiff(node)` |
| 72 | +3. If `data.comparisonId` exists: |
| 73 | + - Calls `/api/mcp/function-diff` with comparison ID and function name |
| 74 | + - API route calls MCP server's `get_function_diff` tool |
| 75 | + - MCP server returns function diff with full content |
| 76 | + - Response is converted to `FileDiff` format |
| 77 | + - Diff is displayed in modal |
| 78 | +4. If no comparison ID: |
| 79 | + - Falls back to file-based diff (old behavior) |
| 80 | + |
| 81 | +### MCP Server Communication |
| 82 | + |
| 83 | +``` |
| 84 | +Frontend → Next.js API Route → MCP Server |
| 85 | + ← Parsed Response ← JSON-RPC Response |
| 86 | +``` |
| 87 | + |
| 88 | +**MCP Request:** |
| 89 | +```json |
| 90 | +{ |
| 91 | + "jsonrpc": "2.0", |
| 92 | + "id": 123, |
| 93 | + "method": "tools/call", |
| 94 | + "params": { |
| 95 | + "name": "get_function_diff", |
| 96 | + "arguments": { |
| 97 | + "comparison_id": "uuid", |
| 98 | + "function_name": "my_function", |
| 99 | + "include_content": true |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +**MCP Response:** |
| 106 | +```json |
| 107 | +{ |
| 108 | + "jsonrpc": "2.0", |
| 109 | + "id": 123, |
| 110 | + "result": { |
| 111 | + "content": [{ |
| 112 | + "type": "text", |
| 113 | + "text": "Function: my_function\nChange Type: modified\n..." |
| 114 | + }] |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +## Remaining Work |
| 120 | + |
| 121 | +### 1. Store Comparison ID When Creating Comparisons |
| 122 | + |
| 123 | +The frontend needs to be updated to: |
| 124 | +- Call the MCP server's `compare_locations` tool when creating comparisons |
| 125 | +- Extract and store the comparison ID from the response |
| 126 | +- Pass the comparison ID to the `ComparisonResult` |
| 127 | + |
| 128 | +**Files to update:** |
| 129 | +- Where comparisons are initiated (likely in a comparison page or service) |
| 130 | +- Need to create an API route that calls MCP's `compare_locations` |
| 131 | +- Store the comparison ID in the result |
| 132 | + |
| 133 | +### 2. Create MCP Compare API Route |
| 134 | + |
| 135 | +Create `nextjs-frontend/src/app/api/mcp/compare/route.ts`: |
| 136 | +```typescript |
| 137 | +POST /api/mcp/compare |
| 138 | +{ |
| 139 | + "sourcePath": "/path/to/source", |
| 140 | + "targetPath": "/path/to/target", |
| 141 | + "recursive": true |
| 142 | +} |
| 143 | + |
| 144 | +Response: |
| 145 | +{ |
| 146 | + "comparisonId": "uuid", |
| 147 | + "summary": { ... }, |
| 148 | + "functions": [ ... ] |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +### 3. Update Comparison Flow |
| 153 | + |
| 154 | +Current flow (Rust backend): |
| 155 | +``` |
| 156 | +Frontend → Rust API → Parser → Comparison Result |
| 157 | +``` |
| 158 | + |
| 159 | +New flow (MCP server): |
| 160 | +``` |
| 161 | +Frontend → Next.js API → MCP Server → Comparison Result (with ID) |
| 162 | + → Store ID |
| 163 | + → Use ID for function diffs |
| 164 | +``` |
| 165 | + |
| 166 | +## Benefits |
| 167 | + |
| 168 | +1. ✅ **No file access issues** - MCP server already has the comparison data |
| 169 | +2. ✅ **Complete function content** - No truncation (1MB limit) |
| 170 | +3. ✅ **Consistent data** - Same source for comparison and diffs |
| 171 | +4. ✅ **Better performance** - No need to re-parse files |
| 172 | +5. ✅ **Unified diff format** - Proper diff with context |
| 173 | + |
| 174 | +## Testing |
| 175 | + |
| 176 | +### Test the MCP Function Diff API |
| 177 | + |
| 178 | +```bash |
| 179 | +# 1. Create a comparison |
| 180 | +curl -s -X POST http://127.0.0.1:8011/message \ |
| 181 | + -H "Content-Type: application/json" \ |
| 182 | + -d '{ |
| 183 | + "jsonrpc": "2.0", |
| 184 | + "id": 1, |
| 185 | + "method": "tools/call", |
| 186 | + "params": { |
| 187 | + "name": "compare_locations", |
| 188 | + "arguments": { |
| 189 | + "source_path": "/home/matteius/isp-was-better/driver", |
| 190 | + "target_path": "/home/matteius/isp-latest/driver" |
| 191 | + } |
| 192 | + } |
| 193 | + }' | jq -r '.result.content[0].text' |
| 194 | + |
| 195 | +# Note the comparison ID from the output |
| 196 | + |
| 197 | +# 2. Test the Next.js API route |
| 198 | +curl -X POST http://localhost:3000/api/mcp/function-diff \ |
| 199 | + -H "Content-Type: application/json" \ |
| 200 | + -d '{ |
| 201 | + "comparisonId": "YOUR_COMPARISON_ID", |
| 202 | + "functionName": "vic_framedone_irq_function", |
| 203 | + "includeContent": true |
| 204 | + }' | jq . |
| 205 | +``` |
| 206 | + |
| 207 | +## Environment Variables |
| 208 | + |
| 209 | +Make sure these are set in `.env.local`: |
| 210 | + |
| 211 | +```bash |
| 212 | +MCP_SERVER_URL=http://127.0.0.1:8011 |
| 213 | +``` |
| 214 | + |
| 215 | +## Next Steps |
| 216 | + |
| 217 | +1. Create `/api/mcp/compare` route to initiate comparisons via MCP |
| 218 | +2. Update comparison pages to use MCP for comparisons |
| 219 | +3. Ensure comparison ID is passed through to graph viewer |
| 220 | +4. Test end-to-end flow |
| 221 | +5. Remove old file-based diff code once MCP integration is complete |
| 222 | + |
| 223 | +## Status |
| 224 | + |
| 225 | +- ✅ MCP function diff API created |
| 226 | +- ✅ DiffService updated with MCP method |
| 227 | +- ✅ FunctionGraphViewer updated to use MCP diffs |
| 228 | +- ✅ ComparisonResult interface updated |
| 229 | +- ⏳ Need to create MCP compare API |
| 230 | +- ⏳ Need to update comparison initiation code |
| 231 | +- ⏳ Need to test end-to-end flow |
| 232 | + |
0 commit comments