diff --git a/.mcpx.json b/.mcpx.json index 07fdef26ef..e53c26b3ed 100644 --- a/.mcpx.json +++ b/.mcpx.json @@ -11,7 +11,10 @@ }, "fff": { "command": "/home/user/.local/bin/fff-mcp", - "args": [] + "args": [], + "mcpx": { + "readOnlyTools": ["find_files", "grep", "multi_grep"] + } }, "chrome-devtools": { "command": "npx", @@ -69,11 +72,17 @@ }, "fff-nix": { "command": "/home/user/.local/bin/fff-mcp", - "args": ["/home/user/code"] + "args": ["/home/user/code"], + "mcpx": { + "readOnlyTools": ["find_files", "grep", "multi_grep"] + } }, "fff-worktree": { "command": "/home/user/.local/bin/fff-mcp", - "args": ["/home/user/.local/share/opencode/worktree"] + "args": ["/home/user/.local/share/opencode/worktree"], + "mcpx": { + "readOnlyTools": ["find_files", "grep", "multi_grep"] + } }, "codedb": { "command": "node", diff --git a/home/common/mcpx-settings.nix b/home/common/mcpx-settings.nix index fb15bbf463..449a0854b5 100644 --- a/home/common/mcpx-settings.nix +++ b/home/common/mcpx-settings.nix @@ -94,17 +94,32 @@ ]; "command" = "npx"; }; + # readOnlyTools (#6116): fff-mcp publishes no tool annotations, so + # the specification's defaults made every search a possibly + # destructive write and the write-policy gate (default ask) + # refused them all in headless/API sessions, where the + # confirmation question has no answerer. All three fff tools are + # searches; the operator declares them read-only here. "fff" = { "args" = [ ]; "command" = "/home/user/.local/bin/fff-mcp"; + "mcpx" = { + "readOnlyTools" = [ "find_files" "grep" "multi_grep" ]; + }; }; "fff-nix" = { "args" = [ "/home/user/code" ]; "command" = "/home/user/.local/bin/fff-mcp"; + "mcpx" = { + "readOnlyTools" = [ "find_files" "grep" "multi_grep" ]; + }; }; "fff-worktree" = { "args" = [ "/home/user/.local/share/opencode/worktree" ]; "command" = "/home/user/.local/bin/fff-mcp"; + "mcpx" = { + "readOnlyTools" = [ "find_files" "grep" "multi_grep" ]; + }; }; "gopls-mcp-server" = { "args" = [ diff --git a/pkgs/mcpx/docs/configuration.md b/pkgs/mcpx/docs/configuration.md index 6cfa26c6d8..3c9c27bebb 100644 --- a/pkgs/mcpx/docs/configuration.md +++ b/pkgs/mcpx/docs/configuration.md @@ -568,6 +568,42 @@ annotation-trust rules unchanged -- and the floor never weakens a `deny`. Under `ask` the policy has already asked once, so no call is asked about twice. +### Operator-declared read-only tools (`readOnlyTools`) + +The gate reads a tool's classification from its annotations, and many +real servers publish none -- fff, whose tools are all searches, is +one. The specification's defaults then make every call a possibly +destructive write, so under `ask` a headless caller (an API session, +a script -- the normal fleet mode) can never run them at all: the +confirmation question has no answerer, and every call comes back as +the gate's error (#6116). + +The operator can declare specific tools read-only in a server's +`mcpx` block, by exact name: + +```json +{ "mcpServers": { "fff": { "command": "...", "mcpx": { "readOnlyTools": ["find_files", "grep", "multi_grep"] } } } } +``` + +A declared tool is classified read-only whatever its annotations say +(or do not say): the write policy does not gate it in any mode, and +the destructive floor does not ask about it. The declaration is the +operator's own word in their own configuration, which is why it is +trusted -- a server cannot declare its own tools read-only this way, +and a remote server's annotations still clear nothing (#2191). Names +are exact, never patterns, and the declaration covers only the tools +listed: a tool the server adds later, under a new name, is gated by +the defaults until the operator lists it too. Listing a tool that +actually writes is the operator's mistake to make -- and theirs +alone; nothing infers the list. + +When the gate does refuse a call -- an unanswered confirmation, a +denied write -- the refusal is typed, not a generic execution +failure: HTTP 403 with a `code` in the error body +(`confirmation_required` for an unconfirmed call, +`write_policy_denied` for a policy denial), so a caller can tell +"the gate refused this" apart from "the tool ran and failed". + This gate is an operator consent gate, not the autonomy dial (`docs/decisions/0002-autonomy-dial.md`): the dial bounds what mcpx does on its own, the write policy bounds what a caller may do to the diff --git a/pkgs/mcpx/docs/consumer.md b/pkgs/mcpx/docs/consumer.md index 8cf5941bf1..82f86fca92 100644 --- a/pkgs/mcpx/docs/consumer.md +++ b/pkgs/mcpx/docs/consumer.md @@ -110,7 +110,11 @@ idle time, pid — because a choice without that is not a choice. **Destructive confirmation** (`elicit.confirmDestructive`, off). Before a call to a tool that may be destructive: annotated `destructiveHint: true`, or (the specification's default) not annotated `readOnlyHint: true` or `destructiveHint: false`. This is the step a -human-driven client has and a script does not. +human-driven client has and a script does not. A tool the operator +declared in the server's `readOnlyTools` (#6116) is pre-authorized +and never asked about: the declaration is the answer, given in +configuration ahead of time, which is the only answer a headless +caller can give. The two disagree about what a silent deadline means, deliberately. An unanswered disambiguation takes `elicit.disambiguateDefault`, which is `new` — diff --git a/pkgs/mcpx/internal/config/config.go b/pkgs/mcpx/internal/config/config.go index 4b24e814a6..b3c0c33858 100644 --- a/pkgs/mcpx/internal/config/config.go +++ b/pkgs/mcpx/internal/config/config.go @@ -161,6 +161,22 @@ type Extras struct { // server alone: ask, allow or deny (#1979). Empty means inherit the // daemon mode. WritePolicy string `json:"writePolicy,omitempty"` + // ReadOnlyTools names tools the operator declares read-only for + // this server (#6116): exact tool names, no patterns. The + // declaration is the operator's own word in their own config -- + // the one classification input mcpx trusts outright, unlike a + // server's annotations, which a remote server can lie about + // (#2191). Many real servers publish no annotations at all (fff + // is one), and the specification's defaults then read every call + // as a possibly destructive write, so under policy.write=ask a + // headless caller -- which has nobody to answer the gate's + // question -- could never run a search tool. A declared tool is + // classified read-only by the write policy gate and clears the + // elicit.confirmDestructive floor. A tool that is not listed + // keeps the specification's defaults exactly; listing a tool + // here is a claim the operator is responsible for, and it never + // extends to a tool added later under a new name. + ReadOnlyTools []string `json:"readOnlyTools,omitempty"` // ArgumentRules constrains calls by their arguments (#1986): keys // are tool patterns matched exactly as Tools/ExcludeTools match, // values are JSON Schema objects a call's arguments must validate @@ -503,6 +519,11 @@ type Resolved struct { // daemon policy.write setting (#1979). WritePolicy string + // ReadOnlyTools is this view's operator-declared read-only tools + // (#6116): the pool block's names and the server's own, resolved + // at Resolve time. See Extras.ReadOnlyTools. + ReadOnlyTools []string + // ArgumentRules is this server's compiled argument rules (#1986): // the pool block's rules and the server's own, compiled at Resolve // time so a bad pattern or schema is a load-time error. Consumed by @@ -515,6 +536,22 @@ type Resolved struct { Runtime func(name string) (string, bool) } +// DeclaresReadOnly reports whether the operator declared this tool +// read-only for this server in local configuration (#6116). Exact +// names only: a pattern here could silently widen to a tool the +// operator never saw, which is the failure the gate exists to stop. +func (r *Resolved) DeclaresReadOnly(tool string) bool { + if r == nil { + return false + } + for _, name := range r.ReadOnlyTools { + if name == tool { + return true + } + } + return false +} + // SecretNames are the secrets this server requires: its own list and every // tool's requiresSecret, sorted, without repeats. func (r *Resolved) SecretNames() []string { @@ -875,6 +912,7 @@ func (c *Config) Resolve(name string) (*Resolved, error) { Tools: allow, ExcludeTools: deny, WritePolicy: pick(ex.WritePolicy, d.WritePolicy), + ReadOnlyTools: append(append([]string{}, d.ReadOnlyTools...), ex.ReadOnlyTools...), ArgumentRules: rules, Profiles: append(append([]string{}, d.Profiles...), ex.Profiles...), Tags: append(append([]string{}, d.Tags...), ex.Tags...), @@ -1116,6 +1154,10 @@ func mergeExtras(near, far Extras) Extras { near.StartTimeout = pick(near.StartTimeout, far.StartTimeout) near.Namespace = pick(near.Namespace, far.Namespace) near.WritePolicy = pick(near.WritePolicy, far.WritePolicy) + // Declarations accumulate across layered files, as + // ExtraIncludeTools does: a nearer file adds its servers' tools + // to the farther file's list rather than replacing it (#6116). + near.ReadOnlyTools = concat(far.ReadOnlyTools, near.ReadOnlyTools) near.Description = pick(near.Description, far.Description) near.Prelude = pick(near.Prelude, far.Prelude) if len(near.Tools) == 0 { diff --git a/pkgs/mcpx/internal/config/config_test.go b/pkgs/mcpx/internal/config/config_test.go index c466d57ac9..c990652736 100644 --- a/pkgs/mcpx/internal/config/config_test.go +++ b/pkgs/mcpx/internal/config/config_test.go @@ -665,3 +665,55 @@ func TestResolveCarriesTags(t *testing.T) { t.Fatalf("resolved toolTags = %v", r.ToolTags) } } + +// #6116: readOnlyTools in a server's mcpx block resolves onto the +// view, by exact name: a listed tool is declared read-only, an +// unlisted one is not, and a server with no declaration declares +// nothing. +func TestResolveCarriesReadOnlyTools(t *testing.T) { + c := &config.Config{MCPServers: map[string]*config.Server{ + "a": {Name: "a", Command: "x", Mcpx: &config.Extras{ + ReadOnlyTools: []string{"find_files", "grep"}, + }}, + "b": {Name: "b", Command: "x"}, + }} + r, err := c.Resolve("a") + if err != nil { + t.Fatal(err) + } + if !r.DeclaresReadOnly("find_files") || !r.DeclaresReadOnly("grep") { + t.Fatalf("resolved readOnlyTools = %v, want find_files and grep declared", r.ReadOnlyTools) + } + if r.DeclaresReadOnly("wipe") { + t.Fatal("an unlisted tool must not be declared read-only") + } + rb, err := c.Resolve("b") + if err != nil { + t.Fatal(err) + } + if rb.DeclaresReadOnly("find_files") { + t.Fatal("a server with no readOnlyTools declared a tool read-only") + } +} + +// The declaration survives the JSON wire form, which is how an +// operator actually writes it. +func TestReadOnlyToolsFromJSONConfig(t *testing.T) { + dir := t.TempDir() + p := write(t, dir, "c.json", `{ + "mcpServers": { + "fff": { "command": "fff-mcp", "mcpx": { "readOnlyTools": ["find_files"] } } + } + }`) + c, err := config.Load(p) + if err != nil { + t.Fatal(err) + } + r, err := c.Resolve("fff") + if err != nil { + t.Fatal(err) + } + if !r.DeclaresReadOnly("find_files") { + t.Fatalf("readOnlyTools from JSON did not resolve: %v", r.ReadOnlyTools) + } +} diff --git a/pkgs/mcpx/internal/daemon/callerr.go b/pkgs/mcpx/internal/daemon/callerr.go index 26bae45ab6..4aaf1a0d09 100644 --- a/pkgs/mcpx/internal/daemon/callerr.go +++ b/pkgs/mcpx/internal/daemon/callerr.go @@ -104,7 +104,14 @@ func (r *Registry) repairAutonomy(ctx context.Context) string { // finding as data, for a caller that would rather act on a field than parse // a sentence -- which is also what a repair step needs to start from. type CallErrorBody struct { - Error string `json:"error"` + Error string `json:"error"` + // Code is the machine-readable kind of a daemon-side refusal + // (#6116): "confirmation_required" when the confirmation gate + // refused a call nobody confirmed, "write_policy_denied" when + // the write policy refused it. Empty for every other failure -- + // a tool that ran and failed carries Upstream instead -- so a + // caller can branch on the gate without parsing Error's text. + Code string `json:"code,omitempty"` Diagnostics []diagnose.Diagnostic `json:"diagnostics,omitempty"` // Upstream is the JSON-RPC error the server answered with, when the // failure was one. A relay needs the code and data, not the sentence: @@ -227,6 +234,14 @@ func callDiagnostics(err error) []diagnose.Diagnostic { func callErrorBody(err error) CallErrorBody { body := CallErrorBody{Error: err.Error(), Diagnostics: callDiagnostics(err)} + var notConfirmed ErrNotConfirmed + var writeDenied ErrWriteDenied + switch { + case errors.As(err, ¬Confirmed): + body.Code = "confirmation_required" + case errors.As(err, &writeDenied): + body.Code = "write_policy_denied" + } if code, msg, ok := upstreamFault(err); ok { body.Upstream = &UpstreamError{Code: code, Message: msg, Data: upstreamData(err)} } diff --git a/pkgs/mcpx/internal/daemon/consumer.go b/pkgs/mcpx/internal/daemon/consumer.go index c96338461b..9539833844 100644 --- a/pkgs/mcpx/internal/daemon/consumer.go +++ b/pkgs/mcpx/internal/daemon/consumer.go @@ -414,6 +414,13 @@ func describeInstance(in pool.InstanceStatus) string { // ---- confirm a destructive call ---- // ErrNotConfirmed is returned when a destructive call was not approved. +// +// It is a typed refusal, not a tool failure (#6116): failureStatus +// answers it 403 and callErrorBody carries code +// "confirmation_required", so a caller -- above all a headless one, +// which can never answer the question -- can tell "the gate refused +// this call" apart from "the tool ran and failed" (a 502 with an +// upstream error) instead of quoting the gate text as a result. type ErrNotConfirmed struct { Tool string Reason string @@ -434,7 +441,27 @@ func (e ErrNotConfirmed) Error() string { // An unanswered question refuses the call. That is the opposite of the // disambiguation default, and for the opposite reason: there, doing nothing // is safe; here, doing nothing is the destructive thing. -func (r *Registry) confirmDestructive(ctx context.Context, p *pool.Pool, tool string, cc config.CallContext) error { +// declaredReadOnly reports whether the operator declared this tool +// read-only for this server in local configuration (#6116): the +// per-server readOnlyTools list, read from the requested server's +// view for the same reason the write policy override is -- pools are +// shared between aliases, and the declaration belongs to the name +// the caller asked for. The operator's own declaration is trusted +// outright, as the locally declared stdio server's annotations are +// (#2191); a server cannot declare its own tools read-only this way. +func (r *Registry) declaredReadOnly(server, tool string) bool { + view, ok := r.View(server) + return ok && view.DeclaresReadOnly(tool) +} + +func (r *Registry) confirmDestructive(ctx context.Context, p *pool.Pool, server, tool string, cc config.CallContext) error { + if r.declaredReadOnly(server, tool) { + // The operator declared this tool read-only (#6116): a + // pre-authorized call, not a destructive one, so there is + // nothing to confirm -- in any session mode, including a + // headless one with nobody to answer. + return nil + } if !r.consumer.ConfirmDestructive || r.broker == nil { return nil } @@ -566,7 +593,8 @@ func (r *Registry) applyWritePolicy(ctx context.Context, p *pool.Pool, server, t tools = fresh } for _, candidate := range tools { - if candidate.Name == tool && policy.Classify(candidate.Annotations).ReadOnly { + if candidate.Name == tool && (policy.Classify(candidate.Annotations).ReadOnly || + r.declaredReadOnly(server, tool)) { return policy.Decision{}, false } } @@ -655,6 +683,16 @@ func (r *Registry) applyWritePolicy(ctx context.Context, p *pool.Pool, server, t break } } + if r.declaredReadOnly(server, tool) { + // The operator declared this tool read-only in local + // configuration (#6116). That declaration outranks the + // server's annotations -- including their absence, which + // the specification's defaults read as a possibly + // destructive write and which is what gated manifestly + // read-only search tools (fff.find_files) in headless + // sessions, where the gate's question has no answerer. + class = policy.Classification{ReadOnly: true} + } decision := policy.Evaluator{}.Evaluate(policy.Input{ Mode: parsed, diff --git a/pkgs/mcpx/internal/daemon/registry.go b/pkgs/mcpx/internal/daemon/registry.go index 03452b96e1..a800b8a619 100644 --- a/pkgs/mcpx/internal/daemon/registry.go +++ b/pkgs/mcpx/internal/daemon/registry.go @@ -2217,6 +2217,7 @@ func failureStatus(err error) int { var hidden HiddenTool var secret SecretRequired var denied ErrWriteDenied + var notConfirmed ErrNotConfirmed var argDenied ErrArgumentRuleDenied var unknownOutcome ErrUnknownOutcome switch { @@ -2227,6 +2228,13 @@ func failureStatus(err error) int { // The write policy refused the call: a permission answer, not // a bad request and not an upstream failure. return http.StatusForbidden + case errors.As(err, ¬Confirmed): + // The confirmation gate refused the call (#6116): nobody + // confirmed it -- in a headless session, nobody could. That + // is a permission answer like the write policy's, not an + // upstream failure; answering 502 here made the gate + // indistinguishable from a tool that ran and broke. + return http.StatusForbidden case errors.As(err, &secret): // A conflict with the state of the daemon, not a fault in the request // and not a failure of the server: it can succeed once a value exists. @@ -2432,7 +2440,7 @@ func (r *Registry) resolveAndGuard(ctx context.Context, p *pool.Pool, server, to // Not asked by the write policy: the destructive floor still // gets its say. Under ask the policy already asked once, and // asking twice about one call would be noise, not consent. - if err := r.confirmDestructive(ctx, p, effTool, cc); err != nil { + if err := r.confirmDestructive(ctx, p, server, effTool, cc); err != nil { return config.ResolvedScope{}, "", nil, err } } diff --git a/pkgs/mcpx/internal/daemon/write_policy_test.go b/pkgs/mcpx/internal/daemon/write_policy_test.go index 947150e11d..ec5ea785dc 100644 --- a/pkgs/mcpx/internal/daemon/write_policy_test.go +++ b/pkgs/mcpx/internal/daemon/write_policy_test.go @@ -13,6 +13,7 @@ import ( "time" "github.com/developing-today/code/pkgs/mcpx/internal/config" + "github.com/developing-today/code/pkgs/mcpx/internal/elicit" "github.com/developing-today/code/pkgs/mcpx/internal/mcpclient" "github.com/developing-today/code/pkgs/mcpx/internal/pool" ) @@ -337,3 +338,112 @@ func TestWritePolicyDenyPassesAReadOnlyTool(t *testing.T) { t.Errorf("read-only under deny: asked=%v err=%v, want a pass", asked, err) } } + +// writePolicyRegistryWithReadOnly is a registry whose per-name view +// for "demo" declares the named tools read-only (#6116). +func writePolicyRegistryWithReadOnly(mode string, tools ...string) *Registry { + r := writePolicyRegistry(mode) + r.views = map[string]*config.Resolved{ + "demo": {Namespace: "demo", ReadOnlyTools: tools}, + } + return r +} + +// (#6116) A tool the operator declared read-only passes the gate in +// every mode, with no broker and nobody to answer: the declaration is +// the pre-authorization a headless session can actually give. The +// tool here is unannotated, so without the declaration the +// specification's defaults make it a possibly destructive write -- +// the fff.find_files case, gated identically to a delete. +func TestWritePolicyDeclaredReadOnlyPassesWithNoAnswerer(t *testing.T) { + for _, mode := range []string{"ask", "deny"} { + r := writePolicyRegistryWithReadOnly(mode, "wipe") + p := writePolicyPool(t, "", ``) + asked, err := r.applyWritePolicy(context.Background(), p, "demo", "wipe", config.CallContext{}, nil) + if err != nil || asked { + t.Errorf("mode %s: declared read-only: asked=%v err=%v, want a pass", mode, asked, err) + } + } +} + +// The declaration names tools exactly: a tool that is not listed +// keeps the gate, in the same registry, on the same server. +func TestWritePolicyDeclarationDoesNotWidenToUnlistedTools(t *testing.T) { + r := writePolicyRegistryWithReadOnly("ask", "wipe") + p := writePolicyPool(t, "", ``) + _, err := r.applyWritePolicy(context.Background(), p, "demo", "other", config.CallContext{}, nil) + var notConfirmed ErrNotConfirmed + if !errors.As(err, ¬Confirmed) { + t.Fatalf("unlisted tool under ask with no broker = %v, want ErrNotConfirmed", err) + } +} + +// The destructive floor is cleared by the declaration too, and only +// by it: with a broker installed and confirmDestructive on, the +// declared tool is not asked about (it returns at once, where an +// unanswered question would wait out the ask timeout and refuse), +// and the undeclared one is refused when nobody answers. +func TestDeclaredReadOnlyClearsTheDestructiveFloor(t *testing.T) { + broker, err := elicit.Open(filepath.Join(t.TempDir(), "elicit.db")) + if err != nil { + t.Fatal(err) + } + defer broker.Close() + r := &Registry{ + consumer: ConsumerPolicy{ + WritePolicy: "allow", + ConfirmDestructive: true, + AskTimeout: 2 * time.Second, + }, + broker: broker, + views: map[string]*config.Resolved{ + "demo": {Namespace: "demo", ReadOnlyTools: []string{"seek"}}, + }, + } + p := pool.New(&config.Resolved{ + Server: &config.Server{Name: "demo", Command: "demo"}, + Namespace: "demo", + }) + p.SetSchemas( + []mcpclient.Tool{{Name: "wipe"}, {Name: "seek"}}, + nil, nil, "", time.Now(), time.Time{}, false) + + if err := r.confirmDestructive(context.Background(), p, "demo", "seek", config.CallContext{}); err != nil { + t.Fatalf("declared read-only tool was asked about: %v", err) + } + err = r.confirmDestructive(context.Background(), p, "demo", "wipe", config.CallContext{}) + var notConfirmed ErrNotConfirmed + if !errors.As(err, ¬Confirmed) { + t.Fatalf("undeclared tool with nobody to answer = %v, want ErrNotConfirmed", err) + } +} + +// (#6116) A gate refusal is a typed refusal, not a tool failure: HTTP +// 403 and a machine-readable code in the error body, so a headless +// caller can tell "the gate refused this call" apart from "the tool +// ran and failed" (502, no code) without parsing the error text. +func TestGateRefusalsAreTyped(t *testing.T) { + r := writePolicyRegistry("ask") + p := writePolicyPool(t, "", ``) + _, err := r.applyWritePolicy(context.Background(), p, "demo", "wipe", config.CallContext{}, nil) + if got := failureStatus(err); got != 403 { + t.Errorf("failureStatus(ErrNotConfirmed) = %d, want 403", got) + } + if body := callErrorBody(err); body.Code != "confirmation_required" { + t.Errorf("callErrorBody(ErrNotConfirmed).Code = %q, want confirmation_required", body.Code) + } + + r = writePolicyRegistry("deny") + _, err = r.applyWritePolicy(context.Background(), p, "demo", "wipe", config.CallContext{}, nil) + if body := callErrorBody(err); body.Code != "write_policy_denied" { + t.Errorf("callErrorBody(ErrWriteDenied).Code = %q, want write_policy_denied", body.Code) + } + + plain := errors.New("boom") + if got := failureStatus(plain); got != 502 { + t.Errorf("failureStatus(plain error) = %d, want 502", got) + } + if body := callErrorBody(plain); body.Code != "" { + t.Errorf("callErrorBody(plain error).Code = %q, want empty", body.Code) + } +} diff --git a/pkgs/mcpx/internal/e2e/readonly_tools_test.go b/pkgs/mcpx/internal/e2e/readonly_tools_test.go new file mode 100644 index 0000000000..39cfa88ae5 --- /dev/null +++ b/pkgs/mcpx/internal/e2e/readonly_tools_test.go @@ -0,0 +1,55 @@ +package e2e_test + +import ( + "strings" + "testing" +) + +// #6116: an unannotated tool is a possibly destructive write by the +// specification's defaults, so under the default policy (write=ask, +// confirmDestructive floor on) a headless caller -- nobody answers +// the gate's question -- could never run it. Declaring the tool +// readOnlyTools in the server's mcpx block is the operator's +// pre-authorization: the call runs end-to-end and returns its real +// result. +const declaredReadOnlyServer = `{ + "mcpServers": { + "demo": { "command": "FAKE", "mcpx": { "sharing": "shared", "scope": "global", "readOnlyTools": ["structured"] } } + } +}` + +func TestDeclaredReadOnlyToolRunsHeadless(t *testing.T) { + e := newEnv(t, declaredReadOnlyServer) + e.setenv("MCPX_POLICY_WRITE=ask", "MCPX_ELICIT_CONFIRM_DESTRUCTIVE=true", "MCPX_ELICIT_ASK_TIMEOUT=2s") + + // structured publishes no annotations. Declared, it executes + // and its genuine result comes back. + if out := e.run("call", "demo.structured", `{}`); !strings.Contains(out, "42") { + t.Fatalf("a declared read-only call should return its result:\n%s", out) + } + + // wipe is not declared: the gate still fires, and the refusal + // names the gate -- a typed refusal, not a fake result. + out, err := e.try("call", "demo.wipe", `{}`) + if err == nil { + t.Fatalf("an undeclared destructive call should be refused:\n%s", out) + } + if !strings.Contains(out, "not confirmed") { + t.Fatalf("the refusal should say it was not confirmed:\n%s", out) + } +} + +// The control: the same server, the same headless session, no +// declaration -- the unannotated tool is gated and never runs. +func TestUndeclaredUnannotatedToolStaysGatedHeadless(t *testing.T) { + e := newEnv(t, oneServer) + e.setenv("MCPX_POLICY_WRITE=ask", "MCPX_ELICIT_CONFIRM_DESTRUCTIVE=true", "MCPX_ELICIT_ASK_TIMEOUT=2s") + + out, err := e.try("call", "demo.structured", `{}`) + if err == nil { + t.Fatalf("an undeclared unannotated call should be gated:\n%s", out) + } + if !strings.Contains(out, "not confirmed") { + t.Fatalf("the gate refusal should say it was not confirmed:\n%s", out) + } +}