Touch Flow Base (MCP Counter POC)

Touch Flow Base (MCP Counter POC)

An MCP server that enables AI agents to execute sandboxed JavaScript code to control external web applications in real-time. It utilizes WebSockets to synchronize script execution with frontend animations, allowing complex UI interactions to be handled in a single agent turn.

Category
访问服务器

README

MCP Counter POC - Code Execution Architecture

Overview

This is a proof-of-concept (POC) demonstrating the "Code execution with MCP" pattern, which enables AI agents to execute JavaScript code on a server to control external applications. This architecture can be used as a foundation for building interactive, agent-controlled applications.

What Does This POC Do?

This POC implements a Model Context Protocol (MCP) server that:

  1. Exposes a run_script tool to AI agents (like Claude via Gemini CLI)
  2. Executes JavaScript code in a sandboxed environment
  3. Controls a web-based counter application via WebSocket
  4. Synchronizes execution with frontend animations

MCP Counter POC - Code Execution Architecture

Overview

This is a proof-of-concept (POC) demonstrating the "Code execution with MCP" pattern, which enables AI agents to execute JavaScript code on a server to control external applications. This architecture can be used as a foundation for building interactive, agent-controlled applications.

What Does This POC Do?

This POC implements a Model Context Protocol (MCP) server that:

  1. Exposes a run_script tool to AI agents (like Claude via Gemini CLI)
  2. Executes JavaScript code in a sandboxed environment
  3. Controls a web-based counter application via WebSocket
  4. Synchronizes execution with frontend animations

Key Capability: Agent-Controlled UI

An AI agent can write JavaScript code that directly controls the counter displayed in a web browser, with execution paused until animations complete to ensure smooth user experience.

Architecture

graph TD
    User[User Mobile/Desktop] -->|HTTPS| Vercel[Vercel Frontend]
    Vercel -->|WebSocket| Tunnel[Localtunnel URL]
    Tunnel -->|Tunnel| LocalServer[Local Server :3000]
    LocalServer -->|MCP| Agent[AI Agent (Gemini)]
    LocalServer -->|Sandbox| VM[VM Context]

Components

  1. server.js - MCP server with:

    • Express web server (port 3000)
    • WebSocket server for real-time communication
    • MCP server exposing run_script tool
    • VM sandbox for secure code execution
  2. index.html - Frontend (Deployed on Vercel) with:

    • Real-time counter display
    • WebSocket client connecting to Localtunnel URL
    • Animation system (3-second delay per update)
    • Pause/Resume controls
  3. launcher.js - Launcher for MCP integration

    • Spawns server.js with correct environment
    • Captures error logs for debugging

The "Code Execution with MCP" Pattern

Traditional Approach (Inefficient)

Agent calls: update_count(1)  → Server updates → Frontend shows 1
Agent waits...
Agent calls: update_count(2)  → Server updates → Frontend shows 2
Agent waits...
Agent calls: update_count(3)  → Server updates → Frontend shows 3

Problem: Multiple round-trips to the agent for simple loops.

Code Execution Approach (Efficient)

Agent calls: run_script({
  code: `
    for (let i = 1; i <= 3; i++) {
      await update_count(i);
    }
  `
})

The server executes the entire loop, pausing at each update_count() call until the frontend animation completes.

Benefit: Complex logic runs in a single agent turn, reducing token usage and latency.

How It Works

  1. Agent Generates Code

    • Claude (or another LLM) writes JavaScript to accomplish a task
    • Sends code via the run_script MCP tool
  2. Server Executes in Sandbox

    • Code runs in a VM context with access to:
      • update_count(n): Updates counter and pauses until frontend confirms
      • console.log(): Captures logs returned to agent
      • setTimeout, Promise: For delays and async operations
  3. WebSocket Synchronization

    • Server broadcasts count updates to frontend
    • Frontend displays animation (3-second delay)
    • Frontend sends turn_complete signal when ready
    • Server resumes script execution
  4. Agent Receives Result

    • Script logs and execution status returned to agent
    • Agent can continue with next actions

Setup & Usage

1. Installation

npm install

2. Backend & MCP Setup

  1. Start the local backend:
    node server.js
    
  2. Configure Gemini (.gemini/settings.json):
    {
      "mcpServers": {
        "pm-jarvis": {
          "command": "node",
          "args": ["C:\\path\\to\\project\\launcher.js"]
        }
      }
    }
    

3. Mobile Access (Localtunnel)

To access the app from a mobile device, expose your local backend:

npx localtunnel --port 3000
  • Copy the URL (e.g., https://example.loca.lt).
  • Important: Visit this URL in your browser first and enter your IP as the password.

4. Frontend (Vercel)

  1. Deploy to Vercel:
    vercel deploy --prod
    
  2. Open the Vercel URL on your device.
  3. Go to Settings and enter your Localtunnel URL.

Use Cases as Base Architecture

This POC can be adapted for:

1. Agent-Controlled Dashboards

  • Replace counter with charts/graphs
  • Agent updates visualizations based on data analysis

2. Interactive Forms & Workflows

  • Agent fills forms step-by-step
  • Validates inputs and handles errors

3. Real-time Notifications

  • Agent processes events and updates UI
  • Users see live status without polling

4. Game Controllers

  • Agent plays games by executing move sequences
  • UI updates reflect game state

5. IoT Device Control

  • Replace WebSocket frontend with IoT devices
  • Agent sends control sequences to hardware

License

MIT

References

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选