Adding a New Tool
Step-by-step guide to authoring a new MCP tool with strict Zod schemas, RBAC and tier guards, DTO projections, registry registration, and automated docs manifest sync.
Design contract for Taicket MCP tools
Taicket MCP exposes coarse-grained, domain-level tools rather than thin CRUD wrappers around raw MongoDB collections. Every tool enforces authentication, RBAC, cross-guild resource validation, subscription tier parity, DTO projection, and HMAC audit logging before returning.
- Strict Zod input schema:: Use .strict() on input objects, validate all Discord IDs with the 17 to 20 digit snowflake regex, bound string lengths and array sizes, and never accept a caller userId field.
- Explicit scopes and annotations:: Declare required McpScope values (mcp:read, mcp:write, mcp:deploy, mcp:destructive) and accurate MCP annotations (readOnlyHint, destructiveHint, idempotentHint, openWorldHint).
- Preview/apply parity:: Configuration tools must accept mode: "preview" | "apply" and route destructive or structural actions through McpSetupPreview with requiresHumanApproval: true.
Tool implementation blueprint
import { z } from 'zod';
import { McpToolError } from '../security/errors.js';
import { assertGuildAccess, assertManageGuild } from '../security/permissions.js';
import { assertGuildChannelOwned } from '../security/discord.js';
import { enforceModuleTierPolicy } from '../security/premium.js';
import { appendAuditLog } from '../security/audit.js';
import type { RegisteredToolDefinition } from './registry.js';
const inputSchema = z
.object({
guildId: z.string().regex(/^\d{17,20}$/, 'Invalid Discord server ID'),
mode: z.enum(['preview', 'apply']).default('preview'),
logChannelId: z.string().regex(/^\d{17,20}$/).optional(),
enabled: z.boolean(),
})
.strict();
export const exampleConfigTool: RegisteredToolDefinition<typeof inputSchema> = {
name: 'configure_example_module',
title: 'Configure Example Module',
category: 'Automation & Utility',
summary: 'Preview or apply settings for an example guild module.',
scopes: ['mcp:write'],
annotations: {
readOnlyHint: false,
destructiveHint: false,
idempotentHint: true,
openWorldHint: false,
},
inputSchema,
async execute(input, ctx) {
// 1. Enforce grant allowedGuildIds, BotBlacklist, and live RBAC
await assertGuildAccess(ctx, input.guildId);
await assertManageGuild(ctx.userId, input.guildId);
// 2. Enforce 18-surface subscription policy
await enforceModuleTierPolicy(input.guildId, 'example', input);
// 3. Validate Discord channel ownership
if (input.logChannelId) {
await assertGuildChannelOwned(input.guildId, input.logChannelId, 'logChannelId');
}
if (input.mode === 'preview') {
return {
mode: 'preview',
guildId: input.guildId,
proposedChanges: {
enabled: input.enabled,
logChannelId: input.logChannelId ?? null,
},
};
}
// 4. Persist using explicit field allowlist (never spread untrusted input)
const updated = await ExampleConfigModel.findOneAndUpdate(
{ guildId: input.guildId },
{
$set: {
enabled: input.enabled,
logChannelId: input.logChannelId ?? null,
},
},
{ upsert: true, new: true }
).lean();
if (!updated) {
throw new McpToolError('INTERNAL_ERROR', 'Unable to save module settings.');
}
// 5. Append tamper-evident HMAC audit entry
await appendAuditLog({
ctx,
guildId: input.guildId,
toolName: 'configure_example_module',
module: 'example',
action: 'apply',
outcome: 'allowed',
changesSummary: 'Updated enabled state and log channel.',
});
// 6. Return explicit DTO (never return raw Mongoose document)
return {
mode: 'apply',
guildId: input.guildId,
config: {
enabled: Boolean(updated.enabled),
logChannelId: updated.logChannelId ?? null,
},
};
},
};Registering the tool and syncing tools-manifest.json
- 1
Add to mcp/src/tools/registry.ts
Export the tool from its domain file and add it to the central tool registry array along with documentation metadata (overview, permissions, example request/response, errors, behavior, and related tools).
- 2
Regenerate the live schema manifest
Run build:docs-manifest so mcp/scripts/generate-docs-manifest.ts compiles the Zod schema into mcp/docs-site/src/generated/tools-manifest.json. This keeps /docs/tools/<toolName> 100% synchronized with production validation.
Regenerate docs manifest and run parity verificationbashnpm --prefix mcp run build:docs-manifest npm --prefix mcp run typecheck npm --prefix mcp test npm --prefix mcp run build:all