mcp-adr-analysis-server 1.1.0 → 2.0.2
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.
- package/README.md +367 -150
- package/dist/src/config/ai-config.d.ts +81 -0
- package/dist/src/config/ai-config.d.ts.map +1 -0
- package/dist/src/config/ai-config.js +134 -0
- package/dist/src/config/ai-config.js.map +1 -0
- package/dist/src/index.d.ts +97 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2430 -186
- package/dist/src/index.js.map +1 -1
- package/dist/src/prompts/adr-suggestion-prompts.d.ts +2 -2
- package/dist/src/prompts/adr-suggestion-prompts.d.ts.map +1 -1
- package/dist/src/prompts/adr-suggestion-prompts.js +25 -17
- package/dist/src/prompts/adr-suggestion-prompts.js.map +1 -1
- package/dist/src/prompts/analysis-prompts.js +1 -1
- package/dist/src/prompts/analysis-prompts.js.map +1 -1
- package/dist/src/resources/index.d.ts +1 -1
- package/dist/src/resources/index.d.ts.map +1 -1
- package/dist/src/resources/index.js +52 -44
- package/dist/src/resources/index.js.map +1 -1
- package/dist/src/templates/domain-knowledge-templates.d.ts +36 -0
- package/dist/src/templates/domain-knowledge-templates.d.ts.map +1 -0
- package/dist/src/templates/domain-knowledge-templates.js +376 -0
- package/dist/src/templates/domain-knowledge-templates.js.map +1 -0
- package/dist/src/tools/adr-suggestion-tool.d.ts +8 -3
- package/dist/src/tools/adr-suggestion-tool.d.ts.map +1 -1
- package/dist/src/tools/adr-suggestion-tool.js +561 -172
- package/dist/src/tools/adr-suggestion-tool.js.map +1 -1
- package/dist/src/tools/content-masking-tool.d.ts.map +1 -1
- package/dist/src/tools/content-masking-tool.js +168 -22
- package/dist/src/tools/content-masking-tool.js.map +1 -1
- package/dist/src/tools/deployment-analysis-tool.d.ts.map +1 -1
- package/dist/src/tools/deployment-analysis-tool.js +64 -27
- package/dist/src/tools/deployment-analysis-tool.js.map +1 -1
- package/dist/src/tools/environment-analysis-tool.d.ts.map +1 -1
- package/dist/src/tools/environment-analysis-tool.js +65 -29
- package/dist/src/tools/environment-analysis-tool.js.map +1 -1
- package/dist/src/tools/research-question-tool.d.ts.map +1 -1
- package/dist/src/tools/research-question-tool.js +150 -49
- package/dist/src/tools/research-question-tool.js.map +1 -1
- package/dist/src/tools/rule-generation-tool.d.ts.map +1 -1
- package/dist/src/tools/rule-generation-tool.js +328 -143
- package/dist/src/tools/rule-generation-tool.js.map +1 -1
- package/dist/src/types/ape-framework.d.ts +566 -0
- package/dist/src/types/ape-framework.d.ts.map +1 -0
- package/dist/src/types/ape-framework.js +98 -0
- package/dist/src/types/ape-framework.js.map +1 -0
- package/dist/src/types/index.d.ts +96 -93
- package/dist/src/types/index.d.ts.map +1 -1
- package/dist/src/types/knowledge-generation.d.ts +671 -0
- package/dist/src/types/knowledge-generation.d.ts.map +1 -0
- package/dist/src/types/knowledge-generation.js +107 -0
- package/dist/src/types/knowledge-generation.js.map +1 -0
- package/dist/src/types/reflexion-framework.d.ts +652 -0
- package/dist/src/types/reflexion-framework.d.ts.map +1 -0
- package/dist/src/types/reflexion-framework.js +85 -0
- package/dist/src/types/reflexion-framework.js.map +1 -0
- package/dist/src/utils/adr-suggestions.d.ts +1 -1
- package/dist/src/utils/adr-suggestions.d.ts.map +1 -1
- package/dist/src/utils/adr-suggestions.js +8 -50
- package/dist/src/utils/adr-suggestions.js.map +1 -1
- package/dist/src/utils/ai-executor.d.ts +125 -0
- package/dist/src/utils/ai-executor.d.ts.map +1 -0
- package/dist/src/utils/ai-executor.js +292 -0
- package/dist/src/utils/ai-executor.js.map +1 -0
- package/dist/src/utils/automatic-prompt-engineering.d.ts +72 -0
- package/dist/src/utils/automatic-prompt-engineering.d.ts.map +1 -0
- package/dist/src/utils/automatic-prompt-engineering.js +820 -0
- package/dist/src/utils/automatic-prompt-engineering.js.map +1 -0
- package/dist/src/utils/cache.d.ts +54 -22
- package/dist/src/utils/cache.d.ts.map +1 -1
- package/dist/src/utils/cache.js +1234 -143
- package/dist/src/utils/cache.js.map +1 -1
- package/dist/src/utils/config.d.ts +2 -2
- package/dist/src/utils/config.js +3 -3
- package/dist/src/utils/config.js.map +1 -1
- package/dist/src/utils/deployment-analysis.d.ts.map +1 -1
- package/dist/src/utils/deployment-analysis.js +57 -38
- package/dist/src/utils/deployment-analysis.js.map +1 -1
- package/dist/src/utils/environment-analysis.d.ts.map +1 -1
- package/dist/src/utils/environment-analysis.js +113 -100
- package/dist/src/utils/environment-analysis.js.map +1 -1
- package/dist/src/utils/file-system.d.ts +42 -13
- package/dist/src/utils/file-system.d.ts.map +1 -1
- package/dist/src/utils/file-system.js +795 -89
- package/dist/src/utils/file-system.js.map +1 -1
- package/dist/src/utils/index.d.ts +9 -0
- package/dist/src/utils/index.d.ts.map +1 -0
- package/dist/src/utils/index.js +13 -0
- package/dist/src/utils/index.js.map +1 -0
- package/dist/src/utils/knowledge-generation.d.ts +79 -0
- package/dist/src/utils/knowledge-generation.d.ts.map +1 -0
- package/dist/src/utils/knowledge-generation.js +646 -0
- package/dist/src/utils/knowledge-generation.js.map +1 -0
- package/dist/src/utils/output-masking.d.ts.map +1 -1
- package/dist/src/utils/output-masking.js +2 -1
- package/dist/src/utils/output-masking.js.map +1 -1
- package/dist/src/utils/prompt-composition.d.ts +234 -0
- package/dist/src/utils/prompt-composition.d.ts.map +1 -0
- package/dist/src/utils/prompt-composition.js +379 -0
- package/dist/src/utils/prompt-composition.js.map +1 -0
- package/dist/src/utils/prompt-execution.d.ts +88 -0
- package/dist/src/utils/prompt-execution.d.ts.map +1 -0
- package/dist/src/utils/prompt-execution.js +227 -0
- package/dist/src/utils/prompt-execution.js.map +1 -0
- package/dist/src/utils/reflexion.d.ts +92 -0
- package/dist/src/utils/reflexion.d.ts.map +1 -0
- package/dist/src/utils/reflexion.js +1100 -0
- package/dist/src/utils/reflexion.js.map +1 -0
- package/dist/src/utils/research-integration.d.ts.map +1 -1
- package/dist/src/utils/research-integration.js +156 -100
- package/dist/src/utils/research-integration.js.map +1 -1
- package/dist/src/utils/research-questions.d.ts.map +1 -1
- package/dist/src/utils/research-questions.js +85 -24
- package/dist/src/utils/research-questions.js.map +1 -1
- package/dist/src/utils/rule-generation.d.ts.map +1 -1
- package/dist/src/utils/rule-generation.js +92 -108
- package/dist/src/utils/rule-generation.js.map +1 -1
- package/package.json +9 -3
package/dist/src/utils/cache.js
CHANGED
|
@@ -1,33 +1,136 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Caching system for MCP resources and expensive operations
|
|
3
|
-
* Implements
|
|
3
|
+
* Implements prompt-driven caching with TTL and invalidation through AI delegation
|
|
4
4
|
*/
|
|
5
|
-
import { promises as fs } from 'fs';
|
|
6
|
-
import { join } from 'path';
|
|
7
5
|
import { McpAdrError } from '../types/index.js';
|
|
8
6
|
const CACHE_DIR = '.mcp-adr-cache';
|
|
9
7
|
/**
|
|
10
|
-
*
|
|
8
|
+
* Generate prompt for AI-driven cache directory initialization
|
|
11
9
|
*/
|
|
12
10
|
export async function initializeCache() {
|
|
13
11
|
try {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
12
|
+
console.error(`[DEBUG] Generating cache initialization prompt for: ${CACHE_DIR}`);
|
|
13
|
+
const prompt = `
|
|
14
|
+
# Cache Directory Initialization Request
|
|
15
|
+
|
|
16
|
+
Please initialize the cache directory system for MCP ADR Analysis Server.
|
|
17
|
+
|
|
18
|
+
## Cache Directory Setup
|
|
19
|
+
|
|
20
|
+
1. **Create Cache Directory**: ${CACHE_DIR}
|
|
21
|
+
- Create directory with recursive option (create parent directories if needed)
|
|
22
|
+
- Set appropriate permissions (755 or system default)
|
|
23
|
+
|
|
24
|
+
2. **Create Metadata File**: ${CACHE_DIR}/metadata.json
|
|
25
|
+
- Check if metadata.json already exists
|
|
26
|
+
- If it doesn't exist, create it with the following content:
|
|
27
|
+
|
|
28
|
+
\`\`\`json
|
|
29
|
+
{
|
|
30
|
+
"version": "1.0.0",
|
|
31
|
+
"created": "${new Date().toISOString()}",
|
|
32
|
+
"lastCleanup": "${new Date().toISOString()}"
|
|
33
|
+
}
|
|
34
|
+
\`\`\`
|
|
35
|
+
|
|
36
|
+
## Security Validation
|
|
37
|
+
|
|
38
|
+
The AI agent must:
|
|
39
|
+
1. **Validate cache directory path**: Ensure ${CACHE_DIR} is safe and within project scope
|
|
40
|
+
2. **Prevent system directory access**: Reject attempts to create cache in system directories
|
|
41
|
+
3. **Handle permissions safely**: Set appropriate directory and file permissions
|
|
42
|
+
4. **Validate metadata content**: Ensure metadata file contains valid JSON
|
|
43
|
+
|
|
44
|
+
## Expected Output Format
|
|
45
|
+
|
|
46
|
+
\`\`\`json
|
|
47
|
+
{
|
|
48
|
+
"success": true,
|
|
49
|
+
"cacheDirectory": "${CACHE_DIR}",
|
|
50
|
+
"metadataFile": "${CACHE_DIR}/metadata.json",
|
|
51
|
+
"operations": {
|
|
52
|
+
"directoryCreated": boolean,
|
|
53
|
+
"metadataCreated": boolean,
|
|
54
|
+
"metadataExists": boolean
|
|
55
|
+
},
|
|
56
|
+
"metadata": {
|
|
57
|
+
"version": "1.0.0",
|
|
58
|
+
"created": "ISO_timestamp",
|
|
59
|
+
"lastCleanup": "ISO_timestamp"
|
|
60
|
+
},
|
|
61
|
+
"security": {
|
|
62
|
+
"pathValidated": true,
|
|
63
|
+
"permissionsSet": true,
|
|
64
|
+
"systemDirectoryCheck": "passed"
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
\`\`\`
|
|
68
|
+
|
|
69
|
+
## Error Response Format
|
|
70
|
+
|
|
71
|
+
\`\`\`json
|
|
72
|
+
{
|
|
73
|
+
"success": false,
|
|
74
|
+
"cacheDirectory": "${CACHE_DIR}",
|
|
75
|
+
"error": "detailed error message",
|
|
76
|
+
"errorType": "PERMISSION_DENIED|INVALID_PATH|SECURITY_VIOLATION|METADATA_ERROR",
|
|
77
|
+
"security": {
|
|
78
|
+
"pathValidated": boolean,
|
|
79
|
+
"systemDirectoryCheck": "passed|failed",
|
|
80
|
+
"rejectionReason": "specific reason if initialization failed"
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
\`\`\`
|
|
84
|
+
`;
|
|
85
|
+
const instructions = `
|
|
86
|
+
## Implementation Steps
|
|
87
|
+
|
|
88
|
+
1. **Security Validation**:
|
|
89
|
+
- Validate cache directory path: ${CACHE_DIR}
|
|
90
|
+
- Ensure it's within project scope and not a system directory
|
|
91
|
+
- Check for path traversal attempts
|
|
92
|
+
|
|
93
|
+
2. **Directory Creation**:
|
|
94
|
+
- Create cache directory: ${CACHE_DIR}
|
|
95
|
+
- Use recursive creation (mkdir -p equivalent)
|
|
96
|
+
- Set appropriate permissions (755)
|
|
97
|
+
|
|
98
|
+
3. **Metadata File Management**:
|
|
99
|
+
- Check if ${CACHE_DIR}/metadata.json exists
|
|
100
|
+
- If not exists, create with initial metadata
|
|
101
|
+
- Validate JSON structure and content
|
|
102
|
+
|
|
103
|
+
4. **Response Generation**:
|
|
104
|
+
- Format response according to specified JSON structure
|
|
105
|
+
- Include operation results and security validation
|
|
106
|
+
- Provide detailed error information if initialization fails
|
|
107
|
+
|
|
108
|
+
## Safety Checks
|
|
109
|
+
|
|
110
|
+
- ✅ Path security validation delegated to AI agent
|
|
111
|
+
- ✅ System directory protection enabled
|
|
112
|
+
- ✅ Permission handling with security validation
|
|
113
|
+
- ✅ Metadata file validation and creation
|
|
114
|
+
|
|
115
|
+
## Context Information
|
|
116
|
+
|
|
117
|
+
- **Cache Directory**: ${CACHE_DIR}
|
|
118
|
+
- **Operation**: Cache system initialization
|
|
119
|
+
- **Security Level**: High (with path validation and permission control)
|
|
120
|
+
`;
|
|
121
|
+
return {
|
|
122
|
+
prompt,
|
|
123
|
+
instructions,
|
|
124
|
+
context: {
|
|
125
|
+
cacheDirectory: CACHE_DIR,
|
|
126
|
+
operation: 'cache_initialization',
|
|
127
|
+
securityLevel: 'high',
|
|
128
|
+
expectedFormat: 'json'
|
|
129
|
+
}
|
|
130
|
+
};
|
|
28
131
|
}
|
|
29
132
|
catch (error) {
|
|
30
|
-
throw new McpAdrError(`Failed to
|
|
133
|
+
throw new McpAdrError(`Failed to generate cache initialization prompt: ${error instanceof Error ? error.message : String(error)}`, 'CACHE_ERROR');
|
|
31
134
|
}
|
|
32
135
|
}
|
|
33
136
|
/**
|
|
@@ -36,14 +139,16 @@ export async function initializeCache() {
|
|
|
36
139
|
function getCacheFilePath(key) {
|
|
37
140
|
// Create safe filename from cache key
|
|
38
141
|
const safeKey = key.replace(/[^a-zA-Z0-9-_]/g, '_');
|
|
39
|
-
return
|
|
142
|
+
return `${CACHE_DIR}/${safeKey}.json`;
|
|
40
143
|
}
|
|
41
144
|
/**
|
|
42
|
-
*
|
|
145
|
+
* Generate prompt for AI-driven cache data storage
|
|
43
146
|
*/
|
|
44
147
|
export async function setCache(key, data, options = {}) {
|
|
45
148
|
try {
|
|
46
|
-
|
|
149
|
+
console.error(`[DEBUG] Generating cache set prompt for key: ${key}`);
|
|
150
|
+
// First ensure cache is initialized
|
|
151
|
+
const initPrompt = await initializeCache();
|
|
47
152
|
const cacheEntry = {
|
|
48
153
|
key,
|
|
49
154
|
data,
|
|
@@ -55,189 +160,1175 @@ export async function setCache(key, data, options = {}) {
|
|
|
55
160
|
},
|
|
56
161
|
};
|
|
57
162
|
const filePath = getCacheFilePath(key);
|
|
58
|
-
|
|
163
|
+
const cacheContent = JSON.stringify(cacheEntry, null, 2);
|
|
164
|
+
const prompt = `
|
|
165
|
+
# Cache Data Storage Request
|
|
166
|
+
|
|
167
|
+
Please store cache data for the MCP ADR Analysis Server.
|
|
168
|
+
|
|
169
|
+
## Step 1: Cache Initialization
|
|
170
|
+
${initPrompt.prompt}
|
|
171
|
+
|
|
172
|
+
**Instructions**: First, execute the cache initialization above to ensure the cache directory exists.
|
|
173
|
+
|
|
174
|
+
## Step 2: Cache Data Storage
|
|
175
|
+
|
|
176
|
+
Store the following cache entry:
|
|
177
|
+
|
|
178
|
+
**Cache Key**: ${key}
|
|
179
|
+
**File Path**: ${filePath}
|
|
180
|
+
**TTL**: ${cacheEntry.ttl} seconds
|
|
181
|
+
**Data Size**: ${JSON.stringify(data).length} characters
|
|
182
|
+
|
|
183
|
+
### Cache Entry Content
|
|
184
|
+
\`\`\`json
|
|
185
|
+
${cacheContent}
|
|
186
|
+
\`\`\`
|
|
187
|
+
|
|
188
|
+
## Security Validation
|
|
189
|
+
|
|
190
|
+
The AI agent must:
|
|
191
|
+
1. **Validate cache file path**: Ensure ${filePath} is within cache directory scope
|
|
192
|
+
2. **Prevent path traversal**: Reject attempts to write outside cache directory
|
|
193
|
+
3. **Validate cache key**: Ensure key contains only safe characters
|
|
194
|
+
4. **Content validation**: Verify cache entry structure and JSON validity
|
|
195
|
+
|
|
196
|
+
## Expected Output Format
|
|
197
|
+
|
|
198
|
+
\`\`\`json
|
|
199
|
+
{
|
|
200
|
+
"success": true,
|
|
201
|
+
"cacheKey": "${key}",
|
|
202
|
+
"filePath": "${filePath}",
|
|
203
|
+
"operations": {
|
|
204
|
+
"cacheInitialized": boolean,
|
|
205
|
+
"fileWritten": true,
|
|
206
|
+
"dataSize": ${JSON.stringify(data).length},
|
|
207
|
+
"ttl": ${cacheEntry.ttl}
|
|
208
|
+
},
|
|
209
|
+
"cacheEntry": {
|
|
210
|
+
"key": "${key}",
|
|
211
|
+
"timestamp": "${cacheEntry.timestamp}",
|
|
212
|
+
"ttl": ${cacheEntry.ttl},
|
|
213
|
+
"compressed": ${cacheEntry.metadata?.['compressed'] || false}
|
|
214
|
+
},
|
|
215
|
+
"security": {
|
|
216
|
+
"pathValidated": true,
|
|
217
|
+
"keyValidated": true,
|
|
218
|
+
"contentValidated": true,
|
|
219
|
+
"systemDirectoryCheck": "passed"
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
\`\`\`
|
|
223
|
+
|
|
224
|
+
## Error Response Format
|
|
225
|
+
|
|
226
|
+
\`\`\`json
|
|
227
|
+
{
|
|
228
|
+
"success": false,
|
|
229
|
+
"cacheKey": "${key}",
|
|
230
|
+
"filePath": "${filePath}",
|
|
231
|
+
"error": "detailed error message",
|
|
232
|
+
"errorType": "PERMISSION_DENIED|INVALID_PATH|SECURITY_VIOLATION|CACHE_ERROR|JSON_ERROR",
|
|
233
|
+
"security": {
|
|
234
|
+
"pathValidated": boolean,
|
|
235
|
+
"keyValidated": boolean,
|
|
236
|
+
"contentValidated": boolean,
|
|
237
|
+
"systemDirectoryCheck": "passed|failed",
|
|
238
|
+
"rejectionReason": "specific reason if storage failed"
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
\`\`\`
|
|
242
|
+
`;
|
|
243
|
+
const instructions = `
|
|
244
|
+
## Implementation Steps
|
|
245
|
+
|
|
246
|
+
1. **Cache Initialization**:
|
|
247
|
+
- Execute cache directory initialization from Step 1
|
|
248
|
+
- Ensure cache directory exists and is accessible
|
|
249
|
+
|
|
250
|
+
2. **Security Validation**:
|
|
251
|
+
- Validate cache key: ${key}
|
|
252
|
+
- Validate file path: ${filePath}
|
|
253
|
+
- Ensure path is within cache directory scope
|
|
254
|
+
- Check for path traversal attempts
|
|
255
|
+
|
|
256
|
+
3. **Cache Entry Storage**:
|
|
257
|
+
- Write cache entry to file: ${filePath}
|
|
258
|
+
- Use UTF-8 encoding for JSON content
|
|
259
|
+
- Set appropriate file permissions (644)
|
|
260
|
+
- Ensure atomic write operations
|
|
261
|
+
|
|
262
|
+
4. **Response Generation**:
|
|
263
|
+
- Format response according to specified JSON structure
|
|
264
|
+
- Include operation results and cache metadata
|
|
265
|
+
- Provide detailed error information if storage fails
|
|
266
|
+
|
|
267
|
+
## Safety Checks
|
|
268
|
+
|
|
269
|
+
- ✅ Path security validation delegated to AI agent
|
|
270
|
+
- ✅ Cache directory initialization required
|
|
271
|
+
- ✅ File path validation and permission handling
|
|
272
|
+
- ✅ JSON content validation and safe storage
|
|
273
|
+
|
|
274
|
+
## Context Information
|
|
275
|
+
|
|
276
|
+
- **Cache Key**: ${key}
|
|
277
|
+
- **File Path**: ${filePath}
|
|
278
|
+
- **Data Size**: ${JSON.stringify(data).length} characters
|
|
279
|
+
- **TTL**: ${cacheEntry.ttl} seconds
|
|
280
|
+
- **Operation**: Cache data storage
|
|
281
|
+
- **Security Level**: High (with path and content validation)
|
|
282
|
+
`;
|
|
283
|
+
return {
|
|
284
|
+
prompt,
|
|
285
|
+
instructions,
|
|
286
|
+
context: {
|
|
287
|
+
cacheKey: key,
|
|
288
|
+
filePath,
|
|
289
|
+
dataSize: JSON.stringify(data).length,
|
|
290
|
+
ttl: cacheEntry.ttl,
|
|
291
|
+
operation: 'cache_set',
|
|
292
|
+
securityLevel: 'high',
|
|
293
|
+
expectedFormat: 'json'
|
|
294
|
+
}
|
|
295
|
+
};
|
|
59
296
|
}
|
|
60
297
|
catch (error) {
|
|
61
|
-
throw new McpAdrError(`Failed to set
|
|
298
|
+
throw new McpAdrError(`Failed to generate cache set prompt: ${error instanceof Error ? error.message : String(error)}`, 'CACHE_ERROR');
|
|
62
299
|
}
|
|
63
300
|
}
|
|
64
301
|
/**
|
|
65
|
-
*
|
|
302
|
+
* Generate prompt for AI-driven cache data retrieval
|
|
66
303
|
*/
|
|
67
304
|
export async function getCache(key) {
|
|
68
305
|
try {
|
|
306
|
+
console.error(`[DEBUG] Generating cache get prompt for key: ${key}`);
|
|
69
307
|
const filePath = getCacheFilePath(key);
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
308
|
+
const currentTime = new Date().toISOString();
|
|
309
|
+
const prompt = `
|
|
310
|
+
# Cache Data Retrieval Request
|
|
311
|
+
|
|
312
|
+
Please retrieve and validate cache data for the MCP ADR Analysis Server.
|
|
313
|
+
|
|
314
|
+
## Cache Retrieval Operation
|
|
315
|
+
|
|
316
|
+
**Cache Key**: ${key}
|
|
317
|
+
**File Path**: ${filePath}
|
|
318
|
+
**Current Time**: ${currentTime}
|
|
319
|
+
|
|
320
|
+
### Required Operations
|
|
321
|
+
|
|
322
|
+
1. **File Existence Check**:
|
|
323
|
+
- Check if cache file exists: ${filePath}
|
|
324
|
+
- If file doesn't exist, return cache miss result
|
|
325
|
+
|
|
326
|
+
2. **Cache Content Reading**:
|
|
327
|
+
- Read cache file content from: ${filePath}
|
|
328
|
+
- Parse JSON content to extract cache entry
|
|
329
|
+
- Validate cache entry structure
|
|
330
|
+
|
|
331
|
+
3. **TTL Validation**:
|
|
332
|
+
- Extract timestamp and TTL from cache entry
|
|
333
|
+
- Calculate cache age: current_time - cache_timestamp
|
|
334
|
+
- Compare age with TTL to determine if cache is expired
|
|
335
|
+
|
|
336
|
+
4. **Cache Cleanup** (if expired):
|
|
337
|
+
- If cache is expired, delete the cache file
|
|
338
|
+
- Return cache miss result
|
|
339
|
+
|
|
340
|
+
## Security Validation
|
|
341
|
+
|
|
342
|
+
The AI agent must:
|
|
343
|
+
1. **Validate cache file path**: Ensure ${filePath} is within cache directory scope
|
|
344
|
+
2. **Prevent path traversal**: Reject attempts to read outside cache directory
|
|
345
|
+
3. **Validate cache key**: Ensure key contains only safe characters
|
|
346
|
+
4. **Content validation**: Verify cache entry JSON structure and integrity
|
|
347
|
+
|
|
348
|
+
## Expected Output Format (Cache Hit)
|
|
349
|
+
|
|
350
|
+
\`\`\`json
|
|
351
|
+
{
|
|
352
|
+
"success": true,
|
|
353
|
+
"cacheKey": "${key}",
|
|
354
|
+
"filePath": "${filePath}",
|
|
355
|
+
"cacheHit": true,
|
|
356
|
+
"data": "[extracted cache data]",
|
|
357
|
+
"cacheInfo": {
|
|
358
|
+
"timestamp": "cache_entry_timestamp",
|
|
359
|
+
"ttl": "cache_entry_ttl",
|
|
360
|
+
"ageInSeconds": "calculated_age",
|
|
361
|
+
"expired": false
|
|
362
|
+
},
|
|
363
|
+
"security": {
|
|
364
|
+
"pathValidated": true,
|
|
365
|
+
"keyValidated": true,
|
|
366
|
+
"contentValidated": true,
|
|
367
|
+
"systemDirectoryCheck": "passed"
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
\`\`\`
|
|
371
|
+
|
|
372
|
+
## Expected Output Format (Cache Miss)
|
|
373
|
+
|
|
374
|
+
\`\`\`json
|
|
375
|
+
{
|
|
376
|
+
"success": true,
|
|
377
|
+
"cacheKey": "${key}",
|
|
378
|
+
"filePath": "${filePath}",
|
|
379
|
+
"cacheHit": false,
|
|
380
|
+
"data": null,
|
|
381
|
+
"reason": "FILE_NOT_FOUND|CACHE_EXPIRED|CORRUPTED_DATA",
|
|
382
|
+
"cacheInfo": {
|
|
383
|
+
"fileExists": boolean,
|
|
384
|
+
"expired": boolean,
|
|
385
|
+
"cleanupPerformed": boolean
|
|
386
|
+
},
|
|
387
|
+
"security": {
|
|
388
|
+
"pathValidated": true,
|
|
389
|
+
"keyValidated": true,
|
|
390
|
+
"systemDirectoryCheck": "passed"
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
\`\`\`
|
|
394
|
+
|
|
395
|
+
## Error Response Format
|
|
396
|
+
|
|
397
|
+
\`\`\`json
|
|
398
|
+
{
|
|
399
|
+
"success": false,
|
|
400
|
+
"cacheKey": "${key}",
|
|
401
|
+
"filePath": "${filePath}",
|
|
402
|
+
"error": "detailed error message",
|
|
403
|
+
"errorType": "PERMISSION_DENIED|INVALID_PATH|SECURITY_VIOLATION|JSON_ERROR|READ_ERROR",
|
|
404
|
+
"security": {
|
|
405
|
+
"pathValidated": boolean,
|
|
406
|
+
"keyValidated": boolean,
|
|
407
|
+
"systemDirectoryCheck": "passed|failed",
|
|
408
|
+
"rejectionReason": "specific reason if retrieval failed"
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
\`\`\`
|
|
412
|
+
`;
|
|
413
|
+
const instructions = `
|
|
414
|
+
## Implementation Steps
|
|
415
|
+
|
|
416
|
+
1. **Security Validation**:
|
|
417
|
+
- Validate cache key: ${key}
|
|
418
|
+
- Validate file path: ${filePath}
|
|
419
|
+
- Ensure path is within cache directory scope
|
|
420
|
+
- Check for path traversal attempts
|
|
421
|
+
|
|
422
|
+
2. **File Operations**:
|
|
423
|
+
- Check if cache file exists: ${filePath}
|
|
424
|
+
- If exists, read file content with UTF-8 encoding
|
|
425
|
+
- Parse JSON content to extract cache entry
|
|
426
|
+
|
|
427
|
+
3. **TTL Validation**:
|
|
428
|
+
- Extract timestamp and TTL from cache entry
|
|
429
|
+
- Calculate cache age using current time: ${currentTime}
|
|
430
|
+
- Determine if cache is expired (age > TTL)
|
|
431
|
+
|
|
432
|
+
4. **Cache Cleanup** (if needed):
|
|
433
|
+
- If cache is expired, delete the cache file
|
|
434
|
+
- Ensure safe file deletion with proper error handling
|
|
435
|
+
|
|
436
|
+
5. **Response Generation**:
|
|
437
|
+
- Format response according to specified JSON structure
|
|
438
|
+
- Include cache data if valid, null if miss/expired
|
|
439
|
+
- Provide detailed cache information and security validation
|
|
440
|
+
|
|
441
|
+
## Safety Checks
|
|
442
|
+
|
|
443
|
+
- ✅ Path security validation delegated to AI agent
|
|
444
|
+
- ✅ Cache key validation and sanitization
|
|
445
|
+
- ✅ File existence and permission checking
|
|
446
|
+
- ✅ JSON parsing with error handling
|
|
447
|
+
- ✅ TTL validation and automatic cleanup
|
|
448
|
+
|
|
449
|
+
## Context Information
|
|
450
|
+
|
|
451
|
+
- **Cache Key**: ${key}
|
|
452
|
+
- **File Path**: ${filePath}
|
|
453
|
+
- **Current Time**: ${currentTime}
|
|
454
|
+
- **Operation**: Cache data retrieval with TTL validation
|
|
455
|
+
- **Security Level**: High (with path and content validation)
|
|
456
|
+
`;
|
|
457
|
+
return {
|
|
458
|
+
prompt,
|
|
459
|
+
instructions,
|
|
460
|
+
context: {
|
|
461
|
+
cacheKey: key,
|
|
462
|
+
filePath,
|
|
463
|
+
currentTime,
|
|
464
|
+
operation: 'cache_get',
|
|
465
|
+
securityLevel: 'high',
|
|
466
|
+
expectedFormat: 'json'
|
|
81
467
|
}
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
catch (error) {
|
|
85
|
-
// Cache file doesn't exist or is corrupted
|
|
86
|
-
return null;
|
|
87
|
-
}
|
|
468
|
+
};
|
|
88
469
|
}
|
|
89
470
|
catch (error) {
|
|
90
|
-
throw new McpAdrError(`Failed to get
|
|
471
|
+
throw new McpAdrError(`Failed to generate cache get prompt: ${error instanceof Error ? error.message : String(error)}`, 'CACHE_ERROR');
|
|
91
472
|
}
|
|
92
473
|
}
|
|
93
474
|
/**
|
|
94
|
-
*
|
|
475
|
+
* Generate prompt for AI-driven cache validity check
|
|
95
476
|
*/
|
|
96
477
|
export async function hasValidCache(key) {
|
|
97
478
|
try {
|
|
98
|
-
|
|
99
|
-
|
|
479
|
+
console.error(`[DEBUG] Generating cache validity check prompt for key: ${key}`);
|
|
480
|
+
// Use the getCache prompt to check validity
|
|
481
|
+
const getCachePrompt = await getCache(key);
|
|
482
|
+
const prompt = `
|
|
483
|
+
# Cache Validity Check Request
|
|
484
|
+
|
|
485
|
+
Please check if a cache entry exists and is valid for the MCP ADR Analysis Server.
|
|
486
|
+
|
|
487
|
+
## Cache Validity Operation
|
|
488
|
+
|
|
489
|
+
This is a simplified cache check that determines if a cache entry exists and is not expired.
|
|
490
|
+
|
|
491
|
+
${getCachePrompt.prompt}
|
|
492
|
+
|
|
493
|
+
## Expected Output Format (Valid Cache)
|
|
494
|
+
|
|
495
|
+
\`\`\`json
|
|
496
|
+
{
|
|
497
|
+
"success": true,
|
|
498
|
+
"cacheKey": "${key}",
|
|
499
|
+
"isValid": true,
|
|
500
|
+
"cacheHit": true,
|
|
501
|
+
"reason": "CACHE_VALID"
|
|
502
|
+
}
|
|
503
|
+
\`\`\`
|
|
504
|
+
|
|
505
|
+
## Expected Output Format (Invalid/Missing Cache)
|
|
506
|
+
|
|
507
|
+
\`\`\`json
|
|
508
|
+
{
|
|
509
|
+
"success": true,
|
|
510
|
+
"cacheKey": "${key}",
|
|
511
|
+
"isValid": false,
|
|
512
|
+
"cacheHit": false,
|
|
513
|
+
"reason": "FILE_NOT_FOUND|CACHE_EXPIRED|CORRUPTED_DATA"
|
|
514
|
+
}
|
|
515
|
+
\`\`\`
|
|
516
|
+
|
|
517
|
+
**Note**: This operation should return isValid: true only if the cache exists AND is not expired.
|
|
518
|
+
`;
|
|
519
|
+
const instructions = `
|
|
520
|
+
## Implementation Steps
|
|
521
|
+
|
|
522
|
+
1. **Execute Cache Retrieval**:
|
|
523
|
+
- Use the cache retrieval logic from the embedded prompt
|
|
524
|
+
- Check if cache file exists and is readable
|
|
525
|
+
- Validate TTL and expiration status
|
|
526
|
+
|
|
527
|
+
2. **Validity Determination**:
|
|
528
|
+
- Return isValid: true only if cache exists and is not expired
|
|
529
|
+
- Return isValid: false for any other condition (missing, expired, corrupted)
|
|
530
|
+
|
|
531
|
+
3. **Response Generation**:
|
|
532
|
+
- Format response according to specified JSON structure
|
|
533
|
+
- Include validity status and reason
|
|
534
|
+
|
|
535
|
+
## Context Information
|
|
536
|
+
|
|
537
|
+
- **Cache Key**: ${key}
|
|
538
|
+
- **Operation**: Cache validity check
|
|
539
|
+
- **Security Level**: High (inherited from cache retrieval)
|
|
540
|
+
`;
|
|
541
|
+
return {
|
|
542
|
+
prompt,
|
|
543
|
+
instructions,
|
|
544
|
+
context: {
|
|
545
|
+
cacheKey: key,
|
|
546
|
+
operation: 'cache_validity_check',
|
|
547
|
+
securityLevel: 'high',
|
|
548
|
+
expectedFormat: 'json'
|
|
549
|
+
}
|
|
550
|
+
};
|
|
100
551
|
}
|
|
101
|
-
catch {
|
|
102
|
-
|
|
552
|
+
catch (error) {
|
|
553
|
+
throw new McpAdrError(`Failed to generate cache validity check prompt: ${error instanceof Error ? error.message : String(error)}`, 'CACHE_ERROR');
|
|
103
554
|
}
|
|
104
555
|
}
|
|
105
556
|
/**
|
|
106
|
-
*
|
|
557
|
+
* Generate prompt for AI-driven cache entry invalidation
|
|
107
558
|
*/
|
|
108
559
|
export async function invalidateCache(key) {
|
|
109
560
|
try {
|
|
561
|
+
console.error(`[DEBUG] Generating cache invalidation prompt for key: ${key}`);
|
|
110
562
|
const filePath = getCacheFilePath(key);
|
|
111
|
-
|
|
563
|
+
const prompt = `
|
|
564
|
+
# Cache Entry Invalidation Request
|
|
565
|
+
|
|
566
|
+
Please invalidate (delete) a specific cache entry for the MCP ADR Analysis Server.
|
|
567
|
+
|
|
568
|
+
## Cache Invalidation Operation
|
|
569
|
+
|
|
570
|
+
**Cache Key**: ${key}
|
|
571
|
+
**File Path**: ${filePath}
|
|
572
|
+
|
|
573
|
+
### Required Operations
|
|
574
|
+
|
|
575
|
+
1. **File Existence Check**:
|
|
576
|
+
- Check if cache file exists: ${filePath}
|
|
577
|
+
- If file doesn't exist, consider operation successful (already invalidated)
|
|
578
|
+
|
|
579
|
+
2. **Cache File Deletion**:
|
|
580
|
+
- Delete the cache file: ${filePath}
|
|
581
|
+
- Handle file deletion safely with proper error handling
|
|
582
|
+
- Ensure file is completely removed from filesystem
|
|
583
|
+
|
|
584
|
+
## Security Validation
|
|
585
|
+
|
|
586
|
+
The AI agent must:
|
|
587
|
+
1. **Validate cache file path**: Ensure ${filePath} is within cache directory scope
|
|
588
|
+
2. **Prevent path traversal**: Reject attempts to delete files outside cache directory
|
|
589
|
+
3. **Validate cache key**: Ensure key contains only safe characters
|
|
590
|
+
4. **Safe deletion**: Ensure only cache files are deleted, not system files
|
|
591
|
+
|
|
592
|
+
## Expected Output Format (Successful Invalidation)
|
|
593
|
+
|
|
594
|
+
\`\`\`json
|
|
595
|
+
{
|
|
596
|
+
"success": true,
|
|
597
|
+
"cacheKey": "${key}",
|
|
598
|
+
"filePath": "${filePath}",
|
|
599
|
+
"operations": {
|
|
600
|
+
"fileExists": boolean,
|
|
601
|
+
"fileDeleted": boolean,
|
|
602
|
+
"alreadyInvalidated": boolean
|
|
603
|
+
},
|
|
604
|
+
"security": {
|
|
605
|
+
"pathValidated": true,
|
|
606
|
+
"keyValidated": true,
|
|
607
|
+
"systemDirectoryCheck": "passed"
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
\`\`\`
|
|
611
|
+
|
|
612
|
+
## Error Response Format
|
|
613
|
+
|
|
614
|
+
\`\`\`json
|
|
615
|
+
{
|
|
616
|
+
"success": false,
|
|
617
|
+
"cacheKey": "${key}",
|
|
618
|
+
"filePath": "${filePath}",
|
|
619
|
+
"error": "detailed error message",
|
|
620
|
+
"errorType": "PERMISSION_DENIED|INVALID_PATH|SECURITY_VIOLATION|DELETE_ERROR",
|
|
621
|
+
"security": {
|
|
622
|
+
"pathValidated": boolean,
|
|
623
|
+
"keyValidated": boolean,
|
|
624
|
+
"systemDirectoryCheck": "passed|failed",
|
|
625
|
+
"rejectionReason": "specific reason if invalidation failed"
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
\`\`\`
|
|
629
|
+
`;
|
|
630
|
+
const instructions = `
|
|
631
|
+
## Implementation Steps
|
|
632
|
+
|
|
633
|
+
1. **Security Validation**:
|
|
634
|
+
- Validate cache key: ${key}
|
|
635
|
+
- Validate file path: ${filePath}
|
|
636
|
+
- Ensure path is within cache directory scope
|
|
637
|
+
- Check for path traversal attempts
|
|
638
|
+
|
|
639
|
+
2. **File Operations**:
|
|
640
|
+
- Check if cache file exists: ${filePath}
|
|
641
|
+
- If exists, delete the file safely
|
|
642
|
+
- If doesn't exist, consider operation successful
|
|
643
|
+
|
|
644
|
+
3. **Response Generation**:
|
|
645
|
+
- Format response according to specified JSON structure
|
|
646
|
+
- Include operation results and security validation
|
|
647
|
+
- Handle both success and error cases gracefully
|
|
648
|
+
|
|
649
|
+
## Safety Checks
|
|
650
|
+
|
|
651
|
+
- ✅ Path security validation delegated to AI agent
|
|
652
|
+
- ✅ Cache key validation and sanitization
|
|
653
|
+
- ✅ Safe file deletion with error handling
|
|
654
|
+
- ✅ Prevention of system file deletion
|
|
655
|
+
|
|
656
|
+
## Context Information
|
|
657
|
+
|
|
658
|
+
- **Cache Key**: ${key}
|
|
659
|
+
- **File Path**: ${filePath}
|
|
660
|
+
- **Operation**: Cache entry invalidation (deletion)
|
|
661
|
+
- **Security Level**: High (with path and operation validation)
|
|
662
|
+
`;
|
|
663
|
+
return {
|
|
664
|
+
prompt,
|
|
665
|
+
instructions,
|
|
666
|
+
context: {
|
|
667
|
+
cacheKey: key,
|
|
668
|
+
filePath,
|
|
669
|
+
operation: 'cache_invalidate',
|
|
670
|
+
securityLevel: 'high',
|
|
671
|
+
expectedFormat: 'json'
|
|
672
|
+
}
|
|
673
|
+
};
|
|
112
674
|
}
|
|
113
|
-
catch {
|
|
114
|
-
|
|
675
|
+
catch (error) {
|
|
676
|
+
throw new McpAdrError(`Failed to generate cache invalidation prompt: ${error instanceof Error ? error.message : String(error)}`, 'CACHE_ERROR');
|
|
115
677
|
}
|
|
116
678
|
}
|
|
117
679
|
/**
|
|
118
|
-
*
|
|
680
|
+
* Generate prompt for AI-driven cache clearing
|
|
119
681
|
*/
|
|
120
682
|
export async function clearCache() {
|
|
121
683
|
try {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
684
|
+
console.error(`[DEBUG] Generating cache clear prompt for directory: ${CACHE_DIR}`);
|
|
685
|
+
const prompt = `
|
|
686
|
+
# Cache Clearing Request
|
|
687
|
+
|
|
688
|
+
Please clear all cache entries for the MCP ADR Analysis Server.
|
|
689
|
+
|
|
690
|
+
## Cache Clearing Operation
|
|
691
|
+
|
|
692
|
+
**Cache Directory**: ${CACHE_DIR}
|
|
693
|
+
|
|
694
|
+
### Required Operations
|
|
695
|
+
|
|
696
|
+
1. **Directory Listing**:
|
|
697
|
+
- List all files in cache directory: ${CACHE_DIR}
|
|
698
|
+
- Filter for JSON files (*.json)
|
|
699
|
+
- Exclude metadata.json file (preserve cache metadata)
|
|
700
|
+
|
|
701
|
+
2. **Cache File Deletion**:
|
|
702
|
+
- Delete all cache files except metadata.json
|
|
703
|
+
- Handle file deletion safely with proper error handling
|
|
704
|
+
- Ensure all cache entries are completely removed
|
|
705
|
+
|
|
706
|
+
3. **Preserve System Files**:
|
|
707
|
+
- Keep metadata.json file intact
|
|
708
|
+
- Do not delete non-cache files
|
|
709
|
+
- Maintain cache directory structure
|
|
710
|
+
|
|
711
|
+
## Security Validation
|
|
712
|
+
|
|
713
|
+
The AI agent must:
|
|
714
|
+
1. **Validate cache directory**: Ensure ${CACHE_DIR} is the correct cache directory
|
|
715
|
+
2. **Prevent system file deletion**: Only delete cache files, preserve metadata.json
|
|
716
|
+
3. **Safe bulk deletion**: Handle multiple file deletions safely
|
|
717
|
+
4. **Path validation**: Ensure all operations stay within cache directory scope
|
|
718
|
+
|
|
719
|
+
## Expected Output Format (Successful Clearing)
|
|
720
|
+
|
|
721
|
+
\`\`\`json
|
|
722
|
+
{
|
|
723
|
+
"success": true,
|
|
724
|
+
"cacheDirectory": "${CACHE_DIR}",
|
|
725
|
+
"operations": {
|
|
726
|
+
"filesListed": true,
|
|
727
|
+
"cacheFilesFound": "number_of_cache_files",
|
|
728
|
+
"filesDeleted": "number_of_files_deleted",
|
|
729
|
+
"metadataPreserved": true
|
|
730
|
+
},
|
|
731
|
+
"deletedFiles": [
|
|
732
|
+
"list_of_deleted_cache_files"
|
|
733
|
+
],
|
|
734
|
+
"preservedFiles": [
|
|
735
|
+
"metadata.json"
|
|
736
|
+
],
|
|
737
|
+
"security": {
|
|
738
|
+
"directoryValidated": true,
|
|
739
|
+
"systemFilesProtected": true,
|
|
740
|
+
"bulkDeletionSafe": true
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
\`\`\`
|
|
744
|
+
|
|
745
|
+
## Error Response Format
|
|
746
|
+
|
|
747
|
+
\`\`\`json
|
|
748
|
+
{
|
|
749
|
+
"success": false,
|
|
750
|
+
"cacheDirectory": "${CACHE_DIR}",
|
|
751
|
+
"error": "detailed error message",
|
|
752
|
+
"errorType": "PERMISSION_DENIED|DIRECTORY_NOT_FOUND|SECURITY_VIOLATION|DELETE_ERROR",
|
|
753
|
+
"partialResults": {
|
|
754
|
+
"filesProcessed": "number_of_files_processed",
|
|
755
|
+
"filesDeleted": "number_of_files_successfully_deleted",
|
|
756
|
+
"failedDeletions": ["list_of_files_that_failed_to_delete"]
|
|
757
|
+
},
|
|
758
|
+
"security": {
|
|
759
|
+
"directoryValidated": boolean,
|
|
760
|
+
"systemFilesProtected": boolean,
|
|
761
|
+
"rejectionReason": "specific reason if clearing failed"
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
\`\`\`
|
|
765
|
+
`;
|
|
766
|
+
const instructions = `
|
|
767
|
+
## Implementation Steps
|
|
768
|
+
|
|
769
|
+
1. **Security Validation**:
|
|
770
|
+
- Validate cache directory path: ${CACHE_DIR}
|
|
771
|
+
- Ensure directory exists and is accessible
|
|
772
|
+
- Verify this is the correct cache directory
|
|
773
|
+
|
|
774
|
+
2. **File Discovery**:
|
|
775
|
+
- List all files in cache directory: ${CACHE_DIR}
|
|
776
|
+
- Filter for JSON files (*.json extension)
|
|
777
|
+
- Exclude metadata.json from deletion list
|
|
778
|
+
|
|
779
|
+
3. **Bulk File Deletion**:
|
|
780
|
+
- Delete each cache file safely
|
|
781
|
+
- Handle deletion errors gracefully
|
|
782
|
+
- Track successful and failed deletions
|
|
783
|
+
|
|
784
|
+
4. **Response Generation**:
|
|
785
|
+
- Format response according to specified JSON structure
|
|
786
|
+
- Include detailed operation results
|
|
787
|
+
- List all deleted and preserved files
|
|
788
|
+
|
|
789
|
+
## Safety Checks
|
|
790
|
+
|
|
791
|
+
- ✅ Cache directory validation delegated to AI agent
|
|
792
|
+
- ✅ Metadata file preservation (metadata.json)
|
|
793
|
+
- ✅ Safe bulk deletion with error handling
|
|
794
|
+
- ✅ System file protection and validation
|
|
795
|
+
|
|
796
|
+
## Context Information
|
|
797
|
+
|
|
798
|
+
- **Cache Directory**: ${CACHE_DIR}
|
|
799
|
+
- **Operation**: Bulk cache clearing (preserve metadata)
|
|
800
|
+
- **Security Level**: High (with directory and file validation)
|
|
801
|
+
- **Protected Files**: metadata.json
|
|
802
|
+
`;
|
|
803
|
+
return {
|
|
804
|
+
prompt,
|
|
805
|
+
instructions,
|
|
806
|
+
context: {
|
|
807
|
+
cacheDirectory: CACHE_DIR,
|
|
808
|
+
operation: 'cache_clear',
|
|
809
|
+
securityLevel: 'high',
|
|
810
|
+
protectedFiles: ['metadata.json'],
|
|
811
|
+
expectedFormat: 'json'
|
|
126
812
|
}
|
|
127
|
-
}
|
|
813
|
+
};
|
|
128
814
|
}
|
|
129
815
|
catch (error) {
|
|
130
|
-
throw new McpAdrError(`Failed to clear
|
|
816
|
+
throw new McpAdrError(`Failed to generate cache clear prompt: ${error instanceof Error ? error.message : String(error)}`, 'CACHE_ERROR');
|
|
131
817
|
}
|
|
132
818
|
}
|
|
133
819
|
/**
|
|
134
|
-
*
|
|
820
|
+
* Generate prompt for AI-driven cache statistics collection
|
|
135
821
|
*/
|
|
136
822
|
export async function getCacheStats() {
|
|
137
823
|
try {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
824
|
+
console.error(`[DEBUG] Generating cache stats prompt for directory: ${CACHE_DIR}`);
|
|
825
|
+
// First ensure cache is initialized
|
|
826
|
+
const initPrompt = await initializeCache();
|
|
827
|
+
const prompt = `
|
|
828
|
+
# Cache Statistics Collection Request
|
|
829
|
+
|
|
830
|
+
Please collect comprehensive statistics for the MCP ADR Analysis Server cache system.
|
|
831
|
+
|
|
832
|
+
## Step 1: Cache Initialization
|
|
833
|
+
${initPrompt.prompt}
|
|
834
|
+
|
|
835
|
+
**Instructions**: First, execute the cache initialization above to ensure the cache directory exists.
|
|
836
|
+
|
|
837
|
+
## Step 2: Cache Statistics Collection
|
|
838
|
+
|
|
839
|
+
**Cache Directory**: ${CACHE_DIR}
|
|
840
|
+
|
|
841
|
+
### Required Operations
|
|
842
|
+
|
|
843
|
+
1. **Directory Analysis**:
|
|
844
|
+
- List all files in cache directory: ${CACHE_DIR}
|
|
845
|
+
- Filter for cache files (*.json, excluding metadata.json)
|
|
846
|
+
- Count total cache entries
|
|
847
|
+
|
|
848
|
+
2. **File Size Analysis**:
|
|
849
|
+
- Calculate file size for each cache file
|
|
850
|
+
- Sum total cache size in bytes
|
|
851
|
+
- Provide size statistics
|
|
852
|
+
|
|
853
|
+
3. **Timestamp Analysis**:
|
|
854
|
+
- Read each cache file and extract timestamp
|
|
855
|
+
- Identify oldest cache entry (earliest timestamp)
|
|
856
|
+
- Identify newest cache entry (latest timestamp)
|
|
857
|
+
- Extract cache keys for oldest and newest entries
|
|
858
|
+
|
|
859
|
+
## Security Validation
|
|
860
|
+
|
|
861
|
+
The AI agent must:
|
|
862
|
+
1. **Validate cache directory**: Ensure ${CACHE_DIR} is the correct cache directory
|
|
863
|
+
2. **Safe file reading**: Read cache files safely with proper error handling
|
|
864
|
+
3. **JSON validation**: Parse cache entries safely and validate structure
|
|
865
|
+
4. **Path validation**: Ensure all operations stay within cache directory scope
|
|
866
|
+
|
|
867
|
+
## Expected Output Format
|
|
868
|
+
|
|
869
|
+
\`\`\`json
|
|
870
|
+
{
|
|
871
|
+
"success": true,
|
|
872
|
+
"cacheDirectory": "${CACHE_DIR}",
|
|
873
|
+
"statistics": {
|
|
874
|
+
"totalEntries": "number_of_cache_files",
|
|
875
|
+
"totalSize": "total_size_in_bytes",
|
|
876
|
+
"oldestEntry": "cache_key_of_oldest_entry_or_null",
|
|
877
|
+
"newestEntry": "cache_key_of_newest_entry_or_null"
|
|
878
|
+
},
|
|
879
|
+
"details": {
|
|
880
|
+
"cacheFiles": ["list_of_cache_file_names"],
|
|
881
|
+
"oldestTimestamp": "ISO_timestamp_or_null",
|
|
882
|
+
"newestTimestamp": "ISO_timestamp_or_null",
|
|
883
|
+
"averageFileSize": "average_size_in_bytes"
|
|
884
|
+
},
|
|
885
|
+
"operations": {
|
|
886
|
+
"cacheInitialized": boolean,
|
|
887
|
+
"filesListed": true,
|
|
888
|
+
"filesAnalyzed": "number_of_files_analyzed",
|
|
889
|
+
"timestampsProcessed": "number_of_timestamps_processed"
|
|
890
|
+
},
|
|
891
|
+
"security": {
|
|
892
|
+
"directoryValidated": true,
|
|
893
|
+
"filesReadSafely": true,
|
|
894
|
+
"jsonValidated": true
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
\`\`\`
|
|
898
|
+
|
|
899
|
+
## Error Response Format
|
|
900
|
+
|
|
901
|
+
\`\`\`json
|
|
902
|
+
{
|
|
903
|
+
"success": false,
|
|
904
|
+
"cacheDirectory": "${CACHE_DIR}",
|
|
905
|
+
"error": "detailed error message",
|
|
906
|
+
"errorType": "PERMISSION_DENIED|DIRECTORY_NOT_FOUND|SECURITY_VIOLATION|READ_ERROR|JSON_ERROR",
|
|
907
|
+
"partialResults": {
|
|
908
|
+
"filesProcessed": "number_of_files_processed",
|
|
909
|
+
"partialStatistics": {
|
|
910
|
+
"entriesFound": "number_found_before_error",
|
|
911
|
+
"sizeCalculated": "partial_size_if_available"
|
|
912
|
+
}
|
|
913
|
+
},
|
|
914
|
+
"security": {
|
|
915
|
+
"directoryValidated": boolean,
|
|
916
|
+
"rejectionReason": "specific reason if stats collection failed"
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
\`\`\`
|
|
920
|
+
`;
|
|
921
|
+
const instructions = `
|
|
922
|
+
## Implementation Steps
|
|
923
|
+
|
|
924
|
+
1. **Cache Initialization**:
|
|
925
|
+
- Execute cache directory initialization from Step 1
|
|
926
|
+
- Ensure cache directory exists and is accessible
|
|
927
|
+
|
|
928
|
+
2. **Security Validation**:
|
|
929
|
+
- Validate cache directory path: ${CACHE_DIR}
|
|
930
|
+
- Verify this is the correct cache directory
|
|
931
|
+
- Ensure safe file access permissions
|
|
932
|
+
|
|
933
|
+
3. **File Discovery and Analysis**:
|
|
934
|
+
- List all files in cache directory
|
|
935
|
+
- Filter for JSON files (exclude metadata.json)
|
|
936
|
+
- Calculate file sizes and count entries
|
|
937
|
+
|
|
938
|
+
4. **Timestamp Analysis**:
|
|
939
|
+
- Read each cache file safely
|
|
940
|
+
- Parse JSON content and extract timestamps
|
|
941
|
+
- Identify oldest and newest entries by timestamp
|
|
942
|
+
- Extract corresponding cache keys
|
|
943
|
+
|
|
944
|
+
5. **Statistics Compilation**:
|
|
945
|
+
- Compile comprehensive statistics
|
|
946
|
+
- Calculate totals and averages
|
|
947
|
+
- Format response according to specified JSON structure
|
|
948
|
+
|
|
949
|
+
## Safety Checks
|
|
950
|
+
|
|
951
|
+
- ✅ Cache directory validation delegated to AI agent
|
|
952
|
+
- ✅ Safe file reading with error handling
|
|
953
|
+
- ✅ JSON parsing with validation
|
|
954
|
+
- ✅ Comprehensive statistics collection
|
|
955
|
+
|
|
956
|
+
## Context Information
|
|
957
|
+
|
|
958
|
+
- **Cache Directory**: ${CACHE_DIR}
|
|
959
|
+
- **Operation**: Cache statistics collection
|
|
960
|
+
- **Security Level**: High (with directory and file validation)
|
|
961
|
+
- **Expected Data**: Entry count, total size, oldest/newest entries
|
|
962
|
+
`;
|
|
162
963
|
return {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
964
|
+
prompt,
|
|
965
|
+
instructions,
|
|
966
|
+
context: {
|
|
967
|
+
cacheDirectory: CACHE_DIR,
|
|
968
|
+
operation: 'cache_stats',
|
|
969
|
+
securityLevel: 'high',
|
|
970
|
+
expectedFormat: 'json',
|
|
971
|
+
expectedFields: ['totalEntries', 'totalSize', 'oldestEntry', 'newestEntry']
|
|
972
|
+
}
|
|
167
973
|
};
|
|
168
974
|
}
|
|
169
975
|
catch (error) {
|
|
170
|
-
throw new McpAdrError(`Failed to
|
|
976
|
+
throw new McpAdrError(`Failed to generate cache stats prompt: ${error instanceof Error ? error.message : String(error)}`, 'CACHE_ERROR');
|
|
171
977
|
}
|
|
172
978
|
}
|
|
173
979
|
/**
|
|
174
|
-
*
|
|
980
|
+
* Generate prompt for AI-driven cache cleanup (remove expired entries)
|
|
175
981
|
*/
|
|
176
982
|
export async function cleanupCache() {
|
|
177
983
|
try {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
984
|
+
console.error(`[DEBUG] Generating cache cleanup prompt for directory: ${CACHE_DIR}`);
|
|
985
|
+
// First ensure cache is initialized
|
|
986
|
+
const initPrompt = await initializeCache();
|
|
987
|
+
const currentTime = new Date().toISOString();
|
|
988
|
+
const prompt = `
|
|
989
|
+
# Cache Cleanup Request
|
|
990
|
+
|
|
991
|
+
Please clean up expired cache entries for the MCP ADR Analysis Server.
|
|
992
|
+
|
|
993
|
+
## Step 1: Cache Initialization
|
|
994
|
+
${initPrompt.prompt}
|
|
995
|
+
|
|
996
|
+
**Instructions**: First, execute the cache initialization above to ensure the cache directory exists.
|
|
997
|
+
|
|
998
|
+
## Step 2: Cache Cleanup Operation
|
|
999
|
+
|
|
1000
|
+
**Cache Directory**: ${CACHE_DIR}
|
|
1001
|
+
**Current Time**: ${currentTime}
|
|
1002
|
+
|
|
1003
|
+
### Required Operations
|
|
1004
|
+
|
|
1005
|
+
1. **File Discovery**:
|
|
1006
|
+
- List all files in cache directory: ${CACHE_DIR}
|
|
1007
|
+
- Filter for cache files (*.json, excluding metadata.json)
|
|
1008
|
+
- Prepare for TTL validation
|
|
1009
|
+
|
|
1010
|
+
2. **Expiration Check and Cleanup**:
|
|
1011
|
+
- For each cache file:
|
|
1012
|
+
- Read file content and parse JSON
|
|
1013
|
+
- Extract timestamp and TTL from cache entry
|
|
1014
|
+
- Calculate age: current_time - cache_timestamp
|
|
1015
|
+
- If age > TTL, mark for deletion
|
|
1016
|
+
- If file is corrupted/invalid JSON, mark for deletion
|
|
1017
|
+
|
|
1018
|
+
3. **File Deletion**:
|
|
1019
|
+
- Delete all expired and corrupted cache files
|
|
1020
|
+
- Count number of files cleaned up
|
|
1021
|
+
- Handle deletion errors gracefully
|
|
1022
|
+
|
|
1023
|
+
4. **Metadata Update**:
|
|
1024
|
+
- Update metadata.json with lastCleanup timestamp
|
|
1025
|
+
- If metadata.json is corrupted, recreate it
|
|
1026
|
+
- Preserve cache system integrity
|
|
1027
|
+
|
|
1028
|
+
## Security Validation
|
|
1029
|
+
|
|
1030
|
+
The AI agent must:
|
|
1031
|
+
1. **Validate cache directory**: Ensure ${CACHE_DIR} is the correct cache directory
|
|
1032
|
+
2. **Safe file operations**: Read and delete files safely with proper error handling
|
|
1033
|
+
3. **JSON validation**: Parse cache entries safely and handle corruption
|
|
1034
|
+
4. **Metadata protection**: Update metadata safely, recreate if corrupted
|
|
1035
|
+
|
|
1036
|
+
## Expected Output Format
|
|
1037
|
+
|
|
1038
|
+
\`\`\`json
|
|
1039
|
+
{
|
|
1040
|
+
"success": true,
|
|
1041
|
+
"cacheDirectory": "${CACHE_DIR}",
|
|
1042
|
+
"cleanup": {
|
|
1043
|
+
"cleanedCount": "number_of_files_cleaned",
|
|
1044
|
+
"totalFilesChecked": "number_of_files_checked",
|
|
1045
|
+
"expiredFiles": ["list_of_expired_files_deleted"],
|
|
1046
|
+
"corruptedFiles": ["list_of_corrupted_files_deleted"]
|
|
1047
|
+
},
|
|
1048
|
+
"operations": {
|
|
1049
|
+
"cacheInitialized": boolean,
|
|
1050
|
+
"filesListed": true,
|
|
1051
|
+
"ttlValidationPerformed": true,
|
|
1052
|
+
"metadataUpdated": true
|
|
1053
|
+
},
|
|
1054
|
+
"metadata": {
|
|
1055
|
+
"lastCleanup": "${currentTime}",
|
|
1056
|
+
"metadataRecreated": boolean
|
|
1057
|
+
},
|
|
1058
|
+
"security": {
|
|
1059
|
+
"directoryValidated": true,
|
|
1060
|
+
"filesProcessedSafely": true,
|
|
1061
|
+
"metadataProtected": true
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
\`\`\`
|
|
1065
|
+
|
|
1066
|
+
## Error Response Format
|
|
1067
|
+
|
|
1068
|
+
\`\`\`json
|
|
1069
|
+
{
|
|
1070
|
+
"success": false,
|
|
1071
|
+
"cacheDirectory": "${CACHE_DIR}",
|
|
1072
|
+
"error": "detailed error message",
|
|
1073
|
+
"errorType": "PERMISSION_DENIED|DIRECTORY_NOT_FOUND|SECURITY_VIOLATION|CLEANUP_ERROR",
|
|
1074
|
+
"partialResults": {
|
|
1075
|
+
"filesProcessed": "number_of_files_processed",
|
|
1076
|
+
"filesDeleted": "number_of_files_successfully_deleted",
|
|
1077
|
+
"failedDeletions": ["list_of_files_that_failed_to_delete"]
|
|
1078
|
+
},
|
|
1079
|
+
"security": {
|
|
1080
|
+
"directoryValidated": boolean,
|
|
1081
|
+
"rejectionReason": "specific reason if cleanup failed"
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
\`\`\`
|
|
1085
|
+
`;
|
|
1086
|
+
const instructions = `
|
|
1087
|
+
## Implementation Steps
|
|
1088
|
+
|
|
1089
|
+
1. **Cache Initialization**:
|
|
1090
|
+
- Execute cache directory initialization from Step 1
|
|
1091
|
+
- Ensure cache directory exists and is accessible
|
|
1092
|
+
|
|
1093
|
+
2. **Security Validation**:
|
|
1094
|
+
- Validate cache directory path: ${CACHE_DIR}
|
|
1095
|
+
- Verify this is the correct cache directory
|
|
1096
|
+
- Ensure safe file access permissions
|
|
1097
|
+
|
|
1098
|
+
3. **File Discovery and Analysis**:
|
|
1099
|
+
- List all files in cache directory
|
|
1100
|
+
- Filter for JSON cache files (exclude metadata.json)
|
|
1101
|
+
- Prepare for TTL validation
|
|
1102
|
+
|
|
1103
|
+
4. **Expiration Processing**:
|
|
1104
|
+
- Read each cache file safely
|
|
1105
|
+
- Parse JSON and extract timestamp/TTL
|
|
1106
|
+
- Calculate age using current time: ${currentTime}
|
|
1107
|
+
- Identify expired and corrupted files
|
|
1108
|
+
|
|
1109
|
+
5. **Cleanup Operations**:
|
|
1110
|
+
- Delete expired cache files
|
|
1111
|
+
- Delete corrupted cache files
|
|
1112
|
+
- Count successful deletions
|
|
1113
|
+
- Handle deletion errors gracefully
|
|
1114
|
+
|
|
1115
|
+
6. **Metadata Management**:
|
|
1116
|
+
- Update metadata.json with lastCleanup timestamp
|
|
1117
|
+
- Recreate metadata if corrupted
|
|
1118
|
+
- Ensure cache system integrity
|
|
1119
|
+
|
|
1120
|
+
## Safety Checks
|
|
1121
|
+
|
|
1122
|
+
- ✅ Cache directory validation delegated to AI agent
|
|
1123
|
+
- ✅ Safe file reading and deletion with error handling
|
|
1124
|
+
- ✅ JSON parsing with corruption detection
|
|
1125
|
+
- ✅ Metadata protection and recovery
|
|
1126
|
+
|
|
1127
|
+
## Context Information
|
|
1128
|
+
|
|
1129
|
+
- **Cache Directory**: ${CACHE_DIR}
|
|
1130
|
+
- **Current Time**: ${currentTime}
|
|
1131
|
+
- **Operation**: Cache cleanup (remove expired entries)
|
|
1132
|
+
- **Security Level**: High (with directory and file validation)
|
|
1133
|
+
- **Expected Return**: Number of cleaned files
|
|
1134
|
+
`;
|
|
1135
|
+
return {
|
|
1136
|
+
prompt,
|
|
1137
|
+
instructions,
|
|
1138
|
+
context: {
|
|
1139
|
+
cacheDirectory: CACHE_DIR,
|
|
1140
|
+
currentTime,
|
|
1141
|
+
operation: 'cache_cleanup',
|
|
1142
|
+
securityLevel: 'high',
|
|
1143
|
+
expectedFormat: 'json',
|
|
1144
|
+
expectedReturn: 'cleanedCount'
|
|
199
1145
|
}
|
|
200
|
-
}
|
|
201
|
-
// Update metadata
|
|
202
|
-
const metadataPath = join(CACHE_DIR, 'metadata.json');
|
|
203
|
-
try {
|
|
204
|
-
const metadata = JSON.parse(await fs.readFile(metadataPath, 'utf-8'));
|
|
205
|
-
metadata.lastCleanup = now.toISOString();
|
|
206
|
-
await fs.writeFile(metadataPath, JSON.stringify(metadata, null, 2));
|
|
207
|
-
}
|
|
208
|
-
catch {
|
|
209
|
-
// Metadata file corrupted, recreate it
|
|
210
|
-
const metadata = {
|
|
211
|
-
version: '1.0.0',
|
|
212
|
-
created: now.toISOString(),
|
|
213
|
-
lastCleanup: now.toISOString(),
|
|
214
|
-
};
|
|
215
|
-
await fs.writeFile(metadataPath, JSON.stringify(metadata, null, 2));
|
|
216
|
-
}
|
|
217
|
-
return cleanedCount;
|
|
1146
|
+
};
|
|
218
1147
|
}
|
|
219
1148
|
catch (error) {
|
|
220
|
-
throw new McpAdrError(`Failed to cleanup
|
|
1149
|
+
throw new McpAdrError(`Failed to generate cache cleanup prompt: ${error instanceof Error ? error.message : String(error)}`, 'CACHE_ERROR');
|
|
221
1150
|
}
|
|
222
1151
|
}
|
|
223
1152
|
/**
|
|
224
|
-
*
|
|
1153
|
+
* Generate prompt for AI-driven cache-or-generate operation
|
|
1154
|
+
* Note: This function now returns prompts for AI delegation instead of executing cache operations directly
|
|
225
1155
|
*/
|
|
226
|
-
export async function getCachedOrGenerate(key,
|
|
1156
|
+
export async function getCachedOrGenerate(key, _generator, options = {}) {
|
|
227
1157
|
try {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
1158
|
+
console.error(`[DEBUG] Generating cache-or-generate prompt for key: ${key}`);
|
|
1159
|
+
// Generate cache retrieval prompt
|
|
1160
|
+
const getCachePrompt = await getCache(key);
|
|
1161
|
+
const prompt = `
|
|
1162
|
+
# Cache-or-Generate Operation Request
|
|
1163
|
+
|
|
1164
|
+
Please perform a cache-or-generate operation for the MCP ADR Analysis Server.
|
|
1165
|
+
|
|
1166
|
+
## Cache-or-Generate Workflow
|
|
1167
|
+
|
|
1168
|
+
**Cache Key**: ${key}
|
|
1169
|
+
**TTL**: ${options.ttl || 3600} seconds
|
|
1170
|
+
**Compression**: ${options.compression || false}
|
|
1171
|
+
|
|
1172
|
+
### Step 1: Cache Retrieval Attempt
|
|
1173
|
+
|
|
1174
|
+
${getCachePrompt.prompt}
|
|
1175
|
+
|
|
1176
|
+
### Step 2: Data Generation (if cache miss)
|
|
1177
|
+
|
|
1178
|
+
If the cache retrieval in Step 1 results in a cache miss (cacheHit: false), then:
|
|
1179
|
+
|
|
1180
|
+
1. **Execute Data Generator**:
|
|
1181
|
+
- Call the provided data generator function
|
|
1182
|
+
- Generate fresh data for cache key: ${key}
|
|
1183
|
+
- Ensure data generation completes successfully
|
|
1184
|
+
|
|
1185
|
+
2. **Cache Storage**:
|
|
1186
|
+
- Store the generated data in cache using key: ${key}
|
|
1187
|
+
- Apply TTL: ${options.ttl || 3600} seconds
|
|
1188
|
+
- Apply compression: ${options.compression || false}
|
|
1189
|
+
- Follow cache storage security protocols
|
|
1190
|
+
|
|
1191
|
+
### Step 3: Return Data
|
|
1192
|
+
|
|
1193
|
+
Return the data from either:
|
|
1194
|
+
- Cache retrieval (if cache hit)
|
|
1195
|
+
- Fresh generation and storage (if cache miss)
|
|
1196
|
+
|
|
1197
|
+
## Security Validation
|
|
1198
|
+
|
|
1199
|
+
The AI agent must:
|
|
1200
|
+
1. **Cache security**: Follow all cache operation security protocols
|
|
1201
|
+
2. **Data validation**: Ensure generated data is valid and safe to cache
|
|
1202
|
+
3. **Generator safety**: Execute data generator safely with proper error handling
|
|
1203
|
+
4. **Storage validation**: Validate data before caching
|
|
1204
|
+
|
|
1205
|
+
## Expected Output Format (Cache Hit)
|
|
1206
|
+
|
|
1207
|
+
\`\`\`json
|
|
1208
|
+
{
|
|
1209
|
+
"success": true,
|
|
1210
|
+
"cacheKey": "${key}",
|
|
1211
|
+
"operation": "cache_hit",
|
|
1212
|
+
"data": "[retrieved cache data]",
|
|
1213
|
+
"cacheInfo": {
|
|
1214
|
+
"cacheHit": true,
|
|
1215
|
+
"dataSource": "cache",
|
|
1216
|
+
"timestamp": "cache_entry_timestamp",
|
|
1217
|
+
"ttl": "cache_entry_ttl"
|
|
1218
|
+
},
|
|
1219
|
+
"security": {
|
|
1220
|
+
"cacheSecurityValidated": true,
|
|
1221
|
+
"dataValidated": true
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
\`\`\`
|
|
1225
|
+
|
|
1226
|
+
## Expected Output Format (Cache Miss + Generation)
|
|
1227
|
+
|
|
1228
|
+
\`\`\`json
|
|
1229
|
+
{
|
|
1230
|
+
"success": true,
|
|
1231
|
+
"cacheKey": "${key}",
|
|
1232
|
+
"operation": "cache_miss_generated",
|
|
1233
|
+
"data": "[generated and cached data]",
|
|
1234
|
+
"cacheInfo": {
|
|
1235
|
+
"cacheHit": false,
|
|
1236
|
+
"dataSource": "generated",
|
|
1237
|
+
"generatedAt": "ISO_timestamp",
|
|
1238
|
+
"cachedAt": "ISO_timestamp",
|
|
1239
|
+
"ttl": ${options.ttl || 3600}
|
|
1240
|
+
},
|
|
1241
|
+
"operations": {
|
|
1242
|
+
"cacheChecked": true,
|
|
1243
|
+
"dataGenerated": true,
|
|
1244
|
+
"dataCached": true
|
|
1245
|
+
},
|
|
1246
|
+
"security": {
|
|
1247
|
+
"cacheSecurityValidated": true,
|
|
1248
|
+
"generatorExecutedSafely": true,
|
|
1249
|
+
"dataValidated": true,
|
|
1250
|
+
"storageValidated": true
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
\`\`\`
|
|
1254
|
+
|
|
1255
|
+
## Error Response Format
|
|
1256
|
+
|
|
1257
|
+
\`\`\`json
|
|
1258
|
+
{
|
|
1259
|
+
"success": false,
|
|
1260
|
+
"cacheKey": "${key}",
|
|
1261
|
+
"error": "detailed error message",
|
|
1262
|
+
"errorType": "CACHE_ERROR|GENERATION_ERROR|STORAGE_ERROR|SECURITY_VIOLATION",
|
|
1263
|
+
"failedOperation": "cache_retrieval|data_generation|cache_storage",
|
|
1264
|
+
"security": {
|
|
1265
|
+
"cacheSecurityValidated": boolean,
|
|
1266
|
+
"rejectionReason": "specific reason if operation failed"
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
\`\`\`
|
|
1270
|
+
`;
|
|
1271
|
+
const instructions = `
|
|
1272
|
+
## Implementation Steps
|
|
1273
|
+
|
|
1274
|
+
1. **Cache Retrieval**:
|
|
1275
|
+
- Execute cache retrieval operation from Step 1
|
|
1276
|
+
- Check if cache hit or miss
|
|
1277
|
+
- If cache hit, return cached data
|
|
1278
|
+
|
|
1279
|
+
2. **Data Generation** (if cache miss):
|
|
1280
|
+
- Execute the data generator function safely
|
|
1281
|
+
- Handle generation errors gracefully
|
|
1282
|
+
- Validate generated data
|
|
1283
|
+
|
|
1284
|
+
3. **Cache Storage** (if data generated):
|
|
1285
|
+
- Store generated data in cache
|
|
1286
|
+
- Apply specified TTL and compression options
|
|
1287
|
+
- Validate storage operation
|
|
1288
|
+
|
|
1289
|
+
4. **Response Generation**:
|
|
1290
|
+
- Format response according to specified JSON structure
|
|
1291
|
+
- Include data source information (cache vs generated)
|
|
1292
|
+
- Provide comprehensive operation details
|
|
1293
|
+
|
|
1294
|
+
## Safety Checks
|
|
1295
|
+
|
|
1296
|
+
- ✅ Cache operation security delegated to AI agent
|
|
1297
|
+
- ✅ Data generator execution with error handling
|
|
1298
|
+
- ✅ Generated data validation before caching
|
|
1299
|
+
- ✅ Comprehensive operation tracking
|
|
1300
|
+
|
|
1301
|
+
## Context Information
|
|
1302
|
+
|
|
1303
|
+
- **Cache Key**: ${key}
|
|
1304
|
+
- **TTL**: ${options.ttl || 3600} seconds
|
|
1305
|
+
- **Compression**: ${options.compression || false}
|
|
1306
|
+
- **Operation**: Cache-or-generate with automatic management
|
|
1307
|
+
- **Security Level**: High (with cache and generation validation)
|
|
1308
|
+
|
|
1309
|
+
## Important Notes
|
|
1310
|
+
|
|
1311
|
+
This operation combines cache retrieval and data generation. The AI agent should:
|
|
1312
|
+
1. Always try cache first
|
|
1313
|
+
2. Only generate data if cache miss
|
|
1314
|
+
3. Always cache generated data
|
|
1315
|
+
4. Return data from appropriate source
|
|
1316
|
+
`;
|
|
1317
|
+
return {
|
|
1318
|
+
prompt,
|
|
1319
|
+
instructions,
|
|
1320
|
+
context: {
|
|
1321
|
+
cacheKey: key,
|
|
1322
|
+
ttl: options.ttl || 3600,
|
|
1323
|
+
compression: options.compression || false,
|
|
1324
|
+
operation: 'cache_or_generate',
|
|
1325
|
+
securityLevel: 'high',
|
|
1326
|
+
expectedFormat: 'json'
|
|
1327
|
+
}
|
|
1328
|
+
};
|
|
238
1329
|
}
|
|
239
1330
|
catch (error) {
|
|
240
|
-
throw new McpAdrError(`Failed to
|
|
1331
|
+
throw new McpAdrError(`Failed to generate cache-or-generate prompt: ${error instanceof Error ? error.message : String(error)}`, 'CACHE_ERROR');
|
|
241
1332
|
}
|
|
242
1333
|
}
|
|
243
1334
|
//# sourceMappingURL=cache.js.map
|