nsmcp::Protocol method handle (public)

 <instance of nsmcp::Protocol[i]> handle context body

Defined in /usr/local/ns/tcl/nsmcp/protocol.tcl

Parse and handle one JSON-RPC request body. Enforce JSON structure and size bounds, then encode protocol errors or dispatch results.

Parameters:
context (required)
Authenticated request context carrying the principal, grants and selected server.
body (required)
Serialized HTTP or JSON-RPC request body.
Returns:
The serialized protocol response.

Testcases:
No testcase defined.
Source code:
set null {{} null {}}
if {[catch {
    ns_json parse -maxdepth 32 -maxstring 65536 -maxcontainer 256  -validatenumbers -output triples -- $body
} request]} {
    return [dict create status 400 body [:error $null -32700 "Parse error"]]
}
set id $null
set hasId 0
if {[catch {
    :uniqueMembers $request
    if {[lindex $request 1] ne "object"} {
        error "expected object"
    }
    set hasId [expr {![catch {
        ns_json triples gettype -path id -- $request
    } idType]}]
    if {$hasId} {
        set id [list {} $idType [:parameter $request id {string number}]]
    }
    if {[:parameter $request jsonrpc string] ne "2.0"} {
        error "invalid JSON-RPC version"
    }
    set method [:parameter $request method string]
    set params {{} object {}}
    if {![catch {
        ns_json triples gettype -path params -- $request
    }]} {
        set params [list {} object [:parameter $request params object]]
    }
}]} {
    return [dict create status 400 body [:error $null -32600 "Invalid request"]]
}
if {!$hasId} {
    # Notifications cannot invoke tools. No asynchronous work is supported.
    return [dict create status 202 body {}]
}
if {[catch {
    :dispatch $context $method $params
} result options]} {
    set code -32603
    set message "Internal error"
    set ec [dict get $options -errorcode]
    if {[lrange $ec 0 1] eq {NSMCP RPC}} {
        set code [lindex $ec 2]
        set message $result
    }
    return [dict create status 200 body [:error $id $code $message]]
}
return [dict create status 200 body [ns_json value -- [list {} object  [list jsonrpc string 2.0 id [lindex $id 1] [lindex $id 2]  result object [lindex $result 2]]]]]
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: