# Décrivez une fonction pour un modèle d'IA avec un schéma JSON

> Transformez un schéma compact de Total.js en l'outil ou la définition d'objet qu'attend un modèle d'IA, avec toAITool() et toAIObject(), et laissez une action produire son propre outil MCP.

Framework: total5
Product: Total.js 5 (total5@0.0.20)
Language: fr
Content-SHA256: 2efa999b0191ec18f764758b0346431687fdf7f0d5dafe13d1735976c62f8330
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

Le schéma compact que vous écrivez déjà pour `input` (`'*name:String // The name, age:Number'`) suffit pour décrire une fonction à un modèle d'IA. `0.0.20` ajoute trois conversions :

| Appel | Résultat |
|---|---|
| `'<schema>'.toJSONSchema().toAITool(name)` | Une définition de fonction au format des API de chat : `{ type: 'function', function: { name, description, parameters } }` |
| `'<schema>'.toJSONSchema().toAIObject(name, description)` | Un objet JSON Schema avec `name`, `description`, `properties` et `required`, pour demander au modèle une sortie structurée |
| `Total.actions['Name'].toAITool()` | L'outil MCP d'une action (nom, description, `inputSchema` et `outputSchema`) |

Le texte après `//` dans un champ devient sa `description`, que le modèle lit pour décider quoi y mettre. Un champ marqué `*` va dans `required`. N'envoyez pas la saisie d'un utilisateur à un modèle comme une instruction : le schéma ne décrit que la forme des arguments.

```js controllers/tools.js
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());
}
```

```js definitions/weather.js
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 });
	}
});
```
