-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathtest_mcp.py
More file actions
596 lines (498 loc) · 22.4 KB
/
Copy pathtest_mcp.py
File metadata and controls
596 lines (498 loc) · 22.4 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import json
import os
import stat
import sys
from pathlib import Path
from unittest.mock import patch
import cloudsmith_api
import pytest
from ....cli.commands.mcp import (
_atomic_write_json,
_configure_claude_code,
_get_server_config,
_safe_update_json,
configure_client,
detect_available_clients,
list_groups,
list_tools,
)
from ....core.mcp.data import OpenAPITool
from ....core.mcp.server import DynamicMCPServer
class TestMCPListToolsCommand:
def test_list_tools_command_basic(self, runner):
"""Test that list_tools command returns available tools."""
# Mock tools data
mock_tools = {
"repos_list": OpenAPITool(
name="repos_list",
description="List repositories",
method="GET",
path="/repos/",
parameters={"type": "object", "properties": {}, "required": []},
base_url="https://api.cloudsmith.io",
query_filter=None,
is_destructive=False,
is_read_only=True,
),
"packages_list": OpenAPITool(
name="packages_list",
description="List packages",
method="GET",
path="/packages/",
parameters={"type": "object", "properties": {}, "required": []},
base_url="https://api.cloudsmith.io",
query_filter=None,
is_destructive=False,
is_read_only=True,
),
}
with patch(
"cloudsmith_cli.core.mcp.server.DynamicMCPServer.list_tools"
) as list_tools_mock:
list_tools_mock.return_value = mock_tools
result = runner.invoke(list_tools, catch_exceptions=False)
assert result.exit_code == 0
assert "repos_list" in result.output
assert "packages_list" in result.output
assert "List repositories" in result.output
assert "List packages" in result.output
list_tools_mock.assert_called_once()
def test_list_tools_command_with_filtering(self, runner):
"""Test that list_tools command respects filtering configuration."""
# Mock tools data - simulating filtered results
mock_tools = {
"repos_list": OpenAPITool(
name="repos_list",
description="List repositories",
method="GET",
path="/repos/",
parameters={"type": "object", "properties": {}, "required": []},
base_url="https://api.cloudsmith.io",
query_filter=None,
is_destructive=False,
is_read_only=True,
),
}
with patch(
"cloudsmith_cli.core.mcp.server.DynamicMCPServer.list_tools"
) as list_tools_mock:
list_tools_mock.return_value = mock_tools
result = runner.invoke(list_tools, catch_exceptions=False)
assert result.exit_code == 0
assert "repos_list" in result.output
# Verify that filtered tools are not in the output
assert "packages_list" not in result.output
list_tools_mock.assert_called_once()
def test_list_tools_command_json_output(self, runner):
"""Test that list_tools command can output JSON format."""
mock_tools = {
"repos_list": OpenAPITool(
name="repos_list",
description="List repositories",
method="GET",
path="/repos/",
parameters={"type": "object", "properties": {}, "required": []},
base_url="https://api.cloudsmith.io",
query_filter=None,
is_destructive=False,
is_read_only=True,
),
}
with patch(
"cloudsmith_cli.core.mcp.server.DynamicMCPServer.list_tools"
) as list_tools_mock:
list_tools_mock.return_value = mock_tools
result = runner.invoke(
list_tools, ["--output-format", "json"], catch_exceptions=False
)
assert result.exit_code == 0
# JSON output should contain structured data
assert '"name":' in result.output or '"name": "repos_list"' in result.output
list_tools_mock.assert_called_once()
def test_list_tools_command_empty(self, runner):
"""Test that list_tools command handles empty tool list."""
mock_tools = {}
with patch(
"cloudsmith_cli.core.mcp.server.DynamicMCPServer.list_tools"
) as list_tools_mock:
list_tools_mock.return_value = mock_tools
result = runner.invoke(list_tools, catch_exceptions=False)
assert result.exit_code == 0
assert "0 tools visible" in result.output
list_tools_mock.assert_called_once()
class TestMCPListGroupsCommand:
def test_list_groups_command_basic(self, runner):
"""Test that list_groups command returns available tool groups."""
# Mock groups data
mock_groups = {
"repos": ["repos_list", "repos_create", "repos_delete"],
"packages": ["packages_list", "packages_read", "packages_delete"],
"orgs": ["orgs_list", "orgs_read"],
}
with patch(
"cloudsmith_cli.core.mcp.server.DynamicMCPServer.list_groups"
) as list_groups_mock:
list_groups_mock.return_value = mock_groups
result = runner.invoke(list_groups, catch_exceptions=False)
assert result.exit_code == 0
assert "repos" in result.output
assert "packages" in result.output
assert "orgs" in result.output
# Check tool counts are displayed
assert "3" in result.output # repos has 3 tools
assert "2" in result.output # orgs has 2 tools
list_groups_mock.assert_called_once()
def test_list_groups_command_with_filtering(self, runner):
"""Test that list_groups command respects filtering configuration."""
# Mock groups data - simulating filtered results with only repos group
mock_groups = {
"repos": ["repos_list", "repos_create"],
}
with patch(
"cloudsmith_cli.core.mcp.server.DynamicMCPServer.list_groups"
) as list_groups_mock:
list_groups_mock.return_value = mock_groups
result = runner.invoke(list_groups, catch_exceptions=False)
assert result.exit_code == 0
assert "repos" in result.output
# Verify that filtered groups are not in the output
assert "packages" not in result.output
assert "orgs" not in result.output
list_groups_mock.assert_called_once()
def test_list_groups_command_json_output(self, runner):
"""Test that list_groups command can output JSON format."""
mock_groups = {
"repos": ["repos_list", "repos_create"],
"packages": ["packages_list"],
}
with patch(
"cloudsmith_cli.core.mcp.server.DynamicMCPServer.list_groups"
) as list_groups_mock:
list_groups_mock.return_value = mock_groups
result = runner.invoke(
list_groups, ["--output-format", "json"], catch_exceptions=False
)
assert result.exit_code == 0
# JSON output should contain structured data
assert '"name":' in result.output or '"name": "repos"' in result.output
assert '"tools":' in result.output
list_groups_mock.assert_called_once()
def test_list_groups_command_empty(self, runner):
"""Test that list_groups command handles empty group list."""
mock_groups = {}
with patch(
"cloudsmith_cli.core.mcp.server.DynamicMCPServer.list_groups"
) as list_groups_mock:
list_groups_mock.return_value = mock_groups
result = runner.invoke(list_groups, catch_exceptions=False)
assert result.exit_code == 0
assert "0 groups visible" in result.output
list_groups_mock.assert_called_once()
def test_list_groups_command_with_many_tools(self, runner):
"""Test that list_groups command shows sample tools for groups with many tools."""
# Mock a group with more than 3 tools to test the "... (+N more)" feature
mock_groups = {
"repos": [
"repos_list",
"repos_create",
"repos_read",
"repos_update",
"repos_delete",
],
}
with patch(
"cloudsmith_cli.core.mcp.server.DynamicMCPServer.list_groups"
) as list_groups_mock:
list_groups_mock.return_value = mock_groups
result = runner.invoke(list_groups, catch_exceptions=False)
assert result.exit_code == 0
assert "repos" in result.output
assert "5" in result.output # Total count of tools
# Should show "... (+2 more)" since it only displays first 3
assert "+2 more" in result.output
list_groups_mock.assert_called_once()
class TestMCPServerDynamicToolGeneration:
def test_server_generates_tools_from_openapi_spec(self):
"""Test that MCP server dynamically creates tools from OpenAPI spec."""
# Create a minimal OpenAPI spec
mock_openapi_spec = {
"paths": {
"/repos/": {
"get": {
"operationId": "repos_list",
"summary": "List all repositories",
"parameters": [
{
"name": "page",
"in": "query",
"type": "integer",
"description": "Page number",
}
],
}
},
"/repos/{owner}/{identifier}/": {
"parameters": [
{
"name": "owner",
"in": "path",
"required": True,
"type": "string",
},
{
"name": "identifier",
"in": "path",
"required": True,
"type": "string",
},
],
"get": {
"operationId": "repos_read",
"summary": "Get a specific repository",
},
"delete": {
"operationId": "repos_delete",
"summary": "Delete a repository",
},
},
}
}
# Create API config
api_config = cloudsmith_api.Configuration()
api_config.host = "https://api.cloudsmith.io"
api_config.api_key = {"X-Api-Key": "test-key"}
# Create MCP server instance
server = DynamicMCPServer(api_config=api_config, force_all_tools=True)
# Mock the spec loading directly
server.spec = mock_openapi_spec
# Call the synchronous tool generation method
import asyncio
asyncio.run(
server._generate_tools_from_spec() # pylint: disable=protected-access
)
# Verify tools were created
assert len(server.tools) == 3
assert "repos_list" in server.tools
assert "repos_read" in server.tools
assert "repos_delete" in server.tools
# Verify tool details
repos_list_tool = server.tools["repos_list"]
assert repos_list_tool.name == "repos_list"
assert repos_list_tool.description == "List all repositories"
assert repos_list_tool.method == "GET"
assert repos_list_tool.path == "/repos/"
assert repos_list_tool.is_read_only is True
assert repos_list_tool.is_destructive is False
# Verify delete tool is marked as destructive
repos_delete_tool = server.tools["repos_delete"]
assert repos_delete_tool.is_destructive is True
assert repos_delete_tool.is_read_only is False
def test_server_respects_tool_filtering(self):
"""Test that MCP server filters tools based on configuration."""
# Create a simple OpenAPI spec
mock_openapi_spec = {
"paths": {
"/repos/": {
"get": {
"operationId": "repos_list",
"summary": "List repositories",
}
},
"/packages/": {
"get": {
"operationId": "packages_list",
"summary": "List packages",
}
},
}
}
api_config = cloudsmith_api.Configuration()
api_config.host = "https://api.cloudsmith.io"
api_config.api_key = {"X-Api-Key": "test-key"}
# Create server with filtering - only allow repos group
server = DynamicMCPServer(api_config=api_config, allowed_tool_groups=["repos"])
# Mock the spec loading directly
server.spec = mock_openapi_spec
# Call the tool generation method
import asyncio
asyncio.run(
server._generate_tools_from_spec() # pylint: disable=protected-access
)
# Verify only repos tools were created
assert len(server.tools) == 1
assert "repos_list" in server.tools
assert "packages_list" not in server.tools
SERVER_CONFIG = {"command": "cloudsmith", "args": ["mcp", "start"]}
class TestMCPServerConfig:
def test_frozen_executable_runs_mcp_directly(self):
with (
patch.object(sys, "frozen", True, create=True),
patch.object(sys, "executable", "/opt/cloudsmith/cloudsmith"),
):
assert _get_server_config("staging") == {
"command": "/opt/cloudsmith/cloudsmith",
"args": ["-P", "staging", "mcp", "start"],
}
class TestMCPConfigureClaudeCode:
def test_user_scope_merges_into_existing_claude_json(self, tmp_path):
claude_json = tmp_path / ".claude.json"
existing = {
"numStartups": 42,
"mcpServers": {"other": {"command": "other-mcp"}},
"projects": {"/some/path": {"lastSessionId": "abc"}},
}
claude_json.write_text(json.dumps(existing, indent=2))
with patch("cloudsmith_cli.cli.commands.mcp.Path.home", return_value=tmp_path):
assert _configure_claude_code("cloudsmith", SERVER_CONFIG, is_global=True)
result = json.loads(claude_json.read_text())
assert result["mcpServers"]["other"] == {"command": "other-mcp"}
assert result["mcpServers"]["cloudsmith"] == SERVER_CONFIG
assert result["numStartups"] == 42
assert result["projects"] == existing["projects"]
def test_user_scope_errors_when_claude_json_missing(self, tmp_path):
with patch("cloudsmith_cli.cli.commands.mcp.Path.home", return_value=tmp_path):
with pytest.raises(ValueError, match="Launch Claude Code at least once"):
_configure_claude_code("cloudsmith", SERVER_CONFIG, is_global=True)
def test_project_scope_writes_local_mcp_json(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
assert _configure_claude_code("cloudsmith", SERVER_CONFIG, is_global=False)
local = tmp_path / ".mcp.json"
assert local.exists()
assert json.loads(local.read_text()) == {
"mcpServers": {"cloudsmith": SERVER_CONFIG}
}
def test_project_scope_merges_into_existing_mcp_json(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
existing = {"mcpServers": {"other": {"command": "other-mcp"}}}
(tmp_path / ".mcp.json").write_text(json.dumps(existing))
_configure_claude_code("cloudsmith", SERVER_CONFIG, is_global=False)
result = json.loads((tmp_path / ".mcp.json").read_text())
assert result["mcpServers"]["other"] == {"command": "other-mcp"}
assert result["mcpServers"]["cloudsmith"] == SERVER_CONFIG
def test_configure_client_routes_claude_code_with_profile(self, tmp_path):
(tmp_path / ".claude.json").write_text("{}")
with patch("cloudsmith_cli.cli.commands.mcp.Path.home", return_value=tmp_path):
configure_client(
"claude-code", SERVER_CONFIG, is_global=True, profile="staging"
)
result = json.loads((tmp_path / ".claude.json").read_text())
assert "cloudsmith-staging" in result["mcpServers"]
def test_detect_available_clients_respects_claude_json_presence(self, tmp_path):
# Real get_config_path for claude-code (resolves under the patched
# home); None for everything else so this host's real configs don't
# leak into the result.
import cloudsmith_cli.cli.commands.mcp as mcp_module
real_get_config_path = mcp_module.get_config_path
def selective(client, is_global=True):
if client == "claude-code":
return real_get_config_path(client, is_global=is_global)
return None
with (
patch("cloudsmith_cli.cli.commands.mcp.Path.home", return_value=tmp_path),
patch(
"cloudsmith_cli.cli.commands.mcp.get_config_path", side_effect=selective
),
):
assert "claude-code" not in detect_available_clients()
(tmp_path / ".claude.json").write_text("{}")
assert "claude-code" in detect_available_clients()
class TestSafeWriteHelpers:
def test_atomic_write_preserves_existing_file_mode(self, tmp_path):
target = tmp_path / "config.json"
target.write_text("{}")
os.chmod(target, 0o600)
_atomic_write_json(target, {"k": "v"})
assert json.loads(target.read_text()) == {"k": "v"}
assert stat.S_IMODE(target.stat().st_mode) == 0o600
def test_atomic_write_creates_parent_directory(self, tmp_path):
target = tmp_path / "nested" / "deeper" / "config.json"
_atomic_write_json(target, {"k": "v"})
assert json.loads(target.read_text()) == {"k": "v"}
def test_atomic_write_cleans_up_tempfile_on_failure(self, tmp_path):
target = tmp_path / "config.json"
with patch(
"cloudsmith_cli.cli.commands.mcp.os.replace",
side_effect=OSError("boom"),
):
with pytest.raises(OSError, match="boom"):
_atomic_write_json(target, {"k": "v"})
leftovers = [
p for p in tmp_path.iterdir() if p.name.startswith(".config.json.")
]
assert leftovers == []
def test_safe_update_creates_file_when_missing(self, tmp_path):
target = tmp_path / "config.json"
_safe_update_json(target, lambda c: c.setdefault("a", []).append(1))
assert json.loads(target.read_text()) == {"a": [1]}
def test_atomic_write_follows_symlinks(self, tmp_path):
real = tmp_path / "real" / "config.json"
real.parent.mkdir()
real.write_text(json.dumps({"existing": True}))
link = tmp_path / "config.json"
link.symlink_to(real)
_atomic_write_json(link, {"k": "v"})
assert link.is_symlink()
assert link.readlink() == real
assert json.loads(real.read_text()) == {"k": "v"}
def test_safe_update_retries_on_concurrent_write(self, tmp_path):
target = tmp_path / "config.json"
target.write_text(json.dumps({"counter": 0}))
calls = {"n": 0}
def mutate(config):
config["counter"] = config.get("counter", 0) + 1
if calls["n"] == 0:
# Simulate a concurrent writer landing between our read and
# our pre-write mtime check. Bumping mtime explicitly so the
# change is visible even on filesystems with coarse mtime.
target.write_text(json.dumps({"counter": 99}))
before = target.stat()
os.utime(
target, ns=(before.st_atime_ns, before.st_mtime_ns + 1_000_000)
)
calls["n"] += 1
_safe_update_json(target, mutate)
# First attempt reads 0, racing writer lands 99, retry reads 99 and
# increments to 100.
assert json.loads(target.read_text())["counter"] == 100
def test_safe_update_raises_after_max_retries(self, tmp_path):
target = tmp_path / "config.json"
target.write_text("{}")
original_stat = Path.stat
def always_racing(self, *args, **kwargs):
result = original_stat(self, *args, **kwargs)
if str(self) == str(target):
# Bump the file on disk every time we observe it, so the
# mtime check always fails.
os.utime(target, ns=(result.st_atime_ns, result.st_mtime_ns + 1000))
return result
with patch.object(Path, "stat", always_racing):
with pytest.raises(ValueError, match="another process keeps modifying"):
_safe_update_json(target, lambda c: c, max_retries=2)
def test_safe_update_raises_on_malformed_json(self, tmp_path):
target = tmp_path / "config.json"
target.write_text("{not valid json}")
with pytest.raises(ValueError, match="Cannot parse config file"):
_safe_update_json(target, lambda c: c)
class TestConfigureClientSafeWrite:
"""Other clients should now use the atomic safe-update path."""
def test_writes_atomically_for_claude_desktop(self, tmp_path):
config_path = tmp_path / "claude_desktop_config.json"
with patch(
"cloudsmith_cli.cli.commands.mcp.get_config_path",
return_value=config_path,
):
configure_client("claude", SERVER_CONFIG, is_global=True)
assert json.loads(config_path.read_text()) == {
"mcpServers": {"cloudsmith": SERVER_CONFIG}
}
def test_writes_vscode_key_for_vscode_client(self, tmp_path):
config_path = tmp_path / "settings.json"
config_path.write_text(json.dumps({"editor.fontSize": 14}))
with patch(
"cloudsmith_cli.cli.commands.mcp.get_config_path",
return_value=config_path,
):
configure_client("vscode", SERVER_CONFIG, is_global=True)
result = json.loads(config_path.read_text())
assert result["editor.fontSize"] == 14
assert result["chat.mcp.servers"]["cloudsmith"] == SERVER_CONFIG