indusagi 0.12.13 → 0.12.15
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/dist/agent/tools/registry.d.ts +1 -1
- package/dist/agent/tools/registry.d.ts.map +1 -1
- package/dist/agent/types.d.ts +1 -0
- package/dist/agent/types.d.ts.map +1 -1
- package/dist/agent/types.js.map +1 -1
- package/dist/ai/models.generated.d.ts +621 -53
- package/dist/ai/models.generated.d.ts.map +1 -1
- package/dist/ai/models.generated.js +548 -0
- package/dist/ai/models.generated.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/mcp/client-pool.d.ts +106 -0
- package/dist/mcp/client-pool.d.ts.map +1 -0
- package/dist/mcp/client-pool.js +233 -0
- package/dist/mcp/client-pool.js.map +1 -0
- package/dist/mcp/client.d.ts +158 -0
- package/dist/mcp/client.d.ts.map +1 -0
- package/dist/mcp/client.js +586 -0
- package/dist/mcp/client.js.map +1 -0
- package/dist/mcp/config.d.ts +61 -0
- package/dist/mcp/config.d.ts.map +1 -0
- package/dist/mcp/config.js +250 -0
- package/dist/mcp/config.js.map +1 -0
- package/dist/mcp/errors.d.ts +104 -0
- package/dist/mcp/errors.d.ts.map +1 -0
- package/dist/mcp/errors.js +138 -0
- package/dist/mcp/errors.js.map +1 -0
- package/dist/mcp/index.d.ts +56 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +83 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/schema-converter.d.ts +68 -0
- package/dist/mcp/schema-converter.d.ts.map +1 -0
- package/dist/mcp/schema-converter.js +230 -0
- package/dist/mcp/schema-converter.js.map +1 -0
- package/dist/mcp/server.d.ts +111 -0
- package/dist/mcp/server.d.ts.map +1 -0
- package/dist/mcp/server.js +300 -0
- package/dist/mcp/server.js.map +1 -0
- package/dist/mcp/tool-factory.d.ts +63 -0
- package/dist/mcp/tool-factory.d.ts.map +1 -0
- package/dist/mcp/tool-factory.js +228 -0
- package/dist/mcp/tool-factory.js.map +1 -0
- package/dist/mcp/types.d.ts +289 -0
- package/dist/mcp/types.d.ts.map +1 -0
- package/dist/mcp/types.js +8 -0
- package/dist/mcp/types.js.map +1 -0
- package/dist/mcp.d.ts +6 -0
- package/dist/mcp.d.ts.map +1 -0
- package/dist/mcp.js +6 -0
- package/dist/mcp.js.map +1 -0
- package/dist/memory/embedder/base.d.ts +41 -0
- package/dist/memory/embedder/base.d.ts.map +1 -0
- package/dist/memory/embedder/base.js +10 -0
- package/dist/memory/embedder/base.js.map +1 -0
- package/dist/memory/embedder/index.d.ts +8 -0
- package/dist/memory/embedder/index.d.ts.map +1 -0
- package/dist/memory/embedder/index.js +6 -0
- package/dist/memory/embedder/index.js.map +1 -0
- package/dist/memory/embedder/openai.d.ts +35 -0
- package/dist/memory/embedder/openai.d.ts.map +1 -0
- package/dist/memory/embedder/openai.js +103 -0
- package/dist/memory/embedder/openai.js.map +1 -0
- package/dist/memory/index.d.ts +33 -0
- package/dist/memory/index.d.ts.map +1 -0
- package/dist/memory/index.js +31 -0
- package/dist/memory/index.js.map +1 -0
- package/dist/memory/memory.d.ts +126 -0
- package/dist/memory/memory.d.ts.map +1 -0
- package/dist/memory/memory.js +280 -0
- package/dist/memory/memory.js.map +1 -0
- package/dist/memory/processors/base.d.ts +42 -0
- package/dist/memory/processors/base.d.ts.map +1 -0
- package/dist/memory/processors/base.js +6 -0
- package/dist/memory/processors/base.js.map +1 -0
- package/dist/memory/processors/index.d.ts +16 -0
- package/dist/memory/processors/index.d.ts.map +1 -0
- package/dist/memory/processors/index.js +18 -0
- package/dist/memory/processors/index.js.map +1 -0
- package/dist/memory/processors/message-history.d.ts +35 -0
- package/dist/memory/processors/message-history.d.ts.map +1 -0
- package/dist/memory/processors/message-history.js +51 -0
- package/dist/memory/processors/message-history.js.map +1 -0
- package/dist/memory/processors/observational-memory/index.d.ts +82 -0
- package/dist/memory/processors/observational-memory/index.d.ts.map +1 -0
- package/dist/memory/processors/observational-memory/index.js +234 -0
- package/dist/memory/processors/observational-memory/index.js.map +1 -0
- package/dist/memory/processors/observational-memory/observer-agent.d.ts +64 -0
- package/dist/memory/processors/observational-memory/observer-agent.d.ts.map +1 -0
- package/dist/memory/processors/observational-memory/observer-agent.js +362 -0
- package/dist/memory/processors/observational-memory/observer-agent.js.map +1 -0
- package/dist/memory/processors/observational-memory/reflector-agent.d.ts +38 -0
- package/dist/memory/processors/observational-memory/reflector-agent.d.ts.map +1 -0
- package/dist/memory/processors/observational-memory/reflector-agent.js +213 -0
- package/dist/memory/processors/observational-memory/reflector-agent.js.map +1 -0
- package/dist/memory/processors/observational-memory/token-counter.d.ts +35 -0
- package/dist/memory/processors/observational-memory/token-counter.d.ts.map +1 -0
- package/dist/memory/processors/observational-memory/token-counter.js +90 -0
- package/dist/memory/processors/observational-memory/token-counter.js.map +1 -0
- package/dist/memory/processors/semantic-recall.d.ts +55 -0
- package/dist/memory/processors/semantic-recall.d.ts.map +1 -0
- package/dist/memory/processors/semantic-recall.js +143 -0
- package/dist/memory/processors/semantic-recall.js.map +1 -0
- package/dist/memory/processors/working-memory.d.ts +41 -0
- package/dist/memory/processors/working-memory.d.ts.map +1 -0
- package/dist/memory/processors/working-memory.js +82 -0
- package/dist/memory/processors/working-memory.js.map +1 -0
- package/dist/memory/storage/base.d.ts +288 -0
- package/dist/memory/storage/base.d.ts.map +1 -0
- package/dist/memory/storage/base.js +211 -0
- package/dist/memory/storage/base.js.map +1 -0
- package/dist/memory/storage/index.d.ts +9 -0
- package/dist/memory/storage/index.d.ts.map +1 -0
- package/dist/memory/storage/index.js +7 -0
- package/dist/memory/storage/index.js.map +1 -0
- package/dist/memory/storage/inmemory.d.ts +93 -0
- package/dist/memory/storage/inmemory.d.ts.map +1 -0
- package/dist/memory/storage/inmemory.js +646 -0
- package/dist/memory/storage/inmemory.js.map +1 -0
- package/dist/memory/tools/working-memory.d.ts +100 -0
- package/dist/memory/tools/working-memory.d.ts.map +1 -0
- package/dist/memory/tools/working-memory.js +237 -0
- package/dist/memory/tools/working-memory.js.map +1 -0
- package/dist/memory/types.d.ts +386 -0
- package/dist/memory/types.d.ts.map +1 -0
- package/dist/memory/types.js +58 -0
- package/dist/memory/types.js.map +1 -0
- package/dist/memory/vector/base.d.ts +145 -0
- package/dist/memory/vector/base.d.ts.map +1 -0
- package/dist/memory/vector/base.js +83 -0
- package/dist/memory/vector/base.js.map +1 -0
- package/dist/memory/vector/index.d.ts +8 -0
- package/dist/memory/vector/index.d.ts.map +1 -0
- package/dist/memory/vector/index.js +7 -0
- package/dist/memory/vector/index.js.map +1 -0
- package/dist/memory/vector/inmemory.d.ts +47 -0
- package/dist/memory/vector/inmemory.d.ts.map +1 -0
- package/dist/memory/vector/inmemory.js +234 -0
- package/dist/memory/vector/inmemory.js.map +1 -0
- package/package.json +32 -2
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Server
|
|
3
|
+
*
|
|
4
|
+
* Exposes new_indusagi tools as an MCP server.
|
|
5
|
+
* Compatible with Claude Desktop, Cursor, etc.
|
|
6
|
+
*
|
|
7
|
+
* Reference: @mastra/mcp packages/mcp/src/server/server.ts MCPServer
|
|
8
|
+
*/
|
|
9
|
+
import { MCPError, MCPErrorCode } from "./errors.js";
|
|
10
|
+
/**
|
|
11
|
+
* MCP Server - exposes new_indusagi tools via the Model Context Protocol.
|
|
12
|
+
*
|
|
13
|
+
* Supports stdio transport for subprocess communication.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { MCPServer } from "new_indusagi/mcp";
|
|
18
|
+
* import { createBashTool } from "new_indusagi/agent/tools";
|
|
19
|
+
*
|
|
20
|
+
* const server = new MCPServer({
|
|
21
|
+
* name: "My Tools Server",
|
|
22
|
+
* version: "1.0.0",
|
|
23
|
+
* tools: [createBashTool()],
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* await server.startStdio();
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export class MCPServer {
|
|
30
|
+
constructor(options) {
|
|
31
|
+
this.options = options;
|
|
32
|
+
this.tools = new Map();
|
|
33
|
+
this.buffer = "";
|
|
34
|
+
this.isConnected = false;
|
|
35
|
+
this.name = options.name;
|
|
36
|
+
this.version = options.version || "1.0.0";
|
|
37
|
+
// Convert and index tools
|
|
38
|
+
for (const tool of options.tools) {
|
|
39
|
+
const converted = this.convertTool(tool);
|
|
40
|
+
this.tools.set(converted.name, converted);
|
|
41
|
+
}
|
|
42
|
+
console.error(`[MCP Server] Initialized "${this.name}" v${this.version} with ${this.tools.size} tools`);
|
|
43
|
+
}
|
|
44
|
+
// ========================================================================
|
|
45
|
+
// Lifecycle
|
|
46
|
+
// ========================================================================
|
|
47
|
+
/**
|
|
48
|
+
* Start the server using stdio transport.
|
|
49
|
+
*
|
|
50
|
+
* This is typically used when running the server as a subprocess
|
|
51
|
+
* that MCP clients (like Claude Desktop) spawn.
|
|
52
|
+
*/
|
|
53
|
+
async startStdio() {
|
|
54
|
+
this.isConnected = true;
|
|
55
|
+
// Log to stderr so it doesn't interfere with MCP protocol on stdout
|
|
56
|
+
console.error(`[MCP Server] Starting stdio transport`);
|
|
57
|
+
// Set up stdin for reading
|
|
58
|
+
process.stdin.setEncoding("utf8");
|
|
59
|
+
process.stdin.on("data", (chunk) => {
|
|
60
|
+
this.buffer += chunk;
|
|
61
|
+
this.processBuffer();
|
|
62
|
+
});
|
|
63
|
+
process.stdin.on("end", () => {
|
|
64
|
+
console.error(`[MCP Server] Stdin closed, shutting down`);
|
|
65
|
+
this.isConnected = false;
|
|
66
|
+
process.exit(0);
|
|
67
|
+
});
|
|
68
|
+
// Keep the process alive
|
|
69
|
+
return new Promise(() => { });
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Stop the server.
|
|
73
|
+
*/
|
|
74
|
+
async stop() {
|
|
75
|
+
this.isConnected = false;
|
|
76
|
+
console.error(`[MCP Server] Stopped`);
|
|
77
|
+
}
|
|
78
|
+
// ========================================================================
|
|
79
|
+
// Tool Management
|
|
80
|
+
// ========================================================================
|
|
81
|
+
/**
|
|
82
|
+
* Add a tool to the server.
|
|
83
|
+
*/
|
|
84
|
+
addTool(tool) {
|
|
85
|
+
const converted = this.convertTool(tool);
|
|
86
|
+
this.tools.set(converted.name, converted);
|
|
87
|
+
console.error(`[MCP Server] Added tool: ${converted.name}`);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Remove a tool from the server.
|
|
91
|
+
*/
|
|
92
|
+
removeTool(name) {
|
|
93
|
+
const result = this.tools.delete(name);
|
|
94
|
+
if (result) {
|
|
95
|
+
console.error(`[MCP Server] Removed tool: ${name}`);
|
|
96
|
+
}
|
|
97
|
+
return result;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* List all available tools.
|
|
101
|
+
*/
|
|
102
|
+
listTools() {
|
|
103
|
+
return Array.from(this.tools.values());
|
|
104
|
+
}
|
|
105
|
+
// ========================================================================
|
|
106
|
+
// Private Methods
|
|
107
|
+
// ========================================================================
|
|
108
|
+
convertTool(tool) {
|
|
109
|
+
// Convert TypeBox schema to JSON Schema
|
|
110
|
+
const inputSchema = this.typeBoxToJsonSchema(tool.parameters);
|
|
111
|
+
return {
|
|
112
|
+
name: tool.name,
|
|
113
|
+
description: tool.description || tool.label,
|
|
114
|
+
inputSchema,
|
|
115
|
+
execute: async (args) => {
|
|
116
|
+
const result = await tool.execute(`mcp_${Date.now()}`, args, undefined, undefined);
|
|
117
|
+
return result;
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
typeBoxToJsonSchema(schema) {
|
|
122
|
+
// TypeBox schemas have a structure that can be converted to JSON Schema
|
|
123
|
+
// This is a simplified conversion - full implementation would traverse the schema
|
|
124
|
+
if (!schema) {
|
|
125
|
+
return { type: "object", properties: {} };
|
|
126
|
+
}
|
|
127
|
+
// If it already looks like a JSON Schema, return as-is
|
|
128
|
+
if (schema.type || schema.properties || schema.$schema) {
|
|
129
|
+
return schema;
|
|
130
|
+
}
|
|
131
|
+
// Try to extract JSON Schema from TypeBox
|
|
132
|
+
// TypeBox stores the schema in various ways depending on the version
|
|
133
|
+
const jsonSchema = schema.toJSONSchema?.() || schema;
|
|
134
|
+
return {
|
|
135
|
+
type: "object",
|
|
136
|
+
properties: jsonSchema.properties || {},
|
|
137
|
+
required: jsonSchema.required || [],
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
processBuffer() {
|
|
141
|
+
const lines = this.buffer.split("\n");
|
|
142
|
+
this.buffer = lines.pop() || "";
|
|
143
|
+
for (const line of lines) {
|
|
144
|
+
const trimmed = line.trim();
|
|
145
|
+
if (!trimmed)
|
|
146
|
+
continue;
|
|
147
|
+
try {
|
|
148
|
+
const message = JSON.parse(trimmed);
|
|
149
|
+
this.handleMessage(message);
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
console.error(`[MCP Server] Failed to parse message: ${trimmed}`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async handleMessage(message) {
|
|
157
|
+
console.error(`[MCP Server] Received: ${message.method || `response to ${message.id}`}`);
|
|
158
|
+
try {
|
|
159
|
+
// Handle requests
|
|
160
|
+
if (message.method) {
|
|
161
|
+
const response = await this.handleRequest(message);
|
|
162
|
+
this.sendResponse(message.id, response);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
console.error(`[MCP Server] Error handling message:`, error);
|
|
167
|
+
if (message.id !== undefined) {
|
|
168
|
+
this.sendError(message.id, error);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
async handleRequest(message) {
|
|
173
|
+
switch (message.method) {
|
|
174
|
+
case "initialize":
|
|
175
|
+
return this.handleInitialize(message.params);
|
|
176
|
+
case "tools/list":
|
|
177
|
+
return this.handleToolsList();
|
|
178
|
+
case "tools/call":
|
|
179
|
+
return this.handleToolsCall(message.params);
|
|
180
|
+
case "resources/list":
|
|
181
|
+
return { resources: [] };
|
|
182
|
+
case "prompts/list":
|
|
183
|
+
return { prompts: [] };
|
|
184
|
+
default:
|
|
185
|
+
throw new MCPError(`Unknown method: ${message.method}`, MCPErrorCode.PROTOCOL_ERROR);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
handleInitialize(params) {
|
|
189
|
+
console.error(`[MCP Server] Initialize from client: ${params?.clientInfo?.name || "unknown"}`);
|
|
190
|
+
return {
|
|
191
|
+
protocolVersion: "2024-11-05",
|
|
192
|
+
serverInfo: {
|
|
193
|
+
name: this.name,
|
|
194
|
+
version: this.version,
|
|
195
|
+
},
|
|
196
|
+
capabilities: {
|
|
197
|
+
tools: {},
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
handleToolsList() {
|
|
202
|
+
const tools = Array.from(this.tools.values()).map((tool) => ({
|
|
203
|
+
name: tool.name,
|
|
204
|
+
description: tool.description,
|
|
205
|
+
inputSchema: tool.inputSchema,
|
|
206
|
+
}));
|
|
207
|
+
return { tools };
|
|
208
|
+
}
|
|
209
|
+
async handleToolsCall(params) {
|
|
210
|
+
const { name, arguments: args } = params;
|
|
211
|
+
const tool = this.tools.get(name);
|
|
212
|
+
if (!tool) {
|
|
213
|
+
return {
|
|
214
|
+
content: [{ type: "text", text: `Unknown tool: ${name}` }],
|
|
215
|
+
isError: true,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
try {
|
|
219
|
+
console.error(`[MCP Server] Executing tool: ${name}`);
|
|
220
|
+
const result = await tool.execute(args || {});
|
|
221
|
+
// Convert AgentToolResult to MCP result
|
|
222
|
+
if (result && typeof result === "object" && "content" in result) {
|
|
223
|
+
const agentResult = result;
|
|
224
|
+
const content = [];
|
|
225
|
+
for (const item of agentResult.content || []) {
|
|
226
|
+
if (item.type === "text") {
|
|
227
|
+
content.push({ type: "text", text: item.text });
|
|
228
|
+
}
|
|
229
|
+
else if (item.type === "image") {
|
|
230
|
+
content.push({
|
|
231
|
+
type: "text",
|
|
232
|
+
text: `[Image: ${item.mimeType}, ${item.data.length} bytes]`,
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
content,
|
|
238
|
+
isError: agentResult.isError || false,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
// Fallback: stringify result
|
|
242
|
+
return {
|
|
243
|
+
content: [
|
|
244
|
+
{
|
|
245
|
+
type: "text",
|
|
246
|
+
text: typeof result === "string" ? result : JSON.stringify(result, null, 2),
|
|
247
|
+
},
|
|
248
|
+
],
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
catch (error) {
|
|
252
|
+
console.error(`[MCP Server] Tool execution failed:`, error);
|
|
253
|
+
return {
|
|
254
|
+
content: [
|
|
255
|
+
{
|
|
256
|
+
type: "text",
|
|
257
|
+
text: `Error: ${error instanceof Error ? error.message : String(error)}`,
|
|
258
|
+
},
|
|
259
|
+
],
|
|
260
|
+
isError: true,
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
sendResponse(id, result) {
|
|
265
|
+
const response = {
|
|
266
|
+
jsonrpc: "2.0",
|
|
267
|
+
id,
|
|
268
|
+
result,
|
|
269
|
+
};
|
|
270
|
+
process.stdout.write(JSON.stringify(response) + "\n");
|
|
271
|
+
}
|
|
272
|
+
sendError(id, error) {
|
|
273
|
+
const response = {
|
|
274
|
+
jsonrpc: "2.0",
|
|
275
|
+
id,
|
|
276
|
+
error: {
|
|
277
|
+
code: -32000,
|
|
278
|
+
message: error instanceof Error ? error.message : String(error),
|
|
279
|
+
},
|
|
280
|
+
};
|
|
281
|
+
process.stdout.write(JSON.stringify(response) + "\n");
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Create an MCP server from tools.
|
|
286
|
+
*
|
|
287
|
+
* @example
|
|
288
|
+
* ```typescript
|
|
289
|
+
* const server = createMCPServer({
|
|
290
|
+
* name: "My Tools",
|
|
291
|
+
* tools: [bashTool, readTool, writeTool],
|
|
292
|
+
* });
|
|
293
|
+
*
|
|
294
|
+
* await server.startStdio();
|
|
295
|
+
* ```
|
|
296
|
+
*/
|
|
297
|
+
export function createMCPServer(options) {
|
|
298
|
+
return new MCPServer(options);
|
|
299
|
+
}
|
|
300
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/mcp/server.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAmCrD;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,OAAO,SAAS;IAUpB,YAAoB,OAAyB;QAAzB,YAAO,GAAP,OAAO,CAAkB;QATrC,UAAK,GAAG,IAAI,GAAG,EAA4B,CAAC;QAC5C,WAAM,GAAG,EAAE,CAAC;QACZ,gBAAW,GAAG,KAAK,CAAC;QAQ1B,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QACzB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC;QAE1C,0BAA0B;QAC1B,KAAK,MAAM,IAAI,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC;YACjC,MAAM,SAAS,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;YACzC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;QAC5C,CAAC;QAED,OAAO,CAAC,KAAK,CAAC,6BAA6B,IAAI,CAAC,IAAI,MAAM,IAAI,CAAC,OAAO,SAAS,IAAI,CAAC,KAAK,CAAC,IAAI,QAAQ,CAAC,CAA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E,KAAc;QACnD,MAAM,QAAQ,GAAG;YACf,OAAO,EAAE,KAAK;YACd,EAAE;YACF,KAAK,EAAE;gBACL,IAAI,EAAE,CAAC,KAAK;gBACZ,OAAO,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;aAChE;SACF,CAAC;QAEF,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAC,CAAC;IACxD,CAAC;CACF;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,eAAe,CAAC,OAAyB;IACvD,OAAO,IAAI,SAAS,CAAC,OAAO,CAAC,CAAC;AAChC,CAAC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Tool Factory
|
|
3
|
+
*
|
|
4
|
+
* Creates AgentTool instances from MCP tool definitions.
|
|
5
|
+
* Uses the factory pattern to match new_indusagi's ToolRegistry.
|
|
6
|
+
*
|
|
7
|
+
* Reference: @mastra/mcp packages/mcp/src/client/client.ts tools() method
|
|
8
|
+
*/
|
|
9
|
+
import type { TSchema } from "@sinclair/typebox";
|
|
10
|
+
import type { AgentTool } from "../agent/types.js";
|
|
11
|
+
import type { MCPToolDefinition, MCPToolCallResult } from "./types.js";
|
|
12
|
+
/**
|
|
13
|
+
* Interface for MCP client that can call tools.
|
|
14
|
+
*/
|
|
15
|
+
export interface MCPToolClient {
|
|
16
|
+
/** Server name */
|
|
17
|
+
readonly serverName: string;
|
|
18
|
+
/** Whether client is connected */
|
|
19
|
+
readonly connected: boolean;
|
|
20
|
+
/** Call a tool on the MCP server */
|
|
21
|
+
callTool(name: string, args: Record<string, unknown>): Promise<MCPToolCallResult>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Create an AgentTool from an MCP tool definition.
|
|
25
|
+
*
|
|
26
|
+
* This function creates a factory that matches ToolRegistry's pattern.
|
|
27
|
+
* The tool is namespaced with the server name to avoid conflicts.
|
|
28
|
+
*
|
|
29
|
+
* @param mcpTool - MCP tool definition from the server
|
|
30
|
+
* @param client - MCP client to use for tool execution
|
|
31
|
+
* @returns Factory function that creates an AgentTool
|
|
32
|
+
*/
|
|
33
|
+
export declare function createMCPAgentToolFactory(mcpTool: MCPToolDefinition, client: MCPToolClient): () => AgentTool<TSchema, MCPToolCallResult>;
|
|
34
|
+
/**
|
|
35
|
+
* Register all MCP tools from a client into a ToolRegistry.
|
|
36
|
+
*
|
|
37
|
+
* This is the main integration point with new_indusagi's ToolRegistry.
|
|
38
|
+
*
|
|
39
|
+
* @param registry - ToolRegistry instance to register tools into
|
|
40
|
+
* @param client - MCP client with tools to register
|
|
41
|
+
* @param tools - List of tool definitions from the server
|
|
42
|
+
* @returns Number of tools registered
|
|
43
|
+
*/
|
|
44
|
+
export declare function registerMCPToolsInRegistry(registry: import("../agent/tools/registry.js").ToolRegistry, client: MCPToolClient, tools: MCPToolDefinition[]): Promise<number>;
|
|
45
|
+
/**
|
|
46
|
+
* Create a map of AgentTools from MCP tool definitions.
|
|
47
|
+
* Useful when you don't want to use the registry pattern.
|
|
48
|
+
*
|
|
49
|
+
* @param tools - List of tool definitions from the server
|
|
50
|
+
* @param client - MCP client to use for tool execution
|
|
51
|
+
* @returns Map of namespaced tool names to AgentTool instances
|
|
52
|
+
*/
|
|
53
|
+
export declare function createMCPToolsMap(tools: MCPToolDefinition[], client: MCPToolClient): Map<string, AgentTool<TSchema, MCPToolCallResult>>;
|
|
54
|
+
/**
|
|
55
|
+
* Create a record of AgentTools from MCP tool definitions.
|
|
56
|
+
* Matches the format expected by Agent's tools property.
|
|
57
|
+
*
|
|
58
|
+
* @param tools - List of tool definitions from the server
|
|
59
|
+
* @param client - MCP client to use for tool execution
|
|
60
|
+
* @returns Record of namespaced tool names to AgentTool instances
|
|
61
|
+
*/
|
|
62
|
+
export declare function createMCPToolsRecord(tools: MCPToolDefinition[], client: MCPToolClient): Record<string, AgentTool<TSchema, MCPToolCallResult>>;
|
|
63
|
+
//# sourceMappingURL=tool-factory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-factory.d.ts","sourceRoot":"","sources":["../../src/mcp/tool-factory.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAU,MAAM,mBAAmB,CAAC;AACzD,OAAO,KAAK,EAAE,SAAS,EAA4C,MAAM,mBAAmB,CAAC;AAC7F,OAAO,KAAK,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAIvE;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,kBAAkB;IAClB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,kCAAkC;IAClC,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,oCAAoC;IACpC,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;CACnF;AAED;;;;;;;;;GASG;AACH,wBAAgB,yBAAyB,CACvC,OAAO,EAAE,iBAAiB,EAC1B,MAAM,EAAE,aAAa,GACpB,MAAM,SAAS,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAqG7C;AA0ED;;;;;;;;;GASG;AACH,wBAAsB,0BAA0B,CAC9C,QAAQ,EAAE,OAAO,4BAA4B,EAAE,YAAY,EAC3D,MAAM,EAAE,aAAa,EACrB,KAAK,EAAE,iBAAiB,EAAE,GACzB,OAAO,CAAC,MAAM,CAAC,CAyBjB;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,iBAAiB,EAAE,EAC1B,MAAM,EAAE,aAAa,GACpB,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAC,CAUpD;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,iBAAiB,EAAE,EAC1B,MAAM,EAAE,aAAa,GACpB,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAC,CAUvD"}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Tool Factory
|
|
3
|
+
*
|
|
4
|
+
* Creates AgentTool instances from MCP tool definitions.
|
|
5
|
+
* Uses the factory pattern to match new_indusagi's ToolRegistry.
|
|
6
|
+
*
|
|
7
|
+
* Reference: @mastra/mcp packages/mcp/src/client/client.ts tools() method
|
|
8
|
+
*/
|
|
9
|
+
import { convertMCPInputSchema } from "./schema-converter.js";
|
|
10
|
+
import { MCPError, MCPErrorCode } from "./errors.js";
|
|
11
|
+
/**
|
|
12
|
+
* Create an AgentTool from an MCP tool definition.
|
|
13
|
+
*
|
|
14
|
+
* This function creates a factory that matches ToolRegistry's pattern.
|
|
15
|
+
* The tool is namespaced with the server name to avoid conflicts.
|
|
16
|
+
*
|
|
17
|
+
* @param mcpTool - MCP tool definition from the server
|
|
18
|
+
* @param client - MCP client to use for tool execution
|
|
19
|
+
* @returns Factory function that creates an AgentTool
|
|
20
|
+
*/
|
|
21
|
+
export function createMCPAgentToolFactory(mcpTool, client) {
|
|
22
|
+
return () => {
|
|
23
|
+
// Namespace the tool name to avoid conflicts between servers
|
|
24
|
+
const namespacedName = `${client.serverName}_${mcpTool.name}`;
|
|
25
|
+
// Convert JSON Schema to TypeBox schema
|
|
26
|
+
const parameters = convertMCPInputSchema(mcpTool.inputSchema);
|
|
27
|
+
return {
|
|
28
|
+
name: namespacedName,
|
|
29
|
+
label: mcpTool.name,
|
|
30
|
+
description: `[${client.serverName}] ${mcpTool.description || ""}`,
|
|
31
|
+
category: "mcp",
|
|
32
|
+
parameters,
|
|
33
|
+
execute: async (toolCallId, params, signal, onUpdate) => {
|
|
34
|
+
const typedParams = params;
|
|
35
|
+
try {
|
|
36
|
+
// Check connection
|
|
37
|
+
if (!client.connected) {
|
|
38
|
+
throw new MCPError(`MCP server ${client.serverName} not connected`, MCPErrorCode.NOT_CONNECTED, undefined, { serverName: client.serverName, toolName: mcpTool.name });
|
|
39
|
+
}
|
|
40
|
+
// Check for abort signal
|
|
41
|
+
if (signal?.aborted) {
|
|
42
|
+
throw new MCPError("Tool execution aborted", MCPErrorCode.TIMEOUT, undefined, { serverName: client.serverName, toolName: mcpTool.name });
|
|
43
|
+
}
|
|
44
|
+
// Call the tool via MCP
|
|
45
|
+
const result = await client.callTool(mcpTool.name, typedParams);
|
|
46
|
+
// Convert MCP result to AgentToolResult
|
|
47
|
+
return convertMCPResultToAgentResult(result, client.serverName, mcpTool.name);
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
// Handle MCP errors
|
|
51
|
+
if (error instanceof MCPError) {
|
|
52
|
+
return {
|
|
53
|
+
content: [
|
|
54
|
+
{
|
|
55
|
+
type: "text",
|
|
56
|
+
text: `MCP Error: ${error.message}`,
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
details: {
|
|
60
|
+
error: error.message,
|
|
61
|
+
code: error.code,
|
|
62
|
+
serverName: error.serverName,
|
|
63
|
+
toolName: error.toolName,
|
|
64
|
+
},
|
|
65
|
+
isError: true,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
// Handle abort errors
|
|
69
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
70
|
+
return {
|
|
71
|
+
content: [
|
|
72
|
+
{
|
|
73
|
+
type: "text",
|
|
74
|
+
text: `Tool execution aborted`,
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
details: {
|
|
78
|
+
error: "Aborted",
|
|
79
|
+
},
|
|
80
|
+
isError: true,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
// Handle other errors
|
|
84
|
+
return {
|
|
85
|
+
content: [
|
|
86
|
+
{
|
|
87
|
+
type: "text",
|
|
88
|
+
text: `Error: ${error instanceof Error ? error.message : String(error)}`,
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
details: {
|
|
92
|
+
error: String(error),
|
|
93
|
+
},
|
|
94
|
+
isError: true,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Truncate text to a maximum number of lines for display.
|
|
103
|
+
*/
|
|
104
|
+
function truncateTextForDisplay(text, maxLines = 4, maxChars = 500) {
|
|
105
|
+
const lines = text.split('\n');
|
|
106
|
+
// If already short enough, return as-is
|
|
107
|
+
if (lines.length <= maxLines && text.length <= maxChars) {
|
|
108
|
+
return text;
|
|
109
|
+
}
|
|
110
|
+
// Take first maxLines lines
|
|
111
|
+
let truncated = lines.slice(0, maxLines).join('\n');
|
|
112
|
+
// If still too long, truncate by chars
|
|
113
|
+
if (truncated.length > maxChars) {
|
|
114
|
+
truncated = truncated.slice(0, maxChars);
|
|
115
|
+
}
|
|
116
|
+
// Add truncation indicator
|
|
117
|
+
const remaining = lines.length - maxLines;
|
|
118
|
+
if (remaining > 0) {
|
|
119
|
+
truncated += `\n... (${remaining} more lines)`;
|
|
120
|
+
}
|
|
121
|
+
else if (text.length > maxChars) {
|
|
122
|
+
truncated += '...';
|
|
123
|
+
}
|
|
124
|
+
return truncated;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Convert MCP tool call result to AgentToolResult.
|
|
128
|
+
*/
|
|
129
|
+
function convertMCPResultToAgentResult(result, serverName, toolName) {
|
|
130
|
+
const content = [];
|
|
131
|
+
// Handle structured result from MCP
|
|
132
|
+
if (result.content && Array.isArray(result.content)) {
|
|
133
|
+
for (const item of result.content) {
|
|
134
|
+
if (item.type === "text") {
|
|
135
|
+
// Truncate text for display (keep full data in details)
|
|
136
|
+
const truncatedText = truncateTextForDisplay(item.text);
|
|
137
|
+
content.push({ type: "text", text: truncatedText });
|
|
138
|
+
}
|
|
139
|
+
else if (item.type === "image") {
|
|
140
|
+
content.push({
|
|
141
|
+
type: "image",
|
|
142
|
+
data: item.data,
|
|
143
|
+
mimeType: item.mimeType,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
else if (typeof result === "string") {
|
|
149
|
+
const truncatedText = truncateTextForDisplay(result);
|
|
150
|
+
content.push({ type: "text", text: truncatedText });
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
// Fallback: JSON stringify with truncation
|
|
154
|
+
const jsonStr = JSON.stringify(result, null, 2);
|
|
155
|
+
const truncatedText = truncateTextForDisplay(jsonStr);
|
|
156
|
+
content.push({ type: "text", text: truncatedText });
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
content,
|
|
160
|
+
details: result,
|
|
161
|
+
isError: result.isError || false,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Register all MCP tools from a client into a ToolRegistry.
|
|
166
|
+
*
|
|
167
|
+
* This is the main integration point with new_indusagi's ToolRegistry.
|
|
168
|
+
*
|
|
169
|
+
* @param registry - ToolRegistry instance to register tools into
|
|
170
|
+
* @param client - MCP client with tools to register
|
|
171
|
+
* @param tools - List of tool definitions from the server
|
|
172
|
+
* @returns Number of tools registered
|
|
173
|
+
*/
|
|
174
|
+
export async function registerMCPToolsInRegistry(registry, client, tools) {
|
|
175
|
+
let registeredCount = 0;
|
|
176
|
+
for (const tool of tools) {
|
|
177
|
+
try {
|
|
178
|
+
const factory = createMCPAgentToolFactory(tool, client);
|
|
179
|
+
registry.register({
|
|
180
|
+
name: `${client.serverName}_${tool.name}`,
|
|
181
|
+
label: tool.name,
|
|
182
|
+
category: "mcp",
|
|
183
|
+
description: `[${client.serverName}] ${tool.description || ""}`,
|
|
184
|
+
}, factory);
|
|
185
|
+
registeredCount++;
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
console.error(`Failed to register MCP tool ${tool.name}:`, error);
|
|
189
|
+
// Continue with other tools
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return registeredCount;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Create a map of AgentTools from MCP tool definitions.
|
|
196
|
+
* Useful when you don't want to use the registry pattern.
|
|
197
|
+
*
|
|
198
|
+
* @param tools - List of tool definitions from the server
|
|
199
|
+
* @param client - MCP client to use for tool execution
|
|
200
|
+
* @returns Map of namespaced tool names to AgentTool instances
|
|
201
|
+
*/
|
|
202
|
+
export function createMCPToolsMap(tools, client) {
|
|
203
|
+
const toolMap = new Map();
|
|
204
|
+
for (const tool of tools) {
|
|
205
|
+
const factory = createMCPAgentToolFactory(tool, client);
|
|
206
|
+
const namespacedName = `${client.serverName}_${tool.name}`;
|
|
207
|
+
toolMap.set(namespacedName, factory());
|
|
208
|
+
}
|
|
209
|
+
return toolMap;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Create a record of AgentTools from MCP tool definitions.
|
|
213
|
+
* Matches the format expected by Agent's tools property.
|
|
214
|
+
*
|
|
215
|
+
* @param tools - List of tool definitions from the server
|
|
216
|
+
* @param client - MCP client to use for tool execution
|
|
217
|
+
* @returns Record of namespaced tool names to AgentTool instances
|
|
218
|
+
*/
|
|
219
|
+
export function createMCPToolsRecord(tools, client) {
|
|
220
|
+
const toolRecord = {};
|
|
221
|
+
for (const tool of tools) {
|
|
222
|
+
const factory = createMCPAgentToolFactory(tool, client);
|
|
223
|
+
const namespacedName = `${client.serverName}_${tool.name}`;
|
|
224
|
+
toolRecord[namespacedName] = factory();
|
|
225
|
+
}
|
|
226
|
+
return toolRecord;
|
|
227
|
+
}
|
|
228
|
+
//# sourceMappingURL=tool-factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-factory.js","sourceRoot":"","sources":["../../src/mcp/tool-factory.ts"],"names":[],"mappings":"AAAA;;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