libp2p-mesh 2026.5.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 OpenClaw Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,273 @@
1
+ # openclaw-libp2p-mesh
2
+
3
+ P2P mesh network plugin for OpenClaw. Enables direct peer-to-peer communication between OpenClaw instances using libp2p — no central server required.
4
+
5
+ ## Features
6
+
7
+ - **LAN Discovery** — Auto-discovers peers on the same local network via mDNS (Bonjour/Avahi)
8
+ - **Direct Messaging** — Send messages directly to another peer by its Peer ID
9
+ - **Broadcast** — Publish messages to a shared topic, flood-fill forwarded across the mesh
10
+ - **Bootstrap Mode** — Optional static bootstrap peer list for non-LAN scenarios
11
+ - **WebSocket Transport** — Optional WebSocket support for NAT/firewall-friendly connections
12
+
13
+ ## Requirements
14
+
15
+ - OpenClaw >= 2026.3.24
16
+ - Node.js >= 22
17
+ - For LAN discovery: both peers must be on the same local network (same WiFi / Ethernet segment)
18
+
19
+ ## Installation
20
+
21
+ ### Method 1: Via OpenClaw CLI (Recommended)
22
+
23
+ ```bash
24
+ openclaw install openclaw-libp2p-mesh
25
+ ```
26
+
27
+ ### Method 2: Manual (npm)
28
+
29
+ 如果无法通过 OpenClaw CLI 安装,可以手动安装到 managed npm root:
30
+
31
+ ```bash
32
+ cd ~/.openclaw/npm
33
+ npm install openclaw-libp2p-mesh
34
+ ```
35
+
36
+ 然后刷新插件注册表:
37
+
38
+ ```bash
39
+ openclaw plugins registry --refresh
40
+ ```
41
+
42
+ The published npm package includes compiled JavaScript under `dist/`, so OpenClaw and acpx can load it directly.
43
+
44
+ Then add to your `~/.openclaw/openclaw.json`:
45
+
46
+ ```json
47
+ {
48
+ "plugins": {
49
+ "libp2p-mesh": {
50
+ "enabled": true,
51
+ "config": {
52
+ "discovery": "mdns"
53
+ }
54
+ }
55
+ },
56
+ "channels": {
57
+ "libp2p-mesh": {
58
+ "enabled": true
59
+ }
60
+ }
61
+ }
62
+ ```
63
+
64
+ ## Configuration
65
+
66
+ Add a `libp2p-mesh` block to your `openclaw.json` under `plugins`:
67
+
68
+ ### Minimal LAN Setup (Default)
69
+
70
+ ```json
71
+ {
72
+ "plugins": {
73
+ "libp2p-mesh": {
74
+ "enabled": true,
75
+ "config": {
76
+ "discovery": "mdns"
77
+ }
78
+ }
79
+ },
80
+ "channels": {
81
+ "libp2p-mesh": {
82
+ "enabled": true
83
+ }
84
+ }
85
+ }
86
+ ```
87
+
88
+ This is sufficient for two computers on the same WiFi to discover each other.
89
+
90
+ ### With Static Port (Optional)
91
+
92
+ By default, the node picks a random TCP port. To use a fixed port:
93
+
94
+ ```json
95
+ {
96
+ "plugins": {
97
+ "libp2p-mesh": {
98
+ "enabled": true,
99
+ "config": {
100
+ "discovery": "mdns",
101
+ "listenAddrs": ["/ip4/0.0.0.0/tcp/4001"]
102
+ }
103
+ }
104
+ },
105
+ "channels": {
106
+ "libp2p-mesh": {
107
+ "enabled": true
108
+ }
109
+ }
110
+ }
111
+ ```
112
+
113
+ ### With Bootstrap Nodes (Cross-Network)
114
+
115
+ If peers are on different networks, use a bootstrap node:
116
+
117
+ ```json
118
+ {
119
+ "plugins": {
120
+ "libp2p-mesh": {
121
+ "enabled": true,
122
+ "config": {
123
+ "discovery": "bootstrap",
124
+ "bootstrapList": [
125
+ "/ip4/203.0.113.10/tcp/4001/p2p/12D3KooW..."
126
+ ]
127
+ }
128
+ }
129
+ },
130
+ "channels": {
131
+ "libp2p-mesh": {
132
+ "enabled": true
133
+ }
134
+ }
135
+ }
136
+ ```
137
+
138
+ ### Full Configuration Reference
139
+
140
+ | Key | Type | Default | Description |
141
+ |-----|------|---------|-------------|
142
+ | `discovery` | `string` | `"mdns"` | Discovery mechanism: `"mdns"` (LAN), `"bootstrap"` (static list), `"dht"` (not yet implemented) |
143
+ | `listenAddrs` | `string[]` | `["/ip4/0.0.0.0/tcp/0"]` | libp2p listen multiaddrs |
144
+ | `bootstrapList` | `string[]` | `[]` | Static bootstrap peer multiaddrs (when `discovery=bootstrap`) |
145
+ | `enableWebSocket` | `boolean` | `false` | Enable WebSocket transport for browser/NAT compatibility |
146
+ | `meshTopic` | `string` | `"openclaw-mesh"` | Default broadcast topic |
147
+ | `enableAgentSync` | `boolean` | `true` | Enable agent state synchronization over the mesh |
148
+
149
+ ## Usage: Two Computers on the Same LAN
150
+
151
+ ### Step 1 — Start both gateways
152
+
153
+ **Computer A** (e.g. your desktop):
154
+ ```bash
155
+ openclaw gateway run
156
+ ```
157
+
158
+ **Computer B** (e.g. your laptop or a friend's machine):
159
+ ```bash
160
+ openclaw gateway run
161
+ ```
162
+
163
+ Wait ~5–10 seconds for mDNS discovery. You should see in the logs:
164
+ ```
165
+ [libp2p-mesh] Peer connected: 12D3KooW...
166
+ ```
167
+
168
+ ### Step 2 — Find your Peer ID
169
+
170
+ On each computer:
171
+ ```bash
172
+ openclaw channels status --probe
173
+ ```
174
+
175
+ Look for the `libp2p-mesh` channel section — your Peer ID is displayed there. It looks like:
176
+ ```
177
+ 12D3KooWRYyHaWzL8n7i5Z8zZ8Z8Z8Z8Z8Z8Z8Z8Z8Z8Z8
178
+ ```
179
+
180
+ Alternatively, check the gateway startup log:
181
+ ```
182
+ [libp2p-mesh] Node started. Peer ID: 12D3KooW...
183
+ ```
184
+
185
+ ### Step 3 — Send a message
186
+
187
+ **From Computer A to Computer B:**
188
+ ```bash
189
+ openclaw message send libp2p-mesh <COMPUTER-B-PEER-ID> "Hello from A!"
190
+ ```
191
+
192
+ **From Computer B to Computer A:**
193
+ ```bash
194
+ openclaw message send libp2p-mesh <COMPUTER-A-PEER-ID> "Hello from B!"
195
+ ```
196
+
197
+ ### Step 4 — Verify receipt
198
+
199
+ Check the gateway logs on the receiving machine. You should see:
200
+ ```
201
+ [libp2p-mesh] Direct message from <sender-peer-id>: Hello from A!
202
+ ```
203
+
204
+ ## Troubleshooting
205
+
206
+ ### Peers do not discover each other
207
+
208
+ 1. **Confirm same network** — Both computers must be on the same subnet (e.g. `192.168.1.x`). Check with `ip addr` or `ifconfig`.
209
+ 2. **Check firewall** — OpenClaw needs inbound TCP access on the port chosen by libp2p (random by default). Temporarily disable the firewall to test:
210
+ - macOS: `sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate off`
211
+ - Linux (ufw): `sudo ufw disable`
212
+ - Linux (firewalld): `sudo systemctl stop firewalld`
213
+ 3. **Check mDNS** — Ensure mDNS/Bonjour/Avahi is running:
214
+ - macOS: built-in, should work
215
+ - Linux: `sudo systemctl status avahi-daemon`
216
+ 4. **Use static port + manual IP** — If mDNS still fails, switch to bootstrap mode and use the LAN IP directly:
217
+ ```json
218
+ {
219
+ "discovery": "bootstrap",
220
+ "bootstrapList": [
221
+ "/ip4/192.168.1.42/tcp/4001/p2p/<PEER-ID-OF-OTHER-MACHINE>"
222
+ ]
223
+ }
224
+ ```
225
+
226
+ ### "Mesh network is not started" error
227
+
228
+ This error only appears if you run `openclaw message send` while the gateway is **not** running. Start the gateway first:
229
+ ```bash
230
+ openclaw gateway run
231
+ ```
232
+
233
+ If the gateway is already running, the CLI automatically routes through the gateway (this was fixed in recent versions).
234
+
235
+ ### Message timeout after 8 seconds
236
+
237
+ The peer may be unreachable. Check:
238
+ - Is the target gateway still running?
239
+ - Are both machines on the same network?
240
+ - Is there a firewall blocking the connection?
241
+
242
+ ## Architecture
243
+
244
+ ```
245
+ ┌─────────────┐ mDNS LAN ┌─────────────┐
246
+ │ Computer A │ ←────────────────→ │ Computer B │
247
+ │ (OpenClaw) │ auto-discovery │ (OpenClaw) │
248
+ │ │ ◄─── libp2p/TCP ──► │ │
249
+ │ Peer ID: A │ │ Peer ID: B │
250
+ └─────────────┘ └─────────────┘
251
+ ```
252
+
253
+ - **mDNS** broadcasts service announcements on the LAN
254
+ - **libp2p** handles encrypted peer connections and stream multiplexing
255
+ - **Noise** encrypts all traffic between peers
256
+ - Messages are deduplicated by message ID to prevent loops
257
+
258
+ ## Development
259
+
260
+ ```bash
261
+ cd extensions/libp2p-mesh
262
+
263
+ # Standalone mesh test (no OpenClaw required)
264
+ node --import tsx test-p2p-communication.mjs
265
+
266
+ # Build the plugin
267
+ cd ../..
268
+ pnpm build
269
+ ```
270
+
271
+ ## License
272
+
273
+ MIT
package/api.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { createMeshNetwork } from "./src/mesh.js";
2
+ export type { MeshNetwork, P2PMessage, MeshConfig } from "./src/types.js";
package/dist/api.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { createMeshNetwork } from "./src/mesh.js";
2
+ export type { MeshNetwork, P2PMessage, MeshConfig } from "./src/types.js";
package/dist/api.js ADDED
@@ -0,0 +1 @@
1
+ export { createMeshNetwork } from "./src/mesh.js";
@@ -0,0 +1,9 @@
1
+ import type { OpenClawPluginConfigSchema } from "openclaw/plugin-sdk/core";
2
+ declare const _default: {
3
+ id: string;
4
+ name: string;
5
+ description: string;
6
+ configSchema: OpenClawPluginConfigSchema;
7
+ register: NonNullable<import("openclaw/plugin-sdk/core").OpenClawPluginDefinition["register"]>;
8
+ } & Pick<import("openclaw/plugin-sdk/core").OpenClawPluginDefinition, "reload" | "kind" | "nodeHostCommands" | "securityAuditCollectors">;
9
+ export default _default;
package/dist/index.js ADDED
@@ -0,0 +1,62 @@
1
+ import { definePluginEntry } from "openclaw/plugin-sdk/core";
2
+ import { registerLibp2pMesh } from "./src/plugin.js";
3
+ function createLibp2pMeshConfigSchema() {
4
+ return {
5
+ safeParse(value) {
6
+ if (value === undefined) {
7
+ return { success: true, data: undefined };
8
+ }
9
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
10
+ return {
11
+ success: false,
12
+ error: { issues: [{ path: [], message: "expected config object" }] },
13
+ };
14
+ }
15
+ return { success: true, data: value };
16
+ },
17
+ jsonSchema: {
18
+ type: "object",
19
+ additionalProperties: false,
20
+ properties: {
21
+ listenAddrs: {
22
+ type: "array",
23
+ items: { type: "string" },
24
+ default: ["/ip4/0.0.0.0/tcp/0"],
25
+ },
26
+ enableWebSocket: {
27
+ type: "boolean",
28
+ default: false,
29
+ description: "Enable WebSocket transport (useful for browser compatibility)",
30
+ },
31
+ discovery: {
32
+ type: "string",
33
+ enum: ["mdns", "bootstrap", "dht"],
34
+ default: "mdns",
35
+ },
36
+ bootstrapList: {
37
+ type: "array",
38
+ items: { type: "string" },
39
+ },
40
+ meshTopic: {
41
+ type: "string",
42
+ default: "openclaw-mesh",
43
+ },
44
+ enablePubsub: {
45
+ type: "boolean",
46
+ default: true,
47
+ },
48
+ enableAgentSync: {
49
+ type: "boolean",
50
+ default: true,
51
+ },
52
+ },
53
+ },
54
+ };
55
+ }
56
+ export default definePluginEntry({
57
+ id: "libp2p-mesh",
58
+ name: "libp2p Mesh Network",
59
+ description: "P2P network for cross-instance agent communication via libp2p.",
60
+ configSchema: createLibp2pMeshConfigSchema(),
61
+ register: registerLibp2pMesh,
62
+ });
@@ -0,0 +1,130 @@
1
+ import type { MeshNetwork } from "./types.js";
2
+ export declare function buildP2PTools(mesh: MeshNetwork): ({
3
+ name: string;
4
+ label: string;
5
+ description: string;
6
+ parameters: {
7
+ type: "object";
8
+ properties: {
9
+ peerId: {
10
+ type: "string";
11
+ description: string;
12
+ };
13
+ message: {
14
+ type: "string";
15
+ description: string;
16
+ };
17
+ topic?: undefined;
18
+ };
19
+ required: string[];
20
+ };
21
+ execute(_toolCallId: string, params: {
22
+ peerId: string;
23
+ message: string;
24
+ }): Promise<{
25
+ content: {
26
+ type: "text";
27
+ text: string;
28
+ }[];
29
+ details: {
30
+ sent: boolean;
31
+ peerId: string;
32
+ error?: undefined;
33
+ };
34
+ isError?: undefined;
35
+ } | {
36
+ content: {
37
+ type: "text";
38
+ text: string;
39
+ }[];
40
+ details: {
41
+ sent: boolean;
42
+ peerId: string;
43
+ error: string;
44
+ };
45
+ isError: boolean;
46
+ }>;
47
+ } | {
48
+ name: string;
49
+ label: string;
50
+ description: string;
51
+ parameters: {
52
+ type: "object";
53
+ properties: {
54
+ topic: {
55
+ type: "string";
56
+ description: string;
57
+ };
58
+ message: {
59
+ type: "string";
60
+ description: string;
61
+ };
62
+ peerId?: undefined;
63
+ };
64
+ required: string[];
65
+ };
66
+ execute(_toolCallId: string, params: {
67
+ topic: string;
68
+ message: string;
69
+ }): Promise<{
70
+ content: {
71
+ type: "text";
72
+ text: string;
73
+ }[];
74
+ details: {
75
+ broadcast: boolean;
76
+ topic: string;
77
+ error?: undefined;
78
+ };
79
+ isError?: undefined;
80
+ } | {
81
+ content: {
82
+ type: "text";
83
+ text: string;
84
+ }[];
85
+ details: {
86
+ broadcast: boolean;
87
+ topic: string;
88
+ error: string;
89
+ };
90
+ isError: boolean;
91
+ }>;
92
+ } | {
93
+ name: string;
94
+ label: string;
95
+ description: string;
96
+ parameters: {
97
+ type: "object";
98
+ properties: {
99
+ peerId?: undefined;
100
+ message?: undefined;
101
+ topic?: undefined;
102
+ };
103
+ required?: undefined;
104
+ };
105
+ execute(_toolCallId: string): Promise<{
106
+ content: {
107
+ type: "text";
108
+ text: string;
109
+ }[];
110
+ details: {
111
+ localPeerId: string;
112
+ connectedPeers: string[];
113
+ count: number;
114
+ error?: undefined;
115
+ };
116
+ isError?: undefined;
117
+ } | {
118
+ content: {
119
+ type: "text";
120
+ text: string;
121
+ }[];
122
+ details: {
123
+ error: string;
124
+ localPeerId?: undefined;
125
+ connectedPeers?: undefined;
126
+ count?: undefined;
127
+ };
128
+ isError: boolean;
129
+ }>;
130
+ })[];
@@ -0,0 +1,116 @@
1
+ export function buildP2PTools(mesh) {
2
+ return [
3
+ {
4
+ name: "p2p_send_message",
5
+ label: "P2P Send Message",
6
+ description: "Send a direct message to another agent via the P2P mesh network.",
7
+ parameters: {
8
+ type: "object",
9
+ properties: {
10
+ peerId: {
11
+ type: "string",
12
+ description: "Target peer ID (libp2p Peer ID string)",
13
+ },
14
+ message: {
15
+ type: "string",
16
+ description: "Message content to send",
17
+ },
18
+ },
19
+ required: ["peerId", "message"],
20
+ },
21
+ async execute(_toolCallId, params) {
22
+ try {
23
+ await mesh.sendToPeer(params.peerId, params.message);
24
+ return {
25
+ content: [{ type: "text", text: `Message sent to ${params.peerId}` }],
26
+ details: { sent: true, peerId: params.peerId },
27
+ };
28
+ }
29
+ catch (err) {
30
+ return {
31
+ content: [
32
+ {
33
+ type: "text",
34
+ text: `Failed to send message to ${params.peerId}: ${String(err)}`,
35
+ },
36
+ ],
37
+ details: { sent: false, peerId: params.peerId, error: String(err) },
38
+ isError: true,
39
+ };
40
+ }
41
+ },
42
+ },
43
+ {
44
+ name: "p2p_broadcast",
45
+ label: "P2P Broadcast",
46
+ description: "Broadcast a message to all peers on a topic via the P2P mesh network.",
47
+ parameters: {
48
+ type: "object",
49
+ properties: {
50
+ topic: {
51
+ type: "string",
52
+ description: "Topic name to broadcast on",
53
+ },
54
+ message: {
55
+ type: "string",
56
+ description: "Message content to broadcast",
57
+ },
58
+ },
59
+ required: ["topic", "message"],
60
+ },
61
+ async execute(_toolCallId, params) {
62
+ try {
63
+ await mesh.publishToTopic(params.topic, params.message);
64
+ return {
65
+ content: [{ type: "text", text: `Broadcast sent to topic ${params.topic}` }],
66
+ details: { broadcast: true, topic: params.topic },
67
+ };
68
+ }
69
+ catch (err) {
70
+ return {
71
+ content: [
72
+ {
73
+ type: "text",
74
+ text: `Failed to broadcast to topic ${params.topic}: ${String(err)}`,
75
+ },
76
+ ],
77
+ details: { broadcast: false, topic: params.topic, error: String(err) },
78
+ isError: true,
79
+ };
80
+ }
81
+ },
82
+ },
83
+ {
84
+ name: "p2p_list_peers",
85
+ label: "P2P List Peers",
86
+ description: "List currently connected peers in the P2P mesh network.",
87
+ parameters: {
88
+ type: "object",
89
+ properties: {},
90
+ },
91
+ async execute(_toolCallId) {
92
+ try {
93
+ const peers = mesh.getConnectedPeers();
94
+ const text = peers.length === 0
95
+ ? "No peers currently connected."
96
+ : `Connected peers (${peers.length}): ${peers.join(", ")}`;
97
+ return {
98
+ content: [{ type: "text", text }],
99
+ details: {
100
+ localPeerId: mesh.getLocalPeerId(),
101
+ connectedPeers: peers,
102
+ count: peers.length,
103
+ },
104
+ };
105
+ }
106
+ catch (err) {
107
+ return {
108
+ content: [{ type: "text", text: `Failed to list peers: ${String(err)}` }],
109
+ details: { error: String(err) },
110
+ isError: true,
111
+ };
112
+ }
113
+ },
114
+ },
115
+ ];
116
+ }
@@ -0,0 +1,3 @@
1
+ import type { ChannelPlugin } from "openclaw/plugin-sdk/core";
2
+ import type { MeshNetwork } from "./types.js";
3
+ export declare function createLibp2pMeshChannel(mesh: MeshNetwork): ChannelPlugin;
@@ -0,0 +1,66 @@
1
+ import { createChatChannelPlugin } from "openclaw/plugin-sdk/core";
2
+ import { sendViaMesh } from "./send.js";
3
+ export function createLibp2pMeshChannel(mesh) {
4
+ return createChatChannelPlugin({
5
+ base: {
6
+ id: "libp2p-mesh",
7
+ meta: {
8
+ id: "libp2p-mesh",
9
+ label: "P2P Mesh",
10
+ selectionLabel: "P2P Mesh",
11
+ docsPath: "/channels/libp2p-mesh",
12
+ docsLabel: "libp2p-mesh",
13
+ blurb: "libp2p mesh network for cross-instance agent communication.",
14
+ systemImage: "network",
15
+ },
16
+ capabilities: {
17
+ chatTypes: ["direct"],
18
+ media: false,
19
+ blockStreaming: false,
20
+ },
21
+ configSchema: {
22
+ schema: {
23
+ type: "object",
24
+ additionalProperties: false,
25
+ properties: {},
26
+ },
27
+ },
28
+ config: {
29
+ listAccountIds: () => ["default"],
30
+ resolveAccount: () => ({
31
+ accountId: "default",
32
+ configured: true,
33
+ enabled: true,
34
+ }),
35
+ isConfigured: () => true,
36
+ isEnabled: () => true,
37
+ describeAccount: () => ({
38
+ accountId: "default",
39
+ name: "default",
40
+ configured: true,
41
+ enabled: true,
42
+ connected: mesh.getConnectedPeers().length > 0,
43
+ }),
44
+ },
45
+ messaging: {
46
+ normalizeTarget: (raw) => raw.trim(),
47
+ targetResolver: {
48
+ looksLikeId: () => true,
49
+ hint: "peer-id",
50
+ },
51
+ },
52
+ },
53
+ outbound: {
54
+ deliveryMode: "gateway",
55
+ sendText: async ({ to, text }) => {
56
+ try {
57
+ await sendViaMesh(mesh, to, text);
58
+ return { channel: "libp2p-mesh", messageId: `p2p-${Date.now()}` };
59
+ }
60
+ catch (err) {
61
+ return { channel: "libp2p-mesh", messageId: `p2p-${Date.now()}`, meta: { error: String(err) } };
62
+ }
63
+ },
64
+ },
65
+ });
66
+ }