Skip to content

Commit c0b9b32

Browse files
committed
feat: implement MCP tool system with diff integration and comprehensive test coverage
Change-Id: If27e0783e938f4c791c7f9190f897ef25e6ee5b0 Signed-off-by: Thomas Kosiewski <[email protected]>
1 parent fe03dd0 commit c0b9b32

19 files changed

+3484
-126
lines changed

.github/workflows/claude.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ jobs:
2525
id-token: write
2626
steps:
2727
- name: Checkout repository
28-
uses: actions/checkout@v4
28+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2929
with:
3030
fetch-depth: 1
31+
persist-credentials: false
3132

3233
- name: Run Claude Code
3334
id: claude
34-
uses: anthropics/claude-code-action@beta
35+
uses: anthropics/claude-code-action@8e84799f37d42f24e0ebae41205346879bdcab5a # v0.0.7/beta
3536
with:
3637
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}

ARCHITECTURE.md

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,27 @@ The lock file system enables Claude CLI to discover the Neovim integration:
7070
}
7171
```
7272

73-
### 3. MCP Tool Implementation
73+
### 3. MCP Tool System
7474

75-
The plugin implements tools that Claude can invoke:
75+
The plugin implements a dynamic tool system following the Model Context Protocol 2025-03-26 specification. Tools are registered with both handlers and JSON schemas:
7676

77-
- File operations (open, save, check status)
78-
- Editor information (diagnostics, open editors)
79-
- Selection management
80-
- Diff viewing
77+
**MCP-Exposed Tools:**
78+
79+
- `openFile` - Opens files with optional line/text selection
80+
- `getCurrentSelection` - Gets current text selection
81+
- `getOpenEditors` - Lists currently open files
82+
- `openDiff` - Opens native Neovim diff views for file comparisons
83+
84+
**Internal Tools** (not exposed via MCP):
85+
86+
- `getDiagnostics`, `getWorkspaceFolders`, `saveDocument`, etc.
87+
88+
**Tool Architecture:**
89+
90+
- Centralized registration with `M.register(name, schema, handler)`
91+
- Dynamic tool list generation via `M.get_tool_list()`
92+
- Schema validation and JSON-RPC parameter handling
93+
- Automatic MCP exposure based on schema presence
8194

8295
Each tool follows a request/response pattern:
8396

@@ -95,7 +108,62 @@ Claude CLI Neovim Plugin
95108
│ │
96109
```
97110

98-
### 4. Selection Tracking
111+
### 4. Diff Integration System
112+
113+
The plugin provides a configurable diff system that Claude can use to show file changes:
114+
115+
**Diff Providers:**
116+
117+
- `native` - Uses Neovim's built-in diff mode with `diffthis`
118+
- `auto` - Automatically selects the best available provider
119+
- `diffview` - (Future) Integration with diffview.nvim plugin
120+
121+
**Diff Configuration:**
122+
123+
```lua
124+
diff_opts = {
125+
auto_close_on_accept = true, -- Auto-close when accepting changes
126+
show_diff_stats = true, -- Show diff statistics
127+
vertical_split = true, -- Use vertical split for diff view
128+
open_in_current_tab = true, -- Open in current tab (reduces clutter)
129+
}
130+
```
131+
132+
**Native Diff Features:**
133+
134+
- Current-tab mode (default) - opens diff in current tab to reduce clutter
135+
- Helpful keymaps in current-tab mode:
136+
- `<leader>dq` - Exit diff mode and cleanup
137+
- `<leader>da` - Accept all changes
138+
- `]c` / `[c` - Navigate between changes (standard Neovim)
139+
- Automatic temporary file cleanup
140+
- Configurable split orientation (vertical/horizontal)
141+
142+
**Diff Flow:**
143+
144+
```
145+
Claude Request ──► openDiff MCP tool ──► diff.lua provider
146+
147+
148+
┌─────────────────┐
149+
│ Create temp file│
150+
│ with new content│
151+
└─────────────────┘
152+
153+
154+
┌─────────────────┐
155+
│ Open original │
156+
│ file in editor │
157+
└─────────────────┘
158+
159+
160+
┌─────────────────┐
161+
│ Create split & │
162+
│ enable diffthis │
163+
└─────────────────┘
164+
```
165+
166+
### 5. Selection Tracking
99167

100168
The plugin monitors text selections in Neovim:
101169

@@ -105,7 +173,7 @@ The plugin monitors text selections in Neovim:
105173
- Sends updates to Claude via WebSocket
106174
- Supports sending `at_mentioned` notifications for visual selections using the `:ClaudeCodeSend` command, providing focused context to Claude.
107175

108-
### 5. Terminal Integration
176+
### 6. Terminal Integration
109177

110178
The plugin provides a dedicated terminal interface for Claude Code CLI:
111179

@@ -123,7 +191,7 @@ The plugin provides a dedicated terminal interface for Claude Code CLI:
123191
└─────────────┘ └─────────────────┘ └─────────────┘
124192
```
125193

126-
### 6. Environment Integration
194+
### 7. Environment Integration
127195

128196
The plugin manages the environment for Claude CLI:
129197

@@ -186,7 +254,8 @@ lua/claudecode/
186254
│ └── mock.lua # Mock server for testing
187255
├── lockfile.lua # Lock file management
188256
├── tools/
189-
│ └── init.lua # Tool registration and dispatch
257+
│ └── init.lua # MCP tool registration, schema management, and dispatch
258+
├── diff.lua # Diff provider system (native Neovim diff support)
190259
├── selection.lua # Selection tracking and notifications
191260
├── terminal.lua # Terminal management (Snacks.nvim or native)
192261
└── meta/

CLAUDE.md

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,37 @@ The plugin follows a modular architecture with these main components:
3333
- Creates and manages lock files at `~/.claude/ide/[port].lock`
3434
- Enables Claude CLI to discover the Neovim integration
3535

36-
3. **Selection Tracking** (`lua/claudecode/selection.lua`)
36+
3. **MCP Tool System** (`lua/claudecode/tools/init.lua`)
37+
38+
- Dynamic tool registration with schema validation
39+
- Implements openFile, openDiff, getCurrentSelection, getOpenEditors
40+
- Follows Model Context Protocol 2025-03-26 specification
41+
- Centralized tool definitions and automatic MCP exposure
42+
43+
4. **Diff Integration** (`lua/claudecode/diff.lua`)
44+
45+
- Native Neovim diff support with configurable options
46+
- Current-tab mode (default) to reduce tab clutter
47+
- Helpful keymaps: `<leader>dq` (exit), `<leader>da` (accept all)
48+
- Automatic temporary file cleanup
49+
50+
5. **Selection Tracking** (`lua/claudecode/selection.lua`)
3751

3852
- Monitors text selections and cursor position in Neovim
3953
- Sends updates to Claude via WebSocket
4054

41-
4. **Configuration** (`lua/claudecode/config.lua`)
55+
6. **Terminal Integration** (`lua/claudecode/terminal.lua`)
56+
57+
- Supports both Snacks.nvim and native Neovim terminals
58+
- Vertical split terminal with configurable positioning
59+
- Commands: `:ClaudeCode`, `:ClaudeCodeOpen`, `:ClaudeCodeClose`
60+
61+
7. **Configuration** (`lua/claudecode/config.lua`)
4262

4363
- Handles user configuration validation and merging with defaults
64+
- Includes diff provider and terminal configuration
4465

45-
5. **Main Plugin Entry** (`lua/claudecode/init.lua`)
66+
8. **Main Plugin Entry** (`lua/claudecode/init.lua`)
4667
- Exposes setup and control functions
4768
- Manages plugin lifecycle
4869

@@ -54,8 +75,11 @@ The plugin is in beta stage with:
5475
- Complete WebSocket server with RFC 6455 compliance
5576
- Enhanced selection tracking with multi-mode support
5677
- Lock file management implemented
57-
- MCP tool framework implemented
58-
- Comprehensive test suite (55 tests passing)
78+
- Complete MCP tool framework with dynamic registration
79+
- Core MCP tools: openFile, openDiff, getCurrentSelection, getOpenEditors
80+
- Native Neovim diff integration with configurable options
81+
- Terminal integration (Snacks.nvim and native support)
82+
- Comprehensive test suite (55+ tests passing)
5983

6084
## Testing Approach
6185

@@ -73,7 +97,7 @@ The project uses the Busted testing framework:
7397

7498
Current priorities for development are:
7599

76-
1. Enhancing MCP tools with additional file operations and editor features
100+
1. Implementing diffview.nvim integration for the diff provider system
77101
2. Adding Neovim-specific tools (LSP integration, diagnostics, Telescope)
78102
3. Performance optimization for large codebases
79103
4. Integration testing with real Claude Code CLI
@@ -109,10 +133,11 @@ Current priorities for development are:
109133

110134
The plugin provides these user-facing commands:
111135

112-
- `:ClaudeCodeStart` - Start the Claude Code integration
113-
- `:ClaudeCodeStop` - Stop the integration
114-
- `:ClaudeCodeStatus` - Show connection status
115-
- `:ClaudeCodeSend` - Send current selection to Claude
136+
- `:ClaudeCode` - Toggle the Claude Code interactive terminal
137+
- `:ClaudeCodeOpen` - Open/focus the Claude Code terminal
138+
- `:ClaudeCodeClose` - Close the Claude Code terminal
139+
- `:ClaudeCodeSend` - Send current selection to Claude as at-mentioned context
140+
- `:ClaudeCodeStatus` - Show connection status (via Lua API)
116141

117142
## Debugging
118143

@@ -135,12 +160,29 @@ require("claudecode").setup({
135160
auto_start = true,
136161

137162
-- Custom terminal command to use when launching Claude
138-
terminal_cmd = nil, -- e.g., "toggleterm"
163+
terminal_cmd = nil, -- e.g., "claude --project-foo"
139164

140165
-- Log level (trace, debug, info, warn, error)
141166
log_level = "info",
142167

143168
-- Enable sending selection updates to Claude
144169
track_selection = true,
170+
171+
-- Diff provider configuration for openDiff MCP tool
172+
diff_provider = "auto", -- "auto", "native", or "diffview"
173+
diff_opts = {
174+
auto_close_on_accept = true, -- Auto-close diff when accepting changes
175+
show_diff_stats = true, -- Show diff statistics
176+
vertical_split = true, -- Use vertical split for diff view
177+
open_in_current_tab = true, -- Open diff in current tab (reduces clutter)
178+
},
179+
180+
-- Terminal configuration
181+
terminal = {
182+
split_side = "right", -- "left" or "right"
183+
split_width_percentage = 0.30, -- 0.0 to 1.0
184+
provider = "snacks", -- "snacks" or "native"
185+
show_native_term_exit_tip = true, -- Show tip for Ctrl-\\ Ctrl-N
186+
},
145187
}
146188
```

DEVELOPMENT.md

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ claudecode.nvim/
99
├── .github/workflows/ # CI workflow definitions
1010
├── lua/claudecode/ # Plugin implementation
1111
│ ├── server/ # WebSocket server implementation
12-
│ ├── tools/ # MCP tool implementations
12+
│ ├── tools/ # MCP tool implementations and schema management
1313
│ ├── config.lua # Configuration management
14+
│ ├── diff.lua # Diff provider system (native Neovim support)
1415
│ ├── init.lua # Plugin entry point
1516
│ ├── lockfile.lua # Lock file management
16-
│ └── selection.lua # Selection tracking
17+
│ ├── selection.lua # Selection tracking
18+
│ └── terminal.lua # Terminal management
1719
├── plugin/ # Plugin loader
1820
├── tests/ # Test suite
1921
│ ├── unit/ # Unit tests
@@ -27,25 +29,28 @@ claudecode.nvim/
2729

2830
## Core Components Implementation Status
2931

30-
| Component | Status | Priority | Notes |
31-
| ---------------------- | ---------- | -------- | ---------------------------------------- |
32-
| Basic plugin structure | ✅ Done | - | Initial setup complete |
33-
| Configuration system | ✅ Done | - | Support for user configuration |
34-
| WebSocket server | ✅ Done | - | Pure Lua RFC 6455 compliant |
35-
| Lock file management | ✅ Done | - | Basic implementation complete |
36-
| Selection tracking | ✅ Done | - | Enhanced with multi-mode support |
37-
| MCP tools | 🚧 Started | Medium | Basic framework, need more tools |
38-
| Tests | ✅ Done | - | 56 tests passing, comprehensive coverage |
39-
| CI pipeline | ✅ Done | - | GitHub Actions configured |
40-
| Documentation | ✅ Done | - | Complete documentation |
32+
| Component | Status | Priority | Notes |
33+
| ---------------------- | ------- | -------- | ------------------------------------------------------- |
34+
| Basic plugin structure | ✅ Done | - | Initial setup complete |
35+
| Configuration system | ✅ Done | - | Support for user configuration |
36+
| WebSocket server | ✅ Done | - | Pure Lua RFC 6455 compliant |
37+
| Lock file management | ✅ Done | - | Basic implementation complete |
38+
| Selection tracking | ✅ Done | - | Enhanced with multi-mode support |
39+
| MCP tools framework | ✅ Done | - | Dynamic tool registration and schema system |
40+
| Core MCP tools | ✅ Done | - | openFile, openDiff, getCurrentSelection, getOpenEditors |
41+
| Diff integration | ✅ Done | - | Native Neovim diff with configurable options |
42+
| Terminal integration | ✅ Done | - | Snacks.nvim and native terminal support |
43+
| Tests | ✅ Done | - | 55+ tests passing, comprehensive coverage |
44+
| CI pipeline | ✅ Done | - | GitHub Actions configured |
45+
| Documentation | ✅ Done | - | Complete documentation |
4146

4247
## Development Priorities
4348

44-
1. **MCP Tool Enhancement**
49+
1. **Advanced MCP Tools**
4550

46-
- Implement additional tools from the findings document
47-
- Add Neovim-specific tools (LSP, diagnostics, Telescope integration)
48-
- Enhance existing tool implementations
51+
- Add Neovim-specific tools (LSP integration, diagnostics, Telescope integration)
52+
- Implement diffview.nvim integration for the diff provider system
53+
- Add Git integration tools (branch info, status, etc.)
4954

5055
2. **Performance Optimization**
5156

@@ -127,18 +132,35 @@ The WebSocket server is implemented in pure Lua with zero external dependencies:
127132
- **Security**: Pure Lua SHA-1 implementation for WebSocket handshake
128133
- **Performance**: Optimized with lookup tables and efficient algorithms
129134

130-
### Custom Tools
135+
### MCP Tool System
131136

132-
Custom tools beyond the basic VS Code implementation could include:
137+
The plugin implements a sophisticated tool system following MCP 2025-03-26:
138+
139+
**Current Tools:**
140+
141+
- `openFile` - Opens files with optional line/text selection
142+
- `openDiff` - Native Neovim diff views with configurable options
143+
- `getCurrentSelection` - Gets current text selection
144+
- `getOpenEditors` - Lists currently open files
145+
146+
**Tool Architecture:**
147+
148+
- Dynamic registration: `M.register(name, schema, handler)`
149+
- Automatic MCP exposure based on schema presence
150+
- JSON schema validation for parameters
151+
- Centralized tool definitions in `tools/init.lua`
152+
153+
**Future Tools:**
133154

134155
- Neovim-specific diagnostics
135-
- LSP integration
156+
- LSP integration (hover, references, definitions)
136157
- Telescope integration for file finding
137-
- Git integration
158+
- Git integration (status, branch info, blame)
138159

139160
## Next Steps
140161

141-
1. Enhance MCP tool implementations with Neovim-specific features
142-
2. Add integration tests with real Claude Code CLI
143-
3. Optimize performance for large codebases
144-
4. Create example configurations for popular Neovim setups (LazyVim, NvChad, etc.)
162+
1. Implement diffview.nvim integration for the diff provider system
163+
2. Add advanced MCP tools (LSP integration, Telescope, Git)
164+
3. Add integration tests with real Claude Code CLI
165+
4. Optimize performance for large codebases
166+
5. Create example configurations for popular Neovim setups (LazyVim, NvChad, etc.)

0 commit comments

Comments
 (0)