({
action,
overrides,
customToolDefinitions,
cwd,
fs,
env,
}: {
action: ServerAction<'tool-call-request'>
overrides: NonNullable<CodebuffClientOptions['overrideTools']>
customToolDefinitions: Record<string, CustomToolDefinition>
cwd?: string
fs: CodebuffFileSystem
env?: Record<string, string>
})
| 612 | } |
| 613 | |
| 614 | async function handleToolCall({ |
| 615 | action, |
| 616 | overrides, |
| 617 | customToolDefinitions, |
| 618 | cwd, |
| 619 | fs, |
| 620 | env, |
| 621 | }: { |
| 622 | action: ServerAction<'tool-call-request'> |
| 623 | overrides: NonNullable<CodebuffClientOptions['overrideTools']> |
| 624 | customToolDefinitions: Record<string, CustomToolDefinition> |
| 625 | cwd?: string |
| 626 | fs: CodebuffFileSystem |
| 627 | env?: Record<string, string> |
| 628 | }): Promise<{ output: ToolResultOutput[] }> { |
| 629 | const toolName = action.toolName |
| 630 | const input = action.input |
| 631 | |
| 632 | // Handle MCP tool calls when mcpConfig is present |
| 633 | if (action.mcpConfig) { |
| 634 | try { |
| 635 | const mcpClientId = await getMCPClient(action.mcpConfig) |
| 636 | const result = await callMCPTool(mcpClientId, { |
| 637 | name: toolName, |
| 638 | arguments: input, |
| 639 | }) |
| 640 | return { output: result } |
| 641 | } catch (error) { |
| 642 | return { |
| 643 | output: [ |
| 644 | { |
| 645 | type: 'json', |
| 646 | value: { |
| 647 | errorMessage: |
| 648 | error instanceof Error ? error.message : String(error), |
| 649 | }, |
| 650 | }, |
| 651 | ], |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | let result: ToolResultOutput[] |
| 657 | if (toolNames.includes(toolName as ToolName)) { |
| 658 | clientToolCallSchema.parse(action) |
| 659 | } else { |
| 660 | const customToolHandler = customToolDefinitions[toolName] |
| 661 | |
| 662 | if (!customToolHandler) { |
| 663 | throw new Error( |
| 664 | `Custom tool handler not found for user input ID ${action.userInputId}`, |
| 665 | ) |
| 666 | } |
| 667 | return { |
| 668 | output: await customToolHandler.execute(action.input), |
| 669 | } |
| 670 | } |
| 671 |
no test coverage detected