Universal Command Pattern
by @ianderrington
Define commands once, deploy to CLI, API, and MCP automatically. Use when building new commands/tools for Supernal — ensures consistent interfaces across all...
Define a Command
import { UniversalCommand } from '@supernal/universal-command';export const userCreate = new UniversalCommand({
name: 'user create',
description: 'Create a new user',
input: {
parameters: [
{ name: 'name', type: 'string', required: true },
{ name: 'email', type: 'string', required: true },
{ name: 'role', type: 'string', default: 'user', enum: ['user', 'admin'] },
],
},
output: { type: 'json' },
handler: async (args, context) => {
return await createUser(args);
},
});
Deploy Everywhere
// CLI
program.addCommand(userCreate.toCLI());
// → mycli user create --name "Alice" --email "alice@example.com"// Next.js API
export const POST = userCreate.toNextAPI();
// → POST /api/users/create
// MCP Tool
const mcpTool = userCreate.toMCP();
// → user_create tool for AI agents
clawhub install universal-command