component-shape 0.3.2

Framework-neutral component shape metadata and naming utilities.
Documentation
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
use strum::IntoStaticStr;

/// Primitive value kinds a component shape can expose to model-controlled tools.
#[derive(Clone, Copy, Debug, Eq, IntoStaticStr, PartialEq)]
#[strum(serialize_all = "snake_case", const_into_str)]
pub enum McpPrimitiveKind {
    Any,
    Boolean,
    Integer,
    Number,
    Decimal,
    String,
    Date,
    DateTime,
}

impl McpPrimitiveKind {
    /// Returns the stable schema label for this primitive kind.
    pub const fn as_str(self) -> &'static str {
        self.into_str()
    }
}

/// Primitive kinds that can be used as `{ "min": ..., "max": ... }` MCP range bounds.
#[derive(Clone, Copy, Debug, Eq, IntoStaticStr, PartialEq)]
#[strum(serialize_all = "snake_case", const_into_str)]
pub enum McpRangeBoundKind {
    Integer,
    Number,
    Decimal,
    Date,
    DateTime,
}

impl McpRangeBoundKind {
    /// Returns the stable schema label for this range-bound kind.
    pub const fn as_str(self) -> &'static str {
        self.into_str()
    }

    /// Returns the primitive kind used by this range-bound kind.
    pub const fn primitive_kind(self) -> McpPrimitiveKind {
        match self {
            Self::Integer => McpPrimitiveKind::Integer,
            Self::Number => McpPrimitiveKind::Number,
            Self::Decimal => McpPrimitiveKind::Decimal,
            Self::Date => McpPrimitiveKind::Date,
            Self::DateTime => McpPrimitiveKind::DateTime,
        }
    }
}

impl From<McpRangeBoundKind> for McpPrimitiveKind {
    fn from(kind: McpRangeBoundKind) -> Self {
        kind.primitive_kind()
    }
}

/// Framework-neutral shape of structured MCP input accepted by a component.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum McpInputShape {
    /// The component does not expose model-controlled MCP input.
    Unsupported,
    /// A single primitive value.
    Scalar(McpPrimitiveKind),
    /// An ordered array of primitive values.
    List(McpPrimitiveKind),
    /// A unique array of primitive values.
    Set(McpPrimitiveKind),
    /// A range object with `min` and `max` bounds.
    Range(McpRangeBoundKind),
    /// An object with integration-defined structure.
    Object,
}

/// Validation error for generated MCP tool metadata.
#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
pub enum McpToolMetadataError {
    /// The tool name is empty or contains only whitespace.
    #[error("tool name cannot be empty")]
    EmptyName,
    /// The tool name starts with an unsupported character.
    #[error("tool name must start with an ASCII letter or number")]
    InvalidNameStart,
    /// The tool name contains an unsupported character.
    #[error("tool name may only contain ASCII letters, digits, '_' '-' '.'")]
    InvalidNameCharacter,
    /// A human-readable metadata field is empty or contains only whitespace.
    #[error("tool {label} cannot be empty")]
    EmptyText { label: String },
}

/// Validate the MCP tool-name subset used by generated integrations.
///
/// # Errors
///
/// Returns [`McpToolMetadataError`] when `name` is empty, starts with an
/// unsupported character, or contains a character outside the generated tool
/// name subset.
pub fn validate_mcp_tool_name(name: &str) -> Result<(), McpToolMetadataError> {
    if name.trim().is_empty() {
        return Err(McpToolMetadataError::EmptyName);
    }

    let mut chars = name.chars();
    let Some(first) = chars.next() else {
        return Err(McpToolMetadataError::EmptyName);
    };

    if !first.is_ascii_alphanumeric() {
        return Err(McpToolMetadataError::InvalidNameStart);
    }

    if chars.any(|ch| !is_mcp_tool_name_char(ch)) {
        return Err(McpToolMetadataError::InvalidNameCharacter);
    }

    Ok(())
}

fn is_mcp_tool_name_char(ch: char) -> bool {
    ch.is_ascii_alphanumeric() || ch == '_' || ch == '-' || ch == '.'
}

/// Validate human-readable MCP tool metadata text.
///
/// # Errors
///
/// Returns [`McpToolMetadataError`] when `value` is empty or contains only
/// whitespace.
pub fn validate_mcp_tool_metadata_text(
    label: &str,
    value: &str,
) -> Result<(), McpToolMetadataError> {
    if value.trim().is_empty() {
        Err(McpToolMetadataError::EmptyText {
            label: label.to_string(),
        })
    } else {
        Ok(())
    }
}

impl McpInputShape {
    /// Returns whether this shape accepts model-controlled MCP input.
    pub const fn supported(self) -> bool {
        !matches!(self, Self::Unsupported)
    }
}

/// Shape-owned metadata for model-controlled MCP input.
///
/// This is metadata only. Protocol handling, JSON decoding, validation, and
/// application authorization stay in MCP integration crates.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct McpInput {
    input_shape: McpInputShape,
}

impl McpInput {
    /// No model-controlled MCP input is supported.
    pub const fn unsupported() -> Self {
        Self {
            input_shape: McpInputShape::Unsupported,
        }
    }

    /// Accept any JSON value.
    pub const fn any() -> Self {
        Self::scalar(McpPrimitiveKind::Any)
    }

    /// Accept a boolean scalar.
    pub const fn boolean() -> Self {
        Self::scalar(McpPrimitiveKind::Boolean)
    }

    /// Accept an integer scalar.
    pub const fn integer() -> Self {
        Self::scalar(McpPrimitiveKind::Integer)
    }

    /// Accept a number scalar.
    pub const fn number() -> Self {
        Self::scalar(McpPrimitiveKind::Number)
    }

    /// Accept a decimal scalar encoded as a JSON number or string.
    pub const fn decimal() -> Self {
        Self::scalar(McpPrimitiveKind::Decimal)
    }

    /// Accept a string scalar.
    pub const fn string() -> Self {
        Self::scalar(McpPrimitiveKind::String)
    }

    /// Accept an RFC 3339 full-date string.
    pub const fn date() -> Self {
        Self::scalar(McpPrimitiveKind::Date)
    }

    /// Accept an RFC 3339 date-time string.
    pub const fn date_time() -> Self {
        Self::scalar(McpPrimitiveKind::DateTime)
    }

    /// Accept a scalar of the given primitive kind.
    pub const fn scalar(kind: McpPrimitiveKind) -> Self {
        Self {
            input_shape: McpInputShape::Scalar(kind),
        }
    }

    /// Accept an ordered array of strings.
    pub const fn string_list() -> Self {
        Self::list(McpPrimitiveKind::String)
    }

    /// Accept an ordered array of booleans.
    pub const fn boolean_list() -> Self {
        Self::list(McpPrimitiveKind::Boolean)
    }

    /// Accept an ordered array of integers.
    pub const fn integer_list() -> Self {
        Self::list(McpPrimitiveKind::Integer)
    }

    /// Accept an ordered array of numbers.
    pub const fn number_list() -> Self {
        Self::list(McpPrimitiveKind::Number)
    }

    /// Accept an ordered array of decimals encoded as JSON numbers or strings.
    pub const fn decimal_list() -> Self {
        Self::list(McpPrimitiveKind::Decimal)
    }

    /// Accept an ordered array of RFC 3339 full-date strings.
    pub const fn date_list() -> Self {
        Self::list(McpPrimitiveKind::Date)
    }

    /// Accept an ordered array of RFC 3339 date-time strings.
    pub const fn date_time_list() -> Self {
        Self::list(McpPrimitiveKind::DateTime)
    }

    /// Accept an ordered array of primitive values.
    pub const fn list(items: McpPrimitiveKind) -> Self {
        Self {
            input_shape: McpInputShape::List(items),
        }
    }

    /// Accept a unique array of strings.
    pub const fn string_set() -> Self {
        Self::set(McpPrimitiveKind::String)
    }

    /// Accept a unique array of booleans.
    pub const fn boolean_set() -> Self {
        Self::set(McpPrimitiveKind::Boolean)
    }

    /// Accept a unique array of integers.
    pub const fn integer_set() -> Self {
        Self::set(McpPrimitiveKind::Integer)
    }

    /// Accept a unique array of numbers.
    pub const fn number_set() -> Self {
        Self::set(McpPrimitiveKind::Number)
    }

    /// Accept a unique array of decimals encoded as JSON numbers or strings.
    pub const fn decimal_set() -> Self {
        Self::set(McpPrimitiveKind::Decimal)
    }

    /// Accept a unique array of RFC 3339 full-date strings.
    pub const fn date_set() -> Self {
        Self::set(McpPrimitiveKind::Date)
    }

    /// Accept a unique array of RFC 3339 date-time strings.
    pub const fn date_time_set() -> Self {
        Self::set(McpPrimitiveKind::DateTime)
    }

    /// Accept a unique array of primitive values.
    pub const fn set(items: McpPrimitiveKind) -> Self {
        Self {
            input_shape: McpInputShape::Set(items),
        }
    }

    /// Accept a decimal `{ "min": ..., "max": ... }` range object.
    pub const fn decimal_range() -> Self {
        Self::range(McpRangeBoundKind::Decimal)
    }

    /// Accept an integer `{ "min": ..., "max": ... }` range object.
    pub const fn integer_range() -> Self {
        Self::range(McpRangeBoundKind::Integer)
    }

    /// Accept a number `{ "min": ..., "max": ... }` range object.
    pub const fn number_range() -> Self {
        Self::range(McpRangeBoundKind::Number)
    }

    /// Accept a date `{ "min": ..., "max": ... }` range object.
    pub const fn date_range() -> Self {
        Self::range(McpRangeBoundKind::Date)
    }

    /// Accept a date-time `{ "min": ..., "max": ... }` range object.
    pub const fn date_time_range() -> Self {
        Self::range(McpRangeBoundKind::DateTime)
    }

    /// Accept a `{ "min": ..., "max": ... }` range object of the given kind.
    pub const fn range(bound: McpRangeBoundKind) -> Self {
        Self {
            input_shape: McpInputShape::Range(bound),
        }
    }

    /// Accept an object with integration-defined structure.
    pub const fn object() -> Self {
        Self {
            input_shape: McpInputShape::Object,
        }
    }

    /// Return the structured input shape exposed to MCP integrations.
    pub const fn input_shape(self) -> McpInputShape {
        self.input_shape
    }

    /// Whether this metadata describes any supported input shape.
    pub const fn supported(self) -> bool {
        self.input_shape.supported()
    }
}

impl Default for McpInput {
    fn default() -> Self {
        Self::unsupported()
    }
}

#[cfg(test)]
mod tests {
    use super::{
        McpInput, McpInputShape, McpPrimitiveKind, McpRangeBoundKind,
        validate_mcp_tool_metadata_text, validate_mcp_tool_name,
    };

    #[test]
    fn mcp_input_defaults_to_unsupported() {
        let input = McpInput::default();

        assert!(!input.supported());
        assert_eq!(input.input_shape(), McpInputShape::Unsupported);
    }

    #[test]
    fn mcp_input_records_shape() {
        let input = McpInput::range(McpRangeBoundKind::Date);

        assert!(input.supported());
        assert_eq!(
            input.input_shape(),
            McpInputShape::Range(McpRangeBoundKind::Date)
        );
    }

    #[test]
    fn mcp_input_convenience_constructors_match_common_shapes() {
        assert_eq!(
            McpInput::string().input_shape(),
            McpInputShape::Scalar(McpPrimitiveKind::String)
        );
        assert_eq!(
            McpInput::string_list().input_shape(),
            McpInputShape::List(McpPrimitiveKind::String)
        );
        assert_eq!(
            McpInput::string_set().input_shape(),
            McpInputShape::Set(McpPrimitiveKind::String)
        );
        assert_eq!(
            McpInput::date_range().input_shape(),
            McpInputShape::Range(McpRangeBoundKind::Date)
        );
        assert_eq!(
            McpInput::decimal_range().input_shape(),
            McpInputShape::Range(McpRangeBoundKind::Decimal)
        );
        assert_eq!(
            McpInput::integer_range().input_shape(),
            McpInputShape::Range(McpRangeBoundKind::Integer)
        );
        assert_eq!(
            McpInput::date_time_range().input_shape(),
            McpInputShape::Range(McpRangeBoundKind::DateTime)
        );
        assert_eq!(
            McpInput::decimal_list().input_shape(),
            McpInputShape::List(McpPrimitiveKind::Decimal)
        );
        assert_eq!(
            McpInput::decimal_set().input_shape(),
            McpInputShape::Set(McpPrimitiveKind::Decimal)
        );
    }

    #[test]
    fn all_mcp_input_convenience_constructors_match_their_shapes() {
        let scalar_cases: [(fn() -> McpInput, McpPrimitiveKind); 8] = [
            (McpInput::any, McpPrimitiveKind::Any),
            (McpInput::boolean, McpPrimitiveKind::Boolean),
            (McpInput::integer, McpPrimitiveKind::Integer),
            (McpInput::number, McpPrimitiveKind::Number),
            (McpInput::decimal, McpPrimitiveKind::Decimal),
            (McpInput::string, McpPrimitiveKind::String),
            (McpInput::date, McpPrimitiveKind::Date),
            (McpInput::date_time, McpPrimitiveKind::DateTime),
        ];
        for (constructor, kind) in scalar_cases {
            assert_eq!(constructor().input_shape(), McpInputShape::Scalar(kind));
        }

        let collection_cases: [(fn() -> McpInput, McpInputShape); 14] = [
            (
                McpInput::string_list,
                McpInputShape::List(McpPrimitiveKind::String),
            ),
            (
                McpInput::boolean_list,
                McpInputShape::List(McpPrimitiveKind::Boolean),
            ),
            (
                McpInput::integer_list,
                McpInputShape::List(McpPrimitiveKind::Integer),
            ),
            (
                McpInput::number_list,
                McpInputShape::List(McpPrimitiveKind::Number),
            ),
            (
                McpInput::decimal_list,
                McpInputShape::List(McpPrimitiveKind::Decimal),
            ),
            (
                McpInput::date_list,
                McpInputShape::List(McpPrimitiveKind::Date),
            ),
            (
                McpInput::date_time_list,
                McpInputShape::List(McpPrimitiveKind::DateTime),
            ),
            (
                McpInput::string_set,
                McpInputShape::Set(McpPrimitiveKind::String),
            ),
            (
                McpInput::boolean_set,
                McpInputShape::Set(McpPrimitiveKind::Boolean),
            ),
            (
                McpInput::integer_set,
                McpInputShape::Set(McpPrimitiveKind::Integer),
            ),
            (
                McpInput::number_set,
                McpInputShape::Set(McpPrimitiveKind::Number),
            ),
            (
                McpInput::decimal_set,
                McpInputShape::Set(McpPrimitiveKind::Decimal),
            ),
            (
                McpInput::date_set,
                McpInputShape::Set(McpPrimitiveKind::Date),
            ),
            (
                McpInput::date_time_set,
                McpInputShape::Set(McpPrimitiveKind::DateTime),
            ),
        ];
        for (constructor, shape) in collection_cases {
            assert_eq!(constructor().input_shape(), shape);
        }

        let range_cases: [(fn() -> McpInput, McpRangeBoundKind); 5] = [
            (McpInput::integer_range, McpRangeBoundKind::Integer),
            (McpInput::number_range, McpRangeBoundKind::Number),
            (McpInput::decimal_range, McpRangeBoundKind::Decimal),
            (McpInput::date_range, McpRangeBoundKind::Date),
            (McpInput::date_time_range, McpRangeBoundKind::DateTime),
        ];
        for (constructor, kind) in range_cases {
            assert_eq!(constructor().input_shape(), McpInputShape::Range(kind));
            assert_eq!(McpPrimitiveKind::from(kind), kind.primitive_kind());
        }

        assert_eq!(McpInput::object().input_shape(), McpInputShape::Object);
    }

    #[test]
    fn mcp_kind_names_are_stable_metadata() {
        assert_eq!(McpPrimitiveKind::Any.as_str(), "any");
        assert_eq!(McpPrimitiveKind::Boolean.as_str(), "boolean");
        assert_eq!(McpPrimitiveKind::Integer.as_str(), "integer");
        assert_eq!(McpPrimitiveKind::Number.as_str(), "number");
        assert_eq!(McpPrimitiveKind::Decimal.as_str(), "decimal");
        assert_eq!(McpPrimitiveKind::String.as_str(), "string");
        assert_eq!(McpPrimitiveKind::Date.as_str(), "date");
        assert_eq!(McpPrimitiveKind::DateTime.as_str(), "date_time");

        assert_eq!(McpRangeBoundKind::Integer.as_str(), "integer");
        assert_eq!(McpRangeBoundKind::Number.as_str(), "number");
        assert_eq!(McpRangeBoundKind::Decimal.as_str(), "decimal");
        assert_eq!(McpRangeBoundKind::Date.as_str(), "date");
        assert_eq!(McpRangeBoundKind::DateTime.as_str(), "date_time");
    }

    #[test]
    fn mcp_tool_name_validation_matches_generated_tool_contract() {
        assert!(validate_mcp_tool_name("query.users-1").is_ok());
        assert_eq!(
            validate_mcp_tool_name(""),
            Err(super::McpToolMetadataError::EmptyName)
        );
        assert_eq!(
            validate_mcp_tool_name("_query"),
            Err(super::McpToolMetadataError::InvalidNameStart)
        );
        assert_eq!(
            validate_mcp_tool_name("query users"),
            Err(super::McpToolMetadataError::InvalidNameCharacter)
        );
    }

    #[test]
    fn mcp_tool_metadata_text_validation_rejects_blank_text() {
        let error =
            validate_mcp_tool_metadata_text("title", "  ").expect_err("blank title should fail");

        assert_eq!(error.to_string(), "tool title cannot be empty");
        assert!(validate_mcp_tool_metadata_text("title", "Readable title").is_ok());
    }
}