Skip to content

armgit / kiln

Public Imported from GitHub
0

Build and revise procedural 3D assets with your coding agent. Local MCP server, CLI and TypeScript engine with rendered review, editable source and GLB export.

4.5 KB
Raw Blame
/**
 * Builds the MCP manifest from the registry: the tool definitions exactly as
 * `tools/list` advertises them, plus the fixed resources and templates.
 *
 * The JSON Schema for each tool comes from the registry's zod schema through
 * the Standard JSON Schema interface, the same conversion the protocol library
 * applies when it is handed a zod schema, so the manifest is what the library
 * would have advertised -- minus the `$schema` keyword, because 2020-12 is the
 * dialect MCP assumes when none is named, and the keyword costs 56 bytes in
 * every definition a harness sends with every request.
 */
import type { z } from 'zod';

import { localAssetLibrary } from './assets-node';
import type { ExportRootWriter } from './export-root';
import { createLocalToolContext } from './local-runtime';
import {
  KILN_RESOURCE_TEMPLATES,
  KILN_WIDGET_RESOURCE,
  type KilnMcpManifest,
  type KilnMcpManifestTemplate,
  type KilnMcpManifestTool,
} from './mcp-core';
import {
  createKilnProgramToolRegistry,
  type KilnToolContext,
  type KilnToolDef,
} from './tools/registry';

type StandardJsonSchemaCarrier = {
  '~standard': {
    jsonSchema?: {
      input(options: { target: string }): Record<string, unknown>;
      output(options: { target: string }): Record<string, unknown>;
    };
  };
};

const JSON_SCHEMA_TARGET = 'draft-2020-12';

function standardJsonSchema(schema: z.ZodType, io: 'input' | 'output'): Record<string, unknown> {
  const carrier = schema as unknown as StandardJsonSchemaCarrier;
  const converter = carrier['~standard'].jsonSchema;
  if (!converter)
    throw new Error('The registry schema does not implement Standard JSON Schema (zod >= 4.2).');
  const { $schema: _dialect, ...rest } = converter[io]({ target: JSON_SCHEMA_TARGET });
  return rest;
}

/** The input schema as advertised: an object root first, the rest as converted. */
export function toolInputJsonSchema(def: KilnToolDef): KilnMcpManifestTool['inputSchema'] {
  const converted = standardJsonSchema(def.inputSchema, 'input');
  if (converted['type'] !== undefined && converted['type'] !== 'object')
    throw new Error(`${def.name}: a tool input schema must describe an object.`);
  return { type: 'object', ...converted };
}

/** One registry definition as one `tools/list` entry, keys in the order the wire has always carried them. */
export function toolManifestEntry(def: KilnToolDef): KilnMcpManifestTool {
  return {
    name: def.name,
    description: def.description,
    inputSchema: toolInputJsonSchema(def),
    ...(def.annotations ? { annotations: def.annotations } : {}),
    ...(def.ui
      ? {
          _meta: {
            ui: { resourceUri: def.ui.resourceUri, visibility: ['model'] },
            'openai/outputTemplate': def.ui.resourceUri,
            'ui/resourceUri': def.ui.resourceUri,
          },
        }
      : {}),
    ...(def.outputSchema
      ? { outputSchema: { type: 'object', ...standardJsonSchema(def.outputSchema, 'output') } }
      : {}),
  };
}

/** Which templates a context can serve: asset files need a library, project packages a bundle reader. */
export function resourceTemplatesFor(context: KilnToolContext): KilnMcpManifestTemplate[] {
  return [
    ...(context.assetLibrary ? [KILN_RESOURCE_TEMPLATES.assetFile] : []),
    ...(context.projectBundleReader ? [KILN_RESOURCE_TEMPLATES.projectPackage] : []),
    // kiln_export issues these, and it comes with the library whether or not a program is saved.
    ...(context.assetLibrary ? [KILN_RESOURCE_TEMPLATES.programBuild] : []),
  ];
}

export function buildMcpManifest(defs: KilnToolDef[], context: KilnToolContext): KilnMcpManifest {
  return {
    kind: 'kiln.mcp-manifest.v1',
    tools: defs.map(toolManifestEntry),
    resources: [KILN_WIDGET_RESOURCE],
    resourceTemplates: resourceTemplatesFor(context),
  };
}

const MANIFEST_EXPORT_ROOT: ExportRootWriter = {
  writeNew: async () => {
    throw new Error('The manifest export root writes nothing.');
  },
};

/**
 * The manifest the packaged server advertises: the full surface with both
 * templates, built from a context shaped like the packaged host's but with no
 * environment of its own, so the file is the same on every machine.
 */
export function packagedMcpManifest(): KilnMcpManifest {
  const context = createLocalToolContext(
    // The entry drops kiln_export_to unless KILN_EXPORT_ROOT is set; this writer is never called.
    { assetLibrary: localAssetLibrary(), exportRoot: MANIFEST_EXPORT_ROOT },
    {},
  );
  return buildMcpManifest(createKilnProgramToolRegistry(context), context);
}