为 OpenClaw/Clawdbot 提供的 MCP(模型上下文协议)集成。在以下场景中使用:
- 连接并编排 MCP 工具服务器(文件系统、GitHub 等)
- 通过 IndexedDB/localStorage 实现跨会话状态持久化
- 跨多设备同步会话
触发词:"MCP"、"工具服务器"、"子代理编排"、"会话同步"、"状态持久化"、"Claude 代码集成"
npm install openclaw-claude-code-skill
import {
initializeMcpSystem,
addMcpServer,
executeMcpAction,
getAllTools
} from "openclaw-claude-code-skill";
// 1. 初始化所有已配置的服务器
await initializeMcpSystem();
// 2. 添加新的 MCP 服务器
await addMcpServer("fs", {
command: "npx",
args: ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"]
});
// 3. 获取可用工具
const tools = await getAllTools();
// 4. 调用工具
const result = await executeMcpAction("fs", {
method: "tools/call",
params: { name: "read_file", arguments: { path: "/tmp/test.txt" } }
});
import { createPersistStore, indexedDBStorage } from "openclaw-claude-code-skill";
const useStore = createPersistStore(
{ count: 0, items: [] },
(set, get) => ({
increment: () => set({ count: get().count + 1 }),
addItem: (item: string) => set({ items: [...get().items, item] })
}),
{ name: "my-store" },
indexedDBStorage // 省略则使用 localStorage
);
// 检查水合状态
if (useStore.getState()._hasHydrated) {
console.log("状态已恢复!");
}
import { mergeSessions, mergeWithUpdate, mergeKeyValueStore } from "openclaw-claude-code-skill";
// 合并来自多个源的聊天会话
const mergedSessions = mergeSessions(localSessions, remoteSessions);
// 基于时间戳的配置合并
const mergedConfig = mergeWithUpdate(localConfig, remoteConfig);
| 函数 | 用途 |
|---|---|
initializeMcpSystem() |
从配置启动所有 MCP 服务器 |
addMcpServer(id, config) |
动态添加新服务器 |
removeMcpServer(id) |
移除服务器 |
pauseMcpServer(id) |
暂停服务器 |
resumeMcpServer(id) |
恢复已暂停的服务器 |
executeMcpAction(id, req) |
在指定服务器上调用工具 |
getAllTools() |
列出所有可用工具 |
getClientsStatus() |
获取所有 MCP 客户端状态 |
setConfigPath(path) |
设置自定义配置文件路径 |
createPersistStore() |
创建带持久化的 Zustand 存储 |
mergeSessions() |
合并会话数组 |
mergeWithUpdate() |
基于时间戳合并 |
mergeKeyValueStore() |
合并键值存储 |
创建 mcp_config.json:
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"],
"status": "active"
},
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": { "GITHUB_TOKEN": "your-token" },
"status": "active"
}
}
}
设置自定义配置路径:
import { setConfigPath } from "openclaw-claude-code-skill";
setConfigPath("/path/to/mcp_config.json");