Skip to content

🎥 Make videos programmatically with React

Add WebMCP selection tool to Studio

Jonny Burger2f4b806e3751
diff --git a/packages/example/e2e/studio.test.mts b/packages/example/e2e/studio.test.mts
index 03ff1aaae6..10c02f9a0c 100644
--- a/packages/example/e2e/studio.test.mts
+++ b/packages/example/e2e/studio.test.mts
@@ -1832,6 +1832,31 @@ test.describe('visual mode', () => {
await context.grantPermissions(['clipboard-read', 'clipboard-write'], {
origin: STUDIO_URL,
});
+ await page.addInitScript(() => {
+ type RegisteredTool = {
+ readonly name: string;
+ readonly execute: () => Promise<unknown>;
+ };
+ const tools = new Map<string, RegisteredTool>();
+ Object.defineProperty(window, '__remotion_webmcp_tools', {
+ value: tools,
+ });
+ Object.defineProperty(document, 'modelContext', {
+ value: {
+ registerTool: async (
+ tool: RegisteredTool,
+ options: {readonly signal: AbortSignal},
+ ) => {
+ tools.set(tool.name, tool);
+ options.signal.addEventListener('abort', () => {
+ if (tools.get(tool.name) === tool) {
+ tools.delete(tool.name);
+ }
+ });
+ },
+ },
+ });
+ });
await page.route('**/api/default-editor-info', async (route) => {
await route.fulfill({
json: {
@@ -2015,6 +2040,45 @@ test.describe('visual mode', () => {
await expect
.poll(() => page.evaluate(() => navigator.clipboard.readText()))
.toBe(contextForAgents);
+ await page.getByRole('button', {name: 'Jump to beginning'}).click();
+ for (let i = 0; i < 3; i++) {
+ await page
+ .getByRole('button', {name: 'Step forward one frame'})
+ .click();
+ }
+ await expect
+ .poll(() =>
+ page.evaluate(() => {
+ const tools = (
+ window as typeof window & {
+ readonly __remotion_webmcp_tools: Map<string, unknown>;
+ }
+ ).__remotion_webmcp_tools;
+ return tools.has('get_selection');
+ }),
+ )
+ .toBe(true);
+ const webMcpSelection = await page.evaluate(async () => {
+ const tools = (
+ window as typeof window & {
+ readonly __remotion_webmcp_tools: Map<
+ string,
+ {readonly execute: () => Promise<unknown>}
+ >;
+ }
+ ).__remotion_webmcp_tools;
+ const tool = tools.get('get_selection');
+ if (!tool) {
+ throw new Error('get_selection was not registered');
+ }
+
+ return tool.execute();
+ });
+ expect(webMcpSelection).toEqual({
+ currentFrame: 3,
+ currentSelection: contextForAgents,
+ currentComposition: 'AnimatedBarChart',
+ });
fs.writeFileSync(
configFile,
diff --git a/packages/studio/src/components/EditorContent.tsx b/packages/studio/src/components/EditorContent.tsx
index a125cc79af..06e2540c33 100644
--- a/packages/studio/src/components/EditorContent.tsx
+++ b/packages/studio/src/components/EditorContent.tsx
@@ -15,6 +15,7 @@ import {
TimelineSelectionProvider,
useTimelineSelection,
} from './Timeline/TimelineSelection';
+import {WebMcp} from './WebMcp';
const noop = () => undefined;
@@ -86,6 +87,7 @@ export const EditorContent: React.FC<{
return (
<TimelineSelectionProvider>
+ <WebMcp />
<Transform3DModeStateProvider>
<StudioClearSelectionArea>
<InitialCompositionLoader />
diff --git a/packages/studio/src/components/WebMcp.tsx b/packages/studio/src/components/WebMcp.tsx
new file mode 100644
index 0000000000..f7168301d9
--- /dev/null
+++ b/packages/studio/src/components/WebMcp.tsx
@@ -0,0 +1,155 @@
+import {useContext, useEffect, useMemo, useRef, type FC} from 'react';
+import {Internals} from 'remotion';
+import {formatContextForAgents} from '../helpers/format-file-location';
+import {findTrackForNodePathInfo} from './Timeline/find-track-for-node-path-info';
+import {getCurrentFrame} from './Timeline/imperative-state';
+import {parseKeyframeFieldFromNodePath} from './Timeline/parse-keyframe-field-from-node-path';
+import {useTimelineSelection} from './Timeline/TimelineSelection';
+import {useResolveStackAndReactToChange} from './Timeline/use-resolved-stack-react-to-change';
+
+type WebMcpTool = {
+ readonly name: string;
+ readonly title: string;
+ readonly description: string;
+ readonly inputSchema: Record<string, unknown>;
+ readonly annotations: {readonly readOnlyHint: boolean};
+ readonly execute: () => Promise<unknown>;
+};
+
+type WebMcpModelContext = {
+ registerTool: (
+ tool: WebMcpTool,
+ options: {readonly signal: AbortSignal},
+ ) => Promise<void>;
+};
+
+const getNoStack = () => null;
+
+export const WebMcp: FC = () => {
+ const {selectedItems} = useTimelineSelection();
+ const {canvasContent} = useContext(Internals.CompositionManager);
+ const {sequences} = useContext(Internals.SequenceManager);
+ const {overrideIdToNodePathMappings} = useContext(
+ Internals.OverrideIdsToNodePathsGettersContext,
+ );
+ const selectedItem = selectedItems.length === 1 ? selectedItems[0] : null;
+ const track = useMemo(() => {
+ if (selectedItem === null || selectedItem.type === 'guide') {
+ return null;
+ }
+
+ return (
+ findTrackForNodePathInfo({
+ sequences,
+ overrideIdsToNodePaths: overrideIdToNodePathMappings,
+ nodePathInfo: selectedItem.nodePathInfo,
+ }) ?? null
+ );
+ }, [overrideIdToNodePathMappings, selectedItem, sequences]);
+ const {resolvedLocation, stack} = useResolveStackAndReactToChange(
+ track?.sequence.getStack ?? getNoStack,
+ track?.sequence.controls?.overrideId ??
+ track?.sequence.id ??
+ 'no-selection',
+ );
+ const currentStack = track?.sequence.getStack() ?? null;
+ const currentResolvedLocation =
+ stack === currentStack ? resolvedLocation : null;
+ const currentSelection = useMemo(() => {
+ if (selectedItems.length === 0) {
+ return null;
+ }
+
+ if (selectedItems.length > 1) {
+ return null;
+ }
+
+ if (selectedItem === null || selectedItem.type === 'guide') {
+ return null;
+ }
+
+ if (track === null) {
+ return null;
+ }
+
+ let name: string | null;
+ if (selectedItem.type === 'sequence') {
+ name =
+ track.sequence.displayName ||
+ track.sequence.controls?.componentName ||
+ null;
+ } else if (selectedItem.type === 'sequence-prop') {
+ name = `Property "${selectedItem.key}"`;
+ } else if (selectedItem.type === 'sequence-all-effects') {
+ name = 'Effects';
+ } else if (selectedItem.type === 'sequence-effect') {
+ const {effects} = track.sequence;
+ const effect = effects[selectedItem.i];
+ name = effect ? `Effect "${effect.label}"` : null;
+ } else if (selectedItem.type === 'sequence-effect-prop') {
+ name = `Effect property "${selectedItem.key}"`;
+ } else {
+ const field = parseKeyframeFieldFromNodePath(
+ selectedItem.nodePathInfo.auxiliaryKeys,
+ );
+ name =
+ field?.type === 'sequence'
+ ? `Property "${field.fieldKey}"`
+ : field?.type === 'effect'
+ ? `Effect property "${field.fieldKey}"`
+ : null;
+ }
+
+ return formatContextForAgents({
+ location: currentResolvedLocation,
+ name,
+ root: window.remotion_cwd,
+ });
+ }, [currentResolvedLocation, selectedItem, selectedItems.length, track]);
+ const currentSelectionRef = useRef(currentSelection);
+ currentSelectionRef.current = currentSelection;
+ const currentComposition =
+ canvasContent?.type === 'composition' ? canvasContent.compositionId : null;
+ const currentCompositionRef = useRef(currentComposition);
+ currentCompositionRef.current = currentComposition;
+
+ useEffect(() => {
+ const {modelContext} = document as Document & {
+ readonly modelContext?: WebMcpModelContext;
+ };
+ if (typeof modelContext?.registerTool !== 'function') {
+ return;
+ }
+
+ const controller = new AbortController();
+ modelContext
+ .registerTool(
+ {
+ name: 'get_selection',
+ title: 'Get Studio selection',
+ description:
+ 'Read the current frame, composition, and source-code context for the item currently selected in Remotion Studio. The selection matches "Copy context for agents".',
+ inputSchema: {
+ type: 'object',
+ properties: {},
+ additionalProperties: false,
+ },
+ annotations: {readOnlyHint: true},
+ execute: () =>
+ Promise.resolve({
+ currentFrame: getCurrentFrame(),
+ currentSelection: currentSelectionRef.current,
+ currentComposition: currentCompositionRef.current,
+ }),
+ },
+ {signal: controller.signal},
+ )
+ .catch(() => undefined);
+
+ return () => {
+ controller.abort();
+ };
+ }, []);
+
+ return null;
+};