UNPKG

random-tables-mcp

Version:

An MCP (Model Context Protocol) server for managing and rolling on random-table assets used in tabletop RPGs. Create, update, and roll on random tables with support for nested tables, weighted entries, and range-based results.

86 lines 3.21 kB
import { z } from 'zod'; import { Range, TableEntry } from '../../../../domain/index.js'; import { v4 as uuidv4 } from 'uuid'; import { BaseTool } from './tool.js'; /** * Tool for creating tables. */ export class CreateTableTool extends BaseTool { /** * Creates a new CreateTableTool instance. * @param tableService The table service to use. */ constructor(tableService) { super(); this.tableService = tableService; } /** * Gets the name of the tool. * @returns The tool name. */ getToolName() { return 'create_table'; } /** * Gets the description of the tool. * @returns The tool description. */ getToolDescription() { return 'Create a new random table. Table entries can include templates to reference other tables using the format {{reference-title::table-id::table-name::roll-number::separator}}.'; } /** * Gets the input schema for the tool. * @returns The input schema. */ getInputSchema() { return z.object({ name: z.string().describe('Table name'), description: z.string().optional().describe('Optional description'), entries: z .array(z.object({ content: z .string() .describe('The content of this entry. Can include templates in the format {{reference-title::table-id::table-name::roll-number::separator}} to reference other tables.'), weight: z.number().optional().default(1).describe('Probability weight (default: 1)'), range: z .object({ min: z.number().describe('Minimum value (inclusive)'), max: z.number().describe('Maximum value (inclusive)'), }) .optional() .describe('Optional range of values this entry corresponds to'), })) .optional() .describe('Optional initial entries'), }); } /** * Gets the output schema for the tool. * @returns The output schema. */ getOutputSchema() { return z.object({ tableId: z.string().describe('The ID of the created table'), }); } /** * Implements the tool execution logic. * @param args The validated tool arguments. * @returns The tool result. */ async executeImpl(args) { // Convert input entries to TableEntry objects const entries = args.entries?.map(entry => { // Generate a unique ID for the entry const id = uuidv4(); // Create a Range object if range is provided const range = entry.range ? new Range(entry.range.min, entry.range.max) : undefined; // Create the TableEntry with all required parameters return new TableEntry(id, entry.content, entry.weight ?? 1, range); }); // Call the table service to create the table const tableId = await this.tableService.createTable(args.name, args.description, entries); return { tableId }; } } //# sourceMappingURL=create-table-tool.js.map