DEV Community

Sualeh Fatehi
Sualeh Fatehi

Posted on

Generate MCP Tool Schemas Directly From Java Code

If you are building an MCP server, every tool you expose needs an inputSchema. MCP servers written with Spring AI support often start with a simple data class for tool inputs. Then come changes: a new field, a renamed property, or updated constraints. The JSON schema in the tool registration rarely keeps up - that means clients may send invalid payloads. By generating the schema from the source of truth — the Java type — you remove that drift.

Writing that JSON by hand is repetitive, easy to get wrong. MCP supports only a specific sub-type of the JSON Schema specification. The MCP JSON Schema library keeps the parameter schema and the code in lockstep by generating the MCP-compatible JSON Schema from a Jackson 3 annotated Java class or record.

What you get:

  • Use of Jackson 3 annotations for naming, required fields, and descriptions that carry over into the schema
  • Use of Jakarta Bean Validation to adds meaningful constraints to the schema (for example, @Max,@Min, @Positive, @PositiveOrZero, @Negative, @NegativeOrZero on numbers, or @Size, @NotBlank on strings)
  • Automatic handling of required fields, defaults, enums, and descriptions
  • Output that targets the MCP JSON Schema subset, not the entire JSON Schema specification

How it works

Add a dependency to us.fatehi:mcp-json-schema in Maven or Gradle.

<dependency>
    <groupId>us.fatehi</groupId>
    <artifactId>mcp-json-schema</artifactId>
    <version>1.0.1</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Define a parameters type as a Jackson‑annotated record or class and let the library produce the inputSchema JSON. Use annotations to describe intent, and let the library translate that into the MCP schema format.

For example:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import tools.jackson.databind.PropertyNamingStrategies;
import tools.jackson.databind.annotation.JsonNaming;

@JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class)
public record SampleParameters(
    @JsonPropertyDescription("Type of database table dependant objects.")
    @JsonProperty(defaultValue = "NONE", required = true)
    DependantObjectType dependantObjectType,

    @JsonPropertyDescription("Table name.")
    String tableName) {

  public enum DependantObjectType 
    { NONE, COLUMNS, INDEXES, FOREIGN_KEYS, TRIGGERS }
}
Enter fullscreen mode Exit fullscreen mode

Next, generate the MCP inputSchema:

import us.fatehi.mcp_json_schema.McpJsonSchemaUtility;

// Provide this value as the tool's input_schema 
// in your Spring AI MCP server implementation
String inputSchemaJson = 
  McpJsonSchemaUtility.inputSchema(SampleParameters.class);
Enter fullscreen mode Exit fullscreen mode

Prefer a JsonNode for programmatic changes? Use:

var schemaNode = 
  McpJsonSchemaUtility.generateJsonSchema(SampleParameters.class);
Enter fullscreen mode Exit fullscreen mode

The source code is available at sualeh/mcp-json-schema

Top comments (0)