UNPKG

@kolbo/mcp

Version:

Kolbo AI MCP Server - Generate images, videos, music, speech, and sound effects from Claude Code

854 lines • 57.1 kB
/* ⛔ BACKWARD COMPATIBILITY: Tool names and arg names below are a PUBLIC
 * CONTRACT. Never rename, remove, or break an existing tool/arg — old cached
 * `npx @kolbo/mcp` installs in the wild will break silently. Add new tools or
 * new OPTIONAL args only. Full rules: ../index.js top-of-file and CLAUDE.md. */

const { z } = require('zod');
const { buildProjectUrl, compactList } = require('./_shared');
const { listResult } = require('../apps');

function blurb(text) {
  const s = String(text || '').replace(/\s+/g, ' ').trim();
  if (s.length <= 96) return s;
  return s.slice(0, 96).replace(/\s+\S*$/, '') + '…';
}

// A project with no manual cover shows a mosaic of its latest images in the web app,
// but the project list API sends nothing for it. Fetch up to 4 recent image urls for
// the first few visible projects, cached, failures ignored: a missing mosaic falls back
// to the letter monogram exactly as before.
const _mosaicCache = new Map();
const MOSAIC_TTL = 5 * 60 * 1000;
const MOSAIC_MAX_PROJECTS = 12;
async function projectMosaic(client, projectId) {
  const hit = _mosaicCache.get(projectId);
  if (hit && Date.now() - hit.t < MOSAIC_TTL) return hit.urls;
  try {
    const res = await client.get(`/v1/media?project_id=${encodeURIComponent(projectId)}&type=image&page_size=4`);
    const urls = (res.media || res.items || []).map((m) => m.url || m.thumbnail_url).filter(Boolean).slice(0, 4);
    _mosaicCache.set(projectId, { t: Date.now(), urls });
    return urls;
  } catch {
    return [];
  }
}

function registerProjectTools(server, client) {
  // ─── list_projects ─────────────────────────────────────────
  server.tool(
    'list_projects',
    "Read-only. Lists the user's projects (owned, or shared with edit, full or owner permission), paginated (default 50 per page). Returns id, name, role, is_default and is_archived. Pass search to find a project by name. Generation, chat, upload and move tools take the returned id as project_id; they do not accept names, and project_id applies per call. is_default marks the \"API Generations\" project that receives work sent without a project_id. Archived projects are returned only with include_archived: true.",
    {
      search: z.string().optional().describe('Case-insensitive substring match on the project name. Use this whenever the user named a project — it turns a full listing into a one-item answer.'),
      page: z.number().optional().describe('Page number, 1-indexed. Default: 1'),
      limit: z.number().optional().describe('Results per page, max 200. Default: 50'),
      include_archived: z.boolean().optional().describe('Also return archived projects. Default false — archived projects are hidden here exactly as they are in the web app.')
    },
    async ({ search, page, limit, include_archived }) => {
      const params = new URLSearchParams();
      if (search) params.set('search', search);
      if (page) params.set('page', String(page));
      if (limit) params.set('limit', String(limit));
      if (include_archived) params.set('include_archived', 'true');
      const qs = params.toString();
      const result = await client.get(`/v1/projects${qs ? '?' + qs : ''}`);
      const projects = (result.projects || []).map(p => ({
        id: p.id,
        name: p.name,
        description: p.description || null,
        role: p.role,
        is_default: !!p.is_default,
        is_archived: !!p.is_archived,
        thumbnail_url: p.thumbnail_url || null,
        open_url: buildProjectUrl(p.id, { is_default: !!p.is_default })
      }));
      const mosaics = {};
      await Promise.all(projects
        .filter((p) => !p.thumbnail_url)
        .slice(0, MOSAIC_MAX_PROJECTS)
        .map(async (p) => { mosaics[p.id] = await projectMosaic(client, p.id); }));
      const text = JSON.stringify({
        projects,
        count: projects.length,
        pagination: result.pagination || null,
        _hint: 'Pass the chosen `id` as `project_id` on any generate_* tool to drop the generation into that project. Omit project_id to use the project flagged is_default:true. `open_url` opens that project\'s media in the web app (share it with the user). If `pagination.has_more` is true there are more projects — narrow with `search` rather than paging through everything.'
      }, null, 2);

      return listResult(text, {
        widget: 'list',
        title: 'Your Projects',
        items: projects.map(p => ({
          id: p.id,
          title: p.name,
          subtitle: (p.description || p.role) + (p.is_default ? ' · default' : '') + (p.is_archived ? ' · archived' : ''),
          thumbnail: p.thumbnail_url || (mosaics[p.id] && mosaics[p.id].length === 1 ? mosaics[p.id][0] : null),
          ...(mosaics[p.id] && mosaics[p.id].length > 1 ? { thumbnails: mosaics[p.id] } : {}),
          open_url: p.open_url,
          use_hint: 'Use my "{TITLE}" project (project_id: {ID}) for what I do next.'
        })),
        total: projects.length,
        page_tool: 'list_projects',
        next_args: projects.length >= (limit || 50) ? { search, include_archived, page: (page || 1) + 1, limit } : undefined
      });
    }
  );

  // ─── move_session ──────────────────────────────────────────
  server.tool(
    'move_session',
    'Move ONE session — and ALL of its generations and media library items — to another project. Works for image, image_edit, video, video_from_image, elements, first_last_frame, lipsync, video_from_video, global_video_edit, music, speech, sound and chat sessions (the `session_id` returned by generate_* tools, or a chat conversation). Creative Director, transcription, global_image_edit and shorts sessions cannot be moved through the API and return SESSION_TYPE_NOT_MOVABLE. Use this when work landed in the wrong project (e.g. the default "API Generations" bucket) and the user wants it in a named project — moving is always better than regenerating. For SEVERAL sessions use `bulk_move_sessions` instead: one call, up to 100 sessions, and it reports per-session failures. Caller needs edit/full/owner permission on BOTH the source and target projects (a shared-project member can move a teammate\'s session). Resolve the target project id with `list_projects` first.',
    {
      session_id: z.string().describe('The session ObjectId to move (from a generation submit response, chat_list_conversations, or an "Open in Kolbo" link).'),
      project_id: z.string().describe('Target project ObjectId. Call `list_projects` to resolve a project name to its id.'),
      type: z.string().optional().describe('Optional session type hint to speed up the lookup: image, video, video_from_image, music, speech, sound, image_edit, creative_director, chat, elements, first_last_frame, lipsync, video_from_video, transcription, global_image_edit, global_video_edit, shorts. Omit if unsure — the server probes all types.')
    },
    async ({ session_id, project_id, type }) => {
      const body = { project_id };
      if (type) body.type = type;
      const result = await client.patch(`/v1/sessions/${encodeURIComponent(session_id)}/project`, body);
      return {
        content: [{
          type: 'text',
          text: JSON.stringify({
            session: result.session,
            _hint: 'The session and its media now live in the new project. Future generations still need `project_id` passed explicitly on each call.'
          }, null, 2)
        }]
      };
    }
  );
  // ─── bulk_move_sessions ────────────────────────────────────
  server.tool(
    'bulk_move_sessions',
    'Move MANY sessions into one project in a single call — the tool to use when reorganizing a user\'s library ("file all my Acme work into the Acme project", "clean up the API Generations bucket"). Each session carries ALL of its generations and media with it. Prefer this over looping `move_session`: one call handles up to 100 sessions, while `move_session` is rate limited per call. Sessions of mixed types (chat + image + video) can go in the SAME call. Each session moves independently, so one that cannot move — a generation still running, a Creative Director session, or one you lack edit access to — does NOT block the rest; check `failed[]` in the result and report those to the user. Resolve session ids with `list_sessions` and the target project id with `list_projects` first.',
    {
      session_ids: z.array(z.string()).describe('Session ObjectIds to move (from `list_sessions`, a generation submit response, or an "Open in Kolbo" link). Up to 100 per call; types may be mixed.'),
      project_id: z.string().describe('Target project ObjectId. Call `list_projects` to resolve a project name to its id.'),
      type: z.string().optional().describe('Optional session type hint that speeds up the lookup when EVERY id in the batch is the same type: image, video, video_from_image, music, speech, sound, image_edit, chat, elements, first_last_frame, lipsync. Omit for mixed batches — the server probes all types.')
    },
    async ({ session_ids, project_id, type }) => {
      const body = { session_ids, project_id };
      if (type) body.type = type;
      const result = await client.post('/v1/sessions/move', body);
      return {
        content: [{
          type: 'text',
          text: JSON.stringify({
            project_id: result.project_id,
            moved_sessions_count: result.moved_sessions_count,
            moved_generations_count: result.moved_generations_count,
            moved_media_count: result.moved_media_count,
            skipped: result.skipped,
            failed: result.failed,
            operation_ids: result.operation_ids,
            _hint: (result.failed && result.failed.length)
              ? 'Some sessions did not move — tell the user which ones and why (see failed[]). The rest are already in the new project.'
              : 'Every session, its generations and its media now live in the new project. Pass any operation_id to `undo_session_organization` within 15 minutes to reverse one.'
          }, null, 2)
        }]
      };
    }
  );

  // ─── list_session_generations ──────────────────────────────
  server.tool(
    'list_session_generations',
    "Read-only. Lists one session's generations. Each entry is one generation with its prompt and all of its outputs. The returned ids are the inputs of move_generations_to_session and split_session; a generation always moves with its outputs. Supported for image, image-to-video, lipsync and video-to-video sessions; other types return SESSION_TYPE_NOT_MOVABLE (move those whole with move_session).",
    {
      session_id: z.string().describe('The session ObjectId to inspect.'),
      type: z.string().optional().describe('Optional session type hint to speed up the lookup. Omit if unsure.')
    },
    async ({ session_id, type }) => {
      const path = `/v1/sessions/${encodeURIComponent(session_id)}/generations`;
      const result = await client.get(path + (type ? `?type=${encodeURIComponent(type)}` : ''));
      const generations = result.generations || [];
      const text = JSON.stringify({
        session: result.session,
        generations,
        _hint: 'This is an inventory, not a live generation. Pass the `id` values to `move_generations_to_session` (into an existing session) or `split_session` (into a new one). `in_flight: true` means it is still running and cannot be moved yet.'
      }, null, 2);
      return listResult(text, {
        widget: 'list',
        title: (result.session && result.session.name) || 'Session generations',
        items: generations.map((g) => ({
          id: g.id,
          title: (g.prompt && String(g.prompt).slice(0, 80)) || g.id,
          subtitle: [g.status, g.output_count ? g.output_count + ' outputs' : null].filter(Boolean).join(' · '),
          badge: g.in_flight ? 'running' : g.status
        })),
        total: generations.length
      });
    }
  );

  // ─── move_generations_to_session ───────────────────────────
  server.tool(
    'move_generations_to_session',
    'Move SELECTED generations out of one session and into another EXISTING session — the way to merge scattered work ("put these shots into my Hero Sequence session", "these three belong with the earlier batch"). Only the chosen generations and THEIR OWN output media move; shared uploads and reference images stay with the source session, so nothing another generation still depends on is dragged away. The destination must be a session of the SAME kind, and may live in a different project as long as you can edit both. Get the generation ids from `list_session_generations` and the destination id from `list_sessions`. Running generations cannot be moved — wait for them to finish.',
    {
      session_id: z.string().describe('Source session ObjectId — the session the generations are in now.'),
      generation_ids: z.array(z.string()).describe('Generation ids to move, from `list_session_generations`. Whole entries only.'),
      target_session_id: z.string().describe('Destination session ObjectId. Must be the same session kind as the source.'),
      type: z.string().optional().describe('Optional session type hint to speed up the lookup. Omit if unsure.')
    },
    async ({ session_id, generation_ids, target_session_id, type }) => {
      const body = { generation_ids, target_session_id };
      if (type) body.type = type;
      const result = await client.post(
        `/v1/sessions/${encodeURIComponent(session_id)}/generations/move`, body
      );
      return {
        content: [{
          type: 'text',
          text: JSON.stringify({
            moved_generations_count: result.moved_generations_count,
            moved_media_count: result.moved_media_count,
            target_session_id: result.target_session_id,
            project_id: result.project_id,
            operation_id: result.operation_id,
            _hint: 'Reversible for 15 minutes — pass operation_id to `undo_session_organization`.'
          }, null, 2)
        }]
      };
    }
  );

  // ─── split_session ─────────────────────────────────────────
  server.tool(
    'split_session',
    'Carve selected generations out of a session into a BRAND NEW named session, atomically. Use when one session has grown into several distinct pieces of work ("separate the product shots from the lifestyle ones", "give the approved takes their own session"). Creates the new session and moves the chosen generations plus their output media into it in one transaction — nothing half-lands. The new session goes in the same project unless you pass `project_id`. Get the generation ids from `list_session_generations` first.',
    {
      session_id: z.string().describe('Source session ObjectId to split.'),
      generation_ids: z.array(z.string()).describe('Generation ids to move into the new session, from `list_session_generations`.'),
      name: z.string().describe('Name for the new session — make it descriptive, the user sees it in the sidebar.'),
      project_id: z.string().optional().describe('Put the new session in a DIFFERENT project. Omit to keep it in the source session\'s project. You need edit access on both.'),
      type: z.string().optional().describe('Optional session type hint to speed up the lookup. Omit if unsure.')
    },
    async ({ session_id, generation_ids, name, project_id, type }) => {
      const body = { generation_ids, name };
      if (project_id) body.project_id = project_id;
      if (type) body.type = type;
      const result = await client.post(`/v1/sessions/${encodeURIComponent(session_id)}/split`, body);
      return {
        content: [{
          type: 'text',
          text: JSON.stringify({
            session: result.session,
            moved_generations_count: result.moved_generations_count,
            moved_media_count: result.moved_media_count,
            operation_id: result.operation_id,
            _hint: 'Reversible for 15 minutes — `undo_session_organization` removes the new session and returns its generations.'
          }, null, 2)
        }]
      };
    }
  );

  // ─── undo_session_organization ─────────────────────────────
  server.tool(
    'undo_session_organization',
    'Reverse a session move, generation move, or split within 15 minutes of making it. Use immediately when the user says the reorganization was wrong ("no, put that back", "undo that move"). Takes the `operation_id` returned by `move_session`, `bulk_move_sessions`, `move_generations_to_session` or `split_session` — a batch move returns one id PER session, so call this once per id you want to reverse. Refuses safely if the work has moved again since, rather than yanking records out of wherever they now live.',
    {
      operation_id: z.string().describe('The operation_id from the move/split result you want to reverse.')
    },
    async ({ operation_id }) => {
      const result = await client.post(
        `/v1/sessions/organize/undo/${encodeURIComponent(operation_id)}`, {}
      );
      return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] };
    }
  );
  // ─── create_project ────────────────────────────────────────
  server.tool(
    'create_project',
    'Create a new Kolbo project. Use when the user starts a new body of work ("new project for film X", "set up a workspace for the campaign"). After creating, pass the returned id as `project_id` on EVERY subsequent generation/upload/doc call for that work. Plan limits apply (server rejects when the plan\'s project cap is reached).',
    {
      name: z.string().describe('Project name.'),
      description: z.string().optional().describe('Optional description (max 10k chars, markdown OK). Great place for the brief/logline — it also feeds the project\'s AI profile.')
    },
    async ({ name, description }) => {
      const body = { name };
      if (description) body.description = description;
      const result = await client.post('/v1/projects', body);
      const open_url = buildProjectUrl(result.project && result.project.id, { is_default: !!(result.project && result.project.is_default) });
      return { content: [{ type: 'text', text: JSON.stringify({ project: result.project, open_url, _hint: 'Pass this id as project_id on every subsequent call for this work. `open_url` opens the project in the web app — share it with the user.' }, null, 2) }] };
    }
  );

  // ─── duplicate_project ─────────────────────────────────────
  server.tool(
    'duplicate_project',
    'Duplicate one of YOUR OWN projects into a new project you own. Use for a variant of an existing body of work ("make a Hebrew version of this", "branch this campaign") and BEFORE publishing a project publicly — duplicate it, strip what should stay private, and share the copy. Copies the cast (Visual DNAs, moodboards, palette), uploaded media, every tool session with its generations, Flow canvases, and — unlike a public fork — your AI Docs. Third-party stock and failed runs are kept too, since the copy never leaves your account. NOT copied: project context files, the derived AI profile, and chat sessions. In-flight generations are skipped (their provider job belongs to the original). Plan project limits apply.',
    {
      project_id: z.string().describe('ObjectId of a project you own, from list_projects.')
    },
    async ({ project_id }) => {
      const result = await client.post(`/v1/projects/${encodeURIComponent(project_id)}/duplicate`, {});
      const data = result.data || result;
      const open_url = buildProjectUrl(data.projectId);
      return { content: [{ type: 'text', text: JSON.stringify({ ...data, open_url, _hint: 'Use this NEW projectId as project_id for work on the copy. `stats.notCopied` lists what did not travel — say so rather than letting the user discover it.' }, null, 2) }] };
    }
  );

  // ─── get_project ───────────────────────────────────────────
  server.tool(
    'get_project',
    'Fetch one project by id including its FULL description (list_projects clips descriptions to ~400 chars). Call this before `update_project` when you need to edit the brief, logline, or notes — read, apply the user\'s edits, send the complete description back.',
    {
      project_id: z.string().describe('Project ObjectId from list_projects.')
    },
    async ({ project_id }) => {
      const result = await client.get(`/v1/projects/${encodeURIComponent(project_id)}`);
      return { content: [{ type: 'text', text: JSON.stringify(result.project || result, null, 2) }] };
    }
  );

  // ─── update_project ────────────────────────────────────────
  server.tool(
    'update_project',
    'Rename a project and/or replace its description. NEVER delete a project or create a new one just to change the brief — this edits in place. Description REPLACES the whole text: call `get_project` first, apply the user\'s edits, send the complete result. Changing the description also refreshes the project\'s AI profile in the background.',
    {
      project_id: z.string().describe('Project ObjectId (from list_projects / get_project).'),
      name: z.string().optional().describe('New name.'),
      description: z.string().optional().describe('New description (replaces the old one; max 10k chars, markdown OK).')
    },
    async ({ project_id, name, description }) => {
      const body = {};
      if (name !== undefined) body.name = name;
      if (description !== undefined) body.description = description;
      const result = await client.put(`/v1/projects/${encodeURIComponent(project_id)}`, body);
      return { content: [{ type: 'text', text: JSON.stringify(result.project, null, 2) }] };
    }
  );

  // ─── archive_project / unarchive_project ───────────────────
  server.tool(
    'archive_project',
    'Archive a project — hides it from the default project list without deleting anything. Fully reversible with `unarchive_project`. (Permanent project DELETION is intentionally not available via the API — it cascades to all content and stays an in-app action.)',
    { project_id: z.string().describe('Project ObjectId to archive.') },
    async ({ project_id }) => {
      const result = await client.put(`/v1/projects/${encodeURIComponent(project_id)}/archive`, {});
      return { content: [{ type: 'text', text: JSON.stringify(result.project, null, 2) }] };
    }
  );
  server.tool(
    'unarchive_project',
    'Restore an archived project back to the active list.',
    { project_id: z.string().describe('Project ObjectId to unarchive.') },
    async ({ project_id }) => {
      const result = await client.put(`/v1/projects/${encodeURIComponent(project_id)}/unarchive`, {});
      return { content: [{ type: 'text', text: JSON.stringify(result.project, null, 2) }] };
    }
  );

  // ─── publish a project (public showcase link) ──────────────
  const pubText = (r, hint) => ({ content: [{ type: 'text', text: JSON.stringify({ ...r, success: undefined, ...(hint ? { _hint: hint } : {}) }, null, 2) }] });

  server.tool(
    'publish_project',
    'PUBLISH one of YOUR OWN projects as a public showcase page with a shareable link (the same as the app\'s Publish tab) and return the public `url`. Safe to call again: it never changes the URL, it only updates what you pass. In one call you can also set `allow_fork` (let viewers copy the project into their account), `story_only` (publish a blog-style post without sharing the project: see below), the public write-up `showcase_about`, and the project\'s FINAL VIDEO as the page hero. HERO VIDEO: the video must already be in THIS project — a generated video made with `project_id`, or a file you put there with `upload_media` (pass `project_id`; a local file is uploaded from disk). Then pass its media id as `hero_video_media_id` (or its Kolbo URL as `hero_video_url`). Anyone with the link can see the project\'s media, so ALWAYS confirm with the user before publishing, and consider `duplicate_project` first to publish a clean copy. STORY ONLY: with `story_only: true` the page shows only the story (get_project_story / update_project_story) and the media inside it; the media library, sessions, chats/docs/flows, cast and credits are not served, and copying is refused whatever `allow_fork` says. Use it when the user wants a post about the work without sharing the project. Undo with `unpublish_project`.',
    {
      project_id: z.string().describe('ObjectId of a project you OWN (from list_projects).'),
      allow_fork: z.boolean().optional().describe('Let viewers copy the project into their own account. Default true on first publish; omit to keep the current setting. Has no effect while story_only is true.'),
      story_only: z.boolean().optional().describe('true = the page shows only its story and the media in it (no library, sessions, chats, cast, credits, no copying). Default false on first publish; omit to keep the current setting.'),
      showcase_about: z.string().optional().describe('Public "About" write-up shown on the page (max 10000 chars, plain text). Omit on first publish and Kolbo drafts one for you; pass "" to clear.'),
      hero_video_media_id: z.string().optional().describe('Media id (from upload_media / list_media) of a VIDEO already in this project, shown as the full-bleed hero of the public page.'),
      hero_video_url: z.string().optional().describe('Alternative to hero_video_media_id: the Kolbo CDN URL of a video already in this project.')
    },
    async ({ project_id, allow_fork, story_only, showcase_about, hero_video_media_id, hero_video_url }) => {
      const body = {};
      if (allow_fork !== undefined) body.allow_fork = allow_fork;
      if (story_only !== undefined) body.story_only = story_only;
      if (showcase_about !== undefined) body.showcase_about = showcase_about;
      if (hero_video_media_id) body.hero_video_media_id = hero_video_media_id;
      if (hero_video_url) body.hero_video_url = hero_video_url;
      const result = await client.post(`/v1/projects/${encodeURIComponent(project_id)}/publish`, body);
      return pubText(result, 'Share `url` with the user — it is live now. Kolbo drafts the page\'s story on first publish; read or rewrite it with get_project_story / update_project_story. To list it in the public catalog (and Google) call `submit_project_to_catalog` (review is manual).');
    }
  );

  server.tool(
    'get_project_publication',
    'Check whether a project is published and read its public `url`, allow_fork, story_only, showcase_about, hero video, catalog listing status (none / pending / approved / rejected) and fork count. Returns `{ published: false }` when it is not published.',
    { project_id: z.string().describe('ObjectId of a project you OWN.') },
    async ({ project_id }) => pubText(await client.get(`/v1/projects/${encodeURIComponent(project_id)}/publish`))
  );

  server.tool(
    'unpublish_project',
    'Take a published project OFFLINE: the public link stops working immediately. Copies other people already made stay theirs. Re-publishing with `publish_project` creates a new link. Confirm with the user first.',
    { project_id: z.string().describe('ObjectId of a project you OWN.') },
    async ({ project_id }) => pubText(await client.delete(`/v1/projects/${encodeURIComponent(project_id)}/publish`))
  );

  server.tool(
    'submit_project_to_catalog',
    'Submit a PUBLISHED project to Kolbo\'s public catalog (status becomes `pending`; a Kolbo admin approves it — you cannot approve your own). Only approved projects are listed and may appear in Google search; until then the page is reachable by link but not indexed. Pass `submit: false` to withdraw. Fails with NOT_PUBLISHED if `publish_project` has not been called.',
    {
      project_id: z.string().describe('ObjectId of a project you OWN.'),
      submit: z.boolean().optional().describe('true (default) = submit for review, false = withdraw.')
    },
    async ({ project_id, submit }) => pubText(await client.post(`/v1/projects/${encodeURIComponent(project_id)}/publish/listing`, { submit: submit !== false }))
  );

  // ─── project story (the public page as a post) ─────────────
  const storyBlock = z.object({
    type: z.enum(['heading', 'text', 'media', 'step', 'before_after']).describe('heading = section title, text = paragraph, media = an image/video with a caption, step = a generated result shown WITH its real prompt and model (how it was made), before_after = two items side by side.'),
    text: z.string().optional().describe('heading (max 160 chars) / text (max 5000 chars). Plain text; blank lines separate paragraphs.'),
    media_id: z.string().optional().describe('media / step: a media_id from get_project_story `available_media`. A step must be a GENERATED result.'),
    media_url: z.string().optional().describe('Alternative to media_id: the Kolbo URL of media already in this project.'),
    before_media_id: z.string().optional().describe('before_after: the "before" item (often an upload, e.g. a sketch or source photo).'),
    after_media_id: z.string().optional().describe('before_after: the "after" item (the result).'),
    caption: z.string().optional().describe('media / before_after: one line on what it shows (max 300 chars).'),
    note: z.string().optional().describe('step: one or two sentences on what this step did and why (max 1000 chars).')
  });

  server.tool(
    'get_project_story',
    'Read the STORY of a project you own — its public page written as an ordered post (headings, text, media, workflow steps with their real prompts, before/after pairs) — plus `available_media`: the items the story may use (this project\'s generated results and the owner\'s own uploads, newest 200). Each item is COMPACT by default: media_id, file name (generation ids are in it), type, model and the first 120 characters of its prompt. Narrow the list with `search` (file name or prompt text) or `media_type`; pass `media: "full"` only when you need urls and complete prompts, or `media: "none"` when you only need the version. Call this FIRST before writing or editing a story: you need the `version` (pass it back as `base_version`) and the media ids. Also returns whether the project is published and its listing status.',
    {
      project_id: z.string().describe('ObjectId of a project you OWN.'),
      media: z.enum(['compact', 'full', 'none']).optional().describe('How to list available_media. compact (default) = id, file name, type, model, prompt opening. full = urls and complete prompts (large). none = leave the list out.'),
      media_type: z.enum(['image', 'video', 'audio']).optional().describe('Only list this media type.'),
      search: z.string().optional().describe('Only list items whose file name or prompt contains this text, e.g. a generation id.')
    },
    async ({ project_id, media, media_type, search }) => {
      const q = new URLSearchParams();
      if (media) q.set('media', media);
      if (media_type) q.set('media_type', media_type);
      if (search) q.set('search', search);
      const qs = q.toString();
      return pubText(await client.get(`/v1/projects/${encodeURIComponent(project_id)}/publish/story` + (qs ? `?${qs}` : '')));
    }
  );

  server.tool(
    'update_project_story',
    'REPLACE the story of a project you own (the public page\'s post) with the blocks you pass, in order. Read `get_project_story` first and pass its `version` as `base_version`: if the owner (or another agent) saved in between you get STORY_VERSION_CONFLICT — read again and re-apply. Blocks reference media by media_id from `available_media`; to use a NEW image or video, put it in the project first (`upload_media` with `project_id`, or generate with `project_id`). A `step` shows the generation\'s real prompt and model automatically — never retype them. Invalid blocks come back as `errors` with the block `index`; nothing is saved until all are valid. The saved story comes back compact (each block\'s media as id, file name and prompt opening); read the new `version` from it. `warnings` are advice (missing captions, no steps). Good structure: hero media → short intro → "How it was made" heading → 3-8 steps in order → before/after where an upload became a result → one closing line. If the project is published the change is live at once, so confirm with the user before replacing a story they wrote.',
    {
      project_id: z.string().describe('ObjectId of a project you OWN.'),
      blocks: z.array(storyBlock).describe('The whole story, in order (max 60 blocks). An empty array clears it.'),
      base_version: z.number().optional().describe('The `version` from get_project_story. Strongly recommended — without it you overwrite whatever is there.')
    },
    async ({ project_id, blocks, base_version }) => pubText(await client.put(
      `/v1/projects/${encodeURIComponent(project_id)}/publish/story`,
      { blocks, ...(base_version !== undefined ? { base_version } : {}) }
    ), 'Saved. The project page shows it now if the project is published (get_project_publication for the url).')
  );

  server.tool(
    'draft_project_story',
    'Ask Kolbo to DRAFT a story for a project you own, by analysing the project\'s own media (prompts, models, sessions, which generation used which upload). Takes up to about a minute. Returns the draft `blocks` WITHOUT saving them — review or edit them, then save with `update_project_story`. Fails with STORY_DRAFT_UNAVAILABLE when the project has no generated media yet.',
    { project_id: z.string().describe('ObjectId of a project you OWN.') },
    async ({ project_id }) => pubText(
      await client.post(`/v1/projects/${encodeURIComponent(project_id)}/publish/story/draft`, {}),
      'Not saved yet. Edit if needed, then call update_project_story with these blocks and the version from get_project_story.'
    )
  );

  server.tool(
    'get_project_page_insights',
    'Visitor analytics for the PUBLIC PAGE of a project you own (its "Page insights"): visitors, visits, average ACTIVE time (tab visible and in use), how many reached the story, scroll depth, clicks (media opened, recipes opened, project video played, "Use this project"), copies made, SIGN-UPS (new Kolbo accounts whose first visit was this page) with conversion rates, where visitors came from, countries, devices, a per-day series and the most-opened media. Anonymous first-party counts: no personal data, bots and the owner\'s own visits excluded, visitors deduped per day. Use it to tell the owner what works on their page (e.g. which step gets opened most) before editing the story.',
    {
      project_id: z.string().describe('ObjectId of a project you OWN.'),
      days: z.union([z.literal(7), z.literal(30), z.literal(90)]).optional().describe('Window in days: 7, 30 (default) or 90.')
    },
    async ({ project_id, days }) => {
      const qs = days ? `days=${days}` : '';
      return pubText(await client.get(`/v1/projects/${encodeURIComponent(project_id)}/publish/insights${qs ? '?' + qs : ''}`));
    }
  );

  server.tool(
    'remove_project_hero_video',
    'Clear the hero video of a published project (the page falls back to a mosaic of the project\'s stills). The video file itself stays in the project\'s media.',
    { project_id: z.string().describe('ObjectId of a project you OWN.') },
    async ({ project_id }) => pubText(await client.delete(`/v1/projects/${encodeURIComponent(project_id)}/publish/hero-video`))
  );

  // ─── project transfer (hand a project to another account) ──
  server.tool(
    'transfer_project',
    'Ask another Kolbo account (by email) to take over a project you OWN. Nothing moves until they accept (in the app, or via `respond_project_transfer`) within 7 days. On accept EVERYTHING moves to them with the same ids — sessions, generations, media, chats, docs, agents; your Visual DNA / moodboards / palette in the project cast are COPIED to them (you keep yours). Credit history keeps you as the payer. Set `keep_sender_as_editor` false to lose access entirely (default: you stay as an editor). Team/organization projects cannot be transferred. ALWAYS confirm the recipient email and the keep-editor choice with the user before calling.',
    {
      project_id: z.string().describe('Project ObjectId you own (from list_projects).'),
      email: z.string().describe('Email of the EXISTING Kolbo account that should become the owner.'),
      keep_sender_as_editor: z.boolean().optional().describe('Stay on the project as an editor after the transfer. Default: true.'),
    },
    async ({ project_id, email, keep_sender_as_editor }) => {
      const result = await client.post(`/v1/projects/${encodeURIComponent(project_id)}/transfer`, {
        email,
        ...(keep_sender_as_editor === undefined ? {} : { keep_sender_as_editor }),
      });
      return { content: [{ type: 'text', text: JSON.stringify(result.transfer, null, 2) }] };
    }
  );
  server.tool(
    'list_project_transfers',
    'List project transfer requests. `direction: incoming` (default) = projects other people want to give you; `outgoing` = ones you sent. Defaults to pending only. Use the `id` with `respond_project_transfer`.',
    {
      direction: z.enum(['incoming', 'outgoing']).optional().describe('Default: incoming.'),
      status: z.enum(['pending', 'accepted', 'declined', 'cancelled', 'expired', 'failed']).optional().describe('Default: pending.'),
    },
    async ({ direction, status }) => {
      const params = new URLSearchParams();
      if (direction) params.set('direction', direction);
      if (status) params.set('status', status);
      const qs = params.toString();
      const result = await client.get(`/v1/project-transfers${qs ? '?' + qs : ''}`);
      return { content: [{ type: 'text', text: JSON.stringify(result.transfers || [], null, 2) }] };
    }
  );
  server.tool(
    'respond_project_transfer',
    'Act on a project transfer request. `accept` / `decline` = you are the recipient; `cancel` = you sent it. Accepting makes you the owner of the whole project immediately. If the project still has generations running, accept returns TRANSFER_BLOCKED_IN_FLIGHT — wait and retry. Confirm with the user before accepting.',
    {
      transfer_id: z.string().describe('Transfer id from list_project_transfers.'),
      action: z.enum(['accept', 'decline', 'cancel']),
    },
    async ({ transfer_id, action }) => {
      const id = encodeURIComponent(transfer_id);
      // Explicit paths (not `/${action}`) so check-parity can see each route.
      let result;
      if (action === 'cancel') result = await client.delete(`/v1/project-transfers/${id}`);
      else if (action === 'accept') result = await client.post(`/v1/project-transfers/${id}/accept`, {});
      else result = await client.post(`/v1/project-transfers/${id}/decline`, {});
      return { content: [{ type: 'text', text: JSON.stringify(result.transfer, null, 2) }] };
    }
  );

  // ─── list_sessions ─────────────────────────────────────────
  server.tool(
    'list_sessions',
    "Read-only. Lists the user's sessions across all generation types (image, video, music, chat, transcription and more), newest activity first, paginated. Each row has session_id, name, type, types[] and project_id. Filter by project_id, type or types. The returned session_id is the input of move_session, rename_session and delete_session.",
    {
      project_id: z.string().optional().describe('Restrict to one project (ObjectId from list_projects).'),
      type: z.string().optional().describe('Restrict to one session type (pipe string on the row stays). Keys: image, video, video_from_image, music, speech, sound, image_edit, creative_director, chat, elements, first_last_frame, lipsync, video_from_video, transcription, global_image_edit, global_video_edit, shorts.'),
      types: z.array(z.string()).optional().describe('Restrict to several session types at once (same keys as `type`). Additive with `type`.'),
      page: z.number().optional().describe('Page number, 1-indexed. Default: 1'),
      limit: z.number().optional().describe('Results per page, max 50. Default: 20')
    },
    async ({ project_id, type, types, page, limit }) => {
      const params = new URLSearchParams();
      if (project_id) params.set('project_id', project_id);
      if (type) params.set('type', type);
      if (Array.isArray(types) && types.length) params.set('types', types.join(','));
      if (page) params.set('page', String(page));
      if (limit) params.set('limit', String(limit));
      const qs = params.toString();
      const result = await client.get(`/v1/sessions${qs ? '?' + qs : ''}`);
      const sessions = (result.sessions || []).map((s) => {
        const kinds = Array.isArray(s.types)
          ? s.types
          : String(s.type || '').split('|').filter(Boolean);
        return { ...s, type: s.type, types: kinds, project_id: s.project_id || null };
      });
      const text = JSON.stringify({
        sessions,
        pagination: result.pagination || null,
        _hint: 'Each row has project_id — pass it on every later generate/chat/upload in this conversation. Empty leftover sessions after a move: delete_session.'
      }, null, 2);

      return listResult(text, {
        widget: 'list',
        title: 'Sessions' + (type ? ' — ' + type : '') + ' (' + sessions.length + ')',
        items: sessions.map(s => ({
          id: s.session_id,
          title: s.name || s.types[0] || s.type || 'Session',
          subtitle: [
            s.session_id,
            (s.types || []).join(', '),
            s.project_id ? 'project ' + s.project_id : null,
            s.updated_at ? String(s.updated_at).slice(0, 10) : null
          ].filter(Boolean).join(' · '),
          badge: (s.types && s.types[0]) || undefined
        })),
        total: sessions.length,
        page_tool: 'list_sessions',
        next_args: sessions.length >= (limit || 20) ? { project_id, type, types, page: (page || 1) + 1, limit } : undefined
      });
    }
  );

  server.tool(
    'rename_session',
    'Rename a session the user can see in the Kolbo sidebar. Use this to edit a session title in place — never delete and recreate a session just to change its name. Call this immediately after the first generate of a plan bucket so the title matches the production plan (`Cast`, `Locations`, `Scene 03 — rooftop chase`) instead of an API daily name. Also use when the user says "call this Hero Sequence". Does not move the session or its media.',
    {
      session_id: z.string().describe('Session ObjectId from `list_sessions` or a generate_* result.'),
      name: z.string().describe('New sidebar title (1–200 characters).'),
      type: z.string().optional().describe('Optional session type hint to speed up the lookup. Omit if unsure.')
    },
    async ({ session_id, name, type }) => {
      const body = { name };
      if (type) body.type = type;
      const result = await client.patch(`/v1/sessions/${encodeURIComponent(session_id)}`, body);
      return { content: [{ type: 'text', text: JSON.stringify(result.session || result, null, 2) }] };
    }
  );

  server.tool(
    'delete_session',
    'Soft-delete a session (trash). Use for empty leftover sessions after `move_session` / `move_generations_to_session` / `split_session`, or when the user asks to remove a session. Does not permanently wipe media on its own — restore with `restore_session` if they change their mind.',
    {
      session_id: z.string().describe('Session ObjectId from `list_sessions`.'),
      type: z.string().optional().describe('Optional session type hint to speed up the lookup. Omit if unsure.')
    },
    async ({ session_id, type }) => {
      const result = await client.delete(
        `/v1/sessions/${encodeURIComponent(session_id)}`,
        type ? { type } : {}
      );
      return { content: [{ type: 'text', text: JSON.stringify(result.session || result, null, 2) }] };
    }
  );

  server.tool(
    'restore_session',
    'Restore a session previously removed with `delete_session` (clears deletedAt). Use when the user undoes a trash action in this conversation.',
    {
      session_id: z.string().describe('Session ObjectId that was just soft-deleted.'),
      type: z.string().optional().describe('Optional session type hint to speed up the lookup. Omit if unsure.')
    },
    async ({ session_id, type }) => {
      const body = {};
      if (type) body.type = type;
      const result = await client.post(`/v1/sessions/${encodeURIComponent(session_id)}/restore`, body);
      return { content: [{ type: 'text', text: JSON.stringify(result.session || result, null, 2) }] };
    }
  );

  // ─── Project context / knowledge base (NotebookLM-style) ───
  server.tool(
    'add_project_context',
    'Feed knowledge into a project\'s AI knowledge base (RAG): a website URL or pasted text (script, brief, research, brand facts). The server analyzes it in the background (source returns status "analyzing" and settles on its own) and synthesizes everything into the project\'s living profile. Use when the user says "add this to the project", "here\'s the script", "the project should know about X". Provide exactly ONE of url / text.',
    {
      project_id: z.string().describe('Project ObjectId (from list_projects).'),
      url: z.string().optional().describe('Website URL to fetch and analyze as a source.'),
      text: z.string().optional().describe('Raw text to store as a source (script, notes, research). Kept verbatim for RAG; an AI summary is generated for display.'),
      title: z.string().optional().describe('Optional title for a text source.')
    },
    async ({ project_id, url, text, title }) => {
      if (!url && !text) throw new Error('Provide url or text');
      const path = url
        ? `/v1/projects/${encodeURIComponent(project_id)}/context/url`
        : `/v1/projects/${encodeURIComponent(project_id)}/context/text`;
      const body = url ? { url } : { text, ...(title ? { title } : {}) };
      const result = await client.post(path, body);
      return { content: [{ type: 'text', text: JSON.stringify({ source: result.source, _hint: 'Analysis runs in the background — no need to poll; the project profile updates on its own.' }, null, 2) }] };
    }
  );

  server.tool(
    'list_project_context',
    "Read-only. Lists a project's knowledge-base sources (URLs, texts and files) with each source's stored summary and analysis status.",
    { project_id: z.string().describe('Project ObjectId.') },
    async ({ project_id }) => {
      const result = await client.get(`/v1/projects/${encodeURIComponent(project_id)}/context`);
      const sources = result.sources || [];
      const text = JSON.stringify({ sources, count: result.count || 0 }, null, 2);

      return listResult(text, {
        widget: 'list',
        title: 'Project Knowledge Base',
        items: sources.map(s => ({
          id: s.file_key,
          title: s.title || s.type,
          subtitle: s.type + ' · ' + s.status,
          open_url: s.url || null
        })),
        total: sources.length
      });
    }
  );

  server.tool(
    'delete_project_context',
    'Remove one source from a project\'s knowledge base by its file_key (from list_project_context).',
    {
      project_id: z.string().describe('Project ObjectId.'),
      file_key: z.string().describe('The source\'s file_key (URL-encode is handled for you).')
    },
    async ({ project_id, file_key }) => {
      const result = await client.delete(`/v1/projects/${encodeURIComponent(project_id)}/context/${encodeURIComponent(file_key)}`);
      return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] };
    }
  );

  server.tool(
    'get_project_profile',
    'Read a project\'s synthesized AI profile — the living markdown brief the platform maintains from the project\'s description, context sources, and activity. Use it to ground your work in what the project is about before generating.',
    { project_id: z.string().describe('Project ObjectId.') },
    async ({ project_id }) => {
      const result = await client.get(`/v1/projects/${encodeURIComponent(project_id)}/profile`);
      return { content: [{ type: 'text', text: JSON.stringify(result.profile, null, 2) }] };
    }
  );

  server.tool(
    'list_project_assets',
    "Read-only. Lists the Visual DNAs and moodboards on a project's cast (the @Name and #Name assets the project uses). Each Visual DNA row includes its description and a project-specific note. Returns only the assets attached to this project, not the user's whole library.",
    { project_id: z.string().describe('Project ObjectId from list_projects.') },
    async ({ project_id }) => {
      const result = await client.get(`/v1/projects/${encodeURIComponent(project_id)}/assets`);
      const dnas = result.visual_dnas || [];
      const moodboards = result.moodboards || [];
      const text = compactList(
        [
          ...dnas.map((d) => ({
            id: d.id,
            name: d.name,
            dna_type: d.dna_type,
            thumbnail: d.thumbnail_url || d.thumbnail,
            note: blurb(d.note),
            description: blurb(d.description),
          })),
          ...moodboards.map((m) => ({
            id: m.id,
            name: m.name,
            dna_type: 'moodboard',
            thumbnail: m.thumbnail_url || m.thumbnail,
            note: blurb(m.note),
            description: blurb(m.summary),
          })),
        ],
        {
          fields: ['id', 'name', 'dna_type', 'thumbnail', 'note', 'description'],
          cap: 80,
          total: dnas.length + moodboards.length,
          extra: { visual_dna_count: dnas.length, moodboard_count: moodboards.length },
          note: 'Edit identity text with update_project_asset (description) or update_visual_dna. `note` is project-scoped purpose. Link missing assets with link_project_asset — do not delete+recreate.',
        },
      );
      return listResult(text, {
        widget: 'list',
        title: 'Project cast',
        items: [
          ...dnas.map((d) => ({
            id: d.id,
            title: '@' + (d.name || d.id),
            subtitle: blurb(d.note || d.description),
            thumbnail: d.thumbnail_url || d.thumbnail,
            badge: d.dna_type,
          })),
          ...moodboards.map((m) => ({
            id: m.id,
            title: '#' + (m.name || m.id),
            subtitle: blurb(m.note || m.summary),
            thumbnail: m.thumbnail_url || m.thumbnail,
            badge: 'moodboard',
          })),
        ],
        total: dnas.length + moodboards.length
      });
    }
  );

  server.tool(
    'link_project_asset',
    'Tag an existing Visual DNA or moodboard onto a project\'s cast roster so it shows up as @Name / #Name for this project (and in the AI cast list). Does NOT copy or recreate the asset. After linking a DNA, write its description with update_project_asset. Optional `note` is the project-scoped purpose ("hero, dark-bg logo").',
    {
      project_id: z.string().describe('Project ObjectId.'),
      asset_type: z.enum(['visual_dna', 'moodboard']).describe('Kind of asset to tag.'),
      asset_id: z.string().describe('Visual DNA id or moodboard id (from list_visual_dnas / list_moodboards).'),
      note: z.string().optional().describe('Optional project-scoped purpose note (max 1000 chars).')
    },
    async ({ project_id, asset_type, asset_id, note }) => {
      const result = await client.post(`/v1/projects/${encodeURIComponent(project_id)}/assets/link`, {
        asset_type,
        asset_id
      });
      let savedNote = null;
      if (note !== undefined) {
        const n = await client.put(
          `/v1/projects/${encodeURIComponent(project_id)}/assets/${encodeURIComponent(asset_type)}/${encodeURIComponent(asset_id)}/note`,
          { note }
        );
        savedNote = n.note;
      }
      return {
        content: [{
          type: 'text',
          text: JSON.stringify({
            asset: result.asset,
            note: savedNote,
            _hint: asset_type === 'visual_dna'
              ? 'DNA is on the cast. Write its identity description with update_project_asset (description) — do not delete and recreate.'
              : 'Moodboard is on the cast. Set a purpose note with update_project_asset if needed.'
          }, null, 2)
        }]
      };
    }
  );

  server.tool(
    'unlink_project_asset',
    'Remove a Visual DNA or moodboard from a project\'s cast roster. The asset itself stays in the user\'s library — this only untags it from the project. Do NOT use this to edit a description; that is update_project_asset / update_visual_dna.',
    {
      project_id: z.string().describe('Project ObjectId.'),
      asset_type: z.enum(['visual_dna', 'moodboard']).describe('Kind of asset to untag.'),
      asset_id: z.string().describe('Asset id from list_project_assets.')
    },
    async ({ project_id, asset_type, asset_id }) => {
      const result = await client.delete(
        `/v1/projects/${encodeURIComponent(project_id)}/assets/${encodeURIComponent(asset_type)}/${encodeURIComponent(asset_id)}`
      );
      return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] };
    }
  );

  server.tool(
    'update_project_asset',
    'Edit a tagged project-cast asset in place. `description` writes the Visual DNA\'s identity text (the thing @Name injects — same as update_visual_dna prompt_helper). `note` is the project-scoped purpose ("what this asset is for HERE", fed into the AI cast roster). Pass either or both. NEVER unlink+relink or delete+recreate a DNA to change its description. Moodboards: `note` only here; use update_moodboard for style/images.',
    {
      project_id: z.string().describe('Project ObjectId.'),
      asset_type: z.enum(['visual_dna', 'moodboard']).describe('Kind of tagged asset.'),
      asset_id: z.string().describe('Asset id from list_project_assets.'),
      description: z.string().optional().describe('For visual_dna: new identity description (replaces prompt_helper on the DNA itself). Ignored for moodboards.'),
      note: z.string().optional().describe('Project-scoped purpose note (max 1000 chars). Pass "" to clear.')
    },
    async ({ project_id, asset_type, asset_id, description, note }) => {
      if (description === undefined && note === undefined) {
        throw new Error('Provide description and/or note');
      }
      const out = {};
      if (note !== undefined) {
        const n = await client.put(
          `/v1/projects/${encodeURIComponent(project_id)}/assets/${encodeURIComponent(asset_type)}/${encodeURIComponent(asset_id)}/note`,
          { note }
        );
        out.note = n.note;
      }
      if (description !== undefined) {
        if (asset_type !== 'visual_dna') {
          throw new Error('description is only valid for asset_type=visual_dna — use update_moodboard for moodboard style notes');
        }
        const dna = await client.put(`/v1/visual-dna/${encodeURIComponent(asset_id)}`, {
          prompt_helper: description
        });
        out.visual_dna = dna.visual_dna || dna;
      }
      return {
        content: [{
          type: 'text',
          text: JSON.stringify({
            ...out,
            _hint: 'Cast asset updated in place. Keep using the same id and @Name.'
          }, null, 2)
        }]
      };
    }
  );

  server.tool(
    'regenerate_project_profile',
    'Force-regenerate a project\'s AI profile from its current context sources (also clears any manual-edit lock). Use after adding several new sources when the user wants the brief refreshed now.',
    { project_id: z.string().describe('Project ObjectId.') },
    async ({ project_id }) => {
      const result = await client.post(`/v1/projects/${encodeURIComponent(project_id)}/profile/regenerate`, {});
      return { content: [{ type: 'text', text: JSON.stringify(result.profile || result, null, 2) }] };
    }
  );
}

module.exports = { registerProjectTools };