Location>code7788 >text

Quickly convert Java WebApi to Mcp-Server (using Solon AI MCP)

Popularity:826 ℃/2025-04-27 08:13:46

Solon-ai-mcp provides various mcp-related capabilities and supports java8, java11, java17, java21, java24. It is an important part of the solon-ai project, and can also be embedded in springboot2, springboot3, jfinal, and other projects.

Dependency package:

<dependency>
     <groupId></groupId>
     <artifactId>solon-ai-mcp</artifactId>
     <version>Latest version</version>
 </dependency>

1. Look at a simple web controller

This controller has two methods: get_weather, get the weather, get_rainfall, get the rainfall. A quick remodeling demonstration was performed later based on it.

@Mapping("/web/api")
 @Controller
 public class McpServerTool {
     @Mapping("get_weather")
     public String get_weather(String location) {
         return "Sunny, 14 degrees";
     }
    
     @Mapping("get_rainfall")
     public String get_rainfall(String location) {
         return "555mm";
     }
 }

2. Renovation plan 1: Reuse mvc annotations and add@McpServerEndpointannotation

Based on mvc annotation, add description information to methods and parametersdescriptionInformation (only suitable for solon-web projects, its annotation isdescriptionproperty). Add again@McpServerEndpointannotation

@McpServerEndpoint(sseEndpoint = "/mcp/sse")
 @Mapping("/web/api")
 @Controller
 public class McpServerTool {
     @Mapping(path="get_weather", description = "Query weather forecast")
     public String get_weather(@Param(description = "City Location") String location) {
         return "Sunny, 14 degrees";
     }
    
     @Mapping(path="get_rainfall", description = "Query city rainfall")
     public String get_rainfall(@Param(description = "City Location") String location) {
         return "555mm";
     }
 }

Reminder for this plan:

  • @Mapping(General Annotation) Equivalent to ai@ToolMapping(Special note)
  • @Param(General Annotation) Equivalent to ai@ToolParam(Special note)
  • Must statedescriptionProperties (otherwise, there will be exception prompt)
  • Use json to enter parameters to support basic types (entity parameters are not supported for the time being. For example: strings, numbers, booleans, dates)

3. Renovation plan 2: Add complete Mcp related notes on the controller

This solution is also suitable for projects such as springboot2-web, sprngboot3-web, spring-mvc or jfinal. The original controller's code does not need to be moved (there are no annotations in the original form, and it will not affect). Just like adding swagger annotations, add mcp related annotations.

Brief explanation of the annotation (?Indicates optional attributes):

annotation describe
@ToolMapping(name?, description, returnDirect?, resultConverter?) Tool Mapping
@ToolParam(name?, description, required?) Tool parameter statement

Renovated code:

@McpServerEndpoint(sseEndpoint = "/mcp/sse")
 @Mapping("/web/api")
 @RestController
 public class McpServerTool {
     @ToolMapping(description = "Query weather forecast")
     @GetMapping("get_weather")
     public String get_weather(@ToolParam(description = "City Location") String location) {
         return "Sunny, 14 degrees";
     }
    
     @ToolMapping(description = "Query city rainfall")
     @GetMapping("get_rainfall")
     public String get_rainfall(@ToolParam(description = "City Location") String location) {
         return "555mm";
     }
 }

Reminder instructions:

  • Use json to enter parameters to support basic types (entity parameters are not supported for the time being. For example: strings, numbers, booleans, dates)

4. It can also be converted to a local big model tool provider (ToolProvider)

Remove the previous two solutions@McpServerEndpointAnnotations will not be published as MCP service endpoints. Loading it with MethodToolProvider can become a local tool provider.

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

 var chatModel = (...)
                 .defaultToolsAdd(toolProvider) //Add default tools
                 .build();

 ("How is the weather in Hangzhou today?")
               .call();

5. Attached code repository

solon-ai (source code)

  • /opensolon/solon-ai
  • /opensolon/solon-ai
  • /opensolon/solon-ai

solon-ai-mcp-embedded-examples (embedded example)

  • /opensolon/solon-ai-mcp-embedded-examples
  • /opensolon/solon-ai-mcp-embedded-examples
  • /opensolon/solon-ai-mcp-embedded-examples