@tsed/platform-mcp 8.31.2 → 8.32.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.
@@ -1,6 +1,7 @@
1
- import { isArrowFn } from "@tsed/core";
1
+ import { isArrowFn, isClass } from "@tsed/core";
2
2
  import { context, inject, injectable, logger } from "@tsed/di";
3
- import { JsonEntityStore } from "@tsed/schema";
3
+ import { deserialize } from "@tsed/json-mapper";
4
+ import { JsonEntityStore, JsonSchema } from "@tsed/schema";
4
5
  import { constantCase } from "change-case";
5
6
  import { MCP_PROVIDER_TYPES } from "../constants/constants.js";
6
7
  import { toZod } from "../utils/toZod.js";
@@ -11,17 +12,37 @@ function getOutputSchema(methodStore) {
11
12
  function getInputSchema(token, propertyKey) {
12
13
  return JsonEntityStore.from(token, propertyKey, 0).schema?.itemSchema();
13
14
  }
15
+ function resolveInputSchema(inputSchema) {
16
+ return isArrowFn(inputSchema) ? inputSchema() : inputSchema;
17
+ }
18
+ function deserializeInput(args, inputSchema, inputStore) {
19
+ if (!(inputSchema instanceof JsonSchema)) {
20
+ return args;
21
+ }
22
+ const type = inputStore?.getBestType?.() || inputSchema.getTarget();
23
+ if (!type || type === Object || !isClass(type)) {
24
+ return args;
25
+ }
26
+ return deserialize(args, {
27
+ type,
28
+ store: inputStore,
29
+ useAlias: true
30
+ });
31
+ }
14
32
  function mapOptions(options) {
15
33
  let handler;
34
+ let inputStore;
16
35
  if ("propertyKey" in options) {
17
36
  const { token, propertyKey } = options;
37
+ inputStore = JsonEntityStore.from(token, propertyKey, 0);
18
38
  handler = (args, extra) => {
19
39
  const instance = inject(options.token);
20
40
  return instance[options.propertyKey](args, extra);
21
41
  };
22
42
  const methodStore = JsonEntityStore.fromMethod(token, propertyKey);
23
43
  options.description = options.description || methodStore.operation.get("description");
24
- options.inputSchema = options.inputSchema || getInputSchema(token, propertyKey);
44
+ options.inputSchema =
45
+ options.inputSchema || (inputStore.schema?.itemSchema() || getInputSchema(token, propertyKey));
25
46
  options.outputSchema = options.outputSchema || getOutputSchema(methodStore);
26
47
  }
27
48
  else {
@@ -29,22 +50,26 @@ function mapOptions(options) {
29
50
  }
30
51
  return {
31
52
  ...options,
32
- handler
53
+ handler,
54
+ inputStore
33
55
  };
34
56
  }
35
57
  export function defineTool(options) {
36
58
  const provider = injectable(Symbol.for(`MCP:TOOL:${options.name}`))
37
59
  .type(MCP_PROVIDER_TYPES.TOOL)
38
60
  .factory(() => {
39
- let { handler, ...opts } = mapOptions(options);
61
+ const { handler, inputStore, ...opts } = mapOptions(options);
62
+ const inputSchema = resolveInputSchema(opts.inputSchema);
40
63
  return {
41
64
  ...opts,
42
65
  name: opts.name,
43
- inputSchema: toZod(isArrowFn(opts.inputSchema) ? opts.inputSchema() : opts.inputSchema),
66
+ inputSchema: toZod(inputSchema, {
67
+ useAlias: true
68
+ }),
44
69
  outputSchema: toZod(opts.outputSchema),
45
70
  async handler(args, extra) {
46
71
  try {
47
- return await handler(args, extra);
72
+ return await handler(deserializeInput(args, inputSchema, inputStore), extra);
48
73
  }
49
74
  catch (er) {
50
75
  const code = er.name && er.status ? `E_MCP_TOOL_${constantCase(er.name)}` : "E_MCP_TOOL_ERROR";
@@ -1,18 +1,19 @@
1
1
  import { JsonSchema } from "@tsed/schema";
2
2
  import { jsonSchemaToZod } from "json-schema-to-zod";
3
3
  import { z } from "zod";
4
- function transform(schema) {
5
- const schemaFactory = new Function("z", `return ${jsonSchemaToZod(schema.toJSON(), { zodVersion: 4 })}`);
4
+ function transform(schema, opts) {
5
+ const schemaFactory = new Function("z", `return ${jsonSchemaToZod(schema.toJSON(opts), { zodVersion: 4 })}`);
6
6
  return schemaFactory(z);
7
7
  }
8
8
  /**
9
9
  * Converts a {@link JsonSchema} produced by Ts.ED into an executable Zod validation object.
10
10
  *
11
11
  * @param schema JsonSchema instance or a pre-built Zod schema.
12
+ * @param opts
12
13
  * @returns A Zod object when conversion is possible, otherwise the original schema.
13
14
  * @module platform/mcp
14
15
  * @since 8.17.0
15
16
  */
16
- export function toZod(schema) {
17
- return schema && schema instanceof JsonSchema ? transform(schema) : schema;
17
+ export function toZod(schema, opts) {
18
+ return schema && schema instanceof JsonSchema ? transform(schema, opts) : schema;
18
19
  }