工具与技能Tools & skills · 单元Unit 05
工具与 MCP:让模型安全地调用外部能力Tools and MCP: let the model call external capabilities safely
工具 schema 就是提示词的一部分。学完本单元,你能设计可被正确选择、可靠执行、可观测且可权限收口的工具,并把 MCP 服务接入同一套执行体系。Tool schemas are part of the prompt. After this unit you can design tools that are selected correctly, execute reliably, stay observable and pass permission checks, and expose MCP services through the same execution system.
学习目标Learning goals
- 把函数注册为工具并生成正确 schemaRegister functions as tools with correct schemas
- 使用工具组与权限规则收敛执行范围Scope execution with tool groups and permission rules
- 连接 MCP 服务并管理连接生命周期Connect MCP services and manage connection lifecycles
概念闪卡Concept cards
点击卡片翻面查看解释。Click a card to flip it and reveal the explanation.
关键 APIKey APIs
点击进入对应的 Python 或 Java 参考页。Open the exact Python or Java reference for each type.
问题巩固Check yourself
完成 10 道题;答错会自动进入错题本。Answer all ten questions. Wrong answers are added to your wrong-answer book automatically.
01Python 中注册和执行工具的组件是?Which Python component registers and executes tools?
解析Why Toolkit 负责 add_tool、get_tool_schemas 和 call_tool 等核心操作。Toolkit provides core operations such as add_tool, get_tool_schemas and call_tool.
02向 Python Toolkit 添加工具的方法是?How do you add a tool to a Python Toolkit?
解析Why add_tool 接受函数或工具对象并生成可暴露给模型的 schema。add_tool accepts a function or tool object and generates a schema exposed to the model.
03执行一个已注册工具的方法是?How do you execute a registered tool?
解析Why call_tool 负责参数校验、执行、权限检查和结果封装。call_tool handles argument validation, execution, permission checks and result wrapping.
04按业务域批量启停工具应使用?What do you use to activate tools per business domain?
解析Why ToolGroup 支持按组启停和技能暴露,是权限收口的常用手段。ToolGroup supports per-group activation and skill exposure, a common way to scope permissions.
05Python 中连接 MCP 服务的客户端是?Which Python client connects to MCP services?
解析Why MCPClient 负责 connect、list_tools 和 get_tool;MCPTool 是暴露后的工具包装。MCPClient provides connect, list_tools and get_tool; MCPTool wraps an exposed remote tool.
06MCP 支持哪些常见传输方式?Which transports does MCP commonly support?
解析Why 本地子进程用 stdio,远程服务用 SSE 或 streamable HTTP,并注意鉴权头透传。Use stdio for local subprocesses and SSE or streamable HTTP for remote services, forwarding auth headers carefully.
07Java 中把方法标记为工具使用哪个注解?Which Java annotation marks a method as a tool?
解析Why @Tool 声明工具名称、描述、只读性和并发安全等元数据。@Tool declares the tool name, description, read-only and concurrency-safety metadata.
08Java 中描述工具方法参数使用哪个注解?Which Java annotation describes tool method parameters?
解析Why @ToolParam 标注参数名称、是否必填和描述,直接影响模型填参质量。@ToolParam marks parameter name, required flag and description, directly affecting argument quality.
09Java 中注册工具对象的方法是?Which Java method registers a tool object?
解析Why Toolkit.registerTool 同时支持普通工具、AgentTool 和 schema-only 工具。Toolkit.registerTool supports regular tools, AgentTool and schema-only tools.
10MCP 连接的正确生命周期管理方式是?What is the correct MCP connection lifecycle?
解析Why MCP 连接是长生命周期资源,每请求新建会导致握手风暴和端口耗尽。MCP connections are long-lived resources; per-request creation causes handshake storms and port exhaustion.