Explorer

definy.mcp-run-expression

description
The definy_run_expression MCP tool: type-check an expression in the definy text syntax (definy.run-expression-type-check), evaluate it with core.eval-value and return the value as text (only when the host allows it: read_host_settings)
type
Fn(arguments: @definy.json, request: @http.request) -> @definy.service-io
expression
fn(arguments$0, request$1) =>
  #read_host_settings<@definy.service-io>({
    then: fn(settings$2) =>
      match $2.run_expressions {
        false =>
          @definy.mcp-text-error(
            text: "Running expressions is disabled on this server. Run the local definy server (node host.js) and use its MCP to run expressions"
          ),
        true =>
          match @definy.parse-expression-text(
              text: match @definy.json-get-string(value: $0, key: "expression") {
                #some(text$3) =>
                  $3,
                #none(_) =>
                  ""
              }
            ) {
            #error(message$4) =>
              @definy.mcp-text-error(text: @core.string-concat(left: "The expression could not be read: ", right: $4)),
            #ok(expression$5) =>
              @definy.read-snapshots(
                then: fn(commits$6) =>
                  match @definy.run-expression-snapshots(arguments: $0, commits: $6) {
                    #error(message$7) =>
                      @definy.mcp-text-error(text: $7),
                    #ok(branch$8) =>
                      @definy.run-expression-type-check(
                        expression: $5,
                        commits: $6,
                        branch: $8,
                        then: fn(result$20) =>
                          match $20 {
                            #error(error$21) =>
                              @definy.mcp-text-error(
                                text: @core.string-concat(left: "The expression has a type error: ", right: @definy.type-error-text(error: $21))
                              ),
                            #ok(_) =>
                              @definy.read-part-contents(
                                parts: @definy.snapshot-parts-flat(commits: $8, index: 0),
                                then: fn(parts$9) =>
                                  let local$10 = @definy.run-expression-parts(parts: $9);
                                  @definy.collect-evaluation-parts(
                                    frontier: @definy.run-expression-frontier(expression: $5, parts: $10),
                                    seen: @core.list-map(
                                      T: Record{id: String, expression: @core.expression},
                                      U: String,
                                      f: fn(item$11) =>
                                        $11.id,
                                      xs: $10
                                    ),
                                    external: [],
                                    sources: [],
                                    contents: [],
                                    then: fn(external$12) =>
                                      @definy.mcp-text(
                                        text: @definy.expression-text(
                                          expression: @definy.value-literal(value: @core.eval-value(expr: $5, env: [], parts: @core.list-concat(left: $10, right: $12))),
                                          indent: "",
                                          names: @definy.snapshot-part-names(commits: @core.list-concat(left: $8, right: @definy.latest-main-snapshots(commits: $6)))
                                        )
                                      )
                                  )
                              )
                          }
                      )
                  }
              )
          }
      }
  })
visibility
public
definy.mcp-run-expression (project definy, branch main, snapshot l0VFjJZP174d8CbZWFoujAGTYjgsQu4uTq5uWCZaXgo)
id: UB984FQ2PAlnEFs4HZsYKcBgDrNMOXrrWLBQNyrOTPM
content hash: Iuafcg9kF589GYyfi5ZfgSP7u7OlZym4LvE6I4epKQU