* Callback function for MCP tool execution * @return The tool response content, extracted from the utilBuildChatflow result
(
chatflow: ChatFlow,
chatId: string,
req: Request,
args: any
)
| 182 | * @return The tool response content, extracted from the utilBuildChatflow result |
| 183 | */ |
| 184 | async function chatflowCallback( |
| 185 | chatflow: ChatFlow, |
| 186 | chatId: string, |
| 187 | req: Request, |
| 188 | args: any |
| 189 | ): Promise<{ content: { type: 'text'; text: string }[]; isError?: boolean }> { |
| 190 | const inputType = getToolInputType(chatflow) |
| 191 | const body = inputType === 'form' ? { form: args.form || {} } : { question: args.question || '' } |
| 192 | const mockReq = createMockRequest({ |
| 193 | chatflowId: chatflow.id, |
| 194 | body: { |
| 195 | ...body, |
| 196 | chatId |
| 197 | }, |
| 198 | sourceRequest: req |
| 199 | }) |
| 200 | |
| 201 | const result = await utilBuildChatflow(mockReq, true, ChatType.MCP) |
| 202 | |
| 203 | // Extract the text response from the result |
| 204 | let textContent: string |
| 205 | if (typeof result === 'string') { |
| 206 | textContent = result |
| 207 | } else if (result?.text) { |
| 208 | textContent = result.text |
| 209 | } else if (result?.json) { |
| 210 | textContent = JSON.stringify(result.json) |
| 211 | } else { |
| 212 | textContent = JSON.stringify(result) |
| 213 | } |
| 214 | |
| 215 | return { |
| 216 | content: [{ type: 'text' as const, text: textContent }] |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Handle an InternalFlowiseError from getChatflowByIdAndVerifyToken. |
no test coverage detected