---
title: MCP server
project: Total.js V5
version: 0.0.20
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v5/0-0-20/reference/mcp-server/
updated: 2026-11-19T16:17:07.153Z
---

# MCP server

> The $mcp key, the mcp option of actions, the JSON-RPC methods of POST /$mcp/, the client of GET /$mcp/client.js and Total.mcp.

The `$mcp` key, the `mcp` option of actions, the JSON-RPC methods of `POST /$mcp/`, the JavaScript client of `GET /$mcp/client.js` (code mode) and the `Total.mcp` object. The guide [MCP server](../../guides/mcp-server/) explains how actions become tools and how to protect the endpoint.

## API

### $mcp : Boolean

Type: `config`

Turns on the MCP server.

Returns: `Boolean`

`true` creates the routes `POST /$mcp/` and `GET /$mcp/client.js` when the application is ready. Set it in the `config` file or in code before the start.

Default: `false`.

Example: In the config file
```text
$mcp : true
```

### mcp: Boolean

Type: `config`

Makes an action a tool.

Returns: `Boolean`

An action with `mcp: true` is listed by `tools/list` and runs with `tools/call`. The name of the tool is the name of the action with each character other than `A-Z`, `a-z`, `0-9`, `_` and `-` replaced by `_` (at most 64 characters). The description is the `summary` option, or the `name` option. The `input`, `query` and `params` schemas become the arguments and `output` the output schema.

Example: A tool
```js
NEWACTION('Tasks|list', {
	summary: 'Lists the tasks, 20 per page',
	mcp: true,
	query: 'page:Number // The page number',
	action: function($) {
		NOSQL('tasks').find().paginate($.query.page || 1, 20).callback($);
	}
});
// the tool Tasks_list
```

### POST /$mcp/

Type: `type`

The endpoint of the MCP server.

A JSON-RPC 2.0 endpoint: one message in the body, one JSON answer. The body can have up to 10 MB and the request can take up to 300 seconds. A message without `id` is a notification and gets status `202` without a body. A message without `"jsonrpc": "2.0"` or `method`, or an array, gets the error `-32600`; an unknown method gets `-32601`. When `Total.mcp.auth` refuses the request, the status is `401` and the error `-32001`.

Example: A request
```js
await fetch('http://localhost:8000/$mcp/', {
	method: 'POST',
	headers: { 'Content-Type': 'application/json', Authorization: 'Bearer 6f1d2c' },
	body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'tools/list' })
});
```

### GET /$mcp/client.js[?q=words]

Type: `type`

The tools as a JavaScript client, for code mode.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `q` | `String` | no |  | Words that the name or the description of a tool must contain. |

Returns: `String` The JavaScript source.

Returns a CommonJS module (`text/javascript`) made from the tools that the caller may use: one function per tool, with JSDoc for its arguments (`input`, `query`, `params` and their fields, with the `//` messages as descriptions) and for its result. `require('./client.js')(token, [url])` returns an object of these functions. Each function sends `tools/call` to `POST /$mcp/` with `Authorization: Bearer <token>`, resolves with the output of the tool (`structuredContent`, or the parsed text) and throws an `Error` with the message of a tool error. The default `url` is the address the file was downloaded from. It needs Node.js 18 or newer (`fetch`) and no packages.

`?q=words` returns only the tools whose name or description contain all the words (not case-sensitive). The request goes through `Total.mcp.auth` (a refused one gets `401` and the text `Unauthorized`) and `Total.mcp.filter`, so the client has the same tools as `tools/list`. Nothing runs on the server: the script runs where the agent runs, and every call is a normal tool call.

Example: Download and use
```js
// curl -H 'Authorization: Bearer TOKEN' 'https://app.example.com/$mcp/client.js?q=tasks' > client.js
const api = require('./client.js')('TOKEN');

const task = await api.Tasks_create({ input: { name: 'Call Peter' } });
console.log(task.id);
```

Example: A function of the file
```js
/**
 * Creates a task and returns its ID
 * @param {Object} args
 * @param {Object} args.input Payload (request body) passed to this action.
 * @param {String} args.input.name The name of the task
 * @param {String} [args.input.due] When the task must be done (ISO date)
 * @returns {Promise<{ id: ?String }>}
 */
api.Tasks_create = args => call("Tasks_create", args);
```

### { method: 'initialize', params: { protocolVersion } }

Type: `method`

Starts a session of the client.

Returns: `Object` The result.

Returns `protocolVersion` (the version of the client when it is in `Total.mcp.versions`, otherwise the first one), `capabilities: { tools: {} }` and `serverInfo: { name: CONF.name, version: CONF.version }`. The server keeps no state between requests.

Example: Answer
```json
{ "jsonrpc": "2.0", "id": 1, "result": { "protocolVersion": "2025-06-18", "capabilities": { "tools": {} }, "serverInfo": { "name": "Tasks", "version": "1.0" } } }
```

### { method: 'ping' }

Type: `method`

Checks the connection.

Returns: `Object` An empty object.

Returns an empty result `{}`.

### { method: 'tools/list' }

Type: `method`

Lists the tools.

Returns: `Object` The result.

Returns `{ resultType: 'complete', tools: [...] }` with the definitions of all actions that have `mcp: true` (`Total.mcp.tools`) that `Total.mcp.filter` allows for the caller. Each definition has `name`, `description` (when the action has `summary` or `name`), `inputSchema` and, with an `output` schema, `outputSchema`.

### { method: 'tools/call', params: { name, arguments: { input, query, params } } }

Type: `method`

Runs a tool.

Returns: `Object` The result.

Runs the action of the tool with `ACTION()`. `arguments.input` (or `arguments.data`) is the payload, `arguments.query` and `arguments.params` are the query and the URL parameters. The action has no controller, its user is the user that `Total.mcp.auth` accepted the request with (`{ id: 'ai', name: 'AI', sa: true }` with the default function) and `$.config.id` is the `id` from the query string of the endpoint. The result has `content: [{ type: 'text', text }]` with the response (a string as it is, other values as JSON) and, for an object, `structuredContent`. Errors of the action are a result with `isError: true`, also when the action throws an exception (the text is `Tool execution failed`, or `500: Internal Server Error` for an `async` action, and the exception goes to the error log). An unknown tool, a tool that `Total.mcp.filter` hides from the caller, or arguments that are not an object give the error `-32602`.

Example: Call
```json
{ "jsonrpc": "2.0", "id": 5, "method": "tools/call", "params": { "name": "Tasks_remove", "arguments": { "params": { "id": "JafvB11cV61f" } } } }
```

Example: Answer
```json
{ "jsonrpc": "2.0", "id": 5, "result": { "resultType": "complete", "content": [{ "type": "text", "text": "{\"success\":true}" }], "structuredContent": { "success": true } } }
```

### { method: 'server/discover' }

Type: `method`

Describes the server.

Returns: `Object` The result.

Returns `resultType: 'complete'`, `supportedVersions: ['2026-07-28']`, `capabilities: { tools: {} }` and the server info (`CONF.name`, `CONF.version`) in `_meta['io.modelcontextprotocol/serverInfo']`.

### Total.mcp.auth = function($)

Type: `delegate`

Decides who may use the endpoint.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `$` | `Options` | yes |  | The request; call `$.success()` or `$.invalid()`. |

Runs for every request to `POST /$mcp/` and `GET /$mcp/client.js`. `$.token` (and `$.auth`) is the `Authorization` header without `Bearer `, and `$` has `headers`, `query`, `ip`, `ua` and `url`. Call `$.success(user)` to accept the request or `$.invalid()` to refuse it with `401`. The tools run as `user` and `Total.mcp.filter` gets it as `$.user`; `$.success()` without a user gives an empty user `{}`. The default function accepts every request with the user `{ id: 'ai', name: 'AI', sa: true }`. Set it in a file in `definitions/`.

Example: Tokens from the config
```js
Total.mcp.auth = function($) {
	if ($.token && $.token === CONF.mcp_admin)
		$.success({ id: 'admin', name: 'Admin', sa: true });
	else if ($.token && $.token === CONF.mcp_reader)
		$.success({ id: 'reader', name: 'Reader', permissions: ['tasks_read'] });
	else
		$.invalid();
};
```

### Total.mcp.filter = function(tool, $)

Type: `delegate`

Decides which tools a caller sees and may run.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `tool` | `Object` | yes |  | The definition of the tool: `name`, `description`, `inputSchema`, `outputSchema`. |
| `$` | `Options` | yes |  | The request; `$.user` is the caller. |

Returns: `Boolean` `true` when the caller may use the tool.

Optional, `null` by default (every caller has all tools). The function gets the definition of a tool (an item of `Total.mcp.tools`) and `$` with the user from `Total.mcp.auth` in `$.user`, and returns `true` to allow the tool. A tool that is not allowed is left out of `tools/list` and `GET /$mcp/client.js`, and `tools/call` answers it with `-32602` (`Unknown tool`), as if it did not exist.

Example: Admin tools only for admins
```js
Total.mcp.filter = function(tool, $) {
	return !tool.name.startsWith('Admin_') || $.user.sa;
};
```

### Total.mcp.tools

Type: `property`

The definitions of the tools.

Returns: `Array`

An array with the definition of every action that has `mcp: true`, the same objects that `tools/list` returns. It is built again 200 ms after each `NEWACTION()`.

Example: Names
```js
console.log(Total.mcp.tools.map(tool => tool.name));
// ['Tasks_create', 'Tasks_list', 'Tasks_remove']
```

### Total.mcp.versions

Type: `property`

The supported protocol versions.

Returns: `Array`

An array, newest first: `['2026-07-28', '2025-11-25', '2025-06-18', '2025-03-26']`. `initialize` answers with the version of the client when it is in the list, otherwise with the first one.

### F.actions[name].toAITool()

Type: `method`

Returns the tool definition of an action.

Returns: `Object` The tool definition.

Returns `{ name, description, inputSchema, [outputSchema] }` in the format of `tools/list`. It works for any action, also without `mcp: true`, so you can pass actions as tools to an AI model yourself.

Example: One tool
```js
var tool = F.actions['Tasks|remove'].toAITool();
// { name: 'Tasks_remove', description: 'Removes a task', inputSchema: { type: 'object', properties: { params: {...} }, required: ['params'] } }
```
