feat(mcp): add manual knowledge update tool

main
十三分之五六七 2026-09-01 16:01:01 +08:00 committed by lyingbug
parent 25a930b9a0
commit f7cc5969e3
6 changed files with 119 additions and 5 deletions

View File

@ -9,6 +9,7 @@
### 新增
- 新增 `create_knowledge_from_text` 工具:通过手动 Markdown 文本创建知识条目,调用既有 `/knowledge-bases/{id}/knowledge/manual` 接口,补齐 #323 中"文本"部分。默认 `status="publish"`,创建后即进入解析/索引流程、可被检索;可传 `status="draft"` 仅保存不索引。
- 新增 `update_knowledge_from_text` 工具:通过既有 `PUT /knowledge/manual/{id}` 接口更新手工 Markdown 知识;默认重新索引,也可保存为草稿,补齐 #2378。
- README 工具清单补列既有的 `create_knowledge_from_file`。
## [1.1.1] - 2026-07-30
@ -148,4 +149,4 @@ WeKnora/mcp-server/
### 兼容性
- 支持 Windows、macOS、Linux
- 支持 Python 3.10-3.12
- 兼容现代 Python 包管理工具
- 兼容现代 Python 包管理工具

View File

@ -135,6 +135,21 @@ echo "WEKNORA_API_KEY=your_api_key_here" >> .env
}
```
#### 更新手工知识
```json
{
"tool": "update_knowledge_from_text",
"arguments": {
"knowledge_id": "know_789012",
"title": "注意力机制摘要(修订版)",
"content": "# 注意力机制\n\n这里是修订后的 Markdown 内容...",
"status": "publish"
}
}
```
省略 `title` 或传空字符串会保留原标题;将 `status` 设为 `draft` 可只保存内容而不重新索引。
#### 列出知识
```json
{
@ -421,4 +436,4 @@ python main.py --verbose
1. 运行 `python main.py --check-only` 检查环境
2. 使用 `python main.py --verbose` 查看详细日志
3. 检查 WeKnora 服务是否正常运行
4. 验证网络连接和防火墙设置
4. 验证网络连接和防火墙设置

View File

@ -123,6 +123,7 @@ python test_module.py
- `create_knowledge_from_file` - 从本地文件创建知识
- `create_knowledge_from_url` - 从 URL 创建知识
- `create_knowledge_from_text` - 从文本创建知识
- `update_knowledge_from_text` - 更新手工 Markdown 知识,可重新索引或保存为草稿
- `list_knowledge` - 列出知识
- `get_knowledge` - 获取知识详情
- `delete_knowledge` - 删除知识
@ -154,4 +155,4 @@ python test_module.py
## 调用效果
<img width="950" height="2063" alt="118d078426f42f3d4983c13386085d7f" src="https://github.com/user-attachments/assets/09111ec8-0489-415c-969d-aa3835778e14" />
<img width="950" height="2063" alt="118d078426f42f3d4983c13386085d7f" src="https://github.com/user-attachments/assets/09111ec8-0489-415c-969d-aa3835778e14" />

View File

@ -66,7 +66,7 @@ class TransportRegressionTest(unittest.TestCase):
class StdioToolsListTest(unittest.TestCase):
def test_tools_list_returns_30_tools(self):
def test_tools_list_returns_31_tools(self):
async def _run() -> int:
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
@ -86,7 +86,7 @@ class StdioToolsListTest(unittest.TestCase):
return len(tools.tools)
count = asyncio.run(_run())
self.assertEqual(count, 30)
self.assertEqual(count, 31)
class HttpStatelessSmokeTest(unittest.TestCase):

View File

@ -0,0 +1,58 @@
#!/usr/bin/env python3
"""Tests for the update_knowledge_from_text MCP tool and client method."""
import unittest
from unittest import mock
import weknora_mcp_server as srv
class UpdateKnowledgeFromTextTest(unittest.TestCase):
def test_client_puts_manual_update_to_expected_endpoint(self):
client = srv.WeKnoraClient("http://localhost:8080/api/v1", "k")
with mock.patch.object(client, "_request") as req:
req.return_value = {"ok": True}
result = client.update_knowledge_from_text(
"knowledge-1", "# Updated", title="New title"
)
self.assertEqual(result, {"ok": True})
req.assert_called_once_with(
"PUT",
"/knowledge/manual/knowledge-1",
json={
"title": "New title",
"content": "# Updated",
"status": "publish",
},
)
def test_client_can_save_a_draft_and_keep_the_title(self):
client = srv.WeKnoraClient("http://localhost:8080/api/v1", "k")
with mock.patch.object(client, "_request") as req:
req.return_value = {"ok": True}
client.update_knowledge_from_text(
"knowledge-1", "Draft content", status="draft"
)
self.assertEqual(
req.call_args.kwargs["json"],
{"title": "", "content": "Draft content", "status": "draft"},
)
def test_tool_forwards_all_update_fields(self):
with mock.patch.object(
srv.client, "update_knowledge_from_text", return_value={"ok": True}
) as method:
result = srv.update_knowledge_from_text(
"knowledge-1", "Body", title="Title", status="draft"
)
self.assertEqual(result, {"ok": True})
method.assert_called_once_with(
"knowledge-1", "Body", title="Title", status="draft"
)
if __name__ == "__main__":
unittest.main()

View File

@ -360,6 +360,26 @@ class WeKnoraClient:
"POST", f"/knowledge-bases/{kb_id}/knowledge/manual", json=data
)
def update_knowledge_from_text(
self,
knowledge_id: str,
content: str,
title: str = "",
status: str = "publish",
) -> Dict:
"""Update an existing manual Markdown knowledge entry.
An empty ``title`` keeps the current title. ``status`` defaults to
``"publish"`` so the updated content is re-indexed immediately; pass
``"draft"`` to save it without indexing.
"""
data = {
"title": title,
"content": content,
"status": status,
}
return self._request("PUT", f"/knowledge/manual/{knowledge_id}", json=data)
def list_knowledge(self, kb_id: str, page: int = 1, page_size: int = 20) -> Dict:
"""List knowledge in a knowledge base"""
params = {"page": page, "page_size": page_size}
@ -760,6 +780,25 @@ def create_knowledge_from_text(
)
@mcp.tool()
def update_knowledge_from_text(
knowledge_id: str,
content: str,
title: str = "",
status: str = "publish",
) -> dict:
"""Update an existing manual Markdown knowledge entry.
``knowledge_id`` is the ID returned by ``create_knowledge_from_text`` or
``list_knowledge``. ``content`` is required. Leave ``title`` empty to keep
the current title. ``status`` defaults to ``"publish"`` so the new content
is re-indexed; pass ``"draft"`` to save without indexing.
"""
return client.update_knowledge_from_text(
knowledge_id, content, title=title, status=status
)
@mcp.tool()
def list_knowledge(kb_id: str, page: int = 1, page_size: int = 20) -> dict:
"""List knowledge entries in a knowledge base."""