Solon v3.3.0

mcp - 服务端更多示例

</> markdown

1、CalculatorTools(STDIO)

@McpServerEndpoint(channel = McpChannel.STDIO)
public class CalculatorTools {
    @ToolMapping(description = "将两个数字相加")
    public int add(@Param int a, @Param int b) {
        return a + b;
    }

    @ToolMapping(description = "从第一个数中减去第二个数")
    public int subtract(@Param int a, @Param int b) {
        return a - b;
    }

    @ToolMapping(description = "将两个数相乘")
    public int multiply(@Param int a, @Param int b) {
        return a * b;
    }

    @ToolMapping(description = "将第一个数除以第二个数")
    public float divide(@Param float a, @Param float b) {
        return a / b;
    }
}

2、WeatherTools(SSE)

@McpServerEndpoint(sseEndpoint = "/mcp/sse")
public class WeatherTools {
    @ToolMapping(description = "获取指定城市的当前天气")
    public String get_weather(@Param String city) {
        return "{city: '" + city + "', temperature:[10,25], condition:['sunny', 'clear', 'hot'], unit:celsius}";
    }

    //给前端用,需要严格的 json 格式
    @Produces(MimeType.APPLICATION_JSON_VALUE)
    @ResourceMapping(uri = "weather://cities", description = "获取所有可用的城市列表")
    public List<String> get_available_cities() {
        return Arrays.asList("Tokyo", "Sydney", "Tokyo");
    }

    @ResourceMapping(uri = "weather://forecast/{city}", description = "获取指定城市的天气预报资源")
    public String get_forecast(@Param String city) {
         return "{city: '" + city + "', temperature:[10,25], condition:['sunny', 'clear', 'hot'], unit:celsius}";
    }
}

3、McpGiteeSse(代理)

@McpServerEndpoint(sseEndpoint = "/mcp/sse") 
public class McpGiteeSse implements ToolProvider {
    private McpClientProvider stdioToolProvider = McpClientProvider.builder()
            .channel(McpChannel.STDIO) //表示使用 stdio
            .serverParameters(ServerParameters.builder("npx")
                    .args("-y", "@gitee/mcp-gitee@latest")
                    .addEnvVar("GITEE_API_BASE", "https://gitee.com/api/v5")
                    .addEnvVar("GITEE_ACCESS_TOKEN", "<your personal access token>")
                    .build())
            .build();

    @Override
    public Collection<FunctionTool> getTools() {
        return stdioToolProvider.getTools();
    }
}