-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathscript_test.go
More file actions
232 lines (213 loc) · 5.94 KB
/
Copy pathscript_test.go
File metadata and controls
232 lines (213 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc.
// SPDX-License-Identifier: Apache-2.0
package script
import (
"context"
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/require"
"github.com/stacklok/toolhive-core/mcpcompat/mcp"
)
func TestExecutor(t *testing.T) {
t.Parallel()
echoTool := Tool{
Name: "echo",
Description: "Returns arguments as JSON",
Call: func(_ context.Context, args map[string]interface{}) (*mcp.CallToolResult, error) {
b, _ := json.Marshal(args)
return mcp.NewToolResultText(string(b)), nil
},
}
statusTool := Tool{
Name: "check-status",
Description: "Check service status",
Call: func(_ context.Context, args map[string]interface{}) (*mcp.CallToolResult, error) {
svc, _ := args["service"].(string)
status := "healthy"
if svc == "db" {
status = "degraded"
}
return mcp.NewToolResultText(fmt.Sprintf(`{"service": "%s", "status": "%s"}`, svc, status)), nil
},
}
fetchTool := Tool{
Name: "fetch",
Description: "Fetch data by ID",
Call: func(_ context.Context, args map[string]interface{}) (*mcp.CallToolResult, error) {
return mcp.NewToolResultText(fmt.Sprintf(`"result-%v"`, args["id"])), nil
},
}
hyphenatedTool := Tool{
Name: "my-hyphenated-tool",
Description: "A tool with hyphens",
Call: func(_ context.Context, _ map[string]interface{}) (*mcp.CallToolResult, error) {
return mcp.NewToolResultText(`"called"`), nil
},
}
tests := []struct {
name string
tools []Tool
config *Config
script string
data map[string]interface{}
check func(t *testing.T, result *mcp.CallToolResult)
errMsg string
}{
{
name: "multi-tool script with loops and conditionals",
tools: []Tool{statusTool},
script: `
services = ["api", "db", "cache"]
degraded = []
for svc in services:
status = check_status(service=svc)
if status["status"] != "healthy":
degraded.append(status["service"])
return degraded
`,
check: func(t *testing.T, result *mcp.CallToolResult) {
t.Helper()
var parsed []interface{}
require.NoError(t, json.Unmarshal([]byte(extractText(t, result)), &parsed))
require.Equal(t, []interface{}{"db"}, parsed)
},
},
{
name: "JSON text automatically parsed into structured data",
tools: []Tool{echoTool},
script: `
result = echo(name="alice", count=42)
return {"name": result["name"], "count": result["count"]}
`,
check: func(t *testing.T, result *mcp.CallToolResult) {
t.Helper()
var parsed map[string]interface{}
require.NoError(t, json.Unmarshal([]byte(extractText(t, result)), &parsed))
require.Equal(t, "alice", parsed["name"])
require.Equal(t, float64(42), parsed["count"])
},
},
{
name: "parallel fan-out returns ordered results",
tools: []Tool{fetchTool},
script: `
results = parallel([
lambda: fetch(id=1),
lambda: fetch(id=2),
lambda: fetch(id=3),
])
return results
`,
check: func(t *testing.T, result *mcp.CallToolResult) {
t.Helper()
var parsed []interface{}
require.NoError(t, json.Unmarshal([]byte(extractText(t, result)), &parsed))
require.Len(t, parsed, 3)
},
},
{
name: "data arguments injected as top-level variables",
tools: []Tool{echoTool},
script: `
return echo(name=user_name)
`,
data: map[string]interface{}{"user_name": "Alice"},
check: func(t *testing.T, result *mcp.CallToolResult) {
t.Helper()
require.Contains(t, extractText(t, result), "Alice")
},
},
{
name: "call_tool dispatches by original name",
tools: []Tool{hyphenatedTool},
script: `return call_tool("my-hyphenated-tool")`,
check: func(t *testing.T, result *mcp.CallToolResult) {
t.Helper()
require.Contains(t, extractText(t, result), "called")
},
},
{
name: "step limit exceeded",
config: &Config{StepLimit: 100},
script: `
x = 0
for i in range(10000):
x = x + 1
return x
`,
errMsg: "too many steps",
},
{
name: "script logs appear as second content item",
script: `
print("log line 1")
print("log line 2")
return "done"
`,
check: func(t *testing.T, result *mcp.CallToolResult) {
t.Helper()
require.Len(t, result.Content, 2, "should have result text + logs")
logsContent, ok := mcp.AsTextContent(result.Content[1])
require.True(t, ok)
require.Contains(t, logsContent.Text, "log line 1")
require.Contains(t, logsContent.Text, "log line 2")
},
},
{
name: "data argument shadowing builtin rejected",
tools: []Tool{echoTool},
script: `return 1`,
data: map[string]interface{}{"call_tool": "shadow"},
errMsg: "conflicts with",
},
{
name: "data argument shadowing tool rejected",
tools: []Tool{echoTool},
script: `return 1`,
data: map[string]interface{}{"echo": "shadow"},
errMsg: "conflicts with",
},
{
name: "invalid data argument type rejected",
script: `return 1`,
data: map[string]interface{}{"bad": struct{}{}},
errMsg: `data argument "bad"`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
exec := New(tt.tools, tt.config)
result, err := exec.Execute(context.Background(), tt.script, tt.data)
if tt.errMsg != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tt.errMsg)
return
}
require.NoError(t, err)
require.False(t, result.IsError)
tt.check(t, result)
})
}
}
func TestExecutor_ToolDescription(t *testing.T) {
t.Parallel()
tools := []Tool{
{Name: "tool-a", Description: "Does A"},
{Name: "tool-b", Description: "Does B"},
}
exec := New(tools, nil)
desc := exec.ToolDescription()
require.Contains(t, desc, "tool-a")
require.Contains(t, desc, "tool-b")
require.Contains(t, desc, "parallel")
}
// extractText gets the first text content from a CallToolResult.
func extractText(t *testing.T, result *mcp.CallToolResult) string {
t.Helper()
require.NotEmpty(t, result.Content)
tc, ok := mcp.AsTextContent(result.Content[0])
require.True(t, ok, "expected text content")
return tc.Text
}