src/rpc.bend checks
raw source on the hub · import ezmcp@0.5.0.0/src/rpc.bend as Rpc
ezmcp/src/rpc: JSON-RPC 2.0 as MCP 2025-06-18 uses it, with no IO. One decoded message is classified as a request, a notification, a response or an invalid request; replies and requests are built here; a reply a client reads back is classified here too.
3 imports
import Base import ezjson@1.5.0.0/main.bend as Json import ezjson@1.5.0.0/src/value.bend as Value
Types
type Msg source · line 10 · raw
Data
what one incoming message is (JSON-RPC 2.0 §4, MCP basic#messages)
Ask@id:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @method:String -> @params:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> Msg
Tell@method:String -> @params:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> Msg
Answer@id:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> Msg
Junk@id:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @why:String -> Msg
type Head source · line 17 · raw
Data
the fields of a message the classification looks at
Head@obj:Bool -> @v2:Bool -> @has_id:Bool -> @id_ok:Bool -> @id:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @method:Maybe<&2, String> -> @params:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @has_result:Bool -> @has_error:Bool -> Head
type Code source · line 23 · raw
Data
the error codes JSON-RPC 2.0 §5.1 predefines
ParseErrorCode
InvalidRequestCode
MethodNotFoundCode
InvalidParamsCode
InternalErrorCode
type Out source · line 31 · raw
Data
how a request ends: a result, or an error with its code and message
Ok@result:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> Out
Err@code:Code -> @message:String -> Out
type Reply source · line 36 · raw
Data
what a reply a client reads says
Got@id:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @result:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> Reply
Refused@id:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @code:String -> @message:String -> Reply
Unread@why:String -> Reply
type Back source · line 250 · raw
Data
the fields of a reply a client reads
Back@v2:Bool -> @id:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @has_result:Bool -> @result:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @has_error:Bool -> @code:Maybe<&2, String> -> @message:Maybe<&2, String> -> Back
Definitions
def digits.and source · line 44 · raw
@ok:Bool -> @rest:(@_:Unit -> Bool) -> Bool
the rest of a test that holds so far: false once one char fails
def digits.go source · line 52 · raw
@text:String -> Bool
whether every char of a text is an ASCII digit (true for the empty text)
def digits source · line 60 · raw
@text:String -> Bool
whether a text is one or more ASCII digits
def int.text.of source · line 69 · raw
@head:Char -> @tail:String -> @minus:Bool -> Bool
an integer's text after its first char: digits after a minus, else the first char a digit and digits after it
def int.text source · line 77 · raw
@text:String -> Bool
whether a number's text is an integer: an optional minus, then digits
def id.num source · line 84 · raw
@meth:Maybe<&2, String> -> Bool
def id.str source · line 91 · raw
@meth:Maybe<&2, String> -> @+val:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> Bool
def id.ok source · line 100 · raw
@+val:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> Bool
whether a value is a request id MCP allows: a string or an integer, never null (basic#requests)
def is.v2 source · line 105 · raw
@meth:Maybe<&2, String> -> Bool
def head source · line 113 · raw
@+val:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> Head
the fields of one decoded message
def head.id source · line 121 · raw
@has_id:Bool -> @id_ok:Bool -> @id:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> 0x597f8af3f77d8983aacfa076df42d235/src/value.Json
the id an error reply to a message carries: its own when it has a valid one, null otherwise (JSON-RPC 2.0 §5)
def decide.method source · line 126 · raw
@has_id:Bool -> @id_ok:Bool -> @id:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @method:String -> @params:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> Msg
a message with a method: a request when it has an id, which must be a valid one; a notification when it has none
def one source · line 141 · raw
@lead:Bool -> @byte:Bool -> Bool
whether exactly one of two is true
def decide.answer source · line 152 · raw
@+has_id:Bool -> @+id_ok:Bool -> @+id:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @has_result:Bool -> @has_error:Bool -> Msg
a message with no method: a response when it has a valid id and exactly one of result and error
def decide.kind source · line 163 · raw
@has_id:Bool -> @+id_ok:Bool -> @+id:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @method:Maybe<&2, String> -> @params:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @has_result:Bool -> @has_error:Bool -> Msg
def decide.v2 source · line 178 · raw
@v2:Bool -> @+has_id:Bool -> @+id_ok:Bool -> @+id:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @method:Maybe<&2, String> -> @params:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @has_result:Bool -> @has_error:Bool -> Msg
def decide source · line 196 · raw
@head:Head -> Msg
what a message is, from its fields
def classify source · line 205 · raw
@val:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> Msg
what one decoded message is
def code.num source · line 211 · raw
@cb:Code -> String
an error code's number
def error.obj source · line 225 · raw
@cb:Code -> @message:String -> 0x597f8af3f77d8983aacfa076df42d235/src/value.Json
an error object: an integer code and a message (basic#responses)
def reply source · line 229 · raw
@id:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @out:Out -> 0x597f8af3f77d8983aacfa076df42d235/src/value.Json
the reply to a request with this id (MCP basic#responses)
def request source · line 238 · raw
@id:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @method:String -> @params:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> 0x597f8af3f77d8983aacfa076df42d235/src/value.Json
a request: jsonrpc, id, method and params
def notice source · line 243 · raw
@method:String -> @params:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> 0x597f8af3f77d8983aacfa076df42d235/src/value.Json
a notification: jsonrpc, method and params, and no id
def back.refused source · line 254 · raw
@id:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @code:Maybe<&2, String> -> @message:Maybe<&2, String> -> Reply
def back.has source · line 265 · raw
@id:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @has_result:Bool -> @result:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @has_error:Bool -> @code:Maybe<&2, String> -> @message:Maybe<&2, String> -> Reply
def back.decide source · line 282 · raw
@byte:Back -> Reply
what a reply says, from its fields
def back.code source · line 289 · raw
@meth:Maybe<&2, String> -> Maybe<&2, String>
def back source · line 297 · raw
@+val:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> Back
the fields of one decoded reply
def read.parsed source · line 303 · raw
@meth:Maybe<&2, 0x597f8af3f77d8983aacfa076df42d235/src/value.Json> -> Reply
def read source · line 311 · raw
@text:String -> Reply
what one reply's text says