本项目可让编码代理MCP访问代码检查、测试、格式化等功能,只需一个YAML文件即可实现。
uv tool install hooks-mcp
actions:
- name: "all_tests"
description: "Run all tests in the project"
command: "uv run python -m pytest ./tests"
- name: "check_format"
description: "Check if the source code is formatted correctly"
command: "uvx ruff format --check ."
- name: "typecheck"
description: "Typecheck the source code"
command: "uv run pyright ."
- name: "test_file"
description: "Run tests in a specific file or directory"
command: "python -m pytest $TEST_PATH"
parameters:
- name: "TEST_PATH"
type: "project_file_path"
description: "Path to test file or directory"
uvx hooks-mcp
有关更多运行时选项,包括如何在 Cursor、Windsurf 等中运行,请参阅 运行 HooksMCP。
.env
文件、定义所需的机密信息而无需提交、支持退出代码/标准输出/标准错误等。安装步骤如下:
uv tool install hooks-mcp
hooks_mcp.yaml
文件。uvx hooks-mcp
以下是一个简单的 hooks_mcp.yaml
文件示例:
actions:
- name: "all_tests"
description: "Run all tests in the project"
command: "uv run python -m pytest ./tests"
- name: "check_format"
description: "Check if the source code is formatted correctly"
command: "uvx ruff format --check ."
- name: "typecheck"
description: "Typecheck the source code"
command: "uv run pyright ."
- name: "test_file"
description: "Run tests in a specific file or directory"
command: "python -m pytest $TEST_PATH"
parameters:
- name: "TEST_PATH"
type: "project_file_path"
description: "Path to test file or directory"
hooks_mcp.yaml
文件用于定义通过 MCP 服务器公开的工具。
可参考本项目的 hooks_mcp.yaml 作为示例。
server_name
(可选):MCP 服务器的名称(默认:"HooksMCP")server_description
(可选):MCP 服务器的描述(默认:"通过 MCP 公开的特定于项目的开发工具")actions
(必需):操作定义数组actions
数组中的每个操作可以有以下字段:
name
(必需):工具的唯一标识符description
(必需):工具功能的可读描述command
(必需):要执行的 CLI 命令。可以包含动态参数,如 $TEST_FILE_PATH
。parameters
(可选):命令中使用的每个参数的定义。run_path
(可选):命令应从项目根目录开始执行的相对路径。对于单仓库项目很有用。timeout
(可选):命令执行的超时时间(秒)(默认:60 秒)操作的 parameters
数组中的每个参数可以有以下字段:
name
(必需):要替换到命令中的参数名称。例如 TEST_FILE_PATH
。type
(必需):以下参数类型之一:
project_file_path
:项目内的本地路径,相对于项目根目录。经过验证,确保它在项目边界内且存在。required_env_var
:服务器启动前必须设置的环境变量。调用模型不指定。optional_env_var
:可选的环境变量。调用模型不指定。insecure_string
:来自模型的任何字符串。不进行验证。使用时需谨慎。description
(可选):参数的可读描述default
(可选):参数的默认值这种参数类型通过验证路径是否在项目边界内来确保安全性:
- name: "test_file"
description: "Run tests in a specific file"
command: "python -m pytest $TEST_FILE"
parameters:
- name: "TEST_FILE"
type: "project_file_path"
description: "Path to test file"
default: "./tests"
服务器将验证 TEST_FILE
是否在项目内且存在。
这些参数必须在服务器启动前在环境中设置。如果未设置,服务器将在启动时失败,并要求用户设置这些变量。
- name: "deploy"
description: "Deploy the application"
command: "deploy-tool --key=$DEPLOY_KEY"
parameters:
- name: "DEPLOY_KEY"
type: "required_env_var"
description: "Deployment key for the service"
HooksMCP 将从环境中加载环境变量,以及工作目录中的 .env
文件中设置的任何变量。
与 required_env_var
类似,但为可选:
- name: "build"
description: "Build the application"
command: "build-tool"
parameters:
- name: "BUILD_FLAGS"
type: "optional_env_var"
description: "Additional build flags"
允许来自编码助手的任何字符串输入,不进行验证。使用时需谨慎:
- name: "grep_code"
description: "Search code for pattern"
command: "grep -r $PATTERN src/"
parameters:
- name: "PATTERN"
type: "insecure_string"
description: "Pattern to search for"
建议使用 uvx 运行 HooksMCP:
# 安装
uv tool install hooks-mcp
# 运行
uvx hooks-mcp
可选的命令行参数包括:
--working-directory
/-wd
:通常是项目根目录的路径。如果不是从项目根目录运行,则需要设置。hooks_mcp.yaml
文件的路径,如果不使用默认的 ./hooks_mcp.yaml
。大多数其他 IDE 使用 mcp.json 的变体。为 HooksMCP 创建一个条目。 注意:确保从项目根目录运行,或者在启动时手动传递工作目录:
{
"HooksMCP": {
"command": "uvx",
"args": [
"hooks-mcp",
"--working-directory",
"."
]
}
}
HooksMCP 实现了多项安全措施,有助于提高为代理提供终端命令访问的安全性:
hooks_mcp.yaml
中允许的命令,而不是任意的终端命令。project_file_path
参数都经过验证,以确保它们:
required_env_var
和 optional_env_var
参数由开发人员管理,而不是编码助手。这可以防止编码助手访问敏感变量。subprocess.run
并设置 shell=False
以防止 shell 注入shlex.split
正确分隔命令参数使用 HooksMCP 存在一些风险:
hooks_mcp.yaml
,它可以添加命令,然后通过 MCP 运行这些命令。我们不能保证它是完美的,但它可能比给代理无限制的终端访问要好。建议在容器内运行代理。
我为自己构建 Kiln 时开发了这个项目。初稿由 Qwen-Coder-405b 编写,然后由我进行编辑。有关提示信息,请参阅 初始提交。
本项目采用 MIT 许可证。