MCPcopy Index your code
hub / github.com/CopilotKit/CopilotKit / runTool

Method runTool

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

* Programmatically execute a registered frontend tool without going through an LLM turn. * The handler runs, render components show up in the UI, and both the tool call and * result messages are added to `agent.messages`.

(
    params: CopilotKitCoreRunToolParams,
  )

Source from the content-addressed store, hash-verified

796 * result messages are added to `agent.messages`.
797 */
798 async runTool(
799 params: CopilotKitCoreRunToolParams,
800 ): Promise<CopilotKitCoreRunToolResult> {
801 const { name, agentId, parameters = {}, followUp = false } = params;
802
803 // 1. Look up the tool
804 const tool = this.getTool({ toolName: name, agentId });
805 if (!tool) {
806 const error = new Error(`Tool not found: ${name}`);
807 await this._internal.emitError({
808 error,
809 code: CopilotKitCoreErrorCode.TOOL_NOT_FOUND,
810 context: { toolName: name, agentId },
811 });
812 throw error;
813 }
814
815 // 2. Look up the agent
816 const resolvedAgentId = agentId ?? "default";
817 const agent = this._internal.getAgent(resolvedAgentId);
818 if (!agent) {
819 const error = new Error(`Agent not found: ${resolvedAgentId}`);
820 await this._internal.emitError({
821 error,
822 code: CopilotKitCoreErrorCode.AGENT_NOT_FOUND,
823 context: { agentId: resolvedAgentId },
824 });
825 throw error;
826 }
827
828 // 3. Create assistant message with tool call
829 const toolCallId = randomUUID();
830 const assistantToolCall = {
831 id: toolCallId,
832 type: "function" as const,
833 function: {
834 name,
835 arguments: JSON.stringify(parameters),
836 },
837 };
838 const assistantMessage: Message = {
839 id: randomUUID(),
840 role: "assistant",
841 content: "",
842 toolCalls: [assistantToolCall],
843 };
844
845 // 4. Push assistant message into agent's messages
846 agent.messages.push(assistantMessage);
847
848 // 5. Execute the tool handler (if it has one)
849 let handlerResult: ExecuteToolHandlerResult = {
850 result: "",
851 error: undefined,
852 isArgumentError: false,
853 };
854
855 if (tool.handler) {

Callers

nothing calls this directly

Calls 8

getToolMethod · 0.95
executeToolHandlerMethod · 0.95
runAgentMethod · 0.95
randomUUIDFunction · 0.90
emitErrorMethod · 0.65
getAgentMethod · 0.65
pushMethod · 0.65

Tested by

no test coverage detected