MCPcopy
hub / github.com/CopilotKit/CopilotKit / executeSpecificTool

Method executeSpecificTool

packages/core/src/core/run-handler.ts:600–653  ·  view source on GitHub ↗

* Execute a specific tool

(
    tool: FrontendTool<any>,
    toolCall: ToolCall,
    message: Message,
    agent: AbstractAgent,
    agentId: string,
  )

Source from the content-addressed store, hash-verified

598 * Execute a specific tool
599 */
600 private async executeSpecificTool(
601 tool: FrontendTool<any>,
602 toolCall: ToolCall,
603 message: Message,
604 agent: AbstractAgent,
605 agentId: string,
606 ): Promise<boolean> {
607 // Check if tool is constrained to a specific agent
608 if (tool?.agentId && tool.agentId !== agent.agentId) {
609 // Tool is not available for this agent, skip it
610 return false;
611 }
612
613 let handlerResult: ExecuteToolHandlerResult = {
614 result: "",
615 error: undefined,
616 isArgumentError: false,
617 };
618
619 if (tool?.handler) {
620 handlerResult = await this.executeToolHandler({
621 tool,
622 toolCall,
623 agent,
624 agentId,
625 handlerArgs: toolCall.function.arguments,
626 toolType: "specific",
627 messageId: message.id,
628 });
629 }
630
631 {
632 const messageIndex = agent.messages.findIndex((m) => m.id === message.id);
633 if (messageIndex === -1) {
634 // Parent message no longer in agent's messages (e.g. thread was switched
635 // while the tool handler was still executing). Skip result insertion and
636 // do not request a follow-up to avoid mutating the wrong thread.
637 return false;
638 }
639 const toolMessage = {
640 id: randomUUID(),
641 role: "tool" as const,
642 toolCallId: toolCall.id,
643 content: handlerResult.result,
644 };
645 agent.messages.splice(messageIndex + 1, 0, toolMessage);
646
647 if (!handlerResult.error && tool?.followUp !== false) {
648 return true; // Needs follow-up
649 }
650 }
651
652 return false;
653 }
654
655 /**
656 * Execute a wildcard tool.

Callers 1

processAgentResultMethod · 0.95

Calls 2

executeToolHandlerMethod · 0.95
randomUUIDFunction · 0.90

Tested by

no test coverage detected