@openliate/liate 0.0.0-stage → 0.1.6
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 +21 -0
- package/README.md +127 -2
- package/README.npm.md +128 -0
- package/dist/bin.js +28745 -0
- package/dist/liate.js +39479 -0
- package/dist/runtime/hono.js +28072 -0
- package/dist/runtime/index.js +28072 -0
- package/hub/agents/docgyan-agent/agent.json +27 -0
- package/hub/agents/docgyan-agent/agent.ts +39 -0
- package/hub/agents/gnani-agent/agent.json +17 -0
- package/hub/agents/neon-agent/agent.json +18 -0
- package/hub/agents/prism-agent/agent.json +17 -0
- package/hub/agents/sarvam-agent/agent.json +22 -0
- package/package.json +71 -4
- package/src/cli/index.ts +346 -0
- package/src/cli/resolver.ts +153 -0
- package/src/core/aum.ts +137 -0
- package/src/core/om.ts +159 -0
- package/src/core/spec.ts +112 -0
- package/src/core/uuid.ts +8 -0
- package/src/core/version.ts +2 -0
- package/src/layers/agent/harness.ts +118 -0
- package/src/layers/agent/loop.ts +203 -0
- package/src/layers/agent/skills.ts +302 -0
- package/src/layers/agent/types.ts +66 -0
- package/src/layers/environment/sandbox.ts +107 -0
- package/src/layers/environment/types.ts +23 -0
- package/src/layers/integration/knowledge.ts +187 -0
- package/src/layers/integration/lib.ts +167 -0
- package/src/layers/integration/memory.ts +135 -0
- package/src/layers/integration/repo.ts +273 -0
- package/src/layers/integration/types.ts +60 -0
- package/src/layers/llm/model.ts +928 -0
- package/src/layers/llm/types.ts +64 -0
- package/src/layers/tools/mcp.ts +236 -0
- package/src/layers/tools/registry.ts +307 -0
- package/src/layers/tools/server.ts +69 -0
- package/src/layers/tools/types.ts +38 -0
- package/src/liate.ts +65 -0
- package/src/routes/skills.ts +73 -0
- package/src/runtime/hono.ts +346 -0
- package/src/runtime/wasm.ts +23 -0
- package/src/scaffold/project.ts +188 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export type LlmRole = "system" | "user" | "assistant" | "tool";
|
|
2
|
+
|
|
3
|
+
export interface LlmMessage {
|
|
4
|
+
role: LlmRole;
|
|
5
|
+
content: string | null;
|
|
6
|
+
name?: string;
|
|
7
|
+
tool_call_id?: string;
|
|
8
|
+
tool_calls?: any[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type SarvamMessage = LlmMessage;
|
|
12
|
+
|
|
13
|
+
export interface ToolCall {
|
|
14
|
+
id: string;
|
|
15
|
+
name: string;
|
|
16
|
+
arguments: Record<string, any>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface LlmChatOptions {
|
|
20
|
+
model: string;
|
|
21
|
+
provider?: string;
|
|
22
|
+
baseUrl?: string;
|
|
23
|
+
endpoint?: string;
|
|
24
|
+
temperature?: number;
|
|
25
|
+
maxTokens?: number;
|
|
26
|
+
reasoningEffort?: "none" | "low" | "high" | "max";
|
|
27
|
+
apiKey?: string;
|
|
28
|
+
tools?: any[];
|
|
29
|
+
toolChoice?: string | object;
|
|
30
|
+
systemPrompt?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type SarvamChatOptions = LlmChatOptions;
|
|
34
|
+
|
|
35
|
+
export interface LlmChatResponse {
|
|
36
|
+
text: string;
|
|
37
|
+
reasoningContent?: string;
|
|
38
|
+
toolCalls?: ToolCall[];
|
|
39
|
+
model: string;
|
|
40
|
+
usage?: {
|
|
41
|
+
promptTokens: number;
|
|
42
|
+
completionTokens: number;
|
|
43
|
+
totalTokens: number;
|
|
44
|
+
};
|
|
45
|
+
audioBase64?: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface LlmTtsOptions {
|
|
49
|
+
text: string;
|
|
50
|
+
model?: string;
|
|
51
|
+
voice?: string;
|
|
52
|
+
language?: string;
|
|
53
|
+
apiKey?: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface LlmTtsResponse {
|
|
57
|
+
audioBuffer: Buffer;
|
|
58
|
+
contentType: string;
|
|
59
|
+
model: string;
|
|
60
|
+
voice: string;
|
|
61
|
+
byteLength: number;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { Client } from "@modelcontextprotocol/client";
|
|
2
|
+
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
|
|
3
|
+
import type { AgentTool } from "@/layers/agent/types";
|
|
4
|
+
import type { McpServerConfig } from "@/layers/tools/types";
|
|
5
|
+
|
|
6
|
+
export class MCP {
|
|
7
|
+
private static activeClients: Client[] = [];
|
|
8
|
+
private static activeTransports: any[] = [];
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Close all active MCP clients and transports, releasing stdio streams
|
|
12
|
+
*/
|
|
13
|
+
public static async closeAll(): Promise<void> {
|
|
14
|
+
for (const client of this.activeClients) {
|
|
15
|
+
try {
|
|
16
|
+
await client.close();
|
|
17
|
+
} catch {}
|
|
18
|
+
}
|
|
19
|
+
this.activeClients = [];
|
|
20
|
+
|
|
21
|
+
for (const transport of this.activeTransports) {
|
|
22
|
+
try {
|
|
23
|
+
if (typeof transport.close === "function") {
|
|
24
|
+
await transport.close();
|
|
25
|
+
}
|
|
26
|
+
const proc = (transport as any)._process || (transport as any).process;
|
|
27
|
+
if (proc && typeof proc.kill === "function" && !proc.killed) {
|
|
28
|
+
try {
|
|
29
|
+
proc.kill();
|
|
30
|
+
} catch {}
|
|
31
|
+
}
|
|
32
|
+
} catch {}
|
|
33
|
+
}
|
|
34
|
+
this.activeTransports = [];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Connects to an external MCP server (Stdio, SSE, or Streamable HTTP)
|
|
39
|
+
* and discovers all tools exposed by that server.
|
|
40
|
+
*/
|
|
41
|
+
public static async discoverTools(
|
|
42
|
+
config: McpServerConfig,
|
|
43
|
+
toolFilter?: string[]
|
|
44
|
+
): Promise<AgentTool[]> {
|
|
45
|
+
if (
|
|
46
|
+
config.transport === "http" ||
|
|
47
|
+
config.transport === "sse" ||
|
|
48
|
+
(config.url && (config.url.startsWith("http://") || config.url.startsWith("https://")))
|
|
49
|
+
) {
|
|
50
|
+
return this.discoverHttpTools(config, toolFilter);
|
|
51
|
+
}
|
|
52
|
+
return this.discoverStdioTools(config, toolFilter);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Discovers tools from a local MCP server over Stdio (e.g. uvx, bunx, npx, or custom executable).
|
|
57
|
+
* Uses official @modelcontextprotocol/client with full protocol negotiation.
|
|
58
|
+
*/
|
|
59
|
+
public static async discoverStdioTools(
|
|
60
|
+
config: McpServerConfig,
|
|
61
|
+
toolFilter?: string[]
|
|
62
|
+
): Promise<AgentTool[]> {
|
|
63
|
+
const filterSet = toolFilter && toolFilter.length > 0 ? new Set(toolFilter.map(t => t.toLowerCase())) : null;
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
const isWin = process.platform === "win32";
|
|
67
|
+
const containerSafeEnv: Record<string, string> = {
|
|
68
|
+
...(isWin ? {} : { HOME: process.env.HOME || "/tmp" }),
|
|
69
|
+
npm_config_cache: isWin ? (process.env.TEMP || "/tmp") : "/tmp/.npm",
|
|
70
|
+
npm_config_update_notifier: "false",
|
|
71
|
+
...process.env,
|
|
72
|
+
...(config.env || {})
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
let resolvedCommand = config.command || "npx";
|
|
76
|
+
if (isWin && (resolvedCommand === "npx" || resolvedCommand === "npm" || resolvedCommand === "pnpm")) {
|
|
77
|
+
resolvedCommand = `${resolvedCommand}.cmd`;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const transport = new StdioClientTransport({
|
|
81
|
+
command: resolvedCommand,
|
|
82
|
+
args: config.args || [],
|
|
83
|
+
env: containerSafeEnv
|
|
84
|
+
});
|
|
85
|
+
this.activeTransports.push(transport);
|
|
86
|
+
|
|
87
|
+
const safeJsonSchemaValidator = {
|
|
88
|
+
getValidator: () => (input: any) => ({ valid: true, data: input })
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const client = new Client(
|
|
92
|
+
{
|
|
93
|
+
name: `liate-${config.name}`,
|
|
94
|
+
version: config.version || "0.0.1"
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
capabilities: {},
|
|
98
|
+
jsonSchemaValidator: safeJsonSchemaValidator as any,
|
|
99
|
+
timeout: 60000
|
|
100
|
+
} as any
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
await client.connect(transport);
|
|
104
|
+
this.activeClients.push(client);
|
|
105
|
+
|
|
106
|
+
const toolsResult = await client.listTools(undefined, { timeout: 60000 });
|
|
107
|
+
const registeredTools: AgentTool[] = [];
|
|
108
|
+
|
|
109
|
+
for (const t of toolsResult.tools) {
|
|
110
|
+
if (filterSet && !filterSet.has(t.name.toLowerCase())) continue;
|
|
111
|
+
|
|
112
|
+
registeredTools.push({
|
|
113
|
+
name: t.name,
|
|
114
|
+
description: t.description || `Tool from ${config.name} MCP server`,
|
|
115
|
+
parameters: (t.inputSchema as Record<string, any>) || { type: "object", properties: {} },
|
|
116
|
+
execute: async (callArgs: Record<string, any>) => {
|
|
117
|
+
try {
|
|
118
|
+
const res = await client.callTool({
|
|
119
|
+
name: t.name,
|
|
120
|
+
arguments: callArgs
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
if (res && res.content && Array.isArray(res.content)) {
|
|
124
|
+
return res.content.map((c: any) => c.text ?? JSON.stringify(c)).join("\n");
|
|
125
|
+
}
|
|
126
|
+
return typeof res === "string" ? res : JSON.stringify(res);
|
|
127
|
+
} catch (err: any) {
|
|
128
|
+
return `Error calling MCP tool ${t.name}: ${err.message}`;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return registeredTools;
|
|
135
|
+
} catch (err: any) {
|
|
136
|
+
console.warn(`[MCP] Stdio discovery failed for ${config.name}: ${err.message}`);
|
|
137
|
+
return [];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Discovers tools from a remote MCP HTTP/SSE endpoint using zero-dependency native fetch.
|
|
143
|
+
* Sends JSON-RPC 2.0 tools/list and registers tools/call executors.
|
|
144
|
+
*/
|
|
145
|
+
public static async discoverHttpTools(
|
|
146
|
+
config: McpServerConfig,
|
|
147
|
+
toolFilter?: string[]
|
|
148
|
+
): Promise<AgentTool[]> {
|
|
149
|
+
const url = config.url!;
|
|
150
|
+
const filterSet = toolFilter && toolFilter.length > 0 ? new Set(toolFilter.map(t => t.toLowerCase())) : null;
|
|
151
|
+
const headers = {
|
|
152
|
+
"Content-Type": "application/json",
|
|
153
|
+
"Accept": "application/json, text/event-stream, */*",
|
|
154
|
+
...(config.headers || {})
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
function parseMcpPayload(rawText: string): any {
|
|
158
|
+
const trimmed = rawText.trim();
|
|
159
|
+
if (trimmed.includes("data:")) {
|
|
160
|
+
const lines = trimmed.split("\n");
|
|
161
|
+
for (const line of lines) {
|
|
162
|
+
const l = line.trim();
|
|
163
|
+
if (l.startsWith("data:")) {
|
|
164
|
+
try {
|
|
165
|
+
return JSON.parse(l.slice(5).trim());
|
|
166
|
+
} catch {}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return JSON.parse(trimmed);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
try {
|
|
174
|
+
const res = await fetch(url, {
|
|
175
|
+
method: "POST",
|
|
176
|
+
headers,
|
|
177
|
+
body: JSON.stringify({
|
|
178
|
+
jsonrpc: "2.0",
|
|
179
|
+
id: 1,
|
|
180
|
+
method: "tools/list",
|
|
181
|
+
params: {}
|
|
182
|
+
})
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
if (!res.ok) {
|
|
186
|
+
const errBody = await res.text().catch(() => "");
|
|
187
|
+
console.warn(`[MCP] Remote HTTP server '${config.name}' returned status ${res.status}: ${errBody || res.statusText}`);
|
|
188
|
+
return [];
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const resText = await res.text();
|
|
192
|
+
const data: any = parseMcpPayload(resText);
|
|
193
|
+
const rawTools = data.result?.tools || [];
|
|
194
|
+
const registeredTools: AgentTool[] = [];
|
|
195
|
+
|
|
196
|
+
for (const t of rawTools) {
|
|
197
|
+
if (filterSet && !filterSet.has(t.name.toLowerCase())) continue;
|
|
198
|
+
|
|
199
|
+
registeredTools.push({
|
|
200
|
+
name: t.name,
|
|
201
|
+
description: t.description || `HTTP MCP tool ${t.name}`,
|
|
202
|
+
parameters: t.inputSchema || { type: "object", properties: {} },
|
|
203
|
+
execute: async (args: Record<string, any>) => {
|
|
204
|
+
try {
|
|
205
|
+
const callRes = await fetch(url, {
|
|
206
|
+
method: "POST",
|
|
207
|
+
headers,
|
|
208
|
+
body: JSON.stringify({
|
|
209
|
+
jsonrpc: "2.0",
|
|
210
|
+
id: Date.now(),
|
|
211
|
+
method: "tools/call",
|
|
212
|
+
params: { name: t.name, arguments: args }
|
|
213
|
+
})
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
const callText = await callRes.text();
|
|
217
|
+
const callData: any = parseMcpPayload(callText);
|
|
218
|
+
const content = callData.result?.content;
|
|
219
|
+
if (Array.isArray(content)) {
|
|
220
|
+
return content.map((c: any) => c.text ?? JSON.stringify(c)).join("\n");
|
|
221
|
+
}
|
|
222
|
+
return typeof callData.result === "string" ? callData.result : JSON.stringify(callData.result || callData);
|
|
223
|
+
} catch (err: any) {
|
|
224
|
+
return `Error calling HTTP MCP tool ${t.name}: ${err.message}`;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return registeredTools;
|
|
231
|
+
} catch (err: any) {
|
|
232
|
+
console.warn(`[MCP] HTTP discovery failed for ${config.name}: ${err.message}`);
|
|
233
|
+
return [];
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import type { AgentTool } from "@/layers/agent/types";
|
|
4
|
+
import type { McpServerConfig, ParsedToolSpec } from "@/layers/tools/types";
|
|
5
|
+
import { MCP } from "@/layers/tools/mcp";
|
|
6
|
+
|
|
7
|
+
export class ToolRegistry {
|
|
8
|
+
public static parseSpec(spec: string): ParsedToolSpec {
|
|
9
|
+
const trimmed = spec.trim();
|
|
10
|
+
|
|
11
|
+
if (trimmed.toLowerCase() === "fetch" || trimmed.toLowerCase() === "mcp:fetch" || trimmed.toLowerCase() === "mcp/fetch") {
|
|
12
|
+
return {
|
|
13
|
+
raw: trimmed,
|
|
14
|
+
isMcp: true,
|
|
15
|
+
serverName: "fetch"
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (trimmed.startsWith("mcp:")) {
|
|
20
|
+
const remainder = trimmed.slice(4); // Remove "mcp:"
|
|
21
|
+
const colonIndex = remainder.indexOf(":");
|
|
22
|
+
|
|
23
|
+
if (colonIndex === -1) {
|
|
24
|
+
// e.g. "mcp:agentmail" -> all tools
|
|
25
|
+
return {
|
|
26
|
+
raw: trimmed,
|
|
27
|
+
isMcp: true,
|
|
28
|
+
isBuiltin: false,
|
|
29
|
+
serverName: remainder.trim(),
|
|
30
|
+
selectedTools: undefined
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// e.g. "mcp:agentmail:send_email,read_inbox"
|
|
35
|
+
const serverName = remainder.slice(0, colonIndex).trim();
|
|
36
|
+
const toolsPart = remainder.slice(colonIndex + 1).trim();
|
|
37
|
+
const selectedTools = toolsPart ? toolsPart.split(",").map(s => s.trim()).filter(Boolean) : undefined;
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
raw: trimmed,
|
|
41
|
+
isMcp: true,
|
|
42
|
+
isBuiltin: false,
|
|
43
|
+
serverName,
|
|
44
|
+
selectedTools
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Check if it's "serverName:tool1,tool2"
|
|
49
|
+
if (trimmed.includes(":")) {
|
|
50
|
+
const [serverName, toolsPart] = trimmed.split(":");
|
|
51
|
+
const selectedTools = toolsPart ? toolsPart.split(",").map(s => s.trim()).filter(Boolean) : undefined;
|
|
52
|
+
return {
|
|
53
|
+
raw: trimmed,
|
|
54
|
+
isMcp: true,
|
|
55
|
+
isBuiltin: false,
|
|
56
|
+
serverName: serverName.trim(),
|
|
57
|
+
selectedTools
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Default to an MCP server name (e.g. "agentmail")
|
|
62
|
+
return {
|
|
63
|
+
raw: trimmed,
|
|
64
|
+
isMcp: true,
|
|
65
|
+
isBuiltin: false,
|
|
66
|
+
serverName: trimmed,
|
|
67
|
+
selectedTools: undefined
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Resolve local workspace .liate/mcp.json path
|
|
73
|
+
*/
|
|
74
|
+
public static getMcpJsonPath(): string {
|
|
75
|
+
const candidates = [
|
|
76
|
+
path.resolve(process.cwd(), ".liate", "mcp.json"),
|
|
77
|
+
path.resolve(process.cwd(), "..", ".liate", "mcp.json")
|
|
78
|
+
];
|
|
79
|
+
for (const p of candidates) {
|
|
80
|
+
if (fs.existsSync(p)) return p;
|
|
81
|
+
}
|
|
82
|
+
return path.resolve(process.cwd(), ".liate", "mcp.json");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Reads .liate/mcp.json if available
|
|
87
|
+
*/
|
|
88
|
+
public static readWorkspaceMcpServers(): Record<string, any> {
|
|
89
|
+
const mcpJsonPath = ToolRegistry.getMcpJsonPath();
|
|
90
|
+
if (fs.existsSync(mcpJsonPath)) {
|
|
91
|
+
try {
|
|
92
|
+
const raw = fs.readFileSync(mcpJsonPath, "utf-8");
|
|
93
|
+
const parsed = JSON.parse(raw);
|
|
94
|
+
return parsed.mcpServers || parsed || {};
|
|
95
|
+
} catch {}
|
|
96
|
+
}
|
|
97
|
+
return {};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Caches/writes an MCP server definition into .liate/mcp.json
|
|
102
|
+
*/
|
|
103
|
+
public static cacheMcpServer(serverName: string, config: McpServerConfig): void {
|
|
104
|
+
try {
|
|
105
|
+
const mcpJsonPath = ToolRegistry.getMcpJsonPath();
|
|
106
|
+
const dir = path.dirname(mcpJsonPath);
|
|
107
|
+
if (!fs.existsSync(dir)) {
|
|
108
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
let data: { mcpServers: Record<string, any> } = { mcpServers: {} };
|
|
112
|
+
if (fs.existsSync(mcpJsonPath)) {
|
|
113
|
+
try {
|
|
114
|
+
const raw = fs.readFileSync(mcpJsonPath, "utf-8");
|
|
115
|
+
const parsed = JSON.parse(raw);
|
|
116
|
+
data.mcpServers = parsed.mcpServers || parsed || {};
|
|
117
|
+
} catch {}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
data.mcpServers[serverName] = {
|
|
121
|
+
name: config.name || serverName,
|
|
122
|
+
transport: config.transport || (config.url ? "http" : "stdio"),
|
|
123
|
+
...(config.command ? { command: config.command } : {}),
|
|
124
|
+
...(config.args ? { args: config.args } : {}),
|
|
125
|
+
...(config.url ? { url: config.url } : {}),
|
|
126
|
+
...(config.headers ? { headers: config.headers } : {}),
|
|
127
|
+
...(config.env ? { env: config.env } : {})
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
fs.writeFileSync(mcpJsonPath, JSON.stringify(data, null, 2), "utf-8");
|
|
131
|
+
} catch {}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
public static loadMcpConfig(serverName: string, customEnv: Record<string, string | undefined> = process.env): McpServerConfig {
|
|
135
|
+
let parsed: McpServerConfig | null = null;
|
|
136
|
+
|
|
137
|
+
// Check workspace .liate/mcp.json (local cache & user-registered servers)
|
|
138
|
+
const workspaceServers = ToolRegistry.readWorkspaceMcpServers();
|
|
139
|
+
if (workspaceServers && workspaceServers[serverName]) {
|
|
140
|
+
const s = workspaceServers[serverName];
|
|
141
|
+
parsed = {
|
|
142
|
+
id: serverName,
|
|
143
|
+
name: s.name || serverName,
|
|
144
|
+
description: s.description,
|
|
145
|
+
transport: s.transport || (s.url ? "http" : "stdio"),
|
|
146
|
+
command: s.command,
|
|
147
|
+
args: s.args,
|
|
148
|
+
url: s.url,
|
|
149
|
+
headers: s.headers,
|
|
150
|
+
env: s.env
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (!parsed) {
|
|
155
|
+
throw new Error(`MCP server '${serverName}' not found. Define it directly inline in Layer [T] (e.g. { url: "...", transport: "http" }) or register it in .liate/mcp.json.`);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Resolve environment variables in headers and URL
|
|
159
|
+
const resolvedHeaders: Record<string, string> = {};
|
|
160
|
+
for (const [k, v] of Object.entries(parsed.headers || {})) {
|
|
161
|
+
if (typeof v === "string" && v.startsWith("$")) {
|
|
162
|
+
const envName = v.slice(1);
|
|
163
|
+
resolvedHeaders[k] = customEnv[envName] || "";
|
|
164
|
+
} else if (typeof v === "string" && v.includes("$")) {
|
|
165
|
+
resolvedHeaders[k] = v.replace(/\$([A-Z0-9_]+)/g, (_, name) => customEnv[name] || "");
|
|
166
|
+
} else {
|
|
167
|
+
resolvedHeaders[k] = String(v);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
let resolvedUrl = parsed.url;
|
|
172
|
+
if (resolvedUrl && resolvedUrl.includes("$")) {
|
|
173
|
+
resolvedUrl = resolvedUrl.replace(/\$([A-Z0-9_]+)/g, (_, name) => customEnv[name] || "");
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
...parsed,
|
|
178
|
+
url: resolvedUrl,
|
|
179
|
+
headers: resolvedHeaders
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
public static async resolveTools(
|
|
184
|
+
specs: Array<string | AgentTool | Record<string, any>> | Record<string, any>,
|
|
185
|
+
customEnv: Record<string, string | undefined> = process.env
|
|
186
|
+
): Promise<AgentTool[]> {
|
|
187
|
+
const resolved: AgentTool[] = [];
|
|
188
|
+
let rawList: Array<string | AgentTool | Record<string, any>> = [];
|
|
189
|
+
|
|
190
|
+
if (Array.isArray(specs)) {
|
|
191
|
+
rawList = specs;
|
|
192
|
+
} else if (typeof specs === "object" && specs !== null) {
|
|
193
|
+
if ("command" in specs || "url" in specs) {
|
|
194
|
+
rawList = [{ id: "inline_tool", name: "inline_tool", ...specs }];
|
|
195
|
+
} else {
|
|
196
|
+
rawList = Object.entries(specs).map(([key, val]) => {
|
|
197
|
+
if (typeof val === "object" && val !== null) {
|
|
198
|
+
return { id: key, name: key, ...val };
|
|
199
|
+
}
|
|
200
|
+
return key;
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
for (const item of rawList) {
|
|
206
|
+
if (typeof item === "object" && item !== null) {
|
|
207
|
+
// Direct AgentTool object (has name & execute function)
|
|
208
|
+
if ("name" in item && "execute" in item && typeof (item as any).execute === "function") {
|
|
209
|
+
resolved.push(item as AgentTool);
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Inline MCP Config Object (e.g. { command: "uvx", args: ["mcp-server-time"], tools: ["*"] })
|
|
214
|
+
if ("command" in item || "url" in item) {
|
|
215
|
+
const rawArgs = (item as any).args;
|
|
216
|
+
const resolvedArgs = Array.isArray(rawArgs)
|
|
217
|
+
? rawArgs.map(arg => typeof arg === "string" ? arg.replace(/\$([A-Z0-9_]+)/g, (_, name) => customEnv[name] || process.env[name] || "") : arg)
|
|
218
|
+
: undefined;
|
|
219
|
+
|
|
220
|
+
const rawEnv = (item as any).env;
|
|
221
|
+
const resolvedEnv: Record<string, string> = {};
|
|
222
|
+
if (rawEnv && typeof rawEnv === "object") {
|
|
223
|
+
for (const [k, v] of Object.entries(rawEnv)) {
|
|
224
|
+
resolvedEnv[k] = typeof v === "string"
|
|
225
|
+
? v.replace(/\$([A-Z0-9_]+)/g, (_, name) => customEnv[name] || process.env[name] || "")
|
|
226
|
+
: String(v);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
let resolvedUrl = (item as any).url;
|
|
231
|
+
if (resolvedUrl && typeof resolvedUrl === "string" && resolvedUrl.includes("$")) {
|
|
232
|
+
resolvedUrl = resolvedUrl.replace(/\$([A-Z0-9_]+)/g, (_, name) => customEnv[name] || process.env[name] || "");
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const rawHeaders = (item as any).headers;
|
|
236
|
+
const resolvedHeaders: Record<string, string> = {};
|
|
237
|
+
if (rawHeaders && typeof rawHeaders === "object") {
|
|
238
|
+
for (const [k, v] of Object.entries(rawHeaders)) {
|
|
239
|
+
resolvedHeaders[k] = typeof v === "string"
|
|
240
|
+
? v.replace(/\$([A-Z0-9_]+)/g, (_, name) => customEnv[name] || process.env[name] || "")
|
|
241
|
+
: String(v);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const inlineConfig: McpServerConfig = {
|
|
246
|
+
id: (item as any).id || (item as any).name || "inline_mcp",
|
|
247
|
+
name: (item as any).name || (item as any).id || "inline_mcp",
|
|
248
|
+
transport: (item as any).transport || ((item as any).url ? "http" : "stdio"),
|
|
249
|
+
command: (item as any).command,
|
|
250
|
+
args: resolvedArgs,
|
|
251
|
+
url: resolvedUrl,
|
|
252
|
+
env: Object.keys(resolvedEnv).length > 0 ? resolvedEnv : undefined,
|
|
253
|
+
headers: Object.keys(resolvedHeaders).length > 0 ? resolvedHeaders : undefined
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
const rawFilter = (item as any).tools || (item as any).selectedTools;
|
|
257
|
+
const toolFilter: string[] | undefined = Array.isArray(rawFilter)
|
|
258
|
+
? (rawFilter.includes("*") ? undefined : rawFilter)
|
|
259
|
+
: typeof rawFilter === "string" && rawFilter !== "*"
|
|
260
|
+
? rawFilter.split(",").map((s: string) => s.trim()).filter(Boolean)
|
|
261
|
+
: undefined;
|
|
262
|
+
|
|
263
|
+
try {
|
|
264
|
+
const tools = await MCP.discoverTools(inlineConfig, toolFilter);
|
|
265
|
+
resolved.push(...tools);
|
|
266
|
+
} catch (err: any) {
|
|
267
|
+
console.warn(`[ToolRegistry] Failed to resolve inline MCP config '${inlineConfig.name}': ${err.message}`);
|
|
268
|
+
}
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (typeof item === "string") {
|
|
274
|
+
const trimmed = item.trim();
|
|
275
|
+
const parsed = this.parseSpec(trimmed);
|
|
276
|
+
|
|
277
|
+
if (trimmed.toLowerCase() === "fetch" || trimmed.toLowerCase() === "mcp:fetch" || trimmed.toLowerCase() === "mcp/fetch") {
|
|
278
|
+
try {
|
|
279
|
+
const tools = await MCP.discoverTools({
|
|
280
|
+
id: "fetch",
|
|
281
|
+
name: "fetch",
|
|
282
|
+
transport: "stdio",
|
|
283
|
+
command: "uvx",
|
|
284
|
+
args: ["mcp-server-fetch"]
|
|
285
|
+
}, parsed.selectedTools);
|
|
286
|
+
resolved.push(...tools);
|
|
287
|
+
} catch (err: any) {
|
|
288
|
+
console.warn(`[ToolRegistry] Failed to resolve uvx mcp-server-fetch: ${err.message}`);
|
|
289
|
+
}
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (parsed.isMcp && parsed.serverName) {
|
|
294
|
+
try {
|
|
295
|
+
const config = this.loadMcpConfig(parsed.serverName, customEnv);
|
|
296
|
+
const tools = await MCP.discoverTools(config, parsed.selectedTools);
|
|
297
|
+
resolved.push(...tools);
|
|
298
|
+
} catch (err: any) {
|
|
299
|
+
console.warn(`[ToolRegistry] Failed to resolve MCP server '${parsed.serverName}': ${err.message}`);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return resolved;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { McpServer } from "@modelcontextprotocol/server";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
export interface McpAgentLike {
|
|
5
|
+
name: string;
|
|
6
|
+
run(prompt: string): Promise<{ text: string }>;
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Liate MCP Server Exposer [Layer T]
|
|
12
|
+
* Exposes a Liate Agent as a standard Model Context Protocol (MCP) server.
|
|
13
|
+
*/
|
|
14
|
+
export function createAgentMcpServer(agent: McpAgentLike): McpServer {
|
|
15
|
+
const agentName = agent.name || "Agent";
|
|
16
|
+
const agentVersion = (agent as any).version || "0.0.1";
|
|
17
|
+
const server = new McpServer({
|
|
18
|
+
name: `liate-${agentName.toLowerCase()}`,
|
|
19
|
+
version: agentVersion
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
// Tool 1: Run Agent
|
|
23
|
+
(server as any).registerTool(
|
|
24
|
+
`${agentName.toLowerCase()}_run`,
|
|
25
|
+
{
|
|
26
|
+
description: `Execute cognitive reasoning using the ${agentName} agent.`,
|
|
27
|
+
inputSchema: {
|
|
28
|
+
prompt: z.string().describe("The prompt or instruction to execute")
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
async ({ prompt }: { prompt: string }) => {
|
|
32
|
+
const result = await agent.run(prompt);
|
|
33
|
+
return {
|
|
34
|
+
content: [{ type: "text", text: result.text }]
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
// Tool 2: Agent Health & Status
|
|
40
|
+
(server as any).registerTool(
|
|
41
|
+
`${agentName.toLowerCase()}_health`,
|
|
42
|
+
{
|
|
43
|
+
description: `Check the status of the ${agentName} agent.`,
|
|
44
|
+
inputSchema: {}
|
|
45
|
+
},
|
|
46
|
+
async () => {
|
|
47
|
+
return {
|
|
48
|
+
content: [
|
|
49
|
+
{
|
|
50
|
+
type: "text",
|
|
51
|
+
text: JSON.stringify(
|
|
52
|
+
{
|
|
53
|
+
status: "ok",
|
|
54
|
+
agent: agentName,
|
|
55
|
+
protocol: "Liate API v1",
|
|
56
|
+
doctrine: "Where a Class is an Agent. Built for Sovereign AI Agents From India to the World."
|
|
57
|
+
},
|
|
58
|
+
null,
|
|
59
|
+
2
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
]
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
return server;
|
|
68
|
+
}
|
|
69
|
+
|