managarten/packages/mana-tool-registry/src/sync-client.ts
Till JS 16c8818338 feat(mcp): M1+M1.5 MCP gateway + tool-registry + shared-crypto
Foundation for autonomous Claude-driven testing. Plan:
docs/plans/mana-mcp-and-personas.md.

New packages
- @mana/tool-registry — schema-first ToolSpec<InputSchema, OutputSchema>
  with zod generics, scope ('user-space' | 'admin') and policyHint
  ('read' | 'write' | 'destructive'). sync-client helpers speak the
  mana-sync push/pull protocol directly so RLS and field-level LWW are
  preserved. MasterKeyClient fetches per-user MKs via the existing
  mana-auth GET /api/v1/me/encryption-vault/key endpoint (JWT-gated,
  ZK-aware, already audited) — no new service-key endpoint built.
  ZeroKnowledgeUserError surfaced as a typed throw.
- @mana/shared-crypto — AES-GCM-256 primitives extracted from the web
  app's $lib/data/crypto/aes.ts so the server-side tool handlers and the
  browser produce byte-for-byte identical wire format
  (enc:1:{b64(iv)}.{b64(ct)}). Web app aes.ts now re-exports from
  shared-crypto — 5 existing importers unchanged, svelte-check stays
  green.

New service
- services/mana-mcp (:3069, Bun/Hono) — MCP Streamable HTTP gateway.
  JWKS auth against mana-auth, per-user session isolation (session-id
  belongs to the user who opened it — cross-user access returns 403),
  admin-scoped tools filtered out before registration. MasterKeyClient
  cached per process with a 5-minute TTL.

11 tools registered
- habits.{create,list,update,archive}, spaces.list (plaintext, M1)
- todo.{create,list,complete}, notes.{create,search}, journal.add
  (encrypted — field lists match
  apps/mana/apps/web/src/lib/data/crypto/registry.ts verbatim)

Infra
- Port 3069 added to docs/PORT_SCHEMA.md
- services/mana-mcp/CLAUDE.md with architecture, auth model,
  tool-authoring recipe, local smoke-test steps
- Root CLAUDE.md services list updated

Type-check green across shared-crypto, mana-tool-registry, mana-mcp.
svelte-check on apps/mana/apps/web stays at 0 errors / 0 warnings.
Boot smoke verified: /health returns registry.loaded=true, unauthed
/mcp → 401, invalid-JWT /mcp → 401 with descriptive message.

Decisions locked in for later milestones (per plan D1–D10):
- Personas will be real mana-auth users (users.kind='persona'), no
  service-key bypass (D1, D2)
- Tool-registry is the SSOT; mana-ai and the legacy
  apps/api/src/mcp/server.ts get merged into it in M4 (three current
  parallel tool catalogs collapse to one)
- Persona-runner (:3070) will be a separate service using the Claude
  Agent SDK + MCP client (D5)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-23 13:18:35 +02:00

123 lines
3.2 KiB
TypeScript

/**
* Thin client for mana-sync's push/pull protocol.
*
* Tool handlers never touch Postgres directly — they speak the same
* sync protocol the Dexie-backed clients use. This keeps RLS,
* field-level LWW, and membership checks intact.
*
* Wire format reference: services/mana-sync/CLAUDE.md
*/
export interface SyncFieldChange {
value: unknown;
updatedAt: string;
}
export interface SyncChange {
table: string;
id: string;
op: 'insert' | 'update' | 'delete';
spaceId?: string;
data?: Record<string, unknown>;
fields?: Record<string, SyncFieldChange>;
deletedAt?: string;
}
export interface SyncPushRequest {
clientId: string;
/**
* ISO timestamp; we pass the tool-call start time so the server's
* response only contains anything that changed *since* we started
* (not our own just-inserted row).
*/
since: string;
changes: SyncChange[];
}
export interface SyncPullResponse<TRow = Record<string, unknown>> {
changes: Array<{ table: string; id: string; op: string; data?: TRow }>;
syncedUntil: string;
}
export interface SyncClientConfig {
baseUrl: string;
jwt: string;
/** Stable identifier for the calling process — lands in sync_changes.client_id. */
clientId: string;
}
/**
* Push a single insert. Returns once mana-sync has persisted the row.
* Handlers that need multi-record writes should call `push()` directly
* with a batched changes array.
*/
export async function pushInsert(
config: SyncClientConfig,
appId: string,
change: Omit<SyncChange, 'op'>
): Promise<void> {
await push(config, appId, [{ ...change, op: 'insert' }]);
}
export async function push(
config: SyncClientConfig,
appId: string,
changes: SyncChange[]
): Promise<void> {
const body: SyncPushRequest = {
clientId: config.clientId,
since: new Date().toISOString(),
changes,
};
const res = await fetch(`${config.baseUrl}/sync/${appId}`, {
method: 'POST',
headers: {
'content-type': 'application/json',
authorization: `Bearer ${config.jwt}`,
},
body: JSON.stringify(body),
});
if (!res.ok) {
const text = await res.text().catch(() => '<unreadable body>');
throw new Error(
`mana-sync push failed: ${res.status} ${res.statusText}${text.slice(0, 500)}`
);
}
}
/**
* Pull all rows of a collection since a given timestamp. The registry
* uses this for `*.list` and `*.recent` tools — we fetch the current
* state rather than maintaining our own cache, matching the local-first
* model where mana-sync is the source of truth.
*
* `since` defaults to epoch zero, which returns everything.
*/
export async function pullAll<TRow = Record<string, unknown>>(
config: SyncClientConfig,
appId: string,
collection: string,
since = '1970-01-01T00:00:00.000Z'
): Promise<SyncPullResponse<TRow>> {
const url = new URL(`${config.baseUrl}/sync/${appId}/pull`);
url.searchParams.set('collection', collection);
url.searchParams.set('since', since);
const res = await fetch(url, {
headers: {
authorization: `Bearer ${config.jwt}`,
'x-client-id': config.clientId,
},
});
if (!res.ok) {
const text = await res.text().catch(() => '<unreadable body>');
throw new Error(
`mana-sync pull failed: ${res.status} ${res.statusText}${text.slice(0, 500)}`
);
}
return (await res.json()) as SyncPullResponse<TRow>;
}