Skip to content
Closed
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
19 changes: 19 additions & 0 deletions doc/extensions/codecompanion.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ require("codecompanion").setup({
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
format_action = function(action_name, tool)
-- Replace 'use_mcp_tool' with actual tool name and params.
if action_name == 'use_mcp_tool' then
local name = string.format(
'%s/%s',
tool.args.server_name,
tool.args.tool_name
)
local tool_input = vim.deepcopy(tool.args.tool_input)
-- Cut too large params.
if name == 'filesystem/edit_file' then
tool_input.edits = '󰩫'
elseif name == 'filesystem/write_file' then
tool_input.content = '󰩫'
end
local args = vim.inspect(tool_input):gsub('%s+', ' ')
return name .. ' ' .. args
end
end,
}
}
}
Expand Down
23 changes: 16 additions & 7 deletions lua/mcphub/extensions/codecompanion/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function M.create_handler(action_name, has_function_calling, opts)
end

---@param action_name MCPHub.ActionType
---@param tool table
---@param tool CodeCompanion.Agent.Tool
---@param chat any
---@param llm_msg string
---@param is_error boolean
Expand All @@ -106,7 +106,10 @@ local function add_tool_output(action_name, tool, chat, llm_msg, is_error, has_f
tool,
text,
(user_msg or show_result_in_chat or is_error) and (user_msg or text)
or string.format("**`%s` Tool**: Successfully finished", action_name)
or string.format(
"**`%s` Tool**: Successfully finished",
opts.format_action and opts.format_action(action_name, tool) or action_name
)
)
for _, image in ipairs(images) do
helpers.add_image(chat, image)
Expand All @@ -124,7 +127,10 @@ local function add_tool_output(action_name, tool, chat, llm_msg, is_error, has_f
})
chat:add_buf_message({
role = config.constants.USER_ROLE,
content = string.format("I've shared the result of the `%s` tool with you.\n", action_name),
content = string.format(
"I've shared the result of the `%s` tool with you.\n",
opts.format_action and opts.format_action(action_name, tool) or action_name
),
})
end
end
Expand Down Expand Up @@ -154,7 +160,7 @@ function M.create_output_handlers(action_name, has_function_calling, opts)
%s
````
]],
action_name,
opts.format_action and opts.format_action(action_name, self) or action_name,
stderr
)
add_tool_output(action_name, self, agent.chat, err_msg, true, has_function_calling, opts, nil, {})
Expand All @@ -179,7 +185,7 @@ function M.create_output_handlers(action_name, has_function_calling, opts)
````
%s
````]],
action_name,
opts.format_action and opts.format_action(action_name, self) or action_name,
result.text
)
end
Expand All @@ -204,7 +210,7 @@ function M.create_output_handlers(action_name, has_function_calling, opts)
````
%s
````]],
action_name,
opts.format_action and opts.format_action(action_name, self) or action_name,
string.format("%d image%s returned", #result.images, #result.images > 1 and "s" or "")
)
end
Expand All @@ -219,7 +225,10 @@ function M.create_output_handlers(action_name, has_function_calling, opts)
end
end
end
local fallback_to_llm = string.format("**`%s` Tool**: Completed with no output", action_name)
local fallback_to_llm = string.format(
"**`%s` Tool**: Completed with no output",
opts.format_action and opts.format_action(action_name, self) or action_name
)
add_tool_output(
action_name,
self,
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 @@ -12,6 +12,7 @@ local M = {}
---@field make_vars boolean Whether to make variables or not
---@field make_slash_commands boolean Whether to make slash commands or not
---@field show_result_in_chat boolean Whether to show the result in chat or not
---@field format_action fun(action_name: MCPHub.ActionType, tool: CodeCompanion.Agent.Tool): string|nil

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