mcp - 与 Web Api(或控制器) 互通
(v3.2.1 后支持)
在 @Controller
类上添加 @McpServerEndpoint
注解,可以把 WebApi 快速转为 McpServer 服务。
- 在
@Mapping
方法上,按需添加@ToolMapping
注解(@Param
注解为能用)
“反之”,在 @McpServerEndpoint
类上添加 @Controller
,就可以把 McpServer 转为 WebApi。
- 在
@ToolMapping
方法上,按需添加@Mapping
注解(@Param
注解为能用)
@Mapping("/web/api")
@Controller
@McpServerEndpoint(sseEndpoint = "/mcp/sse")
public class McpServerTool {
@ToolMapping(description = "查询天气预报")
@Mapping("get_weather")
public String get_weather(@Param(description = "城市位置") String location) {
return "晴,14度";
}
@ToolMapping(description = "查询城市降雨量")
@Mapping("get_rainfall")
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毫米";
}
}
也可以在 @Mapping
方法上,添加 @PromptMapping
或 @ResourceMapping
,或者混合添加(v3.2.2-M7 后支持)
1、注解使用说明(或注意事项)
注解 | 描述 |
---|---|
@Controller | web 控制器注解(可附加 @Mapping 使用) |
@McpServerEndpoint | mcp server 服务注解 |
@Mapping | web 注解 |
@ToolMapping | mcp tool 注解 |
@PromptMapping | mcp prompt 注解 |
@ResourceMapping | mcp resource 注解 |
- mcp 注解必须申明
description
属性(否则会异常提示)
2、可互通的注解(或对象)
注解或对象 | 描述 |
---|---|
Context | 通用上下文 |
@Header | 头注解 |
@Cookie | 小饼注解 |
@Param | 参数注解(成为 mcp inputSchema 的一部分) |
示例
@Mapping("/web/api")
@Controller
@McpServerEndpoint(sseEndpoint = "/mcp/sse")
public class McpServerTool {
@ToolMapping(description = "查询天气预报")
@Mapping("get_weather")
public String get_weather(@Param(description = "城市位置") String location, @Header("TOKEN") token) {
return "晴,14度";
}
}
更多参数注解参考:《@Mapping、@Param 用法说明》