@simulacra-ai/mcp 0.0.2 → 0.0.4
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 +10 -8
- package/dist/index.cjs +261 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +127 -0
- package/dist/index.d.ts +127 -4
- package/dist/index.js +232 -3
- package/dist/index.js.map +1 -1
- package/package.json +11 -5
- package/dist/index.d.ts.map +0 -1
- package/dist/mcp-tool-provider.d.ts +0 -58
- package/dist/mcp-tool-provider.d.ts.map +0 -1
- package/dist/mcp-tool-provider.js +0 -159
- package/dist/mcp-tool-provider.js.map +0 -1
- package/dist/schema-converter.d.ts +0 -13
- package/dist/schema-converter.d.ts.map +0 -1
- package/dist/schema-converter.js +0 -103
- package/dist/schema-converter.js.map +0 -1
- package/dist/types.d.ts +0 -57
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Simulacra MCP Bridge
|
|
2
2
|
|
|
3
|
-
MCP (Model Context Protocol)
|
|
3
|
+
The MCP (Model Context Protocol) bridge connects Simulacra to MCP tool servers and exposes their tools as Simulacra tool classes, so the model can use them without writing custom tool implementations.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -12,10 +12,9 @@ npm install @simulacra-ai/core @simulacra-ai/mcp @modelcontextprotocol/sdk
|
|
|
12
12
|
|
|
13
13
|
```typescript
|
|
14
14
|
import { Conversation, WorkflowManager } from "@simulacra-ai/core";
|
|
15
|
-
import { AnthropicProvider } from "@simulacra-ai/anthropic";
|
|
16
|
-
import Anthropic from "@anthropic-ai/sdk";
|
|
17
15
|
import { McpToolProvider } from "@simulacra-ai/mcp";
|
|
18
16
|
|
|
17
|
+
// connect to an MCP server
|
|
19
18
|
const mcp = new McpToolProvider({
|
|
20
19
|
servers: [
|
|
21
20
|
{
|
|
@@ -25,21 +24,23 @@ const mcp = new McpToolProvider({
|
|
|
25
24
|
},
|
|
26
25
|
],
|
|
27
26
|
});
|
|
28
|
-
|
|
29
27
|
await mcp.connect();
|
|
30
28
|
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
// add MCP tools to the conversation's toolkit
|
|
30
|
+
using conversation = new Conversation(provider);
|
|
33
31
|
conversation.toolkit = [...conversation.toolkit, ...mcp.getToolClasses()];
|
|
34
|
-
|
|
32
|
+
using manager = new WorkflowManager(conversation);
|
|
35
33
|
|
|
36
34
|
await conversation.prompt("List the files in /tmp");
|
|
37
35
|
|
|
36
|
+
// disconnect when done
|
|
38
37
|
await mcp.disconnect();
|
|
39
38
|
```
|
|
40
39
|
|
|
41
40
|
### Multiple Servers
|
|
42
41
|
|
|
42
|
+
Multiple MCP servers can be configured at once. Tools from all servers are merged into a single toolkit.
|
|
43
|
+
|
|
43
44
|
```typescript
|
|
44
45
|
const mcp = new McpToolProvider({
|
|
45
46
|
servers: [
|
|
@@ -60,9 +61,10 @@ const mcp = new McpToolProvider({
|
|
|
60
61
|
|
|
61
62
|
### Tool Overrides
|
|
62
63
|
|
|
63
|
-
By default, all MCP tools are `parallelizable: true`.
|
|
64
|
+
By default, all MCP tools are `parallelizable: true`. Specific tools with side effects or ordering requirements can be overridden.
|
|
64
65
|
|
|
65
66
|
```typescript
|
|
67
|
+
// inside the servers array
|
|
66
68
|
{
|
|
67
69
|
name: "database",
|
|
68
70
|
command: "node",
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
McpToolProvider: () => McpToolProvider,
|
|
24
|
+
convertJsonSchemaToParameters: () => convertJsonSchemaToParameters
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/schema-converter.ts
|
|
29
|
+
function convertProperty(name, prop, required) {
|
|
30
|
+
const param_type = convertType(prop, required);
|
|
31
|
+
const def = { ...param_type, name };
|
|
32
|
+
if (prop.description) {
|
|
33
|
+
def.description = prop.description;
|
|
34
|
+
}
|
|
35
|
+
return def;
|
|
36
|
+
}
|
|
37
|
+
function convertType(prop, required) {
|
|
38
|
+
if (prop.type === "object" && prop.properties) {
|
|
39
|
+
const nested = {};
|
|
40
|
+
for (const [key, value] of Object.entries(prop.properties)) {
|
|
41
|
+
const child_required = prop.required?.includes(key) ?? false;
|
|
42
|
+
nested[key] = convertType(value, child_required);
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
type: "object",
|
|
46
|
+
required,
|
|
47
|
+
properties: nested,
|
|
48
|
+
...prop.description ? { description: prop.description } : {}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
if (prop.type === "array" && prop.items) {
|
|
52
|
+
return {
|
|
53
|
+
type: "array",
|
|
54
|
+
required,
|
|
55
|
+
items: convertType(prop.items, false),
|
|
56
|
+
...prop.description ? { description: prop.description } : {}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
if (prop.type === "string" && prop.enum) {
|
|
60
|
+
return required ? {
|
|
61
|
+
type: "string",
|
|
62
|
+
required: true,
|
|
63
|
+
enum: prop.enum,
|
|
64
|
+
...prop.description ? { description: prop.description } : {}
|
|
65
|
+
} : {
|
|
66
|
+
type: "string",
|
|
67
|
+
required: false,
|
|
68
|
+
enum: prop.enum,
|
|
69
|
+
...prop.description ? { description: prop.description } : {}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
if (prop.type === "number" || prop.type === "integer") {
|
|
73
|
+
return required ? {
|
|
74
|
+
type: "number",
|
|
75
|
+
required: true,
|
|
76
|
+
...prop.description ? { description: prop.description } : {}
|
|
77
|
+
} : {
|
|
78
|
+
type: "number",
|
|
79
|
+
required: false,
|
|
80
|
+
...prop.description ? { description: prop.description } : {}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
if (prop.type === "boolean") {
|
|
84
|
+
return required ? {
|
|
85
|
+
type: "boolean",
|
|
86
|
+
required: true,
|
|
87
|
+
...prop.description ? { description: prop.description } : {}
|
|
88
|
+
} : {
|
|
89
|
+
type: "boolean",
|
|
90
|
+
required: false,
|
|
91
|
+
...prop.description ? { description: prop.description } : {}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
return required ? {
|
|
95
|
+
type: "string",
|
|
96
|
+
required: true,
|
|
97
|
+
...prop.description ? { description: prop.description } : {}
|
|
98
|
+
} : {
|
|
99
|
+
type: "string",
|
|
100
|
+
required: false,
|
|
101
|
+
...prop.description ? { description: prop.description } : {}
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function convertJsonSchemaToParameters(schema) {
|
|
105
|
+
const s = schema;
|
|
106
|
+
if (!s.properties) {
|
|
107
|
+
return [];
|
|
108
|
+
}
|
|
109
|
+
const required_set = new Set(s.required ?? []);
|
|
110
|
+
return Object.entries(s.properties).map(
|
|
111
|
+
([name, prop]) => convertProperty(name, prop, required_set.has(name))
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// src/mcp-tool-provider.ts
|
|
116
|
+
var import_client = require("@modelcontextprotocol/sdk/client/index.js");
|
|
117
|
+
var import_stdio = require("@modelcontextprotocol/sdk/client/stdio.js");
|
|
118
|
+
var McpToolProvider = class {
|
|
119
|
+
#config;
|
|
120
|
+
#servers = [];
|
|
121
|
+
#tool_classes = [];
|
|
122
|
+
/**
|
|
123
|
+
* Creates a new MCP tool provider.
|
|
124
|
+
*
|
|
125
|
+
* @param config - Configuration specifying which MCP servers to connect to.
|
|
126
|
+
*/
|
|
127
|
+
constructor(config) {
|
|
128
|
+
this.#config = config;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Establishes connections to all configured MCP servers and retrieves their tools.
|
|
132
|
+
*
|
|
133
|
+
* This method spawns each server process, establishes communication over stdio,
|
|
134
|
+
* and queries each server for its available tools. The tools are converted to
|
|
135
|
+
* Simulacra-compatible tool classes and stored internally.
|
|
136
|
+
*
|
|
137
|
+
* @returns A promise that resolves when all servers are connected and tools are loaded.
|
|
138
|
+
*/
|
|
139
|
+
async connect() {
|
|
140
|
+
const connected = [];
|
|
141
|
+
const added_tool_classes = [];
|
|
142
|
+
try {
|
|
143
|
+
for (const server_config of this.#config.servers) {
|
|
144
|
+
const transport = new import_stdio.StdioClientTransport({
|
|
145
|
+
command: server_config.command,
|
|
146
|
+
args: server_config.args,
|
|
147
|
+
env: server_config.env
|
|
148
|
+
});
|
|
149
|
+
const client = new import_client.Client({ name: "simulacra", version: "0.1.0" }, { capabilities: {} });
|
|
150
|
+
await client.connect(transport);
|
|
151
|
+
const server = { config: server_config, client, transport };
|
|
152
|
+
connected.push(server);
|
|
153
|
+
this.#servers.push(server);
|
|
154
|
+
const tools_result = await client.listTools();
|
|
155
|
+
for (const mcp_tool of tools_result.tools) {
|
|
156
|
+
const tool_class = this.#create_tool_class(client, mcp_tool, server_config);
|
|
157
|
+
added_tool_classes.push(tool_class);
|
|
158
|
+
this.#tool_classes.push(tool_class);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
} catch (error) {
|
|
162
|
+
for (const server of connected) {
|
|
163
|
+
await server.client.close().catch((close_error) => {
|
|
164
|
+
this.#config.on_error?.({
|
|
165
|
+
error: close_error,
|
|
166
|
+
operation: "disconnect",
|
|
167
|
+
context: { during: "connect_rollback" }
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
this.#servers = this.#servers.filter((s) => !connected.includes(s));
|
|
172
|
+
this.#tool_classes = this.#tool_classes.filter((t) => !added_tool_classes.includes(t));
|
|
173
|
+
throw error;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Closes all MCP server connections and clears the cached tool classes.
|
|
178
|
+
*
|
|
179
|
+
* This method gracefully shuts down all server processes and cleans up
|
|
180
|
+
* internal state. After calling this method, the tool classes are no longer
|
|
181
|
+
* available.
|
|
182
|
+
*
|
|
183
|
+
* @returns A promise that resolves when all servers are disconnected.
|
|
184
|
+
*/
|
|
185
|
+
async disconnect() {
|
|
186
|
+
await Promise.allSettled(this.#servers.map((s) => s.client.close()));
|
|
187
|
+
this.#servers = [];
|
|
188
|
+
this.#tool_classes = [];
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Enables automatic cleanup when the provider is disposed.
|
|
192
|
+
*
|
|
193
|
+
* This method implements the disposable pattern, allowing the provider to be
|
|
194
|
+
* used with explicit resource management syntax (using keyword). When disposed,
|
|
195
|
+
* it attempts to disconnect from all servers.
|
|
196
|
+
*/
|
|
197
|
+
[Symbol.dispose]() {
|
|
198
|
+
this.disconnect().catch((error) => {
|
|
199
|
+
this.#config.on_error?.({ error, operation: "disconnect", context: { during: "dispose" } });
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Retrieves all tool classes from connected MCP servers.
|
|
204
|
+
*
|
|
205
|
+
* This method returns a shallow copy of the internal tool classes array,
|
|
206
|
+
* containing Simulacra-compatible tool classes for all tools from all
|
|
207
|
+
* connected servers.
|
|
208
|
+
*
|
|
209
|
+
* @returns An array of tool classes ready to be used in a Simulacra workflow.
|
|
210
|
+
*/
|
|
211
|
+
getToolClasses() {
|
|
212
|
+
return [...this.#tool_classes];
|
|
213
|
+
}
|
|
214
|
+
#create_tool_class(client, mcp_tool, server_config) {
|
|
215
|
+
const parameters = mcp_tool.inputSchema ? convertJsonSchemaToParameters(mcp_tool.inputSchema) : [];
|
|
216
|
+
const parallelizable = server_config.tool_overrides?.[mcp_tool.name]?.parallelizable;
|
|
217
|
+
const definition = {
|
|
218
|
+
name: mcp_tool.name,
|
|
219
|
+
description: mcp_tool.description ?? "",
|
|
220
|
+
parameters,
|
|
221
|
+
...parallelizable !== void 0 ? { parallelizable } : {}
|
|
222
|
+
};
|
|
223
|
+
const McpTool = class {
|
|
224
|
+
static get_definition() {
|
|
225
|
+
return definition;
|
|
226
|
+
}
|
|
227
|
+
constructor(_context) {
|
|
228
|
+
}
|
|
229
|
+
async execute(params) {
|
|
230
|
+
try {
|
|
231
|
+
const result = await client.callTool({
|
|
232
|
+
name: mcp_tool.name,
|
|
233
|
+
arguments: params
|
|
234
|
+
});
|
|
235
|
+
if (result.isError) {
|
|
236
|
+
const text2 = result.content?.filter((c) => c.type === "text").map((c) => c.text ?? "").join("\n") || "MCP tool error";
|
|
237
|
+
return { result: false, message: text2 };
|
|
238
|
+
}
|
|
239
|
+
const text = result.content?.filter((c) => c.type === "text").map((c) => c.text ?? "").join("\n");
|
|
240
|
+
return {
|
|
241
|
+
result: true,
|
|
242
|
+
...{ output: text || "success" }
|
|
243
|
+
};
|
|
244
|
+
} catch (error) {
|
|
245
|
+
return {
|
|
246
|
+
result: false,
|
|
247
|
+
message: error instanceof Error ? error.message : String(error),
|
|
248
|
+
error
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
return McpTool;
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
257
|
+
0 && (module.exports = {
|
|
258
|
+
McpToolProvider,
|
|
259
|
+
convertJsonSchemaToParameters
|
|
260
|
+
});
|
|
261
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/schema-converter.ts","../src/mcp-tool-provider.ts"],"sourcesContent":["export * from \"./types.ts\";\nexport * from \"./schema-converter.ts\";\nexport * from \"./mcp-tool-provider.ts\";\n","import type { ToolParameterDefinition, ParameterType } from \"@simulacra-ai/core\";\n\ninterface JsonSchemaProperty {\n type?: string;\n description?: string;\n enum?: string[];\n properties?: Record<string, JsonSchemaProperty>;\n required?: string[];\n items?: JsonSchemaProperty;\n default?: unknown;\n}\n\ninterface JsonSchema {\n type?: string;\n properties?: Record<string, JsonSchemaProperty>;\n required?: string[];\n}\n\nfunction convertProperty(\n name: string,\n prop: JsonSchemaProperty,\n required: boolean,\n): ToolParameterDefinition {\n const param_type = convertType(prop, required);\n const def: ToolParameterDefinition = { ...param_type, name };\n if (prop.description) {\n def.description = prop.description;\n }\n return def;\n}\n\nfunction convertType(prop: JsonSchemaProperty, required: boolean): ParameterType {\n if (prop.type === \"object\" && prop.properties) {\n const nested: Record<string, ParameterType> = {};\n for (const [key, value] of Object.entries(prop.properties)) {\n const child_required = prop.required?.includes(key) ?? false;\n nested[key] = convertType(value, child_required);\n }\n return {\n type: \"object\",\n required,\n properties: nested,\n ...(prop.description ? { description: prop.description } : {}),\n };\n }\n\n if (prop.type === \"array\" && prop.items) {\n return {\n type: \"array\",\n required,\n items: convertType(prop.items, false),\n ...(prop.description ? { description: prop.description } : {}),\n };\n }\n\n if (prop.type === \"string\" && prop.enum) {\n return required\n ? {\n type: \"string\",\n required: true,\n enum: prop.enum,\n ...(prop.description ? { description: prop.description } : {}),\n }\n : {\n type: \"string\",\n required: false,\n enum: prop.enum,\n ...(prop.description ? { description: prop.description } : {}),\n };\n }\n\n if (prop.type === \"number\" || prop.type === \"integer\") {\n return required\n ? {\n type: \"number\",\n required: true,\n ...(prop.description ? { description: prop.description } : {}),\n }\n : {\n type: \"number\",\n required: false,\n ...(prop.description ? { description: prop.description } : {}),\n };\n }\n\n if (prop.type === \"boolean\") {\n return required\n ? {\n type: \"boolean\",\n required: true,\n ...(prop.description ? { description: prop.description } : {}),\n }\n : {\n type: \"boolean\",\n required: false,\n ...(prop.description ? { description: prop.description } : {}),\n };\n }\n\n // Default: treat as string (covers type === \"string\" and unknown types)\n return required\n ? {\n type: \"string\",\n required: true,\n ...(prop.description ? { description: prop.description } : {}),\n }\n : {\n type: \"string\",\n required: false,\n ...(prop.description ? { description: prop.description } : {}),\n };\n}\n\n/**\n * Converts a JSON Schema object into an array of Simulacra tool parameter definitions.\n *\n * This function transforms MCP tool input schemas (which use JSON Schema format)\n * into the parameter format expected by Simulacra tools. It handles nested objects,\n * arrays, enums, and all standard JSON Schema primitive types.\n *\n * @param schema - A JSON Schema object describing the tool's input parameters.\n * @returns An array of tool parameter definitions compatible with Simulacra.\n */\nexport function convertJsonSchemaToParameters(\n schema: Record<string, unknown>,\n): ToolParameterDefinition[] {\n const s = schema as unknown as JsonSchema;\n if (!s.properties) {\n return [];\n }\n\n const required_set = new Set(s.required ?? []);\n return Object.entries(s.properties).map(([name, prop]) =>\n convertProperty(name, prop, required_set.has(name)),\n );\n}\n","import { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\nimport { StdioClientTransport } from \"@modelcontextprotocol/sdk/client/stdio.js\";\nimport type {\n ToolClass,\n ToolDefinition,\n ToolContext,\n ToolSuccessResult,\n ToolErrorResult,\n} from \"@simulacra-ai/core\";\nimport { convertJsonSchemaToParameters } from \"./schema-converter.ts\";\nimport type { McpServerConfig, McpToolProviderConfig } from \"./types.ts\";\n\ninterface ConnectedServer {\n config: McpServerConfig;\n client: Client;\n transport: StdioClientTransport;\n}\n\n/**\n * A provider that connects to Model Context Protocol (MCP) servers and exposes\n * their tools as Simulacra-compatible tool classes.\n *\n * This class manages the lifecycle of MCP server connections, converts MCP tools\n * into the Simulacra tool format, and provides access to all tools from connected\n * servers.\n */\nexport class McpToolProvider {\n readonly #config: McpToolProviderConfig;\n #servers: ConnectedServer[] = [];\n #tool_classes: ToolClass[] = [];\n\n /**\n * Creates a new MCP tool provider.\n *\n * @param config - Configuration specifying which MCP servers to connect to.\n */\n constructor(config: McpToolProviderConfig) {\n this.#config = config;\n }\n\n /**\n * Establishes connections to all configured MCP servers and retrieves their tools.\n *\n * This method spawns each server process, establishes communication over stdio,\n * and queries each server for its available tools. The tools are converted to\n * Simulacra-compatible tool classes and stored internally.\n *\n * @returns A promise that resolves when all servers are connected and tools are loaded.\n */\n async connect(): Promise<void> {\n const connected: ConnectedServer[] = [];\n const added_tool_classes: ToolClass[] = [];\n try {\n for (const server_config of this.#config.servers) {\n const transport = new StdioClientTransport({\n command: server_config.command,\n args: server_config.args,\n env: server_config.env,\n });\n\n const client = new Client({ name: \"simulacra\", version: \"0.1.0\" }, { capabilities: {} });\n\n await client.connect(transport);\n\n const server: ConnectedServer = { config: server_config, client, transport };\n connected.push(server);\n this.#servers.push(server);\n\n const tools_result = await client.listTools();\n\n for (const mcp_tool of tools_result.tools) {\n const tool_class = this.#create_tool_class(client, mcp_tool, server_config);\n added_tool_classes.push(tool_class);\n this.#tool_classes.push(tool_class);\n }\n }\n } catch (error) {\n for (const server of connected) {\n await server.client.close().catch((close_error) => {\n this.#config.on_error?.({\n error: close_error,\n operation: \"disconnect\",\n context: { during: \"connect_rollback\" },\n });\n });\n }\n this.#servers = this.#servers.filter((s) => !connected.includes(s));\n this.#tool_classes = this.#tool_classes.filter((t) => !added_tool_classes.includes(t));\n throw error;\n }\n }\n\n /**\n * Closes all MCP server connections and clears the cached tool classes.\n *\n * This method gracefully shuts down all server processes and cleans up\n * internal state. After calling this method, the tool classes are no longer\n * available.\n *\n * @returns A promise that resolves when all servers are disconnected.\n */\n async disconnect(): Promise<void> {\n await Promise.allSettled(this.#servers.map((s) => s.client.close()));\n this.#servers = [];\n this.#tool_classes = [];\n }\n\n /**\n * Enables automatic cleanup when the provider is disposed.\n *\n * This method implements the disposable pattern, allowing the provider to be\n * used with explicit resource management syntax (using keyword). When disposed,\n * it attempts to disconnect from all servers.\n */\n [Symbol.dispose]() {\n this.disconnect().catch((error) => {\n this.#config.on_error?.({ error, operation: \"disconnect\", context: { during: \"dispose\" } });\n });\n }\n\n /**\n * Retrieves all tool classes from connected MCP servers.\n *\n * This method returns a shallow copy of the internal tool classes array,\n * containing Simulacra-compatible tool classes for all tools from all\n * connected servers.\n *\n * @returns An array of tool classes ready to be used in a Simulacra workflow.\n */\n getToolClasses(): ToolClass[] {\n return [...this.#tool_classes];\n }\n\n #create_tool_class(\n client: Client,\n mcp_tool: { name: string; description?: string; inputSchema?: unknown },\n server_config: McpServerConfig,\n ): ToolClass {\n const parameters = mcp_tool.inputSchema\n ? convertJsonSchemaToParameters(mcp_tool.inputSchema as Record<string, unknown>)\n : [];\n\n const parallelizable = server_config.tool_overrides?.[mcp_tool.name]?.parallelizable;\n\n const definition: ToolDefinition = {\n name: mcp_tool.name,\n description: mcp_tool.description ?? \"\",\n parameters,\n ...(parallelizable !== undefined ? { parallelizable } : {}),\n };\n\n const McpTool = class {\n static get_definition() {\n return definition;\n }\n\n constructor(_context: ToolContext) {}\n\n async execute(params: Record<string, unknown>): Promise<ToolSuccessResult | ToolErrorResult> {\n try {\n const result = await client.callTool({\n name: mcp_tool.name,\n arguments: params,\n });\n\n if (result.isError) {\n const text =\n (result.content as Array<{ type: string; text?: string }>)\n ?.filter((c) => c.type === \"text\")\n .map((c) => c.text ?? \"\")\n .join(\"\\n\") || \"MCP tool error\";\n return { result: false, message: text };\n }\n\n const text = (result.content as Array<{ type: string; text?: string }>)\n ?.filter((c) => c.type === \"text\")\n .map((c) => c.text ?? \"\")\n .join(\"\\n\");\n return {\n result: true,\n ...({ output: text || \"success\" } as Record<string, unknown>),\n } as ToolSuccessResult;\n } catch (error) {\n return {\n result: false,\n message: error instanceof Error ? error.message : String(error),\n error,\n };\n }\n }\n };\n\n return McpTool as unknown as ToolClass;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACkBA,SAAS,gBACP,MACA,MACA,UACyB;AACzB,QAAM,aAAa,YAAY,MAAM,QAAQ;AAC7C,QAAM,MAA+B,EAAE,GAAG,YAAY,KAAK;AAC3D,MAAI,KAAK,aAAa;AACpB,QAAI,cAAc,KAAK;AAAA,EACzB;AACA,SAAO;AACT;AAEA,SAAS,YAAY,MAA0B,UAAkC;AAC/E,MAAI,KAAK,SAAS,YAAY,KAAK,YAAY;AAC7C,UAAM,SAAwC,CAAC;AAC/C,eAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,KAAK,UAAU,GAAG;AAC1D,YAAM,iBAAiB,KAAK,UAAU,SAAS,GAAG,KAAK;AACvD,aAAO,GAAG,IAAI,YAAY,OAAO,cAAc;AAAA,IACjD;AACA,WAAO;AAAA,MACL,MAAM;AAAA,MACN;AAAA,MACA,YAAY;AAAA,MACZ,GAAI,KAAK,cAAc,EAAE,aAAa,KAAK,YAAY,IAAI,CAAC;AAAA,IAC9D;AAAA,EACF;AAEA,MAAI,KAAK,SAAS,WAAW,KAAK,OAAO;AACvC,WAAO;AAAA,MACL,MAAM;AAAA,MACN;AAAA,MACA,OAAO,YAAY,KAAK,OAAO,KAAK;AAAA,MACpC,GAAI,KAAK,cAAc,EAAE,aAAa,KAAK,YAAY,IAAI,CAAC;AAAA,IAC9D;AAAA,EACF;AAEA,MAAI,KAAK,SAAS,YAAY,KAAK,MAAM;AACvC,WAAO,WACH;AAAA,MACE,MAAM;AAAA,MACN,UAAU;AAAA,MACV,MAAM,KAAK;AAAA,MACX,GAAI,KAAK,cAA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ACtB,eAAO;AAAA,MACT;AAAA,MAEA,YAAY,UAAuB;AAAA,MAAC;AAAA,MAEpC,MAAM,QAAQ,QAA+E;AAC3F,YAAI;AACF,gBAAM,SAAS,MAAM,OAAO,SAAS;AAAA,YACnC,MAAM,SAAS;AAAA,YACf,WAAW;AAAA,UACb,CAAC;AAED,cAAI,OAAO,SAAS;AAClB,kBAAMA,QACH,OAAO,SACJ,OAAO,CAAC,MAAM,EAAE,SAAS,MAAM,EAChC,IAAI,CAAC,MAAM,EAAE,QAAQ,EAAE,EACvB,KAAK,IAAI,KAAK;AACnB,mBAAO,EAAE,QAAQ,OAAO,SAASA,MAAK;AAAA,UACxC;AAEA,gBAAM,OAAQ,OAAO,SACjB,OAAO,CAAC,MAAM,EAAE,SAAS,MAAM,EAChC,IAAI,CAAC,MAAM,EAAE,QAAQ,EAAE,EACvB,KAAK,IAAI;AACZ,iBAAO;AAAA,YACL,QAAQ;AAAA,YACR,GAAI,EAAE,QAAQ,QAAQ,UAAU;AAAA,UAClC;AAAA,QACF,SAAS,OAAO;AACd,iBAAO;AAAA,YACL,QAAQ;AAAA,YACR,SAAS,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AAAA,YAC9D;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AACF;","names":["text"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { LifecycleErrorEvent, ToolParameterDefinition, ToolClass } from '@simulacra-ai/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Configuration for a single Model Context Protocol (MCP) server connection.
|
|
5
|
+
*
|
|
6
|
+
* This interface defines how to connect to and configure an MCP server,
|
|
7
|
+
* including the command to execute, environment variables, and tool-specific
|
|
8
|
+
* overrides.
|
|
9
|
+
*/
|
|
10
|
+
interface McpServerConfig {
|
|
11
|
+
/**
|
|
12
|
+
* A unique identifier for this MCP server.
|
|
13
|
+
*/
|
|
14
|
+
name: string;
|
|
15
|
+
/**
|
|
16
|
+
* The command to execute to start the MCP server process.
|
|
17
|
+
*/
|
|
18
|
+
command: string;
|
|
19
|
+
/**
|
|
20
|
+
* Command-line arguments to pass to the server command.
|
|
21
|
+
*/
|
|
22
|
+
args?: string[];
|
|
23
|
+
/**
|
|
24
|
+
* Environment variables to set for the server process.
|
|
25
|
+
*/
|
|
26
|
+
env?: Record<string, string>;
|
|
27
|
+
/**
|
|
28
|
+
* The transport mechanism used to communicate with the server.
|
|
29
|
+
* Currently only "stdio" is supported.
|
|
30
|
+
*/
|
|
31
|
+
transport?: "stdio";
|
|
32
|
+
/**
|
|
33
|
+
* Tool-specific configuration overrides, keyed by tool name.
|
|
34
|
+
*/
|
|
35
|
+
tool_overrides?: Record<string, {
|
|
36
|
+
/**
|
|
37
|
+
* Whether this tool can be executed in parallel with other tools.
|
|
38
|
+
*/
|
|
39
|
+
parallelizable?: boolean;
|
|
40
|
+
}>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Configuration for the MCP tool provider.
|
|
44
|
+
*
|
|
45
|
+
* This interface defines the configuration needed to connect to one or more
|
|
46
|
+
* MCP servers and make their tools available to a Simulacra workflow.
|
|
47
|
+
*/
|
|
48
|
+
interface McpToolProviderConfig {
|
|
49
|
+
/**
|
|
50
|
+
* An array of MCP server configurations to connect to.
|
|
51
|
+
*/
|
|
52
|
+
servers: McpServerConfig[];
|
|
53
|
+
/**
|
|
54
|
+
* Optional callback invoked when a background operation fails during cleanup or disposal.
|
|
55
|
+
*/
|
|
56
|
+
on_error?: (event: LifecycleErrorEvent) => void;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Converts a JSON Schema object into an array of Simulacra tool parameter definitions.
|
|
61
|
+
*
|
|
62
|
+
* This function transforms MCP tool input schemas (which use JSON Schema format)
|
|
63
|
+
* into the parameter format expected by Simulacra tools. It handles nested objects,
|
|
64
|
+
* arrays, enums, and all standard JSON Schema primitive types.
|
|
65
|
+
*
|
|
66
|
+
* @param schema - A JSON Schema object describing the tool's input parameters.
|
|
67
|
+
* @returns An array of tool parameter definitions compatible with Simulacra.
|
|
68
|
+
*/
|
|
69
|
+
declare function convertJsonSchemaToParameters(schema: Record<string, unknown>): ToolParameterDefinition[];
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* A provider that connects to Model Context Protocol (MCP) servers and exposes
|
|
73
|
+
* their tools as Simulacra-compatible tool classes.
|
|
74
|
+
*
|
|
75
|
+
* This class manages the lifecycle of MCP server connections, converts MCP tools
|
|
76
|
+
* into the Simulacra tool format, and provides access to all tools from connected
|
|
77
|
+
* servers.
|
|
78
|
+
*/
|
|
79
|
+
declare class McpToolProvider {
|
|
80
|
+
#private;
|
|
81
|
+
/**
|
|
82
|
+
* Creates a new MCP tool provider.
|
|
83
|
+
*
|
|
84
|
+
* @param config - Configuration specifying which MCP servers to connect to.
|
|
85
|
+
*/
|
|
86
|
+
constructor(config: McpToolProviderConfig);
|
|
87
|
+
/**
|
|
88
|
+
* Establishes connections to all configured MCP servers and retrieves their tools.
|
|
89
|
+
*
|
|
90
|
+
* This method spawns each server process, establishes communication over stdio,
|
|
91
|
+
* and queries each server for its available tools. The tools are converted to
|
|
92
|
+
* Simulacra-compatible tool classes and stored internally.
|
|
93
|
+
*
|
|
94
|
+
* @returns A promise that resolves when all servers are connected and tools are loaded.
|
|
95
|
+
*/
|
|
96
|
+
connect(): Promise<void>;
|
|
97
|
+
/**
|
|
98
|
+
* Closes all MCP server connections and clears the cached tool classes.
|
|
99
|
+
*
|
|
100
|
+
* This method gracefully shuts down all server processes and cleans up
|
|
101
|
+
* internal state. After calling this method, the tool classes are no longer
|
|
102
|
+
* available.
|
|
103
|
+
*
|
|
104
|
+
* @returns A promise that resolves when all servers are disconnected.
|
|
105
|
+
*/
|
|
106
|
+
disconnect(): Promise<void>;
|
|
107
|
+
/**
|
|
108
|
+
* Enables automatic cleanup when the provider is disposed.
|
|
109
|
+
*
|
|
110
|
+
* This method implements the disposable pattern, allowing the provider to be
|
|
111
|
+
* used with explicit resource management syntax (using keyword). When disposed,
|
|
112
|
+
* it attempts to disconnect from all servers.
|
|
113
|
+
*/
|
|
114
|
+
[Symbol.dispose](): void;
|
|
115
|
+
/**
|
|
116
|
+
* Retrieves all tool classes from connected MCP servers.
|
|
117
|
+
*
|
|
118
|
+
* This method returns a shallow copy of the internal tool classes array,
|
|
119
|
+
* containing Simulacra-compatible tool classes for all tools from all
|
|
120
|
+
* connected servers.
|
|
121
|
+
*
|
|
122
|
+
* @returns An array of tool classes ready to be used in a Simulacra workflow.
|
|
123
|
+
*/
|
|
124
|
+
getToolClasses(): ToolClass[];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export { type McpServerConfig, McpToolProvider, type McpToolProviderConfig, convertJsonSchemaToParameters };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,127 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { LifecycleErrorEvent, ToolParameterDefinition, ToolClass } from '@simulacra-ai/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Configuration for a single Model Context Protocol (MCP) server connection.
|
|
5
|
+
*
|
|
6
|
+
* This interface defines how to connect to and configure an MCP server,
|
|
7
|
+
* including the command to execute, environment variables, and tool-specific
|
|
8
|
+
* overrides.
|
|
9
|
+
*/
|
|
10
|
+
interface McpServerConfig {
|
|
11
|
+
/**
|
|
12
|
+
* A unique identifier for this MCP server.
|
|
13
|
+
*/
|
|
14
|
+
name: string;
|
|
15
|
+
/**
|
|
16
|
+
* The command to execute to start the MCP server process.
|
|
17
|
+
*/
|
|
18
|
+
command: string;
|
|
19
|
+
/**
|
|
20
|
+
* Command-line arguments to pass to the server command.
|
|
21
|
+
*/
|
|
22
|
+
args?: string[];
|
|
23
|
+
/**
|
|
24
|
+
* Environment variables to set for the server process.
|
|
25
|
+
*/
|
|
26
|
+
env?: Record<string, string>;
|
|
27
|
+
/**
|
|
28
|
+
* The transport mechanism used to communicate with the server.
|
|
29
|
+
* Currently only "stdio" is supported.
|
|
30
|
+
*/
|
|
31
|
+
transport?: "stdio";
|
|
32
|
+
/**
|
|
33
|
+
* Tool-specific configuration overrides, keyed by tool name.
|
|
34
|
+
*/
|
|
35
|
+
tool_overrides?: Record<string, {
|
|
36
|
+
/**
|
|
37
|
+
* Whether this tool can be executed in parallel with other tools.
|
|
38
|
+
*/
|
|
39
|
+
parallelizable?: boolean;
|
|
40
|
+
}>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Configuration for the MCP tool provider.
|
|
44
|
+
*
|
|
45
|
+
* This interface defines the configuration needed to connect to one or more
|
|
46
|
+
* MCP servers and make their tools available to a Simulacra workflow.
|
|
47
|
+
*/
|
|
48
|
+
interface McpToolProviderConfig {
|
|
49
|
+
/**
|
|
50
|
+
* An array of MCP server configurations to connect to.
|
|
51
|
+
*/
|
|
52
|
+
servers: McpServerConfig[];
|
|
53
|
+
/**
|
|
54
|
+
* Optional callback invoked when a background operation fails during cleanup or disposal.
|
|
55
|
+
*/
|
|
56
|
+
on_error?: (event: LifecycleErrorEvent) => void;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Converts a JSON Schema object into an array of Simulacra tool parameter definitions.
|
|
61
|
+
*
|
|
62
|
+
* This function transforms MCP tool input schemas (which use JSON Schema format)
|
|
63
|
+
* into the parameter format expected by Simulacra tools. It handles nested objects,
|
|
64
|
+
* arrays, enums, and all standard JSON Schema primitive types.
|
|
65
|
+
*
|
|
66
|
+
* @param schema - A JSON Schema object describing the tool's input parameters.
|
|
67
|
+
* @returns An array of tool parameter definitions compatible with Simulacra.
|
|
68
|
+
*/
|
|
69
|
+
declare function convertJsonSchemaToParameters(schema: Record<string, unknown>): ToolParameterDefinition[];
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* A provider that connects to Model Context Protocol (MCP) servers and exposes
|
|
73
|
+
* their tools as Simulacra-compatible tool classes.
|
|
74
|
+
*
|
|
75
|
+
* This class manages the lifecycle of MCP server connections, converts MCP tools
|
|
76
|
+
* into the Simulacra tool format, and provides access to all tools from connected
|
|
77
|
+
* servers.
|
|
78
|
+
*/
|
|
79
|
+
declare class McpToolProvider {
|
|
80
|
+
#private;
|
|
81
|
+
/**
|
|
82
|
+
* Creates a new MCP tool provider.
|
|
83
|
+
*
|
|
84
|
+
* @param config - Configuration specifying which MCP servers to connect to.
|
|
85
|
+
*/
|
|
86
|
+
constructor(config: McpToolProviderConfig);
|
|
87
|
+
/**
|
|
88
|
+
* Establishes connections to all configured MCP servers and retrieves their tools.
|
|
89
|
+
*
|
|
90
|
+
* This method spawns each server process, establishes communication over stdio,
|
|
91
|
+
* and queries each server for its available tools. The tools are converted to
|
|
92
|
+
* Simulacra-compatible tool classes and stored internally.
|
|
93
|
+
*
|
|
94
|
+
* @returns A promise that resolves when all servers are connected and tools are loaded.
|
|
95
|
+
*/
|
|
96
|
+
connect(): Promise<void>;
|
|
97
|
+
/**
|
|
98
|
+
* Closes all MCP server connections and clears the cached tool classes.
|
|
99
|
+
*
|
|
100
|
+
* This method gracefully shuts down all server processes and cleans up
|
|
101
|
+
* internal state. After calling this method, the tool classes are no longer
|
|
102
|
+
* available.
|
|
103
|
+
*
|
|
104
|
+
* @returns A promise that resolves when all servers are disconnected.
|
|
105
|
+
*/
|
|
106
|
+
disconnect(): Promise<void>;
|
|
107
|
+
/**
|
|
108
|
+
* Enables automatic cleanup when the provider is disposed.
|
|
109
|
+
*
|
|
110
|
+
* This method implements the disposable pattern, allowing the provider to be
|
|
111
|
+
* used with explicit resource management syntax (using keyword). When disposed,
|
|
112
|
+
* it attempts to disconnect from all servers.
|
|
113
|
+
*/
|
|
114
|
+
[Symbol.dispose](): void;
|
|
115
|
+
/**
|
|
116
|
+
* Retrieves all tool classes from connected MCP servers.
|
|
117
|
+
*
|
|
118
|
+
* This method returns a shallow copy of the internal tool classes array,
|
|
119
|
+
* containing Simulacra-compatible tool classes for all tools from all
|
|
120
|
+
* connected servers.
|
|
121
|
+
*
|
|
122
|
+
* @returns An array of tool classes ready to be used in a Simulacra workflow.
|
|
123
|
+
*/
|
|
124
|
+
getToolClasses(): ToolClass[];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export { type McpServerConfig, McpToolProvider, type McpToolProviderConfig, convertJsonSchemaToParameters };
|