feat: expose read_asset and download_asset on MCP and the CLI (#499)

* feat(asset-read): expose read_asset on MCP and the CLI

Enable the mcp and cli surfaces for read_asset and get_asset_render.

- MCP: add per-Operation tool extensions. The read presenter attaches up
  to 8 images (2 MB each) inline, lists image links, supports
  inlineImages: false, and tells the model to call get_asset_render when
  a render is pending. Storage keys are stripped from the result.
- CLI: generated commands can flatten object inputs into prefixed flags
  (cli.flatten) and omit fields (cli.omit); add per-command extensions.
  New `read` and `render` commands print image links, download them with
  --out, and support --no-wait with a printed `render <taskId>` hint.
- Round frame timestamps in labels to 3 decimals.

* fix(mcp): make tool input schemas portable to single-type dialects

Nullable inputs were emitted as `type: ["number", "null"]` (or a `null`
anyOf branch), which clients that map tool schemas onto an OpenAPI-style
subset (e.g. Gemini function declarations) may reject.

- Make read_asset's startPage, endPage, and degree optional instead of
  nullable.
- Move null handling into a shared dropNullOptionals helper in
  @shumai/operations that also strips nulls inside nested config
  objects. The agent uses it, and the MCP server applies it before the
  SDK validates arguments, so models that send null keep working.
- Test that no Operation input schema (and no listed MCP tool) uses a
  nullable type.

* feat(asset-download): share download_asset across agent, MCP and CLI

Turn the agent-only download_asset tool into an Operation on all three
surfaces.

- download_asset takes up to 50 file or version stack IDs and returns
  presigned links. proxy (default true) picks the web preview when there
  is one, as the agent always did; proxy: false returns the original.
  Folders are rejected. Read is checked on every ID before anything runs.
- Agent: an extension saves the files into the .pi sandbox and keeps the
  agent-only storage key mode (e.g. frames from read_asset). Agent tool
  extensions gain description, parameterOverrides and intercept.
- CLI: `shumai-cli download <assetId...> [--out <dir>] [--original]`
  streams files to disk without overwriting existing names.
- read_asset's unsupported-type error now names the right way to
  download on each surface (download_asset, or `shumai-cli download`).
- Add the Download Asset toggle to agent settings.
This commit is contained in:
Yiling-J
2026-10-27 08:09:11 +08:00
committed by GitHub
parent 155fb95a0e
commit 50fbd97953
55 changed files with 2612 additions and 620 deletions
+52 -2
View File
@@ -1,7 +1,10 @@
import { Hono } from 'hono'
import { createMcpHandler, McpServer, type CallToolResult } from '@modelcontextprotocol/server'
import type { z } from 'zod'
import type { Prisma } from '@shumai/db'
import { dropNullOptionals } from '@shumai/operations'
import { getOperationsForSurface, runOperation, type Operation } from '@shumai/core/src/operations'
import { mcpExtensions, type McpToolExtension } from './mcp-extensions'
import { assertWritableInDemoMode, describeOperationError } from './operation-support'
type User = Prisma.UserGetPayload<Record<string, never>>
@@ -14,16 +17,58 @@ export const MCP_SERVER_INSTRUCTIONS = [
'Start with list_projects to find a project and its rootFolderId, then pass folder IDs as',
'`parent` to list_assets and create_folder. list_assets is paginated: when',
'pageInfo.hasNextPage is true, call it again with after = pageInfo.endCursor.',
'Use read_asset to look at a file (image, video frames, PDF pages, 3D views, or text);',
'if it returns status "pending", call get_asset_render with its taskId.',
].join(' ')
/**
* The tool's input schema, with `null` for optional arguments treated as omitted. The SDK
* validates arguments before calling the tool, so the nulls must be dropped here; the JSON
* Schema shown to clients is unchanged (optional, not nullable, which more clients support).
*/
function toolInputSchema(schema: z.ZodObject): Pick<z.ZodObject, '~standard'> {
const standard = schema['~standard']
return {
'~standard': {
...standard,
validate: (value) => standard.validate(dropNullOptionals(schema, value)),
},
}
}
/** Separate MCP-only arguments (declared by an extension) from the Operation input. */
function splitArgs(
extension: McpToolExtension | undefined,
args: unknown,
): { input: unknown; extra: Record<string, unknown> } {
if (!extension?.extraInput || !args || typeof args !== 'object') {
return { input: args, extra: {} }
}
const extraKeys = new Set(Object.keys(extension.extraInput.shape))
const input: Record<string, unknown> = {}
const extra: Record<string, unknown> = {}
for (const [key, value] of Object.entries(args)) {
;(extraKeys.has(key) ? extra : input)[key] = value
}
return { input, extra }
}
async function callOperation(
op: Operation,
userId: string,
args: unknown,
): Promise<CallToolResult> {
const extension = mcpExtensions.get(op.definition.name)
try {
await assertWritableInDemoMode(op, userId)
const output = (await runOperation(op, { userId, via: 'mcp' }, args)) as Record<string, unknown>
const { input, extra } = splitArgs(extension, args)
const output = (await runOperation(op, { userId, via: 'mcp' }, input)) as Record<
string,
unknown
>
if (extension) {
return await extension.present(output, extra)
}
return {
content: [{ type: 'text', text: JSON.stringify(output) }],
structuredContent: output,
@@ -44,12 +89,17 @@ export function buildMcpServer(userId: string): McpServer {
)
for (const op of getOperationsForSurface('mcp')) {
const { definition } = op
const extension = mcpExtensions.get(definition.name)
server.registerTool(
definition.name,
{
title: definition.title,
description: definition.description,
inputSchema: definition.input,
inputSchema: toolInputSchema(
extension?.extraInput
? definition.input.extend(extension.extraInput.shape)
: definition.input,
),
outputSchema: definition.output,
annotations: {
title: definition.title,