@vinkius-core/mcp-fusion 2.6.0 → 2.7.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/client/FusionClient.d.ts +122 -1
- package/dist/client/FusionClient.d.ts.map +1 -1
- package/dist/client/FusionClient.js +173 -11
- package/dist/client/FusionClient.js.map +1 -1
- package/dist/client/index.d.ts +2 -2
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +1 -1
- package/dist/client/index.js.map +1 -1
- package/dist/core/StandardSchema.d.ts +178 -0
- package/dist/core/StandardSchema.d.ts.map +1 -0
- package/dist/core/StandardSchema.js +166 -0
- package/dist/core/StandardSchema.js.map +1 -0
- package/dist/core/createGroup.d.ts +140 -0
- package/dist/core/createGroup.d.ts.map +1 -0
- package/dist/core/createGroup.js +133 -0
- package/dist/core/createGroup.js.map +1 -0
- package/dist/core/execution/ExecutionPipeline.d.ts.map +1 -1
- package/dist/core/execution/ExecutionPipeline.js +6 -2
- package/dist/core/execution/ExecutionPipeline.js.map +1 -1
- package/dist/core/index.d.ts +7 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +6 -0
- package/dist/core/index.js.map +1 -1
- package/dist/core/initFusion.d.ts +201 -0
- package/dist/core/initFusion.d.ts.map +1 -0
- package/dist/core/initFusion.js +134 -0
- package/dist/core/initFusion.js.map +1 -0
- package/dist/core/response.d.ts +49 -2
- package/dist/core/response.d.ts.map +1 -1
- package/dist/core/response.js +27 -5
- package/dist/core/response.js.map +1 -1
- package/dist/index.d.ts +16 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -4
- package/dist/index.js.map +1 -1
- package/dist/presenter/ZodDescriptionExtractor.d.ts +54 -0
- package/dist/presenter/ZodDescriptionExtractor.d.ts.map +1 -0
- package/dist/presenter/ZodDescriptionExtractor.js +131 -0
- package/dist/presenter/ZodDescriptionExtractor.js.map +1 -0
- package/dist/presenter/definePresenter.d.ts +172 -0
- package/dist/presenter/definePresenter.d.ts.map +1 -0
- package/dist/presenter/definePresenter.js +96 -0
- package/dist/presenter/definePresenter.js.map +1 -0
- package/dist/presenter/index.d.ts +3 -0
- package/dist/presenter/index.d.ts.map +1 -1
- package/dist/presenter/index.js +4 -0
- package/dist/presenter/index.js.map +1 -1
- package/dist/server/DevServer.d.ts +96 -0
- package/dist/server/DevServer.d.ts.map +1 -0
- package/dist/server/DevServer.js +187 -0
- package/dist/server/DevServer.js.map +1 -0
- package/dist/server/autoDiscover.d.ts +63 -0
- package/dist/server/autoDiscover.d.ts.map +1 -0
- package/dist/server/autoDiscover.js +157 -0
- package/dist/server/autoDiscover.js.map +1 -0
- package/dist/server/index.d.ts +4 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +4 -0
- package/dist/server/index.js.map +1 -1
- package/dist/state-sync/PolicyValidator.d.ts +36 -0
- package/dist/state-sync/PolicyValidator.d.ts.map +1 -1
- package/dist/state-sync/PolicyValidator.js +35 -0
- package/dist/state-sync/PolicyValidator.js.map +1 -1
- package/dist/state-sync/ResponseDecorator.d.ts.map +1 -1
- package/dist/state-sync/ResponseDecorator.js +2 -1
- package/dist/state-sync/ResponseDecorator.js.map +1 -1
- package/dist/state-sync/StateSyncLayer.d.ts +5 -4
- package/dist/state-sync/StateSyncLayer.d.ts.map +1 -1
- package/dist/state-sync/StateSyncLayer.js +35 -4
- package/dist/state-sync/StateSyncLayer.js.map +1 -1
- package/dist/state-sync/index.d.ts +3 -1
- package/dist/state-sync/index.d.ts.map +1 -1
- package/dist/state-sync/index.js +1 -0
- package/dist/state-sync/index.js.map +1 -1
- package/dist/state-sync/types.d.ts +62 -0
- package/dist/state-sync/types.d.ts.map +1 -1
- package/package.json +38 -1
|
@@ -50,6 +50,84 @@ export interface FusionTransport {
|
|
|
50
50
|
* ```
|
|
51
51
|
*/
|
|
52
52
|
export type RouterMap = Record<string, Record<string, unknown>>;
|
|
53
|
+
/**
|
|
54
|
+
* Client-side middleware for request/response interception.
|
|
55
|
+
*
|
|
56
|
+
* Follows the same onion model as server-side middleware:
|
|
57
|
+
* each middleware wraps the next, forming a pipeline.
|
|
58
|
+
*
|
|
59
|
+
* Use cases: authentication injection, request logging,
|
|
60
|
+
* retry logic, timeout enforcement, response transformation.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const authMiddleware: ClientMiddleware = async (action, args, next) => {
|
|
65
|
+
* const enrichedArgs = { ...args, _token: getToken() };
|
|
66
|
+
* return next(action, enrichedArgs);
|
|
67
|
+
* };
|
|
68
|
+
*
|
|
69
|
+
* const retryMiddleware: ClientMiddleware = async (action, args, next) => {
|
|
70
|
+
* for (let i = 0; i < 3; i++) {
|
|
71
|
+
* const result = await next(action, args);
|
|
72
|
+
* if (!result.isError) return result;
|
|
73
|
+
* }
|
|
74
|
+
* return next(action, args);
|
|
75
|
+
* };
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export type ClientMiddleware = (action: string, args: Record<string, unknown>, next: (action: string, args: Record<string, unknown>) => Promise<ToolResponse>) => Promise<ToolResponse>;
|
|
79
|
+
/**
|
|
80
|
+
* Structured error parsed from a `<tool_error>` XML envelope.
|
|
81
|
+
*
|
|
82
|
+
* Provides typed access to self-healing fields so client code
|
|
83
|
+
* can programmatically react to server errors without regex parsing.
|
|
84
|
+
*/
|
|
85
|
+
export declare class FusionClientError extends Error {
|
|
86
|
+
/** Error code from the `code` attribute (e.g. `'NOT_FOUND'`). */
|
|
87
|
+
readonly code: string;
|
|
88
|
+
/** Recovery suggestion from `<recovery>` element. */
|
|
89
|
+
readonly recovery?: string | undefined;
|
|
90
|
+
/** Available actions from `<available_actions>` children. */
|
|
91
|
+
readonly availableActions: readonly string[];
|
|
92
|
+
/** Error severity from the `severity` attribute. */
|
|
93
|
+
readonly severity: string;
|
|
94
|
+
/** Raw ToolResponse that caused the error. */
|
|
95
|
+
readonly raw: ToolResponse;
|
|
96
|
+
constructor(message: string, code: string, raw: ToolResponse, options?: {
|
|
97
|
+
recovery?: string | undefined;
|
|
98
|
+
availableActions?: string[] | undefined;
|
|
99
|
+
severity?: string | undefined;
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Options for creating a FusionClient.
|
|
104
|
+
*/
|
|
105
|
+
export interface FusionClientOptions {
|
|
106
|
+
/**
|
|
107
|
+
* Client-side middleware pipeline.
|
|
108
|
+
*
|
|
109
|
+
* Middleware execute in registration order (first = outermost).
|
|
110
|
+
* Each middleware can modify the request, response, or both.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const client = createFusionClient<AppRouter>(transport, {
|
|
115
|
+
* middleware: [authMiddleware, loggingMiddleware],
|
|
116
|
+
* });
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
middleware?: ClientMiddleware[];
|
|
120
|
+
/**
|
|
121
|
+
* When `true`, `execute()` throws a {@link FusionClientError}
|
|
122
|
+
* for responses with `isError: true`.
|
|
123
|
+
*
|
|
124
|
+
* When `false` (default), error responses are returned normally
|
|
125
|
+
* and the caller must check `result.isError`.
|
|
126
|
+
*
|
|
127
|
+
* @default false
|
|
128
|
+
*/
|
|
129
|
+
throwOnError?: boolean;
|
|
130
|
+
}
|
|
53
131
|
/**
|
|
54
132
|
* Type-safe client that provides autocomplete and compile-time
|
|
55
133
|
* validation for MCP tool calls.
|
|
@@ -65,6 +143,32 @@ export interface FusionClient<TRouter extends RouterMap> {
|
|
|
65
143
|
* @returns The tool response
|
|
66
144
|
*/
|
|
67
145
|
execute<TAction extends keyof TRouter & string>(action: TAction, args: TRouter[TAction]): Promise<ToolResponse>;
|
|
146
|
+
/**
|
|
147
|
+
* Execute multiple tool actions concurrently.
|
|
148
|
+
*
|
|
149
|
+
* All calls run in parallel via `Promise.all`.
|
|
150
|
+
* Use `{ sequential: true }` for ordered execution.
|
|
151
|
+
*
|
|
152
|
+
* @param calls - Array of `{ action, args }` objects
|
|
153
|
+
* @param options - Optional execution mode
|
|
154
|
+
* @returns Array of tool responses, one per call
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* const results = await client.executeBatch([
|
|
159
|
+
* { action: 'projects.list', args: { workspace_id: 'ws_1' } },
|
|
160
|
+
* { action: 'billing.balance', args: { account_id: 'acc_1' } },
|
|
161
|
+
* ]);
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
executeBatch<TActions extends ReadonlyArray<keyof TRouter & string>>(calls: {
|
|
165
|
+
[K in keyof TActions]: {
|
|
166
|
+
action: TActions[K];
|
|
167
|
+
args: TRouter[TActions[K] & keyof TRouter];
|
|
168
|
+
};
|
|
169
|
+
}, options?: {
|
|
170
|
+
sequential?: boolean | undefined;
|
|
171
|
+
} | undefined): Promise<ToolResponse[]>;
|
|
68
172
|
}
|
|
69
173
|
/**
|
|
70
174
|
* Create a type-safe MCP client.
|
|
@@ -75,6 +179,7 @@ export interface FusionClient<TRouter extends RouterMap> {
|
|
|
75
179
|
*
|
|
76
180
|
* @typeParam TRouter - The router map (use `InferRouter<typeof registry>`)
|
|
77
181
|
* @param transport - The MCP transport layer
|
|
182
|
+
* @param options - Client options (middleware, error handling)
|
|
78
183
|
* @returns A typed {@link FusionClient}
|
|
79
184
|
*
|
|
80
185
|
* @example
|
|
@@ -92,6 +197,22 @@ export interface FusionClient<TRouter extends RouterMap> {
|
|
|
92
197
|
* // TS error: missing required arg 'name'
|
|
93
198
|
* await client.execute('projects.create', {});
|
|
94
199
|
* ```
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```typescript
|
|
203
|
+
* // With client middleware and throwOnError
|
|
204
|
+
* const client = createFusionClient<AppRouter>(transport, {
|
|
205
|
+
* throwOnError: true,
|
|
206
|
+
* middleware: [
|
|
207
|
+
* async (action, args, next) => {
|
|
208
|
+
* console.log(`[Client] calling ${action}`);
|
|
209
|
+
* const result = await next(action, args);
|
|
210
|
+
* console.log(`[Client] ${action} done`);
|
|
211
|
+
* return result;
|
|
212
|
+
* },
|
|
213
|
+
* ],
|
|
214
|
+
* });
|
|
215
|
+
* ```
|
|
95
216
|
*/
|
|
96
|
-
export declare function createFusionClient<TRouter extends RouterMap>(transport: FusionTransport): FusionClient<TRouter>;
|
|
217
|
+
export declare function createFusionClient<TRouter extends RouterMap>(transport: FusionTransport, options?: FusionClientOptions): FusionClient<TRouter>;
|
|
97
218
|
//# sourceMappingURL=FusionClient.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FusionClient.d.ts","sourceRoot":"","sources":["../../src/client/FusionClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAMxD;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC5B,yCAAyC;IACzC,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;CAChF;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"FusionClient.d.ts","sourceRoot":"","sources":["../../src/client/FusionClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAMxD;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC5B,yCAAyC;IACzC,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;CAChF;AAED;;;;;;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AO,CAAC;CAC1B;AAMD;;;;;GAKG;AACH,MAAM,WAAW,YAAY,CAAC,OAAO,SAAS,SAAS;IACnD;;;;;;OAMG;IACH,OAAO,CAAC,OAAO,SAAS,MAAM,OAAO,GAAG,MAAM,EAC1C,MAAM,EAAE,OAAO,EACf,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,GACvB,OAAO,CAAC,YAAY,CAAC,CAAC;IAEzB;;;;;;;;;;;;;;;;;OAiBG;IACH,YAAY,CAAC,QAAQ,SAAS,aAAa,CAAC,MAAM,OAAO,GAAG,MAAM,CAAC,EAC/D,KAAK,EAAE;SAAG,CAAC,IAAI,MAAM,QAAQ,GAAG;YAAE,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;YAAC,IAAI,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,MAAM,OAAO,CAAC,CAAA;SAAE;KAAE,EACrG,OAAO,CAAC,EAAE;QAAE,UAAU,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;KAAE,GAAG,SAAS,GAC3D,OAAO,CAAC,YAAY,EAAE,CAAC,CAAC;CAC9B;AA2GD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,SAAS,SAAS,EACxD,SAAS,EAAE,eAAe,EAC1B,OAAO,CAAC,EAAE,mBAAmB,GAC9B,YAAY,CAAC,OAAO,CAAC,CA6EvB"}
|
|
@@ -27,6 +27,110 @@
|
|
|
27
27
|
*/
|
|
28
28
|
import {} from '../core/response.js';
|
|
29
29
|
// ============================================================================
|
|
30
|
+
// Structured Client Error
|
|
31
|
+
// ============================================================================
|
|
32
|
+
/**
|
|
33
|
+
* Structured error parsed from a `<tool_error>` XML envelope.
|
|
34
|
+
*
|
|
35
|
+
* Provides typed access to self-healing fields so client code
|
|
36
|
+
* can programmatically react to server errors without regex parsing.
|
|
37
|
+
*/
|
|
38
|
+
export class FusionClientError extends Error {
|
|
39
|
+
/** Error code from the `code` attribute (e.g. `'NOT_FOUND'`). */
|
|
40
|
+
code;
|
|
41
|
+
/** Recovery suggestion from `<recovery>` element. */
|
|
42
|
+
recovery;
|
|
43
|
+
/** Available actions from `<available_actions>` children. */
|
|
44
|
+
availableActions;
|
|
45
|
+
/** Error severity from the `severity` attribute. */
|
|
46
|
+
severity;
|
|
47
|
+
/** Raw ToolResponse that caused the error. */
|
|
48
|
+
raw;
|
|
49
|
+
constructor(message, code, raw, options) {
|
|
50
|
+
super(message);
|
|
51
|
+
this.name = 'FusionClientError';
|
|
52
|
+
this.code = code;
|
|
53
|
+
this.raw = raw;
|
|
54
|
+
this.recovery = options?.recovery;
|
|
55
|
+
this.availableActions = Object.freeze(options?.availableActions ?? []);
|
|
56
|
+
this.severity = options?.severity ?? 'error';
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// ============================================================================
|
|
60
|
+
// XML Error Parser (Internal)
|
|
61
|
+
// ============================================================================
|
|
62
|
+
/**
|
|
63
|
+
* Decode XML entities back to their original characters.
|
|
64
|
+
*
|
|
65
|
+
* Reverses the escaping applied by `escapeXml()` and `escapeXmlAttr()`
|
|
66
|
+
* so that parsed error messages are human-readable.
|
|
67
|
+
*
|
|
68
|
+
* @internal
|
|
69
|
+
*/
|
|
70
|
+
function unescapeXml(str) {
|
|
71
|
+
return str
|
|
72
|
+
.replace(/</g, '<')
|
|
73
|
+
.replace(/>/g, '>')
|
|
74
|
+
.replace(/"/g, '"')
|
|
75
|
+
.replace(/'/g, "'")
|
|
76
|
+
.replace(/&/g, '&'); // & must be last to avoid double-decode
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Parse a `<tool_error>` XML envelope into structured fields.
|
|
80
|
+
*
|
|
81
|
+
* Coupled to the output format of `toolError()` from `response.ts`.
|
|
82
|
+
* Uses regex for lightweight parsing — acceptable since the XML is
|
|
83
|
+
* self-produced by the framework, not user-authored.
|
|
84
|
+
*
|
|
85
|
+
* @internal
|
|
86
|
+
*/
|
|
87
|
+
function parseToolErrorXml(text) {
|
|
88
|
+
const codeMatch = text.match(/<tool_error\s[^>]*code="([^"]+)"/);
|
|
89
|
+
const severityMatch = text.match(/<tool_error\s[^>]*severity="([^"]+)"/);
|
|
90
|
+
const messageMatch = text.match(/<message>([\s\S]*?)<\/message>/);
|
|
91
|
+
const recoveryMatch = text.match(/<recovery>([\s\S]*?)<\/recovery>/);
|
|
92
|
+
if (!messageMatch)
|
|
93
|
+
return null;
|
|
94
|
+
const actions = [];
|
|
95
|
+
const actionMatches = text.matchAll(/<action>([\s\S]*?)<\/action>/g);
|
|
96
|
+
for (const m of actionMatches) {
|
|
97
|
+
if (m[1])
|
|
98
|
+
actions.push(unescapeXml(m[1].trim()));
|
|
99
|
+
}
|
|
100
|
+
// Fallback: legacy comma-separated format
|
|
101
|
+
if (actions.length === 0) {
|
|
102
|
+
const legacyMatch = text.match(/<available_actions>([\s\S]*?)<\/available_actions>/);
|
|
103
|
+
if (legacyMatch?.[1]) {
|
|
104
|
+
actions.push(...legacyMatch[1].split(',').map(a => unescapeXml(a.trim())).filter(Boolean));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const recovery = recoveryMatch?.[1] != null ? unescapeXml(recoveryMatch[1].trim()) : undefined;
|
|
108
|
+
const result = {
|
|
109
|
+
code: codeMatch?.[1] != null ? unescapeXml(codeMatch[1]) : 'UNKNOWN',
|
|
110
|
+
message: unescapeXml(messageMatch[1].trim()),
|
|
111
|
+
availableActions: actions,
|
|
112
|
+
severity: severityMatch?.[1] ?? 'error',
|
|
113
|
+
};
|
|
114
|
+
if (recovery !== undefined) {
|
|
115
|
+
result.recovery = recovery;
|
|
116
|
+
}
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
119
|
+
// ============================================================================
|
|
120
|
+
// Middleware Chain Compiler (Internal)
|
|
121
|
+
// ============================================================================
|
|
122
|
+
/** @internal */
|
|
123
|
+
function compileClientMiddleware(middleware, terminal) {
|
|
124
|
+
let chain = terminal;
|
|
125
|
+
// Wrap from right to left: first middleware in array = outermost
|
|
126
|
+
for (let i = middleware.length - 1; i >= 0; i--) {
|
|
127
|
+
const mw = middleware[i];
|
|
128
|
+
const next = chain;
|
|
129
|
+
chain = (action, args) => mw(action, args, next);
|
|
130
|
+
}
|
|
131
|
+
return chain;
|
|
132
|
+
}
|
|
133
|
+
// ============================================================================
|
|
30
134
|
// Factory
|
|
31
135
|
// ============================================================================
|
|
32
136
|
/**
|
|
@@ -38,6 +142,7 @@ import {} from '../core/response.js';
|
|
|
38
142
|
*
|
|
39
143
|
* @typeParam TRouter - The router map (use `InferRouter<typeof registry>`)
|
|
40
144
|
* @param transport - The MCP transport layer
|
|
145
|
+
* @param options - Client options (middleware, error handling)
|
|
41
146
|
* @returns A typed {@link FusionClient}
|
|
42
147
|
*
|
|
43
148
|
* @example
|
|
@@ -55,21 +160,78 @@ import {} from '../core/response.js';
|
|
|
55
160
|
* // TS error: missing required arg 'name'
|
|
56
161
|
* await client.execute('projects.create', {});
|
|
57
162
|
* ```
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```typescript
|
|
166
|
+
* // With client middleware and throwOnError
|
|
167
|
+
* const client = createFusionClient<AppRouter>(transport, {
|
|
168
|
+
* throwOnError: true,
|
|
169
|
+
* middleware: [
|
|
170
|
+
* async (action, args, next) => {
|
|
171
|
+
* console.log(`[Client] calling ${action}`);
|
|
172
|
+
* const result = await next(action, args);
|
|
173
|
+
* console.log(`[Client] ${action} done`);
|
|
174
|
+
* return result;
|
|
175
|
+
* },
|
|
176
|
+
* ],
|
|
177
|
+
* });
|
|
178
|
+
* ```
|
|
58
179
|
*/
|
|
59
|
-
export function createFusionClient(transport) {
|
|
180
|
+
export function createFusionClient(transport, options) {
|
|
181
|
+
const throwOnError = options?.throwOnError ?? false;
|
|
182
|
+
/** Terminal function: builds the MCP call from the dotted action path */
|
|
183
|
+
function terminalCall(action, args) {
|
|
184
|
+
const dotIndex = action.indexOf('.');
|
|
185
|
+
if (dotIndex === -1) {
|
|
186
|
+
return transport.callTool(action, args);
|
|
187
|
+
}
|
|
188
|
+
const toolName = action.substring(0, dotIndex);
|
|
189
|
+
const actionName = action.substring(dotIndex + 1);
|
|
190
|
+
return transport.callTool(toolName, {
|
|
191
|
+
action: actionName,
|
|
192
|
+
...args,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
// Compile middleware chain once at creation time
|
|
196
|
+
const dispatch = (options?.middleware != null && options.middleware.length > 0)
|
|
197
|
+
? compileClientMiddleware(options.middleware, terminalCall)
|
|
198
|
+
: terminalCall;
|
|
199
|
+
/** Post-process: handle throwOnError */
|
|
200
|
+
async function executeInternal(action, args) {
|
|
201
|
+
const result = await dispatch(action, args);
|
|
202
|
+
if (throwOnError && result.isError) {
|
|
203
|
+
const text = result.content
|
|
204
|
+
.map(c => c.text)
|
|
205
|
+
.join('\n');
|
|
206
|
+
const parsed = parseToolErrorXml(text);
|
|
207
|
+
if (parsed) {
|
|
208
|
+
const opts = {
|
|
209
|
+
availableActions: parsed.availableActions,
|
|
210
|
+
severity: parsed.severity,
|
|
211
|
+
};
|
|
212
|
+
if (parsed.recovery !== undefined) {
|
|
213
|
+
opts.recovery = parsed.recovery;
|
|
214
|
+
}
|
|
215
|
+
throw new FusionClientError(parsed.message, parsed.code, result, opts);
|
|
216
|
+
}
|
|
217
|
+
throw new FusionClientError(text || 'Unknown error', 'UNKNOWN', result);
|
|
218
|
+
}
|
|
219
|
+
return result;
|
|
220
|
+
}
|
|
60
221
|
return {
|
|
61
222
|
async execute(action, args) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
223
|
+
return executeInternal(action, args);
|
|
224
|
+
},
|
|
225
|
+
async executeBatch(calls, batchOptions) {
|
|
226
|
+
const items = calls;
|
|
227
|
+
if (batchOptions?.sequential) {
|
|
228
|
+
const results = [];
|
|
229
|
+
for (const call of items) {
|
|
230
|
+
results.push(await executeInternal(call.action, call.args));
|
|
231
|
+
}
|
|
232
|
+
return results;
|
|
66
233
|
}
|
|
67
|
-
|
|
68
|
-
const actionName = action.substring(dotIndex + 1);
|
|
69
|
-
return transport.callTool(toolName, {
|
|
70
|
-
action: actionName,
|
|
71
|
-
...args,
|
|
72
|
-
});
|
|
234
|
+
return Promise.all(items.map(call => executeInternal(call.action, call.args)));
|
|
73
235
|
},
|
|
74
236
|
};
|
|
75
237
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FusionClient.js","sourceRoot":"","sources":["../../src/client/FusionClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,OAAO,EAAqB,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"FusionClient.js","sourceRoot":"","sources":["../../src/client/FusionClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,OAAO,EAAqB,MAAM,qBAAqB,CAAC;AAmExD,+EAA+E;AAC/E,0BAA0B;AAC1B,+EAA+E;AAE/E;;;;;GAKG;AACH,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IACxC,iEAAiE;IACxD,IAAI,CAAS;IACtB,qDAAqD;IAC5C,QAAQ,CAAsB;IACvC,6DAA6D;IACpD,gBAAgB,CAAoB;IAC7C,oDAAoD;IAC3C,QAAQ,CAAS;IAC1B,8CAA8C;IACrC,GAAG,CAAe;IAE3B,YACI,OAAe,EACf,IAAY,EACZ,GAAiB,EACjB,OAIC;QAED,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;QAChC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,QAAQ,GAAG,OAAO,EAAE,QAAQ,CAAC;QAClC,IAAI,CAAC,gBAAgB,GAAG,MAAM,CAAC,MAAM,CAAC,O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AC;YAC3F,IAAI,YAAY,EAAE,UAAU,EAAE,CAAC;gBAC3B,MAAM,OAAO,GAAmB,EAAE,CAAC;gBACnC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;oBACvB,OAAO,CAAC,IAAI,CAAC,MAAM,eAAe,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;gBAChE,CAAC;gBACD,OAAO,OAAO,CAAC;YACnB,CAAC;YAED,OAAO,OAAO,CAAC,GAAG,CACd,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,eAAe,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAC7D,CAAC;QACN,CAAC;KACJ,CAAC;AACN,CAAC"}
|
package/dist/client/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** Client Bounded Context — Barrel Export */
|
|
2
|
-
export { createFusionClient } from './FusionClient.js';
|
|
3
|
-
export type { FusionClient, FusionTransport, RouterMap, } from './FusionClient.js';
|
|
2
|
+
export { createFusionClient, FusionClientError } from './FusionClient.js';
|
|
3
|
+
export type { FusionClient, FusionTransport, RouterMap, ClientMiddleware, FusionClientOptions, } from './FusionClient.js';
|
|
4
4
|
export { createTypedRegistry } from './createTypedRegistry.js';
|
|
5
5
|
export type { InferRouter, TypedToolRegistry } from './InferRouter.js';
|
|
6
6
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAC1E,YAAY,EACR,YAAY,EACZ,eAAe,EACf,SAAS,EACT,gBAAgB,EAChB,mBAAmB,GACtB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,YAAY,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/client/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
/** Client Bounded Context — Barrel Export */
|
|
2
|
-
export { createFusionClient } from './FusionClient.js';
|
|
2
|
+
export { createFusionClient, FusionClientError } from './FusionClient.js';
|
|
3
3
|
export { createTypedRegistry } from './createTypedRegistry.js';
|
|
4
4
|
//# sourceMappingURL=index.js.map
|
package/dist/client/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAQ1E,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC"}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* StandardSchema — Universal Schema Abstraction Layer
|
|
3
|
+
*
|
|
4
|
+
* Decouples the MCP Fusion validation engine from Zod specifically,
|
|
5
|
+
* enabling support for any validator that implements the Standard Schema
|
|
6
|
+
* specification (`@standard-schema/spec`).
|
|
7
|
+
*
|
|
8
|
+
* This allows users to choose lighter alternatives:
|
|
9
|
+
* - **Zod** (~14kb min): Full-featured, most popular
|
|
10
|
+
* - **Valibot** (~1kb min via tree-shaking): Ultra-lightweight
|
|
11
|
+
* - **ArkType** (~5kb min): Fastest runtime validation
|
|
12
|
+
* - **TypeBox** (~4kb min): JSON Schema native
|
|
13
|
+
*
|
|
14
|
+
* ## Standard Schema Spec
|
|
15
|
+
*
|
|
16
|
+
* Any object with `~standard` property conforming to:
|
|
17
|
+
* ```typescript
|
|
18
|
+
* interface StandardSchema {
|
|
19
|
+
* '~standard': {
|
|
20
|
+
* version: 1;
|
|
21
|
+
* vendor: string;
|
|
22
|
+
* validate: (value: unknown) => { value: T } | { issues: Issue[] };
|
|
23
|
+
* }
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @see https://github.com/standard-schema/standard-schema
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* import { toStandardValidator } from '@vinkius-core/mcp-fusion';
|
|
32
|
+
* import * as v from 'valibot';
|
|
33
|
+
*
|
|
34
|
+
* // Valibot schemas work natively via Standard Schema
|
|
35
|
+
* const schema = v.object({ name: v.string(), age: v.number() });
|
|
36
|
+
* const validator = toStandardValidator(schema);
|
|
37
|
+
*
|
|
38
|
+
* const result = validator.validate({ name: 'Alice', age: 30 });
|
|
39
|
+
* // { success: true, data: { name: 'Alice', age: 30 } }
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @module
|
|
43
|
+
*/
|
|
44
|
+
/**
|
|
45
|
+
* Issue reported by a Standard Schema validator.
|
|
46
|
+
*/
|
|
47
|
+
export interface StandardSchemaIssue {
|
|
48
|
+
readonly message: string;
|
|
49
|
+
readonly path?: readonly (string | number | symbol)[];
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Standard Schema v1 spec — the universal validator contract.
|
|
53
|
+
*
|
|
54
|
+
* Any schema library implementing this interface can be used with
|
|
55
|
+
* MCP Fusion's validation pipeline.
|
|
56
|
+
*/
|
|
57
|
+
export interface StandardSchemaV1<TInput = unknown, TOutput = TInput> {
|
|
58
|
+
readonly '~standard': {
|
|
59
|
+
readonly version: 1;
|
|
60
|
+
readonly vendor: string;
|
|
61
|
+
readonly validate: (value: TInput) => {
|
|
62
|
+
readonly value: TOutput;
|
|
63
|
+
} | {
|
|
64
|
+
readonly issues: readonly StandardSchemaIssue[];
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Infer the output type from a Standard Schema.
|
|
70
|
+
*/
|
|
71
|
+
export type InferStandardOutput<T> = T extends StandardSchemaV1<unknown, infer O> ? O : never;
|
|
72
|
+
/**
|
|
73
|
+
* Fusion's internal validation result.
|
|
74
|
+
*/
|
|
75
|
+
export type ValidationResult<T> = {
|
|
76
|
+
readonly success: true;
|
|
77
|
+
readonly data: T;
|
|
78
|
+
} | {
|
|
79
|
+
readonly success: false;
|
|
80
|
+
readonly issues: readonly StandardSchemaIssue[];
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Universal validator interface used internally by MCP Fusion.
|
|
84
|
+
*
|
|
85
|
+
* Wraps any schema library (Zod, Valibot, ArkType, etc.) into
|
|
86
|
+
* a consistent validation contract.
|
|
87
|
+
*/
|
|
88
|
+
export interface FusionValidator<T = unknown> {
|
|
89
|
+
/** Run validation and return a result (never throws) */
|
|
90
|
+
validate(value: unknown): ValidationResult<T>;
|
|
91
|
+
/** Vendor identifier (e.g. 'zod', 'valibot', 'arktype') */
|
|
92
|
+
readonly vendor: string;
|
|
93
|
+
/** Original schema reference (for introspection) */
|
|
94
|
+
readonly schema: unknown;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Create a FusionValidator from a Standard Schema v1 compatible schema.
|
|
98
|
+
*
|
|
99
|
+
* This is the primary entry point for non-Zod validators. Any schema
|
|
100
|
+
* library implementing the Standard Schema spec can be used directly.
|
|
101
|
+
*
|
|
102
|
+
* @param schema - A Standard Schema v1 compatible schema
|
|
103
|
+
* @returns A {@link FusionValidator} wrapping the schema
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* import * as v from 'valibot'; // ~1kb tree-shaken
|
|
108
|
+
*
|
|
109
|
+
* const schema = v.object({ name: v.string() });
|
|
110
|
+
* const validator = toStandardValidator(schema);
|
|
111
|
+
*
|
|
112
|
+
* const ok = validator.validate({ name: 'Alice' });
|
|
113
|
+
* // { success: true, data: { name: 'Alice' } }
|
|
114
|
+
*
|
|
115
|
+
* const err = validator.validate({ name: 42 });
|
|
116
|
+
* // { success: false, issues: [{ message: 'Expected string', path: ['name'] }] }
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
export declare function toStandardValidator<T>(schema: StandardSchemaV1<unknown, T>): FusionValidator<T>;
|
|
120
|
+
/**
|
|
121
|
+
* Create a FusionValidator from a raw Zod schema.
|
|
122
|
+
*
|
|
123
|
+
* This adapter uses Zod's `.safeParse()` method and maps the result
|
|
124
|
+
* to the standard FusionValidator interface.
|
|
125
|
+
*
|
|
126
|
+
* @param schema - A Zod schema (z.object, z.string, etc.)
|
|
127
|
+
* @returns A {@link FusionValidator} wrapping the Zod schema
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```typescript
|
|
131
|
+
* import { z } from 'zod';
|
|
132
|
+
*
|
|
133
|
+
* const schema = z.object({ name: z.string() });
|
|
134
|
+
* const validator = fromZodSchema(schema);
|
|
135
|
+
*
|
|
136
|
+
* const ok = validator.validate({ name: 'Alice' });
|
|
137
|
+
* // { success: true, data: { name: 'Alice' } }
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
export declare function fromZodSchema<T>(schema: ZodSchemaLike<T>): FusionValidator<T>;
|
|
141
|
+
/**
|
|
142
|
+
* Check if a value implements the Standard Schema v1 spec.
|
|
143
|
+
*
|
|
144
|
+
* @param value - Any value to check
|
|
145
|
+
* @returns `true` if the value has a valid `~standard` property
|
|
146
|
+
*/
|
|
147
|
+
export declare function isStandardSchema(value: unknown): value is StandardSchemaV1;
|
|
148
|
+
/**
|
|
149
|
+
* Auto-detect and create a FusionValidator from any supported schema.
|
|
150
|
+
*
|
|
151
|
+
* Detection order:
|
|
152
|
+
* 1. Standard Schema v1 (Valibot, ArkType, etc.)
|
|
153
|
+
* 2. Zod-like (has `.safeParse()`)
|
|
154
|
+
* 3. Throws if unrecognized
|
|
155
|
+
*
|
|
156
|
+
* @param schema - Any supported schema
|
|
157
|
+
* @returns A {@link FusionValidator}
|
|
158
|
+
* @throws If the schema type is not recognized
|
|
159
|
+
*/
|
|
160
|
+
export declare function autoValidator<T = unknown>(schema: unknown): FusionValidator<T>;
|
|
161
|
+
/** Duck-typed Zod schema interface */
|
|
162
|
+
interface ZodSchemaLike<T = unknown> {
|
|
163
|
+
safeParse(value: unknown): {
|
|
164
|
+
success: true;
|
|
165
|
+
data: unknown;
|
|
166
|
+
} | {
|
|
167
|
+
success: false;
|
|
168
|
+
error: {
|
|
169
|
+
issues: Array<{
|
|
170
|
+
message: string;
|
|
171
|
+
path?: (string | number)[];
|
|
172
|
+
}>;
|
|
173
|
+
};
|
|
174
|
+
};
|
|
175
|
+
parse?(value: unknown): T;
|
|
176
|
+
}
|
|
177
|
+
export {};
|
|
178
|
+
//# sourceMappingURL=StandardSchema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"StandardSchema.d.ts","sourceRoot":"","sources":["../../src/core/StandardSchema.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAIH;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAChC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,CAAC,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;CACzD;AAED;;;;;GAKG;AACH,MAAM,WAAW,gBAAgB,CAAC,MAAM,GAAG,OAAO,EAAE,OAAO,GAAG,MAAM;IAChE,QAAQ,CAAC,WAAW,EAAE;QACl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