Skip to content

Merge 0.3.1 update #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Dependencies
node_modules/
package-lock.json

# Build output
build/
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ Using this simple set of tools, Claude can peer into your neovim session to answ
- Error handling could be better.
- Sometimes Claude doesn't get the vim command input just right.

## Configuration

### Environment Variables

- `ALLOW_SHELL_COMMANDS`: Set to 'true' to enable shell command execution (e.g. `!ls`). Defaults to false for security.

## Usage with Claude Desktop
Add this to your `claude_desktop_config.json`:
```json
Expand All @@ -72,7 +78,10 @@ Add this to your `claude_desktop_config.json`:
"args": [
"-y",
"mcp-neovim-server"
]
],
"env": {
"ALLOW_SHELL_COMMANDS": "true"
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mcp-neovim-server",
"version": "0.3.0",
"version": "0.3.1",
"description": "An MCP server for neovim",
"type": "module",
"bin": {
Expand Down
22 changes: 18 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { NeovimManager } from "./neovim.js";
const server = new Server(
{
name: "mcp-neovim-server",
version: "0.3.0",
version: "0.3.1"
},
{
capabilities: {
Expand Down Expand Up @@ -99,13 +99,13 @@ const VIM_BUFFER: Tool = {

const VIM_COMMAND: Tool = {
name: "vim_command",
description: "Send a command to VIM for navigation, spot editing, and line deletion.",
description: "Send a command to VIM for navigation, spot editing, and line deletion. For shell commands like ls, use without the leading colon (e.g. '!ls' not ':!ls').",
inputSchema: {
type: "object",
properties: {
command: {
type: "string",
description: "Neovim command to enter for navigation and spot editing. Insert <esc> to return to NORMAL mode. It is possible to send multiple commands separated with <cr>."
description: "Neovim command to enter for navigation and spot editing. For shell commands use without leading colon (e.g. '!ls'). Insert <esc> to return to NORMAL mode. It is possible to send multiple commands separated with <cr>."
}
},
required: ["command"]
Expand Down Expand Up @@ -329,10 +329,24 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {

async function handleCommand(command: string) {
console.error(`Executing command: ${command}`);

// Check if this is a shell command
if (command.startsWith('!')) {
const allowShellCommands = process.env.ALLOW_SHELL_COMMANDS === 'true';
if (!allowShellCommands) {
return {
content: [{
type: "text",
text: "Shell command execution is disabled. Set ALLOW_SHELL_COMMANDS=true environment variable to enable shell commands."
}]
};
}
}

const result = await neovimManager.sendCommand(command);
return {
content: [{
type: "text",
type: "text",
text: result
}]
};
Expand Down
32 changes: 27 additions & 5 deletions src/neovim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,34 @@ export class NeovimManager {
public async sendCommand(command: string): Promise<string> {
try {
const nvim = await this.connect();

// Remove leading colon if present
const normalizedCommand = command.startsWith(':') ? command.substring(1) : command;

// Handle shell commands (starting with !)
if (normalizedCommand.startsWith('!')) {
if (process.env.ALLOW_SHELL_COMMANDS !== 'true') {
return 'Shell command execution is disabled. Set ALLOW_SHELL_COMMANDS=true environment variable to enable shell commands.';
}

try {
const shellCommand = normalizedCommand.substring(1).trim();
// Execute the command and capture output directly
const output = await nvim.eval(`system('${shellCommand.replace(/'/g, "''")}')`);
if (output) {
return String(output).trim();
}
return 'No output from command';
} catch (error) {
console.error('Shell command error:', error);
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
return `Error executing shell command: ${errorMessage}`;
}
}

// For regular Vim commands
await nvim.setVvar('errmsg', '');
await nvim.feedKeys(
await nvim.replaceTermcodes(command + '<cr>', true, true, true),
'n',
false
);
await nvim.command(normalizedCommand);

const vimerr = await nvim.getVvar('errmsg');
if (vimerr) {
Expand Down