Skip to content

Conversation

@ravitemer
Copy link
Owner

@ravitemer ravitemer commented Jun 17, 2025

Description

This PR introduces a significant improvement to the MCPHub-CodeCompanion integration that addresses previous limitations and provides much more flexibility and control over your MCP tools!

Previous Limitations with @mcp

The existing @mcp tool worked, but had some drawbacks:

  • System prompt pollution: All MCP servers were dumped into the system prompt
  • Model compatibility issues: Some models struggle with schema-heavy system prompts
  • Manual server management: Required toggling servers on/off before use
  • Lack of precision: No way to target specific tools or create focused workflows

✨ New: Granular Tool Access

This PR introduces granular tool access that creates proper CodeCompanion function tools for each MCP server and tool:

🎯 Server Groups

Access all tools from a specific server:

@github Handle this entire PR workflow
@neovim Manage all file operations  
@stagehand Automate web interactions
@fetch Gather information from various URLs

🔧 Individual Tools

Target specific functionality with namespaced tools:

@neovim__read_file Show me the configuration
@github__create_issue File a bug report
@github__create_pull_request Submit this fix
@fetch__fetch Get content from this webpage

🛠️ Custom Tool Combinations

Create specialized workflows by mixing tools from different servers:

require("codecompanion").setup({
  strategies = {
    chat = {
      tools = {
        groups = {
          ["github_pr_workflow"] = {
            description = "GitHub operations from issue to PR",
            tools = {
              -- File operations
              "neovim__read_multiple_files", "neovim__write_file", "neovim__replace_in_file",
              -- GitHub operations
              "github__create_issue", "github__create_pull_request", "github__get_file_contents",
              "github__create_or_update_file", "github__list_issues", "github__search_code"
            },
          },
        },
      },
    },
  },
  extensions = {
    mcphub = {
      callback = "mcphub.extensions.codecompanion",
      opts = {
        make_tools = true,  -- Required for individual tools
        -- ... other options
      }
    }
  }
})

Then use your custom groups:

@github_pr_workflow Fix this bug, create tests, and submit a PR with proper documentation

🎛️ Fine-Grained Auto-Approval

One standout feature is per-tool auto-approval control:

  • Configure which tools run automatically vs. require confirmation from the Hub UI
  • Perfect for safe operations (read_multiple_files, search_code) while protecting dangerous ones (delete_items, execute_command)

🚀 Benefits

Better Performance

  • No system prompt pollution with unrelated server info
  • Direct function calls instead of complex schema parsing
  • Improved model compatibility

Enhanced Precision

  • Target exactly the tools you need
  • Create focused workflows for specific tasks
  • Mix MCP tools with CodeCompanion's built-in tools seamlessly

Improved Workflow

  • No manual server toggling required
  • Start your MCP servers once and access tools as needed
  • Create reusable tool combinations for common workflows

⚠️ Note

Some older OpenAI models have character limits for function descriptions. If you encounter issues:

  1. Use more capable models (GPT-4, Claude, etc.)
  2. Fall back to @mcp for specific cases

🚀 Get Started

Use the make_tools branch:

  {
    "ravitemer/mcphub.nvim",
    branch = "make-tools",
    build = "npm i -g mcp-hub@latest",
    config = function()
      require("mcphub").setup({})
    end
  },

No breaking changes! The traditional @mcp tool continues to work exactly as before. The new granular tools are additive features that enhance your workflow without disrupting existing usage. The following is the default config. Just update your MCPHub plugin and start using the new tool access methods.

require("codecompanion").setup({
  extensions = {
    mcphub = {
      callback = "mcphub.extensions.codecompanion",
      opts = {
        make_tools = true,               -- Enable individual tools and server groups (default: true)
        show_server_tools_in_chat = true, -- Show individual tools in completion
        add_mcp_prefix_to_tool_names = false, -- Adds `mcp__` prefix e.g `mcp__github__get_issue`
        show_result_in_chat = true,      -- Show tool results directly in chat
        make_vars = true,                -- Convert MCP resources to #variables  
        make_slash_commands = true,      -- Add MCP prompts as /slash commands
      }
    }
  }
})
  1. Update to the latest MCPHub version
  2. Discover available tools with @ completion in CodeCompanion chat
  3. Set show_server_tools_in_chat = false if you prefer to see only server groups (@github) in completion, hiding individual tools (@github__create_issue)
  4. Configure auto-approval for your trusted tools via the UI

…access

- Split monolithic utils.lua into modular architecture:
  - core.lua: execution logic and output handling
  - tools.lua: static/dynamic tool management with conflict detection
  - variables.lua: MCP resources as variables
  - slash_commands.lua: MCP prompts as slash commands

- Add granular tool access options:
  - Individual tools: @server__tool_name
  - Server groups: @server_name
  - Unique safe naming for servers (handles conflicts like server/one,
server:two)
  - Conflict detection and warnings for existing tools/groups

- Add make_tools config option, opt-out of showing all the server tools
with  `show_server_tools_in_chat`
- Update syntax highlighting for new tool structure
@ravitemer ravitemer marked this pull request as draft June 17, 2025 11:05
@ravitemer ravitemer changed the title Codecompanion: Make MCP Servers to function tools Codecompanion: Make MCP Servers into function tools Jun 17, 2025
@ravitemer
Copy link
Owner Author

There is a bug in codecompanoin for which a PR olimorris/codecompanion.nvim#1693 has been opened. Everything should work fine once it is merged.

@ravitemer
Copy link
Owner Author

Variables and Slash Commands are name spaced by default with mcp: prefix.

image

image

For tools, you can opt in with add_mcp_prefix_to_tool_names option which adds mcp__ to all groups and tools names from MCP servers

image

@powerman
Copy link
Contributor

Works great for me!

ravitemer and others added 3 commits July 14, 2025 02:58
@ravitemer ravitemer marked this pull request as ready for review July 14, 2025 07:41
@ravitemer
Copy link
Owner Author

ravitemer commented Jul 14, 2025

fix-issue.mp4

@ravitemer ravitemer merged commit 7bbb9c1 into main Jul 14, 2025
5 checks passed
@ravitemer ravitemer deleted the make-tools branch July 14, 2025 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants