Let the server decide whose memory a caller may read
A holder is now somebody who can prove it. Schema v11 stores a sha256 of a per-holder token beside the directory row, so a copied database cannot replay it and the plaintext is shown exactly once. The operator token mints and revokes those tokens; a holder token reads that holder's own memories and nothing else; naming somebody else without the operator token is refused with both ids in the message. The visibility rule was leaking before this: browse had none at all, and the search rules short-circuited to the whole library whenever no holder was named. The rule is now written out in five copies with one parameter carried four times, and a test compares the copies so they cannot drift — semgrep rejects an interpolated constant, so the copies are not a choice. Eyes carry three states on purpose. NULL means the whole library, which is what a process that owns the store is entitled to; a holder id means that holder; the empty string means nobody, which is what a tokenless HTTP caller is. Making NULL mean nobody instead would have forced every internal caller — CLI, proofs, the compiler, the bench harnesses — to name eyes, contradicting the rule that a local process reads as its own trusted principal. The capability card answers "can this store answer my kind of question" before anything is sent. It is derived from what the store holds, so it cannot promise a modality the store lacks, and it carries no memory text, so it is safe to read before trusting the store. HTTP, CLI and one MCP tool all serve it without a token.
This commit is contained in:
1 parent
209f2d0a1c
commit
83dfc14daa
29 files changed
+1335
-77
No files matched your search
@@ -0,0 +1,44 @@
|
||||
package mcpserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/ZacharyZhang-NY/ely-eye/ely-eye-mcp/internal/domain"
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
)
|
||||
|
||||
type CardInput struct {
|
||||
HolderID string `json:"holder_id,omitempty" jsonschema:"optional exact holder id; ask for that holder's card instead of the whole store's"`
|
||||
}
|
||||
|
||||
type CardOutput struct {
|
||||
Card *domain.CapabilityCard `json:"card,omitempty"`
|
||||
Holder *domain.HolderCard `json:"holder,omitempty"`
|
||||
}
|
||||
|
||||
// registerCardTools adds the read that needs no memory access at all: what this
|
||||
// store can answer. A caller with a new batch of questions reads the card first
|
||||
// instead of discovering the gaps one question at a time.
|
||||
func (s *Server) registerCardTools(server *mcp.Server) {
|
||||
mcp.AddTool(server, &mcp.Tool{
|
||||
Name: "ely_eye_card",
|
||||
Title: "Capability card",
|
||||
Description: "What this store can answer, derived from what it holds: schema version, senses present, which holders exist and what they hold, and what is mounted. No memory text is shown, so it is safe to read before trusting the store.",
|
||||
}, s.cardTool)
|
||||
}
|
||||
|
||||
func (s *Server) cardTool(ctx context.Context, _ *mcp.CallToolRequest, input CardInput) (*mcp.CallToolResult, CardOutput, error) {
|
||||
if strings.TrimSpace(input.HolderID) == "" {
|
||||
card, err := s.api.Card(ctx)
|
||||
if err != nil {
|
||||
return nil, CardOutput{}, err
|
||||
}
|
||||
return nil, CardOutput{Card: card}, nil
|
||||
}
|
||||
holder, err := s.api.HolderCard(ctx, input.HolderID)
|
||||
if err != nil {
|
||||
return nil, CardOutput{}, err
|
||||
}
|
||||
return nil, CardOutput{Holder: holder}, nil
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package mcpserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/ZacharyZhang-NY/ely-eye/ely-eye-mcp/internal/apiclient"
|
||||
)
|
||||
|
||||
const storeCardBody = `{"schema_version":11,"atoms":3,"sources":3,"modalities":{"text":2,"image":1},
|
||||
"has_audio":false,"holders":[{"holder_id":"wife","name":"小美","kind":"person","knows_about":["home"],
|
||||
"contact":{"channel":"signal"},"memories":2,"modalities":{"text":2},"scenarios":["home"],
|
||||
"newest_memory":"2026-05-01T12:00:00+00:00","registered":true,"token_issued":true}],"mounts":{"mounted":[],"scoped":false,"atoms_in_scope":0}}`
|
||||
|
||||
func cardServer(t *testing.T, seen *[]string) *httptest.Server {
|
||||
t.Helper()
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
*seen = append(*seen, r.Method+" "+r.URL.Path)
|
||||
if r.URL.Path == "/api/card/nobody" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
_ = json.NewEncoder(w).Encode(map[string]string{"detail": "no holder 'nobody' has memories or a directory entry"})
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/api/card/wife" {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte(`{"holder_id":"wife","name":"小美","kind":"person","knows_about":["home"],"contact":{},"memories":2,"modalities":{"text":2},"scenarios":["home"],"newest_memory":"2026-05-01T12:00:00+00:00","registered":true,"token_issued":true}`))
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte(storeCardBody))
|
||||
}))
|
||||
}
|
||||
|
||||
func TestCardAsksTheStoreAndKeepsTheAnswerDerived(t *testing.T) {
|
||||
var seen []string
|
||||
server := cardServer(t, &seen)
|
||||
defer server.Close()
|
||||
|
||||
s := &Server{api: apiclient.New(server.URL)}
|
||||
_, out, err := s.cardTool(context.Background(), nil, CardInput{})
|
||||
if err != nil {
|
||||
t.Fatalf("card failed: %v", err)
|
||||
}
|
||||
if seen[0] != "GET /api/card" {
|
||||
t.Errorf("request = %q, want the store card endpoint", seen[0])
|
||||
}
|
||||
if out.Card == nil || out.Holder != nil {
|
||||
t.Fatalf("card = %+v holder = %+v, want the store card only", out.Card, out.Holder)
|
||||
}
|
||||
if out.Card.SchemaVersion != 11 || out.Card.Atoms != 3 || out.Card.HasAudio {
|
||||
t.Errorf("card = %+v, want the server's own schema, counts and senses", out.Card)
|
||||
}
|
||||
if len(out.Card.Holders) != 1 || out.Card.Holders[0].TokenIssued != true {
|
||||
t.Errorf("holders = %+v, want the directory carried through", out.Card.Holders)
|
||||
}
|
||||
if out.Card.Holders[0].Contact["channel"] != "signal" {
|
||||
t.Errorf("contact = %+v, want the contact a referrer needs", out.Card.Holders[0].Contact)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCardWithAHolderAsksForThatHolderAlone(t *testing.T) {
|
||||
var seen []string
|
||||
server := cardServer(t, &seen)
|
||||
defer server.Close()
|
||||
|
||||
s := &Server{api: apiclient.New(server.URL)}
|
||||
_, out, err := s.cardTool(context.Background(), nil, CardInput{HolderID: "wife"})
|
||||
if err != nil {
|
||||
t.Fatalf("holder card failed: %v", err)
|
||||
}
|
||||
if seen[0] != "GET /api/card/wife" {
|
||||
t.Errorf("request = %q, want the one-holder endpoint", seen[0])
|
||||
}
|
||||
if out.Holder == nil || out.Card != nil {
|
||||
t.Fatalf("holder = %+v card = %+v, want the holder card only", out.Holder, out.Card)
|
||||
}
|
||||
if out.Holder.HolderID != "wife" || out.Holder.Memories != 2 {
|
||||
t.Errorf("holder card = %+v, want wife's own counts", out.Holder)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCardTreatsABlankHolderAsTheWholeStore(t *testing.T) {
|
||||
var seen []string
|
||||
server := cardServer(t, &seen)
|
||||
defer server.Close()
|
||||
|
||||
s := &Server{api: apiclient.New(server.URL)}
|
||||
if _, _, err := s.cardTool(context.Background(), nil, CardInput{HolderID: " "}); err != nil {
|
||||
t.Fatalf("blank holder failed: %v", err)
|
||||
}
|
||||
if seen[0] != "GET /api/card" {
|
||||
t.Errorf("request = %q, want the store card rather than a path of spaces", seen[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestCardPassesOnTheServersReasonForAnUnknownHolder(t *testing.T) {
|
||||
var seen []string
|
||||
server := cardServer(t, &seen)
|
||||
defer server.Close()
|
||||
|
||||
s := &Server{api: apiclient.New(server.URL)}
|
||||
_, _, err := s.cardTool(context.Background(), nil, CardInput{HolderID: "nobody"})
|
||||
if err == nil {
|
||||
t.Fatal("an unknown holder must not answer with an empty card")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "nobody") || !strings.Contains(err.Error(), "/api/card/nobody") {
|
||||
t.Errorf("error = %q, want the endpoint and the id the server named", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHolderCardRefusesAnEmptyIdBeforeTheNetwork(t *testing.T) {
|
||||
var seen []string
|
||||
server := cardServer(t, &seen)
|
||||
defer server.Close()
|
||||
|
||||
if _, err := apiclient.New(server.URL).HolderCard(context.Background(), ""); err == nil {
|
||||
t.Fatal("an empty holder id should be refused, not fetched")
|
||||
}
|
||||
if len(seen) != 0 {
|
||||
t.Errorf("requests = %v, want none", seen)
|
||||
}
|
||||
}
|
||||
@@ -33,13 +33,14 @@ func Run(ctx context.Context, cfg config.Config, version string) error {
|
||||
Title: "Ely-Eye Context Cartridge MCP",
|
||||
Version: version,
|
||||
}, &mcp.ServerOptions{
|
||||
Instructions: "Use Ely-Eye tools to retrieve local cartridge context, evidence atoms, proof suites, and compiled evidence packs. Read tools mirror local Ely-Eye state without writing. remember, supersede_memory and forget_memory mutate memory through the Ely-Eye API, which must be running (`ely-eye serve`); the server, not the caller, decides who owns a memory. mount_cartridge and switch_cartridge decide what a search may see: mount a cartridge to ask only that cartridge, and unmount everything to ask the whole library. working_set reloads what one session already recorded, in order, without searching; memory_conflicts reports what the store has not reconciled, and settling one is the operator's act, not a tool's.",
|
||||
Instructions: "Use Ely-Eye tools to retrieve local cartridge context, evidence atoms, proof suites, and compiled evidence packs. Read tools mirror local Ely-Eye state without writing. remember, supersede_memory and forget_memory mutate memory through the Ely-Eye API, which must be running (`ely-eye serve`); the server, not the caller, decides who owns a memory. mount_cartridge and switch_cartridge decide what a search may see: mount a cartridge to ask only that cartridge, and unmount everything to ask the whole library. working_set reloads what one session already recorded, in order, without searching; memory_conflicts reports what the store has not reconciled, and settling one is the operator's act, not a tool's. ely_eye_card says what the store can answer before anything is sent: schema version, which senses its memory covers, and who holds what. A read with no ELY_EYE_AUTH_TOKEN sees only memories nobody owns and memories marked for everyone, so a private answer needs the token.",
|
||||
})
|
||||
server.registerTools(mcpServer)
|
||||
server.registerWriteTools(mcpServer)
|
||||
server.registerDirectoryTools(mcpServer)
|
||||
server.registerMountTools(mcpServer)
|
||||
server.registerTierTools(mcpServer)
|
||||
server.registerCardTools(mcpServer)
|
||||
server.registerResources(mcpServer)
|
||||
if err := mcpServer.Run(ctx, &mcp.StdioTransport{}); err != nil {
|
||||
if errors.Is(err, io.EOF) || strings.HasSuffix(err.Error(), "EOF") {
|
||||
|
||||
Reference in new issue
Block a user