Skip to main content

MCP - Model Context Protocol

Resource to implement the Model Context Protocol (MCP) that allows exposing tools through scripts dynamically loaded from the app/mcp/ folder. Each script can register one or more tools that can be executed remotely, following the MCP pattern for integration with AI assistants.

MCP automatically loads all JavaScript scripts from the app/mcp/ folder and makes the registered tools available for execution. It supports middlewares to intercept calls before execution, enabling validations, logs, authorization, etc.

MCP server configuration is done in the _app/config.json file in the mcp.server section.

// Example script in app/mcp/calculator.js
// Register an addition tool
_mcp.registerTool(
'add',
'Adds two integer numbers',
_val.map()
.set('type', 'object')
.set('properties', _val.map()
.set('a', _val.map().set('type', 'number').set('description', 'First number'))
.set('b', _val.map().set('type', 'number').set('description', 'Second number'))
)
.set('required', _val.list().add('a').add('b')),
(input) => {
const result = input.get('a') + input.get('b');
return _val.map().set('success', true).set('result', result);
}
);

// Register a multiplication tool
_mcp.registerTool(
'multiply',
'Multiplies two integer numbers',
_val.map()
.set('type', 'object')
.set('properties', _val.map()
.set('a', _val.map().set('type', 'number').set('description', 'First number'))
.set('b', _val.map().set('type', 'number').set('description', 'Second number'))
)
.set('required', _val.list().add('a').add('b')),
(input) => {
const result = input.get('a') * input.get('b');
return _val.map().set('success', true).set('result', result);
}
);
// Example of listing and executing tools
// List all available tools
const tools = _mcp.listAvailableTools();
for (const tool of tools) {
_log.info('Tool: ' + tool.get('name') + ' - ' + tool.get('description'));
}

// Execute a tool
const result = _mcp.executeTool('add', _val.map().set('a', 5).set('b', 3));
if (result.get('success')) {
_log.info('Result: ' + result.get('result'));
}

addMiddlewares


_mcp.addMiddlewares(middlewares: org.netuno.tritao.resource.MCP$MCPMiddleware[]) : void

Description

Adds middlewares that will be executed before each tool. If a middleware returns a non-null result, the tool execution is interrupted and that result is returned.

How To Use
// Example middleware for logging
_mcp.addMiddlewares((tool) => {
_log.info('Executing tool: ' + tool.name);
return null; // Continues execution
});
Attributes
NAMETYPEDESCRIPTION
middlewaresorg.netuno.tritao.resource.MCP$MCPMiddleware[]One or more middlewares to add.
Return

( void )


containsTool


_mcp.containsTool(name: string) : boolean

Description

Checks if a tool with the specified name is registered.

How To Use
if (_mcp.containsTool('add')) {
_log.info('Addition tool is available');
}
Attributes
NAMETYPEDESCRIPTION
namestringName of the tool to check.
Return

( boolean )

True if the tool exists, false otherwise.


executeTool


_mcp.executeTool(name: string, input: Values) : Values

Description

Executes a registered tool with the provided parameters. If the tool does not exist, returns an object with success: false and an error message. If any middleware interrupts execution, returns the middleware's result. If an error occurs during execution, returns an object with success: false and the error message.

How To Use
const result = _mcp.executeTool('add', _val.map().set('a', 10).set('b', 20));
if (result.get('success')) {
_log.info('Result: ' + result.get('result'));
} else {
_log.error('Error: ' + result.get('error'));
}
Attributes
NAMETYPEDESCRIPTION
namestringName of the tool to execute.
inputValuesObject with input parameters according to the schema defined when registering the tool.
Return

( Values )

Execution result. On success, contains success: true and the data returned by the tool. On error, contains success: false and error with the error message.


getServerInfo


_mcp.getServerInfo() : Values

Description

Gets the MCP server information, including configured name and version.

How To Use
const info = _mcp.getServerInfo();
_log.info('Server: ' + info.get('name') + ' v' + info.get('version'));
Return

( Values )

Object with the fields name (server name) and version (server version).


init


_mcp.init() : void

Return

( void )


isEnabled


_mcp.isEnabled() : boolean

Description

Checks if the MCP server is active and available to process requests.

How To Use
if (_mcp.isEnabled()) {
_log.info('MCP server is active');
}
Return

( boolean )

True if the MCP server is active, false otherwise.


listAvailableTools


_mcp.listAvailableTools() : Values

Description

Lists all tools registered in the MCP server, including name, description and input schema.

How To Use
const tools = _mcp.listAvailableTools();
for (const tool of tools) {
_log.info('Name: ' + tool.get('name'));
_log.info('Description: ' + tool.get('description'));
_log.info('Schema: ' + JSON.stringify(tool.get('inputSchema')));
}
Return

( Values )

List of objects, each containing the fields: name (tool name), description (description) and inputSchema (JSON schema of parameters).


registerTool


_mcp.registerTool(name: string, description: string, schema: Values, execute: java.util.function.Function<Values, Values>) : void

Description

Registers a new tool in the MCP server, making it available for remote execution. The schema must follow the JSON Schema format for input parameter validation.

How To Use
_mcp.registerTool(
'greeting',
'Returns a personalized greeting',
_val.map()
.set('type', 'object')
.set('properties', _val.map()
.set('name', _val.map().set('type', 'string').set('description', 'Person\'s name'))
)
.set('required', _val.list().add('name')),
(input) => {
return _val.map()
.set('success', true)
.set('message', 'Hello, ' + input.get('name') + '!');
}
);
Attributes
NAMETYPEDESCRIPTION
namestringUnique name of the tool. Must be used to execute the tool later.
descriptionstringTextual description of the tool's functionality.
schemaValuesJSON schema that defines the tool's input parameters.
executejava.util.function.FunctionFunction that implements the tool's logic. Receives input parameters and returns the result.
Return

( void )