Describe a function for an AI model with a JSON schema
The compact schema that you already write for input ('*name:String // The name, age:Number') is enough to describe a function to an AI model. 0.0.20 adds three conversions:
| Call | Result |
|---|---|
'<schema>'.toJSONSchema().toAITool(name) | A function definition in the shape of the chat APIs: { type: 'function', function: { name, description, parameters } } |
'<schema>'.toJSONSchema().toAIObject(name, description) | A JSON Schema object with name, description, properties and required, to ask the model for structured output |
Total.actions['Name'].toAITool() | The MCP tool of an action (name, description, inputSchema and outputSchema) |
The text after // in a field becomes its description, which is what the model reads to decide what to put there. A field marked * goes into required. Do not send user input to a model as an instruction: the schema only describes the shape of the arguments.
exports.install = function() {
ROUTE('GET /tool/', tool);
ROUTE('GET /object/', object);
ROUTE('GET /action/', action);
};
const SCHEMA = '*city:String // The name of the city, unit:String // celsius or fahrenheit';
function tool($) {
$.json(SCHEMA.toJSONSchema().toAITool('weather'));
}
function object($) {
$.json(SCHEMA.toJSONSchema().toAIObject('weather', 'The weather of a city'));
}
function action($) {
$.json(Total.actions['Weather/get'].toAITool());
}NEWACTION('Weather/get', {
name: 'Get the weather',
summary: 'Returns the weather of a city',
input: '*city:String // The name of the city',
action: function($, model) {
$.callback({ city: model.city, temperature: 21 });
}
});