-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
223 lines (205 loc) · 6.91 KB
/
index.ts
File metadata and controls
223 lines (205 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { execFileSync } from "node:child_process";
import net from "node:net";
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
import { Type } from "@sinclair/typebox";
import type { AnyAgentTool } from "openclaw/agents/tools/common.js";
const DEFAULT_HOST = "127.0.0.1";
const DEFAULT_PORT = 9990;
const CALL_TIMEOUT_MS = 30_000;
interface McpToolDef {
name: string;
description: string;
inputSchema: Record<string, unknown>;
}
function getHost(pluginConfig?: Record<string, unknown>): string {
if (pluginConfig && typeof pluginConfig.mcpHost === "string" && pluginConfig.mcpHost) {
return pluginConfig.mcpHost;
}
return DEFAULT_HOST;
}
function getPort(pluginConfig?: Record<string, unknown>): number {
if (pluginConfig && typeof pluginConfig.mcpPort === "number") {
return pluginConfig.mcpPort;
}
return DEFAULT_PORT;
}
/** Convert a JSON Schema properties object into a TypeBox Type.Object schema. */
function jsonSchemaToTypebox(
inputSchema?: Record<string, unknown>,
): ReturnType<typeof Type.Object> {
if (!inputSchema) {
return Type.Object({});
}
const properties = (inputSchema.properties ?? {}) as Record<string, Record<string, unknown>>;
const required = new Set((inputSchema.required ?? []) as string[]);
const tbProps: Record<string, unknown> = {};
for (const [key, prop] of Object.entries(properties)) {
const desc = typeof prop.description === "string" ? prop.description : undefined;
let inner;
switch (prop.type) {
case "number":
case "integer":
inner = Type.Number({ description: desc });
break;
case "boolean":
inner = Type.Boolean({ description: desc });
break;
case "array":
inner = Type.Array(Type.Unknown(), { description: desc });
break;
case "object":
inner = Type.Record(Type.String(), Type.Unknown(), { description: desc });
break;
default:
inner = Type.String({ description: desc });
break;
}
tbProps[key] = required.has(key) ? inner : Type.Optional(inner);
}
// @ts-expect-error -- heterogeneous property map built dynamically
return Type.Object(tbProps);
}
/**
* Discover MCP tools synchronously by connecting directly to the DimOS TCP server.
* Uses a child process so we can block the main thread during plugin registration.
*/
function discoverToolsSync(host: string, port: number): McpToolDef[] {
const script = `
const net = require('net');
const client = net.createConnection(${port}, ${JSON.stringify(host)}, () => {
client.write(JSON.stringify({jsonrpc:'2.0',id:1,method:'initialize',params:{protocolVersion:'2024-11-05',capabilities:{},clientInfo:{name:'openclaw-dimos',version:'0.0.1'}}})+'\\n');
});
let buf='';
client.on('data',d=>{
buf+=d.toString();
const lines=buf.split('\\n');
buf=lines.pop()||'';
for(const line of lines){
if(!line.trim())continue;
const msg=JSON.parse(line);
if(msg.id===1){
client.write(JSON.stringify({jsonrpc:'2.0',id:2,method:'tools/list',params:{}})+'\\n');
}else if(msg.id===2){
process.stdout.write(JSON.stringify(msg.result.tools));
client.end();
}
}
});
client.on('error',e=>{process.stderr.write(e.message);process.exit(1);});
`;
const result = execFileSync("node", ["-e", script], {
timeout: 10_000,
encoding: "utf-8",
});
return JSON.parse(result);
}
/** Call an MCP tool via direct TCP connection to the DimOS server. */
async function callTool(
host: string,
port: number,
name: string,
args: Record<string, unknown>,
): Promise<string> {
return new Promise((resolve, reject) => {
const client = net.createConnection(port, host, () => {
client.write(
JSON.stringify({
jsonrpc: "2.0",
id: 1,
method: "initialize",
params: {
protocolVersion: "2024-11-05",
capabilities: {},
clientInfo: { name: "openclaw-dimos", version: "0.0.1" },
},
}) + "\n",
);
});
let buf = "";
let phase: "init" | "call" | "done" = "init";
const timer = setTimeout(() => {
client.destroy();
reject(new Error("MCP tool call timed out"));
}, CALL_TIMEOUT_MS);
client.on("data", (d) => {
buf += d.toString();
const lines = buf.split("\n");
buf = lines.pop() || "";
for (const line of lines) {
if (!line.trim()) continue;
const msg = JSON.parse(line);
if (phase === "init" && msg.id === 1) {
phase = "call";
client.write(
JSON.stringify({
jsonrpc: "2.0",
id: 2,
method: "tools/call",
params: { name, arguments: args },
}) + "\n",
);
} else if (phase === "call" && msg.id === 2) {
phase = "done";
clearTimeout(timer);
if (msg.error) {
resolve(`Error: ${msg.error.message}`);
} else {
const content = msg.result?.content;
const text = Array.isArray(content)
? content
.filter((c: { type: string }) => c.type === "text")
.map((c: { text: string }) => c.text)
.join("\n")
: JSON.stringify(content);
resolve(text || "OK");
}
client.end();
}
}
});
client.on("error", (err) => {
clearTimeout(timer);
reject(err);
});
});
}
export default {
id: "dimos",
name: "Dimos MCP Bridge",
description: "Exposes tools from the dimos MCP server as OpenClaw agent tools",
register(api: OpenClawPluginApi) {
const host = getHost(api.pluginConfig);
const port = getPort(api.pluginConfig);
// Discover tools synchronously so they're available immediately —
// no dependency on the service lifecycle (which the CLI agent path skips).
let mcpTools: McpToolDef[];
try {
mcpTools = discoverToolsSync(host, port);
api.logger.info(`dimos: discovered ${mcpTools.length} tool(s) from ${host}:${port}`);
} catch (err) {
api.logger.error(`dimos: failed to discover tools from ${host}:${port}: ${err}`);
return;
}
// Register each tool with a proper name so OpenClaw's tool system tracks them.
for (const mcpTool of mcpTools) {
const parameters = jsonSchemaToTypebox(mcpTool.inputSchema);
const tool: AnyAgentTool = {
name: mcpTool.name,
label: mcpTool.name,
description: mcpTool.description || "",
parameters,
async execute(
_toolCallId: string,
params: Record<string, unknown>,
): Promise<{ content: Array<{ type: "text"; text: string }>; details: unknown }> {
const text = await callTool(host, port, mcpTool.name, params);
return {
content: [{ type: "text" as const, text }],
details: { tool: mcpTool.name, params },
};
},
};
api.registerTool(tool, { name: mcpTool.name });
}
},
};