mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 03:59:29 +00:00
* feat(operations): move MCP to /mcp and add search, comment, metadata, version, and upload operations - Serve the MCP server at /mcp instead of /api/mcp - Add search_assets, copy_assets, get_folder_instructions, list_comments, create_comment, resolve_comment, list_metadata_fields, update_asset_metadata, create_version_stack, list_versions, and remove_version on the agent, MCP, and CLI - Add create_file and create_version (agent, MCP) with Creation Context metadata, and complete_upload (MCP) for presigned two-step uploads - Replace the native agent create_file/create_version tools with Operation-backed ones - Route the comment-complete, metadata, copy, version stack, and AGENTS.md read REST endpoints through their Operations * docs(readme): add built-in MCP server and CLI to features list
371 lines
14 KiB
TypeScript
371 lines
14 KiB
TypeScript
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest'
|
|
import * as fs from 'fs'
|
|
import * as path from 'path'
|
|
import { fileURLToPath } from 'url'
|
|
import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client'
|
|
import { prisma, type User } from '@shumai/db'
|
|
import { setupTestDbHooks } from '@shumai/db/test'
|
|
import { projectService } from '@shumai/core/src/project/project'
|
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
|
import { apiTokenService } from '@shumai/core/src/user/api-token'
|
|
import app from './index'
|
|
|
|
/**
|
|
* End-to-end through the composed API app: API token auth → /mcp and /api/ops →
|
|
* runOperation → core services → database. Nothing is mocked.
|
|
*/
|
|
describe('MCP server and /api/ops (integration)', () => {
|
|
setupTestDbHooks()
|
|
|
|
let owner: User
|
|
let ownerToken: string
|
|
let reviewerToken: string
|
|
let teamId: string
|
|
let projectId: string
|
|
let rootFolderId: string
|
|
const clients: Client[] = []
|
|
|
|
beforeEach(async () => {
|
|
owner = await prisma.user.create({
|
|
data: { name: 'MCP Owner', email: 'mcp-owner@test.com', password: 'pw' },
|
|
})
|
|
const reviewer = await prisma.user.create({
|
|
data: { name: 'MCP Reviewer', email: 'mcp-reviewer@test.com', password: 'pw' },
|
|
})
|
|
const team = await prisma.team.create({ data: { name: 'MCP Team' } })
|
|
teamId = team.id
|
|
await prisma.teamMember.create({ data: { teamId, userId: owner.id, role: 'owner' } })
|
|
await prisma.teamMember.create({ data: { teamId, userId: reviewer.id, role: 'reviewer' } })
|
|
const project = await projectService.createProject(owner, { teamId, name: 'MCP Project' })
|
|
projectId = project.id
|
|
rootFolderId = project.rootFolder!
|
|
|
|
ownerToken = (await apiTokenService.createToken(owner.id, 'mcp')).token
|
|
reviewerToken = (await apiTokenService.createToken(reviewer.id, 'mcp')).token
|
|
})
|
|
|
|
afterEach(async () => {
|
|
await Promise.all(clients.splice(0).map((c) => c.close()))
|
|
})
|
|
|
|
async function connect(token: string): Promise<Client> {
|
|
const transport = new StreamableHTTPClientTransport(new URL('http://shumai.test/mcp'), {
|
|
requestInit: { headers: { Authorization: `Bearer ${token}` } },
|
|
fetch: async (input, init) => app.request(String(input), init),
|
|
})
|
|
const client = new Client({ name: 'integration-test', version: '1.0.0' })
|
|
await client.connect(transport)
|
|
clients.push(client)
|
|
return client
|
|
}
|
|
|
|
it('lets an MCP client find a project and create a folder in it', async () => {
|
|
const client = await connect(ownerToken)
|
|
|
|
const projects = await client.callTool({ name: 'list_projects', arguments: {} })
|
|
expect(projects.structuredContent).toEqual({
|
|
data: [{ id: projectId, name: 'MCP Project', teamId, rootFolderId }],
|
|
})
|
|
|
|
const created = await client.callTool({
|
|
name: 'create_folder',
|
|
arguments: { parent: rootFolderId, name: 'From MCP' },
|
|
})
|
|
expect(created.isError).toBeFalsy()
|
|
const folderId = (created.structuredContent as { id: string }).id
|
|
|
|
const folder = await prisma.asset.findUniqueOrThrow({ where: { id: folderId } })
|
|
expect(folder).toMatchObject({ name: 'From MCP', parentId: rootFolderId, creatorId: owner.id })
|
|
const audit = await prisma.auditLog.findFirstOrThrow({
|
|
where: { action: 'folder_create', itemId: folderId },
|
|
})
|
|
expect(audit).toMatchObject({ userId: owner.id, teamId, projectId, via: 'mcp' })
|
|
})
|
|
|
|
it("enforces the token owner's permissions", async () => {
|
|
const client = await connect(reviewerToken)
|
|
|
|
const result = await client.callTool({
|
|
name: 'create_folder',
|
|
arguments: { parent: rootFolderId, name: 'Not allowed' },
|
|
})
|
|
|
|
expect(result.isError).toBe(true)
|
|
expect(await prisma.asset.count({ where: { name: 'Not allowed' } })).toBe(0)
|
|
})
|
|
|
|
it('rejects MCP requests without a valid token', async () => {
|
|
const initialize = JSON.stringify({
|
|
jsonrpc: '2.0',
|
|
id: 1,
|
|
method: 'initialize',
|
|
params: {
|
|
protocolVersion: '2025-06-18',
|
|
capabilities: {},
|
|
clientInfo: { name: 'x', version: '1' },
|
|
},
|
|
})
|
|
const headers = {
|
|
'Content-Type': 'application/json',
|
|
Accept: 'application/json, text/event-stream',
|
|
}
|
|
|
|
const missing = await app.request('/mcp', { method: 'POST', headers, body: initialize })
|
|
expect(missing.status).toBe(401)
|
|
expect(missing.headers.get('WWW-Authenticate')).toContain('Bearer')
|
|
|
|
const invalid = await app.request('/mcp', {
|
|
method: 'POST',
|
|
headers: { ...headers, Authorization: 'Bearer shumai_invalid' },
|
|
body: initialize,
|
|
})
|
|
expect(invalid.status).toBe(401)
|
|
})
|
|
|
|
it('serves /api/ops for the CLI with via cli', async () => {
|
|
const headers = {
|
|
Authorization: `Bearer ${ownerToken}`,
|
|
'Content-Type': 'application/json',
|
|
'x-shumai-client': 'cli',
|
|
}
|
|
|
|
const created = await app.request('/api/ops/create_folder', {
|
|
method: 'POST',
|
|
headers,
|
|
body: JSON.stringify({ parent: rootFolderId, name: 'From CLI' }),
|
|
})
|
|
expect(created.status).toBe(200)
|
|
const { id } = (await created.json()) as { id: string }
|
|
|
|
const listed = await app.request('/api/ops/list_assets', {
|
|
method: 'POST',
|
|
headers,
|
|
body: JSON.stringify({ parent: rootFolderId }),
|
|
})
|
|
expect(listed.status).toBe(200)
|
|
const listing = (await listed.json()) as { data: Array<{ id: string }>; total: number }
|
|
expect(listing.data.map((a) => a.id)).toContain(id)
|
|
|
|
const audit = await prisma.auditLog.findFirstOrThrow({
|
|
where: { action: 'folder_create', itemId: id },
|
|
})
|
|
expect(audit.via).toBe('cli')
|
|
|
|
const unauthenticated = await app.request('/api/ops/list_projects', { method: 'POST' })
|
|
expect(unauthenticated.status).toBe(401)
|
|
})
|
|
|
|
it('leaves session-only MCP management routes behind session auth', async () => {
|
|
const res = await app.request('/api/mcp/servers/some-id', {
|
|
headers: { Authorization: `Bearer ${ownerToken}` },
|
|
})
|
|
|
|
expect(res.status).toBe(401)
|
|
})
|
|
|
|
it('no longer serves the MCP server under /api', async () => {
|
|
const res = await app.request('/api/mcp', {
|
|
method: 'POST',
|
|
headers: {
|
|
Authorization: `Bearer ${ownerToken}`,
|
|
'Content-Type': 'application/json',
|
|
Accept: 'application/json, text/event-stream',
|
|
},
|
|
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'tools/list' }),
|
|
})
|
|
// It falls through to the session-only API routes instead of answering as MCP.
|
|
expect(res.status).toBe(401)
|
|
expect(await res.text()).not.toContain('jsonrpc')
|
|
})
|
|
|
|
describe('read_asset', () => {
|
|
const bucket = 'shumai-test-mcp-read'
|
|
const originalBucket = process.env.S3_BUCKET
|
|
const png = fs.readFileSync(
|
|
path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../e2e/fixtures/small.png'),
|
|
)
|
|
let imageId: string
|
|
|
|
beforeAll(() => {
|
|
process.env.S3_BUCKET = bucket
|
|
})
|
|
|
|
afterAll(async () => {
|
|
if (originalBucket === undefined) delete process.env.S3_BUCKET
|
|
else process.env.S3_BUCKET = originalBucket
|
|
await s3Service.deletePrefix(bucket, '').catch(() => undefined)
|
|
})
|
|
|
|
beforeEach(async () => {
|
|
const key = `projects/${projectId}/photo.png`
|
|
await s3Service.putObject(bucket, key, png, png.length, 'image/png')
|
|
const storageKey = await prisma.storageKey.create({ data: { key } })
|
|
imageId = (
|
|
await prisma.asset.create({
|
|
data: {
|
|
name: 'photo.png',
|
|
type: 'file',
|
|
status: 'uploaded',
|
|
mediaType: 'image/png',
|
|
projectId,
|
|
parentId: rootFolderId,
|
|
storageKeyId: storageKey.id,
|
|
media: { proxyType: 'image' } as unknown as PrismaJson.MediaInfo,
|
|
},
|
|
})
|
|
).id
|
|
})
|
|
|
|
it('shows a reviewer the image inline, with links but no storage keys', async () => {
|
|
const client = await connect(reviewerToken)
|
|
|
|
const result = await client.callTool({ name: 'read_asset', arguments: { assetId: imageId } })
|
|
|
|
expect(result.isError).toBeFalsy()
|
|
const content = result.content as Array<{ type: string; data?: string; text?: string }>
|
|
expect(content[0].text).toContain(`Image asset "photo.png" (ID: ${imageId}):`)
|
|
expect(content[1]).toEqual({
|
|
type: 'image',
|
|
data: png.toString('base64'),
|
|
mimeType: 'image/png',
|
|
})
|
|
const output = result.structuredContent as { images: Array<Record<string, unknown>> }
|
|
expect(output.images).toEqual([
|
|
{ label: 'Image', mimeType: 'image/png', url: expect.any(String) },
|
|
])
|
|
expect(JSON.stringify(result)).not.toContain('"key"')
|
|
})
|
|
|
|
it('serves read and render to the CLI with links only', async () => {
|
|
const headers = {
|
|
Authorization: `Bearer ${reviewerToken}`,
|
|
'Content-Type': 'application/json',
|
|
'x-shumai-client': 'cli',
|
|
}
|
|
|
|
const read = await app.request('/api/ops/read_asset', {
|
|
method: 'POST',
|
|
headers,
|
|
body: JSON.stringify({ assetId: imageId }),
|
|
})
|
|
expect(read.status).toBe(200)
|
|
const output = (await read.json()) as { status: string; images: Array<{ url: string }> }
|
|
expect(output.status).toBe('ready')
|
|
expect(output.images).toEqual([
|
|
{ label: 'Image', mimeType: 'image/png', url: expect.any(String) },
|
|
])
|
|
|
|
// The link is served by the API itself with local storage
|
|
const link = new URL(output.images[0].url)
|
|
const image = await app.request(link.pathname + link.search)
|
|
expect(image.status).toBe(200)
|
|
expect(Buffer.from(await image.arrayBuffer()).equals(png)).toBe(true)
|
|
|
|
const render = await app.request('/api/ops/get_asset_render', {
|
|
method: 'POST',
|
|
headers,
|
|
body: JSON.stringify({ taskId: 'not-a-task' }),
|
|
})
|
|
expect(render.status).toBe(404)
|
|
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' },
|
|
})
|
|
const token = (await apiTokenService.createToken(outsider.id, 'mcp')).token
|
|
const client = await connect(token)
|
|
|
|
const result = await client.callTool({ name: 'read_asset', arguments: { assetId: imageId } })
|
|
|
|
expect(result.isError).toBe(true)
|
|
})
|
|
|
|
it('lets an MCP client upload a file: create_file, PUT the bytes, complete_upload', async () => {
|
|
const client = await connect(ownerToken)
|
|
|
|
const started = await client.callTool({
|
|
name: 'create_file',
|
|
arguments: { parent: rootFolderId, name: 'upload.png' },
|
|
})
|
|
expect(started.isError).toBeFalsy()
|
|
const { uploadId, uploadUrl, method } = started.structuredContent as {
|
|
uploadId: string
|
|
uploadUrl: string
|
|
method: string
|
|
key?: string
|
|
}
|
|
expect(method).toBe('PUT')
|
|
expect(started.structuredContent).not.toHaveProperty('key')
|
|
|
|
// Upload the bytes the way an agent would with curl, to the API's local storage
|
|
const link = new URL(uploadUrl)
|
|
const put = await app.request(link.pathname + link.search, {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'image/png', 'Content-Length': String(png.length) },
|
|
body: png,
|
|
})
|
|
expect(put.status).toBe(200)
|
|
|
|
const completed = await client.callTool({ name: 'complete_upload', arguments: { uploadId } })
|
|
expect(completed.isError).toBeFalsy()
|
|
const file = completed.structuredContent as { id: string; name: string; size: number }
|
|
expect(file).toMatchObject({ name: 'upload.png', size: png.length })
|
|
|
|
const row = await prisma.asset.findUniqueOrThrow({ where: { id: file.id } })
|
|
expect(row).toMatchObject({ parentId: rootFolderId, creatorId: owner.id })
|
|
const audit = await prisma.auditLog.findFirstOrThrow({
|
|
where: { action: 'file_create', itemId: file.id },
|
|
})
|
|
expect(audit).toMatchObject({ userId: owner.id, teamId, projectId, via: 'mcp' })
|
|
})
|
|
|
|
it('does not let a reviewer start an upload over MCP', async () => {
|
|
const client = await connect(reviewerToken)
|
|
const result = await client.callTool({
|
|
name: 'create_file',
|
|
arguments: { parent: rootFolderId, name: 'nope.png' },
|
|
})
|
|
expect(result.isError).toBe(true)
|
|
})
|
|
})
|
|
})
|