Lesson 4.5 · IV Tools
Tools for an AI
What it's about: No language model has ever seen
knotflow. Still, an AI can write models in it if it gets the same tools
as a human: evaluate, read errors, look at the result, look things up.
kf mcp provides exactly that, over the Model Context
Protocol (MCP). On top of that comes a compact language map that is
generated from the code and therefore never goes out of date.
What you should know first: an AI assistant that
speaks MCP (such as Claude Code, Claude Desktop or Zed's agent panel),
and an installed kf. Lesson 4.4 explains the related
protocol for editors; you do not need to have read it.
1. Tools instead of knowledge
A language model learns a language from examples in its training, and there are none for knotflow. You could train one further (fine-tuning), but that goes out of date with every change to the language. The better way is the one a human takes too: read a description, write something, try it, correct it. For this the AI needs two things:
- Context: a short, complete description of the language, the language map.
- Feedback: a way to evaluate its model and look at it.
2. MCP: a protocol for tools
MCP is to AI assistants what the LSP (Lesson 4.4) is to editors: a shared protocol, so that one server works with many programs, such as Claude Desktop, Claude Code or Zed's agent panel. Here too it is JSON-RPC, just one message per line instead of a length header. A server offers three things:
- Tools the AI may call, with a JSON Schema for the arguments,
- Resources: texts it can read, here the language map
(
knotflow://guide), the grammar (knotflow://grammar), the language tour (knotflow://tour) and the source of every standard library (knotflow://library/<name>), - Prompts: prepared tasks, here
new_model, "a new model from a description".
3. Four tools
| Tool | gives the AI |
|---|---|
evaluate |
errors with line, column and hint; parameters, values; for each export: closed solid yes/no, parts, size, volume |
render |
pictures as PNG, from diagonally above, from above and from the side |
reference |
signature and description of a function, a group or a library, or the whole language map |
examples |
the example models as templates, a project with its libraries |
Here some things that were built for humans pay off:
- The hints from Lesson 2.3 ("write 'and' instead of
&&",
rangestops before the end, …) hit exactly the mistakes a language model makes out of Python or JavaScript habit. - The pictures are the same as with
kf render … -o x.png. Models like Claude can look at pictures and so check whether "a bowl with eight ribs" really looks like one. - The check for a closed solid (Lesson 1.3) is an objective criterion: if an export is not closed, it cannot be printed, and the AI learns this right away.
4. The language map
docs/llms.txt (also kf guide and the
resource knotflow://guide) consists of two parts:
- Written by hand: how to work, how a model is
structured, the expressions with examples, the pitfalls (degrees instead
of radians, no broadcasting, strict instead of lazy,
wherebelongs to the equation, …), the geometry. - Generated from the code: every built-in function,
the prelude and the standard library with signature and description,
from
lang::docs. The same source askf help, the help in the browser and the LSP.
It is in English because it is aimed at language models. A test
compares the file with the output of kf guide. If a
function changes, the test fails until the map has been regenerated.
5. Running it safely
Letting an AI run its own code normally needs an isolated environment (a sandbox). knotflow needs none. The language can neither write files nor reach the network. All it can read is libraries and data files in the model's folder, through relative paths written as literals in the text (Lesson 2.5); a model the AI sends as text reads no data files at all. Every evaluation is limited: at most ten million calls, lists of up to ten million elements, depth 1000 (Lesson 2.3). What was crash safety in Lesson 2.3 is security here. An AI can try out as much as it likes.
6. Setting it up
In Zed, the extension from Lesson 4.4 brings the server along: switch
on "knotflow" in the agent panel. For Claude Code,
claude mcp add knotflow -- kf mcp is enough; for Claude
Desktop, an entry in its configuration. The details are in
editors/README.md. If a newer kf is installed
later, the running server adds a note to its answers that it should be
restarted.
Together with the browser (Lesson 2.4) this makes a workspace for three: the AI writes into the file, the LSP checks while it writes, the browser shows the folder as a 3D model with sliders.
Try it yourself
- Set up the server in Zed or Claude Code and ask the AI: "Write a
knotflow model for a bowl with eight ribs, with diameter and height as
sliders." How often does it call
evaluatebefore the model runs without errors? Does it look at the result withrender? - A touchstone: describe the air-pruning pot in words (Lesson 3.5) and let the AI rebuild it. Where does it fail, and which of that could a better language map fix?
- Read
docs/llms.txt. Is a pitfall missing that you tripped over yourself? Add it tocrates/cli/src/guide.rsand regenerate the map.