内容
# 通用源管理系統
一个用于管理文献、笔记、实体链接和阅读列表资源的轻量级FastMCP服务器,适用于本地SQLite数据库。
## 当前范围
该存储库提供:
- 基于本地SQLite的源管理
- 附属于源的笔记
- 源与命名概念之间的实体链接
- 只读数据库检查工具
- 用于源查找和阅读列表的MCP资源
- 带有过渡JSON缓存的规范化标识符存储
它不与MCP Memory Server或外部内存图集成。
## 快速开始
1. 从当前模式创建数据库:
```bash
sqlite3 sources.db < create_sources_db.sql
```
2. 使用数据库路径安装FastMCP服务器:
```bash
fastmcp install sqlite-paper-fastmcp-server.py --name "源管理器" -e SQLITE_DB_PATH=/path/to/sources.db
```
3. 可选:使用`examples/sources.db`中附带的演示fixture。
## Docker
该服务器可以作为一个容器化的stdio MCP服务器进行打包。
构建镜像:
```bash
docker build -t sqlite-lit-mcp .
```
使用持久化SQLite卷运行:
```bash
mkdir -p data
docker run --rm -i \
-e SQLITE_DB_PATH=/data/sources.db \
-v "$(pwd)/data:/data" \
sqlite-lit-mcp
```
注意:
- 镜像默认为`/data/sources.db`设置`SQLITE_DB_PATH`。
- 如果数据库文件不存在,容器将从`create_sources_db.sql`初始化它。
- 如果您正在挂载较旧的数据库,请在启动服务器之前应用`migrations/`下的所需迁移。
- 该仓库作为stdio MCP服务器运行,因此默认情况下没有HTTP端口可供暴露。
使用Docker Compose:
```bash
docker compose run --rm sqlite-lit-mcp
```
## 当前工具界面
已实现的工具:
- `read_query`
- `list_tables`
- `describe_table`
- `get_table_stats`
- `get_database_info`
- `vacuum_database`
- `add_sources`
- `add_notes`
- `update_status`
- `add_identifiers`
- `link_to_entities`
- `get_source_entities`
- `update_entity_links`
- `remove_entity_links`
- `get_entity_sources`
已实现的资源:
- `source://<id>`
- `source://by-identifier/<type>/<value>`
- `reading-list://unread`
- `reading-list://reading`
- `entity://<entity_name>`
## 模式说明
当前模式以四个表为中心:
- `sources`
- `source_identifiers`
- `source_notes`
- `source_entity_links`
`sources.identifiers`仍作为过渡JSON缓存,但标识符查找现在使用`source_identifiers`。
支持的标识符类型:
- `semantic_scholar`
- `doi`
- `arxiv`
- `openalex`
- `pmid`
- `isbn`
- `url`
支持的实体关系类型:
- `discusses`
- `introduces`
- `extends`
- `evaluates`
- `applies`
- `critiques`
- `supports`
- `contradicts`
- `refutes`
`sources`上的轻量级溯源字段:
- `provider`
- `discovered_via`
- `discovered_at`
模式现在使用`PRAGMA user_version = 3`。
## 迁移
如果您有一个版本0或版本1的数据库,请应用:
```bash
sqlite3 /path/to/sources.db < migrations/2026-03-09__normalize-identifiers.sql
sqlite3 /path/to/sources.db < migrations/2026-03-09__expand-entity-relation-types.sql
```
如果您已经迁移到版本2,请应用:
```bash
sqlite3 /path/to/sources.db < migrations/2026-03-09__expand-entity-relation-types.sql
```
这些迁移:
- 添加`source_identifiers`
- 从遗留JSON列中回填标识符行
- 添加溯源字段
- 扩展`source_entity_links.relation_type`以包含`supports`、`contradicts`和`refutes`
- 将`PRAGMA user_version`更新为`3`
服务器在连接时检查`PRAGMA user_version`,如果数据库未迁移,将拒绝旧数据库。
## 包布局
实现在`sqlite_lit_server/`下组织:
- `app.py`创建FastMCP实例并注册工具/资源
- `db.py`拥有连接设置和模式版本检查
- `repository.py`将SQL密集型查找逻辑靠近数据层
- `tools_admin.py`、`tools_sources.py`和`tools_entities.py`包含MCP工具
- `resources.py`定义MCP资源
`sqlite-paper-fastmcp-server.py`仍然作为一个薄兼容性垫片。
## 批量导入源
`add_sources`是新源的批量导入入口点。它接受一个名为`sources`的参数,其中每个项目是:
```text
[title, source_type, identifier_type, identifier_value, initial_note]
```
`initial_note`必须是`null`或具有`title`和`content`的对象。
支持的`source_type`值:
- `paper`
- `webpage`
- `book`
- `video`
- `blog`
支持的`identifier_type`值:
- `semantic_scholar`
- `doi`
- `arxiv`
- `openalex`
- `pmid`
- `isbn`
- `url`
重复处理:
- 确切的标识符匹配返回`Source already exists`以及现有的源有效载荷。
- 基于标题的模糊匹配返回`Potential duplicates found. Please verify or use add_identifiers if these are the same source.`。
- 成功批量写入返回一个结果列表,每个输入项按请求顺序排列。
示例MCP/JSON有效载荷:
```json
{
"sources": [
[
"Attention Is All You Need",
"paper",
"arxiv",
"1706.03762",
{
"title": "初始想法",
"content": "变换器从这里开始。"
}
],
[
"OpenAlex Import",
"paper",
"openalex",
"W1234567890",
null
]
]
}
```
示例Python调用:
```python
add_sources([
(
"Attention Is All You Need",
"paper",
"arxiv",
"1706.03762",
{
"title": "初始想法",
"content": "变换器从这里开始。",
},
),
(
"OpenAlex Import",
"paper",
"openalex",
"W1234567890",
None,
),
])
```
与`docker compose run --rm sqlite-lit-mcp`一起使用Docker时,相同的工具有效载荷起作用。
## 批量写入约定
复数写入工具接受列表并返回一个结果列表,按输入顺序排列。相关的批量工具包括:
- `add_notes`
- `add_identifiers`
- `update_status`
- `link_to_entities`
## 示例用法
添加另一个标识符:
```python
add_identifiers([
(
"Attention Is All You Need",
"paper",
"arxiv",
"1706.03762",
"semantic_scholar",
"204e3073870fae3d05bcbc2f6a8e263d9b72e776",
),
])
```
读取源资源:
```text
source://by-identifier/arxiv/1706.03762
```