BB.MCP.Json (bb_mcp v0.3.3)

Copy Markdown View Source

Render arbitrary BB terms as something JSON.encode!/1 can carry.

BB is full of values JSON has no equivalent for: Nx tensors behind BB.Math.Vec3 and friends, reference() execution ids, pids, exception structs inside BB.Error payloads, and camera frames as raw binaries. Every MCP reply is encoded eagerly by Anubis.Server.Response.json/3, so a single un-encodable value anywhere in a payload fails the whole reply rather than degrading that one field.

encodable/1 walks a term and leaves only maps, lists, strings, numbers, booleans and nil behind, falling back to inspect/1 for anything it has no better rendering for.

Summary

Functions

Convert a term into one JSON.encode!/1 accepts.

Functions

encodable(value)

@spec encodable(term()) :: term()

Convert a term into one JSON.encode!/1 accepts.

Atoms become bare strings without the leading colon, since an agent reading "idle" has no use for Elixir's atom syntax; a module atom is rendered the way it was written, without its Elixir. prefix. Tuples become lists, and binaries too long or too binary to read become a byte count.

A struct that already implements JSON.Encoder — a DateTime, say — is handed straight through so it keeps its own rendering. Anything else becomes an object of its fields.