@utilarium/cardigantime 0.0.24-dev.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/LICENSE +65 -0
  2. package/README.md +398 -0
  3. package/dist/cardigantime.cjs +2169 -0
  4. package/dist/cardigantime.cjs.map +1 -0
  5. package/dist/cardigantime.d.ts +92 -0
  6. package/dist/cardigantime.js +198 -0
  7. package/dist/cardigantime.js.map +1 -0
  8. package/dist/config/executable-security.d.ts +32 -0
  9. package/dist/config/format-detector.d.ts +59 -0
  10. package/dist/configure.d.ts +55 -0
  11. package/dist/configure.js +125 -0
  12. package/dist/configure.js.map +1 -0
  13. package/dist/constants.d.ts +25 -0
  14. package/dist/constants.js +38 -0
  15. package/dist/constants.js.map +1 -0
  16. package/dist/discovery/discoverer.d.ts +62 -0
  17. package/dist/discovery/hierarchical-modes.d.ts +64 -0
  18. package/dist/discovery/index.d.ts +15 -0
  19. package/dist/discovery/patterns.d.ts +77 -0
  20. package/dist/discovery/root-detection.d.ts +100 -0
  21. package/dist/discovery/traversal-security.d.ts +106 -0
  22. package/dist/env/errors.d.ts +18 -0
  23. package/dist/env/index.d.ts +7 -0
  24. package/dist/env/naming.d.ts +38 -0
  25. package/dist/env/parser.d.ts +61 -0
  26. package/dist/env/reader.d.ts +45 -0
  27. package/dist/env/resolver.d.ts +25 -0
  28. package/dist/env/schema-utils.d.ts +33 -0
  29. package/dist/env/types.d.ts +43 -0
  30. package/dist/error/ArgumentError.d.ts +31 -0
  31. package/dist/error/ArgumentError.js +48 -0
  32. package/dist/error/ArgumentError.js.map +1 -0
  33. package/dist/error/ConfigParseError.d.ts +26 -0
  34. package/dist/error/ConfigurationError.d.ts +21 -0
  35. package/dist/error/ConfigurationError.js +46 -0
  36. package/dist/error/ConfigurationError.js.map +1 -0
  37. package/dist/error/FileSystemError.d.ts +30 -0
  38. package/dist/error/FileSystemError.js +58 -0
  39. package/dist/error/FileSystemError.js.map +1 -0
  40. package/dist/error/index.d.ts +4 -0
  41. package/dist/mcp/discovery.d.ts +105 -0
  42. package/dist/mcp/errors.d.ts +75 -0
  43. package/dist/mcp/index.d.ts +22 -0
  44. package/dist/mcp/integration.d.ts +184 -0
  45. package/dist/mcp/parser.d.ts +141 -0
  46. package/dist/mcp/resolver.d.ts +165 -0
  47. package/dist/mcp/tools/check-config-types.d.ts +208 -0
  48. package/dist/mcp/tools/check-config.d.ts +85 -0
  49. package/dist/mcp/tools/index.d.ts +12 -0
  50. package/dist/mcp/types.d.ts +210 -0
  51. package/dist/parsers/index.d.ts +25 -0
  52. package/dist/parsers/javascript-parser.d.ts +12 -0
  53. package/dist/parsers/json-parser.d.ts +6 -0
  54. package/dist/parsers/typescript-parser.d.ts +15 -0
  55. package/dist/parsers/yaml-parser.d.ts +6 -0
  56. package/dist/read.d.ts +56 -0
  57. package/dist/read.js +653 -0
  58. package/dist/read.js.map +1 -0
  59. package/dist/security/audit-logger.d.ts +135 -0
  60. package/dist/security/cli-validator.d.ts +73 -0
  61. package/dist/security/config-validator.d.ts +95 -0
  62. package/dist/security/defaults.d.ts +17 -0
  63. package/dist/security/index.d.ts +14 -0
  64. package/dist/security/numeric-guard.d.ts +111 -0
  65. package/dist/security/path-guard.d.ts +53 -0
  66. package/dist/security/profiles.d.ts +127 -0
  67. package/dist/security/security-validator.d.ts +109 -0
  68. package/dist/security/string-guard.d.ts +92 -0
  69. package/dist/security/types.d.ts +126 -0
  70. package/dist/security/zod-secure-enum.d.ts +20 -0
  71. package/dist/security/zod-secure-number.d.ts +39 -0
  72. package/dist/security/zod-secure-path.d.ts +24 -0
  73. package/dist/security/zod-secure-string.d.ts +38 -0
  74. package/dist/types.d.ts +584 -0
  75. package/dist/types.js +56 -0
  76. package/dist/types.js.map +1 -0
  77. package/dist/util/hierarchical.d.ts +136 -0
  78. package/dist/util/hierarchical.js +436 -0
  79. package/dist/util/hierarchical.js.map +1 -0
  80. package/dist/util/schema-defaults.d.ts +80 -0
  81. package/dist/util/schema-defaults.js +118 -0
  82. package/dist/util/schema-defaults.js.map +1 -0
  83. package/dist/util/storage.d.ts +31 -0
  84. package/dist/util/storage.js +154 -0
  85. package/dist/util/storage.js.map +1 -0
  86. package/dist/validate.d.ts +113 -0
  87. package/dist/validate.js +260 -0
  88. package/dist/validate.js.map +1 -0
  89. package/package.json +84 -0
@@ -0,0 +1,85 @@
1
+ import { ZodSchema } from 'zod';
2
+ import { MCPInvocationContext } from '../types';
3
+ import { CheckConfigInput, CheckConfigResult } from './check-config-types';
4
+ /**
5
+ * Options for the CheckConfig tool.
6
+ */
7
+ export interface CheckConfigOptions {
8
+ /**
9
+ * Application name for documentation links.
10
+ */
11
+ appName: string;
12
+ /**
13
+ * Zod schema for configuration validation.
14
+ */
15
+ schema: ZodSchema;
16
+ /**
17
+ * Base URL for documentation.
18
+ * @default "https://github.com/utilarium/cardigantime"
19
+ */
20
+ docsBaseUrl?: string;
21
+ /**
22
+ * Function to resolve file-based configuration.
23
+ * Required when MCP config is not provided.
24
+ */
25
+ resolveFileConfig?: (workingDirectory: string) => Promise<any>;
26
+ }
27
+ /**
28
+ * Implements the CheckConfig tool for MCP servers.
29
+ *
30
+ * This tool helps AI assistants understand how a tool is configured by:
31
+ * - Showing where configuration came from (MCP, file, or defaults)
32
+ * - Displaying the resolved configuration (with sensitive values sanitized)
33
+ * - Providing links to relevant documentation
34
+ * - Optionally showing detailed breakdown of config sources
35
+ *
36
+ * @param input - Tool input parameters
37
+ * @param context - MCP invocation context
38
+ * @param options - Tool configuration options
39
+ * @returns Promise resolving to CheckConfigResult
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * const result = await checkConfig(
44
+ * { verbose: true },
45
+ * { config: { port: 3000 } },
46
+ * { appName: 'myapp', schema: mySchema }
47
+ * );
48
+ *
49
+ * console.log(result.summary);
50
+ * // "Configuration loaded from MCP invocation"
51
+ * ```
52
+ */
53
+ export declare function checkConfig(input: CheckConfigInput, context: MCPInvocationContext, options: CheckConfigOptions): Promise<CheckConfigResult>;
54
+ /**
55
+ * Sanitizes a configuration object by masking sensitive values.
56
+ *
57
+ * Recursively walks through the configuration and replaces sensitive
58
+ * values with "***" to prevent accidental exposure.
59
+ *
60
+ * @param config - Configuration object to sanitize
61
+ * @param path - Current path in the object (for nested fields)
62
+ * @returns Sanitized configuration object
63
+ */
64
+ export declare function sanitizeConfig(config: Record<string, unknown>, path?: string): Record<string, unknown>;
65
+ /**
66
+ * Creates a CheckConfig tool handler for MCP servers.
67
+ *
68
+ * This is a convenience function that returns a handler function
69
+ * ready to be registered with an MCP server.
70
+ *
71
+ * @param options - Tool configuration options
72
+ * @returns Tool handler function
73
+ *
74
+ * @example
75
+ * ```typescript
76
+ * const handler = createCheckConfigHandler({
77
+ * appName: 'myapp',
78
+ * schema: myConfigSchema,
79
+ * resolveFileConfig: async (dir) => loadConfig(dir),
80
+ * });
81
+ *
82
+ * server.registerTool('check_config', handler);
83
+ * ```
84
+ */
85
+ export declare function createCheckConfigHandler(options: CheckConfigOptions): (input: CheckConfigInput, context: MCPInvocationContext) => Promise<CheckConfigResult>;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * MCP tools module.
3
+ *
4
+ * This module exports built-in MCP tools that are automatically available
5
+ * in all CardiganTime-based MCP servers.
6
+ *
7
+ * @module mcp/tools
8
+ */
9
+ export type { CheckConfigInput, CheckConfigResult, CheckConfigToolDescriptor, ConfigSourceType, ConfigValueSource, } from './check-config-types';
10
+ export { CHECK_CONFIG_TOOL_DESCRIPTOR, SENSITIVE_FIELD_PATTERNS, isSensitiveField, sanitizeValue, } from './check-config-types';
11
+ export { checkConfig, sanitizeConfig, createCheckConfigHandler, } from './check-config';
12
+ export type { CheckConfigOptions, } from './check-config';
@@ -0,0 +1,210 @@
1
+ import { ConfigFormat } from '../types';
2
+ /**
3
+ * Configuration source from an MCP (Model Context Protocol) server invocation.
4
+ *
5
+ * MCP servers receive configuration as JSON in the server invocation context.
6
+ * This type tracks the raw configuration received from the MCP environment
7
+ * and when it was received.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * const mcpSource: MCPConfigSource = {
12
+ * type: 'mcp',
13
+ * rawConfig: { features: ['validation'], maxRetries: 3 },
14
+ * receivedAt: new Date(),
15
+ * };
16
+ * ```
17
+ */
18
+ export interface MCPConfigSource {
19
+ /** Source type discriminator */
20
+ type: 'mcp';
21
+ /**
22
+ * The raw JSON configuration received from the MCP invocation.
23
+ * This is the unparsed, unvalidated configuration object.
24
+ */
25
+ rawConfig: unknown;
26
+ /**
27
+ * Timestamp when the configuration was received from the MCP server.
28
+ * Used for debugging and cache invalidation.
29
+ */
30
+ receivedAt: Date;
31
+ }
32
+ /**
33
+ * Configuration source from a file on the filesystem.
34
+ *
35
+ * File-based configuration is the traditional way of providing config,
36
+ * supporting multiple formats (YAML, JSON, JavaScript, TypeScript).
37
+ * This type tracks the file path, format, and optional parent configs
38
+ * in hierarchical mode.
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * const fileSource: FileConfigSource = {
43
+ * type: 'file',
44
+ * filePath: '/project/app.config.yaml',
45
+ * format: ConfigFormat.YAML,
46
+ * parents: [parentConfigSource],
47
+ * };
48
+ * ```
49
+ */
50
+ export interface FileConfigSource {
51
+ /** Source type discriminator */
52
+ type: 'file';
53
+ /**
54
+ * Absolute path to the configuration file.
55
+ */
56
+ filePath: string;
57
+ /**
58
+ * The format of the configuration file.
59
+ */
60
+ format: ConfigFormat;
61
+ /**
62
+ * Parent configuration sources in hierarchical mode.
63
+ * These are configs from parent directories that were merged.
64
+ * Ordered from most specific (closest) to least specific (furthest).
65
+ */
66
+ parents?: FileConfigSource[];
67
+ }
68
+ /**
69
+ * Configuration source from environment variables.
70
+ *
71
+ * Environment variables provide a system-wide way to configure applications.
72
+ * This type tracks which environment variables were read and their values.
73
+ *
74
+ * @example
75
+ * ```typescript
76
+ * const envSource: EnvVarConfigSource = {
77
+ * type: 'env',
78
+ * variables: new Map([
79
+ * ['MYAPP_PORT', { value: '3000', isCustom: false }],
80
+ * ['OPENAI_API_KEY', { value: 'sk-...', isCustom: true }],
81
+ * ]),
82
+ * readAt: new Date(),
83
+ * };
84
+ * ```
85
+ */
86
+ export interface EnvVarConfigSource {
87
+ /** Source type discriminator */
88
+ type: 'env';
89
+ /**
90
+ * Map of environment variable names to their values.
91
+ * Keys are the actual env var names (e.g., 'MYAPP_PORT').
92
+ * Values indicate whether custom mapping was used.
93
+ */
94
+ variables: Map<string, {
95
+ value: string;
96
+ isCustom: boolean;
97
+ }>;
98
+ /**
99
+ * Timestamp when the environment variables were read.
100
+ * Used for debugging and cache invalidation.
101
+ */
102
+ readAt: Date;
103
+ }
104
+ /**
105
+ * Union type representing all possible configuration sources.
106
+ *
107
+ * Configuration can come from:
108
+ * - MCP server invocation (for AI assistant tools)
109
+ * - File on the filesystem (traditional config files)
110
+ * - Environment variables (system-wide configuration)
111
+ *
112
+ * Use the `type` discriminator to determine which source type you have.
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * function handleConfig(source: ConfigSource) {
117
+ * if (source.type === 'mcp') {
118
+ * console.log('Config from MCP:', source.rawConfig);
119
+ * } else if (source.type === 'file') {
120
+ * console.log('Config from file:', source.filePath);
121
+ * } else {
122
+ * console.log('Config from env vars:', source.variables.size);
123
+ * }
124
+ * }
125
+ * ```
126
+ */
127
+ export type ConfigSource = MCPConfigSource | FileConfigSource | EnvVarConfigSource;
128
+ /**
129
+ * A fully resolved configuration with source tracking.
130
+ *
131
+ * This type represents the final configuration after all parsing,
132
+ * validation, and merging has been completed. It includes metadata
133
+ * about where the configuration came from and how it was resolved.
134
+ *
135
+ * @template T - The type of the parsed configuration object
136
+ *
137
+ * @example
138
+ * ```typescript
139
+ * const resolved: ResolvedConfig<AppConfig> = {
140
+ * source: { type: 'file', filePath: '/app/config.yaml', format: ConfigFormat.YAML },
141
+ * config: { port: 3000, host: 'localhost' },
142
+ * hierarchical: true,
143
+ * resolution: 'Merged 3 configs from /app, /app/src, /app/src/api',
144
+ * };
145
+ * ```
146
+ */
147
+ export interface ResolvedConfig<T = unknown> {
148
+ /**
149
+ * The source of the configuration (MCP or file).
150
+ */
151
+ source: ConfigSource;
152
+ /**
153
+ * The parsed and validated configuration object.
154
+ */
155
+ config: T;
156
+ /**
157
+ * Whether hierarchical configuration lookup was used.
158
+ * True if multiple configs were merged from parent directories.
159
+ */
160
+ hierarchical: boolean;
161
+ /**
162
+ * Human-readable explanation of how the configuration was resolved.
163
+ * Useful for debugging and the checkConfig tool.
164
+ *
165
+ * @example "Config from MCP invocation"
166
+ * @example "Merged 2 configs: /project/config.yaml, /project/src/config.yaml"
167
+ * @example "Single config from /app/.myapprc.json"
168
+ */
169
+ resolution: string;
170
+ }
171
+ /**
172
+ * Context information provided by MCP server invocations.
173
+ *
174
+ * When an MCP tool is invoked by an AI assistant (like Claude in Cursor),
175
+ * the invocation includes contextual information about the environment.
176
+ * This type captures that context.
177
+ *
178
+ * @example
179
+ * ```typescript
180
+ * // MCP tool invocation from Cursor
181
+ * const context: MCPInvocationContext = {
182
+ * workingDirectory: '/Users/dev/project',
183
+ * targetFile: '/Users/dev/project/src/index.ts',
184
+ * config: {
185
+ * features: ['validation'],
186
+ * maxRetries: 3,
187
+ * },
188
+ * };
189
+ * ```
190
+ */
191
+ export interface MCPInvocationContext {
192
+ /**
193
+ * Working directory for file operations.
194
+ * This is typically the current working directory in the AI assistant's context.
195
+ * Used as the starting point for hierarchical config discovery.
196
+ */
197
+ workingDirectory?: string;
198
+ /**
199
+ * The specific file being operated on.
200
+ * For tools like Protokoll that operate on files, this is the target file path.
201
+ * Used to determine the most relevant configuration.
202
+ */
203
+ targetFile?: string;
204
+ /**
205
+ * MCP-provided configuration object.
206
+ * This is the raw configuration passed by the AI assistant environment.
207
+ * Must be parsed and validated before use.
208
+ */
209
+ config?: unknown;
210
+ }
@@ -0,0 +1,25 @@
1
+ import { ConfigFormat, ConfigParser } from '../types';
2
+ /**
3
+ * Gets a parser for a given file extension.
4
+ *
5
+ * @param extension - File extension (e.g., '.json', '.yaml')
6
+ * @returns The parser for this extension, or undefined if not found
7
+ */
8
+ export declare function getParserForExtension(extension: string): ConfigParser | undefined;
9
+ /**
10
+ * Gets a parser for a given format.
11
+ *
12
+ * @param format - Configuration format
13
+ * @returns The parser for this format, or undefined if not found
14
+ */
15
+ export declare function getParserForFormat(format: ConfigFormat): ConfigParser | undefined;
16
+ /**
17
+ * Gets all registered parsers.
18
+ *
19
+ * @returns Array of all registered parsers
20
+ */
21
+ export declare function getAllParsers(): ConfigParser[];
22
+ export { jsonParser } from './json-parser';
23
+ export { yamlParser } from './yaml-parser';
24
+ export { javascriptParser } from './javascript-parser';
25
+ export { typescriptParser } from './typescript-parser';
@@ -0,0 +1,12 @@
1
+ import { ConfigParser } from '../types';
2
+ /**
3
+ * JavaScript configuration parser.
4
+ * Loads and executes JavaScript configuration files using dynamic import.
5
+ *
6
+ * Supports:
7
+ * - ESM default exports: `export default { ... }`
8
+ * - CommonJS exports: `module.exports = { ... }`
9
+ * - Async function exports: `export default async () => ({ ... })`
10
+ * - Named exports as fallback
11
+ */
12
+ export declare const javascriptParser: ConfigParser;
@@ -0,0 +1,6 @@
1
+ import { ConfigParser } from '../types';
2
+ /**
3
+ * JSON configuration parser.
4
+ * Parses configuration from .json files using native JSON.parse.
5
+ */
6
+ export declare const jsonParser: ConfigParser;
@@ -0,0 +1,15 @@
1
+ import { ConfigParser } from '../types';
2
+ /**
3
+ * TypeScript configuration parser.
4
+ * Loads and executes TypeScript configuration files using dynamic import.
5
+ *
6
+ * IMPORTANT: TypeScript files must be transpiled before they can be imported.
7
+ * This parser relies on a TypeScript runtime being available (tsx, ts-node, etc.)
8
+ * or the files being pre-compiled to JavaScript.
9
+ *
10
+ * Supports:
11
+ * - ESM default exports: `export default { ... }`
12
+ * - Async function exports: `export default async () => ({ ... })`
13
+ * - Type-safe configs with defineConfig helper
14
+ */
15
+ export declare const typescriptParser: ConfigParser;
@@ -0,0 +1,6 @@
1
+ import { ConfigParser } from '../types';
2
+ /**
3
+ * YAML configuration parser.
4
+ * Parses configuration from .yaml and .yml files using js-yaml.
5
+ */
6
+ export declare const yamlParser: ConfigParser;
package/dist/read.d.ts ADDED
@@ -0,0 +1,56 @@
1
+ import { z, ZodObject } from 'zod';
2
+ import { Args, ConfigSchema, Options } from './types';
3
+ /**
4
+ * Reads configuration from files and merges it with CLI arguments.
5
+ *
6
+ * This function implements the core configuration loading logic:
7
+ * 1. Validates and resolves the configuration directory path
8
+ * 2. Attempts to read the YAML configuration file
9
+ * 3. Safely parses the YAML content with security protections
10
+ * 4. Merges file configuration with runtime arguments
11
+ * 5. Returns a typed configuration object
12
+ *
13
+ * The function handles missing files gracefully and provides detailed
14
+ * logging for troubleshooting configuration issues.
15
+ *
16
+ * @template T - The Zod schema shape type for configuration validation
17
+ * @param args - Parsed command-line arguments containing potential config overrides
18
+ * @param options - Cardigantime options with defaults, schema, and logger
19
+ * @returns Promise resolving to the merged and typed configuration object
20
+ * @throws {Error} When configuration directory is invalid or required files cannot be read
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * const config = await read(cliArgs, {
25
+ * defaults: { configDirectory: './config', configFile: 'app.yaml' },
26
+ * configShape: MySchema.shape,
27
+ * logger: console,
28
+ * features: ['config']
29
+ * });
30
+ * // config is fully typed based on your schema
31
+ * ```
32
+ */
33
+ export declare const read: <T extends z.ZodRawShape>(args: Args, options: Options<T>) => Promise<z.infer<ZodObject<T & typeof ConfigSchema.shape>>>;
34
+ /**
35
+ * Checks and displays the resolved configuration with detailed source tracking.
36
+ *
37
+ * This function provides a git blame-like view of configuration resolution,
38
+ * showing which file and hierarchical level contributed each configuration value.
39
+ *
40
+ * @template T - The Zod schema shape type for configuration validation
41
+ * @param args - Parsed command-line arguments
42
+ * @param options - Cardigantime options with defaults, schema, and logger
43
+ * @returns Promise that resolves when the configuration check is complete
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * await checkConfig(cliArgs, {
48
+ * defaults: { configDirectory: './config', configFile: 'app.yaml' },
49
+ * configShape: MySchema.shape,
50
+ * logger: console,
51
+ * features: ['config', 'hierarchical']
52
+ * });
53
+ * // Outputs detailed configuration source analysis
54
+ * ```
55
+ */
56
+ export declare const checkConfig: <T extends z.ZodRawShape>(args: Args, options: Options<T>) => Promise<void>;