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
nametools/call.title, descriptioninputSchemaannotationsreadOnlyHint.authenticatedfalse)true, the handler receives the caller’s { token, clientId, scopes } as auth. See The authenticated flag.handler({ args, auth, logger }) => CallToolResultExample: 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
authenticatedistrue, the custom tools server passes the caller’s{ token, clientId, scopes }to the handler asauth. - When
authenticatedisfalseor 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.
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:
authenticatedcurrentDatefalseEurope/Bucharest. The default timezone is UTC.whoAmItrueGuidelines guidelines
- Set
authenticated: trueonly for tools that need the caller’s identity, such as tools that forwardauth.tokento 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.yamland 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
- Add a unit test for your tool in
test/, using the generated tests as a model. - Run
npm testandnpm run lint. - Run
aio app deploy. - If your server is already registered, refresh the registration so Shopper MCP Server picks up the changed tool definitions.