Define custom tools

Define each custom tool in actions/mcp-tool/tools.js with a schema and a handler, then add it to the TOOLS array. Before you start, create a custom tools server.

Tool definition fields fields

Field
Type
Required
Description
name
string
Yes
Name used by tools/call.
title, description
string
No
Shown to the AI model and client. A clear description helps the model choose the tool.
inputSchema
Zod raw shape
No
Tool arguments. The SDK validates them and publishes the schema as JSON Schema.
annotations
object
No
Standard MCP hints, such as readOnlyHint.
authenticated
boolean
No (defaults to false)
When true, the handler receives the caller’s { token, clientId, scopes } as auth. See The authenticated flag.
handler
function
Yes
({ args, auth, logger }) => CallToolResult

Example: call a custom backend example

The following tool looks up a custom business attribute for a product SKU. Replace the backend call with your own logic.

const { z } = require('zod')

const getCustomAttribute = defineTool({
  name: 'getCustomAttribute',
  title: 'Get custom product attribute',
  description: 'Returns our custom loyalty-tier eligibility for a product SKU.',
  inputSchema: {
    sku: z.string().describe('Product SKU')
  },
  annotations: { readOnlyHint: true, openWorldHint: true },
  authenticated: true,
  handler: async ({ args, auth, logger }) => {
    // auth.token is the caller's IMS access token. Never log or return it.
    const res = await fetch(`https://api.example.com/skus/${encodeURIComponent(args.sku)}`, {
      headers: { Authorization: `Bearer ${auth.token}` }
    })
    if (!res.ok) {
      logger.warn(`lookup failed: ${res.status}`)
      return textResult('Lookup failed.', true)
    }
    return textResult(JSON.stringify(await res.json()))
  }
})

// Register the tool.
const TOOLS = [currentDate, whoAmI, getCustomAttribute]

After you add the tool to TOOLS, tools/list publishes it in this format:

{
  "name": "getCustomAttribute",
  "description": "...",
  "inputSchema": {
    "type": "object",
    "properties": { "sku": { "type": "string", "description": "Product SKU" } },
    "required": ["sku"]
  },
  "_meta": { "authenticated": true }
}

The authenticated flag authenticated

The authenticated field controls whether the handler receives the caller’s authentication context:

  • When authenticated is true, the custom tools server passes the caller’s { token, clientId, scopes } to the handler as auth.
  • When authenticated is false or omitted, the handler receives no authentication context.
  • The server rejects any non-boolean value when it loads the definition.

The custom tools server always publishes the flag in tools/list as _meta.authenticated, with a value of true or false. This tells the Shopper MCP Server which tools need the caller’s authorization header forwarded. The MCP SDK reserves _meta for custom data, so SDK clients such as client.listTools() keep the field.

IMPORTANT
Setting a tool’s authenticated flag to false means its handler does not receive the caller’s authentication context. It does not make the tool public: every MCP POST request still requires IMS authentication.

Example tools example-tools

The generated tools.js file includes two examples that you can use as models and smoke-test targets:

Tool
authenticated
Description
currentDate
false
Returns the current date and time from the Runtime clock, optionally formatted for an IANA timezone such as Europe/Bucharest. The default timezone is UTC.
whoAmI
true
Returns the caller’s IMS client ID, user ID, token type, and expiry. It never returns the token itself. Included when examples are enabled.

Guidelines guidelines

  • Set authenticated: true only for tools that need the caller’s identity, such as tools that forward auth.token to another API.
  • Never log or return auth.token.
  • To use a Runtime-side secret in a handler, add it as an action input in app.config.yaml and read it from the action parameters.

Protect another action protect-action

To apply the same IMS protection to another action in your project, wrap its handler with the middleware chain:

const { withMiddleware } = require('../middleware')
const { cors } = require('../middleware/cors')
const { imsAuth } = require('../middleware/ims-auth')

async function handler (params, ctx) { /* ... */ }

exports.main = withMiddleware(handler, [cors(), imsAuth()])

Test and redeploy redeploy

  1. Add a unit test for your tool in test/, using the generated tests as a model.
  2. Run npm test and npm run lint.
  3. Run aio app deploy.
  4. If your server is already registered, refresh the registration so Shopper MCP Server picks up the changed tool definitions.

Next step next-step

Register custom tools

recommendation-more-help
commerce-help-shopper-mcp