增强目录上下文MCP服务器是一个全面的模型上下文协议(MCP)服务器,它为像Claude这样的AI助手提供了高级的目录分析和文件管理功能。该服务器将目录上下文提取与完整的文件编辑功能相结合,是代码分析、项目管理和文件操作的强大工具。
npm install{
"mcpServers": {
"directory-context": {
"command": "node",
"args": ["path/to/server.js"]
}
}
}
npm install
添加到你的claude_desktop_config.json文件中:
{
"mcpServers": {
"directory-context": {
"command": "node",
"args": ["path/to/your/server.js"],
"cwd": "your-default-working-directory"
}
}
}
使用提供的mcp-json-config.json作为模板,并相应地更新路径。
在使用任何功能之前,设置你的工作目录:
set_working_directory(directory="/path/to/your/project")
get_directory_structure(
max_depth=10, // 最大遍历深度
include_hidden=false, // 是否包含隐藏文件/目录
file_types=[".js", ".py", ".md"] // 按文件扩展名过滤
)
analyze_project_context(
include_code_analysis=true, // 是否包含复杂度分析
focus_files=["specific_file.js"] // 关注特定文件
)
search_files(
query="function myFunction", // 搜索查询
file_types=[".js", ".ts"], // 要搜索的文件类型
is_regex=false, // 是否使用正则表达式匹配
max_results=50 // 最大结果数
)
get_git_context(
include_diff=true, // 是否包含工作目录更改
commit_count=10 // 最近提交的数量
)
create_file(
path="src/new-component.js",
content="const MyComponent = () => { ... }",
encoding="utf8",
overwrite=false
)
update_file(
path="src/config.js",
updates=[
{
search: "oldValue: 'old'",
replace: "newValue: 'new'",
regex: false,
all: true
}
],
backup=true
)
append_to_file(
path="README.md",
content="## New Section\nContent here...",
newline_before=true
)
delete_file(
path="old-file.js",
backup=true // 创建.deleted备份
)
rename_file(
old_path="old-name.js",
new_path="new-name.js",
overwrite=false
)
create_directory(
path="src/components/new-feature",
recursive=true
)
原子执行多个操作:
batch_file_operations(
operations=[
{
operation: "create",
params: {
path: "src/component.js",
content: "export default () => {}"
}
},
{
operation: "update",
params: {
path: "src/index.js",
updates: [...]
}
}
],
rollback_on_error=true
)
服务器根据配置文件自动检测项目类型:
package.jsonrequirements.txtCargo.tomlgo.modpom.xml、build.gradlecomposer.jsonGemfileDockerfile、docker-compose.yml服务器自动识别重要文件:
package.json、config.js等)index.js、main.py、app.js)README.md、docs).env)重要文件会自动作为MCP资源暴露,使AI助手无需显式的文件读取请求即可轻松访问。
| 工具 | 描述 | 必需参数 |
|---|---|---|
set_working_directory |
设置工作目录 | directory |
get_directory_structure |
获取目录树 | 无 |
get_file_contents |
读取多个文件 | files |
search_files |
在文件中搜索 | query |
analyze_project_context |
分析项目 | 无 |
get_git_context |
获取Git仓库信息 | 无 |
| 工具 | 描述 | 必需参数 |
|---|---|---|
create_file |
创建新文件 | path、content |
update_file |
更新现有文件 | path、updates |
append_to_file |
追加内容到文件 | path、content |
delete_file |
删除文件 | path |
rename_file |
重命名/移动文件 | old_path、new_path |
create_directory |
创建目录 | path |
batch_file_operations |
批量操作 | operations |
set_working_directory或在初始化时设置在你的MCP客户端中配置服务器,使用适当的命令和参数。服务器通过标准输入输出进行通信,无需额外的网络配置。
enhanced-directory-context-mcp/
├── server.js # 主要的MCP服务器实现
├── package.json # Node.js包配置
├── mcp-json-config.json # 示例MCP客户端配置
└── README.md # 本说明文档
@modelcontextprotocol/sdk:核心MCP功能fs、path、child_processnpm run dev
这将启动服务器,并启用Node.js检查器以进行调试。
本项目采用MIT许可证,详情请参阅package.json。
如有问题、功能请求或想要贡献代码,请参考仓库的问题跟踪器。
⚠️ 重要提示
此MCP服务器提供强大的文件系统访问功能。请确保你信任AI助手,并了解正在执行的操作,特别是像文件删除或修改这样的破坏性操作。