Expose your actions as MCP tools
Since Total.js 5 0.0.20 the framework carries an MCP server (Model Context Protocol), so an AI client can use your application as a set of tools without a separate service. There is nothing to write apart from the actions you already have: add mcp: true to a NEWACTION() and set CONF.$mcp = true. The framework then answers POST /$mcp/ (JSON-RPC 2.0) and keeps the list of tools up to date as actions are declared.
The tool schema is generated from the action. input, query and params become the sections of the arguments ({ input: {}, query: {}, params: {} }), output becomes the output schema, and the text after // in a field of the schema is the description the model reads and the message of a validation error. The name of the action is the name of the tool; characters that MCP does not allow become _, so Notes/create is the tool Notes_create. The description is summary or, when there is none, name.
CONF.$mcp = true;
// The default allows every caller. Replace it with your own check before you publish the endpoint
Total.mcp.auth = function($) {
if ($.token === 'secret')
$.success({ id: 'ai', name: 'AI' });
else
$.invalid(401);
};
MAIN.notes = [];
NEWACTION('Notes/create', {
name: 'Create a note',
summary: 'Creates a note and returns it with its id',
mcp: true,
input: '*text:String // The text of the note',
output: 'id:Number, text:String',
action: function($, model) {
const note = { id: MAIN.notes.length + 1, text: model.text };
MAIN.notes.push(note);
$.callback(note);
}
});
NEWACTION('Notes/list', {
name: 'List the notes',
mcp: true,
query: 'limit:Number // How many notes to return',
action: function($) {
$.callback(MAIN.notes.slice(0, $.query.limit || 10));
}
});
// Without mcp: true an action is never offered to the AI
NEWACTION('Notes/clear', {
name: 'Remove every note',
action: function($) {
MAIN.notes = [];
$.success();
}
});Total.mcp.auth receives the options of an action: the bearer token of the request is in $.token, $.success(user) accepts the caller and $.invalid() answers 401. The user of a tool call is always the same service user ({ sa: true, name: 'AI' }), so check the token here and not in the action. Tool errors, such as a failed validation, are returned inside the result with isError: true so that the model can read them and correct the call.