@decocms/bindings 0.1.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/README.md +680 -0
- package/dist/index.d.ts +90 -0
- package/dist/index.js +79 -0
- package/dist/index.js.map +1 -0
- package/package.json +41 -0
- package/src/core/binder.ts +221 -0
- package/src/index.ts +16 -0
- package/src/well-known/collections.ts +403 -0
- package/src/well-known/models.ts +69 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { ZodType } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Core Binder Types and Utilities
|
|
5
|
+
*
|
|
6
|
+
* This module provides the core types and utilities for the bindings system.
|
|
7
|
+
* Bindings define standardized interfaces that integrations (MCPs) can implement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* ToolBinder defines a single tool within a binding.
|
|
12
|
+
* It specifies the tool name, input/output schemas, and whether it's optional.
|
|
13
|
+
*
|
|
14
|
+
* @template TName - The tool name (can be a string or RegExp for pattern matching)
|
|
15
|
+
* @template TInput - The input type (inferred from inputSchema)
|
|
16
|
+
* @template TReturn - The return type (inferred from outputSchema)
|
|
17
|
+
*/
|
|
18
|
+
interface ToolBinder<TName extends string | RegExp = string, TInput = any, TReturn extends object | null | boolean = object> {
|
|
19
|
+
/** The name of the tool (e.g., "DECO_CHAT_CHANNELS_JOIN") */
|
|
20
|
+
name: TName;
|
|
21
|
+
/** Zod schema for validating tool input */
|
|
22
|
+
inputSchema: ZodType<TInput>;
|
|
23
|
+
/** Optional Zod schema for validating tool output */
|
|
24
|
+
outputSchema?: ZodType<TReturn>;
|
|
25
|
+
/**
|
|
26
|
+
* Whether this tool is optional in the binding.
|
|
27
|
+
* If true, an implementation doesn't need to provide this tool.
|
|
28
|
+
*/
|
|
29
|
+
opt?: true;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Binder represents a collection of tool definitions that form a binding.
|
|
33
|
+
* A binding is like a TypeScript interface - it defines what tools must be implemented.
|
|
34
|
+
*
|
|
35
|
+
* @template TDefinition - Array of ToolBinder definitions
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* const MY_BINDING = [{
|
|
40
|
+
* name: "MY_TOOL" as const,
|
|
41
|
+
* inputSchema: z.object({ id: z.string() }),
|
|
42
|
+
* outputSchema: z.object({ success: z.boolean() }),
|
|
43
|
+
* }] as const satisfies Binder;
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
type Binder<TDefinition extends readonly ToolBinder[] = readonly ToolBinder[]> = TDefinition;
|
|
47
|
+
/**
|
|
48
|
+
* Tool with schemas for validation
|
|
49
|
+
*/
|
|
50
|
+
interface ToolWithSchemas {
|
|
51
|
+
name: string;
|
|
52
|
+
inputSchema?: ZodType<any> | Record<string, unknown>;
|
|
53
|
+
outputSchema?: ZodType<any> | Record<string, unknown>;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Binding checker interface
|
|
57
|
+
*/
|
|
58
|
+
interface BindingChecker {
|
|
59
|
+
/**
|
|
60
|
+
* Check if a set of tools implements the binding with full schema validation.
|
|
61
|
+
*
|
|
62
|
+
* Validates:
|
|
63
|
+
* - Tool name matches (exact or regex)
|
|
64
|
+
* - Input schema: Tool accepts what binder requires (no removals from binder to tool)
|
|
65
|
+
* - Output schema: Tool provides what binder expects (no removals from tool to binder)
|
|
66
|
+
*
|
|
67
|
+
* @param tools - Array of tools with names and schemas
|
|
68
|
+
* @returns Promise<boolean> - true if all tools implement the binding correctly
|
|
69
|
+
*/
|
|
70
|
+
isImplementedBy: (tools: ToolWithSchemas[]) => Promise<boolean>;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Creates a binding checker with full schema validation using json-schema-diff.
|
|
74
|
+
*
|
|
75
|
+
* This performs strict compatibility checking:
|
|
76
|
+
* - For input schemas: Validates that the tool can accept what the binder requires
|
|
77
|
+
* - For output schemas: Validates that the tool provides what the binder expects
|
|
78
|
+
*
|
|
79
|
+
* @param binderTools - The binding definition to check against
|
|
80
|
+
* @returns A binding checker with an async isImplementedBy method
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* const checker = createBindingChecker(MY_BINDING);
|
|
85
|
+
* const isCompatible = await checker.isImplementedBy(availableTools);
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
declare function createBindingChecker<TDefinition extends readonly ToolBinder[]>(binderTools: TDefinition): BindingChecker;
|
|
89
|
+
|
|
90
|
+
export { type Binder, type BindingChecker, type ToolBinder, type ToolWithSchemas, createBindingChecker };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
2
|
+
import { diffSchemas } from 'json-schema-diff';
|
|
3
|
+
|
|
4
|
+
// src/core/binder.ts
|
|
5
|
+
function normalizeSchema(schema) {
|
|
6
|
+
if (!schema) return void 0;
|
|
7
|
+
if (schema._def) {
|
|
8
|
+
const jsonSchema2 = zodToJsonSchema(schema, {
|
|
9
|
+
// Don't add additionalProperties: false to allow structural compatibility
|
|
10
|
+
$refStrategy: "none"
|
|
11
|
+
});
|
|
12
|
+
if (jsonSchema2.type === "object") {
|
|
13
|
+
delete jsonSchema2.additionalProperties;
|
|
14
|
+
}
|
|
15
|
+
return jsonSchema2;
|
|
16
|
+
}
|
|
17
|
+
const jsonSchema = schema;
|
|
18
|
+
if (jsonSchema.type === "object" && "additionalProperties" in jsonSchema) {
|
|
19
|
+
const copy = { ...jsonSchema };
|
|
20
|
+
delete copy.additionalProperties;
|
|
21
|
+
return copy;
|
|
22
|
+
}
|
|
23
|
+
return jsonSchema;
|
|
24
|
+
}
|
|
25
|
+
function createBindingChecker(binderTools) {
|
|
26
|
+
return {
|
|
27
|
+
isImplementedBy: async (tools) => {
|
|
28
|
+
for (const binderTool of binderTools) {
|
|
29
|
+
const pattern = typeof binderTool.name === "string" ? new RegExp(`^${binderTool.name}$`) : binderTool.name;
|
|
30
|
+
const matchedTool = tools.find((t) => pattern.test(t.name));
|
|
31
|
+
if (!matchedTool && binderTool.opt) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (!matchedTool) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
const binderInputSchema = normalizeSchema(binderTool.inputSchema);
|
|
38
|
+
const toolInputSchema = normalizeSchema(matchedTool.inputSchema);
|
|
39
|
+
if (binderInputSchema && toolInputSchema) {
|
|
40
|
+
try {
|
|
41
|
+
const inputDiff = await diffSchemas({
|
|
42
|
+
sourceSchema: binderInputSchema,
|
|
43
|
+
destinationSchema: toolInputSchema
|
|
44
|
+
});
|
|
45
|
+
if (inputDiff.removalsFound) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
} catch (error) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
} else if (binderInputSchema && !toolInputSchema) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
const binderOutputSchema = normalizeSchema(binderTool.outputSchema);
|
|
55
|
+
const toolOutputSchema = normalizeSchema(matchedTool.outputSchema);
|
|
56
|
+
if (binderOutputSchema && toolOutputSchema) {
|
|
57
|
+
try {
|
|
58
|
+
const outputDiff = await diffSchemas({
|
|
59
|
+
sourceSchema: binderOutputSchema,
|
|
60
|
+
destinationSchema: toolOutputSchema
|
|
61
|
+
});
|
|
62
|
+
if (outputDiff.removalsFound) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
} catch (error) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
} else if (binderOutputSchema && !toolOutputSchema) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export { createBindingChecker };
|
|
78
|
+
//# sourceMappingURL=index.js.map
|
|
79
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core/binder.ts"],"names":["jsonSchema"],"mappings":"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","file":"index.js","sourcesContent":["/**\n * Core Binder Types and Utilities\n *\n * This module provides the core types and utilities for the bindings system.\n * Bindings define standardized interfaces that integrations (MCPs) can implement.\n */\n\nimport type { ZodType } from \"zod\";\nimport { zodToJsonSchema } from \"zod-to-json-schema\";\nimport { diffSchemas } from \"json-schema-diff\";\n\n/**\n * ToolBinder defines a single tool within a binding.\n * It specifies the tool name, input/output schemas, and whether it's optional.\n *\n * @template TName - The tool name (can be a string or RegExp for pattern matching)\n * @template TInput - The input type (inferred from inputSchema)\n * @template TReturn - The return type (inferred from outputSchema)\n */\nexport interface ToolBinder<\n TName extends string | RegExp = string,\n // biome-ignore lint/suspicious/noExplicitAny: Generic type parameter\n TInput = any,\n TReturn extends object | null | boolean = object,\n> {\n /** The name of the tool (e.g., \"DECO_CHAT_CHANNELS_JOIN\") */\n name: TName;\n\n /** Zod schema for validating tool input */\n inputSchema: ZodType<TInput>;\n\n /** Optional Zod schema for validating tool output */\n outputSchema?: ZodType<TReturn>;\n\n /**\n * Whether this tool is optional in the binding.\n * If true, an implementation doesn't need to provide this tool.\n */\n opt?: true;\n}\n\n/**\n * Binder represents a collection of tool definitions that form a binding.\n * A binding is like a TypeScript interface - it defines what tools must be implemented.\n *\n * @template TDefinition - Array of ToolBinder definitions\n *\n * @example\n * ```ts\n * const MY_BINDING = [{\n * name: \"MY_TOOL\" as const,\n * inputSchema: z.object({ id: z.string() }),\n * outputSchema: z.object({ success: z.boolean() }),\n * }] as const satisfies Binder;\n * ```\n */\nexport type Binder<\n TDefinition extends readonly ToolBinder[] = readonly ToolBinder[],\n> = TDefinition;\n\n/**\n * Tool with schemas for validation\n */\nexport interface ToolWithSchemas {\n name: string;\n inputSchema?: ZodType<any> | Record<string, unknown>;\n outputSchema?: ZodType<any> | Record<string, unknown>;\n}\n\n/**\n * Converts a schema to JSON Schema format if it's a Zod schema\n */\nfunction normalizeSchema(schema: any): Record<string, unknown> | undefined {\n if (!schema) return undefined;\n\n // If it's a Zod schema (has _def property), convert it\n if (schema._def) {\n const jsonSchema = zodToJsonSchema(schema, {\n // Don't add additionalProperties: false to allow structural compatibility\n $refStrategy: \"none\",\n }) as Record<string, unknown>;\n\n // Remove additionalProperties constraint to allow subtyping\n if (jsonSchema.type === \"object\") {\n delete jsonSchema.additionalProperties;\n }\n\n return jsonSchema;\n }\n\n // Otherwise assume it's already a JSON Schema\n const jsonSchema = schema as Record<string, unknown>;\n\n // Remove additionalProperties constraint if present\n if (jsonSchema.type === \"object\" && \"additionalProperties\" in jsonSchema) {\n const copy = { ...jsonSchema };\n delete copy.additionalProperties;\n return copy;\n }\n\n return jsonSchema;\n}\n\n/**\n * Binding checker interface\n */\nexport interface BindingChecker {\n /**\n * Check if a set of tools implements the binding with full schema validation.\n *\n * Validates:\n * - Tool name matches (exact or regex)\n * - Input schema: Tool accepts what binder requires (no removals from binder to tool)\n * - Output schema: Tool provides what binder expects (no removals from tool to binder)\n *\n * @param tools - Array of tools with names and schemas\n * @returns Promise<boolean> - true if all tools implement the binding correctly\n */\n isImplementedBy: (tools: ToolWithSchemas[]) => Promise<boolean>;\n}\n\n/**\n * Creates a binding checker with full schema validation using json-schema-diff.\n *\n * This performs strict compatibility checking:\n * - For input schemas: Validates that the tool can accept what the binder requires\n * - For output schemas: Validates that the tool provides what the binder expects\n *\n * @param binderTools - The binding definition to check against\n * @returns A binding checker with an async isImplementedBy method\n *\n * @example\n * ```ts\n * const checker = createBindingChecker(MY_BINDING);\n * const isCompatible = await checker.isImplementedBy(availableTools);\n * ```\n */\nexport function createBindingChecker<TDefinition extends readonly ToolBinder[]>(\n binderTools: TDefinition,\n): BindingChecker {\n return {\n isImplementedBy: async (tools: ToolWithSchemas[]) => {\n for (const binderTool of binderTools) {\n // Find matching tool by name (exact or regex)\n const pattern =\n typeof binderTool.name === \"string\"\n ? new RegExp(`^${binderTool.name}$`)\n : binderTool.name;\n\n const matchedTool = tools.find((t) => pattern.test(t.name));\n\n // Skip optional tools that aren't present\n if (!matchedTool && binderTool.opt) {\n continue;\n }\n\n // Required tool not found\n if (!matchedTool) {\n return false;\n }\n\n // === INPUT SCHEMA VALIDATION ===\n // Tool must accept what binder requires\n // Check: binder (source) -> tool (destination)\n // If removals found, tool doesn't accept something binder requires\n const binderInputSchema = normalizeSchema(binderTool.inputSchema);\n const toolInputSchema = normalizeSchema(matchedTool.inputSchema);\n\n if (binderInputSchema && toolInputSchema) {\n try {\n const inputDiff = await diffSchemas({\n sourceSchema: binderInputSchema,\n destinationSchema: toolInputSchema,\n });\n\n // If something was removed from binder to tool, tool can't accept it\n if (inputDiff.removalsFound) {\n return false;\n }\n } catch (error) {\n // Schema diff failed - consider incompatible\n return false;\n }\n } else if (binderInputSchema && !toolInputSchema) {\n // Binder requires input schema but tool doesn't have one\n return false;\n }\n\n // === OUTPUT SCHEMA VALIDATION ===\n // Tool must provide what binder expects (but can provide more)\n // Check: binder (source) -> tool (destination)\n // If removals found, tool doesn't provide something binder expects\n const binderOutputSchema = normalizeSchema(binderTool.outputSchema);\n const toolOutputSchema = normalizeSchema(matchedTool.outputSchema);\n\n if (binderOutputSchema && toolOutputSchema) {\n try {\n const outputDiff = await diffSchemas({\n sourceSchema: binderOutputSchema,\n destinationSchema: toolOutputSchema,\n });\n\n // If something was removed from binder to tool, tool doesn't provide it\n if (outputDiff.removalsFound) {\n return false;\n }\n } catch (error) {\n // Schema diff failed - consider incompatible\n return false;\n }\n } else if (binderOutputSchema && !toolOutputSchema) {\n // Binder expects output schema but tool doesn't have one\n return false;\n }\n }\n\n return true;\n },\n };\n}\n\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@decocms/bindings",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "tsup",
|
|
7
|
+
"test": "vitest run",
|
|
8
|
+
"test:watch": "vitest",
|
|
9
|
+
"prepublishOnly": "npm run build"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"json-schema-diff": "^1.0.0",
|
|
13
|
+
"zod": "^3.25.76",
|
|
14
|
+
"zod-to-json-schema": "^3.24.4"
|
|
15
|
+
},
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"files": [
|
|
19
|
+
"dist/**/*",
|
|
20
|
+
"src/**/*"
|
|
21
|
+
],
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"source": "./src/index.ts",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"default": "./dist/index.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"tsup": "^8.5.0",
|
|
31
|
+
"typescript": "^5.9.3",
|
|
32
|
+
"vitest": "3.2.4"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=24.0.0"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core Binder Types and Utilities
|
|
3
|
+
*
|
|
4
|
+
* This module provides the core types and utilities for the bindings system.
|
|
5
|
+
* Bindings define standardized interfaces that integrations (MCPs) can implement.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { ZodType } from "zod";
|
|
9
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
10
|
+
import { diffSchemas } from "json-schema-diff";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* ToolBinder defines a single tool within a binding.
|
|
14
|
+
* It specifies the tool name, input/output schemas, and whether it's optional.
|
|
15
|
+
*
|
|
16
|
+
* @template TName - The tool name (can be a string or RegExp for pattern matching)
|
|
17
|
+
* @template TInput - The input type (inferred from inputSchema)
|
|
18
|
+
* @template TReturn - The return type (inferred from outputSchema)
|
|
19
|
+
*/
|
|
20
|
+
export interface ToolBinder<
|
|
21
|
+
TName extends string | RegExp = string,
|
|
22
|
+
// biome-ignore lint/suspicious/noExplicitAny: Generic type parameter
|
|
23
|
+
TInput = any,
|
|
24
|
+
TReturn extends object | null | boolean = object,
|
|
25
|
+
> {
|
|
26
|
+
/** The name of the tool (e.g., "DECO_CHAT_CHANNELS_JOIN") */
|
|
27
|
+
name: TName;
|
|
28
|
+
|
|
29
|
+
/** Zod schema for validating tool input */
|
|
30
|
+
inputSchema: ZodType<TInput>;
|
|
31
|
+
|
|
32
|
+
/** Optional Zod schema for validating tool output */
|
|
33
|
+
outputSchema?: ZodType<TReturn>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Whether this tool is optional in the binding.
|
|
37
|
+
* If true, an implementation doesn't need to provide this tool.
|
|
38
|
+
*/
|
|
39
|
+
opt?: true;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Binder represents a collection of tool definitions that form a binding.
|
|
44
|
+
* A binding is like a TypeScript interface - it defines what tools must be implemented.
|
|
45
|
+
*
|
|
46
|
+
* @template TDefinition - Array of ToolBinder definitions
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* const MY_BINDING = [{
|
|
51
|
+
* name: "MY_TOOL" as const,
|
|
52
|
+
* inputSchema: z.object({ id: z.string() }),
|
|
53
|
+
* outputSchema: z.object({ success: z.boolean() }),
|
|
54
|
+
* }] as const satisfies Binder;
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export type Binder<
|
|
58
|
+
TDefinition extends readonly ToolBinder[] = readonly ToolBinder[],
|
|
59
|
+
> = TDefinition;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Tool with schemas for validation
|
|
63
|
+
*/
|
|
64
|
+
export interface ToolWithSchemas {
|
|
65
|
+
name: string;
|
|
66
|
+
inputSchema?: ZodType<any> | Record<string, unknown>;
|
|
67
|
+
outputSchema?: ZodType<any> | Record<string, unknown>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Converts a schema to JSON Schema format if it's a Zod schema
|
|
72
|
+
*/
|
|
73
|
+
function normalizeSchema(schema: any): Record<string, unknown> | undefined {
|
|
74
|
+
if (!schema) return undefined;
|
|
75
|
+
|
|
76
|
+
// If it's a Zod schema (has _def property), convert it
|
|
77
|
+
if (schema._def) {
|
|
78
|
+
const jsonSchema = zodToJsonSchema(schema, {
|
|
79
|
+
// Don't add additionalProperties: false to allow structural compatibility
|
|
80
|
+
$refStrategy: "none",
|
|
81
|
+
}) as Record<string, unknown>;
|
|
82
|
+
|
|
83
|
+
// Remove additionalProperties constraint to allow subtyping
|
|
84
|
+
if (jsonSchema.type === "object") {
|
|
85
|
+
delete jsonSchema.additionalProperties;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return jsonSchema;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Otherwise assume it's already a JSON Schema
|
|
92
|
+
const jsonSchema = schema as Record<string, unknown>;
|
|
93
|
+
|
|
94
|
+
// Remove additionalProperties constraint if present
|
|
95
|
+
if (jsonSchema.type === "object" && "additionalProperties" in jsonSchema) {
|
|
96
|
+
const copy = { ...jsonSchema };
|
|
97
|
+
delete copy.additionalProperties;
|
|
98
|
+
return copy;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return jsonSchema;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Binding checker interface
|
|
106
|
+
*/
|
|
107
|
+
export interface BindingChecker {
|
|
108
|
+
/**
|
|
109
|
+
* Check if a set of tools implements the binding with full schema validation.
|
|
110
|
+
*
|
|
111
|
+
* Validates:
|
|
112
|
+
* - Tool name matches (exact or regex)
|
|
113
|
+
* - Input schema: Tool accepts what binder requires (no removals from binder to tool)
|
|
114
|
+
* - Output schema: Tool provides what binder expects (no removals from tool to binder)
|
|
115
|
+
*
|
|
116
|
+
* @param tools - Array of tools with names and schemas
|
|
117
|
+
* @returns Promise<boolean> - true if all tools implement the binding correctly
|
|
118
|
+
*/
|
|
119
|
+
isImplementedBy: (tools: ToolWithSchemas[]) => Promise<boolean>;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Creates a binding checker with full schema validation using json-schema-diff.
|
|
124
|
+
*
|
|
125
|
+
* This performs strict compatibility checking:
|
|
126
|
+
* - For input schemas: Validates that the tool can accept what the binder requires
|
|
127
|
+
* - For output schemas: Validates that the tool provides what the binder expects
|
|
128
|
+
*
|
|
129
|
+
* @param binderTools - The binding definition to check against
|
|
130
|
+
* @returns A binding checker with an async isImplementedBy method
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```ts
|
|
134
|
+
* const checker = createBindingChecker(MY_BINDING);
|
|
135
|
+
* const isCompatible = await checker.isImplementedBy(availableTools);
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
export function createBindingChecker<TDefinition extends readonly ToolBinder[]>(
|
|
139
|
+
binderTools: TDefinition,
|
|
140
|
+
): BindingChecker {
|
|
141
|
+
return {
|
|
142
|
+
isImplementedBy: async (tools: ToolWithSchemas[]) => {
|
|
143
|
+
for (const binderTool of binderTools) {
|
|
144
|
+
// Find matching tool by name (exact or regex)
|
|
145
|
+
const pattern =
|
|
146
|
+
typeof binderTool.name === "string"
|
|
147
|
+
? new RegExp(`^${binderTool.name}$`)
|
|
148
|
+
: binderTool.name;
|
|
149
|
+
|
|
150
|
+
const matchedTool = tools.find((t) => pattern.test(t.name));
|
|
151
|
+
|
|
152
|
+
// Skip optional tools that aren't present
|
|
153
|
+
if (!matchedTool && binderTool.opt) {
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Required tool not found
|
|
158
|
+
if (!matchedTool) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// === INPUT SCHEMA VALIDATION ===
|
|
163
|
+
// Tool must accept what binder requires
|
|
164
|
+
// Check: binder (source) -> tool (destination)
|
|
165
|
+
// If removals found, tool doesn't accept something binder requires
|
|
166
|
+
const binderInputSchema = normalizeSchema(binderTool.inputSchema);
|
|
167
|
+
const toolInputSchema = normalizeSchema(matchedTool.inputSchema);
|
|
168
|
+
|
|
169
|
+
if (binderInputSchema && toolInputSchema) {
|
|
170
|
+
try {
|
|
171
|
+
const inputDiff = await diffSchemas({
|
|
172
|
+
sourceSchema: binderInputSchema,
|
|
173
|
+
destinationSchema: toolInputSchema,
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// If something was removed from binder to tool, tool can't accept it
|
|
177
|
+
if (inputDiff.removalsFound) {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
} catch (error) {
|
|
181
|
+
// Schema diff failed - consider incompatible
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
} else if (binderInputSchema && !toolInputSchema) {
|
|
185
|
+
// Binder requires input schema but tool doesn't have one
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// === OUTPUT SCHEMA VALIDATION ===
|
|
190
|
+
// Tool must provide what binder expects (but can provide more)
|
|
191
|
+
// Check: binder (source) -> tool (destination)
|
|
192
|
+
// If removals found, tool doesn't provide something binder expects
|
|
193
|
+
const binderOutputSchema = normalizeSchema(binderTool.outputSchema);
|
|
194
|
+
const toolOutputSchema = normalizeSchema(matchedTool.outputSchema);
|
|
195
|
+
|
|
196
|
+
if (binderOutputSchema && toolOutputSchema) {
|
|
197
|
+
try {
|
|
198
|
+
const outputDiff = await diffSchemas({
|
|
199
|
+
sourceSchema: binderOutputSchema,
|
|
200
|
+
destinationSchema: toolOutputSchema,
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// If something was removed from binder to tool, tool doesn't provide it
|
|
204
|
+
if (outputDiff.removalsFound) {
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
} catch (error) {
|
|
208
|
+
// Schema diff failed - consider incompatible
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
} else if (binderOutputSchema && !toolOutputSchema) {
|
|
212
|
+
// Binder expects output schema but tool doesn't have one
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return true;
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @decocms/bindings
|
|
3
|
+
*
|
|
4
|
+
* Core type definitions for the bindings system.
|
|
5
|
+
* Bindings define standardized interfaces that integrations (MCPs) can implement.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// Re-export core binder types and utilities
|
|
9
|
+
export {
|
|
10
|
+
type ToolBinder,
|
|
11
|
+
type Binder,
|
|
12
|
+
type ToolWithSchemas,
|
|
13
|
+
type BindingChecker,
|
|
14
|
+
createBindingChecker,
|
|
15
|
+
} from "./core/binder";
|
|
16
|
+
|