# mcpserver.nvim A Neovim plugin that implements an MCP (Model Context Protocol) server, allowing external MCP clients to interact with Neovim through a Unix socket. ## Features - **get_buffer_content**: Fetch the contents of the current buffer ## Installation Using [lazy.nvim](https://github.com/folke/lazy.nvim): ```lua { dir = "/path/to/mcpserver.nvim", -- Use your local path -- Server starts automatically on VimEnter } ``` ## Usage ### Neovim Plugin #### Manual Control Start the MCP server: ```vim :MCPServerStart ``` Stop the MCP server: ```vim :MCPServerStop ``` #### Get Socket Path ```lua local mcpserver = require('mcpserver') local socket_path = mcpserver.get_socket_path() print(socket_path) -- e.g., "/tmp/nvim-mcp-server-12345.sock" ``` #### Socket Location The server creates a unique Unix socket at `/tmp/nvim-mcp-server-.sock` where `` is the Neovim process ID. This allows multiple Neovim instances to run MCP servers simultaneously. ### Standalone MCP Server For use with external MCP clients that communicate via stdio: ```bash # Auto-detect running Neovim instance lua standalone.lua # Specify Neovim socket path lua standalone.lua /tmp/nvim-mcp-server-12345.sock ``` The standalone server: - Communicates via stdio (JSON-RPC over stdin/stdout) - Auto-detects running Neovim MCP server sockets - Forwards requests to the Neovim instance - Works with any MCP client that supports stdio transport ### Available Tools #### get_buffer_content Returns JSON with: - `filename`: Path to the current buffer file - `content`: Full text content of the buffer - `line_count`: Number of lines in the buffer ## MCP Client Usage ### Direct Socket Connection Connect to the Unix socket at `/tmp/nvim-mcp-server-.sock` and use standard MCP protocol: ```json { "jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": { "name": "get_buffer_content", "arguments": {} } } ``` ### Stdio Connection Use the standalone server for stdio-based MCP clients: ```bash echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"get_buffer_content","arguments":{}}}' | lua standalone.lua ``` ## Development The plugin structure: - `lua/mcpserver/init.lua`: Core MCP server implementation - `lua/mcpserver/tools.lua`: Tool implementations - `plugin/mcpserver.lua`: Plugin setup and commands - `standalone.lua`: Standalone MCP server for stdio communication ## Requirements - Neovim with Lua support - For standalone mode: `netcat` (nc) for Unix socket communication