Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/worker/src/mcp/capabilities/apps/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { uiGetAppCapability } from './ui-get-app.ts'
import { uiListAppsCapability } from './ui-list-apps.ts'
import { uiLoadAppSourceCapability } from './ui-load-app-source.ts'
import { uiSaveAppCapability } from './ui-save-app.ts'
import { uiUpdateAppCapability } from './ui-update-app.ts'

export const appsDomain = defineDomain({
name: capabilityDomainNames.apps,
Expand All @@ -17,7 +16,6 @@ export const appsDomain = defineDomain({
uiGetAppCapability,
uiListAppsCapability,
uiLoadAppSourceCapability,
uiUpdateAppCapability,
uiDeleteAppCapability,
],
})
1 change: 0 additions & 1 deletion packages/worker/src/mcp/capabilities/apps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ export { uiGetAppCapability } from './ui-get-app.ts'
export { uiListAppsCapability } from './ui-list-apps.ts'
export { uiLoadAppSourceCapability } from './ui-load-app-source.ts'
export { uiSaveAppCapability } from './ui-save-app.ts'
export { uiUpdateAppCapability } from './ui-update-app.ts'

export const appsCapabilities = appsDomain.capabilities
67 changes: 49 additions & 18 deletions packages/worker/src/mcp/capabilities/apps/ui-save-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { buildSavedUiUrl } from '#worker/ui-artifact-urls.ts'
import { defineDomainCapability } from '#mcp/capabilities/define-domain-capability.ts'
import { capabilityDomainNames } from '#mcp/capabilities/domain-metadata.ts'
import { type CapabilityContext } from '#mcp/capabilities/types.ts'
import { deleteUiArtifact, insertUiArtifact } from '#mcp/ui-artifacts-repo.ts'
import {
deleteUiArtifact,
insertUiArtifact,
updateUiArtifact,
} from '#mcp/ui-artifacts-repo.ts'
import { buildUiArtifactEmbedText } from '#mcp/ui-artifacts-embed.ts'
import { upsertUiArtifactVector } from '#mcp/ui-artifacts-vectorize.ts'
import { requireMcpUser } from '#mcp/capabilities/meta/require-user.ts'
Expand All @@ -13,6 +17,13 @@ import {
} from '#mcp/ui-artifact-parameters.ts'

const inputSchema = z.object({
app_id: z
.string()
.min(1)
.optional()
.describe(
'Optional saved UI artifact id to update in place. Omit to create a new saved app.',
),
title: z.string().min(1).describe('Short title for the saved UI artifact.'),
description: z
.string()
Expand Down Expand Up @@ -50,29 +61,45 @@ export const uiSaveAppCapability = defineDomainCapability(
{
name: 'ui_save_app',
description:
'Save a generated UI artifact for the signed-in user so it can be reopened later by app_id without sending the source back through the model context.',
keywords: ['ui', 'app', 'artifact', 'save', 'persist', 'mcp app'],
'Create or replace a saved UI artifact for the signed-in user so it can be reopened later by app_id without sending the source back through the model context.',
keywords: ['ui', 'app', 'artifact', 'save', 'persist', 'update', 'mcp app'],
readOnly: false,
idempotent: false,
destructive: false,
inputSchema,
outputSchema,
async handler(args, ctx: CapabilityContext) {
const user = requireMcpUser(ctx.callerContext)
const appId = crypto.randomUUID()
const now = new Date().toISOString()
const isUpdate = args.app_id !== undefined
const appId = args.app_id ?? crypto.randomUUID()
const parameters = normalizeUiArtifactParameters(args.parameters)
await insertUiArtifact(ctx.env.APP_DB, {
id: appId,
user_id: user.userId,
title: args.title,
description: args.description,
code: args.code,
runtime: args.runtime,
parameters: parameters ? JSON.stringify(parameters) : null,
created_at: now,
updated_at: now,
})
const serializedParameters = parameters ? JSON.stringify(parameters) : null

if (isUpdate) {
const updated = await updateUiArtifact(ctx.env.APP_DB, user.userId, appId, {
title: args.title,
description: args.description,
code: args.code,
runtime: args.runtime,
parameters: serializedParameters,
})
if (!updated) {
throw new Error('Saved UI artifact not found for this user.')
}
} else {
const now = new Date().toISOString()
await insertUiArtifact(ctx.env.APP_DB, {
id: appId,
user_id: user.userId,
title: args.title,
description: args.description,
code: args.code,
runtime: args.runtime,
parameters: serializedParameters,
created_at: now,
updated_at: now,
})
}

try {
await upsertUiArtifactVector(ctx.env, {
Expand All @@ -87,8 +114,12 @@ export const uiSaveAppCapability = defineDomainCapability(
}),
})
} catch (cause) {
await deleteUiArtifact(ctx.env.APP_DB, user.userId, appId)
throw cause
if (!isUpdate) {
await deleteUiArtifact(ctx.env.APP_DB, user.userId, appId)
throw cause
}

// A vector refresh should not fail an in-place app update.
}

return {
Expand Down
147 changes: 0 additions & 147 deletions packages/worker/src/mcp/capabilities/apps/ui-update-app.ts

This file was deleted.

26 changes: 23 additions & 3 deletions packages/worker/src/mcp/mcp-server.mcp-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,6 @@ test('mcp server searches capabilities', async () => {
'code',
])
expect(saveApp?.readOnly).toBeUndefined()
expect(saveApp?.inputFields).toBeUndefined()

const textOutput =
(result as CallToolResult).content.find(
Expand Down Expand Up @@ -1840,7 +1839,7 @@ test('mcp server deletes saved ui app artifacts', async () => {
expect(apps?.some((app) => app.app_id === appId)).toBe(false)
})

test('mcp server updates saved ui app artifacts', async () => {
test('mcp server upserts saved ui app artifacts through ui_save_app', async () => {
await using database = await createTestDatabase()
await using server = await startDevServer(database.persistDir)
await using mcpClient = await createMcpClient(server.origin, database.user)
Expand Down Expand Up @@ -1869,7 +1868,7 @@ test('mcp server updates saved ui app artifacts', async () => {
name: 'execute',
arguments: {
code: `async () =>
await codemode.ui_update_app({
await codemode.ui_save_app({
app_id: ${JSON.stringify(appId)},
title: 'Updated App',
description: 'Updated description.',
Expand Down Expand Up @@ -1909,4 +1908,25 @@ test('mcp server updates saved ui app artifacts', async () => {
expect(getPayload?.description).toBe('Updated description.')
expect(getPayload?.code).toBe('<main><h1>Updated</h1></main>')
expect(getPayload?.runtime).toBe('javascript')

const listResult = await mcpClient.client.callTool({
name: 'execute',
arguments: {
code: `async () => await codemode.ui_list_apps({})`,
},
})
const listStructured = (listResult as CallToolResult).structuredContent as
| {
result?: Record<string, unknown>
}
| undefined
const listPayload = listStructured?.result as
| Record<string, unknown>
| undefined
const apps = listPayload?.apps as
| Array<{ app_id?: string; title?: string }>
| undefined
expect(apps).toHaveLength(1)
expect(apps?.[0]?.app_id).toBe(appId)
expect(apps?.[0]?.title).toBe('Updated App')
})
Loading