MCPcopy Index your code
hub / github.com/github/copilot-sdk / executeToolAndRespond

Method executeToolAndRespond

go/session.go:1496–1553  ·  view source on GitHub ↗

executeToolAndRespond executes a tool handler and sends the result back via RPC.

(requestID, toolName, toolCallID string, arguments any, handler ToolHandler, traceparent, tracestate string)

Source from the content-addressed store, hash-verified

1494
1495// executeToolAndRespond executes a tool handler and sends the result back via RPC.
1496func (s *Session) executeToolAndRespond(requestID, toolName, toolCallID string, arguments any, handler ToolHandler, traceparent, tracestate string) {
1497 ctx := contextWithTraceParent(context.Background(), traceparent, tracestate)
1498 defer func() {
1499 if r := recover(); r != nil {
1500 errMsg := fmt.Sprintf("tool panic: %v", r)
1501 s.RPC.Tools.HandlePendingToolCall(ctx, &rpc.HandlePendingToolCallRequest{
1502 RequestID: requestID,
1503 Error: &errMsg,
1504 })
1505 }
1506 }()
1507
1508 invocation := ToolInvocation{
1509 SessionID: s.SessionID,
1510 ToolCallID: toolCallID,
1511 ToolName: toolName,
1512 Arguments: arguments,
1513 TraceContext: ctx,
1514 }
1515
1516 result, err := handler(invocation)
1517 if err != nil {
1518 errMsg := err.Error()
1519 s.RPC.Tools.HandlePendingToolCall(ctx, &rpc.HandlePendingToolCallRequest{
1520 RequestID: requestID,
1521 Error: &errMsg,
1522 })
1523 return
1524 }
1525
1526 textResultForLLM := result.TextResultForLLM
1527 if textResultForLLM == "" {
1528 textResultForLLM = fmt.Sprintf("%v", result)
1529 }
1530
1531 // Default ResultType to "success" when unset, or "failure" when there's an error.
1532 effectiveResultType := result.ResultType
1533 if effectiveResultType == "" {
1534 if result.Error != "" {
1535 effectiveResultType = "failure"
1536 } else {
1537 effectiveResultType = "success"
1538 }
1539 }
1540
1541 rpcResult := &rpc.ExternalToolTextResultForLlm{
1542 TextResultForLlm: textResultForLLM,
1543 ToolTelemetry: result.ToolTelemetry,
1544 ResultType: &effectiveResultType,
1545 }
1546 if result.Error != "" {
1547 rpcResult.Error = &result.Error
1548 }
1549 s.RPC.Tools.HandlePendingToolCall(ctx, &rpc.HandlePendingToolCallRequest{
1550 RequestID: requestID,
1551 Result: rpcResult,
1552 })
1553}

Callers 1

handleBroadcastEventMethod · 0.95

Calls 4

contextWithTraceParentFunction · 0.85
handlerFunction · 0.85
HandlePendingToolCallMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected