chat-agent-toolkit 1.2.32 → 1.2.34
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/research-agent.cjs.js +1 -1
- package/dist/research-agent.cjs.js.map +1 -1
- package/dist/research-agent.es.js +1 -1
- package/dist/research-agent.es.js.map +1 -1
- package/package.json +1 -1
- package/src/config/config-manager.ts +295 -295
- package/src/config/config-types.ts +65 -65
- package/src/config/environment-variables.ts +16 -16
- package/src/config/index.ts +26 -26
- package/src/config/language-models-database.ts +1434 -1434
- package/src/config/mcp-server-registry.ts +24 -24
- package/src/config/model-registry.ts +271 -271
- package/src/config/model-tester.ts +211 -211
- package/src/config/model-utils.ts +193 -193
- package/src/config/provider-ui-config.ts +196 -196
- package/src/connectors/open-connector.json +130 -130
- package/src/index.ts +26 -26
- package/src/memory/ARCHITECTURE.md +302 -302
- package/src/memory/README.md +224 -224
- package/src/memory/agent-memory-manager.ts +416 -416
- package/src/memory/example.ts +343 -343
- package/src/memory/index.ts +47 -47
- package/src/memory/mastra-integration.ts +604 -604
- package/src/memory/storage/drizzle-storage.ts +236 -236
- package/src/memory/storage/in-memory-storage.ts +551 -551
- package/src/memory/storage/storage-interface.ts +68 -68
- package/src/memory/types.ts +125 -125
- package/src/models/types.ts +4 -4
- package/src/provider-logos/01-ai.png +0 -0
- package/src/provider-logos/anthropic.png +0 -0
- package/src/provider-logos/aws-bedrock.png +0 -0
- package/src/provider-logos/aws-sagemaker.png +0 -0
- package/src/provider-logos/azure-openai-service.png +0 -0
- package/src/provider-logos/chatglm.png +0 -0
- package/src/provider-logos/cohere.png +0 -0
- package/src/provider-logos/gemini.png +0 -0
- package/src/provider-logos/groqcloud.png +0 -0
- package/src/provider-logos/huggingface.png +0 -0
- package/src/provider-logos/jina.png +0 -0
- package/src/provider-logos/localai.png +0 -0
- package/src/provider-logos/mistral-ai.png +0 -0
- package/src/provider-logos/moonshot-ai.png +0 -0
- package/src/provider-logos/ollama.png +0 -0
- package/src/provider-logos/openai.png +0 -0
- package/src/provider-logos/openllm.png +0 -0
- package/src/provider-logos/openrouter.png +0 -0
- package/src/provider-logos/replicate.png +0 -0
- package/src/provider-logos/together-ai.png +0 -0
- package/src/provider-logos/tongyi.png +0 -0
- package/src/provider-logos/xorbits-inference.png +0 -0
- package/src/provider-logos/zhipu-ai.png +0 -0
- package/src/tools/index.ts +13 -13
- package/src/tools/open-connector-mastra.ts +270 -270
- package/src/tools/open-connector-mcp.ts +170 -170
- package/src/tools/qwksearch-api-tools.ts +326 -326
- package/src/tools/search/doc-utils.ts +139 -139
- package/src/tools/search/document.ts +47 -47
- package/src/tools/search/index.ts +14 -14
- package/src/tools/search/link-summarizer.ts +112 -112
- package/src/tools/search/meta-search-types.ts +62 -62
- package/src/tools/search/metaSearchAgent.ts +465 -465
- package/src/tools/search/search-handlers.ts +97 -97
- package/src/tools/search/suggestionGeneratorAgent.ts +56 -56
- package/src/types.d.ts +137 -137
- package/src/utils/index.ts +2 -2
- package/src/utils/markdown-to-html.ts +53 -53
- package/src/utils/outputParser.ts +73 -73
|
@@ -1,270 +1,270 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview OpenConnector + Mastra Integration
|
|
3
|
-
*
|
|
4
|
-
* Connects OpenConnector's MCP endpoint to Mastra agents.
|
|
5
|
-
* OpenConnector provides the tools, Mastra provides the agent framework.
|
|
6
|
-
*
|
|
7
|
-
* Architecture:
|
|
8
|
-
* 1. Connect to OpenConnector Worker URL at /mcp/sse
|
|
9
|
-
* 2. Configure Mastra MCPClient with URL + auth headers
|
|
10
|
-
* 3. Get tools with mcp.listTools() or mcp.listToolsets()
|
|
11
|
-
* 4. Create Mastra Agent with tools
|
|
12
|
-
* 5. Execute agent.generate() with multi-step tool calling
|
|
13
|
-
*
|
|
14
|
-
* @see https://github.com/OpenSourceAGI/open-connector
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
import { Agent } from '@mastra/core/agent';
|
|
18
|
-
import { MCPClient } from '@mastra/mcp';
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Configuration for OpenConnector + Mastra integration
|
|
22
|
-
*/
|
|
23
|
-
export interface OpenConnectorMastraConfig {
|
|
24
|
-
/** OpenConnector admin token */
|
|
25
|
-
adminToken: string;
|
|
26
|
-
/** Base URL of the deployed OpenConnector Worker */
|
|
27
|
-
baseUrl: string;
|
|
28
|
-
/** User ID for scoping the session */
|
|
29
|
-
userId: string;
|
|
30
|
-
/** Apps to enable */
|
|
31
|
-
apps: string[];
|
|
32
|
-
/** Agent configuration */
|
|
33
|
-
agent: {
|
|
34
|
-
id: string;
|
|
35
|
-
name: string;
|
|
36
|
-
instructions: string;
|
|
37
|
-
model: any; // Mastra model instance (e.g., openai('gpt-4o'))
|
|
38
|
-
maxSteps?: number;
|
|
39
|
-
};
|
|
40
|
-
/** Whether to require approval before tool execution */
|
|
41
|
-
requireToolApproval?: boolean;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* OpenConnector + Mastra session manager
|
|
46
|
-
* Handles MCP connection lifecycle and agent creation
|
|
47
|
-
*/
|
|
48
|
-
export class OpenConnectorMastraSession {
|
|
49
|
-
private mcpClient: MCPClient | null = null;
|
|
50
|
-
private agent: Agent | null = null;
|
|
51
|
-
|
|
52
|
-
constructor(private config: OpenConnectorMastraConfig) {}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Get or create Mastra MCP client
|
|
56
|
-
* Connects to OpenConnector's MCP endpoint via HTTP
|
|
57
|
-
*/
|
|
58
|
-
async getMCPClient(): Promise<MCPClient> {
|
|
59
|
-
if (this.mcpClient) {
|
|
60
|
-
return this.mcpClient;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const mcpUrl = `${this.config.baseUrl}/mcp/sse`;
|
|
64
|
-
|
|
65
|
-
// Create Mastra MCP client
|
|
66
|
-
this.mcpClient = new MCPClient({
|
|
67
|
-
id: `open-connector-${this.config.userId}`,
|
|
68
|
-
servers: {
|
|
69
|
-
openconnector: {
|
|
70
|
-
url: new URL(mcpUrl),
|
|
71
|
-
requestInit: {
|
|
72
|
-
headers: {
|
|
73
|
-
Authorization: `Bearer ${this.config.adminToken}`,
|
|
74
|
-
},
|
|
75
|
-
},
|
|
76
|
-
},
|
|
77
|
-
},
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
return this.mcpClient;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Get tools from OpenConnector MCP
|
|
85
|
-
* Use for static agent setup (same tools for all requests)
|
|
86
|
-
*/
|
|
87
|
-
async getTools() {
|
|
88
|
-
const mcp = await this.getMCPClient();
|
|
89
|
-
return await mcp.listTools();
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Get toolsets from OpenConnector MCP
|
|
94
|
-
* Use for dynamic per-request tool configuration
|
|
95
|
-
*/
|
|
96
|
-
async getToolsets() {
|
|
97
|
-
const mcp = await this.getMCPClient();
|
|
98
|
-
return await mcp.listToolsets();
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Create or get Mastra agent with OpenConnector tools
|
|
103
|
-
* Agent is cached and reused across generate() calls
|
|
104
|
-
*/
|
|
105
|
-
async getAgent(): Promise<Agent> {
|
|
106
|
-
if (this.agent) {
|
|
107
|
-
return this.agent;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const tools = await this.getTools();
|
|
111
|
-
|
|
112
|
-
this.agent = new Agent({
|
|
113
|
-
id: this.config.agent.id,
|
|
114
|
-
name: this.config.agent.name,
|
|
115
|
-
instructions: this.config.agent.instructions,
|
|
116
|
-
model: this.config.agent.model,
|
|
117
|
-
tools,
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
return this.agent;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Execute agent with a prompt
|
|
125
|
-
* Handles multi-step tool calling automatically
|
|
126
|
-
*
|
|
127
|
-
* @param prompt - User prompt
|
|
128
|
-
* @param options - Generation options
|
|
129
|
-
* @returns Agent response with text and tool calls
|
|
130
|
-
*/
|
|
131
|
-
async generate(
|
|
132
|
-
prompt: string,
|
|
133
|
-
options?: {
|
|
134
|
-
maxSteps?: number;
|
|
135
|
-
/** Extra context messages passed to the agent (model messages) */
|
|
136
|
-
context?: any[];
|
|
137
|
-
}
|
|
138
|
-
) {
|
|
139
|
-
const agent = await this.getAgent();
|
|
140
|
-
|
|
141
|
-
return await agent.generate(prompt, {
|
|
142
|
-
maxSteps: options?.maxSteps || this.config.agent.maxSteps || 5,
|
|
143
|
-
...(options?.context && { context: options.context }),
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Clean up MCP connection and agent
|
|
149
|
-
* Call when done with the session
|
|
150
|
-
*/
|
|
151
|
-
async cleanup() {
|
|
152
|
-
if (this.mcpClient) {
|
|
153
|
-
await this.mcpClient.disconnect();
|
|
154
|
-
this.mcpClient = null;
|
|
155
|
-
}
|
|
156
|
-
this.agent = null;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Quick helper to create an OpenConnector + Mastra agent
|
|
162
|
-
* For one-off usage without session management
|
|
163
|
-
*
|
|
164
|
-
* @example
|
|
165
|
-
* ```ts
|
|
166
|
-
* const result = await createOpenConnectorMastraAgent({
|
|
167
|
-
* adminToken: process.env.OPEN_CONNECTOR_ADMIN_TOKEN!,
|
|
168
|
-
* baseUrl: 'https://open-connector.example.workers.dev',
|
|
169
|
-
* userId: 'user-123',
|
|
170
|
-
* apps: ['gmail', 'slack'],
|
|
171
|
-
* agent: {
|
|
172
|
-
* id: 'email-agent',
|
|
173
|
-
* name: 'Email Assistant',
|
|
174
|
-
* instructions: 'Help with email tasks',
|
|
175
|
-
* model: openai('gpt-4o'),
|
|
176
|
-
* },
|
|
177
|
-
* }).generate('Check my unread emails');
|
|
178
|
-
* ```
|
|
179
|
-
*/
|
|
180
|
-
export async function createOpenConnectorMastraAgent(
|
|
181
|
-
config: OpenConnectorMastraConfig
|
|
182
|
-
) {
|
|
183
|
-
const session = new OpenConnectorMastraSession(config);
|
|
184
|
-
await session.getAgent(); // Initialize
|
|
185
|
-
return session;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Create Mastra agent with dynamic per-request apps
|
|
190
|
-
* Better for multi-user scenarios where tools vary per request
|
|
191
|
-
*
|
|
192
|
-
* @example
|
|
193
|
-
* ```ts
|
|
194
|
-
* const agent = await createDynamicOpenConnectorAgent({
|
|
195
|
-
* adminToken: process.env.OPEN_CONNECTOR_ADMIN_TOKEN!,
|
|
196
|
-
* baseUrl: 'https://open-connector.example.workers.dev',
|
|
197
|
-
* agent: {
|
|
198
|
-
* id: 'dynamic-agent',
|
|
199
|
-
* name: 'Dynamic Assistant',
|
|
200
|
-
* instructions: 'Use available tools',
|
|
201
|
-
* model: openai('gpt-4o'),
|
|
202
|
-
* },
|
|
203
|
-
* });
|
|
204
|
-
*
|
|
205
|
-
* // Each request can have different userId/apps
|
|
206
|
-
* const result = await agent.generate({
|
|
207
|
-
* userId: 'user-123',
|
|
208
|
-
* apps: ['gmail'],
|
|
209
|
-
* prompt: 'Check emails',
|
|
210
|
-
* });
|
|
211
|
-
* ```
|
|
212
|
-
*/
|
|
213
|
-
export async function createDynamicOpenConnectorAgent(config: {
|
|
214
|
-
adminToken: string;
|
|
215
|
-
baseUrl: string;
|
|
216
|
-
agent: {
|
|
217
|
-
id: string;
|
|
218
|
-
name: string;
|
|
219
|
-
instructions: string;
|
|
220
|
-
model: any;
|
|
221
|
-
maxSteps?: number;
|
|
222
|
-
};
|
|
223
|
-
}) {
|
|
224
|
-
return {
|
|
225
|
-
async generate(params: {
|
|
226
|
-
userId: string;
|
|
227
|
-
apps: string[];
|
|
228
|
-
prompt: string;
|
|
229
|
-
maxSteps?: number;
|
|
230
|
-
}) {
|
|
231
|
-
const mcpUrl = `${config.baseUrl}/mcp/sse`;
|
|
232
|
-
|
|
233
|
-
// Connect MCP client
|
|
234
|
-
const mcp = new MCPClient({
|
|
235
|
-
id: `open-connector-${params.userId}`,
|
|
236
|
-
servers: {
|
|
237
|
-
openconnector: {
|
|
238
|
-
url: new URL(mcpUrl),
|
|
239
|
-
requestInit: {
|
|
240
|
-
headers: {
|
|
241
|
-
Authorization: `Bearer ${config.adminToken}`,
|
|
242
|
-
},
|
|
243
|
-
},
|
|
244
|
-
},
|
|
245
|
-
},
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
try {
|
|
249
|
-
// Get toolsets for this request
|
|
250
|
-
const toolsets = await mcp.listToolsets();
|
|
251
|
-
|
|
252
|
-
// Create agent; toolsets are provided per-request at generate time
|
|
253
|
-
const agent = new Agent({
|
|
254
|
-
id: config.agent.id,
|
|
255
|
-
name: config.agent.name,
|
|
256
|
-
instructions: config.agent.instructions,
|
|
257
|
-
model: config.agent.model,
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
// Execute with dynamic toolsets
|
|
261
|
-
return await agent.generate(params.prompt, {
|
|
262
|
-
maxSteps: params.maxSteps || config.agent.maxSteps || 5,
|
|
263
|
-
toolsets,
|
|
264
|
-
});
|
|
265
|
-
} finally {
|
|
266
|
-
await mcp.disconnect();
|
|
267
|
-
}
|
|
268
|
-
},
|
|
269
|
-
};
|
|
270
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview OpenConnector + Mastra Integration
|
|
3
|
+
*
|
|
4
|
+
* Connects OpenConnector's MCP endpoint to Mastra agents.
|
|
5
|
+
* OpenConnector provides the tools, Mastra provides the agent framework.
|
|
6
|
+
*
|
|
7
|
+
* Architecture:
|
|
8
|
+
* 1. Connect to OpenConnector Worker URL at /mcp/sse
|
|
9
|
+
* 2. Configure Mastra MCPClient with URL + auth headers
|
|
10
|
+
* 3. Get tools with mcp.listTools() or mcp.listToolsets()
|
|
11
|
+
* 4. Create Mastra Agent with tools
|
|
12
|
+
* 5. Execute agent.generate() with multi-step tool calling
|
|
13
|
+
*
|
|
14
|
+
* @see https://github.com/OpenSourceAGI/open-connector
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { Agent } from '@mastra/core/agent';
|
|
18
|
+
import { MCPClient } from '@mastra/mcp';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Configuration for OpenConnector + Mastra integration
|
|
22
|
+
*/
|
|
23
|
+
export interface OpenConnectorMastraConfig {
|
|
24
|
+
/** OpenConnector admin token */
|
|
25
|
+
adminToken: string;
|
|
26
|
+
/** Base URL of the deployed OpenConnector Worker */
|
|
27
|
+
baseUrl: string;
|
|
28
|
+
/** User ID for scoping the session */
|
|
29
|
+
userId: string;
|
|
30
|
+
/** Apps to enable */
|
|
31
|
+
apps: string[];
|
|
32
|
+
/** Agent configuration */
|
|
33
|
+
agent: {
|
|
34
|
+
id: string;
|
|
35
|
+
name: string;
|
|
36
|
+
instructions: string;
|
|
37
|
+
model: any; // Mastra model instance (e.g., openai('gpt-4o'))
|
|
38
|
+
maxSteps?: number;
|
|
39
|
+
};
|
|
40
|
+
/** Whether to require approval before tool execution */
|
|
41
|
+
requireToolApproval?: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* OpenConnector + Mastra session manager
|
|
46
|
+
* Handles MCP connection lifecycle and agent creation
|
|
47
|
+
*/
|
|
48
|
+
export class OpenConnectorMastraSession {
|
|
49
|
+
private mcpClient: MCPClient | null = null;
|
|
50
|
+
private agent: Agent | null = null;
|
|
51
|
+
|
|
52
|
+
constructor(private config: OpenConnectorMastraConfig) {}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Get or create Mastra MCP client
|
|
56
|
+
* Connects to OpenConnector's MCP endpoint via HTTP
|
|
57
|
+
*/
|
|
58
|
+
async getMCPClient(): Promise<MCPClient> {
|
|
59
|
+
if (this.mcpClient) {
|
|
60
|
+
return this.mcpClient;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const mcpUrl = `${this.config.baseUrl}/mcp/sse`;
|
|
64
|
+
|
|
65
|
+
// Create Mastra MCP client
|
|
66
|
+
this.mcpClient = new MCPClient({
|
|
67
|
+
id: `open-connector-${this.config.userId}`,
|
|
68
|
+
servers: {
|
|
69
|
+
openconnector: {
|
|
70
|
+
url: new URL(mcpUrl),
|
|
71
|
+
requestInit: {
|
|
72
|
+
headers: {
|
|
73
|
+
Authorization: `Bearer ${this.config.adminToken}`,
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
return this.mcpClient;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Get tools from OpenConnector MCP
|
|
85
|
+
* Use for static agent setup (same tools for all requests)
|
|
86
|
+
*/
|
|
87
|
+
async getTools() {
|
|
88
|
+
const mcp = await this.getMCPClient();
|
|
89
|
+
return await mcp.listTools();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Get toolsets from OpenConnector MCP
|
|
94
|
+
* Use for dynamic per-request tool configuration
|
|
95
|
+
*/
|
|
96
|
+
async getToolsets() {
|
|
97
|
+
const mcp = await this.getMCPClient();
|
|
98
|
+
return await mcp.listToolsets();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Create or get Mastra agent with OpenConnector tools
|
|
103
|
+
* Agent is cached and reused across generate() calls
|
|
104
|
+
*/
|
|
105
|
+
async getAgent(): Promise<Agent> {
|
|
106
|
+
if (this.agent) {
|
|
107
|
+
return this.agent;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const tools = await this.getTools();
|
|
111
|
+
|
|
112
|
+
this.agent = new Agent({
|
|
113
|
+
id: this.config.agent.id,
|
|
114
|
+
name: this.config.agent.name,
|
|
115
|
+
instructions: this.config.agent.instructions,
|
|
116
|
+
model: this.config.agent.model,
|
|
117
|
+
tools,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
return this.agent;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Execute agent with a prompt
|
|
125
|
+
* Handles multi-step tool calling automatically
|
|
126
|
+
*
|
|
127
|
+
* @param prompt - User prompt
|
|
128
|
+
* @param options - Generation options
|
|
129
|
+
* @returns Agent response with text and tool calls
|
|
130
|
+
*/
|
|
131
|
+
async generate(
|
|
132
|
+
prompt: string,
|
|
133
|
+
options?: {
|
|
134
|
+
maxSteps?: number;
|
|
135
|
+
/** Extra context messages passed to the agent (model messages) */
|
|
136
|
+
context?: any[];
|
|
137
|
+
}
|
|
138
|
+
) {
|
|
139
|
+
const agent = await this.getAgent();
|
|
140
|
+
|
|
141
|
+
return await agent.generate(prompt, {
|
|
142
|
+
maxSteps: options?.maxSteps || this.config.agent.maxSteps || 5,
|
|
143
|
+
...(options?.context && { context: options.context }),
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Clean up MCP connection and agent
|
|
149
|
+
* Call when done with the session
|
|
150
|
+
*/
|
|
151
|
+
async cleanup() {
|
|
152
|
+
if (this.mcpClient) {
|
|
153
|
+
await this.mcpClient.disconnect();
|
|
154
|
+
this.mcpClient = null;
|
|
155
|
+
}
|
|
156
|
+
this.agent = null;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Quick helper to create an OpenConnector + Mastra agent
|
|
162
|
+
* For one-off usage without session management
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```ts
|
|
166
|
+
* const result = await createOpenConnectorMastraAgent({
|
|
167
|
+
* adminToken: process.env.OPEN_CONNECTOR_ADMIN_TOKEN!,
|
|
168
|
+
* baseUrl: 'https://open-connector.example.workers.dev',
|
|
169
|
+
* userId: 'user-123',
|
|
170
|
+
* apps: ['gmail', 'slack'],
|
|
171
|
+
* agent: {
|
|
172
|
+
* id: 'email-agent',
|
|
173
|
+
* name: 'Email Assistant',
|
|
174
|
+
* instructions: 'Help with email tasks',
|
|
175
|
+
* model: openai('gpt-4o'),
|
|
176
|
+
* },
|
|
177
|
+
* }).generate('Check my unread emails');
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
export async function createOpenConnectorMastraAgent(
|
|
181
|
+
config: OpenConnectorMastraConfig
|
|
182
|
+
) {
|
|
183
|
+
const session = new OpenConnectorMastraSession(config);
|
|
184
|
+
await session.getAgent(); // Initialize
|
|
185
|
+
return session;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Create Mastra agent with dynamic per-request apps
|
|
190
|
+
* Better for multi-user scenarios where tools vary per request
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```ts
|
|
194
|
+
* const agent = await createDynamicOpenConnectorAgent({
|
|
195
|
+
* adminToken: process.env.OPEN_CONNECTOR_ADMIN_TOKEN!,
|
|
196
|
+
* baseUrl: 'https://open-connector.example.workers.dev',
|
|
197
|
+
* agent: {
|
|
198
|
+
* id: 'dynamic-agent',
|
|
199
|
+
* name: 'Dynamic Assistant',
|
|
200
|
+
* instructions: 'Use available tools',
|
|
201
|
+
* model: openai('gpt-4o'),
|
|
202
|
+
* },
|
|
203
|
+
* });
|
|
204
|
+
*
|
|
205
|
+
* // Each request can have different userId/apps
|
|
206
|
+
* const result = await agent.generate({
|
|
207
|
+
* userId: 'user-123',
|
|
208
|
+
* apps: ['gmail'],
|
|
209
|
+
* prompt: 'Check emails',
|
|
210
|
+
* });
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
export async function createDynamicOpenConnectorAgent(config: {
|
|
214
|
+
adminToken: string;
|
|
215
|
+
baseUrl: string;
|
|
216
|
+
agent: {
|
|
217
|
+
id: string;
|
|
218
|
+
name: string;
|
|
219
|
+
instructions: string;
|
|
220
|
+
model: any;
|
|
221
|
+
maxSteps?: number;
|
|
222
|
+
};
|
|
223
|
+
}) {
|
|
224
|
+
return {
|
|
225
|
+
async generate(params: {
|
|
226
|
+
userId: string;
|
|
227
|
+
apps: string[];
|
|
228
|
+
prompt: string;
|
|
229
|
+
maxSteps?: number;
|
|
230
|
+
}) {
|
|
231
|
+
const mcpUrl = `${config.baseUrl}/mcp/sse`;
|
|
232
|
+
|
|
233
|
+
// Connect MCP client
|
|
234
|
+
const mcp = new MCPClient({
|
|
235
|
+
id: `open-connector-${params.userId}`,
|
|
236
|
+
servers: {
|
|
237
|
+
openconnector: {
|
|
238
|
+
url: new URL(mcpUrl),
|
|
239
|
+
requestInit: {
|
|
240
|
+
headers: {
|
|
241
|
+
Authorization: `Bearer ${config.adminToken}`,
|
|
242
|
+
},
|
|
243
|
+
},
|
|
244
|
+
},
|
|
245
|
+
},
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
try {
|
|
249
|
+
// Get toolsets for this request
|
|
250
|
+
const toolsets = await mcp.listToolsets();
|
|
251
|
+
|
|
252
|
+
// Create agent; toolsets are provided per-request at generate time
|
|
253
|
+
const agent = new Agent({
|
|
254
|
+
id: config.agent.id,
|
|
255
|
+
name: config.agent.name,
|
|
256
|
+
instructions: config.agent.instructions,
|
|
257
|
+
model: config.agent.model,
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// Execute with dynamic toolsets
|
|
261
|
+
return await agent.generate(params.prompt, {
|
|
262
|
+
maxSteps: params.maxSteps || config.agent.maxSteps || 5,
|
|
263
|
+
toolsets,
|
|
264
|
+
});
|
|
265
|
+
} finally {
|
|
266
|
+
await mcp.disconnect();
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
};
|
|
270
|
+
}
|