@myatkyawthu/mcp-connect 0.1.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/LICENSE +21 -0
- package/README.md +308 -0
- package/package.json +63 -0
- package/src/cli.js +170 -0
- package/src/defineMCP.js +72 -0
- package/src/index.js +35 -0
- package/src/server/mcpServer.js +236 -0
- package/src/types/mcp.js +108 -0
- package/src/utils/configValidation.js +381 -0
- package/src/utils/logger.js +290 -0
- package/src/utils/validation.js +78 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation utilities for MCP-Connect
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {Object} JsonRpcRequest
|
|
7
|
+
* @property {string} jsonrpc - JSON-RPC version
|
|
8
|
+
* @property {string} method - Method name
|
|
9
|
+
* @property {any} [params] - Method parameters
|
|
10
|
+
* @property {string|number|null} [id] - Request ID
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Basic JSON-RPC request validation
|
|
15
|
+
* The MCP SDK handles most validation, but this provides additional safety
|
|
16
|
+
* @param {any} request - Request to validate
|
|
17
|
+
* @returns {JsonRpcRequest} Validated request
|
|
18
|
+
* @throws {Error} If request is invalid
|
|
19
|
+
*/
|
|
20
|
+
export function validateJsonRpcRequest(request) {
|
|
21
|
+
if (!request || typeof request !== "object") {
|
|
22
|
+
throw new Error("Request must be a JSON object");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (request.jsonrpc !== "2.0") {
|
|
26
|
+
throw new Error("Invalid JSON-RPC version. Must be '2.0'");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (!request.method || typeof request.method !== "string") {
|
|
30
|
+
throw new Error("Request must include a 'method' string");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ID is optional for notifications, but if present must be string, number, or null
|
|
34
|
+
if (request.id !== undefined &&
|
|
35
|
+
typeof request.id !== "string" &&
|
|
36
|
+
typeof request.id !== "number" &&
|
|
37
|
+
request.id !== null) {
|
|
38
|
+
throw new Error("Request 'id' must be string, number, or null");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return /** @type {JsonRpcRequest} */ (request);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Validate tool arguments
|
|
46
|
+
* @param {any} args - Arguments to validate
|
|
47
|
+
* @returns {Record<string, any>} Validated arguments object
|
|
48
|
+
* @throws {Error} If arguments are invalid
|
|
49
|
+
*/
|
|
50
|
+
export function validateToolArguments(args) {
|
|
51
|
+
if (args === null || args === undefined) {
|
|
52
|
+
return {};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (typeof args !== "object" || Array.isArray(args)) {
|
|
56
|
+
throw new Error("Tool arguments must be an object");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return args;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Sanitize error messages for client consumption
|
|
64
|
+
* @param {unknown} error - Error to sanitize
|
|
65
|
+
* @returns {string} Sanitized error message
|
|
66
|
+
*/
|
|
67
|
+
export function sanitizeErrorMessage(error) {
|
|
68
|
+
if (error instanceof Error) {
|
|
69
|
+
// Remove sensitive information from stack traces
|
|
70
|
+
return error.message;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (typeof error === "string") {
|
|
74
|
+
return error;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return "An unknown error occurred";
|
|
78
|
+
}
|