Solon v3.2.1

mcp - 与 Web Api(或控制器) 互通

</> markdown

(v3.2.1 后支持)

@Controller 类上添加 @McpServerEndpoint 注解,可以把 WebApi 快速转为 McpTool 服务。

反之在 @McpServerEndpoint 类上添加 @Controller,并替换为通用注解 @Mapping@Param,就可以把 McpServer 转为 WebApi。

@Mapping("/web/api")
@Controller
@McpServerEndpoint(sseEndpoint = "/mcp/sse")
public class McpServerTool {
    @Mapping(description = "查询天气预报")
    public String get_weather(@Param(description = "城市位置") String location) {
        return "晴,14度";
    }
    
    @Mapping(description = "查询城市降雨量")
    public String get_rainfall(@Param(name = "location", description = "城市位置") String location) {
        if (location == null) {
            throw new IllegalStateException("arguments location is null (Assistant recognition failure)");
        }

        return "555毫米";
    }
}

也可以把 @Controller 类转换为本地 ChatModel 的工具提供者

MethodToolProvider toolProvider = new MethodToolProvider(new McpServerTool());

//例
chatModel.defaultToolsAdd(toolProvider);

使用说明(或注意事项)

  • @Mapping (通用注解)相当于 @ToolMapping(专用注解)
  • @Param (通用注解)相当于 @ToolParam(专用注解)
  • 必须申明 description 属性(否则会异常提示)
  • 入参只能是 json 基础支持类型(暂时不支持实体入参。比如:字符串,数字,布尔,日期)