@compilr-dev/agents 0.0.1
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/README.md +1277 -0
- package/dist/agent.d.ts +1272 -0
- package/dist/agent.js +1912 -0
- package/dist/anchors/builtin.d.ts +24 -0
- package/dist/anchors/builtin.js +61 -0
- package/dist/anchors/index.d.ts +6 -0
- package/dist/anchors/index.js +5 -0
- package/dist/anchors/manager.d.ts +115 -0
- package/dist/anchors/manager.js +412 -0
- package/dist/anchors/types.d.ts +168 -0
- package/dist/anchors/types.js +10 -0
- package/dist/context/index.d.ts +12 -0
- package/dist/context/index.js +10 -0
- package/dist/context/manager.d.ts +224 -0
- package/dist/context/manager.js +770 -0
- package/dist/context/types.d.ts +377 -0
- package/dist/context/types.js +7 -0
- package/dist/costs/index.d.ts +8 -0
- package/dist/costs/index.js +7 -0
- package/dist/costs/tracker.d.ts +121 -0
- package/dist/costs/tracker.js +295 -0
- package/dist/costs/types.d.ts +157 -0
- package/dist/costs/types.js +8 -0
- package/dist/errors.d.ts +178 -0
- package/dist/errors.js +249 -0
- package/dist/guardrails/builtin.d.ts +27 -0
- package/dist/guardrails/builtin.js +223 -0
- package/dist/guardrails/index.d.ts +6 -0
- package/dist/guardrails/index.js +5 -0
- package/dist/guardrails/manager.d.ts +117 -0
- package/dist/guardrails/manager.js +288 -0
- package/dist/guardrails/types.d.ts +159 -0
- package/dist/guardrails/types.js +7 -0
- package/dist/hooks/index.d.ts +31 -0
- package/dist/hooks/index.js +29 -0
- package/dist/hooks/manager.d.ts +147 -0
- package/dist/hooks/manager.js +600 -0
- package/dist/hooks/types.d.ts +368 -0
- package/dist/hooks/types.js +12 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +73 -0
- package/dist/mcp/client.d.ts +93 -0
- package/dist/mcp/client.js +287 -0
- package/dist/mcp/errors.d.ts +60 -0
- package/dist/mcp/errors.js +78 -0
- package/dist/mcp/index.d.ts +43 -0
- package/dist/mcp/index.js +45 -0
- package/dist/mcp/manager.d.ts +120 -0
- package/dist/mcp/manager.js +276 -0
- package/dist/mcp/tools.d.ts +54 -0
- package/dist/mcp/tools.js +99 -0
- package/dist/mcp/types.d.ts +150 -0
- package/dist/mcp/types.js +40 -0
- package/dist/memory/index.d.ts +8 -0
- package/dist/memory/index.js +7 -0
- package/dist/memory/loader.d.ts +114 -0
- package/dist/memory/loader.js +463 -0
- package/dist/memory/types.d.ts +182 -0
- package/dist/memory/types.js +8 -0
- package/dist/messages/index.d.ts +82 -0
- package/dist/messages/index.js +155 -0
- package/dist/permissions/index.d.ts +5 -0
- package/dist/permissions/index.js +4 -0
- package/dist/permissions/manager.d.ts +125 -0
- package/dist/permissions/manager.js +379 -0
- package/dist/permissions/types.d.ts +162 -0
- package/dist/permissions/types.js +7 -0
- package/dist/providers/claude.d.ts +90 -0
- package/dist/providers/claude.js +348 -0
- package/dist/providers/index.d.ts +8 -0
- package/dist/providers/index.js +11 -0
- package/dist/providers/mock.d.ts +133 -0
- package/dist/providers/mock.js +204 -0
- package/dist/providers/types.d.ts +168 -0
- package/dist/providers/types.js +4 -0
- package/dist/rate-limit/index.d.ts +45 -0
- package/dist/rate-limit/index.js +47 -0
- package/dist/rate-limit/limiter.d.ts +104 -0
- package/dist/rate-limit/limiter.js +326 -0
- package/dist/rate-limit/provider-wrapper.d.ts +112 -0
- package/dist/rate-limit/provider-wrapper.js +201 -0
- package/dist/rate-limit/retry.d.ts +108 -0
- package/dist/rate-limit/retry.js +287 -0
- package/dist/rate-limit/types.d.ts +181 -0
- package/dist/rate-limit/types.js +22 -0
- package/dist/rehearsal/file-analyzer.d.ts +22 -0
- package/dist/rehearsal/file-analyzer.js +351 -0
- package/dist/rehearsal/git-analyzer.d.ts +22 -0
- package/dist/rehearsal/git-analyzer.js +472 -0
- package/dist/rehearsal/index.d.ts +35 -0
- package/dist/rehearsal/index.js +36 -0
- package/dist/rehearsal/manager.d.ts +100 -0
- package/dist/rehearsal/manager.js +290 -0
- package/dist/rehearsal/types.d.ts +235 -0
- package/dist/rehearsal/types.js +8 -0
- package/dist/skills/index.d.ts +160 -0
- package/dist/skills/index.js +282 -0
- package/dist/state/agent-state.d.ts +41 -0
- package/dist/state/agent-state.js +88 -0
- package/dist/state/checkpointer.d.ts +110 -0
- package/dist/state/checkpointer.js +362 -0
- package/dist/state/errors.d.ts +66 -0
- package/dist/state/errors.js +88 -0
- package/dist/state/index.d.ts +35 -0
- package/dist/state/index.js +37 -0
- package/dist/state/serializer.d.ts +55 -0
- package/dist/state/serializer.js +172 -0
- package/dist/state/types.d.ts +312 -0
- package/dist/state/types.js +14 -0
- package/dist/tools/builtin/bash-output.d.ts +61 -0
- package/dist/tools/builtin/bash-output.js +90 -0
- package/dist/tools/builtin/bash.d.ts +150 -0
- package/dist/tools/builtin/bash.js +354 -0
- package/dist/tools/builtin/edit.d.ts +50 -0
- package/dist/tools/builtin/edit.js +215 -0
- package/dist/tools/builtin/glob.d.ts +62 -0
- package/dist/tools/builtin/glob.js +244 -0
- package/dist/tools/builtin/grep.d.ts +74 -0
- package/dist/tools/builtin/grep.js +363 -0
- package/dist/tools/builtin/index.d.ts +44 -0
- package/dist/tools/builtin/index.js +69 -0
- package/dist/tools/builtin/kill-shell.d.ts +44 -0
- package/dist/tools/builtin/kill-shell.js +80 -0
- package/dist/tools/builtin/read-file.d.ts +57 -0
- package/dist/tools/builtin/read-file.js +184 -0
- package/dist/tools/builtin/shell-manager.d.ts +176 -0
- package/dist/tools/builtin/shell-manager.js +337 -0
- package/dist/tools/builtin/task.d.ts +202 -0
- package/dist/tools/builtin/task.js +350 -0
- package/dist/tools/builtin/todo.d.ts +207 -0
- package/dist/tools/builtin/todo.js +453 -0
- package/dist/tools/builtin/utils.d.ts +27 -0
- package/dist/tools/builtin/utils.js +70 -0
- package/dist/tools/builtin/web-fetch.d.ts +96 -0
- package/dist/tools/builtin/web-fetch.js +290 -0
- package/dist/tools/builtin/write-file.d.ts +54 -0
- package/dist/tools/builtin/write-file.js +147 -0
- package/dist/tools/define.d.ts +60 -0
- package/dist/tools/define.js +65 -0
- package/dist/tools/index.d.ts +10 -0
- package/dist/tools/index.js +37 -0
- package/dist/tools/registry.d.ts +79 -0
- package/dist/tools/registry.js +151 -0
- package/dist/tools/types.d.ts +59 -0
- package/dist/tools/types.js +4 -0
- package/dist/tracing/hooks.d.ts +58 -0
- package/dist/tracing/hooks.js +377 -0
- package/dist/tracing/index.d.ts +51 -0
- package/dist/tracing/index.js +55 -0
- package/dist/tracing/logging.d.ts +78 -0
- package/dist/tracing/logging.js +310 -0
- package/dist/tracing/manager.d.ts +160 -0
- package/dist/tracing/manager.js +468 -0
- package/dist/tracing/otel.d.ts +102 -0
- package/dist/tracing/otel.js +246 -0
- package/dist/tracing/types.d.ts +346 -0
- package/dist/tracing/types.js +38 -0
- package/dist/utils/index.d.ts +23 -0
- package/dist/utils/index.js +44 -0
- package/package.json +79 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Manager - Manages multiple MCP server connections
|
|
3
|
+
*
|
|
4
|
+
* Provides a high-level interface for:
|
|
5
|
+
* - Managing multiple MCP servers
|
|
6
|
+
* - Discovering and registering tools from servers
|
|
7
|
+
* - Handling server lifecycle (connect, disconnect, reconnect)
|
|
8
|
+
*/
|
|
9
|
+
import { MCPClient } from './client.js';
|
|
10
|
+
import { normalizeServerConfig } from './types.js';
|
|
11
|
+
import { MCPError, MCPErrorCode } from './errors.js';
|
|
12
|
+
import { mcpToolsToTools } from './tools.js';
|
|
13
|
+
/**
|
|
14
|
+
* MCP Manager for managing multiple MCP server connections
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const manager = new MCPManager();
|
|
19
|
+
*
|
|
20
|
+
* // Add servers
|
|
21
|
+
* await manager.addServer({
|
|
22
|
+
* name: 'filesystem',
|
|
23
|
+
* transport: 'stdio',
|
|
24
|
+
* command: 'npx',
|
|
25
|
+
* args: ['-y', '@modelcontextprotocol/server-filesystem', '/tmp'],
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* // Discover and get tools
|
|
29
|
+
* const tools = await manager.discoverTools();
|
|
30
|
+
*
|
|
31
|
+
* // Use with agent
|
|
32
|
+
* const agent = new Agent({
|
|
33
|
+
* provider: claude,
|
|
34
|
+
* tools: [...builtinTools, ...tools],
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* // Cleanup
|
|
38
|
+
* await manager.disconnectAll();
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export class MCPManager {
|
|
42
|
+
clients = new Map();
|
|
43
|
+
toolRegistry = null;
|
|
44
|
+
toolPrefix;
|
|
45
|
+
autoConnect;
|
|
46
|
+
eventHandlers = new Set();
|
|
47
|
+
constructor(options = {}) {
|
|
48
|
+
this.toolPrefix = options.toolPrefix ?? 'mcp';
|
|
49
|
+
this.autoConnect = options.autoConnect ?? true;
|
|
50
|
+
if (options.onEvent) {
|
|
51
|
+
this.eventHandlers.add(options.onEvent);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Set a tool registry for automatic tool registration
|
|
56
|
+
* When set, tools from MCP servers will be automatically registered
|
|
57
|
+
*/
|
|
58
|
+
setToolRegistry(registry) {
|
|
59
|
+
this.toolRegistry = registry;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Add an MCP server
|
|
63
|
+
*
|
|
64
|
+
* @param config Server configuration
|
|
65
|
+
* @returns The created MCPClient
|
|
66
|
+
* @throws MCPError if server with same name already exists
|
|
67
|
+
*/
|
|
68
|
+
async addServer(config) {
|
|
69
|
+
if (this.clients.has(config.name)) {
|
|
70
|
+
throw new MCPError(`MCP server '${config.name}' already exists`, config.name, MCPErrorCode.SERVER_EXISTS);
|
|
71
|
+
}
|
|
72
|
+
// Normalize shorthand config to full config
|
|
73
|
+
const fullConfig = normalizeServerConfig(config);
|
|
74
|
+
// Create client
|
|
75
|
+
const client = new MCPClient(fullConfig);
|
|
76
|
+
// Forward events
|
|
77
|
+
client.onEvent((event) => {
|
|
78
|
+
this.emit(event);
|
|
79
|
+
});
|
|
80
|
+
// Store client
|
|
81
|
+
this.clients.set(config.name, client);
|
|
82
|
+
// Auto-connect if enabled
|
|
83
|
+
if (this.autoConnect) {
|
|
84
|
+
await client.connect();
|
|
85
|
+
// Auto-register tools if registry is set
|
|
86
|
+
if (this.toolRegistry) {
|
|
87
|
+
await this.refreshTools(config.name);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return client;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Remove an MCP server
|
|
94
|
+
*
|
|
95
|
+
* @param name Server name
|
|
96
|
+
* @throws MCPError if server not found
|
|
97
|
+
*/
|
|
98
|
+
async removeServer(name) {
|
|
99
|
+
const client = this.clients.get(name);
|
|
100
|
+
if (!client) {
|
|
101
|
+
throw new MCPError(`MCP server '${name}' not found`, name, MCPErrorCode.SERVER_NOT_FOUND);
|
|
102
|
+
}
|
|
103
|
+
// Disconnect if connected
|
|
104
|
+
await client.disconnect();
|
|
105
|
+
// Remove from registry
|
|
106
|
+
this.clients.delete(name);
|
|
107
|
+
// Remove tools from tool registry
|
|
108
|
+
const registry = this.toolRegistry;
|
|
109
|
+
if (registry) {
|
|
110
|
+
const prefix = `${this.toolPrefix}_${name}_`;
|
|
111
|
+
const toolNamesToRemove = registry
|
|
112
|
+
.getNames()
|
|
113
|
+
.filter((toolName) => toolName.startsWith(prefix));
|
|
114
|
+
for (const toolName of toolNamesToRemove) {
|
|
115
|
+
registry.unregister(toolName);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Get an MCP client by name
|
|
121
|
+
*/
|
|
122
|
+
getServer(name) {
|
|
123
|
+
return this.clients.get(name);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Get all MCP clients
|
|
127
|
+
*/
|
|
128
|
+
getServers() {
|
|
129
|
+
return Array.from(this.clients.values());
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Get all server names
|
|
133
|
+
*/
|
|
134
|
+
getServerNames() {
|
|
135
|
+
return Array.from(this.clients.keys());
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Check if a server exists
|
|
139
|
+
*/
|
|
140
|
+
hasServer(name) {
|
|
141
|
+
return this.clients.has(name);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Discover tools from all connected servers
|
|
145
|
+
*
|
|
146
|
+
* @returns Array of converted tools ready for registration
|
|
147
|
+
*/
|
|
148
|
+
async discoverTools() {
|
|
149
|
+
const allTools = [];
|
|
150
|
+
for (const client of this.clients.values()) {
|
|
151
|
+
if (!client.isConnected)
|
|
152
|
+
continue;
|
|
153
|
+
try {
|
|
154
|
+
const mcpTools = await client.listTools();
|
|
155
|
+
const tools = mcpToolsToTools(mcpTools, client, { prefix: this.toolPrefix });
|
|
156
|
+
allTools.push(...tools);
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
// Log but don't fail - other servers may still work
|
|
160
|
+
this.emit({
|
|
161
|
+
type: 'error',
|
|
162
|
+
serverName: client.name,
|
|
163
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return allTools;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Discover tools from a specific server
|
|
171
|
+
*/
|
|
172
|
+
async discoverToolsFrom(serverName) {
|
|
173
|
+
const client = this.clients.get(serverName);
|
|
174
|
+
if (!client) {
|
|
175
|
+
throw new MCPError(`MCP server '${serverName}' not found`, serverName, MCPErrorCode.SERVER_NOT_FOUND);
|
|
176
|
+
}
|
|
177
|
+
if (!client.isConnected) {
|
|
178
|
+
throw new MCPError(`MCP server '${serverName}' is not connected`, serverName, MCPErrorCode.CONNECTION_FAILED);
|
|
179
|
+
}
|
|
180
|
+
const mcpTools = await client.listTools();
|
|
181
|
+
return mcpToolsToTools(mcpTools, client, { prefix: this.toolPrefix });
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Refresh tools from servers and update the tool registry
|
|
185
|
+
*
|
|
186
|
+
* @param serverName Optional server name to refresh. If not provided, refreshes all.
|
|
187
|
+
*/
|
|
188
|
+
async refreshTools(serverName) {
|
|
189
|
+
const registry = this.toolRegistry;
|
|
190
|
+
if (!registry) {
|
|
191
|
+
return; // No registry to update
|
|
192
|
+
}
|
|
193
|
+
if (serverName) {
|
|
194
|
+
// Refresh single server
|
|
195
|
+
const tools = await this.discoverToolsFrom(serverName);
|
|
196
|
+
const prefix = `${this.toolPrefix}_${serverName}_`;
|
|
197
|
+
// Remove old tools from this server
|
|
198
|
+
const oldToolNames = registry.getNames().filter((toolName) => toolName.startsWith(prefix));
|
|
199
|
+
for (const toolName of oldToolNames) {
|
|
200
|
+
registry.unregister(toolName);
|
|
201
|
+
}
|
|
202
|
+
// Register new tools
|
|
203
|
+
for (const tool of tools) {
|
|
204
|
+
registry.register(tool);
|
|
205
|
+
}
|
|
206
|
+
this.emit({ type: 'tools_changed', serverName, tools: await this.getMCPTools(serverName) });
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
// Refresh all servers
|
|
210
|
+
for (const name of this.clients.keys()) {
|
|
211
|
+
try {
|
|
212
|
+
await this.refreshTools(name);
|
|
213
|
+
}
|
|
214
|
+
catch {
|
|
215
|
+
// Continue with other servers
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Get raw MCP tool definitions from a server
|
|
222
|
+
*/
|
|
223
|
+
async getMCPTools(serverName) {
|
|
224
|
+
const client = this.clients.get(serverName);
|
|
225
|
+
if (!client || !client.isConnected)
|
|
226
|
+
return [];
|
|
227
|
+
return client.listTools();
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Connect all servers
|
|
231
|
+
*/
|
|
232
|
+
async connectAll() {
|
|
233
|
+
const promises = Array.from(this.clients.values()).map(async (client) => {
|
|
234
|
+
if (!client.isConnected) {
|
|
235
|
+
try {
|
|
236
|
+
await client.connect();
|
|
237
|
+
}
|
|
238
|
+
catch (error) {
|
|
239
|
+
this.emit({
|
|
240
|
+
type: 'error',
|
|
241
|
+
serverName: client.name,
|
|
242
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
await Promise.all(promises);
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Disconnect all servers
|
|
251
|
+
*/
|
|
252
|
+
async disconnectAll() {
|
|
253
|
+
const promises = Array.from(this.clients.values()).map((client) => client.disconnect());
|
|
254
|
+
await Promise.all(promises);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Register an event handler
|
|
258
|
+
*/
|
|
259
|
+
onEvent(handler) {
|
|
260
|
+
this.eventHandlers.add(handler);
|
|
261
|
+
return () => this.eventHandlers.delete(handler);
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Emit an event to all handlers
|
|
265
|
+
*/
|
|
266
|
+
emit(event) {
|
|
267
|
+
for (const handler of this.eventHandlers) {
|
|
268
|
+
try {
|
|
269
|
+
handler(event);
|
|
270
|
+
}
|
|
271
|
+
catch {
|
|
272
|
+
// Ignore handler errors
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Tool Conversion Utilities
|
|
3
|
+
*
|
|
4
|
+
* Converts MCP tools to the @compilr-dev/agents Tool format
|
|
5
|
+
*/
|
|
6
|
+
import type { Tool, ToolInputSchema } from '../tools/index.js';
|
|
7
|
+
import { createSuccessResult } from '../tools/index.js';
|
|
8
|
+
import type { MCPClient } from './client.js';
|
|
9
|
+
import type { MCPToolDefinition, MCPToolResult, MCPContentBlock } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Options for converting MCP tools
|
|
12
|
+
*/
|
|
13
|
+
export interface MCPToolConversionOptions {
|
|
14
|
+
/** Prefix for tool names (default: 'mcp') */
|
|
15
|
+
prefix?: string;
|
|
16
|
+
/** Whether to include server name in tool name (default: true) */
|
|
17
|
+
includeServerName?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Convert MCP content blocks to a string result
|
|
21
|
+
*/
|
|
22
|
+
export declare function contentBlocksToString(blocks: MCPContentBlock[]): string;
|
|
23
|
+
/**
|
|
24
|
+
* Convert an MCP tool result to our ToolExecutionResult format
|
|
25
|
+
*/
|
|
26
|
+
export declare function convertMCPResult(result: MCPToolResult): ReturnType<typeof createSuccessResult>;
|
|
27
|
+
/**
|
|
28
|
+
* Convert MCP input schema to our ToolInputSchema format
|
|
29
|
+
*/
|
|
30
|
+
export declare function convertInputSchema(schema: MCPToolDefinition['inputSchema']): ToolInputSchema;
|
|
31
|
+
/**
|
|
32
|
+
* Generate a unique tool name for an MCP tool
|
|
33
|
+
*/
|
|
34
|
+
export declare function generateToolName(serverName: string, toolName: string, options?: MCPToolConversionOptions): string;
|
|
35
|
+
/**
|
|
36
|
+
* Convert a single MCP tool to our Tool format
|
|
37
|
+
*
|
|
38
|
+
* @param mcpTool The MCP tool definition from the server
|
|
39
|
+
* @param client The MCP client to use for calling the tool
|
|
40
|
+
* @param options Conversion options
|
|
41
|
+
* @returns A Tool that can be registered with the agent
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* const tools = await client.listTools();
|
|
46
|
+
* const convertedTools = tools.map(t => mcpToolToTool(t, client));
|
|
47
|
+
* agent.toolRegistry.registerTools(convertedTools);
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export declare function mcpToolToTool(mcpTool: MCPToolDefinition, client: MCPClient, options?: MCPToolConversionOptions): Tool;
|
|
51
|
+
/**
|
|
52
|
+
* Convert multiple MCP tools to our Tool format
|
|
53
|
+
*/
|
|
54
|
+
export declare function mcpToolsToTools(mcpTools: MCPToolDefinition[], client: MCPClient, options?: MCPToolConversionOptions): Tool[];
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Tool Conversion Utilities
|
|
3
|
+
*
|
|
4
|
+
* Converts MCP tools to the @compilr-dev/agents Tool format
|
|
5
|
+
*/
|
|
6
|
+
import { createSuccessResult, createErrorResult } from '../tools/index.js';
|
|
7
|
+
/**
|
|
8
|
+
* Convert MCP content blocks to a string result
|
|
9
|
+
*/
|
|
10
|
+
export function contentBlocksToString(blocks) {
|
|
11
|
+
const parts = [];
|
|
12
|
+
for (const block of blocks) {
|
|
13
|
+
if (block.type === 'text' && block.text) {
|
|
14
|
+
parts.push(block.text);
|
|
15
|
+
}
|
|
16
|
+
else if (block.type === 'image') {
|
|
17
|
+
// For images, return a placeholder with the mime type
|
|
18
|
+
parts.push(`[Image: ${block.mimeType ?? 'unknown type'}]`);
|
|
19
|
+
}
|
|
20
|
+
else if (block.type === 'resource' && block.uri) {
|
|
21
|
+
parts.push(`[Resource: ${block.uri}]`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return parts.join('\n');
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Convert an MCP tool result to our ToolExecutionResult format
|
|
28
|
+
*/
|
|
29
|
+
export function convertMCPResult(result) {
|
|
30
|
+
const content = contentBlocksToString(result.content);
|
|
31
|
+
if (result.isError) {
|
|
32
|
+
return createErrorResult(content || 'Tool execution failed');
|
|
33
|
+
}
|
|
34
|
+
return createSuccessResult(content || 'Success');
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Convert MCP input schema to our ToolInputSchema format
|
|
38
|
+
*/
|
|
39
|
+
export function convertInputSchema(schema) {
|
|
40
|
+
// MCP schemas are already JSON Schema compliant
|
|
41
|
+
// We just need to ensure the structure matches our type
|
|
42
|
+
return {
|
|
43
|
+
type: 'object',
|
|
44
|
+
properties: schema.properties ?? {},
|
|
45
|
+
required: schema.required,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Generate a unique tool name for an MCP tool
|
|
50
|
+
*/
|
|
51
|
+
export function generateToolName(serverName, toolName, options = {}) {
|
|
52
|
+
const prefix = options.prefix ?? 'mcp';
|
|
53
|
+
const includeServer = options.includeServerName ?? true;
|
|
54
|
+
if (includeServer) {
|
|
55
|
+
return `${prefix}_${serverName}_${toolName}`;
|
|
56
|
+
}
|
|
57
|
+
return `${prefix}_${toolName}`;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Convert a single MCP tool to our Tool format
|
|
61
|
+
*
|
|
62
|
+
* @param mcpTool The MCP tool definition from the server
|
|
63
|
+
* @param client The MCP client to use for calling the tool
|
|
64
|
+
* @param options Conversion options
|
|
65
|
+
* @returns A Tool that can be registered with the agent
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* const tools = await client.listTools();
|
|
70
|
+
* const convertedTools = tools.map(t => mcpToolToTool(t, client));
|
|
71
|
+
* agent.toolRegistry.registerTools(convertedTools);
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export function mcpToolToTool(mcpTool, client, options = {}) {
|
|
75
|
+
const toolName = generateToolName(client.name, mcpTool.name, options);
|
|
76
|
+
const definition = {
|
|
77
|
+
name: toolName,
|
|
78
|
+
description: mcpTool.description ?? `MCP tool '${mcpTool.name}' from server '${client.name}'`,
|
|
79
|
+
inputSchema: convertInputSchema(mcpTool.inputSchema),
|
|
80
|
+
};
|
|
81
|
+
return {
|
|
82
|
+
definition,
|
|
83
|
+
execute: async (input) => {
|
|
84
|
+
try {
|
|
85
|
+
const result = await client.callTool(mcpTool.name, input);
|
|
86
|
+
return convertMCPResult(result);
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
return createErrorResult(error instanceof Error ? error.message : String(error));
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Convert multiple MCP tools to our Tool format
|
|
96
|
+
*/
|
|
97
|
+
export function mcpToolsToTools(mcpTools, client, options = {}) {
|
|
98
|
+
return mcpTools.map((tool) => mcpToolToTool(tool, client, options));
|
|
99
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP (Model Context Protocol) types and interfaces
|
|
3
|
+
*
|
|
4
|
+
* MCP is an open protocol by Anthropic that standardizes how AI applications
|
|
5
|
+
* connect to external tools and data sources.
|
|
6
|
+
*
|
|
7
|
+
* @see https://modelcontextprotocol.io/specification/2024-11-05/
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Transport type for MCP server connection
|
|
11
|
+
*/
|
|
12
|
+
export type MCPTransport = 'stdio' | 'http';
|
|
13
|
+
/**
|
|
14
|
+
* Connection status of an MCP client
|
|
15
|
+
*/
|
|
16
|
+
export type MCPConnectionStatus = 'disconnected' | 'connecting' | 'connected' | 'error';
|
|
17
|
+
/**
|
|
18
|
+
* Options for connecting to an MCP server via stdio transport
|
|
19
|
+
*/
|
|
20
|
+
export interface MCPStdioOptions {
|
|
21
|
+
/** Command to spawn (e.g., 'npx', 'node', 'python') */
|
|
22
|
+
command: string;
|
|
23
|
+
/** Arguments to pass to the command */
|
|
24
|
+
args?: string[];
|
|
25
|
+
/** Environment variables for the spawned process */
|
|
26
|
+
env?: Record<string, string>;
|
|
27
|
+
/** Working directory for the spawned process */
|
|
28
|
+
cwd?: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Options for connecting to an MCP server via HTTP transport
|
|
32
|
+
*/
|
|
33
|
+
export interface MCPHttpOptions {
|
|
34
|
+
/** Server URL (e.g., 'http://localhost:3000/mcp') */
|
|
35
|
+
url: string;
|
|
36
|
+
/** Custom headers for HTTP requests */
|
|
37
|
+
headers?: Record<string, string>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Configuration for an MCP client
|
|
41
|
+
*/
|
|
42
|
+
export interface MCPClientConfig {
|
|
43
|
+
/** Unique name/identifier for this server connection */
|
|
44
|
+
name: string;
|
|
45
|
+
/** Transport type */
|
|
46
|
+
transport: MCPTransport;
|
|
47
|
+
/** Stdio transport options (required if transport is 'stdio') */
|
|
48
|
+
stdio?: MCPStdioOptions;
|
|
49
|
+
/** HTTP transport options (required if transport is 'http') */
|
|
50
|
+
http?: MCPHttpOptions;
|
|
51
|
+
/** Connection timeout in milliseconds (default: 30000) */
|
|
52
|
+
timeout?: number;
|
|
53
|
+
/** Client name reported to server (default: '@compilr-dev/agents') */
|
|
54
|
+
clientName?: string;
|
|
55
|
+
/** Client version reported to server (default: '1.0.0') */
|
|
56
|
+
clientVersion?: string;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Simplified configuration for quick server setup
|
|
60
|
+
* Allows shorthand notation for common configurations
|
|
61
|
+
*/
|
|
62
|
+
export interface MCPServerConfig {
|
|
63
|
+
/** Unique name/identifier for this server */
|
|
64
|
+
name: string;
|
|
65
|
+
/** Transport type */
|
|
66
|
+
transport: MCPTransport;
|
|
67
|
+
/** Command to spawn */
|
|
68
|
+
command?: string;
|
|
69
|
+
/** Command arguments */
|
|
70
|
+
args?: string[];
|
|
71
|
+
/** Environment variables */
|
|
72
|
+
env?: Record<string, string>;
|
|
73
|
+
/** Working directory */
|
|
74
|
+
cwd?: string;
|
|
75
|
+
/** Server URL */
|
|
76
|
+
url?: string;
|
|
77
|
+
/** Custom headers */
|
|
78
|
+
headers?: Record<string, string>;
|
|
79
|
+
/** Connection timeout in milliseconds */
|
|
80
|
+
timeout?: number;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* MCP tool definition from the server
|
|
84
|
+
* Based on MCP specification's Tool type
|
|
85
|
+
*/
|
|
86
|
+
export interface MCPToolDefinition {
|
|
87
|
+
/** Tool name (unique identifier) */
|
|
88
|
+
name: string;
|
|
89
|
+
/** Human-readable description */
|
|
90
|
+
description?: string;
|
|
91
|
+
/** JSON Schema for tool input */
|
|
92
|
+
inputSchema: {
|
|
93
|
+
type: 'object';
|
|
94
|
+
properties?: Record<string, unknown>;
|
|
95
|
+
required?: string[];
|
|
96
|
+
additionalProperties?: boolean;
|
|
97
|
+
[key: string]: unknown;
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Content block in MCP tool result
|
|
102
|
+
*/
|
|
103
|
+
export interface MCPContentBlock {
|
|
104
|
+
type: 'text' | 'image' | 'resource' | 'audio';
|
|
105
|
+
text?: string;
|
|
106
|
+
data?: string;
|
|
107
|
+
mimeType?: string;
|
|
108
|
+
uri?: string;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Result from calling an MCP tool
|
|
112
|
+
*/
|
|
113
|
+
export interface MCPToolResult {
|
|
114
|
+
/** Content blocks (text, images, resources) */
|
|
115
|
+
content: MCPContentBlock[];
|
|
116
|
+
/** Whether the tool execution resulted in an error */
|
|
117
|
+
isError?: boolean;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Event types emitted by MCPClient
|
|
121
|
+
*/
|
|
122
|
+
export type MCPClientEventType = 'tools_changed' | 'error' | 'disconnected' | 'connected';
|
|
123
|
+
/**
|
|
124
|
+
* Event emitted by MCPClient
|
|
125
|
+
*/
|
|
126
|
+
export interface MCPClientEvent {
|
|
127
|
+
type: MCPClientEventType;
|
|
128
|
+
serverName: string;
|
|
129
|
+
error?: Error;
|
|
130
|
+
tools?: MCPToolDefinition[];
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Handler for MCP client events
|
|
134
|
+
*/
|
|
135
|
+
export type MCPClientEventHandler = (event: MCPClientEvent) => void;
|
|
136
|
+
/**
|
|
137
|
+
* Options for MCPManager
|
|
138
|
+
*/
|
|
139
|
+
export interface MCPManagerOptions {
|
|
140
|
+
/** Prefix for tool names (default: 'mcp') */
|
|
141
|
+
toolPrefix?: string;
|
|
142
|
+
/** Whether to auto-connect servers when added (default: true) */
|
|
143
|
+
autoConnect?: boolean;
|
|
144
|
+
/** Event handler for manager events */
|
|
145
|
+
onEvent?: MCPClientEventHandler;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Convert shorthand MCPServerConfig to full MCPClientConfig
|
|
149
|
+
*/
|
|
150
|
+
export declare function normalizeServerConfig(config: MCPServerConfig): MCPClientConfig;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP (Model Context Protocol) types and interfaces
|
|
3
|
+
*
|
|
4
|
+
* MCP is an open protocol by Anthropic that standardizes how AI applications
|
|
5
|
+
* connect to external tools and data sources.
|
|
6
|
+
*
|
|
7
|
+
* @see https://modelcontextprotocol.io/specification/2024-11-05/
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Convert shorthand MCPServerConfig to full MCPClientConfig
|
|
11
|
+
*/
|
|
12
|
+
export function normalizeServerConfig(config) {
|
|
13
|
+
const normalized = {
|
|
14
|
+
name: config.name,
|
|
15
|
+
transport: config.transport,
|
|
16
|
+
timeout: config.timeout,
|
|
17
|
+
};
|
|
18
|
+
if (config.transport === 'stdio') {
|
|
19
|
+
if (!config.command) {
|
|
20
|
+
throw new Error(`MCP server '${config.name}': stdio transport requires 'command'`);
|
|
21
|
+
}
|
|
22
|
+
normalized.stdio = {
|
|
23
|
+
command: config.command,
|
|
24
|
+
args: config.args,
|
|
25
|
+
env: config.env,
|
|
26
|
+
cwd: config.cwd,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
// transport === 'http'
|
|
31
|
+
if (!config.url) {
|
|
32
|
+
throw new Error(`MCP server '${config.name}': http transport requires 'url'`);
|
|
33
|
+
}
|
|
34
|
+
normalized.http = {
|
|
35
|
+
url: config.url,
|
|
36
|
+
headers: config.headers,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
return normalized;
|
|
40
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory module - Project-level instruction loading
|
|
3
|
+
*
|
|
4
|
+
* Supports various LLM-specific naming conventions and generic patterns
|
|
5
|
+
* for discovering and loading project instructions.
|
|
6
|
+
*/
|
|
7
|
+
export { ProjectMemoryLoader, createProjectMemoryLoader, loadProjectMemory, hasProjectMemory, getProviderPatterns, getSupportedProviders, getGenericPatterns, PROVIDER_PATTERNS, GENERIC_PATTERNS, } from './loader.js';
|
|
8
|
+
export type { LLMProviderName, MemoryFile, ProjectMemory, FilePattern, CombineStrategy, ProjectMemoryOptions, ProjectMemoryEventType, ProjectMemoryEvent, ProjectMemoryEventHandler, MemoryDiscoveryResult, ProviderPatterns, } from './types.js';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory module - Project-level instruction loading
|
|
3
|
+
*
|
|
4
|
+
* Supports various LLM-specific naming conventions and generic patterns
|
|
5
|
+
* for discovering and loading project instructions.
|
|
6
|
+
*/
|
|
7
|
+
export { ProjectMemoryLoader, createProjectMemoryLoader, loadProjectMemory, hasProjectMemory, getProviderPatterns, getSupportedProviders, getGenericPatterns, PROVIDER_PATTERNS, GENERIC_PATTERNS, } from './loader.js';
|