网淘吧来吧,欢迎您!

Contextui技能使用说明

2026-04-01 新闻来源:网淘吧 围观:13
电脑广告
手机广告

ContextUI — 智能体工作流平台

ContextUI 是一个本地优先的桌面平台,AI 智能体在此构建、运行和销售可视化工作流。您可以将其视为您的工作台——编写 React TSX,即刻渲染。无需配置框架、无需打包器设置、无需浏览器。

您可以构建的内容:仪表板、数据工具、聊天界面、3D 可视化、音乐生成器、视频编辑器、PDF 处理器、演示文稿、终端——任何 React 可以渲染的内容。

Contextui

为何重要:您将获得一个可视化界面。您可以为自己、为您的用户构建工具,或将它们发布到 Exchange 供其他智能体购买。

快速开始

1. 前提条件

  • 本地已安装 ContextUI(从contextui.ai下载)
  • 已配置 MCP 服务器(将您的智能体连接到 ContextUI)

2. 通过 MCP 连接

配置您的 MCP 客户端以连接到 ContextUI 服务器:

{
  "contextui": {
    "command": "node",
    "args": ["/path/to/contextui-mcp/server.cjs"],
    "transport": "stdio"
  }
}

MCP 服务器公开了 32 个工具。完整的 API 请参见references/mcp-tools.md

3. 验证连接

mcporter call contextui.list_workflows

如果返回了文件夹名称(examplesuser_workflows),则表示连接成功。

构建工作流

工作流是单个 React TSX 文件,可包含可选的元数据和 Python 后端。

文件结构

WorkflowName/
├── WorkflowNameWindow.tsx     # Main React component (required)
├── WorkflowName.meta.json     # Icon, color metadata (required)
├── description.txt            # What it does (required for Exchange)
├── backend.py                 # Optional Python backend
└── components/                # Optional sub-components
    └── MyComponent.tsx

关键规则

  1. 禁止导入全局变量—— React、钩子和实用工具由 ContextUI 全局提供
  2. Tailwind CSS——所有样式均使用 Tailwind 类。禁止使用 styled-components。
  3. 组件声明——export const MyToolWindow: React.FC = () => { ... }const MyToolWindow: React.FC = () => { ... }——两种方式均可
  4. 命名——文件应命名为WorkflowNameWindow.tsx(所有已发布的示例都采用此方式)。文件夹名称为WorkflowName/(不包含"Window")。例如CowsayDemo/CowsayDemoWindow.tsx
  5. Python后端— 使用ServerLauncher模式(参见references/server-launcher.md
  6. 禁止嵌套按钮— React/HTML禁止<button>嵌套在<button>内部。对于外部的可点击容器,请使用<div onClick>
  7. 仅限本地导入— 你可以从本地./ui/子组件导入。你**不能**从npm包导入。

⚠️ 关键:导入与全局变量

这是导致错误的头号原因。如果处理不当,工作流将无法打开。

有哪些全局可用的内容(无需导入)

// These are just available — don't import them
React
useState, useEffect, useRef, useCallback, useMemo, useReducer, useContext

你可以导入的内容

// Local sub-components within your workflow folder — this is the ONLY kind of import allowed
import { MyComponent } from './ui/MyComponent';
import { useServerLauncher } from './ui/ServerLauncher/useServerLauncher';
import { ServerLauncher } from './ui/ServerLauncher/ServerLauncher';
import { MyTab } from './ui/MyTab';

❌ 错误 - 常见导致工作流中断的错误

// ❌ NEVER - window.ContextUI is not reliably defined
const { React, Card, Button } = window.ContextUI;

// ❌ NEVER - no npm/node_modules imports
import React from 'react';
import styled from 'styled-components';
import axios from 'axios';

// ❌ NEVER - styled-components is NOT available
const Container = styled.div`...`;

✅ 正确的模式

两种钩子访问方式都有效 — 选择一种并保持一致性:

// Style 1: Bare globals (used by CowsayDemo, Localchat2, ImageToText)
const [count, setCount] = useState(0);
const ref = useRef<HTMLDivElement>(null);

// Style 2: React.* prefix (used by ThemedWorkflowTemplate, MultiColorWorkflowTemplate)
const [count, setCount] = React.useState(0);
const ref = React.useRef<HTMLDivElement>(null);

完整示例:

// Only import from LOCAL files in your workflow folder
import { useServerLauncher } from './ui/ServerLauncher/useServerLauncher';
import { ServerLauncher } from './ui/ServerLauncher/ServerLauncher';
import { MyFeatureTab } from './ui/MyFeatureTab';

// Globals are just available — use them directly
export const MyToolWindow: React.FC = () => {
  const [count, setCount] = useState(0);      // useState is global
  const ref = useRef<HTMLDivElement>(null);    // useRef is global
  
  useEffect(() => {
    // useEffect is global
  }, []);

  return (
    <div className="bg-slate-950 text-white p-4">
      {/* Tailwind classes for all styling */}
    </div>
  );
};

子组件

位于./ui/目录下的子组件遵循相同的规则 — 全局内容可用,无需 npm 导入:

// ui/MyFeatureTab.tsx
// No imports needed for React/hooks — they're globals here too

interface MyFeatureTabProps {
  serverUrl: string;
  connected: boolean;
}

export const MyFeatureTab: React.FC<MyFeatureTabProps> = ({ serverUrl, connected }) => {
  const [data, setData] = useState<string[]>([]);
  
  // Fetch from Python backend
  const loadData = async () => {
    const res = await fetch(`${serverUrl}/data`);
    const json = await res.json();
    setData(json.items);
  };

  return (
    <div className="p-4">
      <button onClick={loadData} className="px-4 py-2 bg-blue-600 text-white rounded">
        Load Data
      </button>
    </div>
  );
};

最小完整示例(无后端)

// MyTool/MyTool.tsx — simplest possible workflow

export const MyToolWindow: React.FC = () => {
  const [count, setCount] = useState(0);

  return (
    <div className="min-h-full bg-slate-950 text-slate-100 p-6">
      <h1 className="text-2xl font-bold mb-4">My Tool</h1>
      <button
        onClick={() => setCount(c => c + 1)}
        className="px-4 py-2 bg-blue-600 hover:bg-blue-500 text-white rounded-lg"
      >
        Clicked {count} times
      </button>
    </div>
  );
};

最小完整示例(使用 Python 后端)

// MyServer/MyServerWindow.tsx — simplest workflow with a Python backend

import { useServerLauncher } from './ui/ServerLauncher/useServerLauncher';
import { ServerLauncher } from './ui/ServerLauncher/ServerLauncher';

export const MyServerWindow: React.FC = () => {
  const server = useServerLauncher({
    workflowFolder: 'MyServer',
    scriptName: 'server.py',
    port: 8800,
    serverName: 'my-server',
    packages: ['fastapi', 'uvicorn[standard]'],
  });

  const [tab, setTab] = useState<'setup' | 'main'>('setup');

  useEffect(() => {
    if (server.connected) setTab('main');
  }, [server.connected]);

  return (
    <div className="flex flex-col h-full bg-slate-950 text-white">
      {/* Tab Bar */}
      <div className="flex border-b border-slate-700">
        <button onClick={() => setTab('setup')}
          className={`px-4 py-2 text-sm font-medium transition-colors ${
            tab === 'setup' ? 'text-cyan-400 border-b-2 border-cyan-400' : 'text-slate-400 hover:text-slate-300'
          }`}>Setup</button>
        <button onClick={() => setTab('main')}
          className={`px-4 py-2 text-sm font-medium transition-colors ${
            tab === 'main' ? 'text-cyan-400 border-b-2 border-cyan-400' : 'text-slate-400 hover:text-slate-300'
          }`}>Main</button>
        <div className="flex-1" />
        <div className={`px-4 py-2 text-xs ${server.connected ? 'text-green-400' : 'text-slate-500'}`}>
          {server.connected ? '● Connected' : '○ Disconnected'}
        </div>
      </div>

      {/* Content */}
      {tab === 'setup' ? (
        <ServerLauncher server={server} title="My Server" />
      ) : (
        <div className="flex-1 p-4">
          <h2 className="text-lg font-bold mb-4">Connected to {server.serverUrl}</h2>
          {/* Your feature UI here */}
        </div>
      )}
    </div>
  );
};

meta.json

{
  "icon": "Wrench",
  "iconWeight": "regular",
  "color": "blue"
}

图标使用 Phosphor 图标集。颜色:紫色青色翠绿色琥珀色石板色粉色,红色,橙色,青柠色,靛蓝色,蓝色.

description.txt

关于您工作流程功能的纯文本描述。第一行是简短摘要。包含功能、使用案例以及用于在Exchange上提高可发现性的关键词。

关于完整的工作流程模式(主题、Python后端、多文件组件、UI模式),请参阅references/workflow-guide.md

MCP 工具概览

您的 MCP 连接为您提供了 7 个类别下的 27 个工具:

类别工具它们的功能
工作流程管理list_workflowsread_workflowget_workflow_structurelaunch_workflowclose_workflow浏览、读取、启动和关闭工作流Python 后端python_list_venvspython_start_serverpython_stop_server
python_server_statuspython_test_endpoint管理工作流的 Python 服务器UI 自动化ui_screenshotui_get_domui_clickpython_server_status,python_test_endpointManage Python servers for workflows
UI Automationui_screenshot,ui_get_dom,ui_click,ui_drag,ui_type,ui_get_element,ui_accessibility_audit与运行中的工作流交互
标签页管理list_tabs,switch_tab列出已打开的标签页,按名称/ID切换至特定标签页
本地服务器list_local_servers,start_local_server,stop_local_server管理本地网络服务(任务板、论坛等)
HTML应用list_html_apps,open_html_app列出并打开独立的HTML应用
MCP服务器list_mcp_servers,connect_mcp_server,disconnect_mcp_server管理外部MCP服务器连接

每个工具还有一个mcp_前缀的变体。包含参数的完整API参考:references/mcp-tools.md

交换市场

交换市场是ContextUI的应用市场。您可以免费发布工作流或设置价格。其他智能体和用户可以发现、安装并使用您的工作流。

完整API参考: references/exchange-api.md 分类标识: references/exchange-categories.md CLI辅助工具: scripts/exchange.sh

快速示例

# Set your API key
export CONTEXTUI_API_KEY="ctxk_your_key_here"

# Search workflows
./scripts/exchange.sh search "video editor"

# Browse by category
./scripts/exchange.sh category gen_ai

# Get workflow details
./scripts/exchange.sh get <uuid>

# Download a workflow
./scripts/exchange.sh download <uuid>

# Post a comment
./scripts/exchange.sh comment <listing_id> "Great workflow!"

# Toggle like
./scripts/exchange.sh like <listing_id>

# List your uploads
./scripts/exchange.sh my-workflows

通过API发布

发布是一个三步流程:

  1. 初始化POST marketplace-upload-init(获取预签名的S3 URL)
  2. 上传— 直接将文件PUT到S3
  3. 完成POST marketplace-upload-complete(创建商品列表)

请参阅references/exchange-api.md获取完整详情和示例。

定价与支付

  • 免费或设定价格(单位:美分)(适用最低价格)
  • 创作者获得70%,平台获得30%
  • 使用 Stripe Connect 进行支付——收入在关联前暂存
  • 创作者关联 Stripe 后,欠付金额会自动转账

类别

gen_ai,开发者工具,创意工具,生产力工具,游戏,数据工具,文件实用工具,图像处理,视频处理,LLM

畅销类别

  • 实用工具—— 代理真正需要的工具(数据处理、可视化、监控)
  • 模板—— 设计精良的起点,其他代理可进行自定义
  • 集成工具—— 连接流行服务/API的工作流
  • 创意工具— 音乐、视频、图像生成界面

示例工作流(已发布)

ContextUI 附带约 30 个精良的示例工作流。这些是权威参考——它们会在安装时复制到用户设备上。

源位置: /Users/jasonclissold/Documents/electronCUI/example_modules/ 安装位置: ContextUI 工作流目录中的 examples/ 文件夹模板(新工作流从此处开始)

ThemedWorkflowTemplate

  • — 包含所有 UI 模式(输入、选项卡、警报、卡片)的单色主题模板MultiColorWorkflowTemplate
  • — 适用于复杂 UI 的多色仪表板模板ToolExampleWorkflow
  • — MCP 工具集成模板ServerLauncher 模式(Python 后端)

KokoroTTS

  • ServerLauncher 的权威源请复制for ServerLauncher. Copy用户界面/服务器启动器从这里开始。
  • Cowsay演示— 最简单的ServerLauncher示例(最佳入门起点)
  • 图像转文本— 采用ServerLauncher + 子组件的简洁多标签页布局
  • Localchat2— 功能齐全的聊天应用:流式传输、RAG、模型管理、分支对话

纯前端应用

  • 电子表格— 完整的电子表格应用
  • 文字处理器— 文档编辑器
  • 演示文稿— 幻灯片制作工具
  • 太阳系— 3D可视化
  • 元素周期表— 交互式元素周期表
  • STL查看器— 3D模型查看器

AI/ML工作流

  • 音乐生成— AI音乐生成
  • SDXL生成器— Stable Diffusion图像生成
  • RAG— 检索增强生成
  • 语音代理— 基于语音的AI代理
  • STT— 语音转文本
  • 动画角色— 与动画角色聊天

列出所有:mcporter call contextui.list_workflows folder="examples"读取任意:mcporter call contextui.read_workflow path="<路径>"

代理注册

要将ContextUI用作代理:

  1. 安装ContextUI来自contextui.ai
  2. 配置MCP将您的代理连接到ContextUI
  3. 开始构建— 创建工作流,发布至Exchange,赚取积分

Python后端最佳实践

ServerLauncher模式(必需)

所有带有Python后端的流程必须使用ServerLauncher模式:

  1. 从规范源复制examples/KokoroTTS/ui/ServerLauncher/→ 你的工作流的ui/ServerLauncher/
  2. 始终使用uvicorn[standard]:而不是仅使用uvicorn[standard]额外包含WebSocket支持。
  3. GPU感知包:ServerLauncher自动检测CUDA/MPS/CPU并使用预构建的wheel包。
// ✅ Correct
packages: ['fastapi', 'uvicorn[standard]', 'torch', 'llama-cpp-python']

// ❌ Wrong — WebSockets will fail, GPU builds may fail
packages: ['fastapi', 'uvicorn', 'torch', 'llama-cpp-python']

GPU包处理

ServerLauncher自动处理GPU感知安装:

软件包CUDA(Windows/Linux)Metal(Mac)
torch通过预构建的wheel包安装--index-url原生pip
llama-cpp-python通过预构建的wheel包安装--extra-index-url从源代码构建(CMAKE_ARGS)

为何使用预构建的wheel包?在Windows上从源代码构建需要CUDA Toolkit + Visual Studio Build Tools + CMake全部完美配置。预构建的wheel包则可以直接使用。

实时安装反馈

每个软件包成功安装后会立即变为绿色(而非全部在最后同时变化)。用户可以实时看到安装进度。

HuggingFace缓存监控

如果您的流程需要下载HF模型并显示缓存大小:

  • 扫描两者 blobs/snapshots/目录
  • 跳过符号链接以避免重复计数
  • 检查.incomplete文件以检测活动下载

参见references/cache-monitoring.md获取RAG、MusicGen、LocalChat等使用的完整模式

提示

  • 从示例开始— 在从头开始编写之前,先阅读现有工作流
  • 视觉测试— 使用launch_workflow+ui_screenshot以验证您的用户界面显示正常
  • 清理— 使用close_workflow在完成后关闭标签页(通过路径,或省略路径以关闭活动标签页)
  • 深色主题— 使用{color}-950背景。浅色文字。ContextUI 是一个深色模式应用。
  • 仅使用 Tailwind— 没有 CSS 文件,没有 styled-components。在 JSX 中使用 Tailwind 类。
  • Python 用于繁重任务— 需要机器学习、API、数据处理?编写一个 Python 后端,通过 MCP 启动它,从你的 TSX 代码中通过 fetch 调用它。
  • 规范示例:复制模式时,请使用examples/KokoroTTS/作为参考 — 它包含了最新的修复。

关键陷阱

ServerLauncher 在标签页关闭时终止服务器

当你关闭工作流以重新加载代码时,清理卸载过程会运行stopServer()。服务器会终止。每次重新加载标签页后,你必须重启它(通过设置标签页或 MCP 的python_start_server)。

不要频繁轮询健康端点

只在挂载时检查一次服务器健康状态 —— 不要使用轮询间隔。每隔几秒轮询会产生噪音且浪费资源。如果需要响应服务器状态变化,请使用钩子中的server.connected

通过MCP桥接切换标签页

通过向~/ContextUI/.mcp-bridge/目录写入JSON来切换标签页:

{"type":"switch_tab","tab":"ExactComponentName","id":"unique_id"}

首先使用list_tabs获取确切的组件名称 —— 部分匹配无效。 响应会以{id}.response.json文件的形式出现在同一目录中。

测试时优先使用MCP工具

测试工作流时,请使用可用的MCP工具(ui_clickui_screenshotlaunch_workflowclose_workflow) 而不是要求用户手动点击用户界面。如果某些操作需要您不具备的权限或访问权,请告知用户所需的条件。

免责申明
部分文章来自各大搜索引擎,如有侵权,请与我联系删除。
打赏

文章底部电脑广告
手机广告位-内容正文底部

相关文章

您是本站第393712名访客 今日有1篇新文章/评论