::nx::Object ::nx::Object ::nsmcp::ToolRegistry ::nsmcp::ToolRegistry call list register ::nsmcp::ToolRegistry->::nx::Object

Class ::nsmcp::ToolRegistry

::nsmcp::ToolRegistry[i] create ... \
           [ -tools (default "") ]

Defined in /usr/local/ns/tcl/nsmcp/lib/tools.tcl

Class Relations

  • class: ::nx::Class[i]
  • superclass: ::nx::Object[i]
::nx::Class create ::nsmcp::ToolRegistry \
     -superclass ::nx::Object

Methods (to be applied on instances)

  • call (scripted, public)

     <instance of nsmcp::ToolRegistry[i]> call context authorizer name \
        arguments
    Parameters:
    context (required)
    authorizer (required)
    name (required)
    arguments (required)

    Testcases:
    No testcase defined.
    if {![dict exists ${:tools} $name]} {
        return -code error -errorcode {NSMCP RPC -32602} "Unknown tool"
    }
    set tool [dict get ${:tools} $name]
    $authorizer require $context [$tool permission]
    $tool validate $arguments
    if {[catch {$tool execute $context $arguments} result options]} {
        if {[lrange [dict get $options -errorcode] 0 1] eq {NSMCP RPC}} {
            return -options $options $result
        }
        ns_log error "nsmcp: tool execution failed: $name"
        return [list {} object {content array {0 object {type string text text string {Tool execution failed; consult the server log.}}} isError boolean true}]
    }
    # Both structured output and text for clients without structured support.
    return [list {} object [list structuredContent object [lindex $result 2]  content array [list 0 object [list type string text text string [ns_json value -- $result]]]  isError boolean false]]
  • list (scripted, public)

     <instance of nsmcp::ToolRegistry[i]> list context authorizer
    Parameters:
    context (required)
    authorizer (required)

    Testcases:
    No testcase defined.
    set result {}
    foreach name [lsort [dict keys ${:tools}]] {
        set tool [dict get ${:tools} $name]
        if {[$authorizer allowed $context [$tool permission]]} {
            lappend result [$tool describe]
        }
    }
    set members {}; set index 0
    foreach description $result {
        lappend members $index object [lindex $description 2]
        incr index
    }
    return [list {} object [list tools array $members]]
  • register (scripted, public)

     <instance of nsmcp::ToolRegistry[i]> register tool
    Parameters:
    tool (required)

    Testcases:
    No testcase defined.
    set name [$tool name]
    if {[dict exists ${:tools} $name]} {error "duplicate tool: $name"}
    dict set :tools $name $tool