实战案例

三个由浅入深的任务,覆盖本教程:陌生仓库上手、Plan Mode 多文件功能、以及规则 + MCP 的团队工作流。把提示词贴进 Agent;命令按你的仓库改。不要在生产仓库关着审查跑这些练习。


案例 1:上手陌生仓库

目标: 两小时内能指出入口、测试命令和一处安全的小改动——不靠猜。

准备: 用 Git 克隆任意你不熟的中小型开源库,在 Cursor 里打开仓库根

git clone <repo-url> unfamiliar-repo
cd unfamiliar-repo
cursor .
git clone <repo-url> unfamiliar-repo
cd unfamiliar-repo
cursor .

Ctrl+I / Cmd+I,按顺序发送:

Explain this codebase. Point me to the main entry points, key modules,
and anything I should read before making changes.
Quote the exact commands this repo uses for install, test, lint, and
dev server. Prefer package.json / Makefile / pyproject.toml / go.mod.
Do not invent scripts.

然后在本机跑它引用的安装与测试(示例):

# 以仓库读到的为准
npm ci
npm test
npm ci
npm test

若测试因缺少密钥失败,把错误贴回 Agent:These tests failed on a clean clone. Which env vars are required? Do not weaken tests.

再要三处小改进,只批准一处(见 快速上手)。审查 diff,再跑同一组测试。

完成标准:

检查通过
你能指出入口文件是 / 否
测试命令来自仓库,不是编造是 / 否
只合并了一处小 diff,测试与克隆时一样绿(或你理解失败)是 / 否

案例 2:用 Plan Mode 做多文件功能

目标: 加一个不改鉴权的只读 /health(或等价)端点,并带上仓库风格的测试。

准备: 选一个已有 HTTP 层的应用(Express、FastAPI、Rails、Go net/http…)。新建分支:

git checkout -b practice/health-endpoint

Shift+Tab 进入 Plan Mode,发送:

Add a public health endpoint that returns JSON { "ok": true }.

Constraints:
- Reuse the existing HTTP stack and folder layout
- Do not change authentication or existing routes
- Do not add dependencies
- Follow existing test style

Research the repo, ask clarifying questions, and write a plan that lists
every file, the test command, and rollback. Do not edit files until I approve.

审计划:

  1. 文件是否只有路由 + 测试(外加框架要求的注册处)?
  2. 测试命令是否已存在?
  3. 有没有「顺手重构」?

把计划 Save to workspace,例如 docs/plans/health-endpoint.md。批准构建。构建后:

Run only the commands listed in the plan. Paste stdout and exit codes.
If a check failed, fix it without expanding scope.

自己再跑一遍。若实现偏了:Restore 检查点或 git restore .,收紧计划,再构建(见 Plan Mode)。

完成标准:

检查通过
构建前计划已获批准是 / 否
curl 或测试客户端返回 {"ok":true}(或仓库约定的等价形状)是 / 否
已有鉴权测试仍然通过是 / 否
diff 能对上计划中的文件列表是 / 否

PowerShell 快速探测(端口按项目改):

Invoke-RestMethod http://127.0.0.1:3000/health
curl -s http://127.0.0.1:3000/health

案例 3:MCP + 规则的团队工作流

目标: 新同事(或未来的你)克隆后,Agent 自动遵守同一套规范,并能安全调用一台 MCP 服务器。

3a. 检入规则与共享说明

在仓库根:

New-Item -ItemType Directory -Force -Path .cursor\rules, docs\plans | Out-Null
mkdir -p .cursor/rules docs/plans

.cursor/rules/team.mdc

---
alwaysApply: true
---

- Read AGENTS.md before the first edit in a session
- Do not add dependencies without asking
- Never commit `.env`, credentials, or `mcp.json` secrets
- Prefer Plan Mode when a task will touch more than five files

根目录 AGENTS.md

# Agent instructions

- Install: use the commands already documented in README
- Test: use the existing script (do not invent a new runner)
- Public API changes require a plan saved under docs/plans/

提交这些文件。新开一条 Agent 聊天,故意说:Add lodash for a one-liner. 合格的 Agent 应当拒绝或先问——因为规则禁止擅自加依赖。

3b. 项目 MCP(无密钥)

无密钥的服务器放进 .cursor/mcp.json。需要 token 的用 ${env:NAME},不要把 token 检入。示例形状(命令换成你们真实、已审查过的服务器):

{
  "mcpServers": {
    "docs": {
      "command": "npx",
      "args": ["-y", "mcp-server"],
      "env": {
        "API_KEY": "${env:API_KEY}"
      }
    }
  }
}

插值与传输见 MCP 集成;协议细节见 MCP 教程

在 Customize 里启用服务器,Output → MCP Logs 确认已连接。然后:

Use only the enabled MCP tools to fetch the current issue/doc titled
"<name>". Summarize acceptance criteria. Do not edit product code.
Then draft a Plan Mode plan that implements those criteria with our
AGENTS.md constraints.

3c. 团队约定

约定做法
每人本机登录 Marketplace / OAuth不要分享 refresh token
密钥系统环境变量或本地 .env(已 gitignore)
审查PR 必须包含规则 / MCP 变更;展开工具参数截图可选
与 CLI Agent 共用同一份 AGENTS.md;Cursor 专属 glob 放 .mdc(见 协作

完成标准:

检查通过
另一台机器(或第二窗口)打开同一仓库时规则仍在是 / 否
Agent 未经询问不会加依赖是 / 否
MCP 在 Available Tools 中;日志无认证错误是 / 否
计划引用了 MCP 摘要,且遵守 AGENTS.md是 / 否

反模式

反模式改进
跳过解释,直接「修所有 bug」案例 1 先画地图
Plan Mode 还没批准就开始改写明 Do not edit files until I approve
把 API key 写进 mcp.json${env:NAME} + gitignore
三份互相打架的规则文件共享内容只放 AGENTS.md
用检查点代替 Git练习结束就分支 + 提交

下一步

评论