跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

mcp.bats

318 行 · 8.5 KB历史下载
1# SPDX-FileCopyrightText: Copyright The Lima Authors
2# SPDX-License-Identifier: Apache-2.0
3
4load "../helpers/load"
5
6INSTANCE=bats
7
8# TODO Move helper functions to shared location
9run_yq() {
10 run -0 --separate-stderr limactl yq "$@"
11}
12
13json_edit() {
14 limactl yq --input-format json --output-format json --indent 0 "$@"
15}
16
17local_setup() {
18 cd "$PATH_BATS_ROOT"
19 coproc MCP { limactl mcp serve "$INSTANCE"; }
20
21 ID=0
22 mcp initialize '{"protocolVersion":"2025-06-18"}'
23
24 # Each mcp request should increment the ID
25 [[ $ID -eq 1 ]]
26
27 run_yq .serverInfo.name <<<"$output"
28 assert_output "lima"
29}
30
31local_teardown() {
32 kill "${MCP_PID:?}" 2>&1 >/dev/null || :
33}
34
35mcp() {
36 local method=$1
37 local params=${2:-}
38
39 local request
40 printf -v request '{"jsonrpc":"2.0","id":%d,"method":"%s"}' "$((++ID))" "$method"
41 if [[ -n $params ]]; then
42 request=$(json_edit ".params=${params}" <<<"$request")
43 fi
44
45 # send request to MCP server stdin
46 echo "$request" >&"${MCP[1]}"
47
48 # read response from MCP server stdout with 5s timeout
49 local json
50 while true; do
51 if ! read -t 5 -r json <&"${MCP[0]}"; then
52 break
53 fi
54 # If it has no "method" field, it's a response, not a notification
55 if ! jq -e 'has("method")' <<<"$json" >/dev/null 2>&1; then
56 break
57 fi
58 done
59
60 # verify that the response matches the request; also validates the output is valid JSON
61 run_yq .id <<<"$json"
62 assert_output "$ID"
63
64 # there must be no error object in the response
65 run_yq .error <<<"$json"
66 assert_output "null"
67
68 # set $output to .result
69 run_yq .result <<<"$json"
70}
71
72tools_call() {
73 local name=$1
74 local args=${2:-}
75
76 local params
77 printf -v params '{"name":"%s"}' "$name"
78 if [[ -n $args ]]; then
79 params=$(json_edit ".arguments=${args}" <<<"$params")
80 fi
81 mcp tools/call "$params"
82}
83
84@test 'list tools' {
85 mcp tools/list
86 run_yq '.tools[].name' <<<"$output"
87 assert_line glob
88 assert_line list_directory
89 assert_line read_file
90 assert_line run_shell_command
91 assert_line search_file_content
92 assert_line write_file
93}
94
95@test 'verify that tools descriptions include input and output schema' {
96 mcp tools/list
97 run_yq '.tools[] | select(.name == "run_shell_command")' <<<"$output"
98 json=$output
99
100 run_yq '.inputSchema.required[]' <<<"$json"
101 assert_line command
102 assert_line directory
103 assert_output_lines_count 2
104
105 run_yq '.inputSchema.properties | keys[]' <<<"$json"
106 assert_line command
107 assert_line description
108 assert_line directory
109 assert_output_lines_count 3
110
111 run_yq '.outputSchema.required[]' <<<"$json"
112 assert_line stdout
113 assert_line stderr
114 assert_output_lines_count 2
115
116 run_yq '.outputSchema.properties | keys[]' <<<"$json"
117 assert_line error
118 assert_line exit_code
119 assert_line stdout
120 assert_line stderr
121 assert_output_lines_count 4
122}
123
124@test 'run shell command returns command output' {
125 run -0 limactl shell "$INSTANCE" cat /etc/os-release
126 assert_output
127 expected=$output
128
129 tools_call run_shell_command '{"directory":"/etc","command":["cat","os-release"]}'
130 json=$output
131
132 run_yq '.structuredContent.exit_code' <<<"$json"
133 assert_output 0
134
135 run_yq '.structuredContent.stdout' <<<"$json"
136 assert_output "$expected"
137
138 run_yq '.structuredContent.stderr' <<<"$json"
139 refute_output
140
141 # The same data is also available as encoded JSON
142 run_yq '.content[0].type' <<<"$json"
143 assert_output "text"
144
145 run_yq '.content[0].text' <<<"$json"
146 text=$output
147
148 run_yq '.exit_code' <<<"$text"
149 assert_output 0
150
151 run_yq '.stdout' <<<"$text"
152 assert_output "$expected"
153
154 run_yq '.stderr' <<<"$text"
155 refute_output
156}
157
158@test 'run shell command returns stderr and exit code' {
159 tools_call run_shell_command '{"directory":"/","command":["bash","-c","echo NO>&2; exit 13"]}'
160 json=$output
161
162 run_yq '.structuredContent.exit_code' <<<"$json"
163 assert_output 13
164
165 run_yq '.structuredContent.error' <<<"$json"
166 assert_output "exit status 13"
167
168 run_yq '.structuredContent.stdout' <<<"$json"
169 refute_output
170
171 run_yq '.structuredContent.stderr' <<<"$json"
172 assert_output "NO"
173}
174
175@test 'run shell command fails if the directory does not exist' {
176 tools_call run_shell_command '{"directory":"/etcetera","command":["cat","os-release"]}'
177 json=$output
178
179 run_yq '.structuredContent.exit_code' <<<"$json"
180 assert_output 1
181
182 run_yq '.structuredContent.stderr' <<<"$json"
183 assert_output --partial "No such file or directory"
184}
185
186@test 'read_file reads a file' {
187 run -0 limactl shell "$INSTANCE" cat /etc/os-release
188 assert_output
189 expected=$output
190
191 tools_call read_file '{"path":"/etc/os-release"}'
192 json=$output
193
194 run_yq '.content[0].text' <<<"$json"
195 run_yq '.content' <<<"$output"
196 assert_output "$expected"
197
198 run_yq '.structuredContent.content' <<<"$json"
199 assert_output "$expected"
200}
201
202@test 'read_file returns an error when path does not exist' {
203 tools_call read_file '{"path":"/etc/os-release-info"}'
204 json=$output
205
206 run_yq '.isError' <<<"$json"
207 assert_output "true"
208
209 run_yq '.content[0].text' <<<"$json"
210 assert_output "file does not exist"
211}
212
213@test 'read_file returns an error when path is not absolute' {
214 tools_call read_file '{"path":"os-release"}'
215 json=$output
216
217 run_yq '.isError' <<<"$json"
218 assert_output "true"
219
220 run_yq '.content[0].text' <<<"$json"
221 assert_output --partial "expected an absolute path"
222}
223
224@test 'write_file creates new file and overwrites existing file' {
225 limactl shell "$INSTANCE" rm -f /tmp/mcp.test
226 tools_call write_file '{"path":"/tmp/mcp.test","content":"foo"}'
227
228 run_yq '.content[0].text' <<<"$output"
229 assert_output "{}"
230
231 run -0 limactl shell "$INSTANCE" cat /tmp/mcp.test
232 assert_output "foo"
233
234 tools_call write_file '{"path":"/tmp/mcp.test","content":"bar"}'
235
236 run_yq '.content[0].text' <<<"$output"
237 assert_output "{}"
238
239 run -0 limactl shell "$INSTANCE" cat /tmp/mcp.test
240 assert_output "bar"
241}
242
243@test 'write_file creates the directory if it does not yet exist' {
244 # Make sure /tmp/tmp is deletable even if we run the tests multiple times against the same Lima instance
245 limactl shell "$INSTANCE" chmod -R 777 /tmp/tmp || true
246 limactl shell "$INSTANCE" rm -rf /tmp/tmp
247 tools_call write_file '{"path":"/tmp/tmp/tmp","content":"tmp"}'
248 json=$output
249
250 run_yq '.isError' <<<"$json"
251 assert_output "null"
252
253 run -0 limactl shell "$INSTANCE" cat /tmp/tmp/tmp
254 assert_output "tmp"
255}
256
257@test 'write_file returns an error when the directory is not writable' {
258 limactl shell "$INSTANCE" mkdir -p /tmp/tmp
259 limactl shell "$INSTANCE" chmod 444 /tmp/tmp
260 tools_call write_file '{"path":"/tmp/tmp/tmp","content":"tmp"}'
261 json=$output
262
263 run_yq '.isError' <<<"$json"
264 assert_output "true"
265
266 run_yq '.content[0].text' <<<"$json"
267 assert_output "permission denied"
268}
269
270@test 'write_file returns an error when path is not absolute' {
271 tools_call write_file '{"path":"tmp/mcp.test","content":"baz"}'
272 json=$output
273
274 run_yq '.isError' <<<"$json"
275 assert_output "true"
276
277 run_yq '.content[0].text' <<<"$json"
278 assert_output --partial "expected an absolute path"
279}
280
281@test 'glob finds files by wildcard' {
282 tools_call glob '{"pattern":"*/*p.bats"}'
283
284 run_yq '.structuredContent.matches[]' <<<"$output"
285 assert_line --regexp '/tests/mcp.bats$'
286}
287
288@test 'glob returns an empty list when the pattern does not match' {
289
290 tools_call glob '{"pattern":"nothing.to.see"}'
291
292 run_yq '.structuredContent.matches[]' <<<"$output"
293 assert_output_lines_count 0
294}
295
296@test 'search_file_content finds text inside files' {
297 tools_call search_file_content '{"pattern":"needle in a haystack"}'
298
299 run_yq '.structuredContent.git_grep_output' <<<"$output"
300 assert_line --regexp '^tests/mcp.bats:[0-9]+: +tools_call'
301}
302
303@test 'search_file_content can find unicode characters above U+FFFF' {
304 # The light bulb emoji 💡 (U+1F4A1)
305 tools_call search_file_content '{"pattern":"💡"}'
306
307 run_yq '.structuredContent.git_grep_output' <<<"$output"
308 assert_line --regexp '^tests/mcp.bats:[0-9]+: +# The light bulb'
309 assert_line --regexp '^tests/mcp.bats:[0-9]+: +tools_call'
310}
311
312@test 'search_file_content returns an empty string if it cannot find the pattern' {
313 tools_call search_file_content "$(printf '{"pattern":"\U0001f4a1 not found"}')"
314
315 run_yq '.structuredContent.git_grep_output' <<<"$output"
316 refute_output
317}
318