Submatter/apps/mcp-server/src/tools/search-objects.ts
Randall Stillwell c582d621ce multi-tenancy: promote workspaces to top-level table
Block A of the EchoDo plan. Workspaces used to live as `objects(type='workspace')`,
which made it impossible to put a real RLS-friendly tenant boundary on the schema
or to give each workspace a stable URL slug. This commit:

- Adds a top-level `workspaces` table (slug unique, owner FK, plan_tier hook).
- Migrates the 8 anchor tables (objects, workspace_members, object_type_defs,
  property_definitions, templates, forms, markdown_backlog_items,
  cursor_sync_mappings) to FK into `workspaces.id` instead of `objects.id`,
  with a hand-augmented data-copy migration that preserves IDs and slug-collision-
  proofs on backfill.
- Introduces a `workspaceProcedure` tRPC middleware + `resolveWorkspace` helper
  that take a UUID-or-slug `workspace` handle and expose `ctx.workspace`. All
  tenant-scoped routers (objects, types, properties, templates, forms, search,
  ai, relations, favorites) now flow through it.
- Updates the web app to pass `workspace` slugs from the URL (or store) instead
  of the old `workspaceId`, including a workspace-sync layer that rewrites
  /<UUID>/... links to /<slug>/...
- Updates the MCP tools (list_objects, create_object, search_objects) and the
  workspace://{handle}/tree resource to accept either a slug or UUID so existing
  agents keep working.
- Adds a Create Workspace dialog and a Workspace Settings page (rename + slug
  rename with redirect, owner-only archive).

Verified locally against a fresh Postgres: migration applies cleanly, slug
uniqueness holds, tenant data is isolated by workspace_id, slug↔UUID resolution
works in both directions, and ON DELETE CASCADE cleans up child rows in the
correct workspace only.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-06 23:02:55 -05:00

69 lines
2.2 KiB
TypeScript

import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { and, asc, eq, ilike, isNull, or } from "../drizzle.js";
import { z } from "zod";
import { db } from "../db.js";
import { objects } from "../schema.js";
import { resolveWorkspaceHandle } from "../lib/resolve-workspace.js";
import { toolCatch, toolOk } from "./tool-result.js";
function escapeLikePattern(q: string): string {
return q.replace(/\\/g, "\\\\").replace(/%/g, "\\%").replace(/_/g, "\\_");
}
const searchObjectsInputSchema = z.object({
query: z.string().min(1),
workspace: z
.string()
.min(1)
.describe("Workspace UUID or slug. Required so search is tenant-scoped."),
type: z.string().optional(),
status: z.string().optional(),
limit: z.number().int().positive().max(500).optional(),
});
export function registerSearchObjectsTool(mcp: McpServer): void {
mcp.registerTool(
"search_objects",
{
description:
"Search objects by text query (case-insensitive match on title and description) within a workspace. Accepts the workspace slug or UUID.",
inputSchema: searchObjectsInputSchema,
},
async (args) => {
try {
const input = searchObjectsInputSchema.parse(args);
const ws = await resolveWorkspaceHandle(input.workspace);
const limit = input.limit ?? 50;
const pattern = `%${escapeLikePattern(input.query)}%`;
const conditions = [
eq(objects.workspaceId, ws.id),
isNull(objects.archivedAt),
or(ilike(objects.title, pattern), ilike(objects.description, pattern)),
];
if (input.type !== undefined) {
conditions.push(eq(objects.type, input.type));
}
if (input.status !== undefined) {
conditions.push(eq(objects.status, input.status));
}
const rows = await db
.select()
.from(objects)
.where(and(...conditions))
.orderBy(asc(objects.sortOrder), asc(objects.id))
.limit(limit);
return toolOk({
workspace: { id: ws.id, slug: ws.slug, name: ws.name },
objects: rows,
count: rows.length,
});
} catch (e) {
return toolCatch(e);
}
},
);
}