@decocms/bindings 0.1.6 → 0.2.1-beta.1
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 +16 -16
- package/dist/browser/agents.js +29 -0
- package/dist/browser/agents.js.map +1 -0
- package/dist/browser/chunk-6QEXJ7XW.js +48564 -0
- package/dist/browser/chunk-6QEXJ7XW.js.map +1 -0
- package/dist/browser/chunk-WKNVAFKE.js +2176 -0
- package/dist/browser/chunk-WKNVAFKE.js.map +1 -0
- package/dist/browser/chunk-XWLBKKHZ.js +127 -0
- package/dist/browser/chunk-XWLBKKHZ.js.map +1 -0
- package/dist/browser/chunk-ZX4ZDU2T.js +58 -0
- package/dist/browser/chunk-ZX4ZDU2T.js.map +1 -0
- package/dist/browser/client.js +9 -0
- package/dist/browser/client.js.map +1 -0
- package/dist/browser/collections.js +4 -0
- package/dist/browser/connection.js +8 -0
- package/dist/browser/connection.js.map +1 -0
- package/dist/browser/index.js +10 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/browser/language-model.js +205 -0
- package/dist/browser/language-model.js.map +1 -0
- package/dist/client.d.ts +12 -0
- package/dist/client.js +54 -0
- package/dist/client.js.map +1 -0
- package/dist/{well-known/collections.d.ts → collections.d.ts} +170 -23
- package/dist/{chunk-L7E6ONLJ.js → collections.js} +18 -24
- package/dist/collections.js.map +1 -0
- package/dist/connection.d.ts +30 -0
- package/dist/connection.js +3 -0
- package/dist/connection.js.map +1 -0
- package/dist/index.d.ts +6 -2
- package/dist/index.js +262 -1
- package/dist/index.js.map +1 -1
- package/dist/language-model.d.ts +3228 -0
- package/dist/language-model.js +628 -0
- package/dist/language-model.js.map +1 -0
- package/dist/models.d.ts +2071 -0
- package/dist/models.js +111 -0
- package/dist/models.js.map +1 -0
- package/dist/node/agents.d.ts +903 -0
- package/dist/node/agents.js +27 -0
- package/dist/node/agents.js.map +1 -0
- package/dist/node/chunk-BLCFITZG.js +56 -0
- package/dist/node/chunk-BLCFITZG.js.map +1 -0
- package/dist/node/chunk-QMQMPK7Q.js +50 -0
- package/dist/node/chunk-QMQMPK7Q.js.map +1 -0
- package/dist/node/chunk-QP7AQCEP.js +23478 -0
- package/dist/node/chunk-QP7AQCEP.js.map +1 -0
- package/dist/node/chunk-T2DG7334.js +125 -0
- package/dist/node/chunk-T2DG7334.js.map +1 -0
- package/dist/node/client.d.ts +12 -0
- package/dist/node/client.js +7 -0
- package/dist/node/client.js.map +1 -0
- package/dist/node/collections.d.ts +537 -0
- package/dist/node/collections.js +4 -0
- package/dist/node/collections.js.map +1 -0
- package/dist/node/connection.d.ts +30 -0
- package/dist/node/connection.js +6 -0
- package/dist/node/connection.js.map +1 -0
- package/dist/node/index.d.ts +94 -0
- package/dist/node/index.js +8 -0
- package/dist/node/index.js.map +1 -0
- package/dist/node/language-model.d.ts +2840 -0
- package/dist/node/language-model.js +203 -0
- package/dist/node/language-model.js.map +1 -0
- package/package.json +45 -17
- package/dist/chunk-L7E6ONLJ.js.map +0 -1
- package/dist/well-known/collections.js +0 -3
- package/dist/well-known/models.d.ts +0 -127
- package/dist/well-known/models.js +0 -46
- package/dist/well-known/models.js.map +0 -1
- package/src/core/binder.ts +0 -221
- package/src/index.ts +0 -16
- package/src/well-known/collections.ts +0 -328
- package/src/well-known/models.ts +0 -79
- /package/dist/{well-known → browser}/collections.js.map +0 -0
|
@@ -11,16 +11,7 @@ var BaseCollectionEntitySchema = z.object({
|
|
|
11
11
|
});
|
|
12
12
|
var ComparisonExpressionSchema = z.object({
|
|
13
13
|
field: z.array(z.string()),
|
|
14
|
-
operator: z.enum([
|
|
15
|
-
"eq",
|
|
16
|
-
"gt",
|
|
17
|
-
"gte",
|
|
18
|
-
"lt",
|
|
19
|
-
"lte",
|
|
20
|
-
"in",
|
|
21
|
-
"like",
|
|
22
|
-
"contains"
|
|
23
|
-
]),
|
|
14
|
+
operator: z.enum(["eq", "gt", "gte", "lt", "lte", "in", "like", "contains"]),
|
|
24
15
|
value: z.unknown()
|
|
25
16
|
});
|
|
26
17
|
var WhereExpressionSchema = z.union([
|
|
@@ -58,7 +49,9 @@ function createCollectionGetOutputSchema(entitySchema) {
|
|
|
58
49
|
}
|
|
59
50
|
function createCollectionInsertInputSchema(entitySchema) {
|
|
60
51
|
return z.object({
|
|
61
|
-
data: entitySchema.describe(
|
|
52
|
+
data: entitySchema.describe(
|
|
53
|
+
"Data for the new entity (id may be auto-generated)"
|
|
54
|
+
)
|
|
62
55
|
});
|
|
63
56
|
}
|
|
64
57
|
function createCollectionInsertOutputSchema(entitySchema) {
|
|
@@ -80,21 +73,22 @@ function createCollectionUpdateOutputSchema(entitySchema) {
|
|
|
80
73
|
var CollectionDeleteInputSchema = z.object({
|
|
81
74
|
id: z.string().describe("ID of the entity to delete")
|
|
82
75
|
});
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
});
|
|
76
|
+
function createCollectionDeleteOutputSchema(entitySchema) {
|
|
77
|
+
return z.object({
|
|
78
|
+
item: entitySchema.describe("The deleted entity")
|
|
79
|
+
});
|
|
80
|
+
}
|
|
87
81
|
function createCollectionBindings(collectionName, entitySchema, options) {
|
|
88
82
|
const upperName = collectionName.toUpperCase();
|
|
89
83
|
const readOnly = options?.readOnly ?? false;
|
|
90
84
|
const bindings = [
|
|
91
85
|
{
|
|
92
|
-
name: `
|
|
86
|
+
name: `COLLECTION_${upperName}_LIST`,
|
|
93
87
|
inputSchema: CollectionListInputSchema,
|
|
94
88
|
outputSchema: createCollectionListOutputSchema(entitySchema)
|
|
95
89
|
},
|
|
96
90
|
{
|
|
97
|
-
name: `
|
|
91
|
+
name: `COLLECTION_${upperName}_GET`,
|
|
98
92
|
inputSchema: CollectionGetInputSchema,
|
|
99
93
|
outputSchema: createCollectionGetOutputSchema(entitySchema)
|
|
100
94
|
}
|
|
@@ -102,21 +96,21 @@ function createCollectionBindings(collectionName, entitySchema, options) {
|
|
|
102
96
|
if (!readOnly) {
|
|
103
97
|
bindings.push(
|
|
104
98
|
{
|
|
105
|
-
name: `
|
|
99
|
+
name: `COLLECTION_${upperName}_CREATE`,
|
|
106
100
|
inputSchema: createCollectionInsertInputSchema(entitySchema),
|
|
107
101
|
outputSchema: createCollectionInsertOutputSchema(entitySchema),
|
|
108
102
|
opt: true
|
|
109
103
|
},
|
|
110
104
|
{
|
|
111
|
-
name: `
|
|
105
|
+
name: `COLLECTION_${upperName}_UPDATE`,
|
|
112
106
|
inputSchema: createCollectionUpdateInputSchema(entitySchema),
|
|
113
107
|
outputSchema: createCollectionUpdateOutputSchema(entitySchema),
|
|
114
108
|
opt: true
|
|
115
109
|
},
|
|
116
110
|
{
|
|
117
|
-
name: `
|
|
111
|
+
name: `COLLECTION_${upperName}_DELETE`,
|
|
118
112
|
inputSchema: CollectionDeleteInputSchema,
|
|
119
|
-
outputSchema:
|
|
113
|
+
outputSchema: createCollectionDeleteOutputSchema(entitySchema),
|
|
120
114
|
opt: true
|
|
121
115
|
}
|
|
122
116
|
);
|
|
@@ -124,6 +118,6 @@ function createCollectionBindings(collectionName, entitySchema, options) {
|
|
|
124
118
|
return bindings;
|
|
125
119
|
}
|
|
126
120
|
|
|
127
|
-
export { BaseCollectionEntitySchema, CollectionDeleteInputSchema,
|
|
128
|
-
//# sourceMappingURL=
|
|
129
|
-
//# sourceMappingURL=
|
|
121
|
+
export { BaseCollectionEntitySchema, CollectionDeleteInputSchema, CollectionGetInputSchema, CollectionListInputSchema, OrderByExpressionSchema, WhereExpressionSchema, createCollectionBindings, createCollectionDeleteOutputSchema, createCollectionGetOutputSchema, createCollectionInsertInputSchema, createCollectionInsertOutputSchema, createCollectionListOutputSchema, createCollectionUpdateInputSchema, createCollectionUpdateOutputSchema };
|
|
122
|
+
//# sourceMappingURL=collections.js.map
|
|
123
|
+
//# sourceMappingURL=collections.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/well-known/collections.ts"],"names":[],"mappings":";;;AAsBO,IAAM,0BAAA,GAA6B,EAAE,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AAA;AAAA,MACb,YAAA,EAAc,iCAAiC,YAAY;AAAA,KAC7D;AAAA,IACA;AAAA,MACE,IAAA,EAAM,cAAc,SAAS,CAAA,IAAA,CAAA;AAAA,MAC7B,WAAA,EAAa,wBAAA;AAAA,MACb,YAAA,EAAc,gCAAgC,YAAY;AAAA;AAC5D,GACF;AAGF,EAAA,IAAI,CAAC,QAAA,EAAU;AACb,IAAA,QAAA,CAAS,IAAA;AAAA,MACP;AAAA,QACE,IAAA,EAAM,cAAc,SAAS,CAAA,OAAA,CAAA;AAAA,QAC7B,WAAA,EAAa,kCAAkC,YAAY,CAAA;AAAA,QAC3D,YAAA,EAAc,mCAAmC,YAAY,CAAA;AAAA,QAC7D,GAAA,EAAK;AAAA,OACP;AAAA,MACA;AAAA,QACE,IAAA,EAAM,cAAc,SAAS,CAAA,OAAA,CAAA;AAAA,QAC7B,WAAA,EAAa,kCAAkC,YAAY,CAAA;AAAA,QAC3D,YAAA,EAAc,mCAAmC,YAAY,CAAA;AAAA,QAC7D,GAAA,EAAK;AAAA,OACP;AAAA,MACA;AAAA,QACE,IAAA,EAAM,cAAc,SAAS,CAAA,OAAA,CAAA;AAAA,QAC7B,WAAA,EAAa,2BAAA;AAAA,QACb,YAAA,EAAc,mCAAmC,YAAY,CAAA;AAAA,QAC7D,GAAA,EAAK;AAAA;AACP,KACF;AAAA,EACF;AAEA,EAAA,OAAO,QAAA;AACT","file":"collections.js","sourcesContent":["import { z } from \"zod\";\nimport type { ToolBinder } from \"../core/binder\";\n\n/**\n * Collection Bindings\n *\n * This module provides standardized tool bindings for Collections, representing\n * SQL table-like structures with CRUD + Search operations compatible with TanStack DB.\n *\n * Key Features:\n * - Generic collection bindings that work with any entity type\n * - Standardized tool naming: `COLLECTION_{COLLECTION}_*`\n * - Compatible with TanStack DB query-collection\n * - Full TypeScript support with proper type constraints\n * - Support for filtering, sorting, and pagination\n * - Simple id and title fields for human-readable identification\n */\n\n/**\n * Base schema for collection entities\n * All collection entities must have an id, title, and audit trail fields\n */\nexport const BaseCollectionEntitySchema = z.object({\n id: z.string().describe(\"Unique identifier for the entity\"),\n title: z.string().describe(\"Human-readable title for the entity\"),\n created_at: z.string().datetime(),\n updated_at: z.string().datetime(),\n created_by: z.string().optional(),\n updated_by: z.string().optional(),\n});\n\n/**\n * Type helper for BaseCollectionEntitySchema\n */\nexport type BaseCollectionEntitySchemaType = typeof BaseCollectionEntitySchema;\n\n/**\n * Comparison expression schema for filtering\n */\nconst ComparisonExpressionSchema = z.object({\n field: z.array(z.string()),\n operator: z.enum([\"eq\", \"gt\", \"gte\", \"lt\", \"lte\", \"in\", \"like\", \"contains\"]),\n value: z.unknown(),\n});\n\n/**\n * Where expression schema for filtering\n * Supports TanStack DB predicate push-down patterns\n */\nexport const WhereExpressionSchema = z.union([\n ComparisonExpressionSchema,\n z.object({\n operator: z.enum([\"and\", \"or\", \"not\"]),\n conditions: z.array(ComparisonExpressionSchema),\n }),\n]);\n\n/**\n * Where expression type for filtering\n * Derived from WhereExpressionSchema\n */\nexport type WhereExpression = z.infer<typeof WhereExpressionSchema>;\n\n/**\n * Order by expression for sorting\n */\nexport const OrderByExpressionSchema = z.object({\n field: z.array(z.string()),\n direction: z.enum([\"asc\", \"desc\"]),\n nulls: z.enum([\"first\", \"last\"]).optional(),\n});\n\n/**\n * List/Query input schema for collections\n * Compatible with TanStack DB LoadSubsetOptions\n */\nexport const CollectionListInputSchema = z.object({\n where: WhereExpressionSchema.optional().describe(\"Filter expression\"),\n orderBy: z\n .array(OrderByExpressionSchema)\n .optional()\n .describe(\"Sort expressions\"),\n limit: z\n .number()\n .int()\n .min(1)\n .max(1000)\n .optional()\n .describe(\"Maximum number of items to return\"),\n offset: z\n .number()\n .int()\n .min(0)\n .optional()\n .describe(\"Number of items to skip\"),\n});\n\n/**\n * Factory function to create list output schema for a specific collection type\n */\nexport function createCollectionListOutputSchema<T extends z.ZodTypeAny>(\n entitySchema: T,\n) {\n return z.object({\n items: z.array(entitySchema).describe(\"Array of collection items\"),\n totalCount: z\n .number()\n .int()\n .min(0)\n .optional()\n .describe(\"Total number of matching items (if available)\"),\n hasMore: z\n .boolean()\n .optional()\n .describe(\"Whether there are more items available\"),\n });\n}\n\n/**\n * Get by ID input schema\n */\nexport const CollectionGetInputSchema = z.object({\n id: z.string().describe(\"ID of the entity to retrieve\"),\n});\n\n/**\n * Factory function to create get output schema\n */\nexport function createCollectionGetOutputSchema<T extends z.ZodTypeAny>(\n entitySchema: T,\n) {\n return z.object({\n item: entitySchema\n .nullable()\n .describe(\"The retrieved item, or null if not found\"),\n });\n}\n\n/**\n * Factory function to create insert input schema\n */\nexport function createCollectionInsertInputSchema<T extends z.ZodTypeAny>(\n entitySchema: T,\n) {\n // Remove id field since it may be auto-generated by the server\n return z.object({\n data: entitySchema.describe(\n \"Data for the new entity (id may be auto-generated)\",\n ),\n });\n}\n\n/**\n * Factory function to create insert output schema\n */\nexport function createCollectionInsertOutputSchema<T extends z.ZodTypeAny>(\n entitySchema: T,\n) {\n return z.object({\n item: entitySchema.describe(\"The created entity with generated id\"),\n });\n}\n\n/**\n * Factory function to create update input schema\n */\nexport function createCollectionUpdateInputSchema<T extends z.ZodTypeAny>(\n entitySchema: T,\n) {\n return z.object({\n id: z.string().describe(\"ID of the entity to update\"),\n data: (entitySchema as unknown as z.AnyZodObject)\n .partial()\n .describe(\"Partial entity data to update\"),\n });\n}\n\n/**\n * Factory function to create update output schema\n */\nexport function createCollectionUpdateOutputSchema<T extends z.ZodTypeAny>(\n entitySchema: T,\n) {\n return z.object({\n item: entitySchema.describe(\"The updated entity\"),\n });\n}\n\n/**\n * Delete input schema\n */\nexport const CollectionDeleteInputSchema = z.object({\n id: z.string().describe(\"ID of the entity to delete\"),\n});\n\n/**\n * Factory function to create delete output schema\n */\nexport function createCollectionDeleteOutputSchema<T extends z.ZodTypeAny>(\n entitySchema: T,\n) {\n return z.object({\n item: entitySchema.describe(\"The deleted entity\"),\n });\n}\n\n/**\n * Options for creating collection bindings\n */\nexport interface CollectionBindingOptions {\n /**\n * If true, only LIST and GET operations will be included (read-only collection)\n * @default false\n */\n readOnly?: boolean;\n}\n\n/**\n * Creates generic collection bindings for a specific entity type\n *\n * This function generates standardized tool bindings that work with any collection/table\n * by accepting a custom entity schema and collection name. The bindings provide:\n * - COLLECTION_{NAME}_LIST - Query/search entities with filtering and sorting (required)\n * - COLLECTION_{NAME}_GET - Get a single entity by ID (required)\n * - COLLECTION_{NAME}_CREATE - Create a new entity (optional, excluded if readOnly=true)\n * - COLLECTION_{NAME}_UPDATE - Update an existing entity (optional, excluded if readOnly=true)\n * - COLLECTION_{NAME}_DELETE - Delete an entity (optional, excluded if readOnly=true)\n *\n * @param collectionName - The name of the collection/table (e.g., \"users\", \"products\", \"orders\")\n * @param entitySchema - The Zod schema for the entity type (must extend BaseCollectionEntitySchema)\n * @param options - Optional configuration for the collection bindings\n * @returns Array of tool bindings for Collection CRUD + Query operations\n *\n * @example\n * ```typescript\n * const UserSchema = z.object({\n * id: z.string(),\n * title: z.string(),\n * created_at: z.string().datetime(),\n * updated_at: z.string().datetime(),\n * created_by: z.string().optional(),\n * updated_by: z.string().optional(),\n * email: z.string().email(),\n * });\n *\n * // Full CRUD collection\n * const USER_COLLECTION_BINDING = createCollectionBindings(\"users\", UserSchema);\n *\n * // Read-only collection (only LIST and GET)\n * const READONLY_COLLECTION_BINDING = createCollectionBindings(\"products\", ProductSchema, { readOnly: true });\n * ```\n */\nexport function createCollectionBindings<\n TEntitySchema extends BaseCollectionEntitySchemaType,\n TName extends string,\n>(\n collectionName: TName,\n entitySchema: TEntitySchema,\n options?: CollectionBindingOptions,\n) {\n const upperName = collectionName.toUpperCase() as Uppercase<TName>;\n const readOnly = options?.readOnly ?? false;\n\n const bindings: CollectionBinding<TEntitySchema, Uppercase<TName>>[number][] =\n [\n {\n name: `COLLECTION_${upperName}_LIST` as const,\n inputSchema: CollectionListInputSchema,\n outputSchema: createCollectionListOutputSchema(entitySchema),\n },\n {\n name: `COLLECTION_${upperName}_GET` as const,\n inputSchema: CollectionGetInputSchema,\n outputSchema: createCollectionGetOutputSchema(entitySchema),\n },\n ];\n\n // Only include mutation operations if not read-only\n if (!readOnly) {\n bindings.push(\n {\n name: `COLLECTION_${upperName}_CREATE` as const,\n inputSchema: createCollectionInsertInputSchema(entitySchema),\n outputSchema: createCollectionInsertOutputSchema(entitySchema),\n opt: true,\n },\n {\n name: `COLLECTION_${upperName}_UPDATE` as const,\n inputSchema: createCollectionUpdateInputSchema(entitySchema),\n outputSchema: createCollectionUpdateOutputSchema(entitySchema),\n opt: true,\n },\n {\n name: `COLLECTION_${upperName}_DELETE` as const,\n inputSchema: CollectionDeleteInputSchema,\n outputSchema: createCollectionDeleteOutputSchema(entitySchema),\n opt: true,\n },\n );\n }\n\n return bindings satisfies readonly ToolBinder[];\n}\n\nexport type ReadOnlyCollectionBinding<\n TEntitySchema extends BaseCollectionEntitySchemaType,\n TUpperName extends Uppercase<string> = Uppercase<string>,\n> = [\n {\n name: `COLLECTION_${TUpperName}_LIST`;\n inputSchema: typeof CollectionListInputSchema;\n outputSchema: ReturnType<\n typeof createCollectionListOutputSchema<TEntitySchema>\n >;\n },\n {\n name: `COLLECTION_${TUpperName}_GET`;\n inputSchema: typeof CollectionGetInputSchema;\n outputSchema: ReturnType<\n typeof createCollectionGetOutputSchema<TEntitySchema>\n >;\n },\n];\n/**\n * Type helper to extract the collection binding type\n */\nexport type CollectionBinding<\n TEntitySchema extends BaseCollectionEntitySchemaType,\n TUpperName extends Uppercase<string> = Uppercase<string>,\n> = [\n ...ReadOnlyCollectionBinding<TEntitySchema, TUpperName>,\n {\n name: `COLLECTION_${TUpperName}_CREATE`;\n inputSchema: ReturnType<\n typeof createCollectionInsertInputSchema<TEntitySchema>\n >;\n outputSchema: ReturnType<\n typeof createCollectionInsertOutputSchema<TEntitySchema>\n >;\n opt: true;\n },\n {\n name: `COLLECTION_${TUpperName}_UPDATE`;\n inputSchema: ReturnType<\n typeof createCollectionUpdateInputSchema<TEntitySchema>\n >;\n outputSchema: ReturnType<\n typeof createCollectionUpdateOutputSchema<TEntitySchema>\n >;\n opt: true;\n },\n {\n name: `COLLECTION_${TUpperName}_DELETE`;\n inputSchema: typeof CollectionDeleteInputSchema;\n outputSchema: ReturnType<\n typeof createCollectionDeleteOutputSchema<TEntitySchema>\n >;\n opt: true;\n },\n];\n\n/**\n * Type helper to extract tool names from a collection binding\n */\nexport type CollectionTools<\n TEntitySchema extends BaseCollectionEntitySchemaType,\n TUpperName extends Uppercase<string> = Uppercase<string>,\n> = CollectionBinding<TEntitySchema, TUpperName>[number][\"name\"];\n\n// Export types for TypeScript usage\nexport type CollectionListInput = z.infer<typeof CollectionListInputSchema>;\nexport type CollectionGetInput = z.infer<typeof CollectionGetInputSchema>;\nexport type CollectionDeleteInput = z.infer<typeof CollectionDeleteInputSchema>;\nexport type OrderByExpression = z.infer<typeof OrderByExpressionSchema>;\n\n/**\n * Type helper for list output with generic item type\n */\nexport type CollectionListOutput<T> = {\n items: T[];\n totalCount?: number;\n hasMore?: boolean;\n};\n\n/**\n * Type helper for get output with generic item type\n */\nexport type CollectionGetOutput<T> = {\n item: T | null;\n};\n\n/**\n * Type helper for insert output with generic item type\n */\nexport type CollectionInsertOutput<T> = {\n item: T;\n};\n\n/**\n * Type helper for update output with generic item type\n */\nexport type CollectionUpdateOutput<T> = {\n item: T;\n};\n\n/**\n * Type helper for delete output with generic item type\n */\nexport type CollectionDeleteOutput<T> = {\n item: T;\n};\n\n/**\n * Base collection entity type - inferred from BaseCollectionEntitySchema\n */\nexport type BaseCollectionEntity = z.infer<typeof BaseCollectionEntitySchema>;\n"]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
type SSEConnection = {
|
|
2
|
+
type: "SSE";
|
|
3
|
+
url: string;
|
|
4
|
+
token?: string;
|
|
5
|
+
headers?: Record<string, string>;
|
|
6
|
+
};
|
|
7
|
+
type WebsocketConnection = {
|
|
8
|
+
type: "Websocket";
|
|
9
|
+
url: string;
|
|
10
|
+
token?: string;
|
|
11
|
+
};
|
|
12
|
+
type DecoConnection = {
|
|
13
|
+
type: "Deco";
|
|
14
|
+
tenant: string;
|
|
15
|
+
token?: string;
|
|
16
|
+
};
|
|
17
|
+
type InnateConnection = {
|
|
18
|
+
type: "INNATE";
|
|
19
|
+
name: string;
|
|
20
|
+
workspace?: string;
|
|
21
|
+
};
|
|
22
|
+
type HTTPConnection = {
|
|
23
|
+
type: "HTTP";
|
|
24
|
+
url: string;
|
|
25
|
+
headers?: Record<string, string>;
|
|
26
|
+
token?: string;
|
|
27
|
+
};
|
|
28
|
+
type MCPConnection = SSEConnection | WebsocketConnection | InnateConnection | DecoConnection | HTTPConnection;
|
|
29
|
+
|
|
30
|
+
export type { DecoConnection, HTTPConnection, InnateConnection, MCPConnection, SSEConnection, WebsocketConnection };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"connection.js"}
|
package/dist/index.d.ts
CHANGED
|
@@ -15,13 +15,17 @@ import { ZodType } from 'zod';
|
|
|
15
15
|
* @template TInput - The input type (inferred from inputSchema)
|
|
16
16
|
* @template TReturn - The return type (inferred from outputSchema)
|
|
17
17
|
*/
|
|
18
|
-
interface ToolBinder<TName extends string | RegExp = string, TInput = any, TReturn extends object | null | boolean = object> {
|
|
18
|
+
interface ToolBinder<TName extends string | RegExp = string, TInput = any, TReturn extends object | null | boolean = object, TStreamable extends boolean = boolean> {
|
|
19
19
|
/** The name of the tool (e.g., "DECO_CHAT_CHANNELS_JOIN") */
|
|
20
20
|
name: TName;
|
|
21
21
|
/** Zod schema for validating tool input */
|
|
22
22
|
inputSchema: ZodType<TInput>;
|
|
23
23
|
/** Optional Zod schema for validating tool output */
|
|
24
|
-
outputSchema?: ZodType<TReturn>;
|
|
24
|
+
outputSchema?: TStreamable extends true ? never : ZodType<TReturn>;
|
|
25
|
+
/**
|
|
26
|
+
* Whether this tool is streamable.
|
|
27
|
+
*/
|
|
28
|
+
streamable?: TStreamable;
|
|
25
29
|
/**
|
|
26
30
|
* Whether this tool is optional in the binding.
|
|
27
31
|
* If true, an implementation doesn't need to provide this tool.
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,266 @@
|
|
|
1
|
-
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
2
1
|
import { diffSchemas } from 'json-schema-diff';
|
|
2
|
+
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
3
|
+
import { convertJsonSchemaToZod } from 'zod-from-json-schema';
|
|
4
|
+
import { Client as Client$1 } from '@modelcontextprotocol/sdk/client/index.js';
|
|
5
|
+
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
|
|
6
|
+
import { WebSocketClientTransport } from '@modelcontextprotocol/sdk/client/websocket.js';
|
|
7
|
+
import { ListToolsResultSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
8
|
+
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
9
|
+
|
|
10
|
+
// src/core/binder.ts
|
|
11
|
+
var HTTPClientTransport = class extends StreamableHTTPClientTransport {
|
|
12
|
+
constructor(url, opts) {
|
|
13
|
+
super(url, opts);
|
|
14
|
+
}
|
|
15
|
+
send(message, options) {
|
|
16
|
+
const mockAction = getMockActionFor(message);
|
|
17
|
+
if (mockAction?.type === "emit") {
|
|
18
|
+
this.onmessage?.(mockAction.message);
|
|
19
|
+
return Promise.resolve();
|
|
20
|
+
}
|
|
21
|
+
if (mockAction?.type === "suppress") {
|
|
22
|
+
return Promise.resolve();
|
|
23
|
+
}
|
|
24
|
+
return super.send(message, options);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
function getMockActionFor(message) {
|
|
28
|
+
const m = message;
|
|
29
|
+
if (!m || typeof m !== "object" || !("method" in m)) return null;
|
|
30
|
+
switch (m.method) {
|
|
31
|
+
case "initialize": {
|
|
32
|
+
const protocolVersion = m?.params?.protocolVersion;
|
|
33
|
+
if (!protocolVersion) return null;
|
|
34
|
+
return {
|
|
35
|
+
type: "emit",
|
|
36
|
+
message: {
|
|
37
|
+
result: {
|
|
38
|
+
protocolVersion,
|
|
39
|
+
capabilities: { tools: {} },
|
|
40
|
+
serverInfo: { name: "deco-chat-server", version: "1.0.0" }
|
|
41
|
+
},
|
|
42
|
+
jsonrpc: m.jsonrpc ?? "2.0",
|
|
43
|
+
// @ts-expect-error - id is not typed
|
|
44
|
+
id: m.id
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
case "notifications/roots/list_changed":
|
|
49
|
+
case "notifications/initialized":
|
|
50
|
+
case "notifications/cancelled":
|
|
51
|
+
case "notifications/progress": {
|
|
52
|
+
return { type: "suppress" };
|
|
53
|
+
}
|
|
54
|
+
default:
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// src/core/client/mcp-client.ts
|
|
60
|
+
var Client = class extends Client$1 {
|
|
61
|
+
constructor(_clientInfo, options) {
|
|
62
|
+
super(_clientInfo, options);
|
|
63
|
+
}
|
|
64
|
+
async listTools(params, options) {
|
|
65
|
+
const result = await this.request(
|
|
66
|
+
{ method: "tools/list", params },
|
|
67
|
+
ListToolsResultSchema,
|
|
68
|
+
options
|
|
69
|
+
);
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
var createServerClient = async (mcpServer, signal, extraHeaders) => {
|
|
74
|
+
const transport = createTransport(mcpServer.connection, signal, extraHeaders);
|
|
75
|
+
if (!transport) {
|
|
76
|
+
throw new Error("Unknown MCP connection type");
|
|
77
|
+
}
|
|
78
|
+
const client = new Client({
|
|
79
|
+
name: mcpServer?.name ?? "MCP Client",
|
|
80
|
+
version: "1.0.0"
|
|
81
|
+
});
|
|
82
|
+
await client.connect(transport);
|
|
83
|
+
return {
|
|
84
|
+
client,
|
|
85
|
+
callStreamableTool: (tool, args) => {
|
|
86
|
+
if (mcpServer.connection.type !== "HTTP") {
|
|
87
|
+
throw new Error("HTTP connection required");
|
|
88
|
+
}
|
|
89
|
+
return fetch(mcpServer.connection.url + `/call-tool/${tool}`, {
|
|
90
|
+
method: "POST",
|
|
91
|
+
redirect: "manual",
|
|
92
|
+
body: JSON.stringify(args),
|
|
93
|
+
headers: {
|
|
94
|
+
...extraHeaders,
|
|
95
|
+
Authorization: `Bearer ${mcpServer.connection.token}`
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
var createTransport = (connection, signal, extraHeaders) => {
|
|
102
|
+
if (connection.type === "Websocket") {
|
|
103
|
+
return new WebSocketClientTransport(new URL(connection.url));
|
|
104
|
+
}
|
|
105
|
+
if (connection.type !== "SSE" && connection.type !== "HTTP") {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
const authHeaders = connection.token ? { authorization: `Bearer ${connection.token}` } : {};
|
|
109
|
+
const headers = {
|
|
110
|
+
...authHeaders,
|
|
111
|
+
...extraHeaders ?? {},
|
|
112
|
+
..."headers" in connection ? connection.headers || {} : {}
|
|
113
|
+
};
|
|
114
|
+
if (connection.type === "SSE") {
|
|
115
|
+
const config = {
|
|
116
|
+
requestInit: { headers, signal }
|
|
117
|
+
};
|
|
118
|
+
if (connection.token) {
|
|
119
|
+
config.eventSourceInit = {
|
|
120
|
+
fetch: (req, init) => {
|
|
121
|
+
return fetch(req, {
|
|
122
|
+
...init,
|
|
123
|
+
headers: {
|
|
124
|
+
...headers,
|
|
125
|
+
Accept: "text/event-stream"
|
|
126
|
+
},
|
|
127
|
+
signal
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
return new SSEClientTransport(new URL(connection.url), config);
|
|
133
|
+
}
|
|
134
|
+
return new HTTPClientTransport(new URL(connection.url), {
|
|
135
|
+
requestInit: {
|
|
136
|
+
headers,
|
|
137
|
+
signal,
|
|
138
|
+
// @ts-ignore - this is a valid option for fetch
|
|
139
|
+
credentials: "include"
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
// src/core/client/proxy.ts
|
|
145
|
+
var safeParse = (content) => {
|
|
146
|
+
try {
|
|
147
|
+
return JSON.parse(content);
|
|
148
|
+
} catch {
|
|
149
|
+
return content;
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
var toolsMap = /* @__PURE__ */ new Map();
|
|
153
|
+
function createMCPClientProxy(options) {
|
|
154
|
+
return new Proxy({}, {
|
|
155
|
+
get(_, name) {
|
|
156
|
+
if (name === "toJSON") {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
if (typeof name !== "string") {
|
|
160
|
+
throw new Error("Name must be a string");
|
|
161
|
+
}
|
|
162
|
+
async function callToolFn(args) {
|
|
163
|
+
const debugId = options?.debugId?.();
|
|
164
|
+
const extraHeaders = debugId ? { "x-trace-debug-id": debugId } : void 0;
|
|
165
|
+
const { client, callStreamableTool } = await createServerClient(
|
|
166
|
+
{ connection: options.connection },
|
|
167
|
+
void 0,
|
|
168
|
+
extraHeaders
|
|
169
|
+
);
|
|
170
|
+
if (options?.streamable?.[String(name)]) {
|
|
171
|
+
return callStreamableTool(String(name), args);
|
|
172
|
+
}
|
|
173
|
+
const { structuredContent, isError, content } = await client.callTool(
|
|
174
|
+
{
|
|
175
|
+
name: String(name),
|
|
176
|
+
arguments: args
|
|
177
|
+
},
|
|
178
|
+
void 0,
|
|
179
|
+
{
|
|
180
|
+
timeout: 3e6
|
|
181
|
+
}
|
|
182
|
+
);
|
|
183
|
+
if (isError) {
|
|
184
|
+
const maybeErrorMessage = content?.[0]?.text;
|
|
185
|
+
const error = typeof maybeErrorMessage === "string" ? safeParse(maybeErrorMessage) : null;
|
|
186
|
+
const throwableError = error?.code && typeof options?.getErrorByStatusCode === "function" ? options.getErrorByStatusCode(
|
|
187
|
+
error.code,
|
|
188
|
+
error.message,
|
|
189
|
+
error.traceId
|
|
190
|
+
) : null;
|
|
191
|
+
if (throwableError) {
|
|
192
|
+
throw throwableError;
|
|
193
|
+
}
|
|
194
|
+
throw new Error(
|
|
195
|
+
`Tool ${String(name)} returned an error: ${JSON.stringify(
|
|
196
|
+
structuredContent ?? content
|
|
197
|
+
)}`
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
return structuredContent;
|
|
201
|
+
}
|
|
202
|
+
const listToolsFn = async () => {
|
|
203
|
+
const { client } = await createServerClient({
|
|
204
|
+
connection: options.connection
|
|
205
|
+
});
|
|
206
|
+
const { tools } = await client.listTools();
|
|
207
|
+
return tools;
|
|
208
|
+
};
|
|
209
|
+
async function listToolsOnce() {
|
|
210
|
+
const conn = options.connection;
|
|
211
|
+
const key = JSON.stringify(conn);
|
|
212
|
+
try {
|
|
213
|
+
if (!toolsMap.has(key)) {
|
|
214
|
+
toolsMap.set(key, listToolsFn());
|
|
215
|
+
}
|
|
216
|
+
return await toolsMap.get(key);
|
|
217
|
+
} catch (error) {
|
|
218
|
+
console.error("Failed to list tools", error);
|
|
219
|
+
toolsMap.delete(key);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
callToolFn.asTool = async () => {
|
|
224
|
+
const tools = await listToolsOnce() ?? [];
|
|
225
|
+
const tool = tools.find((t) => t.name === name);
|
|
226
|
+
if (!tool) {
|
|
227
|
+
throw new Error(`Tool ${name} not found`);
|
|
228
|
+
}
|
|
229
|
+
return {
|
|
230
|
+
...tool,
|
|
231
|
+
id: tool.name,
|
|
232
|
+
inputSchema: tool.inputSchema ? convertJsonSchemaToZod(tool.inputSchema) : void 0,
|
|
233
|
+
outputSchema: tool.outputSchema ? convertJsonSchemaToZod(tool.outputSchema) : void 0,
|
|
234
|
+
execute: (input) => {
|
|
235
|
+
return callToolFn(input.context);
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
return callToolFn;
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// src/core/client/mcp.ts
|
|
245
|
+
new Proxy(
|
|
246
|
+
{},
|
|
247
|
+
{
|
|
248
|
+
get(_, name) {
|
|
249
|
+
if (name === "toJSON") {
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
if (name === "forConnection") {
|
|
253
|
+
return (connection) => createMCPFetchStub({
|
|
254
|
+
connection
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
return global[name];
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
);
|
|
261
|
+
function createMCPFetchStub(options) {
|
|
262
|
+
return createMCPClientProxy(options);
|
|
263
|
+
}
|
|
3
264
|
|
|
4
265
|
// src/core/binder.ts
|
|
5
266
|
function normalizeSchema(schema) {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/core/binder.ts"],"names":["jsonSchema"],"mappings":";;;;AAwEA,SAAS,gBAAgB,MAAA,EAAkD;AACzE,EAAA,IAAI,CAAC,QAAQ,OAAO,MAAA;AAGpB,EAAA,IAAI,OAAO,IAAA,EAAM;AACf,IAAA,MAAMA,WAAAA,GAAa,gBAAgB,MAAA,EAAQ;AAAA;AAAA,MAEzC,YAAA,EAAc;AAAA,KACf,CAAA;AAGD,IAAA,IAAIA,WAAAA,CAAW,SAAS,QAAA,EAAU;AAChC,MAAA,OAAOA,WAAAA,CAAW,oBAAA;AAAA,IACpB;AAEA,IAAA,OAAOA,WAAAA;AAAA,EACT;AAGA,EAAA,MAAM,UAAA,GAAa,MAAA;AAGnB,EAAA,IAAI,UAAA,CAAW,IAAA,KAAS,QAAA,IAAY,sBAAA,IAA0B,UAAA,EAAY;AACxE,IAAA,MAAM,IAAA,GAAO,EAAE,GAAG,UAAA,EAAW;AAC7B,IAAA,OAAO,IAAA,CAAK,oBAAA;AACZ,IAAA,OAAO,IAAA;AAAA,EACT;AAEA,EAAA,OAAO,UAAA;AACT;AAoCO,SAAS,qBACd,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AAY,CAAA;AAClE,QAAA,MAAM,gBAAA,GAAmB,eAAA,CAAgB,WAAA,CAAY,YAAY,CAAA;AAEjE,QAAA,IAAI,sBAAsB,gBAAA,EAAkB;AAC1C,UAAA,IAAI;AACF,YAAA,MAAM,UAAA,GAAa,MAAM,WAAA,CAAY;AAAA,cACnC,YAAA,EAAc,kBAAA;AAAA,cACd,iBAAA,EAAmB;AAAA,aACpB,CAAA;AAGD,YAAA,IAAI,WAAW,aAAA,EAAe;AAC5B,cAAA,OAAO,KAAA;AAAA,YACT;AAAA,UACF,SAAS,KAAA,EAAO;AACd,YAAA,OAAA,CAAQ,KAAA,CAAM,sBAAsB,KAAK,CAAA;AAEzC,YAAA,OAAO,KAAA;AAAA,UACT;AAAA,QACF,CAAA,MAAA,IAAW,kBAAA,IAAsB,CAAC,gBAAA,EAAkB;AAElD,UAAA,OAAO,KAAA;AAAA,QACT;AAAA,MACF;AAEA,MAAA,OAAO,IAAA;AAAA,IACT;AAAA,GACF;AACF","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 = 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 console.error(\"Schema diff failed\", 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 console.error(\"Schema diff failed\", 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"]}
|
|
1
|
+
{"version":3,"sources":["../src/core/client/http-client-transport.ts","../src/core/client/mcp-client.ts","../src/core/client/proxy.ts","../src/core/client/mcp.ts","../src/core/binder.ts"],"names":["BaseClient","jsonSchema"],"mappings":";;;;;;;;;;AAMO,IAAM,mBAAA,GAAN,cAAkC,6BAAA,CAA8B;AAAA,EACrE,WAAA,CAAY,KAAU,IAAA,EAA6C;AACjE,IAAA,KAAA,CAAM,KAAK,IAAI,CAAA;AAAA,EACjB;AAAA,EAES,IAAA,CACP,SACA,OAAA,EAIe;AACf,IAAA,MAAM,UAAA,GAAa,iBAAiB,OAAO,CAAA;AAC3C,IAAA,IAAI,UAAA,EAAY,SAAS,MAAA,EAAQ;AAC/B,MAAA,IAAA,CAAK,SAAA,GAAY,WAAW,OAAO,CAAA;AACnC,MAAA,OAAO,QAAQ,OAAA,EAAQ;AAAA,IACzB;AACA,IAAA,IAAI,UAAA,EAAY,SAAS,UAAA,EAAY;AACnC,MAAA,OAAO,QAAQ,OAAA,EAAQ;AAAA,IACzB;AACA,IAAA,OAAO,KAAA,CAAM,IAAA,CAAK,OAAA,EAAS,OAAO,CAAA;AAAA,EACpC;AACF,CAAA;AAMA,SAAS,iBAAiB,OAAA,EAA4C;AACpE,EAAA,MAAM,CAAA,GAAI,OAAA;AACV,EAAA,IAAI,CAAC,KAAK,OAAO,CAAA,KAAM,YAAY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AEzC,YAAA,OAAO,KAAA;AAAA,UACT;AAAA,QACF,CAAA,MAAA,IAAW,kBAAA,IAAsB,CAAC,gBAAA,EAAkB;AAElD,UAAA,OAAO,KAAA;AAAA,QACT;AAAA,MACF;AAEA,MAAA,OAAO,IAAA;AAAA,IACT;AAAA,GACF;AACF","file":"index.js","sourcesContent":["import type { JSONRPCMessage } from \"@modelcontextprotocol/sdk/types.js\";\nimport {\n StreamableHTTPClientTransport,\n type StreamableHTTPClientTransportOptions,\n} from \"@modelcontextprotocol/sdk/client/streamableHttp.js\";\n\nexport class HTTPClientTransport extends StreamableHTTPClientTransport {\n constructor(url: URL, opts?: StreamableHTTPClientTransportOptions) {\n super(url, opts);\n }\n\n override send(\n message: JSONRPCMessage,\n options?: {\n resumptionToken?: string;\n onresumptiontoken?: (token: string) => void;\n },\n ): Promise<void> {\n const mockAction = getMockActionFor(message);\n if (mockAction?.type === \"emit\") {\n this.onmessage?.(mockAction.message);\n return Promise.resolve();\n }\n if (mockAction?.type === \"suppress\") {\n return Promise.resolve();\n }\n return super.send(message, options);\n }\n}\n\ntype MockAction =\n | { type: \"emit\"; message: JSONRPCMessage }\n | { type: \"suppress\" };\n\nfunction getMockActionFor(message: JSONRPCMessage): MockAction | null {\n const m = message;\n if (!m || typeof m !== \"object\" || !(\"method\" in m)) return null;\n\n switch (m.method) {\n case \"initialize\": {\n const protocolVersion = m?.params?.protocolVersion;\n if (!protocolVersion) return null;\n return {\n type: \"emit\",\n message: {\n result: {\n protocolVersion,\n capabilities: { tools: {} },\n serverInfo: { name: \"deco-chat-server\", version: \"1.0.0\" },\n },\n jsonrpc: m.jsonrpc ?? \"2.0\",\n // @ts-expect-error - id is not typed\n id: m.id,\n } as JSONRPCMessage,\n };\n }\n case \"notifications/roots/list_changed\":\n case \"notifications/initialized\":\n case \"notifications/cancelled\":\n case \"notifications/progress\": {\n return { type: \"suppress\" };\n }\n default:\n return null;\n }\n}\n","import {\n Client as BaseClient,\n ClientOptions,\n} from \"@modelcontextprotocol/sdk/client/index.js\";\nimport {\n SSEClientTransport,\n SSEClientTransportOptions,\n} from \"@modelcontextprotocol/sdk/client/sse.js\";\nimport { WebSocketClientTransport } from \"@modelcontextprotocol/sdk/client/websocket.js\";\nimport { RequestOptions } from \"@modelcontextprotocol/sdk/shared/protocol.js\";\nimport {\n Implementation,\n ListToolsRequest,\n ListToolsResultSchema,\n} from \"@modelcontextprotocol/sdk/types.js\";\nimport { MCPConnection } from \"../connection\";\nimport { HTTPClientTransport } from \"./http-client-transport\";\n\n/**\n * WARNNING: This is a hack to prevent schema compilation errors.\n * More info at: https://github.com/modelcontextprotocol/typescript-sdk/issues/923\n *\n * Make sure to keep this updated with the right version of the SDK.\n * https://github.com/modelcontextprotocol/typescript-sdk/blob/bf817939917277a4c59f2e19e7b44b8dd7ff140c/src/client/index.ts#L480\n */\nclass Client extends BaseClient {\n constructor(_clientInfo: Implementation, options?: ClientOptions) {\n super(_clientInfo, options);\n }\n\n override async listTools(\n params?: ListToolsRequest[\"params\"],\n options?: RequestOptions,\n ) {\n const result = await this.request(\n { method: \"tools/list\", params },\n ListToolsResultSchema,\n options,\n );\n\n return result;\n }\n}\n\nexport interface ServerClient {\n client: Client;\n callStreamableTool: (tool: string, args: unknown) => Promise<Response>;\n}\nexport const createServerClient = async (\n mcpServer: { connection: MCPConnection; name?: string },\n signal?: AbortSignal,\n extraHeaders?: Record<string, string>,\n): Promise<ServerClient> => {\n const transport = createTransport(mcpServer.connection, signal, extraHeaders);\n\n if (!transport) {\n throw new Error(\"Unknown MCP connection type\");\n }\n\n const client = new Client({\n name: mcpServer?.name ?? \"MCP Client\",\n version: \"1.0.0\",\n });\n\n await client.connect(transport);\n\n return {\n client,\n callStreamableTool: (tool, args) => {\n if (mcpServer.connection.type !== \"HTTP\") {\n throw new Error(\"HTTP connection required\");\n }\n return fetch(mcpServer.connection.url + `/call-tool/${tool}`, {\n method: \"POST\",\n redirect: \"manual\",\n body: JSON.stringify(args),\n headers: {\n ...extraHeaders,\n Authorization: `Bearer ${mcpServer.connection.token}`,\n },\n });\n },\n };\n};\n\nexport const createTransport = (\n connection: MCPConnection,\n signal?: AbortSignal,\n extraHeaders?: Record<string, string>,\n) => {\n if (connection.type === \"Websocket\") {\n return new WebSocketClientTransport(new URL(connection.url));\n }\n\n if (connection.type !== \"SSE\" && connection.type !== \"HTTP\") {\n return null;\n }\n\n const authHeaders: Record<string, string> = connection.token\n ? { authorization: `Bearer ${connection.token}` }\n : {};\n\n const headers: Record<string, string> = {\n ...authHeaders,\n ...(extraHeaders ?? {}),\n ...(\"headers\" in connection ? connection.headers || {} : {}),\n };\n\n if (connection.type === \"SSE\") {\n const config: SSEClientTransportOptions = {\n requestInit: { headers, signal },\n };\n\n if (connection.token) {\n config.eventSourceInit = {\n fetch: (req, init) => {\n return fetch(req, {\n ...init,\n headers: {\n ...headers,\n Accept: \"text/event-stream\",\n },\n signal,\n });\n },\n };\n }\n\n return new SSEClientTransport(new URL(connection.url), config);\n }\n return new HTTPClientTransport(new URL(connection.url), {\n requestInit: {\n headers,\n signal,\n // @ts-ignore - this is a valid option for fetch\n credentials: \"include\",\n },\n });\n};\n","/* oxlint-disable no-explicit-any */\nimport { convertJsonSchemaToZod } from \"zod-from-json-schema\";\nimport type { CreateStubAPIOptions } from \"./mcp\";\nimport { createServerClient } from \"./mcp-client\";\n\nconst safeParse = (content: string) => {\n try {\n return JSON.parse(content as string);\n } catch {\n return content;\n }\n};\n\nconst toolsMap = new Map<\n string,\n Promise<\n Array<{\n name: string;\n inputSchema: any;\n outputSchema?: any;\n description: string;\n }>\n >\n>();\n\n/**\n * The base fetcher used to fetch the MCP from API.\n */\nexport function createMCPClientProxy<T extends Record<string, unknown>>(\n options: CreateStubAPIOptions,\n): T {\n return new Proxy<T>({} as T, {\n get(_, name) {\n if (name === \"toJSON\") {\n return null;\n }\n if (typeof name !== \"string\") {\n throw new Error(\"Name must be a string\");\n }\n async function callToolFn(args: unknown) {\n const debugId = options?.debugId?.();\n const extraHeaders = debugId\n ? { \"x-trace-debug-id\": debugId }\n : undefined;\n\n const { client, callStreamableTool } = await createServerClient(\n { connection: options.connection },\n undefined,\n extraHeaders,\n );\n\n if (options?.streamable?.[String(name)]) {\n return callStreamableTool(String(name), args);\n }\n\n const { structuredContent, isError, content } = await client.callTool(\n {\n name: String(name),\n arguments: args as Record<string, unknown>,\n },\n undefined,\n {\n timeout: 3000000,\n },\n );\n\n if (isError) {\n const maybeErrorMessage = (content as { text: string }[])?.[0]?.text;\n const error =\n typeof maybeErrorMessage === \"string\"\n ? safeParse(maybeErrorMessage)\n : null;\n\n const throwableError =\n error?.code && typeof options?.getErrorByStatusCode === \"function\"\n ? options.getErrorByStatusCode(\n error.code,\n error.message,\n error.traceId,\n )\n : null;\n\n if (throwableError) {\n throw throwableError;\n }\n\n throw new Error(\n `Tool ${String(name)} returned an error: ${JSON.stringify(\n structuredContent ?? content,\n )}`,\n );\n }\n return structuredContent;\n }\n\n const listToolsFn = async () => {\n const { client } = await createServerClient({\n connection: options.connection,\n });\n const { tools } = await client.listTools();\n\n return tools as {\n name: string;\n inputSchema: any;\n outputSchema?: any;\n description: string;\n }[];\n };\n\n async function listToolsOnce() {\n const conn = options.connection;\n const key = JSON.stringify(conn);\n\n try {\n if (!toolsMap.has(key)) {\n toolsMap.set(key, listToolsFn());\n }\n\n return await toolsMap.get(key)!;\n } catch (error) {\n console.error(\"Failed to list tools\", error);\n\n toolsMap.delete(key);\n return;\n }\n }\n callToolFn.asTool = async () => {\n const tools = (await listToolsOnce()) ?? [];\n const tool = tools.find((t) => t.name === name);\n if (!tool) {\n throw new Error(`Tool ${name} not found`);\n }\n\n return {\n ...tool,\n id: tool.name,\n inputSchema: tool.inputSchema\n ? convertJsonSchemaToZod(tool.inputSchema)\n : undefined,\n outputSchema: tool.outputSchema\n ? convertJsonSchemaToZod(tool.outputSchema)\n : undefined,\n execute: (input: any) => {\n return callToolFn(input.context);\n },\n };\n };\n return callToolFn;\n },\n });\n}\n","/* oxlint-disable no-explicit-any */\nimport { z } from \"zod\";\nimport type { MCPConnection } from \"../connection\";\nimport { createMCPClientProxy } from \"./proxy\";\n\nexport interface FetchOptions extends RequestInit {\n path?: string;\n segments?: string[];\n}\n\n// Default fetcher instance with API_SERVER_URL and API_HEADERS\nexport const MCPClient = new Proxy(\n {} as {\n forConnection: <TDefinition extends readonly ToolBinder[]>(\n connection: MCPConnection,\n ) => MCPClientFetchStub<TDefinition>;\n },\n {\n get(_, name) {\n if (name === \"toJSON\") {\n return null;\n }\n\n if (name === \"forConnection\") {\n return <TDefinition extends readonly ToolBinder[]>(\n connection: MCPConnection,\n ) =>\n createMCPFetchStub<TDefinition>({\n connection,\n });\n }\n return global[name as keyof typeof global];\n },\n },\n);\n\nimport type { ToolBinder } from \"../binder\";\nexport type { ToolBinder };\n\nexport const isStreamableToolBinder = (\n toolBinder: ToolBinder,\n): toolBinder is ToolBinder<string, any, any, true> => {\n return toolBinder.streamable === true;\n};\nexport type MCPClientStub<TDefinition extends readonly ToolBinder[]> = {\n [K in TDefinition[number] as K[\"name\"]]: K extends ToolBinder<\n string,\n infer TInput,\n infer TReturn\n >\n ? (params: TInput, init?: RequestInit) => Promise<TReturn>\n : never;\n};\n\nexport type MCPClientFetchStub<TDefinition extends readonly ToolBinder[]> = {\n [K in TDefinition[number] as K[\"name\"]]: K[\"streamable\"] extends true\n ? K extends ToolBinder<string, infer TInput, any, true>\n ? (params: TInput, init?: RequestInit) => Promise<Response>\n : never\n : K extends ToolBinder<string, infer TInput, infer TReturn, any>\n ? (params: TInput, init?: RequestInit) => Promise<Awaited<TReturn>>\n : never;\n};\n\nexport interface MCPClientRaw {\n callTool: (tool: string, args: unknown) => Promise<unknown>;\n listTools: () => Promise<\n {\n name: string;\n inputSchema: any;\n outputSchema?: any;\n description: string;\n }[]\n >;\n}\nexport type JSONSchemaToZodConverter = (jsonSchema: any) => z.ZodTypeAny;\nexport interface CreateStubAPIOptions {\n connection: MCPConnection;\n streamable?: Record<string, boolean>;\n debugId?: () => string;\n getErrorByStatusCode?: (\n statusCode: number,\n message?: string,\n traceId?: string,\n errorObject?: unknown,\n ) => Error;\n}\n\nexport function createMCPFetchStub<TDefinition extends readonly ToolBinder[]>(\n options: CreateStubAPIOptions,\n): MCPClientFetchStub<TDefinition> {\n return createMCPClientProxy<MCPClientFetchStub<TDefinition>>(options);\n}\n","/**\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 { diffSchemas } from \"json-schema-diff\";\nimport type { ZodType } from \"zod\";\nimport { zodToJsonSchema } from \"zod-to-json-schema\";\nimport { createMCPFetchStub, MCPClientFetchStub } from \"./client/mcp\";\nimport { MCPConnection } from \"./connection\";\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 // eslint-disable-next-line @typescript-eslint/no-explicit-any\n TInput = any,\n TReturn extends object | null | boolean = object,\n TStreamable extends boolean = boolean,\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?: TStreamable extends true ? never : ZodType<TReturn>;\n\n /**\n * Whether this tool is streamable.\n */\n streamable?: TStreamable;\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 // eslint-disable-next-line @typescript-eslint/no-explicit-any\n inputSchema?: ZodType<any> | Record<string, unknown>;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\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 */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\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\nexport const bindingClient = <TDefinition extends readonly ToolBinder[]>(\n binder: TDefinition,\n) => {\n return {\n ...createBindingChecker(binder),\n forConnection: (\n mcpConnection: MCPConnection,\n ): MCPClientFetchStub<TDefinition> => {\n return createMCPFetchStub<TDefinition>({\n connection: mcpConnection,\n streamable: binder.reduce(\n (acc, tool) => {\n acc[tool.name] = tool.streamable === true;\n return acc;\n },\n {} as Record<string, boolean>,\n ),\n });\n },\n };\n};\n\nexport type MCPBindingClient<T extends ReturnType<typeof bindingClient>> =\n ReturnType<T[\"forConnection\"]>;\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 console.error(\"Schema diff failed\", 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 console.error(\"Schema diff failed\", 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"]}
|