Class ::nsmcp::HttpTransport

::nsmcp::HttpTransport[i] create ... \
           [ -allowedOrigins (default "") ] \
           [ -authenticator:required authenticator:required ] \
           [ -maxBodyBytes:integer (default "65536") ] \
           [ -protocol:required protocol:required ]

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

Class Relations

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

Methods (to be applied on instances)

  • maxBodyBytes (scripted, public)

     <instance of nsmcp::HttpTransport[i]> maxBodyBytes

    Testcases:
    No testcase defined.
    return ${:maxBodyBytes}
  • mediaTypes (scripted, public)

     <instance of nsmcp::HttpTransport[i]> mediaTypes header
    Parameters:
    header (required)

    Testcases:
    No testcase defined.
    set types {}
    foreach part [split [string tolower $header] ,] {
        lappend types [string trim [lindex [split $part \;] 0]]
    }
    return $types
  • request (scripted, public)

     <instance of nsmcp::HttpTransport[i]> request method headers body \
        server

    Header dictionary uses lower-case keys; created by Server.http.

    Parameters:
    method (required)
    headers (required)
    body (required)
    server (required)

    Testcases:
    No testcase defined.
    try {
        if {[dict exists $headers origin] &&
            [dict get $headers origin] ni ${:allowedOrigins}} {
            return [:response 403 "Origin not permitted"]
        }
        set authorization {}
        if {[dict exists $headers authorization]} {set authorization [dict get $headers authorization]}
        if {[catch {${:authenticator} authenticate $authorization $server} context options]} {
            set ec [dict get $options -errorcode]
            if {[lrange $ec 0 1] eq {NSMCP HTTP}} {
                return [:response [lindex $ec 2] "Authentication required"  [dict create WWW-Authenticate {Bearer realm="nsmcp"}]]
            }
            ns_log error "nsmcp: authentication adapter failed"
            return [:response 500 "Internal error"]
        }
        if {$server ni [$context targets]} {return [:response 403 "Target not permitted"]}
        if {$method ne "POST"} {return [:response 405 "Method not allowed" [dict create Allow POST]]}
        if {[dict exists $headers mcp-protocol-version] &&
            ![${:protocol} supports [dict get $headers mcp-protocol-version]]} {
            return [:response 400 "Unsupported protocol version"]
        }
        if {![dict exists $headers content-type] ||
            [:mediaTypes [dict get $headers content-type]] ne {application/json}} {
            return [:response 415 "Expected application/json"]
        }
        if {![dict exists $headers accept]} {return [:response 406 "Missing Accept header"]}
        set accepted [:mediaTypes [dict get $headers accept]]
        if {"application/json" ni $accepted || "text/event-stream" ni $accepted} {
            return [:response 406 "Accept must include application/json and text/event-stream"]
        }
        if {[string length [encoding convertto utf-8 $body]] > ${:maxBodyBytes}} {
            return [:response 413 "Request body too large"]
        }
        set response [${:protocol} handle $context $body]
        dict set response headers [dict create Content-Type {application/json; charset=utf-8}]
        ns_log debug "nsmcp: principal=[ns_json value -type string -- [$context principal]] target=[ns_json value -type string -- $server] status=[dict get $response status]"
        return $response
    } finally {
        # Immediate cleanup plus the deallocate hook as a fallback for aborts.
        ::nsmcp::cleanup
    }
  • response (scripted, public)

     <instance of nsmcp::HttpTransport[i]> response status body \
        [ headers ]
    Parameters:
    status (required)
    body (required)
    headers (optional)

    Testcases:
    No testcase defined.
    return [dict create status $status body $body headers $headers]