MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / handleToolCall

Function handleToolCall

sdk/src/run.ts:614–764  ·  view source on GitHub ↗
({
  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>
})

Source from the content-addressed store, hash-verified

612}
613
614async 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

Callers 1

runOnceFunction · 0.70

Calls 11

getMCPClientFunction · 0.90
callMCPToolFunction · 0.90
changeFileFunction · 0.90
applyPatchToolFunction · 0.90
runTerminalCommandFunction · 0.90
readUrlFunction · 0.90
codeSearchFunction · 0.90
listDirectoryFunction · 0.90
globFunction · 0.90
requireCwdFunction · 0.85
parseMethod · 0.80

Tested by

no test coverage detected