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
2026-10-26 22:33:30 +08:00
parent b7dccad6e5
commit bc3368f3e3
40 changed files with 1323 additions and 579 deletions
@@ -99,7 +99,7 @@ export async function presentReadForMcp(
if (output.text) {
text += `\n\n${output.text.content}`
if (output.text.truncated) {
text += `\n\n[Content truncated: showing the first part of a ${output.text.size}-byte file.]`
text += `\n\n[Content truncated: showing the first part of a ${output.text.size}-byte file. Use download_asset to get the whole file.]`
}
}
return { content: [{ type: 'text', text }, ...images], structuredContent }
@@ -255,6 +255,43 @@ describe('MCP server and /api/ops (integration)', () => {
expect(await render.json()).toEqual({ error: 'Render not found' })
})
it('gives MCP and CLI callers a download link to the file', async () => {
const client = await connect(reviewerToken)
const result = await client.callTool({
name: 'download_asset',
arguments: { assetIds: [imageId] },
})
expect(result.isError).toBeFalsy()
const viaMcp = result.structuredContent as { files: Array<Record<string, unknown>> }
expect(viaMcp.files).toEqual([
{
assetId: imageId,
name: 'photo.png',
kind: 'original',
size: png.length,
mimeType: 'image/png',
url: expect.any(String),
},
])
const res = await app.request('/api/ops/download_asset', {
method: 'POST',
headers: {
Authorization: `Bearer ${reviewerToken}`,
'Content-Type': 'application/json',
'x-shumai-client': 'cli',
},
body: JSON.stringify({ assetIds: [imageId], proxy: false }),
})
expect(res.status).toBe(200)
const viaCli = (await res.json()) as { files: Array<{ url: string; key?: string }> }
expect(viaCli.files[0].key).toBeUndefined()
const link = new URL(viaCli.files[0].url)
const file = await app.request(link.pathname + link.search)
expect(file.status).toBe(200)
expect(Buffer.from(await file.arrayBuffer()).equals(png)).toBe(true)
})
it('does not let users outside the project read', async () => {
const outsider = await prisma.user.create({
data: { name: 'Outsider', email: 'mcp-outsider@test.com', password: 'pw' },
+2 -1
View File
@@ -55,6 +55,7 @@ describe('MCP server', () => {
expect(tools.map((t) => t.name).sort()).toEqual([
'create_folder',
'delete_asset',
'download_asset',
'get_asset_render',
'list_assets',
'list_projects',
@@ -136,7 +137,7 @@ describe('MCP server', () => {
it('does not expose agent-only capabilities', async () => {
const names = (await client.listTools()).tools.map((t) => t.name)
for (const agentOnly of ['bash', 'read_skill', 'download_asset', 'mcp']) {
for (const agentOnly of ['bash', 'read_skill', 'mcp']) {
expect(names).not.toContain(agentOnly)
}
})