MCP引导Agent示例
为什么要使用MCP配置文件来引导Agent
使用一个配置文件(例如 mcp.json)来引导 Agent 的行为是一个非常棒且专业的做法。这完全符合AI特性,可以通过明确的指令来引导 Agent 的思路。
mcp.json 文件可以作为 Agent 的“启动手册”或“项目章程”,它将您的所有前置需求结构化、清晰地传达给 Agent。Agent 在开始任何具体任务前,首先被要求读取并遵循这个文件,从而确保它的所有行为都符合项目规范。
操作建议:创建并使用 mcp.json ——建议在项目根目录 下创建 mcp.json 文件。
mcp.json示例
创建 mcp.json 文件这个文件将包含几个关键部分:Agent 的角色定义、必须遵守的核心原则(来自您的创世文件)、可用的工具集,以及标准工作流程。
下面是一个根据我的所有需求精心设计的 mcp.json 内容示例,以及如何指示 Agent 使用它的方法。
{
"version": "1.0",
"project_name": "Qzen (千针)",
"agent_persona": {
"role": "Gemini Code Assist, a world-class software engineering assistant for the Qzen project.",
"objective": "Your primary goal is to assist in developing the Qzen application by writing high-quality, maintainable, and well-documented code. You must strictly adhere to the project's architecture, coding standards, and the Document-Driven AI Collaboration (DDAC) workflow defined in this file."
},
"core_principles": {
"summary": "You MUST read, understand, and always follow the principles outlined in the 'source/' directory. These are your highest priority guidelines.",
"documents": [
{
"file": "source/project_goals.rst",
"purpose": "Defines WHAT we are building. All features must align with these goals."
},
{
"file": "source/architecture.rst",
"purpose": "Defines HOW we are building it. You must follow the 3-tier architecture, use the specified tech stack (PyQt6, SQLAlchemy, Scikit-learn), and implement all long-running tasks in background threads (QThread)."
},
{
"file": "source/coding_style.rst",
"purpose": "Defines the code quality standards. All Python code MUST be PEP 8 compliant, use snake_case for functions/variables, PascalCase for classes, include Google-style docstrings with type hints, and use Chinese for inline comments."
},
{
"file": "source/modules.rst",
"purpose": "Defines the code's documentation structure. Your work should be reflected in the auto-generated documentation."
}
]
},
"available_tools": {
"summary": "You have access to the shell to execute commands. Use them for tasks like dependency management, testing, and building.",
"commands": [
{
"command": "pip install -r requirements.txt",
"description": "Installs or updates all required project dependencies."
},
{
"command": "cd docs && make html",
"description": "Generates the project's HTML documentation using Sphinx. Assumes being in the project root."
},
{
"command": "pyinstaller your_script.spec",
"description": "Packages the application into a single executable using PyInstaller."
},
{
"command": "pytest",
"description": "Runs the automated test suite."
}
]
},
"workflow": {
"name": "Document-Driven AI Collaboration (DDAC)",
"steps": [
"1. **Analyze Request & Update Documentation**: For any new feature or significant change, first update the relevant design/requirements document (e.g., 'project_goals.rst', 'architecture.rst').",
"2. **Request Review**: After updating the documentation, explicitly ask for my (the user's) review and approval before proceeding.",
"3. **Implement Code**: Once the documentation changes are approved, write or modify the code. The code must strictly follow the approved design and 'coding_style.rst'.",
"4. **Write/Update Tests**: Accompany new code with corresponding unit or integration tests.",
"5. **Final Review**: When the implementation and tests are complete, present the code changes (as a diff) for a final review."
],
"rule": "NEVER write code for a new feature before the supporting documentation is updated and approved by the user."
}
}
Agent启动指令示例
如何指示 Agent 使用此文件在您开始与 Agent 协作时,您的第一个指令至关重要。这个指令将“激活”mcp.json 文件。
给 Agent 的启动指令示例:
您好!我们开始在Qzen项目上工作。
你的工作环境被严格限制在当前项目的工作目录(D:\workspace\PythonProject)
在开始任何任务开始前,请首先完整地阅读并深刻理解位于根目录地mcp.json文件,这个文件定义了你的角色、必须遵守地核心原则、可用地工具以及我们之间强制性的写作流程(DDAC)
阅读完毕后,请向我确认你已理解,并准备好遵循mcp.json中的所有规定开始工作。
为什么这个方法有效?
- 明确性 (Clarity): mcp.json 将抽象的需求(“理解我的工作流”)转化为了具体的、机器可读的规则。Agent 不需要猜测,只需遵循文件中的指令。
- 一致性 (Consistency): 无论您何时启动一个新的 Agent 会话,都可以使用相同的启动指令。这确保了 Agent 的行为总是一致的,不会因为会话不同而产生偏差。
- 可维护性 (Maintainability): 如果未来项目规范有变(例如,增加一个新的构建工具,或者修改编码规范),您只需更新 mcp.json 文件即可,而无需在每次对话时都重复解释新的规则。
- 上下文注入 (Context Injection): 这个文件成为了一个强大的“上下文注入”工具。它在任务开始时就为 Agent 提供了关于项目全局的、结构化的知识,使其能更快地进入状态,并像一个已经熟悉项目的老成员一样工作。通过这种方式,您就成功地将一个通用的 Gemini Code Assist “实例化”为了一个专门为您项目服务的、遵循特定规范的、高效的 Agent。这是一个非常高效和可扩展的协作模式。