Repository navigation
Add ui_update_app capability #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
packages/worker/src/mcp/capabilities/apps/ui-update-app.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| import { z } from 'zod' | ||
| 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 { requireMcpUser } from '#mcp/capabilities/meta/require-user.ts' | ||
| import { buildUiArtifactEmbedText } from '#mcp/ui-artifacts-embed.ts' | ||
| import { getUiArtifactById, updateUiArtifact } from '#mcp/ui-artifacts-repo.ts' | ||
| import { upsertUiArtifactVector } from '#mcp/ui-artifacts-vectorize.ts' | ||
|
|
||
| function parseStringArray(raw: string): Array<string> { | ||
| try { | ||
| const value = JSON.parse(raw) as unknown | ||
| if (!Array.isArray(value)) return [] | ||
| return value.filter((entry): entry is string => typeof entry === 'string') | ||
| } catch { | ||
| return [] | ||
| } | ||
| } | ||
|
|
||
| const inputSchema = z | ||
| .object({ | ||
| app_id: z | ||
| .string() | ||
| .min(1) | ||
| .describe('Saved UI artifact id returned by ui_save_app.'), | ||
| title: z | ||
| .string() | ||
| .min(1) | ||
| .optional() | ||
| .describe('Short title for the saved UI artifact.'), | ||
| description: z | ||
| .string() | ||
| .min(1) | ||
| .optional() | ||
| .describe('What the saved app does and when it is useful.'), | ||
| keywords: z | ||
| .array(z.string()) | ||
| .optional() | ||
| .describe('Extra search keywords for discovery in the search tool.'), | ||
| code: z | ||
| .string() | ||
| .min(1) | ||
| .optional() | ||
| .describe( | ||
| 'App source for the generic MCP UI shell. Prefer a self-contained HTML document or fragment so the generated app owns the visible UI. Legacy `javascript` source is still supported for previously saved apps.', | ||
| ), | ||
| runtime: z | ||
| .enum(['html', 'javascript']) | ||
| .optional() | ||
| .describe( | ||
| 'Source format accepted by the generic UI shell. Prefer `html`; `javascript` is kept for legacy saved apps.', | ||
| ), | ||
| search_text: z | ||
| .string() | ||
| .optional() | ||
| .describe( | ||
|
Comment on lines
+48
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Allow explicit Line 54 currently accepts only Suggested patch- search_text: z
- .string()
- .optional()
+ search_text: z
+ .string()
+ .nullable()
+ .optional()
.describe(
'Optional retrieval-only text that improves search recall without being part of the visible app description.',
),Also applies to: 111-113 🤖 Prompt for AI Agents |
||
| 'Optional retrieval-only text that improves search recall without being part of the visible app description.', | ||
| ), | ||
| }) | ||
| .refine( | ||
| (value) => | ||
| value.title !== undefined || | ||
| value.description !== undefined || | ||
| value.keywords !== undefined || | ||
| value.code !== undefined || | ||
| value.runtime !== undefined || | ||
| value.search_text !== undefined, | ||
| { | ||
| message: 'Provide at least one field to update.', | ||
| }, | ||
| ) | ||
|
|
||
| const outputSchema = z.object({ | ||
| app_id: z.string(), | ||
| runtime: z.enum(['html', 'javascript']), | ||
| hosted_url: z.string().url(), | ||
| }) | ||
|
|
||
| export const uiUpdateAppCapability = defineDomainCapability( | ||
| capabilityDomainNames.apps, | ||
| { | ||
| name: 'ui_update_app', | ||
| description: | ||
| 'Update an existing saved UI artifact owned by the signed-in user. Only the fields provided will be changed.', | ||
| keywords: ['ui', 'app', 'artifact', 'update', 'edit', 'modify'], | ||
| readOnly: false, | ||
| idempotent: false, | ||
| destructive: false, | ||
| inputSchema, | ||
| outputSchema, | ||
| async handler(args, ctx: CapabilityContext) { | ||
| const user = requireMcpUser(ctx.callerContext) | ||
| const updates: Parameters<typeof updateUiArtifact>[3] = {} | ||
|
|
||
| if (args.title !== undefined) { | ||
| updates.title = args.title | ||
| } | ||
| if (args.description !== undefined) { | ||
| updates.description = args.description | ||
| } | ||
| if (args.keywords !== undefined) { | ||
| updates.keywords = JSON.stringify(args.keywords) | ||
| } | ||
| if (args.code !== undefined) { | ||
| updates.code = args.code | ||
| } | ||
| if (args.runtime !== undefined) { | ||
| updates.runtime = args.runtime | ||
| } | ||
| if (args.search_text !== undefined) { | ||
| updates.search_text = args.search_text | ||
| } | ||
|
|
||
| const updated = await updateUiArtifact( | ||
| ctx.env.APP_DB, | ||
| user.userId, | ||
| args.app_id, | ||
| updates, | ||
| ) | ||
| if (!updated) { | ||
| throw new Error('Saved UI artifact not found for this user.') | ||
| } | ||
|
|
||
| const refreshed = await getUiArtifactById( | ||
| ctx.env.APP_DB, | ||
| user.userId, | ||
| args.app_id, | ||
| ) | ||
| if (!refreshed) { | ||
| throw new Error('Saved UI artifact not found after update.') | ||
| } | ||
|
|
||
| await upsertUiArtifactVector(ctx.env, { | ||
| appId: refreshed.id, | ||
| userId: user.userId, | ||
| embedText: buildUiArtifactEmbedText({ | ||
| title: refreshed.title, | ||
| description: refreshed.description, | ||
| keywords: parseStringArray(refreshed.keywords), | ||
| searchText: refreshed.search_text, | ||
| runtime: refreshed.runtime, | ||
| }), | ||
| }) | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| return { | ||
| app_id: refreshed.id, | ||
| runtime: refreshed.runtime, | ||
| hosted_url: buildSavedUiUrl(ctx.callerContext.baseUrl, refreshed.id), | ||
| } | ||
| }, | ||
| }, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.