Repository navigation
Expand file tree
/
Copy pathtools_script_test.go
More file actions
116 lines (88 loc) · 2.83 KB
/
Copy pathtools_script_test.go
File metadata and controls
116 lines (88 loc) · 2.83 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
package main
import (
"testing"
"time"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func newTestScriptServer(t *testing.T, scripts map[string][]string) *server.MCPServer {
t.Helper()
cfg := SecurityConfig{
Enabled: true,
WorkingDirectory: t.TempDir(),
MaxExecutionTime: 30 * time.Second,
}
executor := newCommandExecutor(cfg, zerolog.Nop())
s := server.NewMCPServer("t", "0")
newScriptTools(scripts, executor, zerolog.Nop()).register(s)
return s
}
func TestScriptTools(t *testing.T) {
t.Parallel()
scripts := map[string][]string{
"ok": {"sh", "-c", "echo ok"},
"fail": {"sh", "-c", "exit 2"},
}
t.Run("run_script succeeds", func(t *testing.T) {
t.Parallel()
s := newTestScriptServer(t, scripts)
res := callTool(t, s, "run_script", map[string]any{"name": "ok"})
text := resultText(t, res)
assert.Equal(t, "ok", text)
})
t.Run("run_script non-zero exit", func(t *testing.T) {
t.Parallel()
s := newTestScriptServer(t, scripts)
res := callTool(t, s, "run_script", map[string]any{"name": "fail"})
require.True(t, res.IsError)
textContent, ok := res.Content[0].(mcp.TextContent)
require.True(t, ok)
assert.Contains(t, textContent.Text, "exit 2")
})
t.Run("run_script unknown name", func(t *testing.T) {
t.Parallel()
s := newTestScriptServer(t, scripts)
res := callTool(t, s, "run_script", map[string]any{"name": "missing"})
require.True(t, res.IsError)
textContent, ok := res.Content[0].(mcp.TextContent)
require.True(t, ok)
assert.Contains(t, textContent.Text, "unknown script")
assert.Contains(t, textContent.Text, "ok")
})
t.Run("run_script missing name", func(t *testing.T) {
t.Parallel()
s := newTestScriptServer(t, scripts)
res := callTool(t, s, "run_script", map[string]any{})
require.True(t, res.IsError)
textContent, ok := res.Content[0].(mcp.TextContent)
require.True(t, ok)
assert.Contains(t, textContent.Text, "name")
})
t.Run("run_script executor error", func(t *testing.T) {
t.Parallel()
s := newTestScriptServer(t, map[string][]string{
"badbin": {"/nonexistent/definitely-not-a-binary"},
})
res := callTool(t, s, "run_script", map[string]any{"name": "badbin"})
require.True(t, res.IsError)
_, ok := res.Content[0].(mcp.TextContent)
require.True(t, ok)
})
t.Run("empty scripts registers no tool", func(t *testing.T) {
t.Parallel()
s := newTestScriptServer(t, map[string][]string{})
tools := s.ListTools()
assert.NotContains(t, tools, "run_script")
})
t.Run("description lists sorted names", func(t *testing.T) {
t.Parallel()
s := newTestScriptServer(t, scripts)
tools := s.ListTools()
tool, ok := tools["run_script"]
assert.True(t, ok)
assert.Contains(t, tool.Tool.Description, "fail, ok")
})
}