@@ -87,7 +87,7 @@ function M.execute_mcp_tool(params, agent, output_handler, context)
8787end
8888
8989--- @param display_name string
90- --- @param tool table
90+ --- @param tool CodeCompanion.Agent.Tool
9191--- @param chat any
9292--- @param llm_msg string
9393--- @param is_error boolean
@@ -111,13 +111,14 @@ local function add_tool_output(
111111 local helpers = require (" codecompanion.strategies.chat.helpers" )
112112 local show_result_in_chat = opts .show_result_in_chat == true
113113 local text = llm_msg
114+ local formatted_name = opts .format_tool and opts .format_tool (display_name , tool ) or display_name
114115
115116 if has_function_calling then
116117 chat :add_tool_output (
117118 tool ,
118119 text ,
119120 (user_msg or show_result_in_chat or is_error ) and (user_msg or text )
120- or string.format (" **`%s` Tool**: Successfully finished" , display_name )
121+ or string.format (" **`%s` Tool**: Successfully finished" , formatted_name )
121122 )
122123 for _ , image in ipairs (images ) do
123124 helpers .add_image (chat , image )
@@ -135,7 +136,7 @@ local function add_tool_output(
135136 })
136137 chat :add_buf_message ({
137138 role = config .constants .USER_ROLE ,
138- content = string.format (" I've shared the result of the `%s` tool with you.\n " , display_name ),
139+ content = string.format (" I've shared the result of the `%s` tool with you.\n " , formatted_name ),
139140 })
140141 end
141142 end
@@ -159,14 +160,15 @@ function M.create_output_handlers(display_name, has_function_calling, opts)
159160 --- @diagnostic disable-next-line : cast-local-type
160161 stderr = vim .inspect (stderr )
161162 end
163+ local formatted_name = opts .format_tool and opts .format_tool (display_name , self ) or display_name
162164 local err_msg = string.format (
163165 [[ **`%s` Tool**: Failed with the following error:
164166
165167````
166168%s
167169````
168170]] ,
169- display_name ,
171+ formatted_name ,
170172 stderr
171173 )
172174 add_tool_output (display_name , self , agent .chat , err_msg , true , has_function_calling , opts , nil , {})
@@ -182,6 +184,7 @@ function M.create_output_handlers(display_name, has_function_calling, opts)
182184 local result = has_function_calling and stdout [# stdout ] or cmd [# cmd ]
183185 --- @diagnostic disable-next-line : cast-local-type
184186 agent = has_function_calling and agent or self
187+ local formatted_name = opts .format_tool and opts .format_tool (display_name , self ) or display_name
185188 local to_llm = nil
186189 local to_user = nil
187190 local images = {}
@@ -193,7 +196,7 @@ function M.create_output_handlers(display_name, has_function_calling, opts)
193196````
194197%s
195198````]] ,
196- display_name ,
199+ formatted_name ,
197200 result .text
198201 )
199202 end
@@ -216,7 +219,7 @@ function M.create_output_handlers(display_name, has_function_calling, opts)
216219````
217220%s
218221````]] ,
219- display_name ,
222+ formatted_name ,
220223 string.format (" %d image%s returned" , # result .images , # result .images > 1 and " s" or " " )
221224 )
222225 end
@@ -233,9 +236,9 @@ function M.create_output_handlers(display_name, has_function_calling, opts)
233236 end
234237 end
235238
236- local fallback_to_llm = string.format (" **`%s` Tool**: Completed with no output" , display_name )
239+ local fallback_to_llm = string.format (" **`%s` Tool**: Completed with no output" , formatted_name )
237240 if opts .show_result_in_chat == false and not to_user then
238- to_user = string.format (" **`%s` Tool**: Successfully finished" , display_name )
241+ to_user = string.format (" **`%s` Tool**: Successfully finished" , formatted_name )
239242 end
240243 add_tool_output (
241244 display_name ,
0 commit comments