Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions doc/extensions/codecompanion.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require("codecompanion").setup({
show_server_tools_in_chat = true, -- Show individual tools in chat completion (when make_tools=true)
add_mcp_prefix_to_tool_names = false, -- Add mcp__ prefix (e.g `@mcp__github`, `@mcp__neovim__list_issues`)
show_result_in_chat = true, -- Show tool results directly in chat buffer
format_tool = nil, -- function(tool_name:string, tool: CodeCompanion.Agent.Tool) : string Function to format tool names to show in the chat buffer
-- MCP Resources
make_vars = true, -- Convert MCP resources to #variables for prompts
-- MCP Prompts
Expand Down Expand Up @@ -131,8 +132,8 @@ Then use your custom groups:
If `make_vars = true`, MCP resources become available as variables prefixed with `#`:

```
Fix diagnostics in the file #neovim://diagnostics/current
Analyze the current buffer #neovim:buffer
Fix diagnostics in the file #mcp:neovim://diagnostics/current
Analyze the current buffer #mcp:neovim:buffer
```

*Example: Accessing LSP diagnostics*:
Expand Down
20 changes: 20 additions & 0 deletions doc/mcphub.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2785,6 +2785,26 @@ Register MCP Hub as an extension in your CodeCompanion configuration:
show_server_tools_in_chat = true, -- Show individual tools in chat completion (when make_tools=true)
add_mcp_prefix_to_tool_names = false, -- Add mcp__ prefix (e.g `@mcp__github`, `@mcp__neovim__list_issues`)
show_result_in_chat = true, -- Show tool results directly in chat buffer
format_tool = function(display_name, tool)
local args = vim.deepcopy(tool.args)
-- Replace 'use_mcp_tool' with actual tool name and params.
if name == 'use_mcp_tool' then
name = string.format('%s__%s', args.server_name, args.tool_name)
args = args.tool_input
end

if name == 'filesystem__edit_file' then
if type(args.edits) == 'table' then
args.edits = '󰩫'
end
elseif name == 'filesystem__write_file' then
if type(args.content) == 'string' then
args.content = '󰩫'
end
end

return name .. ' ' .. vim.inspect(args):gsub('%s+', ' ')
end,
-- MCP Resources
make_vars = true, -- Convert MCP resources to #variables for prompts
-- MCP Prompts
Expand Down
19 changes: 11 additions & 8 deletions lua/mcphub/extensions/codecompanion/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function M.execute_mcp_tool(params, agent, output_handler, context)
end

---@param display_name string
---@param tool table
---@param tool CodeCompanion.Agent.Tool
---@param chat any
---@param llm_msg string
---@param is_error boolean
Expand All @@ -111,13 +111,14 @@ local function add_tool_output(
local helpers = require("codecompanion.strategies.chat.helpers")
local show_result_in_chat = opts.show_result_in_chat == true
local text = llm_msg
local formatted_name = opts.format_tool and opts.format_tool(display_name, tool) or display_name

if has_function_calling then
chat:add_tool_output(
tool,
text,
(user_msg or show_result_in_chat or is_error) and (user_msg or text)
or string.format("**`%s` Tool**: Successfully finished", display_name)
or string.format("**`%s` Tool**: Successfully finished", formatted_name)
)
for _, image in ipairs(images) do
helpers.add_image(chat, image)
Expand All @@ -135,7 +136,7 @@ local function add_tool_output(
})
chat:add_buf_message({
role = config.constants.USER_ROLE,
content = string.format("I've shared the result of the `%s` tool with you.\n", display_name),
content = string.format("I've shared the result of the `%s` tool with you.\n", formatted_name),
})
end
end
Expand All @@ -159,14 +160,15 @@ function M.create_output_handlers(display_name, has_function_calling, opts)
---@diagnostic disable-next-line: cast-local-type
stderr = vim.inspect(stderr)
end
local formatted_name = opts.format_tool and opts.format_tool(display_name, self) or display_name
local err_msg = string.format(
[[**`%s` Tool**: Failed with the following error:

````
%s
````
]],
display_name,
formatted_name,
stderr
)
add_tool_output(display_name, self, agent.chat, err_msg, true, has_function_calling, opts, nil, {})
Expand All @@ -182,6 +184,7 @@ function M.create_output_handlers(display_name, has_function_calling, opts)
local result = has_function_calling and stdout[#stdout] or cmd[#cmd]
---@diagnostic disable-next-line: cast-local-type
agent = has_function_calling and agent or self
local formatted_name = opts.format_tool and opts.format_tool(display_name, self) or display_name
local to_llm = nil
local to_user = nil
local images = {}
Expand All @@ -193,7 +196,7 @@ function M.create_output_handlers(display_name, has_function_calling, opts)
````
%s
````]],
display_name,
formatted_name,
result.text
)
end
Expand All @@ -216,7 +219,7 @@ function M.create_output_handlers(display_name, has_function_calling, opts)
````
%s
````]],
display_name,
formatted_name,
string.format("%d image%s returned", #result.images, #result.images > 1 and "s" or "")
)
end
Expand All @@ -233,9 +236,9 @@ function M.create_output_handlers(display_name, has_function_calling, opts)
end
end

local fallback_to_llm = string.format("**`%s` Tool**: Completed with no output", display_name)
local fallback_to_llm = string.format("**`%s` Tool**: Completed with no output", formatted_name)
if opts.show_result_in_chat == false and not to_user then
to_user = string.format("**`%s` Tool**: Successfully finished", display_name)
to_user = string.format("**`%s` Tool**: Successfully finished", formatted_name)
end
add_tool_output(
display_name,
Expand Down
1 change: 1 addition & 0 deletions lua/mcphub/extensions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ local M = {}
---@field make_tools boolean Whether to make individual tools and server groups or not
---@field show_server_tools_in_chat boolean Whether to show all tools in cmp or not
---@field show_result_in_chat boolean Whether to show the result in chat or not
---@field format_tool function(tool_name: string, tool: CodeCompanion.Agent.Tool): string

---@class MCPHub.Extensions.Config
---@field avante MCPHub.Extensions.AvanteConfig Configuration for the Avante extension
Expand Down
Loading