Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(mcp): support recursive directory deletion in forget tool (#1935)

t0saki0f5a569eecd6
diff --git a/docs/en/guides/06-mcp-integration.md b/docs/en/guides/06-mcp-integration.md
index c3f944cfd..74f01a318 100644
--- a/docs/en/guides/06-mcp-integration.md
+++ b/docs/en/guides/06-mcp-integration.md
@@ -114,7 +114,7 @@ Once connected, OpenViking exposes 9 tools:
| `add_resource` | Add a local file or URL as a resource | `path`, `description` (optional) |
| `grep` | Regex content search across `viking://` files | `uri`, `pattern` (string or array), `case_insensitive` |
| `glob` | Find files matching a glob pattern | `pattern`, `uri` (optional scope) |
-| `forget` | Delete any `viking://` URI (use `search` to find it first) | `uri` |
+| `forget` | Delete any `viking://` URI (use `search` to find it first; pass `recursive=true` to delete a directory) | `uri`, `recursive` (optional) |
| `health` | Check OpenViking service health | none |
## Troubleshooting
diff --git a/docs/zh/guides/06-mcp-integration.md b/docs/zh/guides/06-mcp-integration.md
index f3c271c6b..f174c1cd1 100644
--- a/docs/zh/guides/06-mcp-integration.md
+++ b/docs/zh/guides/06-mcp-integration.md
@@ -106,7 +106,7 @@ claude mcp add --transport http openviking \
| `add_resource` | 添加本地文件或 URL 作为资源 | `path`, `description`(可选) |
| `grep` | 在 `viking://` 文件中进行正则内容搜索 | `uri`, `pattern`(字符串或数组), `case_insensitive` |
| `glob` | 按 glob 模式匹配文件 | `pattern`, `uri`(可选范围) |
-| `forget` | 删除任意 `viking://` URI(先用 `search` 查找) | `uri` |
+| `forget` | 删除任意 `viking://` URI(先用 `search` 查找;删除目录需 `recursive=true`) | `uri`, `recursive`(可选) |
| `health` | 检查 OpenViking 服务健康状态 | 无 |
## 故障排除
diff --git a/openviking/server/mcp_endpoint.py b/openviking/server/mcp_endpoint.py
index 9b1a5cd94..8db6faa4c 100644
--- a/openviking/server/mcp_endpoint.py
+++ b/openviking/server/mcp_endpoint.py
@@ -456,11 +456,11 @@ async def glob(pattern: str, uri: str = "viking://", node_limit: int = 100) -> s
@mcp.tool()
-async def forget(uri: str) -> str:
- """Permanently delete a viking:// URI from OpenViking. This is irreversible. Only use when the user explicitly asks to forget or delete something. Always confirm with the user before calling this tool. Use the search tool first to find the exact URI, then pass it here."""
+async def forget(uri: str, recursive: bool = False) -> str:
+ """Permanently delete a viking:// URI from OpenViking. This is irreversible. Only use when the user explicitly asks to forget or delete something. Always confirm with the user before calling this tool. Use the search tool first to find the exact URI, then pass it here. Set recursive=true only when the user explicitly asks to delete a directory tree."""
service = get_service()
ctx = _get_ctx()
- await service.fs.rm(uri, ctx=ctx)
+ await service.fs.rm(uri, ctx=ctx, recursive=recursive)
return f"Deleted: {uri}"
diff --git a/tests/server/test_mcp_endpoint.py b/tests/server/test_mcp_endpoint.py
index 6cbbc0e9f..5977d29cf 100644
--- a/tests/server/test_mcp_endpoint.py
+++ b/tests/server/test_mcp_endpoint.py
@@ -29,7 +29,7 @@ from openviking.server.mcp_endpoint import (
search,
)
from openviking.server.mcp_endpoint import ls as list_tool
-from openviking_cli.exceptions import UnauthenticatedError
+from openviking_cli.exceptions import FailedPreconditionError, UnauthenticatedError
from openviking_cli.session.user_id import UserIdentifier
DEFAULT_CTX = RequestContext(
@@ -350,6 +350,28 @@ async def test_forget_by_uri_deletes_resource(service):
assert "deleted" in result.lower()
+async def test_forget_directory_without_recursive_fails(service):
+ ctx = DEFAULT_CTX
+ dir_uri = "viking://resources/test_forget_dir"
+ child_uri = f"{dir_uri}/child.md"
+ await service.viking_fs.mkdir(dir_uri, ctx=ctx, exist_ok=True)
+ await service.viking_fs.write(child_uri, "child data", ctx=ctx)
+
+ with pytest.raises(FailedPreconditionError):
+ await forget(uri=dir_uri)
+
+
+async def test_forget_directory_with_recursive_succeeds(service):
+ ctx = DEFAULT_CTX
+ dir_uri = "viking://resources/test_forget_dir_recursive"
+ child_uri = f"{dir_uri}/child.md"
+ await service.viking_fs.mkdir(dir_uri, ctx=ctx, exist_ok=True)
+ await service.viking_fs.write(child_uri, "child data", ctx=ctx)
+
+ result = await forget(uri=dir_uri, recursive=True)
+ assert "deleted" in result.lower()
+
+
# ---------------------------------------------------------------------------
# grep tool
# ---------------------------------------------------------------------------