Skip to content

Commit 6e49ba4

Browse files
authored
feat(cli/invoke): add support for direct tool invocation from CLI (googleapis#2353)
## Description This PR introduces a new subcommand, invoke, to the toolbox CLI. This feature allows developers to execute tools defined in their configuration directly from the command line. - New Subcommand: Implemented invoke as subcommand, which handles tool lookup, parameter unmarshaling from JSON, and invocation. - Persistent Configuration Flags: Updated cmd/root.go to make flags like --tools-file, --tools-folder, and --prebuilt persistent, allowing them to be used with subcommands. - Testing: Added unit tests for various scenarios - Documentation: Created a new "how-to" guide for CLI tool testing and updated the CLI reference documentation.
1 parent 4cff979 commit 6e49ba4

File tree

6 files changed

+511
-80
lines changed

6 files changed

+511
-80
lines changed

cmd/invoke_tool_test.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"context"
19+
"os"
20+
"path/filepath"
21+
"strings"
22+
"testing"
23+
)
24+
25+
func TestInvokeTool(t *testing.T) {
26+
// Create a temporary tools file
27+
tmpDir := t.TempDir()
28+
29+
toolsFileContent := `
30+
sources:
31+
my-sqlite:
32+
kind: sqlite
33+
database: test.db
34+
tools:
35+
hello-sqlite:
36+
kind: sqlite-sql
37+
source: my-sqlite
38+
description: "hello tool"
39+
statement: "SELECT 'hello' as greeting"
40+
echo-tool:
41+
kind: sqlite-sql
42+
source: my-sqlite
43+
description: "echo tool"
44+
statement: "SELECT ? as msg"
45+
parameters:
46+
- name: message
47+
type: string
48+
description: message to echo
49+
`
50+
51+
toolsFilePath := filepath.Join(tmpDir, "tools.yaml")
52+
if err := os.WriteFile(toolsFilePath, []byte(toolsFileContent), 0644); err != nil {
53+
t.Fatalf("failed to write tools file: %v", err)
54+
}
55+
56+
tcs := []struct {
57+
desc string
58+
args []string
59+
want string
60+
wantErr bool
61+
errStr string
62+
}{
63+
{
64+
desc: "success - basic tool call",
65+
args: []string{"invoke", "hello-sqlite", "--tools-file", toolsFilePath},
66+
want: `"greeting": "hello"`,
67+
},
68+
{
69+
desc: "success - tool call with parameters",
70+
args: []string{"invoke", "echo-tool", `{"message": "world"}`, "--tools-file", toolsFilePath},
71+
want: `"msg": "world"`,
72+
},
73+
{
74+
desc: "error - tool not found",
75+
args: []string{"invoke", "non-existent", "--tools-file", toolsFilePath},
76+
wantErr: true,
77+
errStr: `tool "non-existent" not found`,
78+
},
79+
{
80+
desc: "error - invalid JSON params",
81+
args: []string{"invoke", "echo-tool", `invalid-json`, "--tools-file", toolsFilePath},
82+
wantErr: true,
83+
errStr: `params must be a valid JSON string`,
84+
},
85+
}
86+
87+
for _, tc := range tcs {
88+
t.Run(tc.desc, func(t *testing.T) {
89+
_, got, err := invokeCommandWithContext(context.Background(), tc.args)
90+
if (err != nil) != tc.wantErr {
91+
t.Fatalf("got error %v, wantErr %v", err, tc.wantErr)
92+
}
93+
if tc.wantErr && !strings.Contains(err.Error(), tc.errStr) {
94+
t.Fatalf("got error %v, want error containing %q", err, tc.errStr)
95+
}
96+
if !tc.wantErr && !strings.Contains(got, tc.want) {
97+
t.Fatalf("got %q, want it to contain %q", got, tc.want)
98+
}
99+
})
100+
}
101+
}
102+
103+
func TestInvokeTool_AuthUnsupported(t *testing.T) {
104+
tmpDir := t.TempDir()
105+
toolsFileContent := `
106+
sources:
107+
my-bq:
108+
kind: bigquery
109+
project: my-project
110+
useClientOAuth: true
111+
tools:
112+
bq-tool:
113+
kind: bigquery-sql
114+
source: my-bq
115+
description: "bq tool"
116+
statement: "SELECT 1"
117+
`
118+
toolsFilePath := filepath.Join(tmpDir, "auth_tools.yaml")
119+
if err := os.WriteFile(toolsFilePath, []byte(toolsFileContent), 0644); err != nil {
120+
t.Fatalf("failed to write tools file: %v", err)
121+
}
122+
123+
args := []string{"invoke", "bq-tool", "--tools-file", toolsFilePath}
124+
_, _, err := invokeCommandWithContext(context.Background(), args)
125+
if err == nil {
126+
t.Fatal("expected error for tool requiring client auth, but got nil")
127+
}
128+
if !strings.Contains(err.Error(), "client authorization is not supported") {
129+
t.Fatalf("unexpected error message: %v", err)
130+
}
131+
}

0 commit comments

Comments
 (0)