Class McpSchemaConverter

java.lang.Object
org.springframework.ai.mcp.modern.McpSchemaConverter

public final class McpSchemaConverter extends Object
Converts between the legacy (io.modelcontextprotocol.spec.McpSchema) and the modern (io.modelcontextprotocol.modern.McpSchema) schema types.

Both type hierarchies serialize to the same JSON shapes, so the conversion is a mapper-based round trip. Modern-only fields such as resultType, ttlMs and cacheScope take their default values. This is what allows annotated methods and ToolCallbacks written against the legacy types to be served by a 2026-07-28 server without changes.

Since:
2.1.0
Author:
Christian Tzolov
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    convert(Object source, Class<T> targetType)
    Converts any JSON-compatible value to the given target type using the default MCP JSON mapper.
    static io.modelcontextprotocol.spec.McpSchema.CallToolRequest
    toLegacy(io.modelcontextprotocol.modern.McpSchema.CallToolRequest request)
    Builds the legacy request a handler expects from a modern tools/call request.
    static io.modelcontextprotocol.spec.McpSchema.CompleteRequest
    toLegacy(io.modelcontextprotocol.modern.McpSchema.CompleteRequest request)
     
    static io.modelcontextprotocol.spec.McpSchema.GetPromptRequest
    toLegacy(io.modelcontextprotocol.modern.McpSchema.GetPromptRequest request)
     
    static io.modelcontextprotocol.spec.McpSchema.ReadResourceRequest
    toLegacy(io.modelcontextprotocol.modern.McpSchema.ReadResourceRequest request)
     
    static io.modelcontextprotocol.modern.McpException
    toModern(io.modelcontextprotocol.spec.McpError error)
    Converts a legacy protocol error into its modern counterpart so that it is answered with the same JSON-RPC error code and data.
    static io.modelcontextprotocol.modern.McpSchema.CallToolResult
    toModern(io.modelcontextprotocol.spec.McpSchema.CallToolResult result)
     
    static io.modelcontextprotocol.modern.McpSchema.CompleteReference
    toModern(io.modelcontextprotocol.spec.McpSchema.CompleteReference reference)
     
    static io.modelcontextprotocol.modern.McpSchema.CompleteResult
    toModern(io.modelcontextprotocol.spec.McpSchema.CompleteResult result)
     
    static io.modelcontextprotocol.modern.McpSchema.GetPromptResult
    toModern(io.modelcontextprotocol.spec.McpSchema.GetPromptResult result)
     
    static io.modelcontextprotocol.modern.McpSchema.Prompt
    toModern(io.modelcontextprotocol.spec.McpSchema.Prompt prompt)
     
    static io.modelcontextprotocol.modern.McpSchema.ReadResourceResult
    toModern(io.modelcontextprotocol.spec.McpSchema.ReadResourceResult result)
     
    static io.modelcontextprotocol.modern.McpSchema.Resource
    toModern(io.modelcontextprotocol.spec.McpSchema.Resource resource)
     
    static io.modelcontextprotocol.modern.McpSchema.ResourceTemplate
    toModern(io.modelcontextprotocol.spec.McpSchema.ResourceTemplate resourceTemplate)
     
    static io.modelcontextprotocol.modern.McpSchema.Tool
    toModern(io.modelcontextprotocol.spec.McpSchema.Tool tool)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • convert

      public static <T> T convert(Object source, Class<T> targetType)
      Converts any JSON-compatible value to the given target type using the default MCP JSON mapper.
      Type Parameters:
      T - the target type
      Parameters:
      source - the value to convert
      targetType - the type to convert to
      Returns:
      the converted value
    • toModern

      public static io.modelcontextprotocol.modern.McpSchema.CallToolResult toModern(io.modelcontextprotocol.spec.McpSchema.CallToolResult result)
    • toModern

      public static io.modelcontextprotocol.modern.McpSchema.ReadResourceResult toModern(io.modelcontextprotocol.spec.McpSchema.ReadResourceResult result)
    • toModern

      public static io.modelcontextprotocol.modern.McpSchema.GetPromptResult toModern(io.modelcontextprotocol.spec.McpSchema.GetPromptResult result)
    • toModern

      public static io.modelcontextprotocol.modern.McpSchema.CompleteResult toModern(io.modelcontextprotocol.spec.McpSchema.CompleteResult result)
    • toModern

      public static io.modelcontextprotocol.modern.McpSchema.Tool toModern(io.modelcontextprotocol.spec.McpSchema.Tool tool)
    • toModern

      public static io.modelcontextprotocol.modern.McpSchema.Resource toModern(io.modelcontextprotocol.spec.McpSchema.Resource resource)
    • toModern

      public static io.modelcontextprotocol.modern.McpSchema.ResourceTemplate toModern(io.modelcontextprotocol.spec.McpSchema.ResourceTemplate resourceTemplate)
    • toModern

      public static io.modelcontextprotocol.modern.McpSchema.Prompt toModern(io.modelcontextprotocol.spec.McpSchema.Prompt prompt)
    • toModern

      public static io.modelcontextprotocol.modern.McpSchema.CompleteReference toModern(io.modelcontextprotocol.spec.McpSchema.CompleteReference reference)
    • toModern

      public static io.modelcontextprotocol.modern.McpException toModern(io.modelcontextprotocol.spec.McpError error)
      Converts a legacy protocol error into its modern counterpart so that it is answered with the same JSON-RPC error code and data.
      Parameters:
      error - the legacy error
      Returns:
      the modern exception
    • toLegacy

      public static io.modelcontextprotocol.spec.McpSchema.CallToolRequest toLegacy(io.modelcontextprotocol.modern.McpSchema.CallToolRequest request)
      Builds the legacy request a handler expects from a modern tools/call request. The MRTR fields (inputResponses, requestState) have no legacy counterpart and are dropped; handlers that need them take the modern request.
    • toLegacy

      public static io.modelcontextprotocol.spec.McpSchema.ReadResourceRequest toLegacy(io.modelcontextprotocol.modern.McpSchema.ReadResourceRequest request)
    • toLegacy

      public static io.modelcontextprotocol.spec.McpSchema.GetPromptRequest toLegacy(io.modelcontextprotocol.modern.McpSchema.GetPromptRequest request)
    • toLegacy

      public static io.modelcontextprotocol.spec.McpSchema.CompleteRequest toLegacy(io.modelcontextprotocol.modern.McpSchema.CompleteRequest request)