@defai.digital/mcp-runtime 13.0.3
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/LICENSE +214 -0
- package/dist/cache/index.d.ts +2 -0
- package/dist/cache/index.d.ts.map +1 -0
- package/dist/cache/index.js +2 -0
- package/dist/cache/index.js.map +1 -0
- package/dist/cache/lru-cache.d.ts +97 -0
- package/dist/cache/lru-cache.d.ts.map +1 -0
- package/dist/cache/lru-cache.js +295 -0
- package/dist/cache/lru-cache.js.map +1 -0
- package/dist/guard/index.d.ts +2 -0
- package/dist/guard/index.d.ts.map +1 -0
- package/dist/guard/index.js +6 -0
- package/dist/guard/index.js.map +1 -0
- package/dist/guard/runtime-guard.d.ts +98 -0
- package/dist/guard/runtime-guard.d.ts.map +1 -0
- package/dist/guard/runtime-guard.js +204 -0
- package/dist/guard/runtime-guard.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/response/helpers.d.ts +118 -0
- package/dist/response/helpers.d.ts.map +1 -0
- package/dist/response/helpers.js +235 -0
- package/dist/response/helpers.js.map +1 -0
- package/dist/response/index.d.ts +2 -0
- package/dist/response/index.d.ts.map +1 -0
- package/dist/response/index.js +6 -0
- package/dist/response/index.js.map +1 -0
- package/dist/timeout/index.d.ts +2 -0
- package/dist/timeout/index.d.ts.map +1 -0
- package/dist/timeout/index.js +2 -0
- package/dist/timeout/index.js.map +1 -0
- package/dist/timeout/wrapper.d.ts +52 -0
- package/dist/timeout/wrapper.d.ts.map +1 -0
- package/dist/timeout/wrapper.js +133 -0
- package/dist/timeout/wrapper.js.map +1 -0
- package/dist/validation/index.d.ts +2 -0
- package/dist/validation/index.d.ts.map +1 -0
- package/dist/validation/index.js +2 -0
- package/dist/validation/index.js.map +1 -0
- package/dist/validation/request-validator.d.ts +28 -0
- package/dist/validation/request-validator.d.ts.map +1 -0
- package/dist/validation/request-validator.js +151 -0
- package/dist/validation/request-validator.js.map +1 -0
- package/package.json +41 -0
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { DEFAULT_RESPONSE_LIMITS, isRetryableError, TIMEOUT_HEALTH_CHECK, TIMEOUT_ORCHESTRATION_GRACEFUL, } from '@defai.digital/contracts';
|
|
2
|
+
// ============================================================================
|
|
3
|
+
// Response Helpers
|
|
4
|
+
// ============================================================================
|
|
5
|
+
/**
|
|
6
|
+
* Create a success response.
|
|
7
|
+
*
|
|
8
|
+
* INV-MCP-RESP-001: Uses consistent envelope structure.
|
|
9
|
+
* INV-MCP-RESP-005: Includes duration tracking.
|
|
10
|
+
*/
|
|
11
|
+
export function createSuccessResponse(data, options = {}) {
|
|
12
|
+
const response = {
|
|
13
|
+
success: true,
|
|
14
|
+
data,
|
|
15
|
+
};
|
|
16
|
+
// Add metadata if any tracking info provided
|
|
17
|
+
if (options.startTime !== undefined || options.cached || options.truncated) {
|
|
18
|
+
response.metadata = {
|
|
19
|
+
durationMs: options.startTime ? Date.now() - options.startTime : 0,
|
|
20
|
+
cached: options.cached ?? false,
|
|
21
|
+
truncated: options.truncated ?? false,
|
|
22
|
+
originalSizeBytes: options.originalSizeBytes,
|
|
23
|
+
requestId: options.requestId,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
content: [
|
|
28
|
+
{
|
|
29
|
+
type: 'text',
|
|
30
|
+
text: JSON.stringify(response, null, 2),
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Create an error response.
|
|
37
|
+
*
|
|
38
|
+
* INV-MCP-RESP-001: Uses consistent envelope structure.
|
|
39
|
+
* INV-MCP-RESP-002: Includes error code.
|
|
40
|
+
* INV-MCP-RESP-006: Sets retryable based on error code.
|
|
41
|
+
*/
|
|
42
|
+
export function createErrorResponse(code, message, options = {}) {
|
|
43
|
+
const error = {
|
|
44
|
+
code,
|
|
45
|
+
message,
|
|
46
|
+
context: options.context,
|
|
47
|
+
retryable: isRetryableError(code),
|
|
48
|
+
retryAfterMs: options.retryAfterMs,
|
|
49
|
+
};
|
|
50
|
+
const response = {
|
|
51
|
+
success: false,
|
|
52
|
+
error,
|
|
53
|
+
};
|
|
54
|
+
// Add metadata
|
|
55
|
+
if (options.startTime !== undefined || options.requestId) {
|
|
56
|
+
response.metadata = {
|
|
57
|
+
durationMs: options.startTime ? Date.now() - options.startTime : 0,
|
|
58
|
+
cached: false,
|
|
59
|
+
truncated: false,
|
|
60
|
+
requestId: options.requestId,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
content: [
|
|
65
|
+
{
|
|
66
|
+
type: 'text',
|
|
67
|
+
text: JSON.stringify(response, null, 2),
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
isError: true,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Create a list response with pagination.
|
|
75
|
+
*
|
|
76
|
+
* INV-MCP-RESP-003: Enforces list item limits.
|
|
77
|
+
*/
|
|
78
|
+
export function createListResponse(items, options = {}) {
|
|
79
|
+
const limits = options.limits ?? DEFAULT_RESPONSE_LIMITS;
|
|
80
|
+
const limit = Math.min(options.limit ?? limits.maxListItems, limits.maxListItems);
|
|
81
|
+
const offset = options.offset ?? 0;
|
|
82
|
+
const total = options.total ?? items.length;
|
|
83
|
+
// Apply limit
|
|
84
|
+
const limitedItems = items.slice(0, limit);
|
|
85
|
+
const truncated = items.length > limit;
|
|
86
|
+
const pagination = {
|
|
87
|
+
total,
|
|
88
|
+
limit,
|
|
89
|
+
offset,
|
|
90
|
+
hasMore: offset + limitedItems.length < total,
|
|
91
|
+
};
|
|
92
|
+
const data = {
|
|
93
|
+
items: limitedItems,
|
|
94
|
+
pagination,
|
|
95
|
+
};
|
|
96
|
+
const successOptions = { truncated };
|
|
97
|
+
if (options.startTime !== undefined) {
|
|
98
|
+
successOptions.startTime = options.startTime;
|
|
99
|
+
}
|
|
100
|
+
if (options.requestId !== undefined) {
|
|
101
|
+
successOptions.requestId = options.requestId;
|
|
102
|
+
}
|
|
103
|
+
return createSuccessResponse(data, successOptions);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Create a not found error response.
|
|
107
|
+
*/
|
|
108
|
+
export function createNotFoundResponse(resourceType, resourceId, options = {}) {
|
|
109
|
+
const errorOptions = { context: { resourceType, resourceId } };
|
|
110
|
+
if (options.startTime !== undefined) {
|
|
111
|
+
errorOptions.startTime = options.startTime;
|
|
112
|
+
}
|
|
113
|
+
if (options.requestId !== undefined) {
|
|
114
|
+
errorOptions.requestId = options.requestId;
|
|
115
|
+
}
|
|
116
|
+
return createErrorResponse('NOT_FOUND', `${resourceType} not found: ${resourceId}`, errorOptions);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Create a validation error response.
|
|
120
|
+
*/
|
|
121
|
+
export function createValidationErrorResponse(message, errors, options = {}) {
|
|
122
|
+
const errorOptions = { context: { validationErrors: errors } };
|
|
123
|
+
if (options.startTime !== undefined) {
|
|
124
|
+
errorOptions.startTime = options.startTime;
|
|
125
|
+
}
|
|
126
|
+
if (options.requestId !== undefined) {
|
|
127
|
+
errorOptions.requestId = options.requestId;
|
|
128
|
+
}
|
|
129
|
+
return createErrorResponse('INVALID_INPUT', message, errorOptions);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Create a timeout error response.
|
|
133
|
+
*/
|
|
134
|
+
export function createTimeoutResponse(timeoutMs, options = {}) {
|
|
135
|
+
const errorOptions = {
|
|
136
|
+
context: { timeoutMs },
|
|
137
|
+
retryAfterMs: Math.min(timeoutMs, TIMEOUT_HEALTH_CHECK),
|
|
138
|
+
};
|
|
139
|
+
if (options.startTime !== undefined) {
|
|
140
|
+
errorOptions.startTime = options.startTime;
|
|
141
|
+
}
|
|
142
|
+
if (options.requestId !== undefined) {
|
|
143
|
+
errorOptions.requestId = options.requestId;
|
|
144
|
+
}
|
|
145
|
+
return createErrorResponse('TOOL_TIMEOUT', `Operation timed out after ${timeoutMs}ms`, errorOptions);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Create a memory pressure error response.
|
|
149
|
+
*/
|
|
150
|
+
export function createMemoryPressureResponse(pressureLevel, options = {}) {
|
|
151
|
+
const errorOptions = {
|
|
152
|
+
context: { pressureLevel },
|
|
153
|
+
retryAfterMs: TIMEOUT_ORCHESTRATION_GRACEFUL,
|
|
154
|
+
};
|
|
155
|
+
if (options.startTime !== undefined) {
|
|
156
|
+
errorOptions.startTime = options.startTime;
|
|
157
|
+
}
|
|
158
|
+
if (options.requestId !== undefined) {
|
|
159
|
+
errorOptions.requestId = options.requestId;
|
|
160
|
+
}
|
|
161
|
+
return createErrorResponse('MEMORY_PRESSURE', `Operation rejected due to ${pressureLevel} memory pressure`, errorOptions);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Create an internal error response.
|
|
165
|
+
*/
|
|
166
|
+
export function createInternalErrorResponse(error, options = {}) {
|
|
167
|
+
const errorOptions = {
|
|
168
|
+
context: {
|
|
169
|
+
errorName: error.name,
|
|
170
|
+
// Don't include stack in production
|
|
171
|
+
...(process.env.NODE_ENV === 'development' && { stack: error.stack }),
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
if (options.startTime !== undefined) {
|
|
175
|
+
errorOptions.startTime = options.startTime;
|
|
176
|
+
}
|
|
177
|
+
if (options.requestId !== undefined) {
|
|
178
|
+
errorOptions.requestId = options.requestId;
|
|
179
|
+
}
|
|
180
|
+
return createErrorResponse('INTERNAL_ERROR', error.message, errorOptions);
|
|
181
|
+
}
|
|
182
|
+
// ============================================================================
|
|
183
|
+
// Truncation Helpers
|
|
184
|
+
// ============================================================================
|
|
185
|
+
/**
|
|
186
|
+
* Truncate a string to max length.
|
|
187
|
+
*
|
|
188
|
+
* INV-MCP-RESP-003: Enforces string length limits.
|
|
189
|
+
* INV-MCP-RESP-004: Adds truncation indicator.
|
|
190
|
+
*/
|
|
191
|
+
export function truncateString(str, maxLength, suffix = '... [truncated]') {
|
|
192
|
+
if (str.length <= maxLength) {
|
|
193
|
+
return { text: str, truncated: false };
|
|
194
|
+
}
|
|
195
|
+
const truncatedLength = maxLength - suffix.length;
|
|
196
|
+
return {
|
|
197
|
+
text: str.slice(0, truncatedLength) + suffix,
|
|
198
|
+
truncated: true,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Truncate response data to fit within size limits.
|
|
203
|
+
*
|
|
204
|
+
* INV-MCP-RESP-003: Enforces response size limits.
|
|
205
|
+
* INV-MCP-RESP-004: Sets truncated flag.
|
|
206
|
+
*/
|
|
207
|
+
export function truncateResponse(data, limits = DEFAULT_RESPONSE_LIMITS) {
|
|
208
|
+
const serialized = JSON.stringify(data);
|
|
209
|
+
const originalSizeBytes = serialized.length * 2; // Approximate UTF-8 size
|
|
210
|
+
if (originalSizeBytes <= limits.maxResponseBytes) {
|
|
211
|
+
return { data, truncated: false, originalSizeBytes };
|
|
212
|
+
}
|
|
213
|
+
// For arrays, reduce items
|
|
214
|
+
if (Array.isArray(data)) {
|
|
215
|
+
const targetItems = Math.floor((limits.maxResponseBytes / originalSizeBytes) * data.length * 0.8);
|
|
216
|
+
return {
|
|
217
|
+
data: data.slice(0, Math.max(1, targetItems)),
|
|
218
|
+
truncated: true,
|
|
219
|
+
originalSizeBytes,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
// For strings, truncate directly
|
|
223
|
+
if (typeof data === 'string') {
|
|
224
|
+
const result = truncateString(data, Math.floor(limits.maxResponseBytes / 2), limits.truncationSuffix);
|
|
225
|
+
return {
|
|
226
|
+
data: result.text,
|
|
227
|
+
truncated: result.truncated,
|
|
228
|
+
originalSizeBytes,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
// For objects, try to reduce nested arrays/strings
|
|
232
|
+
// This is a simplified approach - complex objects may need custom handling
|
|
233
|
+
return { data, truncated: false, originalSizeBytes };
|
|
234
|
+
}
|
|
235
|
+
//# sourceMappingURL=helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../src/response/helpers.ts"],"names":[],"mappings":"AAOA,OAAO,EACL,uBAAuB,EACvB,gBAAgB,EAChB,oBAAoB,EACpB,8BAA8B,GAC/B,MAAM,0BAA0B,CAAC;AAgClC,+EAA+E;AAC/E,mBAAmB;AACnB,+EAA+E;AAE/E;;;;;GAKG;AACH,MAAM,UAAU,qBAAqB,CACnC,IAAO,EACP,UAMI,EAAE;IAEN,MAAM,QAAQ,GAA2B;QACvC,OAAO,EAAE,IAAI;QACb,IAAI;KACL,CAAC;IAEF,6CAA6C;IAC7C,IAAI,OAAO,CAAC,SAAS,KAAK,SAAS,IAAI,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;QAC3E,QAAQ,CAAC,QAAQ,GAAG;YAClB,UAAU,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;YAClE,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,KAAK;YAC/B,SAAS,EAAE,OAAO,CAAC,SAAS,IAAI,KAAK;YACrC,iBAAiB,EAAE,OAAO,CAAC,iBAAiB;YAC5C,SAAS,EAAE,OAAO,CAAC,SAAS;SAC7B,CAAC;IACJ,CAAC;IAED,OAAO;QACL,OAAO,EAAE;YACP;gBACE,IAAI,EAAE,MAAM;gBACZ,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;aACxC;SACF;KACF,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,mBAAmB,CACjC,IAAkB,EAClB,OAAe,EACf,UAKI,EAAE;IAEN,MAAM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mD;IACnD,2EAA2E;IAC3E,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,iBAAiB,EAAE,CAAC;AACvD,CAAC"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { type MCPToolResult, type SuccessResponseData, type ErrorResponseData, type ResponseData, createSuccessResponse, createErrorResponse, createListResponse, createNotFoundResponse, createValidationErrorResponse, createTimeoutResponse, createMemoryPressureResponse, createInternalErrorResponse, truncateString, truncateResponse, } from './helpers.js';
|
|
2
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/response/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,aAAa,EAClB,KAAK,mBAAmB,EACxB,KAAK,iBAAiB,EACtB,KAAK,YAAY,EAGjB,qBAAqB,EACrB,mBAAmB,EACnB,kBAAkB,EAClB,sBAAsB,EACtB,6BAA6B,EAC7B,qBAAqB,EACrB,4BAA4B,EAC5B,2BAA2B,EAG3B,cAAc,EACd,gBAAgB,GACjB,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export {
|
|
2
|
+
// Response helpers
|
|
3
|
+
createSuccessResponse, createErrorResponse, createListResponse, createNotFoundResponse, createValidationErrorResponse, createTimeoutResponse, createMemoryPressureResponse, createInternalErrorResponse,
|
|
4
|
+
// Truncation helpers
|
|
5
|
+
truncateString, truncateResponse, } from './helpers.js';
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/response/index.ts"],"names":[],"mappings":"AAAA,OAAO;AAOL,mBAAmB;AACnB,qBAAqB,EACrB,mBAAmB,EACnB,kBAAkB,EAClB,sBAAsB,EACtB,6BAA6B,EAC7B,qBAAqB,EACrB,4BAA4B,EAC5B,2BAA2B;AAE3B,qBAAqB;AACrB,cAAc,EACd,gBAAgB,GACjB,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/timeout/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sBAAsB,EACtB,YAAY,EACZ,WAAW,EACX,eAAe,EACf,oBAAoB,EACpB,eAAe,EACf,eAAe,EACf,eAAe,EACf,YAAY,GACb,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/timeout/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sBAAsB,EACtB,YAAY,EACZ,WAAW,EACX,eAAe,EACf,oBAAoB,EACpB,eAAe,EACf,eAAe,EACf,eAAe,EACf,YAAY,GACb,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { MCPTimeoutConfig, TimeoutResult, ToolCategory } from '@defai.digital/contracts';
|
|
2
|
+
/**
|
|
3
|
+
* Default timeout configuration.
|
|
4
|
+
*/
|
|
5
|
+
export declare const DEFAULT_TIMEOUT_CONFIG: MCPTimeoutConfig;
|
|
6
|
+
/**
|
|
7
|
+
* Timeout error class for identification.
|
|
8
|
+
*/
|
|
9
|
+
export declare class TimeoutError extends Error {
|
|
10
|
+
readonly code = "TOOL_TIMEOUT";
|
|
11
|
+
readonly timeoutMs: number;
|
|
12
|
+
constructor(timeoutMs: number, message?: string);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Wrap an async operation with timeout protection.
|
|
16
|
+
*
|
|
17
|
+
* Invariants enforced:
|
|
18
|
+
* - INV-MCP-TIMEOUT-001: Guaranteed termination
|
|
19
|
+
* - INV-MCP-TIMEOUT-004: Returns TOOL_TIMEOUT error code
|
|
20
|
+
* - INV-MCP-TIMEOUT-005: Duration tracking
|
|
21
|
+
*/
|
|
22
|
+
export declare function withTimeout<T>(operation: () => Promise<T>, timeoutMs: number): Promise<TimeoutResult>;
|
|
23
|
+
/**
|
|
24
|
+
* Wrap a tool handler with timeout protection.
|
|
25
|
+
*
|
|
26
|
+
* Invariants enforced:
|
|
27
|
+
* - INV-MCP-TIMEOUT-002: Category consistency
|
|
28
|
+
* - INV-MCP-TIMEOUT-003: Override precedence
|
|
29
|
+
*/
|
|
30
|
+
export declare function withToolTimeout<TArgs, TResult>(toolName: string, handler: (args: TArgs) => Promise<TResult>, config?: MCPTimeoutConfig): (args: TArgs) => Promise<TimeoutResult>;
|
|
31
|
+
/**
|
|
32
|
+
* Create a timeout-wrapped handler factory.
|
|
33
|
+
*/
|
|
34
|
+
export declare function createTimeoutWrapper(config?: MCPTimeoutConfig): <TArgs, TResult>(toolName: string, handler: (args: TArgs) => Promise<TResult>) => (args: TArgs) => Promise<TimeoutResult>;
|
|
35
|
+
/**
|
|
36
|
+
* Get the category for a tool.
|
|
37
|
+
*/
|
|
38
|
+
export declare function getToolCategory(toolName: string): ToolCategory | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Check if a result is a timeout.
|
|
41
|
+
*/
|
|
42
|
+
export declare function isTimeoutResult(result: TimeoutResult): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Check if a result is successful.
|
|
45
|
+
*/
|
|
46
|
+
export declare function isSuccessResult(result: TimeoutResult): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Extract the result value from a successful TimeoutResult.
|
|
49
|
+
* Throws if result is not successful.
|
|
50
|
+
*/
|
|
51
|
+
export declare function unwrapResult<T>(result: TimeoutResult): T;
|
|
52
|
+
//# sourceMappingURL=wrapper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wrapper.d.ts","sourceRoot":"","sources":["../../src/timeout/wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAChB,aAAa,EACb,YAAY,EACb,MAAM,0BAA0B,CAAC;AAGlC;;GAEG;AACH,eAAO,MAAM,sBAA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_CATEGORIES, getToolTimeout } from '@defai.digital/contracts';
|
|
2
|
+
/**
|
|
3
|
+
* Default timeout configuration.
|
|
4
|
+
*/
|
|
5
|
+
export const DEFAULT_TIMEOUT_CONFIG = {
|
|
6
|
+
defaultTimeoutMs: 30_000,
|
|
7
|
+
toolTimeouts: {
|
|
8
|
+
query: 10_000, // 10 seconds
|
|
9
|
+
mutation: 30_000, // 30 seconds
|
|
10
|
+
scan: 120_000, // 2 minutes
|
|
11
|
+
execution: 1_200_000, // 20 minutes
|
|
12
|
+
},
|
|
13
|
+
toolOverrides: {},
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Timeout error class for identification.
|
|
17
|
+
*/
|
|
18
|
+
export class TimeoutError extends Error {
|
|
19
|
+
code = 'TOOL_TIMEOUT';
|
|
20
|
+
timeoutMs;
|
|
21
|
+
constructor(timeoutMs, message) {
|
|
22
|
+
super(message ?? `Operation timed out after ${timeoutMs}ms`);
|
|
23
|
+
this.name = 'TimeoutError';
|
|
24
|
+
this.timeoutMs = timeoutMs;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Wrap an async operation with timeout protection.
|
|
29
|
+
*
|
|
30
|
+
* Invariants enforced:
|
|
31
|
+
* - INV-MCP-TIMEOUT-001: Guaranteed termination
|
|
32
|
+
* - INV-MCP-TIMEOUT-004: Returns TOOL_TIMEOUT error code
|
|
33
|
+
* - INV-MCP-TIMEOUT-005: Duration tracking
|
|
34
|
+
*/
|
|
35
|
+
export async function withTimeout(operation, timeoutMs) {
|
|
36
|
+
const startTime = Date.now();
|
|
37
|
+
// Create timeout promise
|
|
38
|
+
let timeoutId;
|
|
39
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
40
|
+
timeoutId = setTimeout(() => {
|
|
41
|
+
reject(new TimeoutError(timeoutMs));
|
|
42
|
+
}, timeoutMs);
|
|
43
|
+
});
|
|
44
|
+
try {
|
|
45
|
+
// Race between operation and timeout
|
|
46
|
+
const result = await Promise.race([operation(), timeoutPromise]);
|
|
47
|
+
// Clear timeout if operation completed first
|
|
48
|
+
if (timeoutId)
|
|
49
|
+
clearTimeout(timeoutId);
|
|
50
|
+
return {
|
|
51
|
+
status: 'completed',
|
|
52
|
+
result,
|
|
53
|
+
durationMs: Date.now() - startTime,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
// Clear timeout
|
|
58
|
+
if (timeoutId)
|
|
59
|
+
clearTimeout(timeoutId);
|
|
60
|
+
const durationMs = Date.now() - startTime;
|
|
61
|
+
// Check if it was a timeout
|
|
62
|
+
if (error instanceof TimeoutError) {
|
|
63
|
+
return {
|
|
64
|
+
status: 'timeout',
|
|
65
|
+
timeoutMs: error.timeoutMs,
|
|
66
|
+
durationMs,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
// Other error
|
|
70
|
+
return {
|
|
71
|
+
status: 'error',
|
|
72
|
+
error: {
|
|
73
|
+
code: error instanceof Error ? error.name : 'UNKNOWN_ERROR',
|
|
74
|
+
message: error instanceof Error ? error.message : String(error),
|
|
75
|
+
stack: error instanceof Error ? error.stack : undefined,
|
|
76
|
+
},
|
|
77
|
+
durationMs,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Wrap a tool handler with timeout protection.
|
|
83
|
+
*
|
|
84
|
+
* Invariants enforced:
|
|
85
|
+
* - INV-MCP-TIMEOUT-002: Category consistency
|
|
86
|
+
* - INV-MCP-TIMEOUT-003: Override precedence
|
|
87
|
+
*/
|
|
88
|
+
export function withToolTimeout(toolName, handler, config = DEFAULT_TIMEOUT_CONFIG) {
|
|
89
|
+
const timeoutMs = getToolTimeout(toolName, config);
|
|
90
|
+
return async (args) => {
|
|
91
|
+
return withTimeout(() => handler(args), timeoutMs);
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Create a timeout-wrapped handler factory.
|
|
96
|
+
*/
|
|
97
|
+
export function createTimeoutWrapper(config = DEFAULT_TIMEOUT_CONFIG) {
|
|
98
|
+
return function wrapHandler(toolName, handler) {
|
|
99
|
+
return withToolTimeout(toolName, handler, config);
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Get the category for a tool.
|
|
104
|
+
*/
|
|
105
|
+
export function getToolCategory(toolName) {
|
|
106
|
+
return TOOL_CATEGORIES[toolName];
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Check if a result is a timeout.
|
|
110
|
+
*/
|
|
111
|
+
export function isTimeoutResult(result) {
|
|
112
|
+
return result.status === 'timeout';
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Check if a result is successful.
|
|
116
|
+
*/
|
|
117
|
+
export function isSuccessResult(result) {
|
|
118
|
+
return result.status === 'completed';
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Extract the result value from a successful TimeoutResult.
|
|
122
|
+
* Throws if result is not successful.
|
|
123
|
+
*/
|
|
124
|
+
export function unwrapResult(result) {
|
|
125
|
+
if (result.status !== 'completed') {
|
|
126
|
+
if (result.status === 'timeout') {
|
|
127
|
+
throw new TimeoutError(result.timeoutMs);
|
|
128
|
+
}
|
|
129
|
+
throw new Error(result.error.message);
|
|
130
|
+
}
|
|
131
|
+
return result.result;
|
|
132
|
+
}
|
|
133
|
+
//# sourceMappingURL=wrapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wrapper.js","sourceRoot":"","sources":["../../src/timeout/wrapper.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAE3E;;GAEG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAqB;IACtD,gBAAgB,EAAE,MAAM;IACxB,YAAY,EAAE;QACZ,KAAK,EAAE,MAAM,EAAE,aAAa;QAC5B,QAAQ,EAAE,MAAM,EAAE,a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AED;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,QAAgB;IAC9C,OAAO,eAAe,CAAC,QAAQ,CAAC,CAAC;AACnC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,MAAqB;IACnD,OAAO,MAAM,CAAC,MAAM,KAAK,SAAS,CAAC;AACrC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,MAAqB;IACnD,OAAO,MAAM,CAAC,MAAM,KAAK,WAAW,CAAC;AACvC,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAI,MAAqB;IACnD,IAAI,MAAM,CAAC,MAAM,KAAK,WAAW,EAAE,CAAC;QAClC,IAAI,MAAM,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;YAChC,MAAM,IAAI,YAAY,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QAC3C,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IACxC,CAAC;IACD,OAAO,MAAM,CAAC,MAAW,CAAC;AAC5B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/validation/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,0BAA0B,EAC1B,cAAc,EACd,sBAAsB,GACvB,MAAM,wBAAwB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/validation/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,0BAA0B,EAC1B,cAAc,EACd,sBAAsB,GACvB,MAAM,wBAAwB,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { MCPRequestLimits, ValidationResult, ValidationError } from '@defai.digital/contracts';
|
|
2
|
+
/**
|
|
3
|
+
* Validate request against limits.
|
|
4
|
+
*
|
|
5
|
+
* Invariants enforced:
|
|
6
|
+
* - INV-MCP-LIMIT-001: Array size enforcement
|
|
7
|
+
* - INV-MCP-LIMIT-002: Early rejection
|
|
8
|
+
* - INV-MCP-LIMIT-003: Tool-specific limits
|
|
9
|
+
* - INV-MCP-LIMIT-004: Descriptive errors
|
|
10
|
+
*/
|
|
11
|
+
export declare function validateRequest(toolName: string, args: unknown, limits?: MCPRequestLimits): ValidationResult;
|
|
12
|
+
/**
|
|
13
|
+
* Create a validation middleware for tool handlers.
|
|
14
|
+
*
|
|
15
|
+
* INV-MCP-LIMIT-002: Early rejection.
|
|
16
|
+
*/
|
|
17
|
+
export declare function createValidationMiddleware(limits?: MCPRequestLimits): (toolName: string, args: unknown) => ValidationResult;
|
|
18
|
+
/**
|
|
19
|
+
* Check if a validation result indicates success.
|
|
20
|
+
*/
|
|
21
|
+
export declare function isValidRequest(result: ValidationResult): result is {
|
|
22
|
+
valid: true;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Format validation errors for display.
|
|
26
|
+
*/
|
|
27
|
+
export declare function formatValidationErrors(errors: ValidationError[]): string;
|
|
28
|
+
//# sourceMappingURL=request-validator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-validator.d.ts","sourceRoot":"","sources":["../../src/validation/request-validator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,EAChB,MAAM,0BAA0B,CAAC;AAOlC;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAC7B,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,OAAO,EACb,MAAM,GAAE,gBAAyC,GAChD,gBAAgB,CAuBlB;AA6HD;;;;GAIG;AACH,wBAAgB,0BAA0B,CACxC,MAAM,GAAE,gBAAyC,IAG/C,UAAU,MAAM,EAChB,MAAM,OAAO,KACZ,gBAAgB,CAGpB;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,gBAAgB,GAAG,MAAM,IAAI;IAAE,KAAK,EAAE,IAAI,CAAA;CAAE,CAElF;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,eAAe,EAAE,GAAG,MAAM,CAUxE"}
|