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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright 2026 Paolo Vella
// SPDX-License-Identifier: MPL-2.0
//! Transport protocol and SDK tier types for MCP June 2026 spec compliance.
//!
//! These types support transport discovery, negotiation, and SDK capability
//! advertisement across all Vellaveto transport modes (HTTP, WebSocket, gRPC, stdio).
use serde::{Deserialize, Serialize};
/// Supported MCP transport protocols, ordered by preference (highest first).
///
/// The `Ord` implementation reflects the default priority ordering:
/// gRPC > WebSocket > HTTP > stdio. Negotiation logic uses this ordering
/// when no explicit client preference is provided.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
#[serde(rename_all = "lowercase")]
pub enum TransportProtocol {
/// gRPC transport (highest throughput, bidirectional streaming).
Grpc = 0,
/// WebSocket transport (bidirectional, persistent connection).
WebSocket = 1,
/// Streamable HTTP transport (request/response + SSE).
Http = 2,
/// Standard I/O transport (stdin/stdout, local only).
Stdio = 3,
}
/// A single transport endpoint advertisement for discovery responses.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct TransportEndpoint {
/// The transport protocol type.
pub protocol: TransportProtocol,
/// The endpoint URL (e.g., `http://localhost:3001/mcp`).
pub url: String,
/// Whether this transport is currently available and accepting connections.
pub available: bool,
/// MCP protocol versions supported on this transport.
pub protocol_versions: Vec<String>,
}
impl TransportEndpoint {
/// Maximum number of protocol versions per endpoint.
///
/// SECURITY (FIND-R113-001): Bound protocol_versions to prevent memory exhaustion.
pub const MAX_PROTOCOL_VERSIONS: usize = 20;
/// Maximum length for the endpoint URL.
///
/// SECURITY (FIND-R113-001): Bound URL to prevent memory exhaustion.
pub const MAX_URL_LEN: usize = 2048;
/// Maximum length of a single protocol version string.
const MAX_VERSION_LEN: usize = 64;
/// Validate structural bounds on deserialized data.
///
/// SECURITY (FIND-R113-001): Prevents memory exhaustion via oversized
/// TransportEndpoint payloads and rejects control/format characters.
pub fn validate(&self) -> Result<(), String> {
if self.url.len() > Self::MAX_URL_LEN {
return Err(format!(
"TransportEndpoint url length {} exceeds max {}",
self.url.len(),
Self::MAX_URL_LEN,
));
}
if crate::core::has_dangerous_chars(&self.url) {
return Err("TransportEndpoint url contains control or format characters".to_string());
}
if self.protocol_versions.len() > Self::MAX_PROTOCOL_VERSIONS {
return Err(format!(
"TransportEndpoint protocol_versions count {} exceeds max {}",
self.protocol_versions.len(),
Self::MAX_PROTOCOL_VERSIONS,
));
}
for (i, ver) in self.protocol_versions.iter().enumerate() {
if ver.len() > Self::MAX_VERSION_LEN {
return Err(format!(
"TransportEndpoint protocol_versions[{}] length {} exceeds max {}",
i,
ver.len(),
Self::MAX_VERSION_LEN,
));
}
if ver
.chars()
.any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
{
return Err(format!(
"TransportEndpoint protocol_versions[{i}] contains control or format characters",
));
}
}
Ok(())
}
}
/// SDK maturity tier levels per MCP June 2026 draft.
///
/// The `Ord` implementation provides `Core < Standard < Extended < Full`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SdkTier {
/// Minimal MCP implementation: tool calling + policy evaluation.
Core = 0,
/// Core + audit logging, DLP scanning, injection detection.
Standard = 1,
/// Standard + multi-transport, advanced threat detection, compliance.
Extended = 2,
/// Extended + all optional features (semantic guardrails, RAG defense, A2A).
Full = 3,
}
/// Record of a single transport fallback attempt (Phase 29).
///
/// Captures the outcome (success/failure), duration, and optional error
/// for one leg of a cross-transport fallback sequence.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct TransportAttempt {
/// The transport protocol that was tried.
pub protocol: TransportProtocol,
/// The endpoint URL that was targeted.
pub endpoint_url: String,
/// Whether this attempt succeeded.
pub succeeded: bool,
/// Wall-clock duration of this attempt in milliseconds.
pub duration_ms: u64,
/// Human-readable error message if the attempt failed (`succeeded == false`).
///
/// `None` when the attempt succeeded. Contains transport-level error details
/// (e.g., connection refused, timeout, TLS handshake failure) for audit trail
/// and debugging purposes. Sensitive details (credentials, internal IPs) are
/// redacted before population.
pub error: Option<String>,
}
impl TransportAttempt {
/// Maximum length for endpoint URL.
const MAX_URL_LEN: usize = 2048;
/// Maximum length for error message.
const MAX_ERROR_LEN: usize = 4096;
/// Validate structural bounds on deserialized data.
///
/// SECURITY (FIND-R113-P3): Prevents memory exhaustion and control char
/// injection via oversized TransportAttempt payloads.
pub fn validate(&self) -> Result<(), String> {
if self.endpoint_url.len() > Self::MAX_URL_LEN {
return Err(format!(
"TransportAttempt endpoint_url length {} exceeds max {}",
self.endpoint_url.len(),
Self::MAX_URL_LEN,
));
}
if crate::core::has_dangerous_chars(&self.endpoint_url) {
return Err(
"TransportAttempt endpoint_url contains control or format characters".to_string(),
);
}
if let Some(ref err) = self.error {
if err.len() > Self::MAX_ERROR_LEN {
return Err(format!(
"TransportAttempt error length {} exceeds max {}",
err.len(),
Self::MAX_ERROR_LEN,
));
}
// SECURITY (FIND-R157-007): Reject control/format chars in error
// messages to prevent log injection via crafted transport errors.
if crate::core::has_dangerous_chars(err) {
return Err(
"TransportAttempt error contains control or format characters".to_string(),
);
}
}
Ok(())
}
}
/// Full audit trail for a cross-transport fallback negotiation (Phase 29).
///
/// Captures every attempt made during a fallback sequence and which
/// transport (if any) ultimately handled the request.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct FallbackNegotiationHistory {
/// Ordered list of transport attempts (first = highest priority).
pub attempts: Vec<TransportAttempt>,
/// The transport that successfully handled the request, if any.
pub successful_transport: Option<TransportProtocol>,
/// Total wall-clock duration of the entire fallback sequence in milliseconds.
pub total_duration_ms: u64,
}
impl FallbackNegotiationHistory {
/// Maximum number of attempts in a single fallback sequence.
///
/// SECURITY (FIND-R113-P3): Bound attempts vector to prevent memory exhaustion.
pub const MAX_ATTEMPTS: usize = 100;
/// Validate structural bounds on deserialized data.
pub fn validate(&self) -> Result<(), String> {
if self.attempts.len() > Self::MAX_ATTEMPTS {
return Err(format!(
"FallbackNegotiationHistory attempts count {} exceeds max {}",
self.attempts.len(),
Self::MAX_ATTEMPTS,
));
}
for (i, attempt) in self.attempts.iter().enumerate() {
attempt
.validate()
.map_err(|e| format!("attempts[{i}]: {e}"))?;
}
Ok(())
}
}
/// Declared SDK capabilities for a Vellaveto instance.
///
/// Advertised via the `/.well-known/mcp-transport` discovery endpoint.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct SdkCapabilities {
/// The declared SDK tier level.
pub tier: SdkTier,
/// Individual capability strings (e.g., `"policy-evaluation"`, `"dlp-scanning"`).
pub capabilities: Vec<String>,
/// MCP protocol versions this SDK supports.
pub supported_versions: Vec<String>,
}
impl SdkCapabilities {
/// Maximum number of individual capability strings.
pub const MAX_CAPABILITIES: usize = 100;
/// Maximum number of supported protocol versions.
pub const MAX_VERSIONS: usize = 20;
/// Maximum length of a single capability string.
const MAX_CAPABILITY_LEN: usize = 256;
/// Maximum length of a single version string.
const MAX_VERSION_LEN: usize = 64;
/// Validate structural bounds on `SdkCapabilities`.
///
/// SECURITY (FIND-R49-002): Unbounded capability/version vectors could be
/// used for memory exhaustion via oversized discovery responses.
/// SECURITY (FIND-R113-P3): Control/format char validation on string entries.
pub fn validate(&self) -> Result<(), String> {
if self.capabilities.len() > Self::MAX_CAPABILITIES {
return Err(format!(
"capabilities count {} exceeds maximum {}",
self.capabilities.len(),
Self::MAX_CAPABILITIES,
));
}
for (i, cap) in self.capabilities.iter().enumerate() {
if cap.len() > Self::MAX_CAPABILITY_LEN {
return Err(format!(
"capabilities[{}] length {} exceeds max {}",
i,
cap.len(),
Self::MAX_CAPABILITY_LEN,
));
}
if cap
.chars()
.any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
{
return Err(format!(
"capabilities[{i}] contains control or format characters",
));
}
}
if self.supported_versions.len() > Self::MAX_VERSIONS {
return Err(format!(
"supported_versions count {} exceeds maximum {}",
self.supported_versions.len(),
Self::MAX_VERSIONS,
));
}
for (i, ver) in self.supported_versions.iter().enumerate() {
if ver.len() > Self::MAX_VERSION_LEN {
return Err(format!(
"supported_versions[{}] length {} exceeds max {}",
i,
ver.len(),
Self::MAX_VERSION_LEN,
));
}
if ver
.chars()
.any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
{
return Err(format!(
"supported_versions[{i}] contains control or format characters",
));
}
}
Ok(())
}
}