Skip to contents

MCP/JSON-RPC 2.0 exchange with envelope validation (Geron Ch 15, morie.fn hmmcp)

Usage

morie_geron_model_context_protocol(server, client, requests = NULL)

Arguments

server

function(request) -> response.

client

Callable or list of requests.

requests

Optional override.

Value

List with exchanges, n_ok, n_errors, methods, estimate, n, method.

Examples

loud <- array(1, dim = c(2, 8, 4))
a <- morie_geron_kv_cache_compress(loud, loud, n_bits = 8, per_head = TRUE)
b <- morie_geron_kv_cache_compress(loud, loud, n_bits = 8, per_head = FALSE)
TOOLS <- list(list(name = "add", description = "add two numbers"))
server <- function(req) {
    if (req$method == "tools/list")
        return(list(jsonrpc = "2.0", id = req$id, result = list(tools = TOOLS)))
    if (req$method == "tools/call") {
        a <- req$params$arguments$a
        b <- req$params$arguments$b
        return(list(jsonrpc = "2.0", id = req$id, result = list(content = a +
            b)))
    }
    list(jsonrpc = "2.0", id = req$id, error = list(code = -32601L,
        message = "Method not found"))
}
reqs <- list(list(jsonrpc = "2.0", id = 1, method = "tools/list"),
    list(jsonrpc = "2.0", id = 2, method = "tools/call", params = list(name = "add",
        arguments = list(a = 2, b = 3))))
morie_geron_model_context_protocol(server, reqs)
#> $exchanges
#> $exchanges[[1]]
#> $exchanges[[1]]$request
#> $exchanges[[1]]$request$jsonrpc
#> [1] "2.0"
#> 
#> $exchanges[[1]]$request$id
#> [1] 1
#> 
#> $exchanges[[1]]$request$method
#> [1] "tools/list"
#> 
#> 
#> $exchanges[[1]]$response
#> $exchanges[[1]]$response$jsonrpc
#> [1] "2.0"
#> 
#> $exchanges[[1]]$response$id
#> [1] 1
#> 
#> $exchanges[[1]]$response$result
#> $exchanges[[1]]$response$result$tools
#> $exchanges[[1]]$response$result$tools[[1]]
#> $exchanges[[1]]$response$result$tools[[1]]$name
#> [1] "add"
#> 
#> $exchanges[[1]]$response$result$tools[[1]]$description
#> [1] "add two numbers"
#> 
#> 
#> 
#> 
#> 
#> $exchanges[[1]]$method
#> [1] "tools/list"
#> 
#> $exchanges[[1]]$ok
#> [1] TRUE
#> 
#> $exchanges[[1]]$error_name
#> NULL
#> 
#> 
#> $exchanges[[2]]
#> $exchanges[[2]]$request
#> $exchanges[[2]]$request$jsonrpc
#> [1] "2.0"
#> 
#> $exchanges[[2]]$request$id
#> [1] 2
#> 
#> $exchanges[[2]]$request$method
#> [1] "tools/call"
#> 
#> $exchanges[[2]]$request$params
#> $exchanges[[2]]$request$params$name
#> [1] "add"
#> 
#> $exchanges[[2]]$request$params$arguments
#> $exchanges[[2]]$request$params$arguments$a
#> [1] 2
#> 
#> $exchanges[[2]]$request$params$arguments$b
#> [1] 3
#> 
#> 
#> 
#> 
#> $exchanges[[2]]$response
#> $exchanges[[2]]$response$jsonrpc
#> [1] "2.0"
#> 
#> $exchanges[[2]]$response$id
#> [1] 2
#> 
#> $exchanges[[2]]$response$result
#> $exchanges[[2]]$response$result$content
#> [1] 5
#> 
#> 
#> 
#> $exchanges[[2]]$method
#> [1] "tools/call"
#> 
#> $exchanges[[2]]$ok
#> [1] TRUE
#> 
#> $exchanges[[2]]$error_name
#> NULL
#> 
#> 
#> 
#> $n_ok
#> [1] 2
#> 
#> $n_errors
#> [1] 0
#> 
#> $methods
#> [1] "tools/list" "tools/call"
#> 
#> $wire_bytes
#> [1] NA
#> 
#> $estimate
#> [1] 2
#> 
#> $n
#> [1] 2
#> 
#> $method
#> [1] "MCP / JSON-RPC 2.0 exchange with envelope validation"
#>