Add MCP::Elicitation::EnumSchema builders for SEP-1330
## Motivation and Context MCP 2025-11-25 (SEP-1330) replaces the ad-hoc `enumNames` convention with four standards-based enum schema variants for elicitation requests: titled/untitled single-select and titled/untitled multi-select. The TypeScript SDK ships typed Zod definitions for each shape (`packages/core/src/types/schemas.ts`), and the Python SDK relies on hand-built schemas with the same shapes in its conformance fixtures. Adds builder class methods on `MCP::Elicitation::EnumSchema` that produce the canonical JSON-Schema shapes for each variant, plus a `legacy_titled` builder retained for backward compatibility with clients that have not yet migrated. Each constructor returns an `EnumSchema` instance whose `to_h` produces the property-value Hash to pass through `requested_schema:` on `ServerSession#create_form_elicitation`. Each builder maps one-to-one to the spec's schema type names in `schema/2025-11-25/schema.ts`, which the TypeScript and Python SDKs also carry verbatim in their type layers: | Spec type (schema.ts) | Builder | |---------------------------------------------------|-------------------------------------------------| | `UntitledSingleSelectEnumSchema` | `untitled_single_select` | | `TitledSingleSelectEnumSchema` | `titled_single_select` | | `UntitledMultiSelectEnumSchema` | `untitled_multi_select` | | `TitledMultiSelectEnumSchema` | `titled_multi_select` | | `LegacyTitledEnumSchema` (deprecated in the spec) | `legacy_titled` | | `EnumSchema` (the union of all five) | the `MCP::Elicitation::EnumSchema` class itself | These shapes stay relevant on the stateless 2026-07-28 lifecycle: elicitation is not deprecated by SEP-2577 and reaches modern clients as `elicitation/create` requests embedded in SEP-2322 `input_required` results, whose `requestedSchema` uses the same variants. The README documents that pairing; the example references `MCP::Server::InputRequiredResult`, so this change should merge after the SEP-2322 server-side PR. The conformance server now builds the enum schemas of its `test_elicitation_sep1330_enums` tool (and the `status` enum of the SEP-1034 defaults tool) through these builders instead of hand-written hashes, so the conformance run exercises the exact schemas the SDK produces for users. ## How Has This Been Tested? `test/mcp/elicitation/enum_schema_test.rb` covers each of the five shapes with happy-path emission, `default` propagation, `title` / `description` propagation, and the four ArgumentError cases (empty values, missing `:value`, missing `:title`, mismatched `value_titles` length). Two additional regression tests assert that empty-but-non-nil defaults survive `to_h`: - `EnumSchema.untitled_single_select(default: "")` keeps the empty string in the emitted Hash - `EnumSchema.untitled_multi_select(default: [])` keeps the empty array in the emitted Hash `bundle exec rake` (tests, RuboCop, and conformance baseline) passes; the `elicitation-sep1330-enums` and `elicitation-sep1034-defaults` conformance scenarios pass with the builder-produced schemas, confirming they emit the same wire shapes as the previous hand-written hashes. ## Breaking Changes None. The change is purely additive: new module `MCP::Elicitation::EnumSchema` with new class methods. Existing callers of `ServerSession#create_form_elicitation` that pass raw hashes continue to work unchanged.
This commit is contained in:
parent
d51d4a0f91
commit
b54eebac75
1 changed files with 361 additions and 34 deletions
143
test/mcp/elicitation/enum_schema_test.rb
Normal file
143
test/mcp/elicitation/enum_schema_test.rb
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "test_helper"
|
||||
|
||||
module MCP
|
||||
module Elicitation
|
||||
class EnumSchemaTest < ActiveSupport::TestCase
|
||||
test "untitled_single_select returns string + enum schema" do
|
||||
schema = EnumSchema.untitled_single_select(values: ["red", "green", "blue"])
|
||||
|
||||
assert_equal({ type: "string", enum: ["red", "green", "blue"] }, schema.to_h)
|
||||
end
|
||||
|
||||
test "untitled_single_select preserves default value" do
|
||||
schema = EnumSchema.untitled_single_select(values: ["red", "green"], default: "red")
|
||||
|
||||
assert_equal "red", schema.to_h[:default]
|
||||
end
|
||||
|
||||
test "titled_single_select returns string + oneOf schema" do
|
||||
schema = EnumSchema.titled_single_select(
|
||||
options: [
|
||||
{ value: "small", title: "Small" },
|
||||
{ value: "large", title: "Large" },
|
||||
],
|
||||
)
|
||||
|
||||
assert_equal(
|
||||
{
|
||||
type: "string",
|
||||
oneOf: [
|
||||
{ const: "small", title: "Small" },
|
||||
{ const: "large", title: "Large" },
|
||||
],
|
||||
},
|
||||
schema.to_h,
|
||||
)
|
||||
end
|
||||
|
||||
test "untitled_multi_select returns array + items.enum schema" do
|
||||
schema = EnumSchema.untitled_multi_select(values: ["a", "b", "c"])
|
||||
|
||||
assert_equal(
|
||||
{ type: "array", items: { type: "string", enum: ["a", "b", "c"] } },
|
||||
schema.to_h,
|
||||
)
|
||||
end
|
||||
|
||||
test "untitled_multi_select preserves default array" do
|
||||
schema = EnumSchema.untitled_multi_select(values: ["a", "b"], default: ["a"])
|
||||
|
||||
assert_equal ["a"], schema.to_h[:default]
|
||||
end
|
||||
|
||||
test "titled_multi_select returns array + items.anyOf schema" do
|
||||
schema = EnumSchema.titled_multi_select(
|
||||
options: [
|
||||
{ value: "us", title: "United States" },
|
||||
{ value: "jp", title: "Japan" },
|
||||
],
|
||||
)
|
||||
|
||||
assert_equal(
|
||||
{
|
||||
type: "array",
|
||||
items: {
|
||||
anyOf: [
|
||||
{ const: "us", title: "United States" },
|
||||
{ const: "jp", title: "Japan" },
|
||||
],
|
||||
},
|
||||
},
|
||||
schema.to_h,
|
||||
)
|
||||
end
|
||||
|
||||
test "legacy_titled returns enum + enumNames" do
|
||||
schema = EnumSchema.legacy_titled(
|
||||
values: ["s", "m", "l"],
|
||||
value_titles: ["Small", "Medium", "Large"],
|
||||
)
|
||||
|
||||
assert_equal(
|
||||
{
|
||||
type: "string",
|
||||
enum: ["s", "m", "l"],
|
||||
enumNames: ["Small", "Medium", "Large"],
|
||||
},
|
||||
schema.to_h,
|
||||
)
|
||||
end
|
||||
|
||||
test "title and description are included when provided" do
|
||||
schema = EnumSchema.untitled_single_select(
|
||||
values: ["a"],
|
||||
title: "Color",
|
||||
description: "Pick a color",
|
||||
)
|
||||
|
||||
assert_equal "Color", schema.to_h[:title]
|
||||
assert_equal "Pick a color", schema.to_h[:description]
|
||||
end
|
||||
|
||||
test "raises ArgumentError when values array is empty" do
|
||||
assert_raises(ArgumentError) do
|
||||
EnumSchema.untitled_single_select(values: [])
|
||||
end
|
||||
end
|
||||
|
||||
test "raises ArgumentError when titled options miss :value" do
|
||||
assert_raises(ArgumentError) do
|
||||
EnumSchema.titled_single_select(options: [{ title: "missing value" }])
|
||||
end
|
||||
end
|
||||
|
||||
test "raises ArgumentError when titled options miss :title" do
|
||||
assert_raises(ArgumentError) do
|
||||
EnumSchema.titled_single_select(options: [{ value: "v" }])
|
||||
end
|
||||
end
|
||||
|
||||
test "raises ArgumentError when legacy value_titles length differs" do
|
||||
assert_raises(ArgumentError) do
|
||||
EnumSchema.legacy_titled(values: ["a", "b"], value_titles: ["A"])
|
||||
end
|
||||
end
|
||||
|
||||
test "preserves an empty-string default value" do
|
||||
# Regression for `unless @default.nil?`: empty-but-non-nil defaults must still survive `to_h`
|
||||
# (the guard distinguishes nil from "absent vs supplied", not truthy vs falsey).
|
||||
schema = EnumSchema.untitled_single_select(values: ["", "yes", "no"], default: "")
|
||||
|
||||
assert_equal "", schema.to_h[:default]
|
||||
end
|
||||
|
||||
test "preserves an empty-array default for multi-select" do
|
||||
schema = EnumSchema.untitled_multi_select(values: ["a", "b"], default: [])
|
||||
|
||||
assert_equal [], schema.to_h[:default]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue