forked from modelcontextprotocol/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_roots.py
More file actions
176 lines (118 loc) · 5.3 KB
/
test_roots.py
File metadata and controls
176 lines (118 loc) · 5.3 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
"""Tests for mcp.server.mcpserver.utilities.roots."""
from __future__ import annotations
from pathlib import Path
from unittest.mock import AsyncMock, MagicMock
import pytest
from mcp.server.mcpserver.utilities.roots import (
assert_within_roots,
get_roots,
within_roots_check,
)
pytestmark = pytest.mark.anyio
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def make_ctx(root_uris: list[str]) -> MagicMock:
root_objects = [MagicMock(uri=uri) for uri in root_uris]
list_roots_result = MagicMock()
list_roots_result.roots = root_objects
session = MagicMock()
session.list_roots = AsyncMock(return_value=list_roots_result)
ctx = MagicMock()
ctx.session = session
return ctx
def make_failing_ctx() -> MagicMock:
session = MagicMock()
session.list_roots = AsyncMock(side_effect=Exception("not supported"))
ctx = MagicMock()
ctx.session = session
return ctx
# ---------------------------------------------------------------------------
# get_roots
# ---------------------------------------------------------------------------
async def test_get_roots_returns_uris():
ctx = make_ctx(["file:///home/user/project", "file:///tmp/work"])
result = await get_roots(ctx)
assert result == ["file:///home/user/project", "file:///tmp/work"]
async def test_get_roots_returns_empty_when_no_roots():
ctx = make_ctx([])
result = await get_roots(ctx)
assert result == []
async def test_get_roots_returns_empty_on_exception():
ctx = make_failing_ctx()
result = await get_roots(ctx)
assert result == []
# ---------------------------------------------------------------------------
# assert_within_roots
# ---------------------------------------------------------------------------
async def test_assert_passes_when_no_roots():
ctx = make_ctx([])
await assert_within_roots("/any/path/at/all", ctx)
async def test_assert_passes_when_path_inside_root():
ctx = make_ctx(["file:///home/user/project"])
await assert_within_roots("/home/user/project/src/main.py", ctx)
async def test_assert_raises_when_path_outside_root():
ctx = make_ctx(["file:///home/user/project"])
with pytest.raises(PermissionError, match="Access denied"):
await assert_within_roots("/etc/passwd", ctx)
async def test_assert_passes_with_multiple_roots_matching_second():
ctx = make_ctx(["file:///home/user/project", "file:///tmp/work"])
await assert_within_roots("/tmp/work/file.txt", ctx)
async def test_assert_raises_outside_all_roots():
ctx = make_ctx(["file:///home/user/project", "file:///tmp/work"])
with pytest.raises(PermissionError):
await assert_within_roots("/var/log/syslog", ctx)
async def test_assert_accepts_pathlib_path():
ctx = make_ctx(["file:///home/user/project"])
await assert_within_roots(Path("/home/user/project/file.txt"), ctx)
async def test_assert_skips_non_file_roots():
ctx = make_ctx(["https://api.example.com/v1"])
await assert_within_roots("/any/local/path", ctx)
async def test_assert_no_raise_when_client_doesnt_support_roots():
ctx = make_failing_ctx()
await assert_within_roots("/any/path", ctx)
# ---------------------------------------------------------------------------
# within_roots_check decorator
# ---------------------------------------------------------------------------
async def test_decorator_passes_inside_root():
ctx = make_ctx(["file:///home/user/project"])
@within_roots_check
async def read_file(path: str, ctx: MagicMock) -> str:
return "file contents"
result = await read_file(path="/home/user/project/notes.txt", ctx=ctx)
assert result == "file contents"
async def test_decorator_raises_outside_root():
ctx = make_ctx(["file:///home/user/project"])
@within_roots_check
async def read_file(path: str, ctx: MagicMock) -> str:
raise AssertionError("tool body must not run when decorator denies access") # pragma: no cover
with pytest.raises(PermissionError):
await read_file(path="/etc/passwd", ctx=ctx)
async def test_decorator_checks_star_path_params():
ctx = make_ctx(["file:///home/user/project"])
@within_roots_check
async def copy_file(source_path: str, dest_path: str, ctx: MagicMock) -> str:
raise AssertionError("tool body must not run when decorator denies access") # pragma: no cover
with pytest.raises(PermissionError):
await copy_file(
source_path="/home/user/project/file.txt",
dest_path="/etc/shadow",
ctx=ctx,
)
async def test_decorator_ignores_non_path_string_params():
ctx = make_ctx(["file:///home/user/project"])
@within_roots_check
async def tool(name: str, path: str, ctx: MagicMock) -> str:
return f"{name}:{path}"
result = await tool(
name="greeting",
path="/home/user/project/file.txt",
ctx=ctx,
)
assert result == "greeting:/home/user/project/file.txt"
async def test_decorator_raises_without_ctx():
@within_roots_check
async def bad_tool(path: str) -> str:
raise AssertionError("tool body must not run when ctx is missing") # pragma: no cover
with pytest.raises(ValueError, match="ctx"):
await bad_tool(path="/some/path")