@payloadcms/plugin-mcp 4.0.0-internal.1f9ae9a → 4.0.0-internal.293e026

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 (57) hide show
  1. package/dist/@types/assets.d.js +2 -0
  2. package/dist/@types/assets.d.js.map +1 -0
  3. package/dist/collection/index.d.ts.map +1 -1
  4. package/dist/collection/index.js +2 -1
  5. package/dist/collection/index.js.map +1 -1
  6. package/dist/mcp/buildMcpServer.d.ts.map +1 -1
  7. package/dist/mcp/buildMcpServer.js +17 -10
  8. package/dist/mcp/buildMcpServer.js.map +1 -1
  9. package/dist/mcp/builtin/collections/createTool.d.ts.map +1 -1
  10. package/dist/mcp/builtin/collections/createTool.js +12 -8
  11. package/dist/mcp/builtin/collections/createTool.js.map +1 -1
  12. package/dist/mcp/builtin/collections/updateTool.d.ts.map +1 -1
  13. package/dist/mcp/builtin/collections/updateTool.js +21 -17
  14. package/dist/mcp/builtin/collections/updateTool.js.map +1 -1
  15. package/dist/mcp/builtin/globals/updateTool.d.ts.map +1 -1
  16. package/dist/mcp/builtin/globals/updateTool.js +13 -9
  17. package/dist/mcp/builtin/globals/updateTool.js.map +1 -1
  18. package/dist/utils/schemaConversion/buildToolInput.d.ts +29 -0
  19. package/dist/utils/schemaConversion/buildToolInput.d.ts.map +1 -0
  20. package/dist/utils/schemaConversion/buildToolInput.js +51 -0
  21. package/dist/utils/schemaConversion/buildToolInput.js.map +1 -0
  22. package/dist/utils/schemaConversion/sanitizeEntitySchema.d.ts +15 -0
  23. package/dist/utils/schemaConversion/sanitizeEntitySchema.d.ts.map +1 -0
  24. package/dist/utils/schemaConversion/sanitizeEntitySchema.js +464 -0
  25. package/dist/utils/schemaConversion/sanitizeEntitySchema.js.map +1 -0
  26. package/dist/utils/schemaConversion/sanitizeEntitySchema.spec.js +158 -0
  27. package/dist/utils/schemaConversion/sanitizeEntitySchema.spec.js.map +1 -0
  28. package/package.json +5 -5
  29. package/src/@types/assets.d.ts +3 -0
  30. package/src/collection/index.ts +1 -0
  31. package/src/mcp/buildMcpServer.ts +19 -14
  32. package/src/mcp/builtin/collections/createTool.ts +37 -37
  33. package/src/mcp/builtin/collections/updateTool.ts +54 -49
  34. package/src/mcp/builtin/globals/updateTool.ts +35 -33
  35. package/src/utils/schemaConversion/buildToolInput.ts +68 -0
  36. package/src/utils/schemaConversion/sanitizeEntitySchema.spec.ts +103 -0
  37. package/src/utils/schemaConversion/sanitizeEntitySchema.ts +529 -0
  38. package/dist/utils/schemaConversion/prepareCollectionSchema.d.ts +0 -7
  39. package/dist/utils/schemaConversion/prepareCollectionSchema.d.ts.map +0 -1
  40. package/dist/utils/schemaConversion/prepareCollectionSchema.js +0 -37
  41. package/dist/utils/schemaConversion/prepareCollectionSchema.js.map +0 -1
  42. package/dist/utils/schemaConversion/sanitizeJsonSchema.d.ts +0 -13
  43. package/dist/utils/schemaConversion/sanitizeJsonSchema.d.ts.map +0 -1
  44. package/dist/utils/schemaConversion/sanitizeJsonSchema.js +0 -56
  45. package/dist/utils/schemaConversion/sanitizeJsonSchema.js.map +0 -1
  46. package/dist/utils/schemaConversion/simplifyRelationshipFields.d.ts +0 -20
  47. package/dist/utils/schemaConversion/simplifyRelationshipFields.d.ts.map +0 -1
  48. package/dist/utils/schemaConversion/simplifyRelationshipFields.js +0 -56
  49. package/dist/utils/schemaConversion/simplifyRelationshipFields.js.map +0 -1
  50. package/dist/utils/schemaConversion/transformPointFields.d.ts +0 -3
  51. package/dist/utils/schemaConversion/transformPointFields.d.ts.map +0 -1
  52. package/dist/utils/schemaConversion/transformPointFields.js +0 -57
  53. package/dist/utils/schemaConversion/transformPointFields.js.map +0 -1
  54. package/src/utils/schemaConversion/prepareCollectionSchema.ts +0 -39
  55. package/src/utils/schemaConversion/sanitizeJsonSchema.ts +0 -62
  56. package/src/utils/schemaConversion/simplifyRelationshipFields.ts +0 -70
  57. package/src/utils/schemaConversion/transformPointFields.ts +0 -56
@@ -1,20 +0,0 @@
1
- import type { JsonSchemaType } from '../../types.js';
2
- /**
3
- * Recursively processes JSON schema properties to simplify relationship fields
4
- * and convert `oneOf` constructs into MCP-friendly schemas.
5
- *
6
- * For create/update validation we only need to accept IDs (string/number),
7
- * not populated objects. `$ref` options pointing to full entity definitions
8
- * are removed entirely from `oneOf` unions. When a single option remains the
9
- * `oneOf` is unwrapped; otherwise it is converted to `anyOf`.
10
- *
11
- * This matters because `json-schema-to-zod` converts `oneOf` into a strict
12
- * `z.any().superRefine(...)` validator whose base type is `z.any()`, causing
13
- * `zodToJsonSchema` to emit `{}` and losing all type information in the MCP
14
- * tool input schema. `anyOf` instead produces a clean `z.union([...])`.
15
- *
16
- * NOTE: This function must operate on a cloned schema to avoid mutating
17
- * the original JSON schema used for tool listing.
18
- */
19
- export declare function simplifyRelationshipFields(schema: JsonSchemaType): JsonSchemaType;
20
- //# sourceMappingURL=simplifyRelationshipFields.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"simplifyRelationshipFields.d.ts","sourceRoot":"","sources":["../../../src/utils/schemaConversion/simplifyRelationshipFields.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,0BAA0B,CAAC,MAAM,EAAE,cAAc,GAAG,cAAc,CAkDjF"}
@@ -1,56 +0,0 @@
1
- /**
2
- * Recursively processes JSON schema properties to simplify relationship fields
3
- * and convert `oneOf` constructs into MCP-friendly schemas.
4
- *
5
- * For create/update validation we only need to accept IDs (string/number),
6
- * not populated objects. `$ref` options pointing to full entity definitions
7
- * are removed entirely from `oneOf` unions. When a single option remains the
8
- * `oneOf` is unwrapped; otherwise it is converted to `anyOf`.
9
- *
10
- * This matters because `json-schema-to-zod` converts `oneOf` into a strict
11
- * `z.any().superRefine(...)` validator whose base type is `z.any()`, causing
12
- * `zodToJsonSchema` to emit `{}` and losing all type information in the MCP
13
- * tool input schema. `anyOf` instead produces a clean `z.union([...])`.
14
- *
15
- * NOTE: This function must operate on a cloned schema to avoid mutating
16
- * the original JSON schema used for tool listing.
17
- */ export function simplifyRelationshipFields(schema) {
18
- if (!schema || typeof schema !== 'object') {
19
- return schema;
20
- }
21
- const processed = {
22
- ...schema
23
- };
24
- if (Array.isArray(processed.oneOf)) {
25
- const hasRef = processed.oneOf.some((option)=>option && typeof option === 'object' && '$ref' in option);
26
- const recurse = (option)=>typeof option === 'object' ? simplifyRelationshipFields(option) : option;
27
- if (hasRef) {
28
- const nonRefOptions = processed.oneOf.filter((option)=>!(option && typeof option === 'object' && '$ref' in option)).map(recurse);
29
- if (nonRefOptions.length === 1) {
30
- const single = nonRefOptions[0];
31
- delete processed.oneOf;
32
- if (typeof single === 'object') {
33
- Object.assign(processed, single);
34
- }
35
- } else if (nonRefOptions.length > 1) {
36
- delete processed.oneOf;
37
- processed.anyOf = nonRefOptions;
38
- }
39
- } else {
40
- processed.anyOf = processed.oneOf.map(recurse);
41
- delete processed.oneOf;
42
- }
43
- }
44
- if (processed.properties && typeof processed.properties === 'object') {
45
- processed.properties = Object.fromEntries(Object.entries(processed.properties).map(([key, value])=>[
46
- key,
47
- typeof value === 'object' ? simplifyRelationshipFields(value) : value
48
- ]));
49
- }
50
- if (processed.items && typeof processed.items === 'object' && !Array.isArray(processed.items)) {
51
- processed.items = simplifyRelationshipFields(processed.items);
52
- }
53
- return processed;
54
- }
55
-
56
- //# sourceMappingURL=simplifyRelationshipFields.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../src/utils/schemaConversion/simplifyRelationshipFields.ts"],"sourcesContent":["import type { JsonSchemaType } from '../../types.js'\n\n/**\n * Recursively processes JSON schema properties to simplify relationship fields\n * and convert `oneOf` constructs into MCP-friendly schemas.\n *\n * For create/update validation we only need to accept IDs (string/number),\n * not populated objects. `$ref` options pointing to full entity definitions\n * are removed entirely from `oneOf` unions. When a single option remains the\n * `oneOf` is unwrapped; otherwise it is converted to `anyOf`.\n *\n * This matters because `json-schema-to-zod` converts `oneOf` into a strict\n * `z.any().superRefine(...)` validator whose base type is `z.any()`, causing\n * `zodToJsonSchema` to emit `{}` and losing all type information in the MCP\n * tool input schema. `anyOf` instead produces a clean `z.union([...])`.\n *\n * NOTE: This function must operate on a cloned schema to avoid mutating\n * the original JSON schema used for tool listing.\n */\nexport function simplifyRelationshipFields(schema: JsonSchemaType): JsonSchemaType {\n if (!schema || typeof schema !== 'object') {\n return schema\n }\n\n const processed = { ...schema }\n\n if (Array.isArray(processed.oneOf)) {\n const hasRef = processed.oneOf.some(\n (option) => option && typeof option === 'object' && '$ref' in option,\n )\n\n const recurse = (option: boolean | JsonSchemaType): boolean | JsonSchemaType =>\n typeof option === 'object' ? simplifyRelationshipFields(option) : option\n\n if (hasRef) {\n const nonRefOptions = processed.oneOf\n .filter((option) => !(option && typeof option === 'object' && '$ref' in option))\n .map(recurse)\n\n if (nonRefOptions.length === 1) {\n const single = nonRefOptions[0]!\n delete processed.oneOf\n if (typeof single === 'object') {\n Object.assign(processed, single)\n }\n } else if (nonRefOptions.length > 1) {\n delete processed.oneOf\n processed.anyOf = nonRefOptions\n }\n } else {\n processed.anyOf = processed.oneOf.map(recurse)\n delete processed.oneOf\n }\n }\n\n if (processed.properties && typeof processed.properties === 'object') {\n processed.properties = Object.fromEntries(\n Object.entries(processed.properties).map(([key, value]) => [\n key,\n typeof value === 'object' ? simplifyRelationshipFields(value) : value,\n ]),\n )\n }\n\n if (processed.items && typeof processed.items === 'object' && !Array.isArray(processed.items)) {\n processed.items = simplifyRelationshipFields(processed.items)\n }\n\n return processed\n}\n"],"names":["simplifyRelationshipFields","schema","processed","Array","isArray","oneOf","hasRef","some","option","recurse","nonRefOptions","filter","map","length","single","Object","assign","anyOf","properties","fromEntries","entries","key","value","items"],"mappings":"AAEA;;;;;;;;;;;;;;;;CAgBC,GACD,OAAO,SAASA,2BAA2BC,MAAsB;IAC/D,IAAI,CAACA,UAAU,OAAOA,WAAW,UA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;QAC7FrB,UAAUqB,KAAK,GAAGvB,2BAA2BE,UAAUqB,KAAK;IAC9D;IAEA,OAAOrB;AACT"}
@@ -1,3 +0,0 @@
1
- import type { JsonSchemaType } from '../../types.js';
2
- export declare function transformPointFieldsForMCP(schema: JsonSchemaType): JsonSchemaType;
3
- //# sourceMappingURL=transformPointFields.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"transformPointFields.d.ts","sourceRoot":"","sources":["../../../src/utils/schemaConversion/transformPointFields.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD,wBAAgB,0BAA0B,CAAC,MAAM,EAAE,cAAc,GAAG,cAAc,CAqDjF"}
@@ -1,57 +0,0 @@
1
- export function transformPointFieldsForMCP(schema) {
2
- if (!schema || typeof schema !== 'object') {
3
- return schema;
4
- }
5
- const transformed = {
6
- ...schema
7
- };
8
- if (transformed.properties && typeof transformed.properties === 'object') {
9
- transformed.properties = Object.fromEntries(Object.entries(transformed.properties).map(([key, value])=>{
10
- if (!value || typeof value !== 'object') {
11
- return [
12
- key,
13
- value
14
- ];
15
- }
16
- const isArrayType = value.type === 'array' || Array.isArray(value.type) && value.type.includes('array');
17
- if (isArrayType && Array.isArray(value.items) && value.items.length === 2 && value.items.every((item)=>item && typeof item === 'object' && item.type === 'number')) {
18
- // Transform to object format
19
- const isNullable = Array.isArray(value.type) && value.type.includes('null');
20
- return [
21
- key,
22
- {
23
- type: isNullable ? [
24
- 'object',
25
- 'null'
26
- ] : 'object',
27
- description: value.description || 'Geographic coordinates (longitude, latitude)',
28
- properties: {
29
- latitude: {
30
- type: 'number',
31
- description: 'Latitude coordinate'
32
- },
33
- longitude: {
34
- type: 'number',
35
- description: 'Longitude coordinate'
36
- }
37
- },
38
- required: [
39
- 'longitude',
40
- 'latitude'
41
- ]
42
- }
43
- ];
44
- }
45
- return [
46
- key,
47
- transformPointFieldsForMCP(value)
48
- ];
49
- }));
50
- }
51
- if (transformed.items && typeof transformed.items === 'object' && !Array.isArray(transformed.items)) {
52
- transformed.items = transformPointFieldsForMCP(transformed.items);
53
- }
54
- return transformed;
55
- }
56
-
57
- //# sourceMappingURL=transformPointFields.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../src/utils/schemaConversion/transformPointFields.ts"],"sourcesContent":["import type { JsonSchemaType } from '../../types.js'\n\nexport function transformPointFieldsForMCP(schema: JsonSchemaType): JsonSchemaType {\n if (!schema || typeof schema !== 'object') {\n return schema\n }\n\n const transformed = { ...schema }\n\n if (transformed.properties && typeof transformed.properties === 'object') {\n transformed.properties = Object.fromEntries(\n Object.entries(transformed.properties).map(([key, value]) => {\n if (!value || typeof value !== 'object') {\n return [key, value]\n }\n const isArrayType =\n value.type === 'array' || (Array.isArray(value.type) && value.type.includes('array'))\n\n if (\n isArrayType &&\n Array.isArray(value.items) &&\n value.items.length === 2 &&\n value.items.every((item) => item && typeof item === 'object' && item.type === 'number')\n ) {\n // Transform to object format\n const isNullable = Array.isArray(value.type) && value.type.includes('null')\n\n return [\n key,\n {\n type: isNullable ? ['object', 'null'] : 'object',\n description: value.description || 'Geographic coordinates (longitude, latitude)',\n properties: {\n latitude: { type: 'number', description: 'Latitude coordinate' },\n longitude: { type: 'number', description: 'Longitude coordinate' },\n },\n required: ['longitude', 'latitude'],\n },\n ]\n }\n\n return [key, transformPointFieldsForMCP(value)]\n }),\n )\n }\n\n if (\n transformed.items &&\n typeof transformed.items === 'object' &&\n !Array.isArray(transformed.items)\n ) {\n transformed.items = transformPointFieldsForMCP(transformed.items)\n }\n\n return transformed\n}\n"],"names":["transformPointFieldsForMCP","schema","transformed","properties","Object","fromEntries","entries","map","key","value","isArrayType","type","Array","isArray","includes","items","length","every","item","isNullable","description","latitude","longitude","required"],"mappings":"AAEA,OAAO,SAASA,2BAA2BC,MAAsB;IAC/D,IAAI,CAACA,UAAU,OAAOA,WAAW,UAAU;QACzC,OAAOA;IACT;IAEA,MAAMC,cAAc;QA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E;IAEA,OAAOb;AACT"}
@@ -1,39 +0,0 @@
1
- import type { JsonSchemaType } from '../../types.js'
2
-
3
- import { sanitizeJsonSchema } from './sanitizeJsonSchema.js'
4
- import { simplifyRelationshipFields } from './simplifyRelationshipFields.js'
5
- import { transformPointFieldsForMCP } from './transformPointFields.js'
6
-
7
- /**
8
- * Fields Payload manages automatically — clients should never be required to provide
9
- * them. Stripped from `required` and `properties` so they don't appear in tool
10
- * inputs.
11
- */
12
- const PAYLOAD_MANAGED_FIELDS = new Set(['_status', 'createdAt', 'id', 'updatedAt'])
13
-
14
- /**
15
- * Sanitizes Payload's auto-generated collection JSON Schema so it can be safely
16
- * fed into the MCP server's `fromJsonSchema()` adapter (no zod intermediate step).
17
- */
18
- export const prepareCollectionSchema = (schema: JsonSchemaType): JsonSchemaType => {
19
- // Clone to avoid mutating the original schema (used elsewhere for tool listing)
20
- const schemaClone = JSON.parse(JSON.stringify(schema)) as JsonSchemaType
21
-
22
- const sanitized = sanitizeJsonSchema(schemaClone)
23
- const pointTransformed = transformPointFieldsForMCP(sanitized)
24
- const simplified = simplifyRelationshipFields(pointTransformed)
25
-
26
- if (simplified.properties) {
27
- for (const field of PAYLOAD_MANAGED_FIELDS) {
28
- delete simplified.properties[field]
29
- }
30
- }
31
- if (Array.isArray(simplified.required)) {
32
- simplified.required = simplified.required.filter((name) => !PAYLOAD_MANAGED_FIELDS.has(name))
33
- if (simplified.required.length === 0) {
34
- delete simplified.required
35
- }
36
- }
37
-
38
- return simplified
39
- }
@@ -1,62 +0,0 @@
1
- import type { JsonSchemaType } from '../../types.js'
2
-
3
- /**
4
- * Removes internal Payload properties (id, createdAt, updatedAt) from a
5
- * JSON Schema so they don't appear in the generated Zod validation schema.
6
- * Also strips `id` from the `required` array when present.
7
- *
8
- * Additionally normalizes nullable type arrays (e.g. `['array', 'null']` →
9
- * `'array'`) throughout the schema tree. Without this, `json-schema-to-zod`
10
- * emits a Zod union which the MCP SDK serialises back as `anyOf`, stripping
11
- * the concrete `type` from the output and breaking schema introspection.
12
- */
13
- export function sanitizeJsonSchema(schema: JsonSchemaType): JsonSchemaType {
14
- delete schema?.properties?.id
15
- delete schema?.properties?.createdAt
16
- delete schema?.properties?.updatedAt
17
-
18
- if (Array.isArray(schema.required)) {
19
- schema.required = schema.required.filter((field) => field !== 'id')
20
- if (schema.required.length === 0) {
21
- delete schema.required
22
- }
23
- }
24
-
25
- if (schema.properties && typeof schema.properties === 'object') {
26
- for (const key of Object.keys(schema.properties)) {
27
- const prop = schema.properties[key] as JsonSchemaType
28
- if (!prop || typeof prop !== 'object') {
29
- continue
30
- }
31
- normalizeNullableType(prop)
32
- if (prop.properties) {
33
- sanitizeJsonSchema(prop)
34
- }
35
- if (prop.items && typeof prop.items === 'object' && !Array.isArray(prop.items)) {
36
- sanitizeJsonSchema(prop.items)
37
- }
38
- }
39
- }
40
-
41
- return schema
42
- }
43
-
44
- /**
45
- * Strips `'null'` from a `type` array only when the remaining type is a
46
- * complex structural type (`array` or `object`).
47
- *
48
- * Simple scalar types (`string`, `number`, `boolean`) are intentionally
49
- * preserved as `['string', 'null']` so that the MCP SDK serialises them as a
50
- * compact inline `type` array. Complex types however cause `zodToJsonSchema`
51
- * to emit `anyOf: [{ type: 'array', items: ... }, { type: 'null' }]`, which
52
- * has no top-level `type` property and breaks schema introspection by clients.
53
- */
54
- function normalizeNullableType(schema: JsonSchemaType): void {
55
- if (!Array.isArray(schema.type)) {
56
- return
57
- }
58
- const nonNullTypes = schema.type.filter((t) => t !== 'null')
59
- if (nonNullTypes.length === 1 && (nonNullTypes[0] === 'array' || nonNullTypes[0] === 'object')) {
60
- schema.type = nonNullTypes[0]
61
- }
62
- }
@@ -1,70 +0,0 @@
1
- import type { JsonSchemaType } from '../../types.js'
2
-
3
- /**
4
- * Recursively processes JSON schema properties to simplify relationship fields
5
- * and convert `oneOf` constructs into MCP-friendly schemas.
6
- *
7
- * For create/update validation we only need to accept IDs (string/number),
8
- * not populated objects. `$ref` options pointing to full entity definitions
9
- * are removed entirely from `oneOf` unions. When a single option remains the
10
- * `oneOf` is unwrapped; otherwise it is converted to `anyOf`.
11
- *
12
- * This matters because `json-schema-to-zod` converts `oneOf` into a strict
13
- * `z.any().superRefine(...)` validator whose base type is `z.any()`, causing
14
- * `zodToJsonSchema` to emit `{}` and losing all type information in the MCP
15
- * tool input schema. `anyOf` instead produces a clean `z.union([...])`.
16
- *
17
- * NOTE: This function must operate on a cloned schema to avoid mutating
18
- * the original JSON schema used for tool listing.
19
- */
20
- export function simplifyRelationshipFields(schema: JsonSchemaType): JsonSchemaType {
21
- if (!schema || typeof schema !== 'object') {
22
- return schema
23
- }
24
-
25
- const processed = { ...schema }
26
-
27
- if (Array.isArray(processed.oneOf)) {
28
- const hasRef = processed.oneOf.some(
29
- (option) => option && typeof option === 'object' && '$ref' in option,
30
- )
31
-
32
- const recurse = (option: boolean | JsonSchemaType): boolean | JsonSchemaType =>
33
- typeof option === 'object' ? simplifyRelationshipFields(option) : option
34
-
35
- if (hasRef) {
36
- const nonRefOptions = processed.oneOf
37
- .filter((option) => !(option && typeof option === 'object' && '$ref' in option))
38
- .map(recurse)
39
-
40
- if (nonRefOptions.length === 1) {
41
- const single = nonRefOptions[0]!
42
- delete processed.oneOf
43
- if (typeof single === 'object') {
44
- Object.assign(processed, single)
45
- }
46
- } else if (nonRefOptions.length > 1) {
47
- delete processed.oneOf
48
- processed.anyOf = nonRefOptions
49
- }
50
- } else {
51
- processed.anyOf = processed.oneOf.map(recurse)
52
- delete processed.oneOf
53
- }
54
- }
55
-
56
- if (processed.properties && typeof processed.properties === 'object') {
57
- processed.properties = Object.fromEntries(
58
- Object.entries(processed.properties).map(([key, value]) => [
59
- key,
60
- typeof value === 'object' ? simplifyRelationshipFields(value) : value,
61
- ]),
62
- )
63
- }
64
-
65
- if (processed.items && typeof processed.items === 'object' && !Array.isArray(processed.items)) {
66
- processed.items = simplifyRelationshipFields(processed.items)
67
- }
68
-
69
- return processed
70
- }
@@ -1,56 +0,0 @@
1
- import type { JsonSchemaType } from '../../types.js'
2
-
3
- export function transformPointFieldsForMCP(schema: JsonSchemaType): JsonSchemaType {
4
- if (!schema || typeof schema !== 'object') {
5
- return schema
6
- }
7
-
8
- const transformed = { ...schema }
9
-
10
- if (transformed.properties && typeof transformed.properties === 'object') {
11
- transformed.properties = Object.fromEntries(
12
- Object.entries(transformed.properties).map(([key, value]) => {
13
- if (!value || typeof value !== 'object') {
14
- return [key, value]
15
- }
16
- const isArrayType =
17
- value.type === 'array' || (Array.isArray(value.type) && value.type.includes('array'))
18
-
19
- if (
20
- isArrayType &&
21
- Array.isArray(value.items) &&
22
- value.items.length === 2 &&
23
- value.items.every((item) => item && typeof item === 'object' && item.type === 'number')
24
- ) {
25
- // Transform to object format
26
- const isNullable = Array.isArray(value.type) && value.type.includes('null')
27
-
28
- return [
29
- key,
30
- {
31
- type: isNullable ? ['object', 'null'] : 'object',
32
- description: value.description || 'Geographic coordinates (longitude, latitude)',
33
- properties: {
34
- latitude: { type: 'number', description: 'Latitude coordinate' },
35
- longitude: { type: 'number', description: 'Longitude coordinate' },
36
- },
37
- required: ['longitude', 'latitude'],
38
- },
39
- ]
40
- }
41
-
42
- return [key, transformPointFieldsForMCP(value)]
43
- }),
44
- )
45
- }
46
-
47
- if (
48
- transformed.items &&
49
- typeof transformed.items === 'object' &&
50
- !Array.isArray(transformed.items)
51
- ) {
52
- transformed.items = transformPointFieldsForMCP(transformed.items)
53
- }
54
-
55
- return transformed
56
- }