Class McpToolArgs

java.lang.Object
com.tnsai.mcp.model.McpToolArgs

public final class McpToolArgs extends Object
Type-safe builder and accessor for MCP tool arguments.

MCP tool arguments are JSON objects represented as Map<String, Object>. This class provides:

  • Null-safe argument construction (unlike Map.of() which rejects nulls)
  • Typed accessor methods for reading argument values
  • Fluent builder API
  • Immutable result map

Usage


 // Build arguments
 Map<String, Object> args = McpToolArgs.builder()
     .put("path", "/tmp/test.txt")
     .put("recursive", true)
     .put("maxDepth", 5)
     .build();

 // Read arguments with type safety
 McpToolArgs toolArgs = McpToolArgs.wrap(args);
 String path = toolArgs.getString("path");
 boolean recursive = toolArgs.getBoolean("recursive", false);
 int maxDepth = toolArgs.getInt("maxDepth", 10);
 
  • Method Details

    • wrap

      public static McpToolArgs wrap(Map<String,Object> args)
      Wraps an existing argument map for typed access.
      Parameters:
      args - the argument map (may be null)
      Returns:
      a McpToolArgs wrapper
    • builder

      public static McpToolArgs.Builder builder()
      Creates a new builder.
      Returns:
      new builder instance
    • of

      public static Map<String,Object> of(String key, Object value)
      Creates a single-argument map. Null values are permitted.
      Parameters:
      key - the argument name
      value - the argument value (may be null)
      Returns:
      unmodifiable argument map
    • of

      public static Map<String,Object> of(String k1, Object v1, String k2, Object v2)
      Creates a two-argument map. Null values are permitted.
    • of

      public static Map<String,Object> of(String k1, Object v1, String k2, Object v2, String k3, Object v3)
      Creates a three-argument map. Null values are permitted.
    • getString

      public String getString(String key)
      Gets a string value.
      Parameters:
      key - the argument name
      Returns:
      the value, or null if absent or not a string
    • getString

      public String getString(String key, String defaultValue)
      Gets a string value with default.
    • getInt

      public int getInt(String key, int defaultValue)
      Gets an integer value.
      Parameters:
      key - the argument name
      defaultValue - fallback if absent or not a number
      Returns:
      the integer value
    • getLong

      public long getLong(String key, long defaultValue)
      Gets a long value.
    • getDouble

      public double getDouble(String key, double defaultValue)
      Gets a double value.
    • getBoolean

      public boolean getBoolean(String key, boolean defaultValue)
      Gets a boolean value.
    • getList

      public <T> List<T> getList(String key)
      Gets a list value.
    • getMap

      public Map<String,Object> getMap(String key)
      Gets a nested map value.
    • has

      public boolean has(String key)
      Checks if a key exists.
    • toMap

      public Map<String,Object> toMap()
      Returns the raw argument map.
    • size

      public int size()
      Returns the number of arguments.
    • toString

      public String toString()
      Overrides:
      toString in class Object