From 5485b40d140501480cbf28dd380b3a54bfd37bb7 Mon Sep 17 00:00:00 2001 From: Riz Ashraf Date: Thu, 8 Oct 2026 23:19:47 +0100 Subject: [PATCH] feat(nvim-core): make nvim_execute_lua read-only to enforce specialized tool usage --- nvim-core/src/lib.rs | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/nvim-core/src/lib.rs b/nvim-core/src/lib.rs index c7741d7..b3687c7 100644 --- a/nvim-core/src/lib.rs +++ b/nvim-core/src/lib.rs @@ -1868,27 +1868,17 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) { "nvim_execute_lua" => { if let Some(code) = args.get("code").and_then(|v| v.as_str()) { - // BAKE IN: Block interactive prompts that cause server deadlocks + // READ-ONLY ENFORCEMENT: Block any Lua code that attempts to mutate state. let lower_code = code.to_lowercase(); - if lower_code.contains("vim.fn.input") - || lower_code.contains("vim.ui.select") - || lower_code.contains("vim.fn.confirm") - || lower_code.contains("vim.ui.input") + if lower_code.contains("vim.cmd") + || lower_code.contains("nvim_buf_set_lines") + || lower_code.contains("nvim_buf_set_text") + || lower_code.contains("nvim_command") + || lower_code.contains("nvim_set_current") + || lower_code.contains("nvim_win_set") + || lower_code.contains("nvim_buf_set_name") { - send_error(id, -32600, "CRITICAL ERROR: Interactive Lua functions (input, select, confirm) are strictly forbidden via MCP as they will hang the headless server.").await; - return; - } - // Block the 'c' confirm flag in vim.cmd substitutions - if (code.contains("vim.cmd") - || code.contains("vim.api.nvim_command")) - && code.contains("%s") - && (code.contains("gc'") - || code.contains("gc\"") - || code.contains("gc\n") - || code.contains("c'") - || code.contains("c\"")) - { - send_error(id, -32600, "CRITICAL ERROR: The 'c' (confirm) flag in Neovim substitutions is strictly forbidden via MCP as it triggers an interactive prompt that hangs the headless server. Use '/g' or '/ge' instead.").await; + send_error(id, -32600, "CRITICAL ERROR: nvim_execute_lua is restricted to READ-ONLY queries to prevent agents from bypassing the specialized visual tools (nvim_buffer, nvim_visual). Use the designated tools to mutate editor state.").await; return; }