-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtest_mcp_server.py
More file actions
270 lines (206 loc) · 9.48 KB
/
Copy pathtest_mcp_server.py
File metadata and controls
270 lines (206 loc) · 9.48 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
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for the MCP server wrapper (run_scan core + scan_skill tool)."""
import asyncio
import os
import sys
from pathlib import Path
import pytest
from skillspector import mcp_server
from skillspector.mcp_server import run_scan
from skillspector.providers import reset_provider, use_provider
def _write_skill(tmp_path: Path, body: str = "# Safe skill") -> Path:
(tmp_path / "SKILL.md").write_text(f"---\nname: mcp-test\n---\n{body}", encoding="utf-8")
return tmp_path
async def test_run_scan_returns_structured_verdict(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""run_scan returns a JSON-serialisable verdict with the expected shape."""
monkeypatch.setattr(mcp_server, "is_llm_available", lambda: (False, "no llm"))
_write_skill(tmp_path)
result = await run_scan(str(tmp_path), use_llm=True, output_format="json")
assert result["target"] == str(tmp_path)
assert isinstance(result["risk_score"], int)
assert 0 <= result["risk_score"] <= 100
assert isinstance(result["findings"], list)
assert isinstance(result["safe_to_install"], bool)
assert result["safe_to_install"] == (result["risk_score"] <= 50)
assert result["report"] # non-empty rendered report
async def test_run_scan_llm_accounting_is_honest_without_credentials(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Requesting the LLM with no credentials must report it as not used."""
monkeypatch.setattr(mcp_server, "is_llm_available", lambda: (False, "no llm"))
_write_skill(tmp_path)
result = await run_scan(str(tmp_path), use_llm=True, output_format="json")
assert result["llm_requested"] is True
assert result["llm_available"] is False
assert result["llm_used"] is False
assert result["scan_mode"] == "static-only"
async def test_run_scan_reports_llm_available_with_credentials(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Credentials present but use_llm=False: available, but honestly not used."""
monkeypatch.setattr(mcp_server, "is_llm_available", lambda: (True, None))
_write_skill(tmp_path)
result = await run_scan(str(tmp_path), use_llm=False, output_format="json")
assert result["llm_available"] is True
assert result["llm_requested"] is False
assert result["llm_used"] is False
assert result["scan_mode"] == "static-only"
async def test_run_scan_uses_bound_provider_without_credentials(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""An injected provider can own the LLM client without exposing raw credentials."""
class _InjectedProvider:
DEFAULT_MODEL = "injected-default"
SLOT_DEFAULTS = {"meta_analyzer": "injected-default"}
def get_context_length(self, model: str) -> int | None:
return 4096 if model == "injected-default" else None
def get_max_output_tokens(self, model: str) -> int | None:
return 128 if model == "injected-default" else None
def resolve_model(self, slot: str = "default") -> str:
return "injected-default"
def resolve_credentials(self) -> tuple[str, str | None] | None:
return None
def create_chat_model(
self,
model: str,
*,
max_tokens: int,
timeout: float | None = 120,
) -> object:
return object()
class _Graph:
async def ainvoke(self, state, config):
assert state["use_llm"] is True
return {
"filtered_findings": [],
"risk_score": 0,
"risk_severity": "LOW",
"risk_recommendation": "OK",
"report_body": "report",
}
token = use_provider(_InjectedProvider())
monkeypatch.setattr(mcp_server, "graph", _Graph())
_write_skill(tmp_path)
try:
result = await run_scan(str(tmp_path), use_llm=True, output_format="json")
finally:
reset_provider(token)
assert result["llm_available"] is True
assert result["llm_requested"] is True
assert result["llm_used"] is True
assert result["scan_mode"] == "static+llm"
async def test_run_scan_disables_llm_for_unavailable_bound_provider(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A bound provider that cannot build a chat model must stay static-only."""
class _UnavailableInjectedProvider:
DEFAULT_MODEL = "injected-default"
SLOT_DEFAULTS = {"meta_analyzer": "injected-default"}
def get_context_length(self, model: str) -> int | None:
return 4096 if model == "injected-default" else None
def get_max_output_tokens(self, model: str) -> int | None:
return 128 if model == "injected-default" else None
def resolve_model(self, slot: str = "default") -> str:
return "injected-default"
def resolve_credentials(self) -> tuple[str, str | None] | None:
return None
def create_chat_model(
self,
model: str,
*,
max_tokens: int,
timeout: float | None = 120,
) -> object | None:
return None
class _Graph:
async def ainvoke(self, state, config):
assert state["use_llm"] is False
return {
"filtered_findings": [],
"risk_score": 0,
"risk_severity": "LOW",
"risk_recommendation": "OK",
"report_body": "report",
}
token = use_provider(_UnavailableInjectedProvider())
monkeypatch.setattr(mcp_server, "graph", _Graph())
_write_skill(tmp_path)
try:
result = await run_scan(str(tmp_path), use_llm=True, output_format="json")
finally:
reset_provider(token)
assert result["llm_available"] is False
assert result["llm_requested"] is True
assert result["llm_used"] is False
assert result["scan_mode"] == "static-only"
async def test_run_scan_rejects_invalid_format(tmp_path: Path) -> None:
"""An unsupported output_format is rejected before any scan runs."""
with pytest.raises(ValueError):
await run_scan(str(tmp_path), output_format="xml")
async def test_mcp_blocks_install_when_execution_failed(monkeypatch: pytest.MonkeyPatch) -> None:
"""A low risk score cannot override failed inspection execution."""
async def failed_execution_result(state: dict, config: dict) -> dict:
return {
"risk_score": 0,
"risk_severity": "LOW",
"risk_recommendation": "CAUTION",
"execution_successful": False,
"analysis_completeness": {
"entirely_uninspected_files": 1,
"ledger_exceptions": [],
},
"filtered_findings": [],
"report_body": "{}",
}
monkeypatch.setattr(mcp_server.graph, "ainvoke", failed_execution_result)
verdict = await mcp_server.run_scan("fixture", use_llm=False)
assert verdict["safe_to_install"] is False
assert verdict["execution_successful"] is False
async def test_build_server_registers_scan_skill() -> None:
"""build_server wires up the scan_skill tool (requires the mcp extra)."""
pytest.importorskip("mcp")
server = mcp_server.build_server()
tools = await server.list_tools()
assert "scan_skill" in {tool.name for tool in tools}
def test_build_server_reports_incompatible_mcp(monkeypatch: pytest.MonkeyPatch) -> None:
"""An installed package without FastMCP must not be reported as missing."""
import builtins
original_import = builtins.__import__
def import_without_fastmcp(name: str, *args: object, **kwargs: object) -> object:
if name == "mcp.server.fastmcp":
raise ModuleNotFoundError("No module named 'mcp.server.fastmcp'", name=name)
return original_import(name, *args, **kwargs)
monkeypatch.setattr(builtins, "__import__", import_without_fastmcp)
with pytest.raises(ModuleNotFoundError, match="installed 'mcp' package is incompatible"):
mcp_server.build_server()
async def test_mcp_stdio_initialize_registers_scan_skill() -> None:
"""The real stdio CLI must initialize and expose the scan_skill tool."""
pytest.importorskip("mcp")
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
repo_root = Path(__file__).resolve().parents[2]
server_params = StdioServerParameters(
command=sys.executable,
args=["-m", "skillspector.cli", "mcp"],
env={**os.environ, "PYTHONPATH": str(repo_root / "src")},
)
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await asyncio.wait_for(session.initialize(), timeout=15)
tools = await asyncio.wait_for(session.list_tools(), timeout=15)
assert "scan_skill" in {tool.name for tool in tools.tools}