diff --git a/pkgs/mcpx/docs/spec/conformance-matrix.md b/pkgs/mcpx/docs/spec/conformance-matrix.md index 4d7d8063e3..fb678193f2 100644 --- a/pkgs/mcpx/docs/spec/conformance-matrix.md +++ b/pkgs/mcpx/docs/spec/conformance-matrix.md @@ -2,7 +2,7 @@ Generated by `internal/conformance` (`MCPX_UPDATE_MATRIX=1 go test ./internal/conformance -run TestMatrix`); do not edit. -Generated from inputs `819a4c9fcb265a84` (sha256 of docs/spec/requirements.md, internal/conformance/overrides/*.json, internal/conformance/coverage*.go). `TestMatrix` regenerates this file and fails when it differs, so it describes the tree it is committed in. +Generated from inputs `d87c25241c253b00` (sha256 of docs/spec/requirements.md, internal/conformance/overrides/*.json, internal/conformance/coverage*.go). `TestMatrix` regenerates this file and fails when it differs, so it describes the tree it is committed in. Every normative requirement of every revision (catalogue: `docs/spec/requirements.md`), for mcpx as a server and as a client. A requirement is *tested* when a named test verifies it in that revision, *n/a* when it does not bind mcpx in that role, and a *gap* when it binds mcpx and mcpx does not meet it yet, with the issue that tracks it. Run exactly the covering tests with `go run ./internal/conformance/cmd/covering`. @@ -36,37 +36,37 @@ Every normative requirement of every revision (catalogue: `docs/spec/requirement | 2025-03-26 | client | other | 7 | 7 | 0 | 0 | 5 | | 2025-06-18 | server | MUST | 65 | 50 | 15 | 0 | 36 | | 2025-06-18 | server | MUST NOT | 10 | 9 | 1 | 0 | 8 | -| 2025-06-18 | server | SHOULD | 52 | 45 | 7 | 0 | 63 | +| 2025-06-18 | server | SHOULD | 52 | 46 | 6 | 0 | 63 | | 2025-06-18 | server | SHOULD NOT | 4 | 3 | 1 | 0 | 1 | | 2025-06-18 | server | MAY | 17 | 15 | 2 | 0 | 18 | | 2025-06-18 | server | other | 15 | 15 | 0 | 0 | 3 | | 2025-06-18 | client | MUST | 52 | 36 | 16 | 0 | 49 | | 2025-06-18 | client | MUST NOT | 13 | 11 | 2 | 0 | 5 | -| 2025-06-18 | client | SHOULD | 52 | 39 | 13 | 0 | 63 | +| 2025-06-18 | client | SHOULD | 52 | 40 | 12 | 0 | 63 | | 2025-06-18 | client | SHOULD NOT | 2 | 1 | 1 | 0 | 3 | | 2025-06-18 | client | MAY | 15 | 13 | 2 | 0 | 20 | | 2025-06-18 | client | other | 10 | 8 | 2 | 0 | 8 | | 2025-11-25 | server | MUST | 115 | 97 | 18 | 0 | 65 | | 2025-11-25 | server | MUST NOT | 22 | 19 | 3 | 0 | 13 | -| 2025-11-25 | server | SHOULD | 78 | 62 | 16 | 0 | 81 | +| 2025-11-25 | server | SHOULD | 78 | 63 | 15 | 0 | 81 | | 2025-11-25 | server | SHOULD NOT | 11 | 10 | 1 | 0 | 3 | | 2025-11-25 | server | MAY | 27 | 25 | 2 | 0 | 28 | | 2025-11-25 | server | other | 19 | 19 | 0 | 0 | 2 | | 2025-11-25 | client | MUST | 76 | 56 | 20 | 0 | 104 | | 2025-11-25 | client | MUST NOT | 16 | 14 | 2 | 0 | 19 | -| 2025-11-25 | client | SHOULD | 67 | 46 | 21 | 0 | 92 | +| 2025-11-25 | client | SHOULD | 67 | 47 | 20 | 0 | 92 | | 2025-11-25 | client | SHOULD NOT | 5 | 4 | 1 | 0 | 9 | | 2025-11-25 | client | MAY | 20 | 17 | 3 | 0 | 35 | | 2025-11-25 | client | other | 13 | 11 | 2 | 0 | 8 | | 2026-07-28 | server | MUST | 128 | 108 | 20 | 0 | 96 | | 2026-07-28 | server | MUST NOT | 44 | 42 | 2 | 0 | 20 | -| 2026-07-28 | server | SHOULD | 74 | 61 | 13 | 0 | 86 | +| 2026-07-28 | server | SHOULD | 74 | 62 | 12 | 0 | 86 | | 2026-07-28 | server | SHOULD NOT | 11 | 10 | 1 | 0 | 9 | | 2026-07-28 | server | MAY | 31 | 29 | 2 | 0 | 30 | | 2026-07-28 | server | other | 23 | 22 | 1 | 0 | 4 | | 2026-07-28 | client | MUST | 91 | 64 | 27 | 0 | 133 | | 2026-07-28 | client | MUST NOT | 25 | 23 | 2 | 0 | 39 | -| 2026-07-28 | client | SHOULD | 74 | 54 | 20 | 0 | 86 | +| 2026-07-28 | client | SHOULD | 74 | 55 | 19 | 0 | 86 | | 2026-07-28 | client | SHOULD NOT | 13 | 11 | 2 | 0 | 7 | | 2026-07-28 | client | MAY | 14 | 11 | 3 | 0 | 47 | | 2026-07-28 | client | other | 13 | 11 | 2 | 0 | 14 | @@ -862,7 +862,7 @@ Every normative requirement of every revision (catalogue: `docs/spec/requirement | [elicitation-client-allow-decline-any-time](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#security-considerations) | SHOULD | client | n/a: addresses the other side | tested: `elicit.TestAnsweringAfterTheProcessDiedStillWorks@2025-06-18,2025-11-25,2026-07-28` | | [elicitation-client-rate-limit](https://modelcontextprotocol.io/specification/2025-11-25/client/elicitation#security-considerations) | SHOULD | client | n/a: addresses the other side | **gap** #257: no per-server limit on elicitation requests | | [elicitation-client-clear-what-and-why](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#security-considerations) | SHOULD | client | n/a: addresses the other side | tested: `conformance.TestClientFeaturesClient/2025-06-18/elicitation/elicitation-client-clear-what-and-why` | -| [elicitation-both-validate-against-schema](https://modelcontextprotocol.io/specification/2025-06-18/client/elicitation#security-considerations) | SHOULD | both | **gap** #255: an answer is checked only for non-empty content, not against requestedSchema | **gap** #255: an answer is checked only for non-empty content, not against requestedSchema | +| [elicitation-both-validate-against-schema](https://modelcontextprotocol.io/specification/2025-06-18/client/elicitation#security-considerations) | SHOULD | both | tested: `mcpclient.TestElicitationAnswersValidatedAgainstRequestedSchema@2025-06-18` | tested: `mcpclient.TestElicitationAnswersValidatedAgainstRequestedSchema@2025-06-18` | ### errors @@ -1341,8 +1341,8 @@ Every normative requirement of every revision (catalogue: `docs/spec/requirement | [elicitation-client-allow-decline-any-time](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#security-considerations) | SHOULD | client | n/a: addresses the other side | tested: `elicit.TestAnsweringAfterTheProcessDiedStillWorks@2025-06-18,2025-11-25,2026-07-28` | | [elicitation-client-rate-limit](https://modelcontextprotocol.io/specification/2025-11-25/client/elicitation#security-considerations) | SHOULD | client | n/a: addresses the other side | **gap** #257: no per-server limit on elicitation requests | | [elicitation-client-clear-what-and-why](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#security-considerations) | SHOULD | client | n/a: addresses the other side | tested: `conformance.TestClientFeaturesClient/2025-11-25/elicitation/elicitation-client-clear-what-and-why` | -| [elicitation-client-validate-responses](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#form-mode-security) | SHOULD | client | n/a: addresses the other side | **gap** #255: an answer is checked only for non-empty content, not against requestedSchema | -| [elicitation-server-validate-received-data](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#form-mode-security) | SHOULD | server | **gap** #255: a client answer is relayed upstream without a schema check | n/a: addresses the other side | +| [elicitation-client-validate-responses](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#form-mode-security) | SHOULD | client | n/a: addresses the other side | tested: `mcpclient.TestElicitationAnswersValidatedAgainstRequestedSchema/2025-11-25/elicitation/wrong-typed-answer-is-32602-not-relayed` | +| [elicitation-server-validate-received-data](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#form-mode-security) | SHOULD | server | tested: `mcpclient.TestElicitationAnswersValidatedAgainstRequestedSchema/2025-11-25/elicitation/wrong-typed-answer-is-32602-not-relayed` | n/a: addresses the other side | | [elicitation-url-no-sensitive-user-info](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#safe-url-handling) | MUST NOT | server | tested: `conformance.TestClientFeaturesServer/2025-11-25/elicitation/elicitation-url-no-sensitive-user-info` | n/a: addresses the other side | | [elicitation-url-not-pre-authenticated](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#safe-url-handling) | MUST NOT | server | tested: `conformance.TestClientFeaturesServer/2025-11-25/elicitation/elicitation-url-not-pre-authenticated` | n/a: addresses the other side | | [elicitation-no-clickable-urls-in-form](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#safe-url-handling) | SHOULD NOT | server | tested: `conformance.TestClientFeaturesServer/2025-11-25/elicitation/elicitation-no-clickable-urls-in-form` | n/a: addresses the other side | @@ -2011,8 +2011,8 @@ Every normative requirement of every revision (catalogue: `docs/spec/requirement | [elicitation-client-user-approval-controls](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#security-considerations) | SHOULD | client | n/a: addresses the other side | tested: `elicit.TestRoutingSendsCredentialsToAHumanAndTheRestToTheAgent@2025-06-18,2025-11-25,2026-07-28` | | [elicitation-client-allow-decline-any-time](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#security-considerations) | SHOULD | client | n/a: addresses the other side | tested: `elicit.TestAnsweringAfterTheProcessDiedStillWorks@2025-06-18,2025-11-25,2026-07-28` | | [elicitation-client-clear-what-and-why](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#security-considerations) | SHOULD | client | n/a: addresses the other side | tested: `conformance.TestClientFeaturesClient/2026-07-28/elicitation/elicitation-client-clear-what-and-why` | -| [elicitation-client-validate-responses](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#form-mode-security) | SHOULD | client | n/a: addresses the other side | **gap** #255: an answer is checked only for non-empty content, not against requestedSchema | -| [elicitation-server-validate-received-data](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#form-mode-security) | SHOULD | server | **gap** #255: a client answer is relayed upstream without a schema check | n/a: addresses the other side | +| [elicitation-client-validate-responses](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#form-mode-security) | SHOULD | client | n/a: addresses the other side | tested: `mcpclient.TestElicitationAnswersValidatedAgainstRequestedSchema/2026-07-28/elicitation/input-required-wrong-typed-answer-is-rejected` | +| [elicitation-server-validate-received-data](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#form-mode-security) | SHOULD | server | tested: `mcpclient.TestElicitationAnswersValidatedAgainstRequestedSchema/2026-07-28/elicitation/input-required-wrong-typed-answer-is-rejected` | n/a: addresses the other side | | [elicitation-url-no-sensitive-user-info](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#safe-url-handling) | MUST NOT | server | tested: `conformance.TestClientFeaturesServer/2026-07-28/elicitation/elicitation-url-no-sensitive-user-info` | n/a: addresses the other side | | [elicitation-url-not-pre-authenticated](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#safe-url-handling) | MUST NOT | server | tested: `conformance.TestClientFeaturesServer/2026-07-28/elicitation/elicitation-url-not-pre-authenticated` | n/a: addresses the other side | | [elicitation-no-clickable-urls-in-form](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#safe-url-handling) | SHOULD NOT | server | tested: `conformance.TestClientFeaturesServer/2026-07-28/elicitation/elicitation-no-clickable-urls-in-form` | n/a: addresses the other side | diff --git a/pkgs/mcpx/internal/conformance/coverage_clientfeatures.go b/pkgs/mcpx/internal/conformance/coverage_clientfeatures.go index 087bcfb5c0..bd24100859 100644 --- a/pkgs/mcpx/internal/conformance/coverage_clientfeatures.go +++ b/pkgs/mcpx/internal/conformance/coverage_clientfeatures.go @@ -11,6 +11,12 @@ func init() { const storeProtected = "elicit.TestOpenAtCreatesOwnerOnlyStore@2025-11-25,2026-07-28" const storeMigrated = "elicit.TestOpenAtMigratesLegacyStore@2025-11-25,2026-07-28" register( + C("elicitation-client-validate-responses", "mcpclient.TestElicitationAnswersValidatedAgainstRequestedSchema/2025-11-25/elicitation/wrong-typed-answer-is-32602-not-relayed", + "mcpclient.TestElicitationAnswersValidatedAgainstRequestedSchema/2026-07-28/elicitation/input-required-wrong-typed-answer-is-rejected"), + C("elicitation-both-validate-against-schema", "mcpclient.TestElicitationAnswersValidatedAgainstRequestedSchema@2025-06-18"), + S("elicitation-both-validate-against-schema", "mcpclient.TestElicitationAnswersValidatedAgainstRequestedSchema@2025-06-18"), + S("elicitation-server-validate-received-data", "mcpclient.TestElicitationAnswersValidatedAgainstRequestedSchema/2025-11-25/elicitation/wrong-typed-answer-is-32602-not-relayed", + "mcpclient.TestElicitationAnswersValidatedAgainstRequestedSchema/2026-07-28/elicitation/input-required-wrong-typed-answer-is-rejected"), C("elicitation-client-32602-on-undeclared-mode", "mcpclient.TestElicitationDeclarations/2025-11-25/elicitation/undeclared-mode-is-32602"), C("sampling-client-error-on-tools-without-capability", "mcpclient.TestSamplingDeclarations/2025-11-25/sampling/tools-without-capability-is-32602", "mcpclient.TestModernSamplingShape/2026-07-28/sampling/tools-without-capability-is-32602"), @@ -130,10 +136,6 @@ func init() { Gap{ID: "elicitation-client-handle-urls-carefully", Side: Client, Issue: "#255", Why: "mcpx elicit prints the raw request: no review step, no domain emphasis or punycode warning, no defaults pre-filled"}, Gap{ID: "elicitation-client-highlight-domain", Side: Client, Issue: "#255", Why: "mcpx elicit prints the raw request: no review step, no domain emphasis or punycode warning, no defaults pre-filled"}, Gap{ID: "elicitation-client-warn-punycode", Side: Client, Issue: "#255", Why: "mcpx elicit prints the raw request: no review step, no domain emphasis or punycode warning, no defaults pre-filled"}, - Gap{ID: "elicitation-client-validate-responses", Side: Client, Issue: "#255", Why: "an answer is checked only for non-empty content, not against requestedSchema"}, - Gap{ID: "elicitation-both-validate-against-schema", Side: Client, Issue: "#255", Why: "an answer is checked only for non-empty content, not against requestedSchema"}, - Gap{ID: "elicitation-both-validate-against-schema", Side: Server, Issue: "#255", Why: "an answer is checked only for non-empty content, not against requestedSchema"}, - Gap{ID: "elicitation-server-validate-received-data", Side: Server, Issue: "#255", Why: "a client answer is relayed upstream without a schema check"}, Gap{ID: "elicitation-server-must-return-32042", Side: Server, Issue: "#214", Why: "a url question for a client that cannot take it stays with the broker; -32042 is never returned"}, Gap{ID: "elicitation-server-state-bound-to-user", Side: Server, Issue: "#257", Why: "mcpx has no authenticated user: state is bound to the request, the session id is whatever the caller sends"}, Gap{ID: "elicitation-server-stored-state-bound-to-user", Side: Server, Issue: "#257", Why: "mcpx has no authenticated user: state is bound to the request, the session id is whatever the caller sends"}, diff --git a/pkgs/mcpx/internal/mcpclient/elicitation_schema.go b/pkgs/mcpx/internal/mcpclient/elicitation_schema.go new file mode 100644 index 0000000000..2997cb9040 --- /dev/null +++ b/pkgs/mcpx/internal/mcpclient/elicitation_schema.go @@ -0,0 +1,73 @@ +package mcpclient + +import ( + "encoding/json" + "fmt" + + "github.com/dezren39/mcpx/internal/schemaval" +) + +// This file wires internal/schemaval into elicitation answers (Phase 3 +// of #1481, issue #1484). Client.answer is the single choke point every +// elicitation answer passes through — a server's own elicitation/create +// request and a 2026-07-28 input_required round alike — so the check +// lives there, after applyDefaults has filled omitted fields from the +// schema's defaults: an omitted field is valid only once its default +// is in. + +// checkElicitationAnswer validates an accepted form answer's content +// against the requestedSchema the requesting server itself supplied. +// +// - A validation failure is Invalid Params (-32602) to the requesting +// server, naming the failing paths, plus a Warning; the answer is +// NOT relayed — the unvalidated answer never reaches the upstream. +// - A requestedSchema that does not compile is the server's own +// defect, not the answerer's: warn and relay the answer +// unvalidated rather than wedging the user's answer on a schema +// they never wrote. +// - Decline/cancel answers (no content to validate), URL mode, and +// requests without a requestedSchema pass through untouched. +func (c *Client) checkElicitationAnswer(out any, params json.RawMessage) (any, *rpcError) { + var p struct { + Mode string `json:"mode"` + RequestedSchema json.RawMessage `json:"requestedSchema"` + } + if json.Unmarshal(params, &p) != nil || len(p.RequestedSchema) == 0 { + return out, nil + } + // Undeclared mode is form (the codebase already treats it so, and + // elicitModeDeclared has already refused any other undeclared + // mode); a declared non-form mode — URL — carries no form content + // to validate. + if p.Mode != "" && p.Mode != "form" { + return out, nil + } + b, err := json.Marshal(out) + if err != nil { + return out, nil + } + var res map[string]json.RawMessage + if json.Unmarshal(b, &res) != nil { + return out, nil + } + var action string + _ = json.Unmarshal(res["action"], &action) + if action != "accept" { + return out, nil + } + content, ok := res["content"] + if !ok || string(content) == "null" { + return out, nil + } + schema, err := schemaval.Compile(p.RequestedSchema) + if err != nil { + c.warn(Warning{Reason: fmt.Sprintf("elicitation answer relayed unvalidated: requestedSchema does not compile: %v", err)}) + return out, nil + } + if err := schema.ValidateJSON(content); err != nil { + c.warn(Warning{Reason: fmt.Sprintf("elicitation answer rejected: content does not match requestedSchema: %v", err)}) + return nil, &rpcError{Code: codeInvalidParams, + Message: fmt.Sprintf("elicitation answer does not match requestedSchema: %v", err)} + } + return out, nil +} diff --git a/pkgs/mcpx/internal/mcpclient/elicitation_schema_test.go b/pkgs/mcpx/internal/mcpclient/elicitation_schema_test.go new file mode 100644 index 0000000000..793c5531e7 --- /dev/null +++ b/pkgs/mcpx/internal/mcpclient/elicitation_schema_test.go @@ -0,0 +1,176 @@ +package mcpclient_test + +import ( + "context" + "encoding/json" + "strings" + "testing" + "time" + + "github.com/dezren39/mcpx/internal/mcpclient" +) + +// Phase 3 of #1481 (#1484): an accepted form answer is validated against +// the requestedSchema the server itself supplied, at the answer choke +// point, after defaults are applied. +// +// https://modelcontextprotocol.io/specification/2025-11-25/client/elicitation#form-mode-security +func TestElicitationAnswersValidatedAgainstRequestedSchema(t *testing.T) { + schema := map[string]any{ + "type": "object", + "required": []any{"name"}, + "properties": map[string]any{ + "name": map[string]any{"type": "string", "default": "John Doe"}, + "age": map[string]any{"type": "integer"}, + }, + } + legacyAt := func(h mcpclient.ElicitHandler, warns chan mcpclient.Warning) (*wire, *mcpclient.Client) { + w := newWire(t, "2025-11-25", func(m string, p map[string]any) (any, map[string]any) { + if m == "initialize" { + return initResult("2025-11-25", nil), nil + } + return nil, nil + }) + c := dialWire(t, w, mcpclient.ForceLegacy, mcpclient.Options{OnServerRequest: h}) + if warns != nil { + c.Subscribe(mcpclient.Notifications{OnWarning: func(wn mcpclient.Warning) { warns <- wn }}) + } + return w, c + } + elicit := func(w *wire, id string, sch map[string]any) map[string]any { + p := map[string]any{"message": "?", "mode": "form", "requestedSchema": sch} + w.push(map[string]any{"jsonrpc": "2.0", "id": id, "method": "elicitation/create", "params": p}) + var r map[string]any + waitFor(t, "a reply to "+id, func() bool { r = reply(w, id); return r != nil }) + return r + } + handler := func(out map[string]any) mcpclient.ElicitHandler { + return func(context.Context, string, json.RawMessage) (any, error) { return out, nil } + } + + t.Run("2025-11-25/elicitation/wrong-typed-answer-is-32602-not-relayed", func(t *testing.T) { + warns := make(chan mcpclient.Warning, 4) + w, _ := legacyAt(handler(map[string]any{"action": "accept", "content": map[string]any{"age": "forty-one"}}), warns) + r := elicit(w, "v1", schema) + e, _ := r["error"].(map[string]any) + if e == nil || e["code"] != float64(-32602) { + t.Fatalf("a wrong-typed answer must be -32602 to the server, got %v", r) + } + if msg, _ := e["message"].(string); !strings.Contains(msg, "$.age") { + t.Errorf("the error must name the failing path, got %q", msg) + } + if r["result"] != nil { + t.Errorf("the unvalidated answer must not be relayed: %v", r) + } + select { + case wn := <-warns: + if !strings.Contains(wn.Reason, "rejected") { + t.Errorf("unexpected warning: %v", wn) + } + case <-time.After(2 * time.Second): + t.Error("a Warning must be emitted for a rejected answer") + } + }) + + t.Run("2025-11-25/elicitation/omitted-field-with-default-validates", func(t *testing.T) { + // "name" is required by the schema; the answer omits it. Only + // the defaults-before-validation order makes this valid. + w, _ := legacyAt(handler(map[string]any{"action": "accept", "content": map[string]any{"age": 41}}), nil) + r := elicit(w, "v2", schema) + if r["error"] != nil { + t.Fatalf("an answer made valid by its default must be relayed, got %v", r) + } + content, _ := r["result"].(map[string]any)["content"].(map[string]any) + if content["name"] != "John Doe" || content["age"] != float64(41) { + t.Errorf("relayed content = %v", content) + } + }) + + t.Run("2025-11-25/elicitation/conforming-answer-relayed-decline-cancel-untouched", func(t *testing.T) { + w, _ := legacyAt(handler(map[string]any{"action": "accept", "content": map[string]any{"name": "Ada", "age": 36}}), nil) + r := elicit(w, "v3", schema) + content, _ := r["result"].(map[string]any)["content"].(map[string]any) + if r["error"] != nil || content["name"] != "Ada" || content["age"] != float64(36) { + t.Errorf("a conforming answer must be relayed as today, got %v", r) + } + for i, action := range []string{"decline", "cancel"} { + w, _ := legacyAt(handler(map[string]any{"action": action}), nil) + r := elicit(w, "v4"+string(rune('a'+i)), schema) + res, _ := r["result"].(map[string]any) + if r["error"] != nil || res["action"] != action { + t.Errorf("%s must pass through with no validation attempted, got %v", action, r) + } + } + }) + + t.Run("2025-11-25/elicitation/uncompilable-schema-relays-unvalidated-with-warning", func(t *testing.T) { + // The schema is the server's own defect (a dialect schemaval + // refuses): the user's answer must not be wedged on it. + bad := map[string]any{ + "$schema": "https://example.invalid/draft-99/schema", + "type": "object", + "properties": map[string]any{ + "age": map[string]any{"type": "integer"}, + }, + } + warns := make(chan mcpclient.Warning, 4) + w, _ := legacyAt(handler(map[string]any{"action": "accept", "content": map[string]any{"age": "forty-one"}}), warns) + r := elicit(w, "v5", bad) + if r["error"] != nil { + t.Fatalf("an uncompilable requestedSchema must not error the answer, got %v", r) + } + content, _ := r["result"].(map[string]any)["content"].(map[string]any) + if content["age"] != "forty-one" { + t.Errorf("the answer must be relayed unvalidated, got %v", r) + } + select { + case wn := <-warns: + if !strings.Contains(wn.Reason, "unvalidated") { + t.Errorf("unexpected warning: %v", wn) + } + case <-time.After(2 * time.Second): + t.Error("a Warning must be emitted when validation is skipped") + } + }) + + // The modern input-required flow inherits the check because + // resolveInput routes through answer(). + t.Run("2026-07-28/elicitation/input-required-wrong-typed-answer-is-rejected", func(t *testing.T) { + retried := false + s := newScripted(func(method string, params map[string]any) any { + switch method { + case "server/discover": + return modernDiscover(params) + case "tools/call": + if _, ok := params["inputResponses"]; ok { + retried = true + return map[string]any{"resultType": "complete", "content": []any{}} + } + return map[string]any{ + "resultType": "input_required", + "inputRequests": map[string]any{ + "q": map[string]any{"method": "elicitation/create", "params": map[string]any{ + "mode": "form", "message": "?", + "requestedSchema": map[string]any{"type": "object", "properties": map[string]any{ + "age": map[string]any{"type": "integer"}, + }}, + }}, + }, + } + } + return nil + }) + c := dialModern(t, s, mcpclient.Options{ + OnServerRequest: handler(map[string]any{"action": "accept", "content": map[string]any{"age": "forty-one"}}), + }) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + _, err := c.CallTool(ctx, "ask", map[string]any{}) + if err == nil || !strings.Contains(err.Error(), "requestedSchema") { + t.Fatalf("the input-required round must surface the -32602, got %v", err) + } + if retried { + t.Error("the unvalidated answer must never be retried upstream") + } + }) +} diff --git a/pkgs/mcpx/internal/mcpclient/modern.go b/pkgs/mcpx/internal/mcpclient/modern.go index 0976876005..7de3752035 100644 --- a/pkgs/mcpx/internal/mcpclient/modern.go +++ b/pkgs/mcpx/internal/mcpclient/modern.go @@ -357,6 +357,11 @@ func (c *Client) answer(ctx context.Context, method string, params json.RawMessa } if method == "elicitation/create" { out = applyDefaults(out, params) + checked, rerr := c.checkElicitationAnswer(out, params) + if rerr != nil { + return nil, rerr + } + out = checked } return out, nil }