Skip to content

Commit 2ba6ed4

Browse files
Yuan325AnujJhunjhunwala
authored andcommitted
chore(tools): invoke return type any instead of []any (googleapis#904)
Update `tool.Invoke()` to return type `any` instead of `[]any`. Toolbox return a map with the `results` key, and the SDK reads the string from the key. So this won't break existing SDK implementation. Fixes googleapis#870
1 parent 3f5bccf commit 2ba6ed4

File tree

32 files changed

+60
-56
lines changed

32 files changed

+60
-56
lines changed

internal/server/common_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type MockTool struct {
4242
manifest tools.Manifest
4343
}
4444

45-
func (t MockTool) Invoke(context.Context, tools.ParamValues) ([]any, error) {
45+
func (t MockTool) Invoke(context.Context, tools.ParamValues) (any, error) {
4646
mock := []any{t.Name}
4747
return mock, nil
4848
}

internal/server/mcp/v20241105/method.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,13 @@ func toolsCallHandler(ctx context.Context, id jsonrpc.RequestId, tools map[strin
122122
}
123123

124124
content := make([]TextContent, 0)
125-
for _, d := range results {
125+
126+
sliceRes, ok := results.([]any)
127+
if !ok {
128+
sliceRes = []any{results}
129+
}
130+
131+
for _, d := range sliceRes {
126132
text := TextContent{Type: "text"}
127133
dM, err := json.Marshal(d)
128134
if err != nil {

internal/server/mcp/v20250326/method.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,13 @@ func toolsCallHandler(ctx context.Context, id jsonrpc.RequestId, tools map[strin
122122
}
123123

124124
content := make([]TextContent, 0)
125-
for _, d := range results {
125+
126+
sliceRes, ok := results.([]any)
127+
if !ok {
128+
sliceRes = []any{results}
129+
}
130+
131+
for _, d := range sliceRes {
126132
text := TextContent{Type: "text"}
127133
dM, err := json.Marshal(d)
128134
if err != nil {

internal/server/mcp/v20250618/method.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,13 @@ func toolsCallHandler(ctx context.Context, id jsonrpc.RequestId, tools map[strin
122122
}
123123

124124
content := make([]TextContent, 0)
125-
for _, d := range results {
125+
126+
sliceRes, ok := results.([]any)
127+
if !ok {
128+
sliceRes = []any{results}
129+
}
130+
131+
for _, d := range sliceRes {
126132
text := TextContent{Type: "text"}
127133
dM, err := json.Marshal(d)
128134
if err != nil {

internal/tools/alloydbainl/alloydbainl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ type Tool struct {
156156
mcpManifest tools.McpManifest
157157
}
158158

159-
func (t Tool) Invoke(ctx context.Context, params tools.ParamValues) ([]any, error) {
159+
func (t Tool) Invoke(ctx context.Context, params tools.ParamValues) (any, error) {
160160
sliceParams := params.AsSlice()
161161
allParamValues := make([]any, len(sliceParams)+1)
162162
allParamValues[0] = fmt.Sprintf("%s", sliceParams[0]) // nl_question

internal/tools/bigquery/bigqueryexecutesql/bigqueryexecutesql.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ type Tool struct {
114114
mcpManifest tools.McpManifest
115115
}
116116

117-
func (t Tool) Invoke(ctx context.Context, params tools.ParamValues) ([]any, error) {
117+
func (t Tool) Invoke(ctx context.Context, params tools.ParamValues) (any, error) {
118118
sliceParams := params.AsSlice()
119119
sql, ok := sliceParams[0].(string)
120120
if !ok {

internal/tools/bigquery/bigquerygetdatasetinfo/bigquerygetdatasetinfo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ type Tool struct {
118118
mcpManifest tools.McpManifest
119119
}
120120

121-
func (t Tool) Invoke(ctx context.Context, params tools.ParamValues) ([]any, error) {
121+
func (t Tool) Invoke(ctx context.Context, params tools.ParamValues) (any, error) {
122122
mapParams := params.AsMap()
123123
projectId, ok := mapParams[projectKey].(string)
124124
if !ok {
@@ -137,7 +137,7 @@ func (t Tool) Invoke(ctx context.Context, params tools.ParamValues) ([]any, erro
137137
return nil, fmt.Errorf("failed to get metadata for dataset %s (in project %s): %w", datasetId, t.Client.Project(), err)
138138
}
139139

140-
return []any{metadata}, nil
140+
return metadata, nil
141141
}
142142

143143
func (t Tool) ParseParams(data map[string]any, claims map[string]map[string]any) (tools.ParamValues, error) {

internal/tools/bigquery/bigquerygettableinfo/bigquerygettableinfo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ type Tool struct {
120120
mcpManifest tools.McpManifest
121121
}
122122

123-
func (t Tool) Invoke(ctx context.Context, params tools.ParamValues) ([]any, error) {
123+
func (t Tool) Invoke(ctx context.Context, params tools.ParamValues) (any, error) {
124124
mapParams := params.AsMap()
125125
projectId, ok := mapParams[projectKey].(string)
126126
if !ok {
@@ -145,7 +145,7 @@ func (t Tool) Invoke(ctx context.Context, params tools.ParamValues) ([]any, erro
145145
return nil, fmt.Errorf("failed to get metadata for table %s.%s.%s: %w", projectId, datasetId, tableId, err)
146146
}
147147

148-
return []any{metadata}, nil
148+
return metadata, nil
149149
}
150150

151151
func (t Tool) ParseParams(data map[string]any, claims map[string]map[string]any) (tools.ParamValues, error) {

internal/tools/bigquery/bigquerylistdatasetids/bigquerylistdatasetids.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ type Tool struct {
118118
mcpManifest tools.McpManifest
119119
}
120120

121-
func (t Tool) Invoke(ctx context.Context, params tools.ParamValues) ([]any, error) {
121+
func (t Tool) Invoke(ctx context.Context, params tools.ParamValues) (any, error) {
122122
mapParams := params.AsMap()
123123
projectId, ok := mapParams[projectKey].(string)
124124
if !ok {

internal/tools/bigquery/bigquerylisttableids/bigquerylisttableids.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ type Tool struct {
119119
mcpManifest tools.McpManifest
120120
}
121121

122-
func (t Tool) Invoke(ctx context.Context, params tools.ParamValues) ([]any, error) {
122+
func (t Tool) Invoke(ctx context.Context, params tools.ParamValues) (any, error) {
123123
mapParams := params.AsMap()
124124
projectId, ok := mapParams[projectKey].(string)
125125
if !ok {

0 commit comments

Comments
 (0)