Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat: enable MCP on editor startup page

esengineb9f2a0c87024
diff --git a/desktop/src-tauri/src/lib.rs b/desktop/src-tauri/src/lib.rs
index 11f06e6a7..447cb040a 100644
--- a/desktop/src-tauri/src/lib.rs
+++ b/desktop/src-tauri/src/lib.rs
@@ -88,7 +88,11 @@ fn start_bridge_server(
project_path: Option<String>,
) -> Result<u16, String> {
let mut bridge = state.bridge_server.lock().unwrap();
- bridge.start(app, project_path)
+ let port = bridge.start(app, project_path.clone())?;
+ if let Some(ref path) = project_path {
+ bridge.update_project_path(path);
+ }
+ Ok(port)
}
#[tauri::command]
@@ -326,6 +330,14 @@ pub fn run() {
compiler::compile_wasm,
compiler::clear_build_cache,
])
+ .setup(|app| {
+ let state = app.state::<AppState>();
+ let mut bridge = state.bridge_server.lock().unwrap();
+ if let Err(e) = bridge.start(app.handle().clone(), None) {
+ eprintln!("[Setup] Failed to start bridge server: {}", e);
+ }
+ Ok(())
+ })
.on_window_event(|window, event| {
if let tauri::WindowEvent::Destroyed = event {
let app = window.app_handle();
diff --git a/desktop/src/main.ts b/desktop/src/main.ts
index 7bc299de7..87d371a56 100644
--- a/desktop/src/main.ts
+++ b/desktop/src/main.ts
@@ -5,7 +5,7 @@
import 'dockview-core/dist/styles/dockview.css';
import '@esengine/editor/styles';
-import { createEditor, ProjectLauncher, setPlatformAdapter, setEditorContext, loadProjectConfig, showToast, dismissToast, showProgressToast, updateToast, getSettingsValue, type Editor } from '@esengine/editor';
+import { createEditor, ProjectLauncher, LauncherBridge, setPlatformAdapter, setEditorContext, loadProjectConfig, showToast, dismissToast, showProgressToast, updateToast, getSettingsValue, type Editor } from '@esengine/editor';
import { TauriPlatformAdapter } from './TauriPlatformAdapter';
import { nativeFS, nativeShell } from './native-fs';
import { invoke } from '@tauri-apps/api/core';
@@ -14,6 +14,7 @@ import { relaunch } from '@tauri-apps/plugin-process';
import type { App, ESEngineModule } from 'esengine';
let currentLauncher: ProjectLauncher | null = null;
+let launcherBridge: LauncherBridge | null = null;
let wasmModule: ESEngineModule | null = null;
function loadESModule(url: string): Promise<any> {
@@ -61,13 +62,21 @@ async function loadWasmModule(): Promise<ESEngineModule | null> {
}
}
+function openProjectFromLauncher(container: HTMLElement, projectPath: string): void {
+ launcherBridge?.dispose();
+ launcherBridge = null;
+ currentLauncher?.dispose();
+ currentLauncher = null;
+ openEditor(container, projectPath);
+}
+
async function showLauncher(container: HTMLElement): Promise<void> {
+ launcherBridge = new LauncherBridge({
+ onOpenProject: (path) => openProjectFromLauncher(container, path),
+ });
+
currentLauncher = new ProjectLauncher(container, {
- onProjectOpen: (projectPath) => {
- currentLauncher?.dispose();
- currentLauncher = null;
- openEditor(container, projectPath);
- },
+ onProjectOpen: (projectPath) => openProjectFromLauncher(container, projectPath),
});
}
diff --git a/editor/src/bridge/LauncherBridge.ts b/editor/src/bridge/LauncherBridge.ts
new file mode 100644
index 000000000..bf78f8946
--- /dev/null
+++ b/editor/src/bridge/LauncherBridge.ts
@@ -0,0 +1,91 @@
+import { listen, emit, type UnlistenFn } from '@tauri-apps/api/event';
+import { getRecentProjects } from '../launcher/ProjectService';
+import { EXAMPLE_PROJECTS } from '../types/ProjectTypes';
+
+interface McpRequest {
+ id: string;
+ method: string;
+ params: Record<string, unknown>;
+}
+
+export interface LauncherBridgeCallbacks {
+ onOpenProject: (projectPath: string) => void;
+}
+
+export class LauncherBridge {
+ private unlisten_: UnlistenFn | null = null;
+ private callbacks_: LauncherBridgeCallbacks;
+
+ constructor(callbacks: LauncherBridgeCallbacks) {
+ this.callbacks_ = callbacks;
+ this.setup_();
+ }
+
+ dispose(): void {
+ if (this.unlisten_) {
+ this.unlisten_();
+ this.unlisten_ = null;
+ }
+ }
+
+ private async setup_(): Promise<void> {
+ this.unlisten_ = await listen<McpRequest>('mcp-request', (event) => {
+ this.handleRequest_(event.payload);
+ });
+ }
+
+ private async handleRequest_(req: McpRequest): Promise<void> {
+ const { id, method, params } = req;
+ try {
+ const result = await this.dispatch_(method, params);
+ await emit(`mcp-response-${id}`, { ok: true, data: result });
+ } catch (e) {
+ const msg = e instanceof Error ? e.message : String(e);
+ await emit(`mcp-response-${id}`, { ok: false, error: msg });
+ }
+ }
+
+ private async dispatch_(method: string, params: Record<string, unknown>): Promise<unknown> {
+ switch (method) {
+ case 'getEditorStatus':
+ return { mode: 'launcher' };
+
+ case 'listRecentProjects':
+ return getRecentProjects();
+
+ case 'listExamples':
+ return EXAMPLE_PROJECTS.map(e => ({
+ id: e.id,
+ name: e.name,
+ description: e.description,
+ category: e.category,
+ }));
+
+ case 'openProject': {
+ const path = params.path as string;
+ if (!path) throw new Error('Missing "path" parameter');
+ this.callbacks_.onOpenProject(path);
+ return { success: true };
+ }
+
+ case 'createFromExample': {
+ const { createFromExample } = await import('../launcher/ProjectService');
+ const exampleId = params.example as string;
+ const name = params.name as string;
+ const location = params.location as string;
+ if (!exampleId || !name || !location) {
+ throw new Error('Missing required parameters: example, name, location');
+ }
+ const example = EXAMPLE_PROJECTS.find(e => e.id === exampleId);
+ if (!example) throw new Error(`Unknown example: ${exampleId}`);
+ const result = await createFromExample({ name, location, example });
+ if (!result.success) throw new Error(result.error);
+ this.callbacks_.onOpenProject(result.data!);
+ return { success: true, projectPath: result.data };
+ }
+
+ default:
+ throw new Error(`Unknown method in launcher mode: ${method}. Open a project first to access editor tools.`);
+ }
+ }
+}
diff --git a/editor/src/bridge/McpBridge.ts b/editor/src/bridge/McpBridge.ts
index f0f62d1d8..5945c9bd0 100644
--- a/editor/src/bridge/McpBridge.ts
+++ b/editor/src/bridge/McpBridge.ts
@@ -105,6 +105,7 @@ export class McpBridge {
private async dispatch_(method: string, params: Record<string, unknown>): Promise<unknown> {
switch (method) {
+ case 'getEditorStatus': return { mode: 'editor', projectPath: this.projectPath_ };
case 'getSceneTree': return this.getSceneTree_(params.depth as number | undefined);
case 'getEntityData': return this.getEntityData_(params);
case 'getSelection': return this.getSelection_();
diff --git a/editor/src/bridge/index.ts b/editor/src/bridge/index.ts
index 5122c593d..b0e0e8ea6 100644
--- a/editor/src/bridge/index.ts
+++ b/editor/src/bridge/index.ts
@@ -5,4 +5,5 @@
export { EditorBridge } from './EditorBridge';
export { McpBridge } from './McpBridge';
+export { LauncherBridge, type LauncherBridgeCallbacks } from './LauncherBridge';
export { RingBuffer } from './RingBuffer';
diff --git a/editor/src/index.ts b/editor/src/index.ts
index 4de7042a2..1b2792de3 100644
--- a/editor/src/index.ts
+++ b/editor/src/index.ts
@@ -252,7 +252,7 @@ export { ContentBrowserPanel } from './panels/ContentBrowserPanel';
// Bridge
// =============================================================================
-export { EditorBridge } from './bridge';
+export { EditorBridge, LauncherBridge, type LauncherBridgeCallbacks } from './bridge';
// =============================================================================
// IO