@mfdaves/okf-mcp 0.3.2 → 0.3.3-rc.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.
@@ -0,0 +1,283 @@
1
+ "use strict";
2
+
3
+ const ERROR_CODES = Object.freeze({
4
+ PARSE_ERROR: -32700,
5
+ INVALID_REQUEST: -32600,
6
+ METHOD_NOT_FOUND: -32601,
7
+ INVALID_PARAMS: -32602,
8
+ INTERNAL_ERROR: -32603,
9
+ RESOURCE_NOT_FOUND: -32002,
10
+ });
11
+
12
+ const SUPPORTED_SCHEMA_KEYWORDS = new Set([
13
+ "additionalProperties",
14
+ "anyOf",
15
+ "default",
16
+ "description",
17
+ "enum",
18
+ "items",
19
+ "maximum",
20
+ "minItems",
21
+ "minLength",
22
+ "minProperties",
23
+ "minimum",
24
+ "properties",
25
+ "required",
26
+ "type",
27
+ ]);
28
+
29
+ class ProtocolError extends Error {
30
+ constructor(code, message, data) {
31
+ super(message);
32
+ this.name = "ProtocolError";
33
+ this.code = code;
34
+ if (data !== undefined) {
35
+ this.data = data;
36
+ }
37
+ }
38
+ }
39
+
40
+ class ToolExecutionError extends Error {
41
+ constructor(message) {
42
+ super(message);
43
+ this.name = "ToolExecutionError";
44
+ }
45
+ }
46
+
47
+ function isPlainObject(value) {
48
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
49
+ return false;
50
+ }
51
+ const prototype = Object.getPrototypeOf(value);
52
+ return prototype === Object.prototype || prototype === null;
53
+ }
54
+
55
+ function isValidRequestId(value) {
56
+ return value === null
57
+ || typeof value === "string"
58
+ || (typeof value === "number" && Number.isFinite(value));
59
+ }
60
+
61
+ function validateJsonRpcEnvelope(value) {
62
+ if (!isPlainObject(value)) {
63
+ throw new ProtocolError(ERROR_CODES.INVALID_REQUEST, "Invalid Request", {
64
+ detail: "JSON-RPC messages must be objects; batch messages are not supported.",
65
+ });
66
+ }
67
+ if (value.jsonrpc !== "2.0") {
68
+ throw new ProtocolError(ERROR_CODES.INVALID_REQUEST, "Invalid Request", {
69
+ detail: 'jsonrpc must be exactly "2.0".',
70
+ });
71
+ }
72
+ if (typeof value.method !== "string" || !value.method.trim()) {
73
+ throw new ProtocolError(ERROR_CODES.INVALID_REQUEST, "Invalid Request", {
74
+ detail: "method must be a non-empty string.",
75
+ });
76
+ }
77
+ if (Object.prototype.hasOwnProperty.call(value, "id") && !isValidRequestId(value.id)) {
78
+ throw new ProtocolError(ERROR_CODES.INVALID_REQUEST, "Invalid Request", {
79
+ detail: "id must be a string, finite number, or null.",
80
+ });
81
+ }
82
+ if (
83
+ Object.prototype.hasOwnProperty.call(value, "params")
84
+ && (value.params === null || typeof value.params !== "object")
85
+ ) {
86
+ throw new ProtocolError(ERROR_CODES.INVALID_REQUEST, "Invalid Request", {
87
+ detail: "params must be an object or array when supplied.",
88
+ });
89
+ }
90
+ return {
91
+ notification: !Object.prototype.hasOwnProperty.call(value, "id"),
92
+ id: Object.prototype.hasOwnProperty.call(value, "id") ? value.id : null,
93
+ };
94
+ }
95
+
96
+ function protocolError(error) {
97
+ if (error instanceof ProtocolError) {
98
+ return error;
99
+ }
100
+ return new ProtocolError(ERROR_CODES.INTERNAL_ERROR, "Internal error");
101
+ }
102
+
103
+ function responseFor(id, result, error) {
104
+ if (error) {
105
+ const normalized = protocolError(error);
106
+ const response = {
107
+ jsonrpc: "2.0",
108
+ id,
109
+ error: {
110
+ code: normalized.code,
111
+ message: normalized.message,
112
+ },
113
+ };
114
+ if (normalized.data !== undefined) {
115
+ response.error.data = normalized.data;
116
+ }
117
+ return response;
118
+ }
119
+ return { jsonrpc: "2.0", id, result };
120
+ }
121
+
122
+ function assertSupportedSchema(schema, path) {
123
+ const location = path || "#";
124
+ if (!isPlainObject(schema)) {
125
+ throw new Error(`Tool schema at ${location} must be an object.`);
126
+ }
127
+ Object.keys(schema).forEach((keyword) => {
128
+ if (!SUPPORTED_SCHEMA_KEYWORDS.has(keyword)) {
129
+ throw new Error(`Unsupported tool schema keyword at ${location}: ${keyword}`);
130
+ }
131
+ });
132
+ if (schema.type !== undefined && !["object", "string", "integer", "boolean", "array"].includes(schema.type)) {
133
+ throw new Error(`Unsupported tool schema type at ${location}: ${schema.type}`);
134
+ }
135
+ if (
136
+ schema.additionalProperties !== undefined
137
+ && typeof schema.additionalProperties !== "boolean"
138
+ ) {
139
+ throw new Error(`Tool schema additionalProperties at ${location} must be boolean.`);
140
+ }
141
+ if (
142
+ schema.required !== undefined
143
+ && (
144
+ !Array.isArray(schema.required)
145
+ || schema.required.some((name) => typeof name !== "string" || !name)
146
+ )
147
+ ) {
148
+ throw new Error(`Tool schema required at ${location} must be an array of non-empty strings.`);
149
+ }
150
+ if (schema.enum !== undefined && (!Array.isArray(schema.enum) || schema.enum.length === 0)) {
151
+ throw new Error(`Tool schema enum at ${location} must be a non-empty array.`);
152
+ }
153
+ ["minimum", "maximum", "minLength", "minItems", "minProperties"].forEach((keyword) => {
154
+ if (schema[keyword] !== undefined && (!Number.isFinite(schema[keyword]) || schema[keyword] < 0)) {
155
+ throw new Error(`Tool schema ${keyword} at ${location} must be a non-negative finite number.`);
156
+ }
157
+ });
158
+ ["minLength", "minItems", "minProperties"].forEach((keyword) => {
159
+ if (schema[keyword] !== undefined && !Number.isInteger(schema[keyword])) {
160
+ throw new Error(`Tool schema ${keyword} at ${location} must be an integer.`);
161
+ }
162
+ });
163
+ if (schema.properties !== undefined) {
164
+ if (!isPlainObject(schema.properties)) {
165
+ throw new Error(`Tool schema properties at ${location} must be an object.`);
166
+ }
167
+ Object.entries(schema.properties).forEach(([name, child]) => {
168
+ assertSupportedSchema(child, `${location}/properties/${escapeJsonPointer(name)}`);
169
+ });
170
+ }
171
+ if (schema.items !== undefined) {
172
+ assertSupportedSchema(schema.items, `${location}/items`);
173
+ }
174
+ if (schema.anyOf !== undefined) {
175
+ if (!Array.isArray(schema.anyOf) || schema.anyOf.length === 0) {
176
+ throw new Error(`Tool schema anyOf at ${location} must be a non-empty array.`);
177
+ }
178
+ schema.anyOf.forEach((branch, index) => {
179
+ assertSupportedSchema(branch, `${location}/anyOf/${index}`);
180
+ });
181
+ }
182
+ }
183
+
184
+ function escapeJsonPointer(value) {
185
+ return String(value).replace(/~/g, "~0").replace(/\//g, "~1");
186
+ }
187
+
188
+ function childPath(path, name) {
189
+ return `${path}/${escapeJsonPointer(name)}`;
190
+ }
191
+
192
+ function addIssue(issues, path, message) {
193
+ issues.push({ path, message });
194
+ }
195
+
196
+ function matchesType(value, type) {
197
+ switch (type) {
198
+ case "object":
199
+ return isPlainObject(value);
200
+ case "array":
201
+ return Array.isArray(value);
202
+ case "integer":
203
+ return typeof value === "number" && Number.isFinite(value) && Number.isInteger(value);
204
+ default:
205
+ return typeof value === type;
206
+ }
207
+ }
208
+
209
+ function validateSchemaValue(schema, value, path, issues) {
210
+ if (schema.type && !matchesType(value, schema.type)) {
211
+ addIssue(issues, path, `must be ${schema.type === "integer" ? "an integer" : `a ${schema.type}`}`);
212
+ return;
213
+ }
214
+ if (schema.enum && !schema.enum.some((candidate) => Object.is(candidate, value))) {
215
+ addIssue(issues, path, `must be one of: ${schema.enum.map(String).join(", ")}`);
216
+ }
217
+ if (typeof value === "string" && schema.minLength !== undefined && value.length < schema.minLength) {
218
+ addIssue(issues, path, `must contain at least ${schema.minLength} character${schema.minLength === 1 ? "" : "s"}`);
219
+ }
220
+ if (typeof value === "number") {
221
+ if (schema.minimum !== undefined && value < schema.minimum) {
222
+ addIssue(issues, path, `must be greater than or equal to ${schema.minimum}`);
223
+ }
224
+ if (schema.maximum !== undefined && value > schema.maximum) {
225
+ addIssue(issues, path, `must be less than or equal to ${schema.maximum}`);
226
+ }
227
+ }
228
+ if (Array.isArray(value)) {
229
+ if (schema.minItems !== undefined && value.length < schema.minItems) {
230
+ addIssue(issues, path, `must contain at least ${schema.minItems} item${schema.minItems === 1 ? "" : "s"}`);
231
+ }
232
+ if (schema.items) {
233
+ value.forEach((item, index) => validateSchemaValue(schema.items, item, childPath(path, index), issues));
234
+ }
235
+ }
236
+ if (isPlainObject(value)) {
237
+ const properties = schema.properties || {};
238
+ (schema.required || []).forEach((name) => {
239
+ if (!Object.prototype.hasOwnProperty.call(value, name)) {
240
+ addIssue(issues, childPath(path, name), "is required");
241
+ }
242
+ });
243
+ if (schema.minProperties !== undefined && Object.keys(value).length < schema.minProperties) {
244
+ addIssue(issues, path, `must contain at least ${schema.minProperties} propert${schema.minProperties === 1 ? "y" : "ies"}`);
245
+ }
246
+ Object.entries(value).forEach(([name, child]) => {
247
+ if (Object.prototype.hasOwnProperty.call(properties, name)) {
248
+ validateSchemaValue(properties[name], child, childPath(path, name), issues);
249
+ } else if (schema.additionalProperties === false) {
250
+ addIssue(issues, childPath(path, name), "is not allowed");
251
+ }
252
+ });
253
+ }
254
+ if (schema.anyOf) {
255
+ const matched = schema.anyOf.some((branch) => {
256
+ const branchIssues = [];
257
+ validateSchemaValue(branch, value, path, branchIssues);
258
+ return branchIssues.length === 0;
259
+ });
260
+ if (!matched) {
261
+ addIssue(issues, path, "must match at least one allowed argument shape");
262
+ }
263
+ }
264
+ }
265
+
266
+ function validateToolArguments(schema, value) {
267
+ assertSupportedSchema(schema);
268
+ const issues = [];
269
+ validateSchemaValue(schema, value, "", issues);
270
+ return issues;
271
+ }
272
+
273
+ module.exports = {
274
+ ERROR_CODES,
275
+ ProtocolError,
276
+ ToolExecutionError,
277
+ assertSupportedSchema,
278
+ isPlainObject,
279
+ isValidRequestId,
280
+ responseFor,
281
+ validateJsonRpcEnvelope,
282
+ validateToolArguments,
283
+ };