Skip to content

CodeCompanion Integration

Add MCP capabilities to CodeCompanion.nvim by including the MCP in your setup:

Features

  • Access MCP tools via the @mcp tool in the chat buffer.
  • Utilize MCP resources as context variables using the # prefix (e.g., #resource_name).
  • Execute MCP prompts directly using /mcp:prompt_name slash commands.
  • Receive real-time updates in CodeCompanion when MCP servers change.

MCP Hub Extension

Register MCP Hub as an extension in your CodeCompanion configuration:

lua
require("codecompanion").setup({
  extensions = {
    mcphub = {
      callback = "mcphub.extensions.codecompanion",
      opts = {
        show_result_in_chat = true,  -- Show mcp tool results in chat
        make_vars = true,            -- Convert resources to #variables
        make_slash_commands = true,  -- Add prompts as /slash commands
      }
    }
  }
})

Usage

Once configured, you can interact with MCP Hub within the CodeCompanion chat buffer:

  • Tool Access: Type @mcp to add available MCP servers to the system prompt, enabling the LLM to use registered MCP tools.
  • Resources as Variables: If make_vars = true, MCP resources become available as variables prefixed with #. You can include these in your prompts (e.g., Summarize the issues in #mcp:lsp:get_diagnostics):

Example: Accessing LSP diagnostics:

image

Prompts as Slash Commands: If make_slash_commands = true, MCP prompts are available as slash commands (e.g., /mcp:prompt_name). Arguments are handled via vim.ui.input.

Example: Using an MCP prompt via slash command:

image

Auto-Approval

By default, whenever codecompanion calls use_mcp_tool or access_mcp_resource tool, it shows a confirm dialog with tool name, server name and arguments.

Image

  1. You can set auto_approve to true to automatically approve MCP tool calls without user confirmation.
lua
require("mcphub").setup({
    -- This sets vim.g.mcphub_auto_approve to true by default (can also be toggled from the HUB UI with `ga`)
    auto_approve = true, 
})

This also sets vim.g.mcphub_auto_approve variable to true. You can also toggle this option in the MCP Hub UI with ga keymap. You can see the current auto approval status in the Hub UI.

Image

  1. MCP Hub also respects CodeCompanion auto tool mode: vim.g.codecompanion_auto_tool_mode = true (toggled via gta in the chat buffer)

Released under the MIT License.