import { type McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import type { ReadResourceResult } from "@modelcontextprotocol/sdk/types.js"; import type { z } from "zod"; import type { Logger } from "./types/Logger.js"; /** * Resource configuration metadata */ export interface ResourceConfig { /** Unique resource identifier (e.g., "modyo-spaces-list") */ name: string; /** URI template for the resource (e.g., "modyo-spaces://list") */ uriTemplate: string; /** Human-readable description of what the resource provides */ description: string; /** MIME type of the resource content (e.g., "application/json", "text/markdown") */ mimeType?: string; /** MCP annotations for the resource */ annotations?: { readOnlyHint?: boolean; idempotentHint?: boolean; destructiveHint?: boolean; }; /** If true, catches errors and returns them in content instead of throwing */ catchErrors?: boolean; } /** * Abstract base class for MCP resources * * Provides a structured, class-based approach to creating resources with: * - Type-safe parameter validation via Zod schemas * - Consistent error handling patterns * - Logging integration * - Repository access helpers * - Support for both dynamic (API-based) and static (file-based) resources * * @example Dynamic Resource (API Data) * ```typescript * class ListSpacesResource extends ResourceBase<{ spaceId: number }> { * protected config: ResourceConfig = { * name: "modyo-spaces-list", * uriTemplate: "modyo-spaces://{spaceId}", * description: "Fetches spaces from Modyo platform", * mimeType: "application/json", * annotations: { readOnlyHint: true } * }; * * protected getParamsSchema() { * return z.object({ spaceId: z.number() }); * } * * async read(uri: URL, params: { spaceId: number }): Promise { * const repo = await this.getRepository(SpacesRepository); * const response = await repo.getSpace(params.spaceId); * return this.success(uri, response); * } * } * ``` * * @example Static Resource (File Content) * ```typescript * class DocResource extends ResourceBase { * protected config: ResourceConfig = { * name: "docs-site-create", * uriTemplate: "docs://tools/site-create", // No parameters * description: "Documentation for site-create tool", * mimeType: "text/markdown", * annotations: { readOnlyHint: true } * }; * * protected getParamsSchema() { * return z.void(); // No parameters * } * * async read(uri: URL): Promise { * const content = readFileSync("docs/site-create.md", "utf-8"); * return this.successText(uri, content, "text/markdown"); * } * } * ``` */ export declare abstract class ResourceBase { protected server: McpServer; protected logger: Logger; /** * Resource configuration - must be defined by subclasses */ protected abstract config: ResourceConfig; /** * Returns the Zod schema for validating resource parameters * Override to provide custom validation * * For static resources with no parameters, return z.void() */ protected abstract getParamsSchema(): z.ZodType; /** * Reads and returns the resource content * @param uri - The resource URI that was requested * @param params - Validated parameters (may be void for static resources) * @returns MCP ReadResourceResult */ protected abstract read(uri: URL, params: TParams): Promise; /** * Initializes the resource with MCP server context * Called automatically during registration */ private initialize; /** * Registers this resource with the MCP server * Called by McpServerBase during setup * Can be overridden by subclasses to support async initialization (e.g., filesystem discovery) */ registerResource(server: McpServer, logger: Logger): void | Promise; /** * Helper: Gets a repository instance using the configured platform * Uses SinglePlatformProvider to get platform config from environment variables * * @param RepositoryClass - Repository constructor * @returns Repository instance * @throws Error if platform not configured */ protected getRepository(RepositoryClass: new (url: string, token: string, logger: Logger) => T): Promise; /** * Helper: Formats successful JSON response * Use for API-based resources that return JSON data */ protected success(uri: URL, data: unknown): ReadResourceResult; /** * Helper: Formats successful text response * Use for file-based resources like markdown docs */ protected successText(uri: URL, text: string, mimeType?: string): ReadResourceResult; /** * Helper: Formats error response as resource content * Returns a properly formatted error response without throwing */ protected errorResponse(uri: URL, error: unknown, context?: Record): ReadResourceResult; /** * Helper: Logs debug information */ protected debug(message: string, data?: unknown): void; /** * Helper: Logs info */ protected info(message: string, data?: unknown): void; /** * Helper: Logs error */ protected logError(message: string, data?: unknown): void; /** * Extracts raw schema shape for MCP registration * Handles Zod object schemas */ protected extractRawSchema(schema: z.ZodType): Record; } //# sourceMappingURL=ResourceBase.d.ts.map