Skip to content

Add Frontend Event Listeners for MCP Real-Time UI Updates #326

Closed

Description

@malibio

Overview

The MCP server emits Tauri events when operations occur (node-created, node-updated, node-deleted), but the frontend doesn't listen to these events. This means when Claude Code creates or modifies nodes via MCP, the UI doesn't update in real-time - users must refresh the page to see changes.

Add event listeners in the Svelte frontend and modify the Rust event payloads to include full node data for instant UI updates.

Problem Statement

Current State:

  • MCP handlers emit lightweight events with only node_id and node_type
  • No frontend listeners exist for these events
  • UI doesn't update when MCP operations occur
  • Users must manually refresh to see Claude Code changes

Desired State:

  • Events include full node data (no extra DB fetch needed)
  • Frontend listens to events and updates sharedNodeStore
  • UI updates instantly when Claude Code creates/modifies nodes
  • Real-time synchronization between MCP and GUI

Architecture Decision

Emit Full Node Data in Events (Not just IDs)

Why:

  • MCP handlers already have full node data in result
  • Avoids extra database round-trip on every operation
  • Zero latency - instant UI updates
  • Guaranteed consistency (exact data MCP created)

Implementation Tasks

Phase 1: Update Rust Event Payloads

File: packages/desktop-app/src-tauri/src/mcp_integration.rs

  • Change NodeCreatedEvent struct to include full Node data
  • Change NodeUpdatedEvent struct to include full Node data
  • Keep NodeDeletedEvent as-is (only needs node_id)
  • Update emit_event_for_method() to extract full node from result

Current Structure:

struct NodeCreatedEvent {
    node_id: String,
    node_type: String,
}

New Structure:

use nodespace_core::Node;

struct NodeCreatedEvent {
    node: Node,
}

struct NodeUpdatedEvent {
    node: Node,
}

struct NodeDeletedEvent {
    node_id: String,
}

Note: For update_node, the MCP result may only contain node_id. Choose approach:

  • Option A: Frontend fetches full node on update events (hybrid)
  • Option B: Pass NodeService to emit_event_for_method() and fetch before emitting

Phase 2: Add Frontend Event Listeners

File: packages/desktop-app/src/lib/components/layout/app-shell.svelte

  • Import necessary dependencies (listen, sharedNodeStore)
  • Add event listeners in onMount
  • Handle node-created event
  • Handle node-updated event
  • Handle node-deleted event
  • Add cleanup on unmount

Implementation Pattern:

import { listen } from '@tauri-apps/api/event';
import { sharedNodeStore } from '$lib/services/shared-node-store';
import { invoke } from '@tauri-apps/api/tauri';
import type { Node } from '$lib/types';

onMount(() => {
  // Listen for MCP node creation
  const unlistenCreated = listen<{ node: Node }>('node-created', (event) => {
    console.log('[MCP] Node created:', event.payload.node.id);
    sharedNodeStore.addNode(event.payload.node);
  });

  // Listen for MCP node updates (hybrid - fetch full node)
  const unlistenUpdated = listen<{ node_id: string }>('node-updated', async (event) => {
    console.log('[MCP] Node updated:', event.payload.node_id);
    const node = await invoke<Node>('get_node', { id: event.payload.node_id });
    if (node) {
      sharedNodeStore.updateNodeData(node.id, node);
    }
  });

  // Listen for MCP node deletion
  const unlistenDeleted = listen<{ node_id: string }>('node-deleted', (event) => {
    console.log('[MCP] Node deleted:', event.payload.node_id);
    sharedNodeStore.deleteNode(event.payload.node_id);
  });

  return () => {
    unlistenCreated.then(fn => fn());
    unlistenUpdated.then(fn => fn());
    unlistenDeleted.then(fn => fn());
  };
});

Phase 3: Verify SharedNodeStore Methods

File: packages/desktop-app/src/lib/services/shared-node-store.ts

  • Verify addNode(node: Node) method exists
  • Verify updateNodeData(nodeId: string, node: Node) method exists
  • Verify deleteNode(nodeId: string) method exists
  • Add methods if missing

Phase 4: Testing

End-to-End Testing with Claude Code

  • Complete Add HTTP Transport to MCP Server for Claude Code Integration #323 (HTTP transport)
  • Launch NodeSpace GUI
  • Connect Claude Code via MCP bridge
  • Have Claude Code create a node
  • Verify node appears instantly in GUI
  • Have Claude Code update a node
  • Verify update appears instantly
  • Have Claude Code delete a node
  • Verify deletion appears instantly

Phase 5: Error Handling

  • Handle case where node already exists (create event)
  • Handle case where node doesn't exist (update event)
  • Handle case where node already deleted (delete event)
  • Add error logging for failures

Acceptance Criteria

Primary Success Criterion

When Claude Code creates/updates/deletes a node via MCP, the NodeSpace GUI updates instantly without manual refresh

Functional Requirements

  • node-created events include full Node data
  • node-updated events trigger frontend to fetch full node
  • node-deleted events include node_id
  • Frontend listens to all three event types
  • SharedNodeStore updates when events received
  • UI re-renders automatically
  • Works for single and batch operations
  • No manual page refresh needed

Technical Requirements

  • Event payload structures updated in Rust
  • Event listeners added to app-shell.svelte
  • SharedNodeStore methods work correctly
  • No memory leaks (listeners cleaned up)
  • Error handling for malformed events
  • Type-safe event payloads

Testing Requirements

  • End-to-end testing with Claude Code
  • Verify no duplicate nodes on rapid events
  • Verify UI state consistency

Technical Specifications

Reference Files

  • Rust Event Emission: packages/desktop-app/src-tauri/src/mcp_integration.rs
  • Frontend App Shell: packages/desktop-app/src/lib/components/layout/app-shell.svelte
  • Shared Store: packages/desktop-app/src/lib/services/shared-node-store.ts
  • Node Types: packages/desktop-app/src/lib/types/index.ts

Event Payload Types

interface NodeCreatedEvent {
  node: Node;
}

interface NodeUpdatedEvent {
  node_id: string;
}

interface NodeDeletedEvent {
  node_id: string;
}

Dependencies

Related Issues

Definition of Done

  • Event payloads include full node data (or trigger fetch)
  • Frontend listeners implemented
  • SharedNodeStore methods implemented
  • UI updates instantly when MCP operations occur
  • End-to-end tested with Claude Code
  • No console errors or memory leaks
  • Code review completed

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions