forked from Softeria/ms-365-mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-recursive-refs.js
More file actions
294 lines (239 loc) · 9.09 KB
/
remove-recursive-refs.js
File metadata and controls
294 lines (239 loc) · 9.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env node
import fs from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
/**
* Detect if a schema definition creates recursive references
* Handles complex recursive paths like #/definitions/X/properties/body/anyOf/1
*
* I really really hope this solves
* https://github.com/Softeria/ms-365-mcp-server/issues/36 and perhaps even
* https://github.com/Softeria/ms-365-mcp-server/issues/62
*
* Or any other silly tool that doesn't support recursive $refs
*
* Note - if the tool still struggles with $ref in general, this fix won't help!
*/
function detectRecursiveRefs(schema, definitionName) {
if (!schema || typeof schema !== 'object') return [];
const recursions = [];
const currentDefPath = `#/definitions/${definitionName}`;
function findAllRefs(obj, path = []) {
const refs = [];
function traverse(current, currentPath) {
if (!current || typeof current !== 'object') return;
if (Array.isArray(current)) {
current.forEach((item, index) => traverse(item, [...currentPath, index]));
return;
}
if (current.$ref) {
refs.push({
ref: current.$ref,
path: currentPath.join('.'),
});
return;
}
Object.entries(current).forEach(([key, value]) => {
traverse(value, [...currentPath, key]);
});
}
traverse(obj, path);
return refs;
}
const allRefs = findAllRefs(schema);
for (const refInfo of allRefs) {
const ref = refInfo.ref;
if (ref.startsWith(currentDefPath)) {
recursions.push({
path: refInfo.path,
ref: ref,
type: 'recursive_reference',
});
} else if (ref === currentDefPath) {
recursions.push({
path: refInfo.path,
ref: ref,
type: 'direct_self_reference',
});
}
}
return recursions;
}
function removeRecursiveProperties(schema, recursions) {
if (!schema || typeof schema !== 'object' || recursions.length === 0) {
return schema;
}
const cleaned = JSON.parse(JSON.stringify(schema));
const propertiesToRemove = new Set();
for (const recursion of recursions) {
const pathParts = recursion.path.split('.').filter((p) => p !== '');
if (pathParts[pathParts.length - 1] === 'items' && pathParts.length > 1) {
const propertyPath = pathParts.slice(0, -1).join('.');
propertiesToRemove.add(propertyPath);
} else {
propertiesToRemove.add(recursion.path);
}
}
const sortedPaths = Array.from(propertiesToRemove).sort(
(a, b) => b.split('.').length - a.split('.').length
);
for (const propertyPath of sortedPaths) {
const pathParts = propertyPath.split('.');
let current = cleaned;
for (let i = 0; i < pathParts.length - 1; i++) {
const part = pathParts[i];
if (current && typeof current === 'object' && part in current) {
current = current[part];
} else {
current = null;
break;
}
}
if (current && typeof current === 'object') {
const propertyName = pathParts[pathParts.length - 1];
if (propertyName in current) {
console.log(` Removing recursive property: ${propertyPath}`);
delete current[propertyName];
}
}
}
return cleaned;
}
/**
* Process a tool to remove recursive references while keeping other $refs
*/
function processToolSchema(tool) {
if (!tool.inputSchema || !tool.inputSchema.definitions) {
return tool;
}
const definitions = tool.inputSchema.definitions;
const processedDefinitions = {};
let totalRecursionsRemoved = 0;
console.log(`\n🔧 Processing ${tool.name}:`);
for (const [defName, defSchema] of Object.entries(definitions)) {
const recursions = detectRecursiveRefs(defSchema, defName);
if (recursions.length > 0) {
console.log(` Found ${recursions.length} recursive references in ${defName}`);
processedDefinitions[defName] = removeRecursiveProperties(defSchema, recursions);
totalRecursionsRemoved += recursions.length;
} else {
processedDefinitions[defName] = defSchema;
}
}
const cleanedTool = {
...tool,
inputSchema: {
...tool.inputSchema,
definitions: processedDefinitions,
},
};
console.log(` ✂️ Removed ${totalRecursionsRemoved} recursive references`);
return cleanedTool;
}
async function removeRecursiveRefs() {
try {
console.log('✂️ Removing Recursive References (Keeping Other $refs)\n');
console.log('='.repeat(60));
const inputPath = join(__dirname, 'schemas-with-refs-direct.json');
if (!fs.existsSync(inputPath)) {
throw new Error('Schema file not found. Run extract-schemas-direct.js first.');
}
const originalData = JSON.parse(fs.readFileSync(inputPath, 'utf8'));
const tools = originalData.result?.tools || [];
console.log(`Processing ${tools.length} tools...`);
const processedTools = tools.map(processToolSchema);
const cleanedData = {
...originalData,
result: {
...originalData.result,
tools: processedTools,
},
};
const outputPath = join(__dirname, 'schemas-properties-removed.json');
const cleanedString = JSON.stringify(cleanedData, null, 2);
fs.writeFileSync(outputPath, cleanedString);
console.log(`\n💾 Cleaned schemas saved to: ${outputPath}`);
const originalString = JSON.stringify(originalData);
const originalRefs = (originalString.match(/\$ref/g) || []).length;
const cleanedRefs = (cleanedString.match(/\$ref/g) || []).length;
const removedRefs = originalRefs - cleanedRefs;
console.log('\n📊 CLEANING ANALYSIS');
console.log('-'.repeat(40));
console.log(
`Original size: ${originalString.length.toLocaleString()} chars (${(originalString.length / 1024).toFixed(2)} KB)`
);
console.log(
`Cleaned size: ${cleanedString.length.toLocaleString()} chars (${(cleanedString.length / 1024).toFixed(2)} KB)`
);
const sizeDiff = cleanedString.length - originalString.length;
const sizeChange = ((sizeDiff / originalString.length) * 100).toFixed(1);
console.log(`Size change: ${sizeDiff.toLocaleString()} chars (${sizeChange}%)`);
console.log(`\nOriginal $refs: ${originalRefs}`);
console.log(`Cleaned $refs: ${cleanedRefs}`);
console.log(`Removed $refs: ${removedRefs}`);
console.log(`Refs remaining: ${((cleanedRefs / originalRefs) * 100).toFixed(1)}%`);
console.log('\n🧪 QUICK RECURSION CHECK');
console.log('-'.repeat(40));
const sampleRecursiveTools = [
'create-calendar-event',
'update-calendar-event',
'create-onenote-page',
];
let foundRecursions = 0;
for (const toolName of sampleRecursiveTools) {
const tool = processedTools.find((t) => t.name === toolName);
if (tool) {
const toolString = JSON.stringify(tool);
const recursivePattern = `#/definitions/${toolName}Parameters/properties/body/anyOf/1`;
if (toolString.includes(recursivePattern)) {
foundRecursions++;
console.log(` ❌ ${toolName}: Still contains recursive pattern`);
} else {
console.log(` ✅ ${toolName}: Recursive pattern removed`);
}
}
}
if (foundRecursions === 0) {
console.log('\n✅ No recursive patterns found in sample tools!');
} else {
console.log(`\n⚠️ ${foundRecursions} tools still contain recursive patterns`);
}
console.log('\n💡 SUMMARY');
console.log('='.repeat(40));
if (removedRefs > 0) {
console.log(`✅ Successfully removed ${removedRefs} recursive references`);
console.log(`✅ Kept ${cleanedRefs} non-recursive $ref references`);
if (sizeDiff < 0) {
console.log(`✅ Reduced schema size by ${Math.abs(sizeDiff).toLocaleString()} characters`);
} else if (sizeDiff < originalString.length * 0.1) {
console.log(`✅ Minimal size increase (${sizeChange}%)`);
}
console.log('\n📋 BENEFITS:');
console.log('• Eliminates infinite recursion during flattening');
console.log('• Preserves beneficial $ref references for shared types');
console.log('• May allow partial flattening for LangChain compatibility');
console.log('• Reduces schema complexity while maintaining functionality');
} else {
console.log('ℹ️ No recursive references found to remove');
}
const remainingRefTypes = new Set();
cleanedString.match(/"#\/definitions\/[^"]+"/g)?.forEach((ref) => {
const defName = ref.split('/').pop()?.replace('"', '');
if (defName) remainingRefTypes.add(defName);
});
console.log(`\n🔗 Remaining reference types: ${remainingRefTypes.size}`);
if (remainingRefTypes.size <= 10) {
console.log('Sample remaining refs:', Array.from(remainingRefTypes).slice(0, 5).join(', '));
}
} catch (error) {
console.error('Error removing recursive refs:', error.message);
console.error(error.stack);
process.exit(1);
}
}
export { removeRecursiveRefs };
if (import.meta.url === `file://${process.argv[1]}`) {
removeRecursiveRefs();
}