Skip to content

feat(chat): list every tool Chat may use in one registry - #1759

Merged
gkhngyk merged 9 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-tool-registry
Oct 29, 2026
Merged

gkhngyk merged 9 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-tool-registry

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Chat P0, part A2: every tool Chat may use, listed once in a registry (CHAT-20 §9 and §20). Nothing is visible. Nothing runs a tool yet; that's the engine, C3. The decision of whether a person may run a tool is A3.

web/src/lib/chat/registry/

  • defineTool defines each row:
    • its class: read, write or execute;
    • the lowest role that may use it, from the app's own roles;
    • its plan gate, by platform or by plan feature (prompt_volumes, shopping_analytics);
    • its default policy and ceiling;
    • its labels (chatTools.<key>.name for settings, .step for a step's title, CHAT-03);
    • its in-app address, its MCP twin and a test fixture.
  • Rules are checked as each row is defined: a read always runs, an execute always needs a click, and a write that always shows a card is capped at Ask.
  • 33 rows:
    • 30 reads wrap the MCP data functions as they are (web/src/lib/mcp/), including the three Mentions tools today's /dashboard/agent uses (get_mention, get_mention_competitors, get_mentions_perception).

    • 3 writes let the approval flow be built and tried. Each calls the function the app's own buttons use:

      • create_draft → createContentDraft;
      • update_opportunity → updateOpportunityStatus, reviewed or dismissed only, with the reason;
      • dismiss_prompt_suggestion → dismissSuggestion.

      All three are manager, policy "Ask, auto in Full auto".

  • registryCatalog() is what settings pages (B1/B3) read. It's serialisable and carries no tool name: each row is keyed by its label key (CHAT-03 fix(ui): link MIT license text in footer to LICENSE file #22). The internal names stay on the server in CHAT_TOOLS; chatToolByKey() maps a key back.

Tests (tools.test.ts, mcp-parity.test.ts)

  • Every row has its en.json labels and a fixture, and names are unique.
  • Every MCP twin is registered in web/src/lib/mcp/server.ts, with the same read/write split as MCP's isReadTool. Where an MCP tool has readOnlyHint (all of them once feat(mcp): give every tool a title and annotations, and tell clients tool results are data #1732 merges), it matches the row's class.
  • The catalog passes A1's browser-payload rules (clientEventProblems), and none of its values is an internal or MCP tool name.

Notes for later parts

  • Some spec rows have no data function yet (navigation, help, settings facts, GSC/GA summaries, files). They come with the context engine (CHAT-04, wave 1).

  • MCP tools missing from §9 (beyond the three Mentions ones) should be added to the spec.

  • The two draft paths differ:

    • MCP create_content_draft writes as the agent, in review;
    • the app writes as the person.

    CHAT-25's origin and conversation_id come later.

  • Server-side role checks on writes are being added in their own PRs. Until then Chat's own gate (A3) is the role check for Chat.

Related issue

None (Chat P0, Drive › Chat).

Stacked on #1758 (Chat A1). The diff against it holds only this change.

Type of change

  • feat — New feature (behind CHAT_AGENTIC, nothing visible)

How to test

cd web && yarn vitest run src/lib/chat/: 5 files, 82 tests (with A1's).

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • Web: format, lint (0 errors, 7 warnings), typecheck, 560 test files / 5,397 tests and build pass
  • No migration, no UI

🤖 Generated with Claude Code

cihan-gyk and others added 7 commits October 29, 2026 12:51
…eams

The first part of the new Chat (Drive Chat specs, P0 part A1). Nothing
uses it yet, and with the flag off nothing changes: today's
/dashboard/agent keeps its own tables and code.

- CHAT_AGENTIC turns the new Chat on, read on the server at request time
  (web lib/chat/flag.ts, server lib/chat-flag.js), so the same build runs
  with it on or off. CHAT_AGENTIC_ORGS narrows it to listed workspaces.
  Off unless "true", so a self-hosted install sees nothing new.
- 00400 adds chat_conversations, chat_messages, chat_runs, chat_run_steps
  and chat_user_prefs. A chat is private to its author (CHAT-20 §16c):
  only they read it, admins included, and messages, runs and steps follow
  their chat's policies, so sharing with the team can be one more policy
  later. The server writes them; a person writes only their own Chat
  settings (menu, where pages open, focus areas, model, effort), kept on
  the server so they follow them across devices (DECISIONS N3).
- A step's tool and debug record stay with the server: members are
  granted every other column. Step kinds live in chat_step_kinds, so a
  new kind is an inserted row, not a rewritten check.
- web lib/chat/events.ts types every event a run streams (CHAT-00, with
  room for CHAT-25's), names steps by their place in the run, and
  chatEvent() refuses a tool's name, a debug record, an id or a dollar
  amount before an event reaches the browser.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A deleted brand now leaves its chats with their author, as about the
workspace, the way agent_conversations does (00009), rather than
deleting a person's private history with it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nd add app links and the step converter

A uuid in an event's text (a page path, a link the person pasted) no
longer stops the run: chatEvent() shows it as "…". The answer's own text
is still sent as it is, and forbidden keys still throw.

- Forbidden keys are patterns now, in the type (ChatForbiddenKey) and at
  run time alike: any tool key (toolInput, toolResult…) and params; debug,
  tokens and latency; money (usd, price, amount, spend, cost, cents). A
  cost multiplier, a percent and a tooltip still pass.
- Deliverable cards and page.opened get an optional href for their Open
  (CHAT-06, CHAT-25): a path under /dashboard/ only, with no host, query,
  fragment, "//" or ".." part, kept whole by the masking.
- steps.ts holds chat_run_steps' member columns (every one but tool and
  debug_json, checked against the migration's grant) and toClientStep(),
  the one way a stored step reaches the browser: built field by field, so
  a row read with the service role gives up its tool and debug record too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ch new message

- chat_user_prefs.focus holds at most the 8 areas, each once
  (chat_text_array_distinct: a check can't hold a subquery); the model
  column says the server checks it against the catalog before a run.
- A trigger on chat_messages sets its chat's last_message_at (security
  definer, so it reaches the chat whoever writes the message; it never
  moves back).
- The step kinds comment no longer says a kind can be removed: steps
  keep theirs, and there is no index on kind to find out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…y be read

A change applies when the app starts again: a restart when self-hosted,
a new deployment on Vercel. The web flag is for dynamic server code only;
a page rendered at build time would keep the build's value.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Chat P0, A2 (CHAT-20 §9): one place that says, for each tool Chat may
call, what it does, who may call it, on which plan and how a call is
approved. The approval flow (A3), the model's tool list (C3), Settings
› Chat and Roles & access (B1, B3) read these rows instead of keeping
their own lists. Nothing calls the registry yet; it sits behind no flag
because it changes nothing on its own.

- lib/chat/registry/define-tool.ts: defineTool and the row fields:
  internal name, class (read / write / execute), min role (TeamRole),
  plan gate (PlanId, plan Feature, or the plan's platforms), default
  and maximum policy, flags (always_card, cost_card, admin may turn a
  read off), cost class, label key, app route, the MCP tool doing the
  same work, its CHAT-20 row and a fixture. defineTool keeps the hard
  rules as rows are defined: a read runs, an execute is always a click,
  and an always_card write never goes past Ask.
- read-tools.ts: 27 READ rows, each calling the MCP data function the
  MCP tool of the same work calls (imported, not changed), so Chat, the
  MCP Server and today's agent read the same numbers.
- write-tools.ts: create_draft, update_opportunity and
  dismiss_prompt_suggestion, calling the server actions the app's own
  buttons call (createContentDraft, updateOpportunityStatus,
  dismissSuggestion), as the signed-in member.
- tools.ts: CHAT_TOOLS, chatTool(name) and registryCatalog(), the rows
  as plain JSON-safe data for the Settings pages. No generated file:
  every reader is in web/ and can call it.
- messages/en.json: chatTools.{key}.name (Settings) and .step (CHAT-03
  step title) per row. The keys don't spell the tool names, since every
  page gets all of en.json and a tool name never reaches a customer.

Tests: every row has its two labels and a fixture its schema takes;
names and labels are unique; routes are pages of the app; each row runs
its own function with the fixture (reads with the member's context,
writes through the app's actions, with the same arguments the pages
pass); an execute is always a click; the catalog is plain data. Against
the MCP Server: every named MCP tool is registered, reads map to reads
and writes to writes, and readOnlyHint agrees wherever a tool states it
(3 tools on main; all once ansvisor#1732 merges).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… mentions reads

- registryCatalog() is now what a browser may get: each row goes by its
  label key (`key`), without its internal name, its MCP tool name or its
  spec row. A tool's name never reaches a customer (CHAT-03 item 22).
  The names stay on the server in CHAT_TOOLS, where the approval checks,
  the run steps' `tool` column and the MCP parity read them, so no
  second, server-side catalog is needed. chatToolByKey() leads a key a
  page sends back (a policy change, say) to its row.
- get_mention, get_mention_competitors and get_mentions_perception: READ
  rows for the Mentions area's reads today's agent (/dashboard/agent)
  has, so Chat answers what it does. Not in CHAT-20 §9 yet; mapped to
  the MCP tools of the same names, with their en.json labels.

Tests: the catalog has no tool name in any value and passes the event
rule chatEvent() applies (clientEventProblems); every key leads back to
its row.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The MCP context has a role since ansvisor#1762; the registry's tests build one
without it, which fails the web typecheck once merged with main.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good with 0b9ae51: I made one change on top.

What I changed

  • Typecheck on current main. Since #1762, McpAuthContext has a role. The two registry tests build the context without one, so tsc fails once this is merged with main (tools.test.ts 173/190, mcp-parity.test.ts 26).
    • Both tests now pass role: 'admin'. In mcp-parity, the context is a const, so it compiles on this branch's base too.
    • No runtime code changed. The runner that builds ChatToolContext will need to fill role the same way resolveAgentAuth/mcp-auth do.

What I checked

  • Nothing calls the registry yet.
    • No route, action or page imports CHAT_TOOLS or registryCatalog, so with CHAT_AGENTIC off (or on) nothing changes at runtime.
    • The chatTools strings in en.json are only labels.
  • Read rows.
    • Every read row calls the MCP data function its MCP tool calls (lib/mcp/data, account, content, mentions).
    • Each one scopes by auth.organizationId: it checks the brand, audit, action or draft against the organization before reading.
    • None of them writes. I read each function the rows point at, and the test checks that each read row calls an mcp/ function with the context unchanged.
    • Row caps match the MCP tools (rowCap, page size 100 for mentions).
    • get_usage returns percentages, not dollars.
  • Write rows. There are three, and each calls the server action the app's own button uses, through the member's session:
    • create_draft → createContentDraft: assertWritable(), the user from the session, and RLS on content_drafts.
    • update_opportunity → PATCH /api/content/:id/status: the API's 402 middleware, assertOpportunityAccess, and a role check on dismiss.
    • dismiss_prompt_suggestion → POST /api/prompts/suggestions/:id/dismiss: the 402 middleware, assertBrandAccess and assertRole.
    • So the read-only gate and organization scoping hold whoever calls them.
    • The registry's minRole: 'manager' is stricter than the API's dismiss roles (admin/manager/analyst). That's fine, but it only holds once the runner checks minRole (follow-up 1).
  • defineTool's invariants.
    • A read is on.
    • An execute is always click.
    • A write's default never passes its maximum.
    • alwaysCard caps a write at Ask.
    • The tests cover each one.
  • The browser catalog. registryCatalog() sends the label key, not the internal or MCP tool name. That keeps CHAT-03's "no tool names in the browser".
  • Classification. Each row's class matches what its function does. The rows with mcpTool set match the MCP tools' read/write annotations (mcp-parity.test.ts).
  • No PostgREST embeds. No migration.
  • Tests: lib/chat/registry passes (82 with the rest of lib/chat). tsc passes on the branch and on main merged in.

Follow-ups

  1. The runner (A3) must enforce each row's minRole, planGate and policy before run(). The actions it calls only enforce their own roles: createContentDraft lets any member insert a draft through RLS, while the row says manager.
  2. The write rows need a request with a session (as the file says). A scheduled run will need another path, with the same read-only and role checks.
  3. get_shopping_visibility's product_title has no .max(). The MCP tool has the same, so this is just for parity.

@gkhngyk
gkhngyk merged commit 7aebf55 into ansvisor:main Oct 29, 2026
4 of 5 checks passed
@github-actions github-actions Bot removed the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 29, 2026
gkhngyk added a commit to cihan-gyk/ansvisor that referenced this pull request Oct 29, 2026
gkhngyk added a commit to cihan-gyk/ansvisor that referenced this pull request Oct 29, 2026
gkhngyk added a commit to cihan-gyk/ansvisor that referenced this pull request Oct 29, 2026
Keeps main's merged copies of ansvisor#1758, ansvisor#1759, ansvisor#1776, ansvisor#1780, ansvisor#1781 and ansvisor#1786
(request_access's Undo is main's, so 4c7b8cb adds nothing).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants