SuiteScript 2.1 Custom Tool Script Type Code Samples

This sample consists of the three requirements for building a custom tool: a custom tool script, a JSON file for the schema, and the SuiteCloud Development Framework (SDF) object XML file. These files show how to structure your custom tool before deployment to your NetSuite account.

In this example, the custom tool script defines two tool methods: add() and reverseText(). The add() tool method takes two parameters and returns their sum. The reverseText() method takes a string and returns the reversed string.

You can also find sample tool implementations in the MCP-Sample-Tools directory of the SuiteCloud Project Repository on Oracle Samples GitHub. This project includes SuiteScript-based sample tools for performing common business tasks through conversational AI. These examples demonstrate the types of AI-powered solutions that you can build and deploy to your own NetSuite account.

NetSuite also provides the MCP Standard Tools SuiteApp, which is available in the SuiteApp Marketplace. For more information, see MCP Standard Tools SuiteApp.

Custom Tool Script that Adds Numbers and Reverses a String

This sample shows a custom tool script that implements two tools: one for adding two numbers, and another for reversing a string.

Note:

This script sample uses the define function, which is required for an entry point script (a script you attach to a script record and deploy). You must use the require function if you want to copy the script into the SuiteScript Debugger and test it. For more information, see SuiteScript 2.x Global Objects.

Important:

This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.

            /**
 * exampletools.js
 * @NApiVersion 2.1
 * @NModuleScope Public
 * @NScriptType CustomTool
 */

define([], function() {
    return {
        add: async function (args) {
            try {
                const a = args["a"];
                const b = args["b"];
                return {
                    result: a+b
                };
            } catch(e) {
                return {
                    result: "",
                    error: "There was an error executin the tool: " + e
                }
            }
        },
        reverseText: async function (args) {
            try {
                const text = args["text"];
                return {
                    result: text.split('').reverse().join('')
                };
            } catch(e) {
                return {
                    result: "",
                    error: "There was an error executin the tool: " + e
                }
            }
        }
    }
}); 

          

Custom Tool Definition Schema Samples

This sample shows the JSON schema definition for the tools defined in the custom tool script. For more information about the properties of the JSON schema, see Custom Tool JSON Schema.

            {
    {
    "tools": [
        {
            "name": "add",
            "description": "Add two numbers together",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "a": { "type": "number", "description": "First addend" },
                    "b": { "type": "number", "description": "Second addend" }
                },
                "required": ["a", "b"]
            },
            "outputSchema": {
                "type": "object",
                "properties": {
                    "result": {
                        "type": "number",
                        "description": "Value of adding the two input numbers"
                    },
                    "error": {
                        "type": "string",
                        "description": "Error message if execution fails"
                    }
                },
                "required": ["result"]
            },
            "annotations": {
                "title": "Add Numbers",
                "readOnlyHint": true,
                "idempotentHint": true,
                "openWorldHint": false
            }
        },
        {
            "name": "reverseText",
            "description": "Reverses a given text string. Example: if the user says 'Reverse the text: I like apples', call this tool with { \"text\": \"I like apples\" }. ",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "text": { "type": "string", "description": "Text to reverse" }
                },
                "required": ["text"]
            },
            "outputSchema": {
                "type": "object",
                "properties": {
                    "result": {
                        "type": "string",
                        "description": "Value of reversing the input text value"
                    },
                    "error": {
                        "type": "string",
                        "description": "Error message if execution fails"
                    }
                },
                "required": ["result"]
            },
            "annotations": {
                "title": "Reverse Text",
                "readOnlyHint": true,
                "idempotentHint": true,
                "openWorldHint": false
            }
        }
    ]
} 

          

This sample shows the JSON schema definition for the tools defined in the custom tool script, without using outputSchema.

            {
    "tools": [
        {
            "name": "add",
            "description": "Add two numbers together",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "a": { "type": "number", "description": "First addend" },
                    "b": { "type": "number", "description": "Second addend" }
                },
                "required": ["a", "b"]
            },
            "annotations": {
                "title": "Add Numbers",
                "readOnlyHint": true,
                "idempotentHint": true,
                "openWorldHint": false
            }
        },
        {
            "name": "reverseText",
            "description": "Reverses a given text string. Example: if the user says 'Reverse the text: I like apples', call this tool with { \"text\": \"I like apples\" }. ",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "text": { "type": "string", "description": "Text to reverse" }
                },
                "required": ["text"]
            },
            "annotations": {
                "title": "Reverse Text",
                "readOnlyHint": true,
                "idempotentHint": true,
                "openWorldHint": false
            }
        }
    ]
} 

          

If you are using ChatGPT, you need to define which schema properties are nullable. The following sample is the JSON schema definition for the custom tools when you use ChatGPT.

            {
    "tools": [
        {
            "name": "add",
            "description": "Add two numbers together",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "a": { "type": "number", "description": "First addend" },
                    "b": { "type": "number", "description": "Second addend" }
                },
                "required": ["a", "b"],
                "nullable": []
            },
            "outputSchema": {
                "type": "object",
                "properties": {
                    "result": {
                        "type": "number",
                        "description": "Value of adding the two input numbers"
                    },
                    "error": {
                        "type": "string",
                        "description": "Error message if execution fails"
                    }
                },
                "required": ["result"],
                "nullable": ["error"]
            },
            "annotations": {
                "title": "Add Numbers",
                "readOnlyHint": true,
                "idempotentHint": true,
                "openWorldHint": false
            }
        },
        {
            "name": "reverseText",
            "description": "Reverses a given text string. Example: if the user says 'Reverse the text: I like apples', call this tool with { \"text\": \"I like apples\" }. ",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "text": { "type": "string", "description": "Text to reverse" }
                },
                "required": ["text"],
                "nullable": []
            },
            "outputSchema": {
                "type": "object",
                "properties": {
                    "result": {
                        "type": "string",
                        "description": "Value of reversing the input text value"
                    },
                    "error": {
                        "type": "string",
                        "description": "Error message if execution fails"
                    }
                },
                "required": ["result"],
                "nullable": ["error"]
            },
            "annotations": {
                "title": "Reverse Text",
                "readOnlyHint": true,
                "idempotentHint": true,
                "openWorldHint": false
            }
        }
    ]
} 

          

Custom Tool SDF XML Object Sample

This sample shows the SDF XML definition for the custom tool. In SDF, custom tools are defined as a tool SDF custom object. For more information, see Custom Tool Scripts as XML Definitions.

            <tool scriptid="customtool_test1">
  <name>Test Tool 1</name>
  <scriptfile>[/SuiteApps/com.netsuite.tools/tools/exampletools.js]</scriptfile>
  <rpcschema>[/SuiteApps/com.netsuite.tools/tools/exampletools_schema.json]</rpcschema>
  <exposeto3rdpartyagents>T</exposeto3rdpartyagents>
  <permissions>
    <permission>
            <permkey>LIST_EMAILTEMPLATE</permkey>
            <permlevel>FULL</permlevel>
    </permission>
    <permission>
            <permkey>ADMI_CRMLIST</permkey>
            <permlevel>VIEW</permlevel>
    </permission>
  </permissions>
</tool> 

          

Related Topics

General Notices