forked from patternfly/patternfly-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.assertions.ts
More file actions
144 lines (130 loc) · 6.08 KB
/
server.assertions.ts
File metadata and controls
144 lines (130 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import assert from 'node:assert';
import { ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js';
/**
* MCP assert. Centralizes and throws an error if the validation fails.
*
* @param condition - Function or condition to be validated.
* @param message - Thrown error message, or function, that returns the error message.
* @param {ErrorCode} [code] - Thrown error code when validation fails. Defaults to `ErrorCode.InvalidParams`.
*
* @throws {McpError} Throw the provided error message and code on failure.
*/
const mcpAssert = (condition: unknown, message: string | (() => string), code: ErrorCode = ErrorCode.InvalidParams) => {
try {
const result = typeof condition === 'function' ? condition() : condition;
const resultMessage = typeof message === 'function' ? message() : message;
assert.ok(result, resultMessage);
} catch (error) {
throw new McpError(code, (error as Error).message);
}
};
/**
* General purpose input assert/validation function.
*
* @alias mcpAssert
*
* @param condition - Function or condition to be validated.
* @param message - Thrown error message, or function, that returns the error message.
* @param {ErrorCode} [code] - Thrown error code when validation fails. Defaults to `ErrorCode.InvalidParams`.
*
* @throws {McpError} Throw the provided error message and code on failure.
*/
function assertInput(
condition: unknown,
message: string | (() => string),
code?: ErrorCode
): asserts condition {
mcpAssert(condition, message, code);
}
/**
* Assert/validate if the input is a non-empty string.
*
* @param input - Input value
* @param [options] - Validation options
* @param [options.inputDisplayName] - Display name for the input. Used in the default error message. Defaults to 'Input'.
* @param [options.message] - Custom error message. A default error message with optional `inputDisplayName` is generated if not provided.
*
* @throws McpError If input is not a non-empty string.
*/
function assertInputString(
input: unknown,
{ inputDisplayName, message }: { inputDisplayName?: string, message?: string } = {}
): asserts input is string {
const isValid = typeof input === 'string' && input.trim().length > 0;
mcpAssert(isValid, message || `"${inputDisplayName || 'Input'}" must be a non-empty string`);
}
/**
* Assert/validate if the input is a string, non-empty, and meets min and max length requirements.
*
* @param input - Input string
* @param options - Validation options
* @param options.max - Maximum length of the string. `Required`
* @param options.min - Minimum length of the string. `Required`
* @param [options.inputDisplayName] - Display name for the input. Used in the default error message. Defaults to 'Input'.
* @param [options.message] - Error description. A default error message with optional `inputDisplayName` is generated if not provided.
*
* @throws McpError If input is not a string or does not meet length requirements.
*/
function assertInputStringLength(
input: unknown,
{ max, min, inputDisplayName, message }: { max: number, min: number, inputDisplayName?: string, message?: string }
): asserts input is string {
const isValid = typeof input === 'string' && input.trim().length <= max && input.trim().length >= min;
mcpAssert(isValid, message || `"${inputDisplayName || 'Input'}" must be a string from ${min} to ${max} characters`);
}
/**
* Assert/validate if input array entries are strings and have min and max length.
*
* @param input - Array of strings
* @param options - Validation options
* @param options.max - Maximum length of each string in the array. `Required`
* @param options.min - Minimum length of each string in the array. `Required`
* @param [options.inputDisplayName] - Display name for the input. Used in the default error messages. Defaults to 'Input'.
* @param [options.message] - Error description. A default error message with optional `inputDisplayName` is generated if not provided.
*
* @throws McpError If input is not an array of strings or array entries do not meet length requirements.
*/
function assertInputStringArrayEntryLength(
input: unknown,
{ max, min, inputDisplayName, message }: { max: number, min: number, inputDisplayName?: string, message?: string }
): asserts input is string[] {
const isValid = Array.isArray(input) && input.every(entry => typeof entry === 'string' && entry.trim().length <= max && entry.trim().length >= min);
mcpAssert(isValid, message || `"${inputDisplayName || 'Input'}" array must contain strings from ${min} to ${max} characters`);
}
/**
* Assert/validate if input is a string or number and is one of the allowed values.
*
* @param input - The input value
* @param values - List of allowed values
* @param [options] - Validation options
* @param [options.inputDisplayName] - Display name for the input. Used in the default error messages. Defaults to 'Input'.
* @param [options.message] - Error description. A default error message with optional `inputDisplayName` is generated if not provided.
*
* @throws McpError If input is not a string or number or is not one of the allowed values.
*/
function assertInputStringNumberEnumLike(
input: unknown,
values: unknown,
{ inputDisplayName, message }: { inputDisplayName?: string, message?: string } = {}
): asserts input is string | number {
const hasArrayWithLength = Array.isArray(values) && values.length > 0;
let updatedDescription;
let errorCode;
if (hasArrayWithLength) {
errorCode = ErrorCode.InvalidParams;
updatedDescription = message || `"${inputDisplayName || 'Input'}" must be one of the following values: ${values.join(', ')}`;
} else {
errorCode = ErrorCode.InternalError;
updatedDescription = `Unable to confirm "${inputDisplayName || 'input'}." List of allowed values is empty or undefined.`;
}
const isStringOrNumber = typeof input === 'string' || typeof input === 'number';
const isValid = isStringOrNumber && hasArrayWithLength && values.includes(input);
mcpAssert(isValid, updatedDescription, errorCode);
}
export {
assertInput,
assertInputString,
assertInputStringLength,
assertInputStringArrayEntryLength,
assertInputStringNumberEnumLike
};