@emmahyde/thinking-patterns
Version:
MCP server combining systematic thinking, mental models, debugging approaches, and stochastic algorithms for comprehensive cognitive pattern support
111 lines (110 loc) • 3.5 kB
TypeScript
import { z } from 'zod';
/**
* Tool registry entry type definition
*/
export interface ToolRegistryEntry<TIn = unknown, TOut = unknown> {
name: string;
schema: z.ZodSchema<TIn>;
server: BaseToolServer<TIn, TOut>;
description?: string;
}
/**
* Standard MCP response envelope
*/
export interface MCPResponse {
content: Array<{
type: string;
text: string;
}>;
isError?: boolean;
}
/**
* Abstract base class for all tool servers
* Provides standardized validation, error handling, and response formatting
*/
export declare abstract class BaseToolServer<TIn, TOut> {
protected schema: z.ZodSchema<TIn>;
/**
* Constructor that accepts a Zod schema for input validation
* @param schema - Zod schema for validating input data
*/
constructor(schema: z.ZodSchema<TIn>);
/**
* Validates input using the provided Zod schema
* @param input - Raw input data to validate
* @returns Validated and typed input data
* @throws Error if validation fails
*/
protected validate(input: unknown): TIn;
/**
* Abstract method to be implemented by concrete servers
* Contains the core business logic for processing validated input
* @param validInput - Validated input data
* @returns Processed output data
*/
protected abstract handle(validInput: TIn): TOut;
/**
* Standardized process method for unified server interface
* Default implementation delegates to handle method for backward compatibility
* Servers can override this to provide standardized processing interface
* @param validInput - Validated input data
* @returns Processed output data
*/
process(validInput: TIn): TOut;
/**
* Main entry point that wraps validation, processing, and error handling
* Provides standardized {content, isError} envelope response
* @param rawInput - Raw input data from MCP request
* @returns Standardized MCP response
*/
run(rawInput: unknown): MCPResponse;
/**
* Optional method for servers that need custom response formatting
* @param result - Result from handle method
* @returns Formatted response content
*/
protected formatResponse(result: TOut): Array<{
type: string;
text: string;
}>;
/**
* Optional method for servers that need custom error formatting
* @param error - Error that occurred during processing
* @returns Formatted error response content
*/
protected formatError(error: Error): Array<{
type: string;
text: string;
}>;
}
/**
* Tool registry for managing all available tools
*/
export declare class ToolRegistry {
private static tools;
/**
* Register a new tool
* @param entry - Tool registry entry
*/
static register<TIn, TOut>(entry: ToolRegistryEntry<TIn, TOut>): void;
/**
* Find a tool by name
* @param name - Tool name
* @returns Tool registry entry or undefined
*/
static findTool(name: string): ToolRegistryEntry | undefined;
/**
* Get all registered tools
* @returns Array of all tool registry entries
*/
static getAllTools(): ToolRegistryEntry[];
/**
* Get tool names for MCP ListTools response
* @returns Array of tool definitions for MCP
*/
static getToolDefinitions(): Array<{
name: string;
description: string;
inputSchema: Record<string, unknown>;
}>;
}