@mcp-craftsman/zod 0.2.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.
package/dist/index.cjs ADDED
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ defineZodTool: () => defineZodTool
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+ var import_core = require("@mcp-craftsman/core");
37
+ var z = __toESM(require("zod"), 1);
38
+ function defineZodTool(definition) {
39
+ return (0, import_core.defineTool)({
40
+ annotations: definition.annotations,
41
+ description: definition.description,
42
+ handler: async (input, context) => {
43
+ const parsedInput = parseInput(definition.name, input, definition.input);
44
+ const result = await definition.handler(parsedInput, context);
45
+ return validateToolResult(definition.name, result, definition.output);
46
+ },
47
+ inputSchema: toJsonSchema(definition.input),
48
+ name: definition.name,
49
+ outputSchema: toJsonSchema(definition.output),
50
+ policy: definition.policy,
51
+ returnsStructuredContent: definition.returnsStructuredContent ?? true,
52
+ title: definition.title
53
+ });
54
+ }
55
+ function parseInput(toolName, input, schema) {
56
+ const result = schema.safeParse(input);
57
+ if (!result.success) {
58
+ throw new Error(`${toolName} received invalid input.`);
59
+ }
60
+ return result.data;
61
+ }
62
+ function validateToolResult(toolName, result, output) {
63
+ if (result.structuredContent === void 0) {
64
+ return result;
65
+ }
66
+ const parsedOutput = output.safeParse(result.structuredContent);
67
+ if (!parsedOutput.success) {
68
+ throw new Error(`${toolName} returned invalid structured content.`);
69
+ }
70
+ return {
71
+ ...result,
72
+ structuredContent: parsedOutput.data
73
+ };
74
+ }
75
+ function toJsonSchema(schema) {
76
+ return z.toJSONSchema(schema);
77
+ }
78
+ // Annotate the CommonJS export names for ESM import in node:
79
+ 0 && (module.exports = {
80
+ defineZodTool
81
+ });
@@ -0,0 +1,11 @@
1
+ import { ToolCapability, ToolCallContext, ToolCallResult } from '@mcp-craftsman/core';
2
+ import * as z from 'zod';
3
+
4
+ type ZodToolDefinition<TInputSchema extends z.ZodType, TOutputSchema extends z.ZodType> = Omit<ToolCapability<z.output<TInputSchema>, z.output<TOutputSchema>>, "handler" | "inputSchema" | "kind" | "outputSchema"> & {
5
+ readonly handler: (input: z.output<TInputSchema>, context: ToolCallContext) => Promise<ToolCallResult<z.output<TOutputSchema>>> | ToolCallResult<z.output<TOutputSchema>>;
6
+ readonly input: TInputSchema;
7
+ readonly output: TOutputSchema;
8
+ };
9
+ declare function defineZodTool<TInputSchema extends z.ZodType, TOutputSchema extends z.ZodType>(definition: ZodToolDefinition<TInputSchema, TOutputSchema>): ToolCapability<z.output<TInputSchema>, z.output<TOutputSchema>>;
10
+
11
+ export { type ZodToolDefinition, defineZodTool };
@@ -0,0 +1,11 @@
1
+ import { ToolCapability, ToolCallContext, ToolCallResult } from '@mcp-craftsman/core';
2
+ import * as z from 'zod';
3
+
4
+ type ZodToolDefinition<TInputSchema extends z.ZodType, TOutputSchema extends z.ZodType> = Omit<ToolCapability<z.output<TInputSchema>, z.output<TOutputSchema>>, "handler" | "inputSchema" | "kind" | "outputSchema"> & {
5
+ readonly handler: (input: z.output<TInputSchema>, context: ToolCallContext) => Promise<ToolCallResult<z.output<TOutputSchema>>> | ToolCallResult<z.output<TOutputSchema>>;
6
+ readonly input: TInputSchema;
7
+ readonly output: TOutputSchema;
8
+ };
9
+ declare function defineZodTool<TInputSchema extends z.ZodType, TOutputSchema extends z.ZodType>(definition: ZodToolDefinition<TInputSchema, TOutputSchema>): ToolCapability<z.output<TInputSchema>, z.output<TOutputSchema>>;
10
+
11
+ export { type ZodToolDefinition, defineZodTool };
package/dist/index.js ADDED
@@ -0,0 +1,46 @@
1
+ // src/index.ts
2
+ import { defineTool } from "@mcp-craftsman/core";
3
+ import * as z from "zod";
4
+ function defineZodTool(definition) {
5
+ return defineTool({
6
+ annotations: definition.annotations,
7
+ description: definition.description,
8
+ handler: async (input, context) => {
9
+ const parsedInput = parseInput(definition.name, input, definition.input);
10
+ const result = await definition.handler(parsedInput, context);
11
+ return validateToolResult(definition.name, result, definition.output);
12
+ },
13
+ inputSchema: toJsonSchema(definition.input),
14
+ name: definition.name,
15
+ outputSchema: toJsonSchema(definition.output),
16
+ policy: definition.policy,
17
+ returnsStructuredContent: definition.returnsStructuredContent ?? true,
18
+ title: definition.title
19
+ });
20
+ }
21
+ function parseInput(toolName, input, schema) {
22
+ const result = schema.safeParse(input);
23
+ if (!result.success) {
24
+ throw new Error(`${toolName} received invalid input.`);
25
+ }
26
+ return result.data;
27
+ }
28
+ function validateToolResult(toolName, result, output) {
29
+ if (result.structuredContent === void 0) {
30
+ return result;
31
+ }
32
+ const parsedOutput = output.safeParse(result.structuredContent);
33
+ if (!parsedOutput.success) {
34
+ throw new Error(`${toolName} returned invalid structured content.`);
35
+ }
36
+ return {
37
+ ...result,
38
+ structuredContent: parsedOutput.data
39
+ };
40
+ }
41
+ function toJsonSchema(schema) {
42
+ return z.toJSONSchema(schema);
43
+ }
44
+ export {
45
+ defineZodTool
46
+ };
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@mcp-craftsman/zod",
3
+ "version": "0.2.0",
4
+ "private": false,
5
+ "description": "Zod schema integration for MCP Craftsman tools.",
6
+ "type": "module",
7
+ "main": "./dist/index.cjs",
8
+ "module": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js",
14
+ "require": "./dist/index.cjs"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "peerDependencies": {
24
+ "@mcp-craftsman/core": ">=0.2.0 <1",
25
+ "zod": "^4.4.3"
26
+ },
27
+ "devDependencies": {
28
+ "zod": "^4.4.3",
29
+ "@mcp-craftsman/core": "^0.2.0"
30
+ },
31
+ "scripts": {
32
+ "build": "tsup src/index.ts --format esm,cjs --dts",
33
+ "test": "vitest run"
34
+ }
35
+ }