Nodecode: Racket SDK
| (require nodecode) | package: nodecode |
Zero-dependency Racket client for the Nodecode inference API (Cyber v1, Decide v1, Image v1) and the Streamable HTTP Model Context Protocol (MCP) endpoint, maintained by Foresight Labs (based in the EU, architectural Zero Data Retention).
Built directly on Racket’s standard net/http-client and json libraries with zero third-party package dependencies.
1 Installation
Install from the Racket Package Catalog:
raco pkg install nodecode |
Or install directly from source:
raco pkg install https://github.com/nodecode-ai/nodecode-racket.git |
2 Quickstart
Create an API key at nodecode.ai/account/keys and export NODECODE_API_KEY. See the Cyber v1 API quickstart for request parameters and wire formats.
(require nodecode) (define client (make-nodecode-client)) (define resp (chat-completion client (list (hasheq 'role "user" 'content "22/tcp and 8080/tcp are open. What next, in order?")) #:model model-cyber-v1 #:max-tokens 600)) (displayln (first-reasoning resp)) (displayln (first-content resp))
2.1 Streaming (SSE)
(require nodecode) (define client (make-nodecode-client)) (define summary (chat-completion-stream client (list (hasheq 'role "user" 'content "Audit this Dockerfile for privilege escalation.")) (lambda (chunk) (define txt (delta-content chunk)) (when txt (display txt) (flush-output))) #:model model-cyber-v1 #:max-tokens 1024))
2.2 Reasoning Token Budget
Cyber v1 reasons before producing its final answer, and #:max-tokens covers both thinking and final output. Passing a very small limit (such as 100) can exhaust the budget during reasoning and return empty content with "length" as the finish reason. Allocate at least a few hundred tokens.
3 Constants and Client Construction
value
value
value
value
value
value
value
struct
(struct nodecode-client (api-key base-url user-agent))
api-key : (or/c string? #f) base-url : string? user-agent : string?
procedure
(make-nodecode-client [ #:api-key api-key #:base-url base-url #:user-agent user-agent]) → nodecode-client? api-key : (or/c string? #f) = #f base-url : string? = default-base-url user-agent : string? = default-user-agent
4 Inference, Models, Images, and Usage
procedure
(list-models client) → jsexpr?
client : nodecode-client?
procedure
client : nodecode-client? model-id : string?
procedure
(chat-completion client messages [ #:model model #:max-tokens max-tokens #:temperature temperature #:top-p top-p #:stop stop #:tools tools #:tool-choice tool-choice #:extra extra]) → jsexpr? client : nodecode-client? messages : (listof hash?) model : string? = model-cyber-v1 max-tokens : (or/c exact-positive-integer? #f) = #f temperature : (or/c real? #f) = #f top-p : (or/c real? #f) = #f stop : (or/c string? (listof string?) #f) = #f tools : (or/c (listof hash?) #f) = #f tool-choice : (or/c string? hash? #f) = #f extra : hash? = (hasheq)
procedure
(chat-completion-stream client messages on-chunk [ #:model model #:max-tokens max-tokens #:temperature temperature #:top-p top-p #:stop stop #:tools tools #:tool-choice tool-choice #:extra extra]) → hash? client : nodecode-client? messages : (listof hash?) on-chunk : (-> jsexpr? any) model : string? = model-cyber-v1 max-tokens : (or/c exact-positive-integer? #f) = #f temperature : (or/c real? #f) = #f top-p : (or/c real? #f) = #f stop : (or/c string? (listof string?) #f) = #f tools : (or/c (listof hash?) #f) = #f tool-choice : (or/c string? hash? #f) = #f extra : hash? = (hasheq)
procedure
(anthropic-messages client messages [ #:model model #:max-tokens max-tokens #:system system #:temperature temperature #:extra extra]) → jsexpr? client : nodecode-client? messages : (listof hash?) model : string? = model-cyber-v1 max-tokens : exact-positive-integer? = 1024 system : (or/c string? (listof hash?) #f) = #f temperature : (or/c real? #f) = #f extra : hash? = (hasheq)
procedure
(generate-image client prompt [ #:model model #:size size #:n n #:response-format response-format #:seed seed #:extra extra]) → jsexpr? client : nodecode-client? prompt : string? model : string? = model-image-v1 size : (or/c string? #f) = #f n : (or/c exact-positive-integer? #f) = #f response-format : string? = "b64_json" seed : (or/c exact-integer? #f) = #f extra : hash? = (hasheq)
procedure
(decode-image-bytes image-hash) → bytes?
image-hash : hash?
procedure
(account-usage client) → jsexpr?
client : nodecode-client?
procedure
(balance-amount micros) → inexact-real?
micros : exact-integer?
procedure
(first-content resp) → (or/c string? #f)
resp : any/c
procedure
(first-reasoning resp) → (or/c string? #f)
resp : any/c
procedure
(delta-content chunk) → (or/c string? #f)
chunk : any/c
procedure
(delta-reasoning chunk) → (or/c string? #f)
chunk : any/c
5 Errors and SSE Utilities
struct
(struct exn:fail:nodecode exn:fail ( status code error-type request-id refill body)) status : exact-nonnegative-integer? code : any/c error-type : (or/c string? #f) request-id : (or/c string? #f) refill : any/c body : string?
procedure
v : any/c
procedure
v : any/c
procedure
v : any/c
procedure
(raise-nodecode-error status body [ #:request-id request-id]) → any status : exact-nonnegative-integer? body : string? request-id : (or/c string? #f) = #f
procedure
(read-sse-event in) → (or/c (hash/c symbol? string?) #f)
in : input-port?
procedure
(call-with-sse-json-stream in on-chunk) → void?
in : input-port? on-chunk : (-> any/c any)
6 Streamable HTTP MCP Client
| (require nodecode/mcp) | package: nodecode |
Streamable HTTP JSON-RPC 2.0 client targeting the live Nodecode MCP endpoint (https://nodecode.ai/mcp) or any Streamable HTTP MCP server. All bindings below are also re-exported by nodecode.
(require nodecode/mcp) (define conn (make-mcp-client)) (define info (mcp-initialize conn)) (define tools (mcp-list-tools conn)) (define models (mcp-call-tool conn "list_models")) (displayln (mcp-tool-text models))
value
value
procedure
(mcp-client? v) → boolean?
v : any/c
procedure
(make-mcp-client [ #:url url #:api-key api-key #:user-agent user-agent]) → mcp-client? url : string? = default-mcp-url api-key : (or/c string? #f) = #f user-agent : string? = "nodecode-racket/0.1.0"
procedure
(mcp-client-url conn) → string?
conn : mcp-client?
procedure
(mcp-client-session-id conn) → (or/c string? #f)
conn : mcp-client?
procedure
(mcp-initialize conn [ #:client-name client-name #:client-version client-version]) → jsexpr? conn : mcp-client? client-name : string? = "nodecode-racket" client-version : string? = "0.1.0"
procedure
(mcp-list-tools conn) → jsexpr?
conn : mcp-client?
procedure
(mcp-call-tool conn name [arguments]) → jsexpr?
conn : mcp-client? name : string? arguments : hash? = (hasheq)
procedure
(mcp-list-resources conn) → jsexpr?
conn : mcp-client?
procedure
(mcp-read-resource conn uri) → jsexpr?
conn : mcp-client? uri : string?
procedure
conn : mcp-client?
procedure
(mcp-tool-text result) → (or/c string? #f)
result : any/c