kg-mcp
kg —— 为你的 AI 助手打造的本地知识图谱
Beta —— API 仍可能变动,且预计仍存在一些缺陷。
kg 为你的 AI 助手提供持久化、结构化、可编辑的项目记忆,以知识图谱的形式本地存储。
与其只依赖文档分块检索,你可以将架构、决策、事故、规则、依赖关系和工作流保存在一个可读、可审查且对 Git 友好的图谱中。
当你希望助手跨会话理解现有项目——而不是每次都从零开始——时,就可以使用它。
为什么使用它
持久化记忆 —— 在多次对话之间保留项目知识
结构化而非模糊 —— 直接检视节点、边、事实和缺口
可编辑、可审查 —— 以
*.kg文件存储图谱,diff 清晰可读本地优先 —— 你的项目记忆以对 Git 友好的格式保存在本机
兼容 MCP 客户端 —— 可作为本地 stdio MCP 服务器接入
Related MCP server: Agentic Memory Server
为什么不用 RAG 就够了
经典 RAG 擅长从文档中检索文本块。
而 kg-mcp 更适合以下场景:
需要稳定的项目记忆,而非反复检索
需要显式的事实、关系和依赖
需要在与助手实际协作过程中更新图谱
需要能够检视、版本化、对比并随时间改进的内容
安装
从 crates.io 安装
cargo install kg-cli通过脚本安装
推荐安装方式:
curl -sSL https://raw.githubusercontent.com/nnar1o/kg/master/install.sh | sh你也可以从 GitHub Releases 下载现成的二进制文件。
将 kg-mcp 连接到你的 AI 客户端
将 kg-mcp 添加为本地 stdio MCP 服务器。
示例配置:
{
"mcpServers": {
"kg": {
"command": "/absolute/path/to/kg-mcp"
}
}
}之后:
重启你的 AI 客户端,
确认
kgMCP 服务器可用,开始使用下面的提示词。
完整的 MCP 配置与参考文档:docs/mcp.md
SCL 快速上手
kg 理解简短的、动词开头的英文命令(SCL —— Simple Command Language,简单命令语言)。
当前使用的图谱会根据你的配置自动解析。
find "compressor defrost"
get concept:refrigerator
add concept:smart_fridge --name "Smart Fridge" --description "Connected refrigerator"
modify concept:smart_fridge --importance 0.9
remove concept:old_idea
connect process:compressor_control TRIGGERS process:auto_defrost
disconnect process:compressor_control TRIGGERS process:auto_defrost
list nodes
stats
use fridge
help核心动词
动词 | 作用 |
| 按文本搜索节点 |
| 按 id 获取单个节点 |
| 创建节点(类型由 id 前缀推断) |
| 更新节点字段 |
| 删除节点 |
| 创建边(别名: |
| 删除边(别名: |
| 列出图谱内容 |
| 显示图谱统计信息 |
| 切换当前使用的图谱 |
| 获取某个动词或全部动词的帮助 |
| 对搜索结果提供反馈 |
| 禁用后续行的默认值 |
ID
格式:<type>:snake_case —— 例如 concept:fridge、bug:door_seal、process:compressor_cycle。
关系
HAS USES STORED_IN TRIGGERS CREATED_BY AFFECTED_BY AVAILABLE_IN DOCUMENTED_IN DEPENDS_ON TRANSITIONS DECIDED_BY GOVERNED_BY READS_FROM
提示
标志参数放在位置参数之后。多词值请加引号。
用
;或换行分隔命令。以#开头的行是注释。在脚本中使用
use <graph>切换图谱。规范的
kg <graph> node find ...命令仍可作为后备方式使用。完整的 SCL 参考文档:
docs/scl.md
生成图谱
这是新项目的第一条工作流:让助手根据你的文档创建或扩展图谱。
默认情况下,图谱以 *.kg 文件存储在 ~/.kg/graphs 中。
最小提示词:
You are connected to kg-mcp.
Project graph name: payments
Build or extend this graph from the project documentation I provide.
Use `payments` as the graph name for all graph operations.
Only add facts grounded in source material.
If an important fact is missing and can be inferred safely from the provided docs, update the graph.
If something is ambiguous, ask or record it as a note instead of inventing facts.带文档的示例提示词:
Use kg-mcp to build or extend the `payments` graph from these documents:
- docs/payments/overview.md
- docs/payments/retries.md
- docs/payments/providers.md
Only add facts grounded in the documents.
If something is ambiguous, keep it out of the graph or record it as a note.
When you finish, summarize what was added, what remains unclear, and what document should be ingested next.此工作流的更详细提示词:docs/ai-prompt-graph-from-docs.md
如需现成的仓库示例,可运行 cargo run --bin repo-example 从本仓库生成 repo-example.kg。
为目录自动生成图谱
kg 可以将现有文件夹自动转换为图谱。它会扫描目录树,识别许多常见文件类型,为 Rust、Java、JavaScript/TypeScript、Python 和 C/C++ 提取符号,并将生成的结构与手动图谱分开保存。
对于类 Markdown 文档,它还会创建文档(GDOC)和章节(GSEC)节点,并包含章节内容。
这是一种快速获取代码库或工作区实用地图的方式,无需手动建模。生成的索引是本地化的、可刷新的,并且可以在 git 中安全忽略。
示例:
cargo run --bin repo-example这会从本仓库生成 repo-example.kg 作为本地演示。
向助手询问图谱中的事实
图谱创建完成后,常规工作流是让助手检视图谱并据此回答问题。
示例提示词:
Use kg-mcp to inspect my existing `payments` graph.
I want to understand:
- how payment authorization works,
- what triggers retries,
- which external providers are involved,
- which datastore reads and writes are part of the flow.
If the graph is missing critical information, say exactly what is missing.其他有用的问题:
"在
payments图谱中,哪些规则控制重试?""哪些系统会写入 orders 数据存储?"
"这个图谱中缺少什么或哪些部分比较薄弱?"
"哪些节点和边可以解释授权流程?"
通过助手添加或更新事实
你也可以在工作过程中让助手改进图谱。
示例提示词:
Use kg-mcp to review my existing `payments` graph.
Find:
- missing important nodes,
- weak descriptions,
- missing facts,
- suspicious or low-value edges.
Apply safe improvements where possible.
Only add facts grounded in the graph, the provided docs, or the current discussion.
If something is ambiguous, leave it out or add a note.
When you finish, summarize:
- what was wrong,
- what you changed,
- what still needs manual review.当你的主系统提示词或项目提示词已经告诉助手哪个图谱属于该项目时,效果最佳。
最小项目级提示词:
You are connected to kg-mcp.
Project graph name: payments.
Use this graph for relevant reads and updates in this project.
If you notice important missing information that is grounded in the available docs or conversation context, update the graph as part of your work.
If uncertain, ask or add a note instead of inventing facts.提示
项目配置(.kg.toml)
kg 会在当前目录及其父目录中查找 .kg.toml。
示例:
backend = "json" # json backend writes native .kg files by default
graph_dir = ".kg/graphs"
graph_dirs = ["../shared-graphs", "../team-graphs"]
nudge = 20
user_short_uid = "dev_01"
[graphs]
payments = "graphs/payments.kg"说明:
backend = "json"是默认值,优先使用.kg文本图谱。backend = "redb"将图谱存储在.db文件中。graph_dir设置主图谱目录。graph_dirs添加由kg list和图谱解析扫描的额外目录。
将图谱纳入 Git 管理
默认图谱目录是 ~/.kg/graphs。
你可以将该目录纳入 git 管理。
推荐做法:
将主要的
*.kg图谱文件纳入 git,忽略生成的附属文件和本地操作文件,
将备份快照和事件日志视为本地机器历史,除非你明确希望对它们进行版本管理。
建议的 .gitignore:
*.kglog
*.kgindex
*.event.log
*.migration.log
*.bak
*.bck.*.gz实际使用中:
*.kg是主要图谱文件,通常是你希望审查和提交的,*.kglog是本地访问/反馈日志,*.kgindex是生成的本地索引,*.event.log是本地追加式变更时间线,*.bak是上次写入前的磁盘旧版本,*.bck.*.gz是定期生成的压缩备份快照,*.migration.log是旧图谱转换时的迁移报告。
*.kg 对 Git 友好,其结构经过精心设计,使 diff 清晰可读,并在多人协作同一图谱时更易于合并。
将图谱导出为 HTML
要生成图谱的交互式 HTML 视图:
kg graph payments export-html --output payments.html你可以将生成的 HTML 作为当前图谱的可分享可视化快照保存。
文档
docs/mcp.md—— MCP 配置与工具参考docs/ai-prompt-graph-from-docs.md—— 文档摄入的详细提示词docs/build-graph-from-docs.md—— 从文档构建图谱的工作流docs/troubleshooting.md—— 常见问题
联系方式
如有问题或反馈:nnar10@proton.me
This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Connectors
Personal wiki and memory layer for AI assistants. Persistent, structured memory across sessions.
Give your AI agent a persistent map of your project's structure, dependencies, and bugs.
Persistent memory and knowledge graphs for AI agents. Hybrid search, context checkpoints, and more.
Persistent knowledge graph for AI-augmented teams. Store decisions, findings, and standing rules across agent sessions with semantic search and typed connections. Includes cross-session memory, audit trail, workspace isolation, and secret detection. Built for teams running agents that need to remember. Free until launch with team tier as default, anon trial available.
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceProvides AI assistants with persistent graph-based memory capabilities using Neo4j, enabling semantic search, relationship tracking, and knowledge organization across multiple project contexts.1,58431MIT
- AlicenseNot gradedqualityNot gradedmaintenanceProvides enterprise-grade persistent memory for AI assistants with complete offline operation, enabling intelligent knowledge storage, branch-based organization, and smart search across project domains while keeping all data local and secure.

Doclea MCPofficial
AlicenseNot gradedqualityCmaintenanceProvides persistent memory for AI coding assistants, storing and retrieving architectural decisions, patterns, and solutions across sessions using semantic search, while also offering git integration for commit messages and code expertise mapping.MIT- AlicenseNot gradedqualityBmaintenanceGives AI assistants persistent, queryable project memory for decisions, patterns, and rules, reducing the need to re-explain context in every prompt.11Apache 2.0
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/nnar1o/kg'
If you have feedback or need assistance with the MCP directory API, please join our Discord server