Skip to content

Conversation

@24kpure
Copy link

@24kpure 24kpure commented Dec 10, 2025

design

During the development of MCP tools, when a method requires multiple parameters, using the @McpToolParam annotation may reduce code readability. To address this, inspired by the @RequestBody(org.springframework.web.bind.annotation.RequestBody) annotation in the Spring framework, I have added the @McpToolBody annotation. This allows parameters to be passed as a structured business object, thereby improving code clarity and maintainability.

example

    // use  @McpToolParam
    @McpTool(name = "calculate-area", 
             description = "Calculate the area of a rectangle")
    public AreaResult calculateRectangleArea(
            @McpToolParam(description = "Width of the rectangle", required = true) double width,
            @McpToolParam(description = "Height of the rectangle", required = true) double height) {
        double area = width * height;
        return new AreaResult(area, "square units");
    }

   // use  @McpToolBody
   @McpTool(name = "calculate-area", 
             description = "Calculate the area of a rectangle")
    public AreaResult calculateRectangleArea(
            @McpToolBody CalculateAreaRequest toolBody) {
        double area = request.getWidth() * request.getHeight();
        return new AreaResult(area, "square units");
    }

    public static class CalculateAreaRequest {
        @McpToolParam(description = "Width of the rectangle", required = true)
        private double width;
        @McpToolParam(description = "Height of the rectangle", required = true) 
        private double height;

        public double getWidth() {
            return width;
        }

        public void setWidth(double width) {
            this.width = width;
        }

        public double getHeight() {
            return height;
        }

        public void setHeight(double height) {
            this.height = height;
        }
    }

changelist

  • add file McpToolBody
  • correct the syntax errors in the comments of file `McpProgress
  • correct the syntax errors in the comments of file `McpPrompt
  • add McpToolBody input support in org.springaicommunity.mcp.method.tool.utils.JsonSchemaGenerator#internalGenerateFromMethodArguments
  • Add McpToolBody parameter conversion support in org.springaicommunity.mcp.method.tool.AbstractMcpToolMethodCallback#buildMethodArguments
  • add McpToolBody example in readme

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant