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.
69 lines • 1.96 kB
JavaScript
import { zodToJsonSchema } from 'zod-to-json-schema';
/**
* Base class for MCP tools.
*/
export class BaseTool {
/**
* Gets the name of the tool.
* @returns The tool name.
*/
getName() {
return this.getToolName();
}
/**
* Gets the tool definition for MCP.
* @returns The tool definition.
*/
getToolDefinition() {
return {
name: this.getToolName(),
description: this.getToolDescription(),
inputSchema: zodToJsonSchema(this.getInputSchema(), {
$refStrategy: 'none',
}),
};
}
/**
* Gets the output schema for the tool.
* @returns The output schema, or undefined if not needed.
*/
getOutputSchema() {
return undefined;
}
/**
* Validates the input arguments against the input schema.
* @param args The arguments to validate.
* @returns The validated arguments.
* @throws If the arguments are invalid.
*/
validateInput(args) {
return this.getInputSchema().parse(args);
}
/**
* Validates the output result against the output schema.
* @param result The result to validate.
* @returns The validated result.
* @throws If the result is invalid.
*/
validateOutput(result) {
const outputSchema = this.getOutputSchema();
if (outputSchema) {
return outputSchema.parse(result);
}
return result;
}
/**
* Executes the tool.
* @param args The tool arguments.
* @returns The tool result.
*/
async execute(args) {
// Validate input
const validatedInput = this.validateInput(args);
// Execute the tool implementation
const result = await this.executeImpl(validatedInput);
// Validate output if an output schema is provided
return this.validateOutput(result);
}
}
//# sourceMappingURL=tool.js.map