nvim markdown笔记memgraph
Memgraph+MCP服务器的独立CLI,带有Docker Compose编排。
从以下位置提取Memgraph图形数据库功能 nvim降价笔记 将其打包成可重复使用、可独立安装的包。
特性
- Docker为Memgraph和MCP服务器编写编排
- 用于管理服务(启动、停止、状态)的CLI命令
- 用于AI助手集成的MCP服务器
- 用于编辑器集成的Neovim桥
- 自动实体提取(维基链接、提及、标签)
需求
- Python 3.10+
- 码头工人
- Docker Compose
- 构建工具 (需要编译Memgraph客户端):
- Ubuntu/Debian: sudo apt install cmake build-essential libssl-dev - macOS: brew install cmake openssl - 拱门: sudo pacman -S cmake base-devel openssl
在使用此软件包之前,必须安装并运行Docker和Docker Compose。CLI管理Docker Compose服务,因此这些都是硬的先决条件。
要验证Docker是否已安装:
docker --version
docker compose version安装
使用pip
pip install nvim-markdown-notes-memgraph使用紫外线
uv pip install nvim-markdown-notes-memgraph来源
git clone https://github.com/username/nvim-markdown-notes-memgraph.git
cd nvim-markdown-notes-memgraph
pip install -e .从Neovim自动安装
验证安装
nvim-markdown-notes-memgraph --help更新中
要更新到最新版本:
uv tool install --force git+https://github.com/xpcoffee/nvim-markdown-notes-memgraph.git或者,如果通过pip安装:
pip install --upgrade git+https://github.com/xpcoffee/nvim-markdown-notes-memgraph.git用法
CLI提供了几个用于管理Memgraph数据库和MCP服务器的命令。
全局选项
所有命令都支持以下全局选项:
--notes-root PATH:markdown注释的根目录(默认为~/notes或$NOTES_ROOT环境变量)
例子:
nvim-markdown-notes-memgraph --notes-root /path/to/notes start或者设置环境变量:
export NOTES_ROOT=/path/to/notes
nvim-markdown-notes-memgraph start命令
开始
启动Docker Compose服务(Memgraph+MCP服务器)。
nvim-markdown-notes-memgraph start使用自定义笔记目录:
nvim-markdown-notes-memgraph --notes-root ~/Documents/notes start此命令:
- 启动Memgraph数据库容器
- 启动MCP服务器容器
- 等待服务健康(最多60秒)
- 将笔记目录以只读方式装载到MCP服务器容器中
输出:
Starting services with notes root: /home/user/notes
Services are healthy and ready!
Memgraph is running on localhost:7687
MCP server is connected and monitoring: /home/user/notes停止
停止Docker Compose服务(Memgraph+MCP服务器)。
nvim-markdown-notes-memgraph stop此命令停止所有正在运行的容器,但保留Memgraph数据量。
输出:
Stopping services...
Services stopped successfully.状态
显示Docker Compose服务(Memgraph+MCP服务器)的状态。
nvim-markdown-notes-memgraph status输出:
Service Status:
--------------------------------------------------------------------------------
Service: memgraph
Name: nvim-markdown-notes-memgraph-memgraph-1
State: running
Health: healthy
Ports: 7687:7687
--------------------------------------------------------------------------------
Service: mcp-server
Name: nvim-markdown-notes-memgraph-mcp-server-1
State: running
Health: N/A
Ports: N/A
--------------------------------------------------------------------------------如果没有服务正在运行:
No services are running.
Run 'nvim-markdown-notes-memgraph start' to start services.配置
输出MCP JSON配置,供MCP客户端使用。
nvim-markdown-notes-memgraph config使用自定义Memgraph主机/端口:
nvim-markdown-notes-memgraph config --memgraph-host localhost --memgraph-port 7687输出(示例):
{
"mcpServers": {
"nvim-markdown-notes-memgraph": {
"command": "nvim-markdown-notes-memgraph",
"args": ["serve"],
"env": {
"MEMGRAPH_HOST": "localhost",
"MEMGRAPH_PORT": "7687",
"NOTES_ROOT": "/home/user/notes"
}
}
}
}此配置可以添加到MCP客户端配置文件中(例如,Claude Desktop、Continue等)。
服务
直接运行MCP服务器(用于容器使用或直接MCP客户端连接)。
nvim-markdown-notes-memgraph serve使用自定义配置:
nvim-markdown-notes-memgraph --notes-root ~/notes serve --memgraph-host localhost --memgraph-port 7687或者使用环境变量:
export MEMGRAPH_HOST=localhost
export MEMGRAPH_PORT=7687
export NOTES_ROOT=~/notes
nvim-markdown-notes-memgraph serve此命令通过stdio启动MCP服务器。它旨在用作Docker容器入口点或用于直接MCP客户端连接。
桥
运行Neovim桥(stdin/stdout JSON)。
nvim-markdown-notes-memgraph bridge使用自定义配置:
nvim-markdown-notes-memgraph --notes-root ~/notes bridge --memgraph-host localhost --memgraph-port 7687或者使用环境变量:
export MEMGRAPH_HOST=localhost
export MEMGRAPH_PORT=7687
export NOTES_ROOT=~/notes
nvim-markdown-notes-memgraph bridge此命令启动用于Neovim集成的Memgraph桥。它通过stdin/stdout上的JSON进行通信,并使用Bolt协议连接到Memgraph。
该桥支持以下操作:
connect:建立与Memgraph的连接health_check:检查连接是否有效update_note:更新图表中的注释及其关系delete_note:从图表中删除注释query:执行Cypher查询reindex:从头开始重建整个图形stats:获取图形统计信息
Docker配置
服务
Docker Compose设置包括两个服务:
- 内存图:Memgraph图形数据库(端口7687)
- mcp服务器:连接到Memgraph并监控您笔记的MCP服务器
卷
Docker Compose设置使用两种类型的卷进行数据持久化:
Memgraph数据卷(命名卷)
命名卷 memgraph-data 会自动创建,以便在容器重新启动时持久化Memgraph数据库。这可确保在停止服务时不会丢失图形数据。
该卷安装在 /var/lib/memgraph 在Memgraph容器内。
Notes目录(绑定装载)
您的markdown notes目录以只读方式装载到MCP服务器容器中 /notes.
默认位置: ~/notes
自定义位置:使用 --notes-root 选项或设置 NOTES_ROOT 环境变量:
# Using environment variable
export NOTES_ROOT=/path/to/my/notes
nvim-markdown-notes-memgraph start
# Or with CLI option
nvim-markdown-notes-memgraph --notes-root /path/to/my/notes start绑定挂载是只读的(:ro)以防止从容器中意外修改笔记。
管理交易量
# List volumes
docker volume ls
# Inspect the Memgraph data volume
docker volume inspect nvim-markdown-notes-memgraph_memgraph-data
# Remove volume (WARNING: deletes all graph data)
docker compose down -v图形架构
系统从您的markdown笔记中提取实体,并使用以下模式将其存储在Memgraph中:
节点
(:Note {path, title, filename, last_modified, content_hash})-Markdown注释(:Person {name, display_name})-人们提到@username(:Tag {name})-标签如#project
关系
(:Note)-[:LINKS_TO {line_number}]->(:Note)-笔记之间的维基链接(:Note)-[:MENTIONS {line_number}]->(:Person)-提及人(:Note)-[:HAS_TAG {line_number}]->(:Tag)-笔记中的标签(:Person)-[:HAS_NOTE]->(:Note)-人员节点链接到关于他们的注释
实体抽取
系统使用正则表达式模式提取实体:
- 维基链接:
[[note-name]]或[[note-name|display text]] - 提及:
@username(带连字符/下划线的字母数字) - 话题标签:
#tagname(带连字符/下划线的字母数字)
MCP集成
MCP(模型上下文协议)服务器为AI助手提供了查询和与笔记图交互的工具。
可用的MCP工具
服务器公开了AI助手可以调用的几个工具:
- find_notes_by_tag:查找包含特定标签的所有笔记
- 查找_备注_提及_人:查找提及某人的笔记
- find_linked_notes:查找链接到/来自特定笔记的笔记
- search_notes_content:在笔记内容中进行全文搜索
- get_note_stats:获取有关笔记的统计信息(链接、提及、标签)
- list_all_tags:列出所有笔记中使用的所有标签
- list_all_人:列出笔记中提到的所有人
与Claude Desktop一起使用
将配置添加到 ~/Library/Application Support/Claude/claude_desktop_config.json (macOS)或 %APPDATA%\Claude\claude_desktop_config.json (Windows):
# Generate the config
nvim-markdown-notes-memgraph config >> claude_desktop_config.json然后重新启动Claude Desktop。
与Continue一起使用
添加到“继续”配置文件中:
nvim-markdown-notes-memgraph config将输出复制到Continue MCP服务器配置中。
Neovim集成
CLI提供了 bridge 命令,使Neovim插件能够通过stdin/stdout协议上的JSON与Memgraph通信。
从Neovim自动安装
当 memgraph.enabled = true 在您的插件配置中,如果CLI不在PATH上,插件将提示您安装它。安装将使用任何可用的工具: uv tool install (优选), pip3 install,或 pip install.
配置选项 (通过 opts.memgraph):
| 选项 | 默认值 | 描述 |
|---|---|---|
install_prompt | true | 设置为 false 从不提示安装 |
install_source | "git+https://github.com/xpcoffee/nvim-markdown-notes-memgraph.git" | 包说明符传递给pip/uv。用PyPI名称或本地路径覆盖。 |
配置示例:
require("nvim-markdown-notes").setup({
notes_root_path = "~/notes",
memgraph = {
enabled = true,
-- Disable auto-install prompt
install_prompt = false,
-- Or point to a local checkout
install_source = "/path/to/nvim-markdown-notes-memgraph",
},
})行为:
- 如果CLI已在PATH上,则不会显示提示。
- 如果用户拒绝了提示,在Neovim重新启动之前,它不会再次出现。
- 如果
python3不可用,或两者都不可用uv也不pip如果找到,提示将被自动跳过,插件将回退到捆绑的脚本。 - 跑
:MemgraphInstallCLI在任何时候手动触发安装提示(重置之前的任何拒绝)。
桥接协议
bridge命令运行一个持久进程,该进程从stdin读取JSON请求并将JSON响应写入stdout。每个请求/响应都是一行JSON。
请求格式:
{
"action": "action_name",
"params": {
"key": "value"
}
}响应格式:
{
"success": true,
"data": {},
"error": null
}支持的操作
该桥支持以下操作:
- 连接:建立与Memgraph的连接
{ "action": "connect", "params": { "host": "localhost", "port": 7687 } }- 健康检查:检查连接是否有效
{ "action": "health_check", "params": {} }- update_note:更新图表中的注释及其关系
{
"action": "update_note",
"params": {
"path": "/path/to/note.md",
"title": "Note Title",
"content": "Note content...",
"wikilinks": [{ "target_path": "/path/to/other.md", "line_number": 5 }],
"mentions": [{ "name": "username", "line_number": 10 }],
"hashtags": [{ "name": "tagname", "line_number": 15 }]
}
}- delete_note:从图表中删除注释
{ "action": "delete_note", "params": { "path": "/path/to/note.md" } }- 怎么翻译:执行Cypher查询
{
"action": "query",
"params": {
"cypher": "MATCH (n:Note) RETURN n.title LIMIT 5",
"params": {}
}
}- 重新索引:从头开始重建整个图形
{
"action": "reindex",
"params": {
"notes": [
{
"path": "/path/to/note.md",
"title": "Note Title",
"content": "Content...",
"wikilinks": [],
"mentions": [],
"hashtags": []
}
]
}
}- 统计:获取图形统计信息
{ "action": "stats", "params": {} }- 退出优雅地关闭了这座桥
{ "action": "quit", "params": {} }示例:Neovim Lua集成
下面是一个如何将CLI桥集成到Neovim插件中的示例:
local M = {}
-- Job ID for the bridge process
local job_id = nil
local is_connected = false
-- Callback management
local pending_callbacks = {}
local callback_counter = 0
local response_buffer = ""
-- Parse JSON response
local function parse_response(line)
local ok, result = pcall(vim.json.decode, line)
if ok then
return result
end
return nil
end
-- Send a request to the bridge
local function send_request(action, params, callback)
if not job_id then
if callback then
callback(false, nil, "Bridge not started")
end
return
end
local request = vim.json.encode({
action = action,
params = params or {}
})
if callback then
callback_counter = callback_counter + 1
pending_callbacks[callback_counter] = callback
end
vim.fn.chansend(job_id, request .. "\n")
end
-- Handle stdout from bridge
local function on_stdout(_, data, _)
for _, line in ipairs(data) do
if line and line ~= "" then
response_buffer = response_buffer .. line
local response = parse_response(response_buffer)
if response then
response_buffer = ""
-- Find oldest pending callback
local oldest_key = nil
for key, _ in pairs(pending_callbacks) do
if oldest_key == nil or key < oldest_key then
oldest_key = key
end
end
if oldest_key and pending_callbacks[oldest_key] then
local cb = pending_callbacks[oldest_key]
pending_callbacks[oldest_key] = nil
cb(response.success, response.data, response.error)
end
-- Update connection state
if type(response.data) == "table" then
if response.data.status == "healthy" then
is_connected = true
elseif response.data.message and response.data.message:match("^Connected") then
is_connected = true
end
end
end
end
end
end
-- Start the bridge process
function M.start_bridge(callback)
if job_id then
if callback then
callback(true, "Bridge already running")
end
return
end
-- Start the bridge command
local cmd = { "nvim-markdown-notes-memgraph", "bridge" }
job_id = vim.fn.jobstart(cmd, {
on_stdout = on_stdout,
on_exit = function(_, exit_code, _)
job_id = nil
is_connected = false
for key, cb in pairs(pending_callbacks) do
cb(false, nil, "Bridge exited with code " .. exit_code)
pending_callbacks[key] = nil
end
end,
stdout_buffered = false,
})
if job_id <= 0 then
job_id = nil
if callback then
callback(false, "Failed to start bridge")
end
return
end
-- Connect to Memgraph
vim.defer_fn(function()
send_request("connect", { host = "localhost", port = 7687 }, callback)
end, 100)
end
-- Ensure services are running and connected
function M.ensure_services(callback)
-- Check if CLI is installed
if vim.fn.executable("nvim-markdown-notes-memgraph") == 0 then
if callback then
callback(false, nil, "nvim-markdown-notes-memgraph CLI not installed")
end
return
end
-- Start services if not running
vim.fn.jobstart(
{ "nvim-markdown-notes-memgraph", "start" },
{
on_exit = function(_, exit_code, _)
if exit_code == 0 then
-- Services started, now start bridge
M.start_bridge(callback)
else
if callback then
callback(false, nil, "Failed to start services")
end
end
end
}
)
end
-- Update a note in the graph
function M.update_note(path, title, content, entities, callback)
send_request("update_note", {
path = path,
title = title,
content = content,
wikilinks = entities.wikilinks or {},
mentions = entities.mentions or {},
hashtags = entities.hashtags or {}
}, callback)
end
-- Get graph statistics
function M.get_stats(callback)
send_request("stats", {}, callback)
end
return M插件中的用法
local graph = require("your_plugin.graph")
-- Ensure services are running and connect
graph.ensure_services(function(success, data, err)
if success then
print("Connected to Memgraph!")
-- Update a note
graph.update_note(
"/home/user/notes/example.md",
"Example Note",
"This is the content...",
{
wikilinks = {
{ target_path = "/home/user/notes/other.md", line_number = 5 }
},
mentions = {
{ name = "alice", line_number = 10 }
},
hashtags = {
{ name = "project", line_number = 15 }
}
},
function(success, data, err)
if success then
print("Note updated!")
else
print("Error: " .. (err or "unknown"))
end
end
)
else
print("Failed to connect: " .. (err or "unknown"))
end
end)ensure_services()模式
这 ensure_services() 模式是将CLI与Neovim插件集成的推荐方式:
- 检查是否安装了CLI:验证
nvim-markdown-notes-memgraph在PATH中 - 启动服务:运行
nvim-markdown-notes-memgraph start确保Memgraph和MCP服务器正在运行 - 启动网桥:启动bridge命令并连接到Memgraph
- 优雅地处理错误:如果任何步骤失败,请提供有用的错误消息
此模式可确保:
- 服务在需要时自动启动
- 用户无需手动启动Docker容器
- 安装CLI后,该插件即用即用
- 服务管理对用户透明
环境变量
bridge命令尊重以下环境变量:
NOTES_ROOT:markdown注释的根目录(默认:~/notes)MEMGRAPH_HOST:Memgraph主机(默认值:localhost)MEMGRAPH_PORT:Memgraph端口(默认值:7687)
您可以在Neovim配置中设置这些:
vim.env.NOTES_ROOT = vim.fn.expand("~/Documents/notes")
vim.env.MEMGRAPH_HOST = "localhost"
vim.env.MEMGRAPH_PORT = "7687"或者在过桥时通过它们:
NOTES_ROOT=~/Documents/notes nvim-markdown-notes-memgraph bridge故障排除
服务无法启动
问题: docker compose 命令失败或服务无法启动。
解决方案:
- 检查Docker是否正在运行:
docker ps- 检查是否安装了Docker Compose:
docker compose version- 检查端口冲突(Memgraph使用端口7687):
lsof -i :7687- 检查Docker日志:
docker compose logs memgraph
docker compose logs mcp-server- 尝试停止并移除所有容器:
docker compose down
nvim-markdown-notes-memgraph start服务不健康
问题: start 等待服务正常运行的命令超时。
解决方案:
- 检查服务日志:
docker compose logs memgraph- 等待更长时间-健康检查可能需要60秒。
- 重新启动Memgraph服务:
docker compose restart memgraph未找到Notes目录
问题:关于笔记目录不存在的错误。
解决方案:
- 验证路径是否存在:
ls ~/notes
# or
ls /path/to/notes- 创建目录:
mkdir -p ~/notes- 设置正确的路径:
nvim-markdown-notes-memgraph --notes-root /correct/path start未安装CLI
问题:插件退回到捆绑的脚本或报告CLI缺失。
解决方案:
- 跑
:MemgraphInstallCLI在Neovim内部触发自动安装提示。
- 或手动安装:
pip install git+https://github.com/xpcoffee/nvim-markdown-notes-memgraph.git- 验证CLI是否在PATH上:
nvim-markdown-notes-memgraph --help安装失败,出现cmake错误
问题: pip install 或 uv tool install 失败,错误跟踪很长,提到cmake或CMakeLists.txt。
解决方案:The pymgclient 依赖关系需要cmake和C编译器从源代码构建。安装构建必备组件:
Ubuntu/Debian: sudo apt install cmake build-essential libssl-dev
macOS: brew install cmake openssl然后重试安装。
Memgraph连接被拒绝
问题:无法连接到Memgraph localhost:7687.
解决方案:
- 检查Memgraph是否正在运行:
nvim-markdown-notes-memgraph status- 检查端口是否暴露:
docker compose ps- 尝试直接连接:
docker exec -it nvim-markdown-notes-memgraph-memgraph-1 mgconsoleMCP服务器没有响应
问题:MCP服务器不响应请求。
解决方案:
- 检查服务器日志:
docker compose logs mcp-server- 重新启动MCP服务器:
docker compose restart mcp-server- 验证环境变量是否设置正确:
docker compose config重新启动后图形数据丢失
问题:停止服务后,图形数据消失。
解决方案:
- 确保您正在使用
stop而不是down:
nvim-markdown-notes-memgraph stop # Preserves data- 避免使用
docker compose down -v这会删除卷。
- 检查卷是否存在:
docker volume ls | grep memgraph-dataDocker中的权限错误
问题:装载notes目录时出现拒绝权限错误。
解决方案:
- 检查笔记目录上的文件权限:
ls -la ~/notes- 确保Docker有权访问该目录(特别是在macOS上-检查Docker桌面设置)。
- 在Linux上,检查SELinux/AppArmor设置(如果适用)。
发展
运行测试
# Install dev dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Run tests with coverage
pytest --cov=src/nvim_markdown_notes_memgraph从源头构建
# Install in development mode
pip install -e .
# Build wheel
pip install build
python -m build许可证
麻省理工学院
