动态上下文MCP服务器
模型上下文协议(MCP)服务器,根据文件路径和glob模式将“即时”上下文注入AI对话。
🚀 这是什么?
当使用AI编码时,您通常会有特定的指导方针、架构模式或数据库模式,这些仅适用于代码库的某些部分。
动态上下文MCP 允许您在带有glob模式头的Markdown文件中定义这些规则。当AI代理(如Claude或Gemini)查看特定的文件路径时,此工具会自动注入该文件的相关上下文。
示例流程:
- 你正在努力
src/backend/api.ts. - 人工智能将此工具称为该路径。
- 此服务器找到
backend-guidelines.md(哪个匹配src/backend/**/*.ts). - 服务器还发现
general-ts-guidelines.md(哪个匹配*.ts) - 它将所有匹配的上下文文件组合在一起,并将结果传递给您的AI代理 _之前_ 它生成代码
______________________________________________________________________
📦 用法
快速启动(而不是手动设置)
运行init命令自动检测你的框架(Laravel、Next.js、Python),在 .agent/rules,并附加(而不是重写)您的主上下文文件(如 GEMINI.md, CLAUDE.md,或 .cursorrules)有必要的指示。
npx dynamic-context-mcp init1.准备你的上下文目录
创建文件夹(默认为 .agent/rules)在您的项目根目录中。添加包含YAML frontmatter的markdown文件 globs (单个字符串或列表)。
示例1:单球
文件: .agent/rules/styles.md
---
trigger: glob
globs: "**/*.css"
---
# CSS Rules
- Use flexbox layout.示例2:多个球体和支架扩展
文件: .agent/rules/frontend.md
---
trigger: glob
globs:
- "src/helpers/**/*.ts"
- "resources/js/**/*.{vue,ts}" # Matches both .vue and .ts files
---
# Frontend Rules
- Ensure strict type checking.
- Prefer Composition API for Vue files.示例3:始终触发
将此用于应适用于的高级规则 _每_ 文件,如通用编码标准或项目理念。
文件: .agent/rules/general.md
---
trigger: always
---
# General Guidelines
- Write clean, commented code.
- Follow SOLID principles.2.配置您的AI客户端
将其添加到您的MCP配置文件中(例如。, claude_desktop_config.json):
{
"mcpServers": {
"dynamic-context": {
"command": "npx",
"args": [
"-y",
"dynamic-context-mcp",
"/optional/absolute/path/to/your/project/.agent/rules"
]
}
}
}_注意:最后一个参数是可选的。如果省略,则查找 .agent/rules 在MCP服务器运行的目录中。_
3.重要提示:代理配置
为确保您的AI代理有效使用此工具,请在代理的系统提示或自定义说明中添加以下说明(例如 GEMINI.md, CLAUDE.md,或您的代理设置):
## DYNAMIC CONTEXT INSTRUCTIONS
This project uses dynamic context mcp to deliver "just in time" context for files that you are about to read, create, or edit.
**CRITICAL GUIDANCE**
- Before you read, create, or edit a file, check for dynamic context by requesting the `dynamic-context.get_context_for_file` tool with the file path as input.
- If dynamic context is available, read it carefully to understand important details about how to work with that file.
- dynamic context mcp takes a file path as input and goes through all context files that match that path (by glob patterns) to compile relevant context for that file
- file paths must be relative to the project root i.e. `resources/js/components/MyComponent.vue` and NOT `/absolute/path/to/project/resources/js/components/MyComponent.vue`
- dynamic context mcp does NOT take a folder path as input. If no file exists at that path, then you can create a fake file name and extension like `path/to/fakefile.txt` to check for context for that folder.
- Group context for files that have the same path and extension. If you are working with multiple files that have the same path and extension then assume that the compiled context is the same every time and use the same context for all of them.
______________________________________________________________________
🛠 本地开发
如果您想修改或参与此项目:
- 克隆仓库:
git clone
cd dynamic-context-mcp- 安装依赖项:
npm install- 构建项目:
npm run build- 本地测试:
您可以直接运行编译后的脚本:
node dist/index.js ./path/to/test/context-folder______________________________________________________________________
🤝 贡献
欢迎投稿!
- 分叉项目
- 创建功能分支(
git checkout -b feature/AmazingFeature) - 提交您的更改(
git commit -m 'Add some AmazingFeature') - 推到分支机构(
git push origin feature/AmazingFeature) - 打开拉取请求
📄 许可证
根据MIT许可证分发。
