document-operations 1.1.76 → 1.1.78

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.
package/dist/index.cjs CHANGED
@@ -12,15 +12,23 @@ let document_outline_js = require("document-outline.js");
12
12
  * Builds a DocumentOperation whose input and output types are both inferred from real Zod schemas, so a call site never has to repeat `z.infer<typeof Schema>` itself. Use `defineOperationWithoutOutputSchema` instead for the rare operation whose original MCP tool registration declared no outputSchema at all.
13
13
  *
14
14
  * Deliberately returns the parameter object's own inferred literal type rather than an explicit `DocumentOperation<...>` return annotation. Zod 4's ZodType carries its own output type as one of three generic parameters (`ZodType<Output, Input, Internals>`), and TypeScript cannot prove, from inside this function body, that a generic `InputSchema extends z.ZodType` also extends `z.ZodType<z.infer<InputSchema>>` — the two-step indirection through an abstract type parameter defeats it even though it is true for every concrete schema. Letting the literal's own type flow out instead sidesteps that: a caller's own concrete schema (e.g. `ConvertDocumentInputSchema`) is trivially assignable to `DocumentOperation<In, Out>`'s `z.ZodType<In>` field wherever that supertype is actually needed (the registry array below, or a future MCP/REST/CLI adapter parameter), because widening a CONCRETE schema's output type to line up is a normal covariant check, not the same self-referential generic one.
15
+ *
16
+ * The returned `run` re-validates `input` against `inputSchema` before calling the operation's own implementation, rather than passing the caller's argument straight through. This is not merely defensive: `run`'s own parameter is contravariant, so a caller-supplied `run(input: z.infer<InputSchema>, ...)` is only soundly assignable to `DocumentOperation<unknown, unknown>["run"]` (what `DOCUMENT_OPERATIONS` holds every operation as) if the exposed `run` genuinely accepts `unknown` — parsing here, rather than trusting whichever transport calls it to have validated first, is what actually makes that true. `inputSchema.parse` throws a ZodError for anything that does not conform, giving every transport (MCP, REST, the CLI) the same well-typed input, or the same failure, an external validation step would have produced anyway wherever one already existed.
15
17
  */
16
18
  function defineOperation(operation) {
17
- return operation;
19
+ return {
20
+ ...operation,
21
+ run: async (input, context) => operation.run(operation.inputSchema.parse(input), context)
22
+ };
18
23
  }
19
24
  /**
20
- * The `defineOperation` counterpart for an operation with no output schema to infer from (the original MCP tool registration declared none) — `Out` is inferred from `run`'s own return type instead. See `defineOperation`'s own comment for why this returns the parameter object's inferred literal type rather than an explicit `DocumentOperation<...>` annotation.
25
+ * The `defineOperation` counterpart for an operation with no output schema to infer from (the original MCP tool registration declared none) — `Out` is inferred from `run`'s own return type instead. See `defineOperation`'s own comment for why this returns the parameter object's inferred literal type rather than an explicit `DocumentOperation<...>` annotation, and why the returned `run` validates `input` against `inputSchema` itself.
21
26
  */
22
27
  function defineOperationWithoutOutputSchema(operation) {
23
- return operation;
28
+ return {
29
+ ...operation,
30
+ run: async (input, context) => operation.run(operation.inputSchema.parse(input), context)
31
+ };
24
32
  }
25
33
  //#endregion
26
34
  //#region src/io/document-input.ts