|
| 1 | +# How Claude Code IDE Extensions Actually Work |
| 2 | + |
| 3 | +This document explains the protocol and architecture behind Claude Code's IDE integrations, based on reverse-engineering the VS Code extension. Use this guide to build your own integrations or understand how the official ones work. |
| 4 | + |
| 5 | +## TL;DR |
| 6 | + |
| 7 | +Claude Code extensions create WebSocket servers in your IDE that Claude connects to. They use a WebSocket variant of MCP (Model Context Protocol) that only Claude supports. The IDE writes a lock file with connection info, sets some environment variables, and Claude automatically connects when launched. |
| 8 | + |
| 9 | +## How Discovery Works |
| 10 | + |
| 11 | +When you launch Claude Code from your IDE, here's what happens: |
| 12 | + |
| 13 | +### 1. IDE Creates a WebSocket Server |
| 14 | + |
| 15 | +The extension starts a WebSocket server on a random port (10000-65535) that listens for connections from Claude. |
| 16 | + |
| 17 | +### 2. Lock File Creation |
| 18 | + |
| 19 | +The IDE writes a discovery file to `~/.claude/ide/[port].lock`: |
| 20 | + |
| 21 | +```json |
| 22 | +{ |
| 23 | + "pid": 12345, // IDE process ID |
| 24 | + "workspaceFolders": ["/path/to/project"], // Open folders |
| 25 | + "ideName": "VS Code", // or "Neovim", "IntelliJ", etc. |
| 26 | + "transport": "ws" // WebSocket transport |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +### 3. Environment Variables |
| 31 | + |
| 32 | +When launching Claude, the IDE sets: |
| 33 | + |
| 34 | +- `CLAUDE_CODE_SSE_PORT`: The WebSocket server port |
| 35 | +- `ENABLE_IDE_INTEGRATION`: Set to "true" |
| 36 | + |
| 37 | +### 4. Claude Connects |
| 38 | + |
| 39 | +Claude reads the lock files, finds the matching port from the environment, and connects to the WebSocket server. |
| 40 | + |
| 41 | +## The Protocol |
| 42 | + |
| 43 | +Communication uses WebSocket with JSON-RPC 2.0 messages: |
| 44 | + |
| 45 | +```json |
| 46 | +{ |
| 47 | + "jsonrpc": "2.0", |
| 48 | + "method": "method_name", |
| 49 | + "params": { |
| 50 | + /* parameters */ |
| 51 | + }, |
| 52 | + "id": "unique-id" // for requests that expect responses |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +The protocol is based on MCP (Model Context Protocol) specification 2025-03-26, but uses WebSocket transport instead of stdio/HTTP. |
| 57 | + |
| 58 | +## Key Message Types |
| 59 | + |
| 60 | +### From IDE to Claude |
| 61 | + |
| 62 | +These are notifications the IDE sends to keep Claude informed: |
| 63 | + |
| 64 | +#### 1. Selection Updates |
| 65 | + |
| 66 | +Sent whenever the user's selection changes: |
| 67 | + |
| 68 | +```json |
| 69 | +{ |
| 70 | + "jsonrpc": "2.0", |
| 71 | + "method": "selection_changed", |
| 72 | + "params": { |
| 73 | + "text": "selected text content", |
| 74 | + "filePath": "/absolute/path/to/file.js", |
| 75 | + "fileUrl": "file:///absolute/path/to/file.js", |
| 76 | + "selection": { |
| 77 | + "start": { "line": 10, "character": 5 }, |
| 78 | + "end": { "line": 15, "character": 20 }, |
| 79 | + "isEmpty": false |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +#### 2. At-Mentions |
| 86 | + |
| 87 | +When the user explicitly sends a selection as context: |
| 88 | + |
| 89 | +```json |
| 90 | +{ |
| 91 | + "jsonrpc": "2.0", |
| 92 | + "method": "at_mentioned", |
| 93 | + "params": { |
| 94 | + "filePath": "/path/to/file", |
| 95 | + "lineStart": 10, |
| 96 | + "lineEnd": 20 |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +### From Claude to IDE |
| 102 | + |
| 103 | +According to the MCP spec, Claude should be able to call tools, but **current implementations are mostly one-way** (IDE → Claude). |
| 104 | + |
| 105 | +#### Tool Calls (Future) |
| 106 | + |
| 107 | +```json |
| 108 | +{ |
| 109 | + "jsonrpc": "2.0", |
| 110 | + "id": "request-123", |
| 111 | + "method": "tools/call", |
| 112 | + "params": { |
| 113 | + "name": "openFile", |
| 114 | + "arguments": { |
| 115 | + "filePath": "/path/to/file.js" |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +#### Tool Responses |
| 122 | + |
| 123 | +```json |
| 124 | +{ |
| 125 | + "jsonrpc": "2.0", |
| 126 | + "id": "request-123", |
| 127 | + "result": { |
| 128 | + "content": [{ "type": "text", "text": "File opened successfully" }] |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +## Available MCP Tools |
| 134 | + |
| 135 | +The extensions register these tools that Claude can (theoretically) call: |
| 136 | + |
| 137 | +### Core Tools |
| 138 | + |
| 139 | +1. **openFile** - Open a file and optionally select text |
| 140 | + |
| 141 | + ```json |
| 142 | + { |
| 143 | + "filePath": "/path/to/file.js", |
| 144 | + "startText": "function hello", // Find and select from this text |
| 145 | + "endText": "}" // To this text |
| 146 | + } |
| 147 | + ``` |
| 148 | + |
| 149 | +2. **openDiff** - Show a diff and wait for user action (blocking!) |
| 150 | + |
| 151 | + ```json |
| 152 | + { |
| 153 | + "old_file_path": "/path/to/original.js", |
| 154 | + "new_file_path": "/path/to/modified.js", |
| 155 | + "new_file_contents": "// Modified content...", |
| 156 | + "tab_name": "Proposed changes" |
| 157 | + } |
| 158 | + ``` |
| 159 | + |
| 160 | + Returns `FILE_SAVED` or `DIFF_REJECTED` based on user action. |
| 161 | + |
| 162 | +3. **getCurrentSelection** - Get the current text selection |
| 163 | +4. **getOpenEditors** - List all open files |
| 164 | +5. **getWorkspaceFolders** - Get project folders |
| 165 | +6. **getDiagnostics** - Get errors/warnings from the IDE |
| 166 | +7. **saveDocument** - Save a file |
| 167 | +8. **close_tab** - Close a tab by name (note the inconsistent naming!) |
| 168 | + |
| 169 | +### Implementation Notes |
| 170 | + |
| 171 | +- Most tools follow camelCase naming except `close_tab` (uses snake_case) |
| 172 | +- The `openDiff` tool is unique - it's **blocking** and waits for user interaction |
| 173 | +- Tools return MCP-formatted responses with content arrays |
| 174 | +- There's also `executeCode` for Jupyter notebooks in the VS Code extension |
| 175 | + |
| 176 | +## Building Your Own Integration |
| 177 | + |
| 178 | +Here's the minimum viable implementation: |
| 179 | + |
| 180 | +### 1. Create a WebSocket Server |
| 181 | + |
| 182 | +```lua |
| 183 | +-- Listen on localhost only (important!) |
| 184 | +local server = create_websocket_server("127.0.0.1", random_port) |
| 185 | +``` |
| 186 | + |
| 187 | +### 2. Write the Lock File |
| 188 | + |
| 189 | +```lua |
| 190 | +-- ~/.claude/ide/[port].lock |
| 191 | +local lock_data = { |
| 192 | + pid = vim.fn.getpid(), |
| 193 | + workspaceFolders = { vim.fn.getcwd() }, |
| 194 | + ideName = "YourEditor", |
| 195 | + transport = "ws" |
| 196 | +} |
| 197 | +write_json(lock_path, lock_data) |
| 198 | +``` |
| 199 | + |
| 200 | +### 3. Set Environment Variables |
| 201 | + |
| 202 | +```bash |
| 203 | +export CLAUDE_CODE_SSE_PORT=12345 |
| 204 | +export ENABLE_IDE_INTEGRATION=true |
| 205 | +claude # Claude will now connect! |
| 206 | +``` |
| 207 | + |
| 208 | +### 4. Handle Messages |
| 209 | + |
| 210 | +```lua |
| 211 | +-- Send selection updates |
| 212 | +send_message({ |
| 213 | + jsonrpc = "2.0", |
| 214 | + method = "selection_changed", |
| 215 | + params = { ... } |
| 216 | +}) |
| 217 | + |
| 218 | +-- Implement tools (if needed) |
| 219 | +register_tool("openFile", function(params) |
| 220 | + -- Open file logic |
| 221 | + return { content = {{ type = "text", text = "Done" }} } |
| 222 | +end) |
| 223 | +``` |
| 224 | + |
| 225 | +## Security Considerations |
| 226 | + |
| 227 | +**Always bind to localhost (`127.0.0.1`) only!** This ensures the WebSocket server is not exposed to the network. |
| 228 | + |
| 229 | +## What's Next? |
| 230 | + |
| 231 | +With this protocol knowledge, you can: |
| 232 | + |
| 233 | +- Build integrations for any editor |
| 234 | +- Create agents that connect to existing IDE extensions |
| 235 | +- Extend the protocol with custom tools |
| 236 | +- Build bridges between different AI assistants and IDEs |
| 237 | + |
| 238 | +The WebSocket MCP variant is currently Claude-specific, but the concepts could be adapted for other AI coding assistants. |
| 239 | + |
| 240 | +## Resources |
| 241 | + |
| 242 | +- [MCP Specification](https://spec.modelcontextprotocol.io) |
| 243 | +- [Claude Code Neovim Implementation](https://github.com/coder/claudecode.nvim) |
| 244 | +- [Official VS Code Extension](https://github.com/anthropic-labs/vscode-mcp) (minified source) |
0 commit comments