@tsed/platform-mcp 8.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/lib/esm/constants/constants.js +11 -0
  2. package/lib/esm/decorators/prompt.js +29 -0
  3. package/lib/esm/decorators/resource.js +33 -0
  4. package/lib/esm/decorators/tool.js +32 -0
  5. package/lib/esm/fn/definePrompt.js +52 -0
  6. package/lib/esm/fn/defineResource.js +31 -0
  7. package/lib/esm/fn/defineTool.js +85 -0
  8. package/lib/esm/index.js +40 -0
  9. package/lib/esm/interfaces/PlatformMcpSettings.js +1 -0
  10. package/lib/esm/services/McpServerFactory.js +46 -0
  11. package/lib/esm/services/PlatformMcpModule.js +54 -0
  12. package/lib/esm/utils/json-schema-to-zod/Types.js +1 -0
  13. package/lib/esm/utils/json-schema-to-zod/cli.js +83 -0
  14. package/lib/esm/utils/json-schema-to-zod/index.js +25 -0
  15. package/lib/esm/utils/json-schema-to-zod/jsonSchemaToZod.js +52 -0
  16. package/lib/esm/utils/json-schema-to-zod/parsers/parseAllOf.js +43 -0
  17. package/lib/esm/utils/json-schema-to-zod/parsers/parseAnyOf.js +17 -0
  18. package/lib/esm/utils/json-schema-to-zod/parsers/parseArray.js +22 -0
  19. package/lib/esm/utils/json-schema-to-zod/parsers/parseBoolean.js +9 -0
  20. package/lib/esm/utils/json-schema-to-zod/parsers/parseConst.js +9 -0
  21. package/lib/esm/utils/json-schema-to-zod/parsers/parseDefault.js +9 -0
  22. package/lib/esm/utils/json-schema-to-zod/parsers/parseEnum.js +21 -0
  23. package/lib/esm/utils/json-schema-to-zod/parsers/parseIfThenElse.js +26 -0
  24. package/lib/esm/utils/json-schema-to-zod/parsers/parseMultipleType.js +10 -0
  25. package/lib/esm/utils/json-schema-to-zod/parsers/parseNot.js +13 -0
  26. package/lib/esm/utils/json-schema-to-zod/parsers/parseNull.js +9 -0
  27. package/lib/esm/utils/json-schema-to-zod/parsers/parseNullable.js +11 -0
  28. package/lib/esm/utils/json-schema-to-zod/parsers/parseNumber.js +52 -0
  29. package/lib/esm/utils/json-schema-to-zod/parsers/parseObject.js +177 -0
  30. package/lib/esm/utils/json-schema-to-zod/parsers/parseOneOf.js +40 -0
  31. package/lib/esm/utils/json-schema-to-zod/parsers/parseSchema.js +153 -0
  32. package/lib/esm/utils/json-schema-to-zod/parsers/parseString.js +63 -0
  33. package/lib/esm/utils/json-schema-to-zod/utils/cliTools.js +126 -0
  34. package/lib/esm/utils/json-schema-to-zod/utils/half.js +9 -0
  35. package/lib/esm/utils/json-schema-to-zod/utils/jsdocs.js +24 -0
  36. package/lib/esm/utils/json-schema-to-zod/utils/omit.js +12 -0
  37. package/lib/esm/utils/json-schema-to-zod/utils/withMessage.js +28 -0
  38. package/lib/esm/utils/toZod.js +17 -0
  39. package/lib/tsconfig.esm.tsbuildinfo +1 -0
  40. package/lib/types/constants/constants.d.ts +11 -0
  41. package/lib/types/decorators/prompt.d.ts +26 -0
  42. package/lib/types/decorators/resource.d.ts +28 -0
  43. package/lib/types/decorators/tool.d.ts +22 -0
  44. package/lib/types/fn/definePrompt.d.ts +78 -0
  45. package/lib/types/fn/defineResource.d.ts +41 -0
  46. package/lib/types/fn/defineTool.d.ts +284 -0
  47. package/lib/types/index.d.ts +40 -0
  48. package/lib/types/interfaces/PlatformMcpSettings.d.ts +43 -0
  49. package/lib/types/services/McpServerFactory.d.ts +15 -0
  50. package/lib/types/services/PlatformMcpModule.d.ts +22 -0
  51. package/lib/types/utils/json-schema-to-zod/Types.d.ts +116 -0
  52. package/lib/types/utils/json-schema-to-zod/cli.d.ts +2 -0
  53. package/lib/types/utils/json-schema-to-zod/index.d.ts +25 -0
  54. package/lib/types/utils/json-schema-to-zod/jsonSchemaToZod.d.ts +11 -0
  55. package/lib/types/utils/json-schema-to-zod/parsers/parseAllOf.d.ts +10 -0
  56. package/lib/types/utils/json-schema-to-zod/parsers/parseAnyOf.d.ts +10 -0
  57. package/lib/types/utils/json-schema-to-zod/parsers/parseArray.d.ts +10 -0
  58. package/lib/types/utils/json-schema-to-zod/parsers/parseBoolean.d.ts +10 -0
  59. package/lib/types/utils/json-schema-to-zod/parsers/parseConst.d.ts +10 -0
  60. package/lib/types/utils/json-schema-to-zod/parsers/parseDefault.d.ts +8 -0
  61. package/lib/types/utils/json-schema-to-zod/parsers/parseEnum.d.ts +10 -0
  62. package/lib/types/utils/json-schema-to-zod/parsers/parseIfThenElse.d.ts +12 -0
  63. package/lib/types/utils/json-schema-to-zod/parsers/parseMultipleType.d.ts +10 -0
  64. package/lib/types/utils/json-schema-to-zod/parsers/parseNot.d.ts +10 -0
  65. package/lib/types/utils/json-schema-to-zod/parsers/parseNull.d.ts +10 -0
  66. package/lib/types/utils/json-schema-to-zod/parsers/parseNullable.d.ts +10 -0
  67. package/lib/types/utils/json-schema-to-zod/parsers/parseNumber.d.ts +10 -0
  68. package/lib/types/utils/json-schema-to-zod/parsers/parseObject.d.ts +10 -0
  69. package/lib/types/utils/json-schema-to-zod/parsers/parseOneOf.d.ts +10 -0
  70. package/lib/types/utils/json-schema-to-zod/parsers/parseSchema.d.ts +61 -0
  71. package/lib/types/utils/json-schema-to-zod/parsers/parseString.d.ts +10 -0
  72. package/lib/types/utils/json-schema-to-zod/utils/cliTools.d.ts +67 -0
  73. package/lib/types/utils/json-schema-to-zod/utils/half.d.ts +7 -0
  74. package/lib/types/utils/json-schema-to-zod/utils/jsdocs.d.ts +15 -0
  75. package/lib/types/utils/json-schema-to-zod/utils/omit.d.ts +7 -0
  76. package/lib/types/utils/json-schema-to-zod/utils/withMessage.d.ts +19 -0
  77. package/lib/types/utils/toZod.d.ts +10 -0
  78. package/package.json +69 -0
  79. package/readme.md +73 -0
  80. package/src/constants/constants.ts +11 -0
  81. package/src/decorators/prompt.spec.ts +30 -0
  82. package/src/decorators/prompt.ts +39 -0
  83. package/src/decorators/resource.spec.ts +33 -0
  84. package/src/decorators/resource.ts +44 -0
  85. package/src/decorators/tool.spec.ts +68 -0
  86. package/src/decorators/tool.ts +34 -0
  87. package/src/fn/definePrompt.ts +93 -0
  88. package/src/fn/defineResource.ts +81 -0
  89. package/src/fn/defineTool.spec.ts +29 -0
  90. package/src/fn/defineTool.ts +141 -0
  91. package/src/index.ts +40 -0
  92. package/src/interfaces/PlatformMcpSettings.ts +45 -0
  93. package/src/services/McpServerFactory.ts +69 -0
  94. package/src/services/PlatformMcpModule.ts +67 -0
  95. package/src/utils/json-schema-to-zod/Types.ts +123 -0
  96. package/src/utils/json-schema-to-zod/cli.ts +88 -0
  97. package/src/utils/json-schema-to-zod/index.ts +26 -0
  98. package/src/utils/json-schema-to-zod/jsonSchemaToZod.ts +60 -0
  99. package/src/utils/json-schema-to-zod/parsers/parseAllOf.ts +50 -0
  100. package/src/utils/json-schema-to-zod/parsers/parseAnyOf.ts +19 -0
  101. package/src/utils/json-schema-to-zod/parsers/parseArray.ts +28 -0
  102. package/src/utils/json-schema-to-zod/parsers/parseBoolean.ts +11 -0
  103. package/src/utils/json-schema-to-zod/parsers/parseConst.ts +11 -0
  104. package/src/utils/json-schema-to-zod/parsers/parseDefault.ts +11 -0
  105. package/src/utils/json-schema-to-zod/parsers/parseEnum.ts +20 -0
  106. package/src/utils/json-schema-to-zod/parsers/parseIfThenElse.ts +35 -0
  107. package/src/utils/json-schema-to-zod/parsers/parseMultipleType.ts +12 -0
  108. package/src/utils/json-schema-to-zod/parsers/parseNot.ts +15 -0
  109. package/src/utils/json-schema-to-zod/parsers/parseNull.ts +11 -0
  110. package/src/utils/json-schema-to-zod/parsers/parseNullable.ts +13 -0
  111. package/src/utils/json-schema-to-zod/parsers/parseNumber.ts +56 -0
  112. package/src/utils/json-schema-to-zod/parsers/parseObject.ts +220 -0
  113. package/src/utils/json-schema-to-zod/parsers/parseOneOf.ts +44 -0
  114. package/src/utils/json-schema-to-zod/parsers/parseSchema.ts +190 -0
  115. package/src/utils/json-schema-to-zod/parsers/parseString.ts +73 -0
  116. package/src/utils/json-schema-to-zod/utils/cliTools.ts +189 -0
  117. package/src/utils/json-schema-to-zod/utils/half.ts +9 -0
  118. package/src/utils/json-schema-to-zod/utils/jsdocs.ts +29 -0
  119. package/src/utils/json-schema-to-zod/utils/omit.ts +14 -0
  120. package/src/utils/json-schema-to-zod/utils/withMessage.ts +42 -0
  121. package/src/utils/toZod.spec.ts +15 -0
  122. package/src/utils/toZod.ts +20 -0
  123. package/tsconfig.esm.json +14 -0
  124. package/vitest.config.mts +36 -0
@@ -0,0 +1,126 @@
1
+ import { readFileSync, statSync } from "fs";
2
+ /**
3
+ * Lightweight CLI arg parser honoring shorthands, type coercion, and required flags.
4
+ *
5
+ * @param params Parameter metadata map.
6
+ * @param args Argument vector (usually `process.argv`).
7
+ * @param help Optional help flag description; when provided, `-h/--help` prints usage.
8
+ * @module platform/mcp
9
+ * @since 8.17.0
10
+ */
11
+ export function parseArgs(params, args, help) {
12
+ const result = {};
13
+ if (help) {
14
+ let index = args.indexOf("--help");
15
+ if (index === -1) {
16
+ index = args.indexOf("-h");
17
+ }
18
+ if (index !== -1) {
19
+ printParams({
20
+ ...params,
21
+ help: {
22
+ shorthand: "h",
23
+ description: typeof help === "string" ? help : "Display this message :)"
24
+ }
25
+ });
26
+ process.exit(0);
27
+ }
28
+ }
29
+ for (const name in params) {
30
+ const { shorthand, required, value } = params[name];
31
+ let index = args.indexOf("--" + name);
32
+ if (index === -1 && shorthand) {
33
+ index = args.indexOf("-" + shorthand);
34
+ }
35
+ if (index === -1) {
36
+ if (required || required === "") {
37
+ throw new Error(typeof required === "string" && required !== "" ? required : `Missing required argument ${name}`);
38
+ }
39
+ result[name] = false;
40
+ continue;
41
+ }
42
+ if (value) {
43
+ const val = args[index + 1];
44
+ if (val === undefined) {
45
+ throw new Error(`Expected a value for argument ${name}`);
46
+ }
47
+ if (value === "number") {
48
+ const asNumber = Number(val);
49
+ if (isNaN(asNumber)) {
50
+ throw new Error(`Value of argument ${name} must be a valid number`);
51
+ }
52
+ result[name] = asNumber;
53
+ continue;
54
+ }
55
+ if (Array.isArray(value) && !value.includes(val)) {
56
+ throw new Error(`Value of argument ${name} must be one of ${value}`);
57
+ }
58
+ result[name] = val;
59
+ }
60
+ else {
61
+ result[name] = true;
62
+ }
63
+ }
64
+ return result;
65
+ }
66
+ /**
67
+ * Parses inline JSON or reads JSON from a file path (auto-detected by stat).
68
+ *
69
+ * @module platform/mcp
70
+ * @since 8.17.0
71
+ */
72
+ export function parseOrReadJSON(jsonOrPath) {
73
+ jsonOrPath = jsonOrPath.trim();
74
+ if (jsonOrPath.length < 255 && statSync(jsonOrPath, { throwIfNoEntry: false })?.isFile()) {
75
+ jsonOrPath = readFileSync(jsonOrPath, "utf-8");
76
+ }
77
+ return JSON.parse(jsonOrPath);
78
+ }
79
+ /**
80
+ * Reads piped stdin into a UTF-8 string.
81
+ *
82
+ * @module platform/mcp
83
+ * @since 8.17.0
84
+ */
85
+ export function readPipe() {
86
+ return new Promise((resolve, reject) => {
87
+ let buf = "";
88
+ process.stdin
89
+ .setEncoding("utf-8")
90
+ .on("end", () => resolve(buf))
91
+ .on("error", reject)
92
+ .on("readable", () => {
93
+ let chunk;
94
+ while ((chunk = process.stdin.read()) != null) {
95
+ buf += chunk;
96
+ }
97
+ });
98
+ });
99
+ }
100
+ /**
101
+ * Prints a simple help table describing available CLI parameters.
102
+ *
103
+ * @module platform/mcp
104
+ * @since 8.17.0
105
+ */
106
+ export function printParams(params) {
107
+ const longest = Object.keys(params).reduce((l, c) => (c.length > l ? c.length : l), 5);
108
+ const header = "Name " + " ".repeat(longest - 2) + "Short Description";
109
+ console.log(header);
110
+ for (const name in params) {
111
+ let { shorthand, description } = params[name];
112
+ if (shorthand) {
113
+ shorthand = " -" + shorthand;
114
+ }
115
+ else {
116
+ shorthand = " ";
117
+ }
118
+ if (description) {
119
+ description = " " + description;
120
+ }
121
+ else {
122
+ description = "";
123
+ }
124
+ console.log("--" + name + " ".repeat(longest - name.length) + shorthand + description);
125
+ }
126
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Splits an array in half, returning the left and right segments.
3
+ *
4
+ * @module platform/mcp
5
+ * @since 8.17.0
6
+ */
7
+ export const half = (arr) => {
8
+ return [arr.slice(0, arr.length / 2), arr.slice(arr.length / 2)];
9
+ };
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Expands a description string into a formatted JSDoc block suitable for emitted code.
3
+ *
4
+ * @module platform/mcp
5
+ * @since 8.17.0
6
+ */
7
+ export const expandJsdocs = (jsdocs) => {
8
+ const lines = jsdocs.split("\n");
9
+ const result = lines.length === 1 ? lines[0] : `\n${lines.map((x) => `* ${x}`).join("\n")}\n`;
10
+ return `/**${result}*/\n`;
11
+ };
12
+ /**
13
+ * Adds inline JSDoc to a parsed snippet when the source schema exposes a description.
14
+ *
15
+ * @module platform/mcp
16
+ * @since 8.17.0
17
+ */
18
+ export const addJsdocs = (schema, parsed) => {
19
+ const description = schema.description;
20
+ if (!description) {
21
+ return parsed;
22
+ }
23
+ return `\n${expandJsdocs(description)}${parsed}`;
24
+ };
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Returns a shallow copy of an object without the provided keys.
3
+ *
4
+ * @module platform/mcp
5
+ * @since 8.17.0
6
+ */
7
+ export const omit = (obj, ...keys) => Object.keys(obj).reduce((acc, key) => {
8
+ if (!keys.includes(key)) {
9
+ acc[key] = obj[key];
10
+ }
11
+ return acc;
12
+ }, {});
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Utility that appends chained Zod calls while preserving custom error messages defined on the JSON Schema node.
3
+ *
4
+ * @param schema Schema containing potential validation keywords.
5
+ * @param key Keyword to inspect (e.g., `minLength`).
6
+ * @param get Builder that returns the string fragments to append.
7
+ * @module platform/mcp
8
+ * @since 8.17.0
9
+ */
10
+ export function withMessage(schema, key, get) {
11
+ const value = schema[key];
12
+ let r = "";
13
+ if (value !== undefined) {
14
+ const got = get({ value, json: JSON.stringify(value) });
15
+ if (got) {
16
+ const opener = got[0];
17
+ const prefix = got.length === 3 ? got[1] : "";
18
+ const closer = got.length === 3 ? got[2] : got[1];
19
+ r += opener;
20
+ if (schema.errorMessage?.[key] !== undefined) {
21
+ r += prefix + JSON.stringify(schema.errorMessage[key]);
22
+ }
23
+ r;
24
+ r += closer;
25
+ }
26
+ }
27
+ return r;
28
+ }
@@ -0,0 +1,17 @@
1
+ import { JsonSchema } from "@tsed/schema";
2
+ import { z } from "zod";
3
+ import { jsonSchemaToZod } from "./json-schema-to-zod/index.js";
4
+ function transform(schema) {
5
+ return eval(`(z) => ${jsonSchemaToZod(schema.toJSON(), { zodVersion: 4 })}`)(z);
6
+ }
7
+ /**
8
+ * Converts a {@link JsonSchema} produced by Ts.ED into an executable Zod validation object.
9
+ *
10
+ * @param schema JsonSchema instance or a pre-built Zod schema.
11
+ * @returns A Zod object when conversion is possible, otherwise the original schema.
12
+ * @module platform/mcp
13
+ * @since 8.17.0
14
+ */
15
+ export function toZod(schema) {
16
+ return schema && schema instanceof JsonSchema ? transform(schema) : schema;
17
+ }