Skip to content

Commit b40d14c

Browse files
committed
fix(extensions): handle empty tool responses better
1 parent 6446531 commit b40d14c

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

lua/mcphub/extensions/avante.lua

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,13 @@ function M.mcp_tool()
9191
},
9292
callback = function(result, err)
9393
--result has .text and .images [{mimeType, data}]
94-
on_complete(result.text, err)
94+
local text = result and result.text or ""
95+
if not text or text == "" then
96+
local empty_response =
97+
string.format("`%s` successfull. `%s` returned no text.", params.action, params.uri)
98+
text = empty_response
99+
end
100+
on_complete(text, err)
95101
end,
96102
})
97103
elseif params.action == "use_mcp_tool" then
@@ -105,7 +111,16 @@ function M.mcp_tool()
105111
if result.error then
106112
on_complete(nil, result.error)
107113
else
108-
on_complete(result.text, err)
114+
local text = result and result.text or ""
115+
if not text or text == "" then
116+
local empty_response = string.format(
117+
"`%s` successfull. `%s` returned no text.",
118+
params.action,
119+
params.tool_name
120+
)
121+
text = empty_response
122+
end
123+
on_complete(text, err)
109124
end
110125
end,
111126
})

lua/mcphub/extensions/codecompanion/utils.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ function M.create_output_handlers(action_name, has_function_calling, opts)
150150
local result = has_function_calling and stdout[#stdout] or cmd[#cmd]
151151
agent = has_function_calling and agent or self
152152
-- Show text content if present
153+
local tool_call_result_added = false
153154
if result.text and result.text ~= "" then
154155
local to_llm = string.format(
155156
[[**`%s` Tool**: Returned the following:
@@ -161,13 +162,12 @@ function M.create_output_handlers(action_name, has_function_calling, opts)
161162
result.text
162163
)
163164
add_tool_output(action_name, self, agent.chat, to_llm, false, has_function_calling, opts)
164-
else
165+
tool_call_result_added = true
166+
end
167+
if not tool_call_result_added then
165168
-- When a tool returns no text content, still send a message to
166169
-- ensure the tool_call_id protocol is satisfied
167-
local to_llm = string.format(
168-
"**`%s` Tool**: Completed with no output",
169-
action_name
170-
)
170+
local to_llm = string.format("**`%s` Tool**: Completed with no output", action_name)
171171
add_tool_output(action_name, self, agent.chat, to_llm, false, has_function_calling, opts)
172172
end
173173
-- TODO: Add image support when codecompanion supports it

0 commit comments

Comments
 (0)