跳转到内容

MCP 工具接入

前面的示例使用 AgentTool 把一个 Java 方法包装成模型可以调用的工具。当工具能力已经由另一个服务通过 MCP 暴露时,就没有必要再为它写一套 HTTP 客户端和参数转换逻辑。

Feat AI 提供 McpClient 与 McpTool:前者负责连接 MCP Server、发现工具并发起远程调用;后者把发现到的 MCP Tool 适配成 Feat AI 的 AgentTool。

这里讨论的是 MCP Client,也就是让 Feat AI 使用其他服务提供的工具。如果你希望把 Feat Cloud 中的业务方法暴露给外部 MCP 客户端,请阅读 Feat Cloud MCP 服务。

先创建客户端并指定 MCP Server 的 Streamable HTTP 地址:

import tech.smartboot.feat.ai.mcp.client.McpClient;
McpClient client = McpClient.streamable(options -> {
options.debug(false).url("https://your-mcp-server.example/mcp");
});

MCP 客户端必须先完成初始化,之后才能查询工具列表或调用工具。初始化过程会与服务端协商协议版本和能力:

client.initialize();

客户端连接由应用负责管理。使用结束后应调用 close() 释放传输资源。

对于 FeatAgent,可以直接使用 McpTool.register(…)。它会初始化客户端、获取服务端工具列表,并把每个 MCP Tool 注册为 AgentTool。

import tech.smartboot.feat.ai.FeatAI;
import tech.smartboot.feat.ai.agent.FeatAgent;
import tech.smartboot.feat.ai.agent.tools.McpTool;
import tech.smartboot.feat.ai.chat.ChatModelVendor;
import tech.smartboot.feat.ai.mcp.client.McpClient;
public class McpAgentDemo {
public static void main(String[] args) throws Exception {
McpClient client = McpClient.streamable(options ->
options.debug(false).url("https://your-mcp-server.example/mcp"));
try {
FeatAgent agent = FeatAI.agent(options -> {
McpTool.register(options, client);
options.chatOptions().model(ChatModelVendor.GiteeAI.Qwen2_5_72B_Instruct);
options.maxIterations(10);
});
String result = agent.execute("请根据可用工具完成我的任务").get();
System.out.println(result);
} finally {
client.close();
}
}
}

McpTool.register(…) 会使用 MCP Server 返回的服务名称为工具添加前缀,降低多个服务之间的工具名称冲突。若服务端没有提供名称,则会回退到适配器生成的名称。

示例使用 Gitee AI 模型,模型 API Key 请按 快速开始 中的方式配置。这条路径适合快速把一个可信 MCP Server 的工具集交给 FeatAgent 使用。但需要注意:FeatAgent 的 AgentTool 注册接口本身不提供 Agent Harness 那样的工具副作用分级和审批机制。不要把来源不明、包含写入或命令执行能力的 MCP Server 当作普通只读工具接入。

Agent Harness 不使用 AgentOptions,也不能直接调用 McpTool.register(options, client)。需要由应用读取 MCP 工具列表,再逐个创建 McpTool 适配器,并为每个工具声明 ToolSpec,最后注册到 ToolRuntime。

下面展示装配方式。示例中的工具被标记为 READ,只适用于你已经确认这些 MCP 工具确实只读的服务:

import tech.smartboot.feat.ai.agent.tools.McpTool;
import tech.smartboot.feat.ai.agent.harness.ToolSpec;
import tech.smartboot.feat.ai.agent.harness.support.SimpleToolRuntime;
import tech.smartboot.feat.ai.mcp.client.McpClient;
import tech.smartboot.feat.ai.mcp.model.McpInitializeResponse;
import tech.smartboot.feat.ai.mcp.model.Tool;
McpClient client = McpClient.streamable(options ->
options.debug(false).url("https://your-mcp-server.example/mcp"));
McpInitializeResponse initialized = client.initialize();
String serverName = initialized.getServerInfo() == null
? null : initialized.getServerInfo().getName();
if (serverName == null || serverName.isEmpty()) {
serverName = "mcp";
}
final String toolPrefix = serverName;
SimpleToolRuntime.Builder runtime = SimpleToolRuntime.builder();
for (Tool remoteTool : client.listTools().getTools()) {
final String registeredName = toolPrefix + "_" + remoteTool.getName();
McpTool adapter = new McpTool(client, remoteTool) {
@Override
public String getName() {
return registeredName;
}
};
ToolSpec spec = ToolSpec.builder(adapter.getName())
.description(adapter.getDescription())
.parametersSchema(adapter.getParametersSchema())
.effect(ToolSpec.Effect.READ)
.build();
runtime.register(adapter, spec);
}
// 将 runtime.build() 传入 HarnessOptions.toolRuntime(...)

如果 MCP Server 同时提供查询和修改操作,就必须逐个判断工具的真实副作用。查询工具可以标记为 READ;会修改数据的工具应标记为 WRITE;执行命令的工具应标记为 EXEC。不要因为它们都通过 MCP 网络调用,就把所有工具统一标记为 READ。

另外,MCP 工具的网络访问目标由客户端连接地址决定。生产环境仍应限制可连接的 MCP Server 地址,并在网络层落实访问控制。

McpClient.listTools() 获取第一页工具列表。ToolListResponse 还提供 nextCursor;如果服务端启用了分页,应用可以将游标传给 listTools(nextCursor) 继续获取后续页面。

当前 McpTool.register(…) 只读取 listTools() 返回的第一页,不会自动遍历所有分页。如果你的 MCP Server 返回 nextCursor,需要自行循环读取后续页面,并逐一注册工具。

McpTool 会将 MCP 工具调用结果转换成字符串交回 Agent:

  • 文本内容会被提取并拼接。
  • 结构化内容会转换为 JSON 字符串。
  • 图片、音频和资源链接目前分别转换为 [Image]、[Audio]、[Resource] 占位文本。
  • MCP 工具返回错误时,会在结果前添加 Error: 前缀。

因此,不要假设 MCP Server 返回的多模态内容会被完整透传给模型。若任务依赖图片、音频或资源链接,需要根据实际内容类型设计额外的处理流程。

当前 Feat v2.5.0 开发线中的 MCP 客户端支持的协议版本为 2025-11-25 和 2025-06-18,并会在初始化时与服务端协商使用双方支持的版本。

当前最新的 MCP 规范 2026-07-28 尚未纳入支持范围。若需要连接采用该版本新协议的服务端,应先确认对端是否仍兼容 Feat 当前支持的协议版本。

MCP 适合把已经独立部署、由其他团队维护,或需要被多个 AI 客户端共享的能力统一暴露出来。

如果工具只在当前应用内部使用,直接实现 AgentTool 往往更简单;如果业务方法属于 Feat Cloud 应用,并希望对外提供标准 MCP 接口,可以使用 Feat Cloud 的 MCP 注解能力。