ToolArgsParser

class ToolArgsParser(args: JsonObject?)

Parser for MCP tool arguments with soft assertion-style validation. Collects all errors and reports them together instead of failing on the first error.

Usage:

val parser = ToolArgsParser(args)
val previewId = parser.requireString("previewId")
val label = parser.requireString("label")
val index = parser.requireInt("index")
parser.validate() // throws if any errors occurred

Constructors

Link copied to clipboard
constructor(args: JsonObject?)

Functions

Link copied to clipboard
fun addError(message: String)

Adds a custom error message.

Link copied to clipboard

Returns an error message string if there are errors, or null if validation passed. Useful for returning error results without throwing exceptions.

Link copied to clipboard

Returns the list of collected errors.

Link copied to clipboard
fun getRaw(name: String): JsonElement?

Gets the raw JsonElement for a given argument name.

Link copied to clipboard

Returns true if there are any validation errors.

Link copied to clipboard
fun optionalInt(name: String, default: Int): Int

Gets an optional integer argument with a default value.

Link copied to clipboard
fun optionalString(name: String, default: String): String

Gets an optional string argument with a default value.

Link copied to clipboard
fun requireInt(name: String, description: String? = null): Int

Requires an integer argument. Returns the value if present and valid, or 0 if missing/invalid (error is collected).

Link copied to clipboard
fun requireString(name: String, description: String? = null): String

Requires a string argument. Returns the value if present, or an empty string if missing (error is collected).

Link copied to clipboard
fun validate()

Validates all collected arguments. Throws ToolArgsValidationException if any errors were collected.