Class ::nsmcp::Tool (public)

 ::nx::Class ::nsmcp::Tool[i]

Defined in /usr/local/ns/tcl/nsmcp/01-tools.tcl

Testcases:
No testcase defined.
Source code:
    :property name:required
    :property description:required
    :property permission:required
    :property {adminSummary {}}
    :property {adminExample {}}
    :property {outputSchema {}}
    :property {inputSchema {
        {
            "type": "object",
            "properties": {},
            "additionalProperties": false
        }
    }}
    :method init {} {
        next
        # Source schemas are JSON text; runtime consumers use typed triples.
        # Subclasses overriding init must chain here with next.
        set :inputSchema [ns_json parse -output triples -- ${:inputSchema}]
        if {[lindex ${:inputSchema} 1] ne "object"} {
            error "Tool inputSchema must be a JSON object"
        }
    }
    :public method name {} {return ${:name}}
    :public method permission {} {return ${:permission}}
    :public method describe {} {
        set members [list name string ${:name}  description string ${:description} inputSchema object [lindex ${:inputSchema} 2]  annotations object {readOnlyHint boolean true destructiveHint boolean false openWorldHint boolean false}]
        if {${:outputSchema} ne ""} {
            lappend members outputSchema object [lindex ${:outputSchema} 2]
        }
        return [list {} object $members]
    }
    :method jsonArguments {arguments} {
        #
        # Convert a JSON argument object in ns_json's rooted typed-triples
        # representation to NX named arguments, using inputSchema types.
        # The input is a three-element Tcl list: name, type, members, where
        # type is object and members is a sequence of name/type/value triples.
        # It is not serialized JSON text or an ordinary Tcl dictionary.
        # Parse JSON text first with ns_json parse -output triples.
        #
        # Check JSON types before Tcl/NX can coerce strings to numbers or
        # booleans. This is not a general JSON Schema validator: NX handles
        # defaults/required parameters; method bodies handle ranges, enums and
        # cross-parameter constraints. Structured values retain their rooted
        # typed triples so nested JSON types are not discarded.
        #
        # @param arguments Rooted typed triples for a JSON object, as returned
        #     by ns_json parse -output triples; serialized JSON is not accepted.
        # @return Tcl list of NX named arguments (-name value pairs).
        #
        if {[llength $arguments] != 3 || [lindex $arguments 1] ne "object"
            || [llength [lindex $arguments 2]] % 3} {error "Expected a JSON arguments object"}
        set properties {}
        foreach {name type members} [ns_json triples getvalue -output triples -path properties -- ${:inputSchema}] {
            if {$type ne "object"} {error "Invalid property schema: $name"}
            dict set properties $name [list {} object $members]
        }
        set result {}; set seen {}
        foreach {key type value} [lindex $arguments 2] {
            if {![dict exists $properties $key]} {error "Unknown argument: $key"}
            if {[dict exists $seen $key]} {error "Duplicate argument: $key"}
            dict set seen $key 1
            set expected [ns_json triples getvalue -output triples -path type -- [dict get $properties $key]]
            switch -- $expected {
                integer {
                    if {$type ne "number" || ![regexp {^-?(?:0|[1-9][0-9]*)$} $value]} {
                        error "Argument $key must be a JSON integer"
                    }
                }
                string - number - boolean - object - array - null {
                    if {$type ne $expected} {error "Argument $key must be a JSON $expected"}
                }
                default {error "Unsupported JSON argument type for $key: $expected"}
            }
            if {$type in {object array null}} {set value [list {} $type $value]}
            lappend result -$key $value
        }
        return $result
    }
    :public method validate {arguments} {
        # The first tool accepts exactly {}. Subclasses must override this
        # together with inputSchema; no partial JSON Schema validator is claimed.
        if {[lindex $arguments 1] ne "object" || [lindex $arguments 2] ne ""} {
            return -code error -errorcode {NSMCP RPC -32602} "Expected an empty arguments object"
        }
    }
    :public method execute {context arguments} {error "abstract Tool.execute"}
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: