配置根节点
动态更新
根级结构
| 字段 | 类型 | 描述 |
|---|---|---|
uri | str | 文件 URI(例如:file:///home/user/project) |
name | str | 可选的人类可读名称 |
安全性
- 只访问已声明的 root 内的文件
- 拒绝对超出 root 边界路径的操作
- 使用 root 来限定文件搜索和操作的作用范围
向服务器公开文件系统边界
from pathlib import Path
from dedalus_mcp.client import ClientCapabilitiesConfig, open_connection
from dedalus_mcp.types import Root
initial_roots = [
Root(uri=Path.cwd().as_uri(), name="Project Directory"),
Root(uri=Path("/tmp").as_uri(), name="Temporary Files"),
]
capabilities = ClientCapabilitiesConfig(
enable_roots=True,
initial_roots=initial_roots,
)
async with open_connection(
url="http://127.0.0.1:8000/mcp",
transport="streamable-http",
capabilities=capabilities,
) as client:
# 列出已通告的根目录
roots = await client.list_roots()
for root in roots:
print(f"{root.name}: {root.uri}")
async with open_connection(
url="http://127.0.0.1:8000/mcp",
transport="streamable-http",
capabilities=capabilities,
) as client:
# 添加新根目录
new_roots = initial_roots + [
Root(uri=Path.home().as_uri(), name="Home Directory"),
]
await client.update_roots(new_roots, notify=True)
# 验证更新
roots = await client.list_roots()
print(f"Now advertising {len(roots)} roots")
from dedalus_mcp.types import Root
Root(
uri="file:///path/to/directory", # File URI
name="Human-readable name", # 可选显示名称
)
| 字段 | 类型 | 描述 |
|---|---|---|
uri | str | 文件 URI(例如:file:///home/user/project) |
name | str | 可选的人类可读名称 |
# 服务器端:检查路径是否在根目录范围内
def is_path_allowed(path: Path, roots: list[Root]) -> bool:
for root in roots:
root_path = Path(str(root.uri).replace("file://", ""))
if path.is_relative_to(root_path):
return True
return False
此页面对您有帮助吗?