tmcp 1.4.0 → 1.6.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.
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Check if a protocol version is supported
3
+ * @param {string} version - The protocol version to check
4
+ * @returns {boolean} True if the version is supported
5
+ */
6
+ export function is_supported_version(version: string): boolean;
7
+ /**
8
+ * Get the latest supported protocol version
9
+ * @returns {string} The latest protocol version
10
+ */
11
+ export function get_latest_version(): string;
12
+ /**
13
+ * Get all supported protocol versions
14
+ * @returns {string[]} Array of supported protocol versions
15
+ */
16
+ export function get_supported_versions(): string[];
17
+ /**
18
+ * Compare two protocol versions
19
+ * @param {string} version1 - First version to compare
20
+ * @param {string} version2 - Second version to compare
21
+ * @returns {number} -1 if version1 < version2, 0 if equal, 1 if version1 > version2
22
+ */
23
+ export function compare_versions(version1: string, version2: string): number;
24
+ /**
25
+ * Check if a protocol version is newer than another
26
+ * @param {string} version1 - Version to check
27
+ * @param {string} version2 - Version to compare against
28
+ * @returns {boolean} True if version1 is newer than version2
29
+ */
30
+ export function is_newer_version(version1: string, version2: string): boolean;
31
+ /**
32
+ * Check if a protocol version is older than another
33
+ * @param {string} version1 - Version to check
34
+ * @param {string} version2 - Version to compare against
35
+ * @returns {boolean} True if version1 is older than version2
36
+ */
37
+ export function is_older_version(version1: string, version2: string): boolean;
38
+ /**
39
+ * Get the minimum version required for a feature
40
+ * @param {string} feature - The feature name
41
+ * @returns {string|null} The minimum version required, or null if unknown
42
+ */
43
+ export function get_minimum_version_for_feature(feature: string): string | null;
44
+ /**
45
+ * Check if a feature is supported in a given protocol version
46
+ * @param {string} feature - The feature name
47
+ * @param {string} version - The protocol version to check
48
+ * @returns {boolean} True if the feature is supported
49
+ */
50
+ export function is_feature_supported(feature: string, version: string): boolean;
51
+ /**
52
+ * Create a protocol version validation error
53
+ * @param {string} version - The invalid version
54
+ * @param {string} reason - The reason for the error
55
+ * @returns {Error} The validation error
56
+ */
57
+ export function create_version_error(version: string, reason: string): Error;
58
+ /**
59
+ * Negotiate protocol version between client and server
60
+ * According to MCP spec:
61
+ * - If server supports client's version, return same version
62
+ * - Otherwise, return server's latest supported version
63
+ * @param {string} client_version - The protocol version requested by client
64
+ * @returns {string} The negotiated protocol version
65
+ */
66
+ export function negotiate_protocol_version(client_version: string): string;
67
+ /**
68
+ * Check if version negotiation should result in an error
69
+ * @param {string} client_version - The protocol version requested by client
70
+ * @returns {boolean} True if negotiation should fail
71
+ */
72
+ export function should_version_negotiation_fail(client_version: string): boolean;
73
+ /**
74
+ * Latest stable protocol version
75
+ */
76
+ export const LATEST_PROTOCOL_VERSION: string;
77
+ /**
78
+ * Validate MCP protocol version format (YYYY-MM-DD)
79
+ */
80
+ export const ProtocolVersionSchema: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.RegexAction<string, "Protocol version must be in YYYY-MM-DD format">]>;
81
+ /**
82
+ * Validate that the protocol version is supported
83
+ */
84
+ export const SupportedProtocolVersionSchema: v.SchemaWithPipe<readonly [v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.RegexAction<string, "Protocol version must be in YYYY-MM-DD format">]>, v.CheckAction<string, "Unsupported protocol version">]>;
85
+ import * as v from 'valibot';
@@ -0,0 +1,197 @@
1
+ import * as v from 'valibot';
2
+
3
+ /**
4
+ * Supported MCP protocol versions in order of preference (newest first)
5
+ */
6
+ const SUPPORTED_VERSIONS = ['2025-06-18', '2025-03-26', '2024-11-05'];
7
+
8
+ /**
9
+ * Latest stable protocol version
10
+ */
11
+ export const LATEST_PROTOCOL_VERSION = SUPPORTED_VERSIONS[0];
12
+
13
+ /**
14
+ * Validate MCP protocol version format (YYYY-MM-DD)
15
+ */
16
+ export const ProtocolVersionSchema = v.pipe(
17
+ v.string(),
18
+ v.regex(
19
+ /^\d{4}-\d{2}-\d{2}$/,
20
+ 'Protocol version must be in YYYY-MM-DD format',
21
+ ),
22
+ );
23
+
24
+ /**
25
+ * Validate that the protocol version is supported
26
+ */
27
+ export const SupportedProtocolVersionSchema = v.pipe(
28
+ ProtocolVersionSchema,
29
+ v.check(
30
+ (version) => SUPPORTED_VERSIONS.includes(version),
31
+ 'Unsupported protocol version',
32
+ ),
33
+ );
34
+
35
+ /**
36
+ * Check if a protocol version is supported
37
+ * @param {string} version - The protocol version to check
38
+ * @returns {boolean} True if the version is supported
39
+ */
40
+ export function is_supported_version(version) {
41
+ return SUPPORTED_VERSIONS.includes(version);
42
+ }
43
+
44
+ /**
45
+ * Get the latest supported protocol version
46
+ * @returns {string} The latest protocol version
47
+ */
48
+ export function get_latest_version() {
49
+ return LATEST_PROTOCOL_VERSION;
50
+ }
51
+
52
+ /**
53
+ * Get all supported protocol versions
54
+ * @returns {string[]} Array of supported protocol versions
55
+ */
56
+ export function get_supported_versions() {
57
+ return [...SUPPORTED_VERSIONS];
58
+ }
59
+
60
+ /**
61
+ * Compare two protocol versions
62
+ * @param {string} version1 - First version to compare
63
+ * @param {string} version2 - Second version to compare
64
+ * @returns {number} -1 if version1 < version2, 0 if equal, 1 if version1 > version2
65
+ */
66
+ export function compare_versions(version1, version2) {
67
+ // Parse versions as dates for comparison
68
+ const date1 = new Date(version1);
69
+ const date2 = new Date(version2);
70
+
71
+ if (date1 < date2) return -1;
72
+ if (date1 > date2) return 1;
73
+ return 0;
74
+ }
75
+
76
+ /**
77
+ * Check if a protocol version is newer than another
78
+ * @param {string} version1 - Version to check
79
+ * @param {string} version2 - Version to compare against
80
+ * @returns {boolean} True if version1 is newer than version2
81
+ */
82
+ export function is_newer_version(version1, version2) {
83
+ return compare_versions(version1, version2) > 0;
84
+ }
85
+
86
+ /**
87
+ * Check if a protocol version is older than another
88
+ * @param {string} version1 - Version to check
89
+ * @param {string} version2 - Version to compare against
90
+ * @returns {boolean} True if version1 is older than version2
91
+ */
92
+ export function is_older_version(version1, version2) {
93
+ return compare_versions(version1, version2) < 0;
94
+ }
95
+
96
+ const feature_versions = {
97
+ // 2025-06-18 features
98
+ 'elicitation/create': '2025-06-18',
99
+ structured_tool_output: '2025-06-18',
100
+ resource_links: '2025-06-18',
101
+ oauth_resource_indicators: '2025-06-18',
102
+ no_batching: '2025-06-18',
103
+ protocol_version_headers: '2025-06-18',
104
+
105
+ // 2025-03-26 features
106
+ authorization: '2025-03-26',
107
+
108
+ // 2024-11-05 features (initial MCP release)
109
+ 'sampling/createMessage': '2024-11-05',
110
+ 'roots/list': '2024-11-05',
111
+ 'notifications/roots/list_changed': '2024-11-05',
112
+ 'tools/list': '2024-11-05',
113
+ 'tools/call': '2024-11-05',
114
+ 'notifications/tools/list_changed': '2024-11-05',
115
+ 'resources/list': '2024-11-05',
116
+ 'resources/read': '2024-11-05',
117
+ 'resources/subscribe': '2024-11-05',
118
+ 'resources/templates/list': '2024-11-05',
119
+ 'notifications/resources/list_changed': '2024-11-05',
120
+ 'notifications/resources/updated': '2024-11-05',
121
+ 'prompts/list': '2024-11-05',
122
+ 'prompts/get': '2024-11-05',
123
+ 'notifications/prompts/list_changed': '2024-11-05',
124
+ 'completion/complete': '2024-11-05',
125
+ initialize: '2024-11-05',
126
+ ping: '2024-11-05',
127
+ 'notifications/initialized': '2024-11-05',
128
+ };
129
+
130
+ /**
131
+ * Get the minimum version required for a feature
132
+ * @param {string} feature - The feature name
133
+ * @returns {string|null} The minimum version required, or null if unknown
134
+ */
135
+ export function get_minimum_version_for_feature(feature) {
136
+ return feature_versions[/** @type {never} */ (feature)] || null;
137
+ }
138
+
139
+ /**
140
+ * Check if a feature is supported in a given protocol version
141
+ * @param {string} feature - The feature name
142
+ * @param {string} version - The protocol version to check
143
+ * @returns {boolean} True if the feature is supported
144
+ */
145
+ export function is_feature_supported(feature, version) {
146
+ const min_version = get_minimum_version_for_feature(feature);
147
+ if (!min_version) return false;
148
+
149
+ return compare_versions(version, min_version) >= 0;
150
+ }
151
+
152
+ /**
153
+ * Create a protocol version validation error
154
+ * @param {string} version - The invalid version
155
+ * @param {string} reason - The reason for the error
156
+ * @returns {Error} The validation error
157
+ */
158
+ export function create_version_error(version, reason) {
159
+ return new Error(
160
+ `Protocol version validation failed: ${reason}. Received: ${version}, Supported: ${SUPPORTED_VERSIONS.join(', ')}`,
161
+ );
162
+ }
163
+
164
+ /**
165
+ * Negotiate protocol version between client and server
166
+ * According to MCP spec:
167
+ * - If server supports client's version, return same version
168
+ * - Otherwise, return server's latest supported version
169
+ * @param {string} client_version - The protocol version requested by client
170
+ * @returns {string} The negotiated protocol version
171
+ */
172
+ export function negotiate_protocol_version(client_version) {
173
+ // If server supports the client's version, use it
174
+ if (is_supported_version(client_version)) {
175
+ return client_version;
176
+ }
177
+
178
+ // Otherwise, return server's latest supported version
179
+ return get_latest_version();
180
+ }
181
+
182
+ /**
183
+ * Check if version negotiation should result in an error
184
+ * @param {string} client_version - The protocol version requested by client
185
+ * @returns {boolean} True if negotiation should fail
186
+ */
187
+ export function should_version_negotiation_fail(client_version) {
188
+ // Only fail if the client version format is completely invalid
189
+ try {
190
+ // Test basic format validation
191
+ const date = new Date(client_version);
192
+ const regex = /^\d{4}-\d{2}-\d{2}$/;
193
+ return !regex.test(client_version) || isNaN(date.getTime());
194
+ } catch {
195
+ return true;
196
+ }
197
+ }