Explorer

definy.handle-mcp

description
(en) Handle a request to /mcp (MCP Streamable HTTP. Only POST with one JSON-RPC message, answered with JSON. docs/mcp.md)
type
Fn(request: @http.request) -> @definy.service-io
expression
fn(request$0) =>
  match @core.equal(left: @http.method-text(method: $0.method), right: "POST") {
    false =>
      #done<@definy.service-io>(#respond({
        status: 405,
        headers: @core.list-concat(left: [{name: "Allow", value: "POST"}], right: @definy.cors-headers),
        body: #text<@definy.http-body>("Method Not Allowed")
      })),
    true =>
      match @definy.request-json-of(request: $0) {
        #json(json$2) =>
          @definy.mcp-handle-message(message: $2, request: $0),
        #invalid(message$3) =>
          @definy.mcp-error(
            status: 400,
            id: #null<@definy.json>,
            code: @core.minus(left: 0, right: 32700),
            message: @core.string-concat(left: "Parse error: ", right: $3)
          )
      }
  }
visibility
public
definy.handle-mcp (project definy, branch main, snapshot l0VFjJZP174d8CbZWFoujAGTYjgsQu4uTq5uWCZaXgo)
id: ofUe8J9ItG_hqqioeE5kwdn-Zzr0d_Xtlx_JarcFWR4
content hash: 6iBMqB7JUhM_-eh5cK9WKztM6Xadpwr90Q2_MR4CRlg