@softeria/ms-365-mcp-server 0.6.2 → 0.7.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.
@@ -31,6 +31,7 @@ export function createAndSaveSimplifiedOpenAPI(endpointsFile, openapiFile, opena
31
31
 
32
32
  if (openApiSpec.components && openApiSpec.components.schemas) {
33
33
  removeODataTypeRecursively(openApiSpec.components.schemas);
34
+ flattenComplexSchemasRecursively(openApiSpec.components.schemas);
34
35
  }
35
36
 
36
37
  fs.writeFileSync(openapiTrimmedFile, yaml.dump(openApiSpec));
@@ -83,3 +84,156 @@ function removeODataTypeRecursively(obj) {
83
84
  }
84
85
  });
85
86
  }
87
+
88
+ function flattenComplexSchemasRecursively(schemas) {
89
+ console.log('Flattening complex schemas for better client compatibility...');
90
+
91
+ let flattenedCount = 0;
92
+
93
+ Object.keys(schemas).forEach((schemaName) => {
94
+ const schema = schemas[schemaName];
95
+
96
+ if (schema.allOf && Array.isArray(schema.allOf) && schema.allOf.length <= 5) {
97
+ try {
98
+ const flattened = { type: 'object', properties: {} };
99
+ const required = new Set();
100
+
101
+ for (const subSchema of schema.allOf) {
102
+ if (subSchema.$ref && subSchema.$ref.startsWith('#/components/schemas/')) {
103
+ const refName = subSchema.$ref.replace('#/components/schemas/', '');
104
+ if (schemas[refName] && schemas[refName].properties) {
105
+ Object.assign(flattened.properties, schemas[refName].properties);
106
+ if (schemas[refName].required) {
107
+ schemas[refName].required.forEach((req) => required.add(req));
108
+ }
109
+ }
110
+ } else if (subSchema.properties) {
111
+ Object.assign(flattened.properties, subSchema.properties);
112
+ if (subSchema.required) {
113
+ subSchema.required.forEach((req) => required.add(req));
114
+ }
115
+ }
116
+
117
+ Object.keys(subSchema).forEach((key) => {
118
+ if (!['allOf', 'properties', 'required', '$ref'].includes(key) && !flattened[key]) {
119
+ flattened[key] = subSchema[key];
120
+ }
121
+ });
122
+ }
123
+
124
+ if (schema.properties) {
125
+ Object.assign(flattened.properties, schema.properties);
126
+ }
127
+
128
+ if (schema.required) {
129
+ schema.required.forEach((req) => required.add(req));
130
+ }
131
+
132
+ Object.keys(schema).forEach((key) => {
133
+ if (!['allOf', 'properties', 'required'].includes(key) && !flattened[key]) {
134
+ flattened[key] = schema[key];
135
+ }
136
+ });
137
+
138
+ if (required.size > 0) {
139
+ flattened.required = Array.from(required);
140
+ }
141
+
142
+ schemas[schemaName] = flattened;
143
+ flattenedCount++;
144
+ } catch (error) {
145
+ console.warn(`Warning: Could not flatten schema ${schemaName}:`, error.message);
146
+ }
147
+ }
148
+
149
+ if (schema.anyOf && Array.isArray(schema.anyOf) && schema.anyOf.length > 2) {
150
+ console.log(`Simplifying anyOf in ${schemaName} (${schema.anyOf.length} -> 1 option)`);
151
+ const simplified = { ...schema.anyOf[0] };
152
+ simplified.nullable = true;
153
+ simplified.description = `Simplified from ${schema.anyOf.length} anyOf options`;
154
+ schemas[schemaName] = simplified;
155
+ flattenedCount++;
156
+ }
157
+
158
+ if (schema.oneOf && Array.isArray(schema.oneOf) && schema.oneOf.length > 2) {
159
+ console.log(`Simplifying oneOf in ${schemaName} (${schema.oneOf.length} -> 1 option)`);
160
+ const simplified = { ...schema.oneOf[0] };
161
+ simplified.nullable = true;
162
+ simplified.description = `Simplified from ${schema.oneOf.length} oneOf options`;
163
+ schemas[schemaName] = simplified;
164
+ flattenedCount++;
165
+ }
166
+
167
+ if (schema.properties && Object.keys(schema.properties).length > 25) {
168
+ console.log(
169
+ `Reducing properties in ${schemaName} (${Object.keys(schema.properties).length} -> 25)`
170
+ );
171
+ const priorityProperties = {};
172
+ const allKeys = Object.keys(schema.properties);
173
+
174
+ if (schema.required) {
175
+ schema.required.forEach((key) => {
176
+ if (schema.properties[key]) {
177
+ priorityProperties[key] = schema.properties[key];
178
+ }
179
+ });
180
+ }
181
+
182
+ const remainingSlots = 25 - Object.keys(priorityProperties).length;
183
+ allKeys.slice(0, remainingSlots).forEach((key) => {
184
+ if (!priorityProperties[key]) {
185
+ priorityProperties[key] = schema.properties[key];
186
+ }
187
+ });
188
+
189
+ schema.properties = priorityProperties;
190
+ schema.description =
191
+ `${schema.description || ''} [Simplified: showing ${Object.keys(priorityProperties).length} of ${allKeys.length} properties]`.trim();
192
+ flattenedCount++;
193
+ }
194
+
195
+ if (schema.properties) {
196
+ simplifyNestedPropertiesRecursively(schema.properties, 0, 4);
197
+ }
198
+ });
199
+
200
+ console.log(`Flattened ${flattenedCount} complex schemas`);
201
+ }
202
+
203
+ function simplifyNestedPropertiesRecursively(properties, currentDepth, maxDepth) {
204
+ if (currentDepth >= maxDepth) {
205
+ return;
206
+ }
207
+
208
+ Object.keys(properties).forEach((key) => {
209
+ const prop = properties[key];
210
+
211
+ if (prop && typeof prop === 'object') {
212
+ if (currentDepth === maxDepth - 1 && prop.properties) {
213
+ console.log(`Flattening nested property at depth ${currentDepth}: ${key}`);
214
+ prop.type = 'object';
215
+ prop.description = `${prop.description || ''} [Simplified: nested object]`.trim();
216
+ delete prop.properties;
217
+ delete prop.additionalProperties;
218
+ } else if (prop.properties) {
219
+ simplifyNestedPropertiesRecursively(prop.properties, currentDepth + 1, maxDepth);
220
+ }
221
+
222
+ if (prop.anyOf && Array.isArray(prop.anyOf) && prop.anyOf.length > 2) {
223
+ prop.type = prop.anyOf[0].type || 'object';
224
+ prop.nullable = true;
225
+ prop.description =
226
+ `${prop.description || ''} [Simplified from ${prop.anyOf.length} options]`.trim();
227
+ delete prop.anyOf;
228
+ }
229
+
230
+ if (prop.oneOf && Array.isArray(prop.oneOf) && prop.oneOf.length > 2) {
231
+ prop.type = prop.oneOf[0].type || 'object';
232
+ prop.nullable = true;
233
+ prop.description =
234
+ `${prop.description || ''} [Simplified from ${prop.oneOf.length} options]`.trim();
235
+ delete prop.oneOf;
236
+ }
237
+ }
238
+ });
239
+ }