From 186b081a9331b95c5dd9b8596752973a8a6274f5 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Tue, 17 Nov 2026 03:51:48 -0600 Subject: [PATCH] mcpx: relay client elicitation answers upstream; surface answer refusals (#8922) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Family 1 of the #8922 conformance remainder. The daemon ask-answers route carries only the downstream client's own elicitation results (its sole poster is the MCP relay), but Broker.Respond Answers validated them against the requestedSchema (the #1954 gate) and daemonAsker.Reply discarded the route's problems map, so a refused answer left the question pending and the ask loop re-pushed it until AskRounds ran out (tools-call-elicitation failing in three server legs). Now: answers arriving via the ask-answers route are marked Relayed and skip the schema gate (the upstream judges its own answers, as on a direct connection; the gate still applies to cli/api/dashboard answers, and the null-content and sampling-shape refusals still apply to all), and Reply surfaces the problems map as an error so any remaining refusal fails the call fast with its reason. Regression tests at both layers. Verified against the official suite at the pinned ref: server-2025-06-18 56/0 (was 55/1), server-2025-11-25 80/0 (was 69/3 — the unasked sep1034/sep1330 scenarios shared this root cause), server-all SEP-2322 failures cleared; tasks-family failures remain and continue in task 20361. --- pkgs/mcpx/internal/cli/serve_ask.go | 37 +++++++++++- .../mcpx/internal/cli/serve_ask_reply_test.go | 60 +++++++++++++++++++ pkgs/mcpx/internal/daemon/routes_proto.go | 5 ++ pkgs/mcpx/internal/elicit/elicit.go | 21 ++++++- .../internal/elicit/relayed_answer_test.go | 53 ++++++++++++++++ 5 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 pkgs/mcpx/internal/cli/serve_ask_reply_test.go create mode 100644 pkgs/mcpx/internal/elicit/relayed_answer_test.go diff --git a/pkgs/mcpx/internal/cli/serve_ask.go b/pkgs/mcpx/internal/cli/serve_ask.go index 59d5e5a5a4..a341774647 100644 --- a/pkgs/mcpx/internal/cli/serve_ask.go +++ b/pkgs/mcpx/internal/cli/serve_ask.go @@ -9,6 +9,7 @@ import ( "fmt" "net/http" "os" + "sort" "strconv" "strings" "sync" @@ -337,9 +338,41 @@ func (d daemonAsker) Reply(ctx context.Context, callID string, answers map[strin if err != nil { return err } - _, err = c.do(ctx, http.MethodPost, "/v1/ask/"+callID+"/answers", + body, err := c.do(ctx, http.MethodPost, "/v1/ask/"+callID+"/answers", map[string]any{"answers": answers}) - return err + if err != nil { + return err + } + // The route answers 200 even when it refused some of the answers, + // naming each refusal in problems. Dropping that body was the + // #8922 loop: the broker kept the question pending, the caller + // believed it answered, re-polled, re-pushed the same question + // and got the same answer until AskRounds ran out. A refusal is + // terminal for the answer that caused it -- the client already + // answered and will answer the same way again -- so surface it + // and let the caller fail the call with the reason. + var out struct { + Problems map[string]string `json:"problems"` + } + if len(body) > 0 { + if err := json.Unmarshal(body, &out); err != nil { + return fmt.Errorf("reading the daemon's answer receipt: %w", err) + } + } + if len(out.Problems) > 0 { + ids := make([]string, 0, len(out.Problems)) + for id := range out.Problems { + ids = append(ids, id) + } + sort.Strings(ids) + parts := make([]string, 0, len(ids)) + for _, id := range ids { + parts = append(parts, id+": "+out.Problems[id]) + } + return fmt.Errorf("the daemon refused %d answer(s): %s", + len(ids), strings.Join(parts, "; ")) + } + return nil } func (d daemonAsker) Abandon(callID string) { diff --git a/pkgs/mcpx/internal/cli/serve_ask_reply_test.go b/pkgs/mcpx/internal/cli/serve_ask_reply_test.go new file mode 100644 index 0000000000..84e64bb3d3 --- /dev/null +++ b/pkgs/mcpx/internal/cli/serve_ask_reply_test.go @@ -0,0 +1,60 @@ +package cli + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/developing-today/code/pkgs/mcpx/internal/daemon" +) + +// #8922: the daemon's ask-answers route reports refused answers in the +// problems map of an otherwise-200 response. daemonAsker.Reply used to +// discard that body, so the relay loop believed the question answered, +// re-polled, re-pushed the same question and burned every AskRound +// before failing the call. Reply must surface the refusal as an error +// naming it, so the loop fails the call fast with the reason. +func TestDaemonAskerReplySurfacesRefusals(t *testing.T) { + daemonSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/v1/ask/call-1/answers" { + http.Error(w, `{"error":"unexpected path"}`, http.StatusNotFound) + return + } + _, _ = w.Write([]byte(`{"accepted":0,"problems":{"q1":` + + `"accepted content does not match the requested schema: repo is required"}}`)) + })) + defer daemonSrv.Close() + + app := &App{client: NewClientAt(daemon.Paths{}, "", daemonSrv.URL), connected: true} + asker := daemonAsker{app: app} + err := asker.Reply(context.Background(), "call-1", map[string]json.RawMessage{ + "q1": json.RawMessage(`{"action":"accept","content":{"count":1}}`), + }) + if err == nil { + t.Fatal("Reply dropped a refused answer: got nil error") + } + if !strings.Contains(err.Error(), "refused") || + !strings.Contains(err.Error(), "q1") || + !strings.Contains(err.Error(), "requested schema") { + t.Fatalf("Reply error does not name the refusal: %v", err) + } +} + +// A receipt with no problems is not an error. +func TestDaemonAskerReplyAcceptsCleanReceipt(t *testing.T) { + daemonSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write([]byte(`{"accepted":1}`)) + })) + defer daemonSrv.Close() + + app := &App{client: NewClientAt(daemon.Paths{}, "", daemonSrv.URL), connected: true} + asker := daemonAsker{app: app} + if err := asker.Reply(context.Background(), "call-1", map[string]json.RawMessage{ + "q1": json.RawMessage(`{"action":"accept","content":{"repo":"me/thing"}}`), + }); err != nil { + t.Fatalf("Reply failed on a clean receipt: %v", err) + } +} diff --git a/pkgs/mcpx/internal/daemon/routes_proto.go b/pkgs/mcpx/internal/daemon/routes_proto.go index 25dd2261a1..cf4fa37725 100644 --- a/pkgs/mcpx/internal/daemon/routes_proto.go +++ b/pkgs/mcpx/internal/daemon/routes_proto.go @@ -810,6 +810,11 @@ func (s *Server) handleAskAnswers(w http.ResponseWriter, r *http.Request) { continue } ans.By = by + // Every answer this route carries is the downstream client's + // own result, relayed to the upstream that asked: the only + // poster is the MCP relay (daemonAsker.Reply). Mark it so the + // broker judges it as a relayed answer (#8922). + ans.Relayed = true if err := s.reg.broker.Respond(ans); err != nil { problems[id] = err.Error() continue diff --git a/pkgs/mcpx/internal/elicit/elicit.go b/pkgs/mcpx/internal/elicit/elicit.go index 6e1a6e702e..86da1d583f 100644 --- a/pkgs/mcpx/internal/elicit/elicit.go +++ b/pkgs/mcpx/internal/elicit/elicit.go @@ -161,6 +161,15 @@ type Answer struct { By string `json:"by,omitempty"` At time.Time `json:"at"` PID int `json:"pid,omitempty"` + // Relayed marks an answer the downstream MCP client itself gave to + // the upstream server's question, carried through mcpx unchanged + // (the daemon ask-answers route is the only source). A relayed + // answer is judged by the upstream, the way a direct connection + // behaves: mcpx cannot correct it, so the requestedSchema gate in + // Respond -- which exists for answers entered on mcpx's own + // surfaces, where the answerer can be shown the failing paths and + // try again -- does not apply to it (#8922). + Relayed bool `json:"relayed,omitempty"` } // Broker stores questions and hands back answers. @@ -569,7 +578,17 @@ func (b *Broker) Respond(a Answer) error { // that does not compile is the asker's defect, not the answerer's: // the answer is recorded unvalidated rather than wedging on a // schema the answerer never wrote (the mcpclient rule). - if a.Action == Accept && r.Mode == Form && len(r.Schema) > 0 { + // + // A relayed answer (Answer.Relayed) skips this gate: it is the + // downstream client's own answer to the upstream's question, and + // the upstream is its judge, as on a direct connection. Refusing + // it here could never produce a corrected answer -- the client + // already answered -- and left the question pending, so the + // relay loop re-pushed the same question until its rounds ran + // out and the call failed (#8922). The refusals above (null + // content, sampling shape) still apply to relayed answers: they + // protect the plumbing, not the content. + if a.Action == Accept && r.Mode == Form && len(r.Schema) > 0 && !a.Relayed { if schema, err := schemaval.Compile(r.Schema); err == nil { if err := schema.ValidateJSON(withSchemaDefaults(r.Schema, a.Content)); err != nil { return fmt.Errorf("accepted content does not match the requested schema: %w", err) diff --git a/pkgs/mcpx/internal/elicit/relayed_answer_test.go b/pkgs/mcpx/internal/elicit/relayed_answer_test.go new file mode 100644 index 0000000000..d85858ad7a --- /dev/null +++ b/pkgs/mcpx/internal/elicit/relayed_answer_test.go @@ -0,0 +1,53 @@ +package elicit_test + +import ( + "encoding/json" + "strings" + "testing" + + "github.com/developing-today/code/pkgs/mcpx/internal/elicit" +) + +// #8922: an answer relayed from the downstream MCP client (marked +// Relayed by the daemon ask-answers route, its only source) is judged +// by the upstream server, the way a direct connection behaves. The +// #1954 requestedSchema gate cannot apply to it: mcpx cannot correct +// a relayed answer, and refusing it left the question pending so the +// relay loop re-pushed the same question until its rounds ran out. +// The gate still applies to answers entered on mcpx's own surfaces. +func TestRespondRelayedAnswerSkipsSchemaGate(t *testing.T) { + b := open(t) + r := ask(t, b, elicit.Request{Mode: elicit.Form, Schema: json.RawMessage(formSchema)}) + + content := json.RawMessage(`{"count": 1}`) // missing the required repo field + + // The same content from mcpx's own surface is still refused. + if err := b.Respond(elicit.Answer{ID: r.ID, Action: elicit.Accept, + Content: content, By: "cli"}); err == nil { + t.Fatal("a cli answer that misses a required field was accepted") + } + + // Relayed, it is recorded as given; the upstream judges it. + if err := b.Respond(elicit.Answer{ID: r.ID, Action: elicit.Accept, + Content: content, By: "mcp-client", Relayed: true}); err != nil { + t.Fatalf("relayed answer refused: %v", err) + } + a, ok, err := b.Lookup(r.ID) + if err != nil || !ok || a.Action != elicit.Accept { + t.Fatalf("relayed answer not recorded: %+v ok=%v err=%v", a, ok, err) + } + if !strings.EqualFold(string(a.Content), string(content)) { + t.Fatalf("relayed content changed in transit: %s", a.Content) + } +} + +// The plumbing refusals are not the schema gate: a relayed accept with +// no content at all is still refused, relayed or not. +func TestRespondRelayedAnswerStillNeedsContent(t *testing.T) { + b := open(t) + r := ask(t, b, elicit.Request{Mode: elicit.Form, Schema: json.RawMessage(formSchema)}) + if err := b.Respond(elicit.Answer{ID: r.ID, Action: elicit.Accept, + By: "mcp-client", Relayed: true}); err == nil { + t.Fatal("a relayed accept with no content was accepted") + } +}