Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions pkgs/mcpx/internal/cli/serve_ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"net/http"
"os"
"sort"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -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) {
Expand Down
60 changes: 60 additions & 0 deletions pkgs/mcpx/internal/cli/serve_ask_reply_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
5 changes: 5 additions & 0 deletions pkgs/mcpx/internal/daemon/routes_proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion pkgs/mcpx/internal/elicit/elicit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
53 changes: 53 additions & 0 deletions pkgs/mcpx/internal/elicit/relayed_answer_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}