Agent Build智能体搭建 Tutor教程
Platforms各平台 / Coding agent and SDK编程智能体与 SDK

Claude Code / Agent SDK

Claude Code is a harness with the same parts you built: a project instruction file, skills, hooks, MCP tools, subagents. The Agent SDK exposes that loop as a library. The reference build uses it with its own MCP server and skills.Claude Code 这个 Harness 的部件和你亲手搭建的一样:项目指令文件、技能、钩子、MCP 工具、子智能体。Agent SDK 把这个循环以库的形式提供出来。参考系统就把它和自己的 MCP 服务、技能配合使用。

These guides were written from vendor documentation read in October 2026. None was executed: there was no account on these platforms. Product names change often. Confirm each command against the linked source before you rely on it.这些指南依据 2026 年 10 月查阅的厂商文档编写。全部未经实际执行:编写时没有这些平台的账号。产品名称经常变化。使用前请对照链接的来源逐条核实命令。

What you own归你负责的

CLAUDE.md, skills, hook scripts, MCP servers, subagent definitions, eval scripts.CLAUDE.md、技能、钩子脚本、MCP 服务、子智能体定义、评估脚本。

What the platform runs由平台运行的

The loop, context management, permission prompts, session history.循环、上下文管理、权限提示、会话历史。

Build steps搭建步骤

01Write the project instruction file编写项目指令文件

Do this操作

  1. Put standing instructions in `CLAUDE.md` at the project root.把长期有效的指令放在项目根目录的 `CLAUDE.md` 里。
  2. Keep it short. It is charged against context on every turn. The reference build adds a rule only after it has been broken once.保持简短。它每一轮都要占用上下文。参考系统的做法是:某条规则被违反过一次之后才加进去。
You are done when完成标志A new session follows a rule that appears only in that file.新的会话遵守了一条只出现在该文件里的规则。
02Register the MCP server注册 MCP 服务

Do this操作

  1. Add the server to `.mcp.json` in the project root with an absolute interpreter path.在项目根目录的 `.mcp.json` 里添加该服务,解释器使用绝对路径。
  2. Approve the server when the session starts.会话启动时批准该服务。
.mcp.json
{
  "mcpServers": {
    "policy-desk": {
      "command": "/abs/path/to/venv/bin/python3",
      "args": ["-m", "labs.m06_tools.mcp_server"]
    }
  }
}
You are done when完成标志The session lists `search_policy` and `query_expenses`.会话列出了 `search_policy` 和 `query_expenses`。
03Share skills共享技能

Do this操作

  1. Create `.claude/skills/<name>/SKILL.md`. The description decides when it loads.创建 `.claude/skills/<name>/SKILL.md`。其中的描述决定它何时加载。
  2. Link the files from lab 8 so one edit changes both agents.链接 lab 8 的文件,这样改一处两个智能体同时生效。
bash
mkdir -p .claude/skills/expense-investigation
ln -s "$PWD/labs/m08_skills/skills/expense-investigation.md" .claude/skills/expense-investigation/SKILL.md
You are done when完成标志The skill appears in the session's skill list.该技能出现在会话的技能列表里。
04Block tool calls with a PreToolUse hook用 PreToolUse 钩子阻止工具调用

Do this操作

  1. Hooks are configured in `.claude/settings.json` for the project, or `~/.claude/settings.json` for all projects.钩子在项目的 `.claude/settings.json` 里配置,或在对所有项目生效的 `~/.claude/settings.json` 里配置。
  2. A hook group has a `matcher` and a list of handlers of type `command`.一组钩子包含一个 `matcher` 和若干 `command` 类型的处理程序。
  3. To block: exit with code 2, or print the deny decision as JSON and exit 0.要阻止调用:以退出码 2 退出,或以 JSON 输出拒绝决定并以 0 退出。
  4. A hook that times out does not block. Do not rely on a stalled hook as a gate.超时的钩子不会阻止调用。不要指望一个卡住的钩子能起到关卡作用。
.claude/settings.json
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          { "type": "command", "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/block-rm.sh" }
        ]
      }
    ]
  }
}

// block-rm.sh prints this and exits 0 to deny:
// {"hookSpecificOutput": {"hookEventName": "PreToolUse", "permissionDecision": "deny",
//   "permissionDecisionReason": "Destructive command blocked by hook"}}
You are done when完成标志The blocked command does not run and the reason is shown.被阻止的命令没有执行,并显示了原因。
05Optional: run it against local models可选:让它对接本地模型

Do this操作

  1. The reference build runs a translation proxy that accepts the vendor's API shape and forwards to local models.参考系统运行了一个协议转换代理,接受该厂商的 API 格式并转发给本地模型。
  2. Point the client at the proxy with its base URL setting, and give the proxy its own key.通过 base URL 设置让客户端指向这个代理,并给代理单独的密钥。
  3. Check the real prompt size in the engine log. A context setting that is present may not be in effect.在引擎日志里检查真实的提示词大小。写了的上下文设置未必生效。
You are done when完成标志The engine log shows the request and a prompt size within your limit.引擎日志显示了这次请求,且提示词大小在你的限制之内。
06Use the Agent SDK for your own service用 Agent SDK 构建自己的服务

Do this操作

  1. Lab 14 has a port that lists the MCP server in the options and sets `max_turns`.lab 14 里有一个移植版本,在选项中列出 MCP 服务并设置了 `max_turns`。
  2. It was not executed in the lab run. Run it with your own key.它没有在 lab 运行中执行过。请用你自己的密钥运行。
bash
pip install claude-agent-sdk
export ANTHROPIC_API_KEY=...
python3 -m labs.m14_sdk.claude_agent_sdk_agent
You are done when完成标志The answer states the cap and the amount from the tools.回答中给出了工具返回的上限和金额。

Sources来源