@genkit-ai/mcp 1.14.1-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +203 -0
- package/README.md +205 -0
- package/examples/client/index.js +36 -0
- package/examples/client/package.json +25 -0
- package/examples/server/index.js +46 -0
- package/examples/server/package.json +18 -0
- package/examples/server/prompts/port_code.prompt +13 -0
- package/lib/client/client.d.mts +177 -0
- package/lib/client/client.d.ts +177 -0
- package/lib/client/client.js +282 -0
- package/lib/client/client.js.map +1 -0
- package/lib/client/client.mjs +267 -0
- package/lib/client/client.mjs.map +1 -0
- package/lib/client/host.d.mts +202 -0
- package/lib/client/host.d.ts +202 -0
- package/lib/client/host.js +392 -0
- package/lib/client/host.js.map +1 -0
- package/lib/client/host.mjs +368 -0
- package/lib/client/host.mjs.map +1 -0
- package/lib/client/index.d.mts +9 -0
- package/lib/client/index.d.ts +9 -0
- package/lib/client/index.js +32 -0
- package/lib/client/index.js.map +1 -0
- package/lib/client/index.mjs +7 -0
- package/lib/client/index.mjs.map +1 -0
- package/lib/index.d.mts +12 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.js +48 -0
- package/lib/index.js.map +1 -0
- package/lib/index.mjs +22 -0
- package/lib/index.mjs.map +1 -0
- package/lib/server.d.mts +188 -0
- package/lib/server.d.ts +188 -0
- package/lib/server.js +280 -0
- package/lib/server.js.map +1 -0
- package/lib/server.mjs +249 -0
- package/lib/server.mjs.map +1 -0
- package/lib/util/index.d.mts +11 -0
- package/lib/util/index.d.ts +11 -0
- package/lib/util/index.js +29 -0
- package/lib/util/index.js.map +1 -0
- package/lib/util/index.mjs +5 -0
- package/lib/util/index.mjs.map +1 -0
- package/lib/util/message.d.mts +43 -0
- package/lib/util/message.d.ts +43 -0
- package/lib/util/message.js +61 -0
- package/lib/util/message.js.map +1 -0
- package/lib/util/message.mjs +36 -0
- package/lib/util/message.mjs.map +1 -0
- package/lib/util/prompts.d.mts +45 -0
- package/lib/util/prompts.d.ts +45 -0
- package/lib/util/prompts.js +147 -0
- package/lib/util/prompts.js.map +1 -0
- package/lib/util/prompts.mjs +123 -0
- package/lib/util/prompts.mjs.map +1 -0
- package/lib/util/resource.d.mts +28 -0
- package/lib/util/resource.d.ts +28 -0
- package/lib/util/resource.js +116 -0
- package/lib/util/resource.js.map +1 -0
- package/lib/util/resource.mjs +95 -0
- package/lib/util/resource.mjs.map +1 -0
- package/lib/util/tools.d.mts +37 -0
- package/lib/util/tools.d.ts +37 -0
- package/lib/util/tools.js +120 -0
- package/lib/util/tools.js.map +1 -0
- package/lib/util/tools.mjs +95 -0
- package/lib/util/tools.mjs.map +1 -0
- package/lib/util/transport.d.mts +39 -0
- package/lib/util/transport.d.ts +39 -0
- package/lib/util/transport.js +63 -0
- package/lib/util/transport.js.map +1 -0
- package/lib/util/transport.mjs +29 -0
- package/lib/util/transport.mjs.map +1 -0
- package/package.json +57 -0
- package/src/client/client.ts +414 -0
- package/src/client/host.ts +485 -0
- package/src/client/index.ts +29 -0
- package/src/index.ts +114 -0
- package/src/server.ts +330 -0
- package/src/util/index.ts +20 -0
- package/src/util/message.ts +72 -0
- package/src/util/prompts.ts +223 -0
- package/src/util/resource.ts +141 -0
- package/src/util/tools.ts +164 -0
- package/src/util/transport.ts +67 -0
- package/tests/fakes.ts +221 -0
- package/tests/host_test.ts +609 -0
- package/tests/server_test.ts +165 -0
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
import { logger } from "genkit/logging";
|
|
2
|
+
import { GenkitMcpClient } from "./client.js";
|
|
3
|
+
class GenkitMcpHost {
|
|
4
|
+
name;
|
|
5
|
+
_clients = {};
|
|
6
|
+
_clientStates = {};
|
|
7
|
+
_readyListeners = [];
|
|
8
|
+
_ready = false;
|
|
9
|
+
roots;
|
|
10
|
+
rawToolResponses;
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.name = options.name || "genkit-mcp";
|
|
13
|
+
this.rawToolResponses = options.rawToolResponses;
|
|
14
|
+
this.roots = options.roots;
|
|
15
|
+
if (options.mcpServers) {
|
|
16
|
+
this.updateServers(options.mcpServers);
|
|
17
|
+
} else {
|
|
18
|
+
this._ready = true;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Returns a Promise that resolves when the host has attempted to connect
|
|
23
|
+
* to all configured clients, or rejects if a critical error occurs during
|
|
24
|
+
* the initial connection phase.
|
|
25
|
+
*/
|
|
26
|
+
async ready() {
|
|
27
|
+
if (this._ready) return;
|
|
28
|
+
return new Promise((resolve, reject) => {
|
|
29
|
+
this._readyListeners.push({ resolve, reject });
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Connects to a single MCP server defined by the provided configuration.
|
|
34
|
+
* If a server with the same name already exists, it will be disconnected first.
|
|
35
|
+
* Stores the client and transport references internally. Handles connection errors
|
|
36
|
+
* by marking the server as disabled.
|
|
37
|
+
* @param serverName The name to assign to this server connection.
|
|
38
|
+
* @param config The configuration object for the server.
|
|
39
|
+
*/
|
|
40
|
+
async connect(serverName, config) {
|
|
41
|
+
const existingEntry = this._clients[serverName];
|
|
42
|
+
if (existingEntry) {
|
|
43
|
+
try {
|
|
44
|
+
await existingEntry._disconnect();
|
|
45
|
+
} catch (e) {
|
|
46
|
+
existingEntry.disable();
|
|
47
|
+
this.setError(serverName, {
|
|
48
|
+
message: `[MCP Host] Error disconnecting from existing connection for ${serverName}`,
|
|
49
|
+
detail: `Details: ${e}`
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
logger.debug(
|
|
54
|
+
`[MCP Host] Connecting to MCP server '${serverName}' in host '${this.name}'.`
|
|
55
|
+
);
|
|
56
|
+
try {
|
|
57
|
+
const client = new GenkitMcpClient({
|
|
58
|
+
name: this.name,
|
|
59
|
+
serverName,
|
|
60
|
+
mcpServer: { ...config, roots: config.roots || this.roots },
|
|
61
|
+
rawToolResponses: this.rawToolResponses
|
|
62
|
+
});
|
|
63
|
+
this._clients[serverName] = client;
|
|
64
|
+
} catch (e) {
|
|
65
|
+
this.setError(serverName, {
|
|
66
|
+
message: `[MCP Host] Error connecting to ${serverName} with config ${config}`,
|
|
67
|
+
detail: `Details: ${e}`
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Disconnects the specified MCP server and removes its registration
|
|
73
|
+
* from this client instance.
|
|
74
|
+
* @param serverName The name of the server to disconnect.
|
|
75
|
+
*/
|
|
76
|
+
async disconnect(serverName) {
|
|
77
|
+
const client = this._clients[serverName];
|
|
78
|
+
if (!client) {
|
|
79
|
+
logger.warn(`[MCP Host] unable to find server ${serverName}`);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
logger.debug(
|
|
83
|
+
`[MCP Host] Disconnecting MCP server '${serverName}' in host '${this.name}'.`
|
|
84
|
+
);
|
|
85
|
+
try {
|
|
86
|
+
await client._disconnect();
|
|
87
|
+
} catch (e) {
|
|
88
|
+
client.disable();
|
|
89
|
+
this.setError(serverName, {
|
|
90
|
+
message: `[MCP Host] Error disconnecting from existing connection for ${serverName}`,
|
|
91
|
+
detail: `Details: ${e}`
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
delete this._clients[serverName];
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Temporarily disables a server connection. Closes the underlying transport
|
|
98
|
+
* but retains the server's configuration. Does nothing if the server is
|
|
99
|
+
* already disabled.
|
|
100
|
+
* @param serverName The name of the server to disable.
|
|
101
|
+
*/
|
|
102
|
+
async disable(serverName) {
|
|
103
|
+
const client = this._clients[serverName];
|
|
104
|
+
if (!client) {
|
|
105
|
+
logger.warn(`[MCP Host] unable to find server ${serverName}`);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (!client.isEnabled()) {
|
|
109
|
+
logger.warn(`[MCP Host] server ${serverName} already disabled`);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
logger.debug(
|
|
113
|
+
`[MCP Host] Disabling MCP server '${serverName}' in host '${this.name}'`
|
|
114
|
+
);
|
|
115
|
+
await client.disable();
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Enables a server connection, including previously disabled ones. Attempts to reconnect
|
|
119
|
+
* using the stored transport. Does nothing if the server is not disabled.
|
|
120
|
+
* @param serverName The name of the server to re-enable.
|
|
121
|
+
*/
|
|
122
|
+
async enable(serverName) {
|
|
123
|
+
const client = this._clients[serverName];
|
|
124
|
+
if (!client) {
|
|
125
|
+
logger.warn(`[MCP Host] unable to find server ${serverName}`);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
logger.debug(
|
|
129
|
+
`[MCP Host] Reenabling MCP server '${serverName}' in host '${this.name}'`
|
|
130
|
+
);
|
|
131
|
+
try {
|
|
132
|
+
await client.enable();
|
|
133
|
+
} catch (e) {
|
|
134
|
+
client.disable();
|
|
135
|
+
this.setError(serverName, {
|
|
136
|
+
message: `[MCP Host] Error reenabling server ${serverName}`,
|
|
137
|
+
detail: `Details: ${e}`
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Closes and then restarts the transport connection for the specified server.
|
|
143
|
+
* Useful for attempting to recover from connection issues without full
|
|
144
|
+
* reconfiguration.
|
|
145
|
+
* @param serverName The name of the server to reconnect.
|
|
146
|
+
*/
|
|
147
|
+
async reconnect(serverName) {
|
|
148
|
+
const client = this._clients[serverName];
|
|
149
|
+
if (!client) {
|
|
150
|
+
logger.warn(`[MCP Host] unable to find server ${serverName}`);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
logger.debug(
|
|
154
|
+
`[MCP Host] Restarting connection to MCP server '${serverName}' in host '${this.name}'`
|
|
155
|
+
);
|
|
156
|
+
try {
|
|
157
|
+
await client.restart();
|
|
158
|
+
} catch (e) {
|
|
159
|
+
client.disable();
|
|
160
|
+
this.setError(serverName, {
|
|
161
|
+
message: `[MCP Host] Error restarting to server ${serverName}`,
|
|
162
|
+
detail: `Details: ${e}`
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Updates the connections based on a provided map of server configurations.
|
|
168
|
+
* - Connects any new servers defined in `mcpServers`.
|
|
169
|
+
* - Disconnects any servers currently connected but not present in `mcpServers`.
|
|
170
|
+
* - Reconnects existing servers if their configuration appears to have changed (implicitly handled by `connectServer`).
|
|
171
|
+
* Sets the client's ready state once all connection attempts are complete.
|
|
172
|
+
* @param mcpServers A record mapping server names to their configurations.
|
|
173
|
+
*/
|
|
174
|
+
updateServers(mcpServers) {
|
|
175
|
+
this._ready = false;
|
|
176
|
+
const newServerNames = new Set(Object.keys(mcpServers));
|
|
177
|
+
const currentServerNames = new Set(Object.keys(this._clients));
|
|
178
|
+
const promises = [];
|
|
179
|
+
for (const serverName in mcpServers) {
|
|
180
|
+
promises.push(this.connect(serverName, mcpServers[serverName]));
|
|
181
|
+
}
|
|
182
|
+
for (const serverName of currentServerNames) {
|
|
183
|
+
if (!newServerNames.has(serverName)) {
|
|
184
|
+
this.disconnect(serverName);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
Promise.all(promises).then(() => {
|
|
188
|
+
this._ready = true;
|
|
189
|
+
while (this._readyListeners.length) {
|
|
190
|
+
this._readyListeners.pop()?.resolve();
|
|
191
|
+
}
|
|
192
|
+
}).catch((err) => {
|
|
193
|
+
while (this._readyListeners.length) {
|
|
194
|
+
this._readyListeners.pop()?.reject(err);
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Retrieves all tools from all connected and enabled MCP clients managed by
|
|
200
|
+
* this instance. This method waits for the host to be ready (all initial
|
|
201
|
+
* connection attempts made) before fetching tools.
|
|
202
|
+
*
|
|
203
|
+
* It iterates through each managed `GenkitMcpClient`, and if the client is
|
|
204
|
+
* not disabled, it calls the client's `getTools` method to fetch its
|
|
205
|
+
* available tools. These are then aggregated into a single array.
|
|
206
|
+
*
|
|
207
|
+
* This is useful for dynamically providing a list of all available MCP tools
|
|
208
|
+
* to Genkit, for example, when setting up a Genkit plugin.
|
|
209
|
+
*
|
|
210
|
+
* ```ts
|
|
211
|
+
* const mcpHost = createMcpHost({ ... });
|
|
212
|
+
* // In your Genkit configuration:
|
|
213
|
+
* // const allMcpTools = await McpHost.getActiveTools(ai);
|
|
214
|
+
* // Then, these tools can be used or registered with Genkit.
|
|
215
|
+
* ```
|
|
216
|
+
*
|
|
217
|
+
* @param ai The Genkit instance, used by individual clients to define dynamic
|
|
218
|
+
* tools.
|
|
219
|
+
* @returns A Promise that resolves to an array of `ToolAction` from all
|
|
220
|
+
* active MCP clients.
|
|
221
|
+
*/
|
|
222
|
+
async getActiveTools(ai) {
|
|
223
|
+
await this.ready();
|
|
224
|
+
let allTools = [];
|
|
225
|
+
for (const serverName in this._clients) {
|
|
226
|
+
const client = this._clients[serverName];
|
|
227
|
+
if (client.isEnabled() && !this.hasError(serverName)) {
|
|
228
|
+
try {
|
|
229
|
+
const tools = await client.getActiveTools(ai);
|
|
230
|
+
allTools.push(...tools);
|
|
231
|
+
} catch (e) {
|
|
232
|
+
logger.error(
|
|
233
|
+
`Error fetching active tools from client ${serverName}.`,
|
|
234
|
+
JSON.stringify(e)
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return allTools;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Retrieves all resources from all connected and enabled MCP clients managed by
|
|
243
|
+
* this instance. This method waits for the host to be ready (all initial
|
|
244
|
+
* connection attempts made) before fetching resources.
|
|
245
|
+
*
|
|
246
|
+
* It iterates through each managed `GenkitMcpClient`, and if the client is
|
|
247
|
+
* not disabled, it calls the client's `getActiveResources` method to fetch its
|
|
248
|
+
* available resources. These are then aggregated into a single array.
|
|
249
|
+
*
|
|
250
|
+
* This is useful for dynamically providing a list of all available MCP resources
|
|
251
|
+
* to Genkit, for example, when setting up a Genkit plugin.
|
|
252
|
+
*
|
|
253
|
+
* @param ai The Genkit instance, used by individual clients to define dynamic
|
|
254
|
+
* resources.
|
|
255
|
+
* @returns A Promise that resolves to an array of `DynamicResourceAction` from all
|
|
256
|
+
* active MCP clients.
|
|
257
|
+
*/
|
|
258
|
+
async getActiveResources(ai) {
|
|
259
|
+
await this.ready();
|
|
260
|
+
let allResources = [];
|
|
261
|
+
for (const serverName in this._clients) {
|
|
262
|
+
const client = this._clients[serverName];
|
|
263
|
+
if (client.isEnabled() && !this.hasError(serverName)) {
|
|
264
|
+
try {
|
|
265
|
+
const resources = await client.getActiveResources(ai);
|
|
266
|
+
allResources.push(...resources);
|
|
267
|
+
} catch (e) {
|
|
268
|
+
logger.error(
|
|
269
|
+
`Error fetching active resources from client ${serverName}.`,
|
|
270
|
+
JSON.stringify(e)
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return allResources;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Retrieves all prompts from all connected and enabled MCP clients managed by
|
|
279
|
+
* this instance. This method waits for the host to be ready (all initial
|
|
280
|
+
* connection attempts made) before fetching prompts.
|
|
281
|
+
*
|
|
282
|
+
* It iterates through each managed `GenkitMcpClient`, and if the client is
|
|
283
|
+
* not disabled, it calls the client's `getActivePrompts` method to fetch its
|
|
284
|
+
* available prompts. These are then aggregated into a single array.
|
|
285
|
+
*
|
|
286
|
+
* This is useful for dynamically providing a list of all available MCP prompts
|
|
287
|
+
* to Genkit, for example, when setting up a Genkit plugin.
|
|
288
|
+
*
|
|
289
|
+
* @param ai The Genkit instance, used by individual clients to define dynamic
|
|
290
|
+
* prompts.
|
|
291
|
+
* @returns A Promise that resolves to an array of `ExecutablePrompt` from all
|
|
292
|
+
* active MCP clients.
|
|
293
|
+
*/
|
|
294
|
+
async getActivePrompts(ai) {
|
|
295
|
+
await this.ready();
|
|
296
|
+
let allPrompts = [];
|
|
297
|
+
for (const serverName in this._clients) {
|
|
298
|
+
const client = this._clients[serverName];
|
|
299
|
+
if (client.isEnabled() && !this.hasError(serverName)) {
|
|
300
|
+
allPrompts.push(...await client.getActivePrompts(ai));
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return allPrompts;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Get the specified prompt as an `ExecutablePrompt` available through the
|
|
307
|
+
* specified server. If no such prompt is found, return undefined.
|
|
308
|
+
*/
|
|
309
|
+
async getPrompt(ai, serverName, promptName, opts) {
|
|
310
|
+
await this.ready();
|
|
311
|
+
const client = this._clients[serverName];
|
|
312
|
+
if (!client) {
|
|
313
|
+
logger.error(`No client found with name '${serverName}'.`);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if (this.hasError(serverName)) {
|
|
317
|
+
const errorStringified = JSON.stringify(
|
|
318
|
+
this._clientStates[serverName].error
|
|
319
|
+
);
|
|
320
|
+
logger.error(
|
|
321
|
+
`Client '${serverName}' is in an error state. ${errorStringified}`
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
if (client.isEnabled()) {
|
|
325
|
+
const prompt = await client.getPrompt(ai, promptName, opts);
|
|
326
|
+
if (!prompt) {
|
|
327
|
+
logger.error(
|
|
328
|
+
`[MCP Host] Unable to fetch the specified ${promptName} in server ${serverName}.`
|
|
329
|
+
);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
return prompt;
|
|
333
|
+
}
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
async close() {
|
|
337
|
+
for (const client of Object.values(this._clients)) {
|
|
338
|
+
await client._disconnect();
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
/** Helper method to track and log client errors. */
|
|
342
|
+
setError(serverName, error) {
|
|
343
|
+
this._clientStates[serverName] = { error };
|
|
344
|
+
logger.warn(
|
|
345
|
+
`An error has occured while managing your MCP client '${serverName}'. The client may be disabled to avoid further issues. Please resolve the issue and reenable the client '${serverName}' to continue using its resources.`
|
|
346
|
+
);
|
|
347
|
+
logger.warn(error);
|
|
348
|
+
}
|
|
349
|
+
hasError(serverName) {
|
|
350
|
+
return this._clientStates[serverName] && !!this._clientStates[serverName].error;
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Returns an array of all active clients.
|
|
354
|
+
*/
|
|
355
|
+
get activeClients() {
|
|
356
|
+
return Object.values(this._clients).filter((c) => c.isEnabled());
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Returns the client by name.
|
|
360
|
+
*/
|
|
361
|
+
getClient(name) {
|
|
362
|
+
return this._clients[name];
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
export {
|
|
366
|
+
GenkitMcpHost
|
|
367
|
+
};
|
|
368
|
+
//# sourceMappingURL=host.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/client/host.ts"],"sourcesContent":["/**\n * Copyright 2025 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Root } from '@modelcontextprotocol/sdk/types.js';\nimport {\n DynamicResourceAction,\n ExecutablePrompt,\n Genkit,\n PromptGenerateOptions,\n ToolAction,\n} from 'genkit';\nimport { logger } from 'genkit/logging';\nimport { GenkitMcpClient, McpServerConfig } from './client.js';\n\nexport interface McpHostOptions {\n /**\n * An optional client name for this MCP host. This name is advertised to MCP Servers\n * as the connecting client name. Defaults to 'genkit-mcp'.\n */\n name?: string;\n /**\n * An optional version for this MCP host. Primarily for\n * logging and identification within Genkit.\n * Defaults to '1.0.0'.\n */\n version?: string;\n /**\n * A record for configuring multiple MCP servers. Each server connection is\n * controlled by a `GenkitMcpClient` instance managed by `GenkitMcpHost`.\n * The key in the record is used as the identifier for the MCP server.\n */\n mcpServers?: Record<string, McpServerConfig>;\n\n /**\n * If true, tool responses from the MCP server will be returned in their raw\n * MCP format. Otherwise (default), they are processed and potentially\n * simplified for better compatibility with Genkit's typical data structures.\n */\n rawToolResponses?: boolean;\n\n /**\n * When provided, each connected MCP server will be sent the roots specified here. Overridden by any specific roots sent in the `mcpServers` config for a given server.\n */\n roots?: Root[];\n}\n\n/** Internal representation of client state for logging. */\ninterface ClientState {\n error?: {\n message: string;\n detail?: any;\n };\n}\n\n/**\n * Manages connections to multiple MCP (Model Context Protocol) servers.\n * Each server connection is individually configured and managed by an instance of `GenkitMcpClient`.\n * This host provides a centralized way to initialize, update, and interact with these clients.\n *\n * It allows for dynamic registration of tools from all connected and enabled MCP servers\n * into a Genkit instance.\n */\nexport class GenkitMcpHost {\n name: string;\n private _clients: Record<string, GenkitMcpClient> = {};\n private _clientStates: Record<string, ClientState> = {};\n private _readyListeners: {\n resolve: () => void;\n reject: (err: Error) => void;\n }[] = [];\n private _ready = false;\n private roots: Root[] | undefined;\n rawToolResponses?: boolean;\n\n constructor(options: McpHostOptions) {\n this.name = options.name || 'genkit-mcp';\n this.rawToolResponses = options.rawToolResponses;\n this.roots = options.roots;\n\n if (options.mcpServers) {\n this.updateServers(options.mcpServers);\n } else {\n this._ready = true;\n }\n }\n\n /**\n * Returns a Promise that resolves when the host has attempted to connect\n * to all configured clients, or rejects if a critical error occurs during\n * the initial connection phase.\n */\n async ready() {\n if (this._ready) return;\n return new Promise<void>((resolve, reject) => {\n this._readyListeners.push({ resolve, reject });\n });\n }\n\n /**\n * Connects to a single MCP server defined by the provided configuration.\n * If a server with the same name already exists, it will be disconnected first.\n * Stores the client and transport references internally. Handles connection errors\n * by marking the server as disabled.\n * @param serverName The name to assign to this server connection.\n * @param config The configuration object for the server.\n */\n async connect(serverName: string, config: McpServerConfig) {\n const existingEntry = this._clients[serverName];\n if (existingEntry) {\n try {\n await existingEntry._disconnect();\n } catch (e) {\n existingEntry.disable();\n this.setError(serverName, {\n message: `[MCP Host] Error disconnecting from existing connection for ${serverName}`,\n detail: `Details: ${e}`,\n });\n }\n }\n\n logger.debug(\n `[MCP Host] Connecting to MCP server '${serverName}' in host '${this.name}'.`\n );\n try {\n const client = new GenkitMcpClient({\n name: this.name,\n serverName: serverName,\n mcpServer: { ...config, roots: config.roots || this.roots },\n rawToolResponses: this.rawToolResponses,\n });\n this._clients[serverName] = client;\n } catch (e) {\n this.setError(serverName, {\n message: `[MCP Host] Error connecting to ${serverName} with config ${config}`,\n detail: `Details: ${e}`,\n });\n }\n }\n\n /**\n * Disconnects the specified MCP server and removes its registration\n * from this client instance.\n * @param serverName The name of the server to disconnect.\n */\n async disconnect(serverName: string) {\n const client = this._clients[serverName];\n if (!client) {\n logger.warn(`[MCP Host] unable to find server ${serverName}`);\n return;\n }\n\n logger.debug(\n `[MCP Host] Disconnecting MCP server '${serverName}' in host '${this.name}'.`\n );\n try {\n await client._disconnect();\n } catch (e) {\n client.disable();\n this.setError(serverName, {\n message: `[MCP Host] Error disconnecting from existing connection for ${serverName}`,\n detail: `Details: ${e}`,\n });\n }\n delete this._clients[serverName];\n }\n\n /**\n * Temporarily disables a server connection. Closes the underlying transport\n * but retains the server's configuration. Does nothing if the server is\n * already disabled.\n * @param serverName The name of the server to disable.\n */\n async disable(serverName: string) {\n const client = this._clients[serverName];\n if (!client) {\n logger.warn(`[MCP Host] unable to find server ${serverName}`);\n return;\n }\n if (!client.isEnabled()) {\n logger.warn(`[MCP Host] server ${serverName} already disabled`);\n return;\n }\n\n logger.debug(\n `[MCP Host] Disabling MCP server '${serverName}' in host '${this.name}'`\n );\n await client.disable();\n }\n\n /**\n * Enables a server connection, including previously disabled ones. Attempts to reconnect\n * using the stored transport. Does nothing if the server is not disabled.\n * @param serverName The name of the server to re-enable.\n */\n async enable(serverName: string) {\n const client = this._clients[serverName];\n if (!client) {\n logger.warn(`[MCP Host] unable to find server ${serverName}`);\n return;\n }\n\n logger.debug(\n `[MCP Host] Reenabling MCP server '${serverName}' in host '${this.name}'`\n );\n try {\n await client.enable();\n } catch (e) {\n client.disable();\n this.setError(serverName, {\n message: `[MCP Host] Error reenabling server ${serverName}`,\n detail: `Details: ${e}`,\n });\n }\n }\n\n /**\n * Closes and then restarts the transport connection for the specified server.\n * Useful for attempting to recover from connection issues without full\n * reconfiguration.\n * @param serverName The name of the server to reconnect.\n */\n async reconnect(serverName: string) {\n const client = this._clients[serverName];\n if (!client) {\n logger.warn(`[MCP Host] unable to find server ${serverName}`);\n return;\n }\n\n logger.debug(\n `[MCP Host] Restarting connection to MCP server '${serverName}' in host '${this.name}'`\n );\n try {\n await client.restart();\n } catch (e) {\n client.disable();\n this.setError(serverName, {\n message: `[MCP Host] Error restarting to server ${serverName}`,\n detail: `Details: ${e}`,\n });\n }\n }\n\n /**\n * Updates the connections based on a provided map of server configurations.\n * - Connects any new servers defined in `mcpServers`.\n * - Disconnects any servers currently connected but not present in `mcpServers`.\n * - Reconnects existing servers if their configuration appears to have changed (implicitly handled by `connectServer`).\n * Sets the client's ready state once all connection attempts are complete.\n * @param mcpServers A record mapping server names to their configurations.\n */\n updateServers(mcpServers: Record<string, McpServerConfig>) {\n this._ready = false;\n const newServerNames = new Set(Object.keys(mcpServers));\n const currentServerNames = new Set(Object.keys(this._clients));\n\n const promises: Promise<void>[] = [];\n for (const serverName in mcpServers) {\n promises.push(this.connect(serverName, mcpServers[serverName]));\n }\n\n // Disconnect servers that are no longer in the config\n for (const serverName of currentServerNames) {\n if (!newServerNames.has(serverName)) {\n this.disconnect(serverName);\n }\n }\n\n Promise.all(promises)\n .then(() => {\n this._ready = true;\n while (this._readyListeners.length) {\n this._readyListeners.pop()?.resolve();\n }\n })\n .catch((err) => {\n while (this._readyListeners.length) {\n this._readyListeners.pop()?.reject(err);\n }\n });\n }\n\n /**\n * Retrieves all tools from all connected and enabled MCP clients managed by\n * this instance. This method waits for the host to be ready (all initial\n * connection attempts made) before fetching tools.\n *\n * It iterates through each managed `GenkitMcpClient`, and if the client is\n * not disabled, it calls the client's `getTools` method to fetch its\n * available tools. These are then aggregated into a single array.\n *\n * This is useful for dynamically providing a list of all available MCP tools\n * to Genkit, for example, when setting up a Genkit plugin.\n *\n * ```ts\n * const mcpHost = createMcpHost({ ... });\n * // In your Genkit configuration:\n * // const allMcpTools = await McpHost.getActiveTools(ai);\n * // Then, these tools can be used or registered with Genkit.\n * ```\n *\n * @param ai The Genkit instance, used by individual clients to define dynamic\n * tools.\n * @returns A Promise that resolves to an array of `ToolAction` from all\n * active MCP clients.\n */\n async getActiveTools(ai: Genkit): Promise<ToolAction[]> {\n await this.ready();\n let allTools: ToolAction[] = [];\n\n for (const serverName in this._clients) {\n const client = this._clients[serverName];\n if (client.isEnabled() && !this.hasError(serverName)) {\n try {\n const tools = await client.getActiveTools(ai);\n allTools.push(...tools);\n } catch (e) {\n logger.error(\n `Error fetching active tools from client ${serverName}.`,\n JSON.stringify(e)\n );\n }\n }\n }\n return allTools;\n }\n\n /**\n * Retrieves all resources from all connected and enabled MCP clients managed by\n * this instance. This method waits for the host to be ready (all initial\n * connection attempts made) before fetching resources.\n *\n * It iterates through each managed `GenkitMcpClient`, and if the client is\n * not disabled, it calls the client's `getActiveResources` method to fetch its\n * available resources. These are then aggregated into a single array.\n *\n * This is useful for dynamically providing a list of all available MCP resources\n * to Genkit, for example, when setting up a Genkit plugin.\n *\n * @param ai The Genkit instance, used by individual clients to define dynamic\n * resources.\n * @returns A Promise that resolves to an array of `DynamicResourceAction` from all\n * active MCP clients.\n */\n async getActiveResources(ai: Genkit): Promise<DynamicResourceAction[]> {\n await this.ready();\n let allResources: DynamicResourceAction[] = [];\n\n for (const serverName in this._clients) {\n const client = this._clients[serverName];\n if (client.isEnabled() && !this.hasError(serverName)) {\n try {\n const resources = await client.getActiveResources(ai);\n allResources.push(...resources);\n } catch (e) {\n logger.error(\n `Error fetching active resources from client ${serverName}.`,\n JSON.stringify(e)\n );\n }\n }\n }\n return allResources;\n }\n\n /**\n * Retrieves all prompts from all connected and enabled MCP clients managed by\n * this instance. This method waits for the host to be ready (all initial\n * connection attempts made) before fetching prompts.\n *\n * It iterates through each managed `GenkitMcpClient`, and if the client is\n * not disabled, it calls the client's `getActivePrompts` method to fetch its\n * available prompts. These are then aggregated into a single array.\n *\n * This is useful for dynamically providing a list of all available MCP prompts\n * to Genkit, for example, when setting up a Genkit plugin.\n *\n * @param ai The Genkit instance, used by individual clients to define dynamic\n * prompts.\n * @returns A Promise that resolves to an array of `ExecutablePrompt` from all\n * active MCP clients.\n */\n async getActivePrompts(ai: Genkit): Promise<ExecutablePrompt[]> {\n await this.ready();\n let allPrompts: ExecutablePrompt[] = [];\n\n for (const serverName in this._clients) {\n const client = this._clients[serverName];\n if (client.isEnabled() && !this.hasError(serverName)) {\n allPrompts.push(...(await client.getActivePrompts(ai)));\n }\n }\n return allPrompts;\n }\n\n /**\n * Get the specified prompt as an `ExecutablePrompt` available through the\n * specified server. If no such prompt is found, return undefined.\n */\n async getPrompt(\n ai: Genkit,\n serverName: string,\n promptName: string,\n opts?: PromptGenerateOptions\n ): Promise<ExecutablePrompt<any> | undefined> {\n await this.ready();\n const client = this._clients[serverName];\n if (!client) {\n logger.error(`No client found with name '${serverName}'.`);\n return;\n }\n if (this.hasError(serverName)) {\n const errorStringified = JSON.stringify(\n this._clientStates[serverName].error\n );\n logger.error(\n `Client '${serverName}' is in an error state. ${errorStringified}`\n );\n }\n if (client.isEnabled()) {\n const prompt = await client.getPrompt(ai, promptName, opts);\n if (!prompt) {\n logger.error(\n `[MCP Host] Unable to fetch the specified ${promptName} in server ${serverName}.`\n );\n return;\n }\n return prompt;\n }\n return;\n }\n\n async close() {\n for (const client of Object.values(this._clients)) {\n await client._disconnect();\n }\n }\n\n /** Helper method to track and log client errors. */\n private setError(\n serverName: string,\n error: {\n message: string;\n detail?: any;\n }\n ) {\n this._clientStates[serverName] = { error };\n logger.warn(\n `An error has occured while managing your MCP client '${serverName}'. The client may be disabled to avoid further issues. Please resolve the issue and reenable the client '${serverName}' to continue using its resources.`\n );\n logger.warn(error);\n }\n\n private hasError(serverName: string) {\n return (\n this._clientStates[serverName] && !!this._clientStates[serverName].error\n );\n }\n\n /**\n * Returns an array of all active clients.\n */\n get activeClients(): GenkitMcpClient[] {\n return Object.values(this._clients).filter((c) => c.isEnabled());\n }\n\n /**\n * Returns the client by name.\n */\n getClient(name: string) {\n return this._clients[name];\n }\n}\n"],"mappings":"AAwBA,SAAS,cAAc;AACvB,SAAS,uBAAwC;AAkD1C,MAAM,cAAc;AAAA,EACzB;AAAA,EACQ,WAA4C,CAAC;AAAA,EAC7C,gBAA6C,CAAC;AAAA,EAC9C,kBAGF,CAAC;AAAA,EACC,SAAS;AAAA,EACT;AAAA,EACR;AAAA,EAEA,YAAY,SAAyB;AACnC,SAAK,OAAO,QAAQ,QAAQ;AAC5B,SAAK,mBAAmB,QAAQ;AAChC,SAAK,QAAQ,QAAQ;AAErB,QAAI,QAAQ,YAAY;AACtB,WAAK,cAAc,QAAQ,UAAU;AAAA,IACvC,OAAO;AACL,WAAK,SAAS;AAAA,IAChB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,QAAQ;AACZ,QAAI,KAAK,OAAQ;AACjB,WAAO,IAAI,QAAc,CAAC,SAAS,WAAW;AAC5C,WAAK,gBAAgB,KAAK,EAAE,SAAS,OAAO,CAAC;AAAA,IAC/C,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,MAAM,QAAQ,YAAoB,QAAyB;AACzD,UAAM,gBAAgB,KAAK,SAAS,UAAU;AAC9C,QAAI,eAAe;AACjB,UAAI;AACF,cAAM,cAAc,YAAY;AAAA,MAClC,SAAS,GAAG;AACV,sBAAc,QAAQ;AAC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B,YAAM,mBAAmB,KAAK;AAAA,QAC5B,KAAK,cAAc,UAAU,EAAE;AAAA,MACjC;AACA,aAAO;AAAA,QACL,WAAW,UAAU,2BAA2B,gBAAgB;AAAA,MAClE;AAAA,IACF;AACA,QAAI,OAAO,UAAU,GAAG;AACtB,YAAM,SAAS,MAAM,OAAO,UAAU,IAAI,YAAY,IAAI;AAC1D,UAAI,CAAC,QAAQ;AACX,eAAO;AAAA,UACL,4CAA4C,UAAU,cAAc,UAAU;AAAA,QAChF;AACA;AAAA,MACF;AACA,aAAO;AAAA,IACT;AACA;AAAA,EACF;AAAA,EAEA,MAAM,QAAQ;AACZ,eAAW,UAAU,OAAO,OAAO,KAAK,QAAQ,GAAG;AACjD,YAAM,OAAO,YAAY;AAAA,IAC3B;AAAA,EACF;AAAA;AAAA,EAGQ,SACN,YACA,OAIA;AACA,SAAK,cAAc,UAAU,IAAI,EAAE,MAAM;AACzC,WAAO;AAAA,MACL,wDAAwD,UAAU,4GAA4G,UAAU;AAAA,IAC1L;AACA,WAAO,KAAK,KAAK;AAAA,EACnB;AAAA,EAEQ,SAAS,YAAoB;AACnC,WACE,KAAK,cAAc,UAAU,KAAK,CAAC,CAAC,KAAK,cAAc,UAAU,EAAE;AAAA,EAEvE;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,gBAAmC;AACrC,WAAO,OAAO,OAAO,KAAK,QAAQ,EAAE,OAAO,CAAC,MAAM,EAAE,UAAU,CAAC;AAAA,EACjE;AAAA;AAAA;AAAA;AAAA,EAKA,UAAU,MAAc;AACtB,WAAO,KAAK,SAAS,IAAI;AAAA,EAC3B;AACF;","names":[]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { SSEClientTransportOptions } from '@modelcontextprotocol/sdk/client/sse.js';
|
|
2
|
+
export { StdioServerParameters } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
3
|
+
export { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';
|
|
4
|
+
export { GenkitMcpClient, McpClientOptions } from './client.mjs';
|
|
5
|
+
export { GenkitMcpHost, McpHostOptions } from './host.mjs';
|
|
6
|
+
import '@modelcontextprotocol/sdk/client/index.js';
|
|
7
|
+
import '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
8
|
+
import '@modelcontextprotocol/sdk/types.js';
|
|
9
|
+
import 'genkit';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { SSEClientTransportOptions } from '@modelcontextprotocol/sdk/client/sse.js';
|
|
2
|
+
export { StdioServerParameters } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
3
|
+
export { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';
|
|
4
|
+
export { GenkitMcpClient, McpClientOptions } from './client.js';
|
|
5
|
+
export { GenkitMcpHost, McpHostOptions } from './host.js';
|
|
6
|
+
import '@modelcontextprotocol/sdk/client/index.js';
|
|
7
|
+
import '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
8
|
+
import '@modelcontextprotocol/sdk/types.js';
|
|
9
|
+
import 'genkit';
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
var client_exports = {};
|
|
20
|
+
__export(client_exports, {
|
|
21
|
+
GenkitMcpClient: () => import_client.GenkitMcpClient,
|
|
22
|
+
GenkitMcpHost: () => import_host.GenkitMcpHost
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(client_exports);
|
|
25
|
+
var import_client = require("./client.js");
|
|
26
|
+
var import_host = require("./host.js");
|
|
27
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
28
|
+
0 && (module.exports = {
|
|
29
|
+
GenkitMcpClient,
|
|
30
|
+
GenkitMcpHost
|
|
31
|
+
});
|
|
32
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/client/index.ts"],"sourcesContent":["/**\n * Copyright 2025 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { SSEClientTransportOptions } from '@modelcontextprotocol/sdk/client/sse.js';\nimport { StdioServerParameters } from '@modelcontextprotocol/sdk/client/stdio.js';\nimport { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';\nimport { GenkitMcpClient, McpClientOptions } from './client.js';\nimport { GenkitMcpHost, McpHostOptions } from './host.js';\nexport { GenkitMcpClient, GenkitMcpHost };\nexport type {\n McpClientOptions,\n McpHostOptions,\n SSEClientTransportOptions,\n StdioServerParameters,\n Transport,\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAmBA,oBAAkD;AAClD,kBAA8C;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/client/index.ts"],"sourcesContent":["/**\n * Copyright 2025 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { SSEClientTransportOptions } from '@modelcontextprotocol/sdk/client/sse.js';\nimport { StdioServerParameters } from '@modelcontextprotocol/sdk/client/stdio.js';\nimport { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';\nimport { GenkitMcpClient, McpClientOptions } from './client.js';\nimport { GenkitMcpHost, McpHostOptions } from './host.js';\nexport { GenkitMcpClient, GenkitMcpHost };\nexport type {\n McpClientOptions,\n McpHostOptions,\n SSEClientTransportOptions,\n StdioServerParameters,\n Transport,\n};\n"],"mappings":"AAmBA,SAAS,uBAAyC;AAClD,SAAS,qBAAqC;","names":[]}
|
package/lib/index.d.mts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import 'genkit';
|
|
2
|
+
export { GenkitMcpClient, McpClientOptions, McpServerConfig, McpStdioServerConfig } from './client/client.mjs';
|
|
3
|
+
export { GenkitMcpHost, McpHostOptions } from './client/host.mjs';
|
|
4
|
+
export { M as McpServerOptions, a as createMcpClient, c as createMcpHost, b as createMcpServer } from './server.mjs';
|
|
5
|
+
import '@modelcontextprotocol/sdk/client/index.js';
|
|
6
|
+
import '@modelcontextprotocol/sdk/client/stdio.js';
|
|
7
|
+
import '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
8
|
+
import '@modelcontextprotocol/sdk/shared/transport.js';
|
|
9
|
+
import '@modelcontextprotocol/sdk/types.js';
|
|
10
|
+
import '@modelcontextprotocol/sdk/client/sse.js';
|
|
11
|
+
import '@modelcontextprotocol/sdk/server/index.js';
|
|
12
|
+
import 'genkit/tool';
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import 'genkit';
|
|
2
|
+
export { GenkitMcpClient, McpClientOptions, McpServerConfig, McpStdioServerConfig } from './client/client.js';
|
|
3
|
+
export { GenkitMcpHost, McpHostOptions } from './client/host.js';
|
|
4
|
+
export { M as McpServerOptions, a as createMcpClient, c as createMcpHost, b as createMcpServer } from './server.js';
|
|
5
|
+
import '@modelcontextprotocol/sdk/client/index.js';
|
|
6
|
+
import '@modelcontextprotocol/sdk/client/stdio.js';
|
|
7
|
+
import '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
8
|
+
import '@modelcontextprotocol/sdk/shared/transport.js';
|
|
9
|
+
import '@modelcontextprotocol/sdk/types.js';
|
|
10
|
+
import '@modelcontextprotocol/sdk/client/sse.js';
|
|
11
|
+
import '@modelcontextprotocol/sdk/server/index.js';
|
|
12
|
+
import 'genkit/tool';
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
var index_exports = {};
|
|
20
|
+
__export(index_exports, {
|
|
21
|
+
GenkitMcpClient: () => import_client.GenkitMcpClient,
|
|
22
|
+
GenkitMcpHost: () => import_client2.GenkitMcpHost,
|
|
23
|
+
createMcpClient: () => createMcpClient,
|
|
24
|
+
createMcpHost: () => createMcpHost,
|
|
25
|
+
createMcpServer: () => createMcpServer
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
var import_client = require("./client/client.js");
|
|
29
|
+
var import_client2 = require("./client/index.js");
|
|
30
|
+
var import_server = require("./server.js");
|
|
31
|
+
function createMcpHost(options) {
|
|
32
|
+
return new import_client2.GenkitMcpHost(options);
|
|
33
|
+
}
|
|
34
|
+
function createMcpClient(options) {
|
|
35
|
+
return new import_client.GenkitMcpClient(options);
|
|
36
|
+
}
|
|
37
|
+
function createMcpServer(ai, options) {
|
|
38
|
+
return new import_server.GenkitMcpServer(ai, options);
|
|
39
|
+
}
|
|
40
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
41
|
+
0 && (module.exports = {
|
|
42
|
+
GenkitMcpClient,
|
|
43
|
+
GenkitMcpHost,
|
|
44
|
+
createMcpClient,
|
|
45
|
+
createMcpHost,
|
|
46
|
+
createMcpServer
|
|
47
|
+
});
|
|
48
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { Genkit } from 'genkit';\nimport {\n GenkitMcpClient,\n McpClientOptions,\n McpServerConfig,\n McpStdioServerConfig,\n} from './client/client.js';\nimport { GenkitMcpHost, McpHostOptions } from './client/index.js';\nimport { GenkitMcpServer } from './server.js';\nexport {\n GenkitMcpClient,\n GenkitMcpHost,\n type McpClientOptions,\n type McpHostOptions,\n type McpServerConfig,\n type McpStdioServerConfig,\n};\n\nexport interface McpServerOptions {\n /** The name you want to give your server for MCP inspection. */\n name: string;\n /** The version you want the server to advertise to clients. Defaults to\n * 1.0.0. */\n version?: string;\n}\n\n/**\n * Creates an MCP Client Host that connects to one or more MCP servers.\n * Each server is defined in the `mcpClients` option, where the key is a\n * client-side name for the server and the value is the server's configuration.\n *\n * By default, all servers in the config will be attempted to connect unless\n * their configuration includes `{disabled: true}`.\n *\n * ```ts\n * const clientHost = createMcpHost({\n * name: \"my-mcp-client-host\", // Name for the host itself\n * mcpServers: {\n * // Each key is a name for this client/server configuration\n * // Each value is an McpServerConfig object\n * gitToolServer: { command: \"uvx\", args: [\"mcp-server-git\"] },\n * customApiServer: { url: \"http://localhost:1234/mcp\" }\n * }\n * });\n * ```\n *\n * @param options Configuration for the MCP Client Host, including the definitions of MCP servers to connect to.\n * @returns A new instance of GenkitMcpHost.\n */\nexport function createMcpHost(options: McpHostOptions) {\n return new GenkitMcpHost(options);\n}\n\n/**\n * Creates an MCP Client that connects to a single MCP server.\n * This is useful when you only need to interact with one MCP server,\n * or if you want to manage client instances individually.\n *\n * ```ts\n * const client = createMcpClient({\n * name: \"mySingleMcpClient\", // A name for this client instance\n * command: \"npx\", // Example: Launching a local server\n * args: [\"-y\", \"@modelcontextprotocol/server-everything\", \"/path/to/allowed/dir\"],\n * });\n *\n * // To get tools from this client:\n * // const tools = await client.getActiveTools(ai);\n * ```\n *\n * @param options Configuration for the MCP Client, defining how it connects\n * to the MCP server and its behavior.\n * @returns A new instance of GenkitMcpClient.\n */\nexport function createMcpClient(options: McpClientOptions) {\n return new GenkitMcpClient(options);\n}\n\n/**\n * Creates an MCP server based on the supplied Genkit instance. All tools and prompts\n * will be automatically converted to MCP compatibility.\n *\n * ```ts\n * const mcpServer = createMcpServer(ai, {name: 'my-mcp-server', version: '0.1.0'});\n *\n * await mcpServer.start(); // starts a stdio transport, OR\n * await mcpServer.start(customMcpTransport); // starts server using supplied transport\n * ```\n *\n * @param ai Your Genkit instance with registered tools and prompts.\n * @param options Configuration metadata for the server.\n * @returns GenkitMcpServer instance.\n */\nexport function createMcpServer(\n ai: Genkit,\n options: McpServerOptions\n): GenkitMcpServer {\n return new GenkitMcpServer(ai, options);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAiBA,oBAKO;AACP,IAAAA,iBAA8C;AAC9C,oBAAgC;AAyCzB,SAAS,cAAc,SAAyB;AACrD,SAAO,IAAI,6BAAc,OAAO;AAClC;AAsBO,SAAS,gBAAgB,SAA2B;AACzD,SAAO,IAAI,8BAAgB,OAAO;AACpC;AAiBO,SAAS,gBACd,IACA,SACiB;AACjB,SAAO,IAAI,8BAAgB,IAAI,OAAO;AACxC;","names":["import_client"]}
|
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {
|
|
2
|
+
GenkitMcpClient
|
|
3
|
+
} from "./client/client.js";
|
|
4
|
+
import { GenkitMcpHost } from "./client/index.js";
|
|
5
|
+
import { GenkitMcpServer } from "./server.js";
|
|
6
|
+
function createMcpHost(options) {
|
|
7
|
+
return new GenkitMcpHost(options);
|
|
8
|
+
}
|
|
9
|
+
function createMcpClient(options) {
|
|
10
|
+
return new GenkitMcpClient(options);
|
|
11
|
+
}
|
|
12
|
+
function createMcpServer(ai, options) {
|
|
13
|
+
return new GenkitMcpServer(ai, options);
|
|
14
|
+
}
|
|
15
|
+
export {
|
|
16
|
+
GenkitMcpClient,
|
|
17
|
+
GenkitMcpHost,
|
|
18
|
+
createMcpClient,
|
|
19
|
+
createMcpHost,
|
|
20
|
+
createMcpServer
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { Genkit } from 'genkit';\nimport {\n GenkitMcpClient,\n McpClientOptions,\n McpServerConfig,\n McpStdioServerConfig,\n} from './client/client.js';\nimport { GenkitMcpHost, McpHostOptions } from './client/index.js';\nimport { GenkitMcpServer } from './server.js';\nexport {\n GenkitMcpClient,\n GenkitMcpHost,\n type McpClientOptions,\n type McpHostOptions,\n type McpServerConfig,\n type McpStdioServerConfig,\n};\n\nexport interface McpServerOptions {\n /** The name you want to give your server for MCP inspection. */\n name: string;\n /** The version you want the server to advertise to clients. Defaults to\n * 1.0.0. */\n version?: string;\n}\n\n/**\n * Creates an MCP Client Host that connects to one or more MCP servers.\n * Each server is defined in the `mcpClients` option, where the key is a\n * client-side name for the server and the value is the server's configuration.\n *\n * By default, all servers in the config will be attempted to connect unless\n * their configuration includes `{disabled: true}`.\n *\n * ```ts\n * const clientHost = createMcpHost({\n * name: \"my-mcp-client-host\", // Name for the host itself\n * mcpServers: {\n * // Each key is a name for this client/server configuration\n * // Each value is an McpServerConfig object\n * gitToolServer: { command: \"uvx\", args: [\"mcp-server-git\"] },\n * customApiServer: { url: \"http://localhost:1234/mcp\" }\n * }\n * });\n * ```\n *\n * @param options Configuration for the MCP Client Host, including the definitions of MCP servers to connect to.\n * @returns A new instance of GenkitMcpHost.\n */\nexport function createMcpHost(options: McpHostOptions) {\n return new GenkitMcpHost(options);\n}\n\n/**\n * Creates an MCP Client that connects to a single MCP server.\n * This is useful when you only need to interact with one MCP server,\n * or if you want to manage client instances individually.\n *\n * ```ts\n * const client = createMcpClient({\n * name: \"mySingleMcpClient\", // A name for this client instance\n * command: \"npx\", // Example: Launching a local server\n * args: [\"-y\", \"@modelcontextprotocol/server-everything\", \"/path/to/allowed/dir\"],\n * });\n *\n * // To get tools from this client:\n * // const tools = await client.getActiveTools(ai);\n * ```\n *\n * @param options Configuration for the MCP Client, defining how it connects\n * to the MCP server and its behavior.\n * @returns A new instance of GenkitMcpClient.\n */\nexport function createMcpClient(options: McpClientOptions) {\n return new GenkitMcpClient(options);\n}\n\n/**\n * Creates an MCP server based on the supplied Genkit instance. All tools and prompts\n * will be automatically converted to MCP compatibility.\n *\n * ```ts\n * const mcpServer = createMcpServer(ai, {name: 'my-mcp-server', version: '0.1.0'});\n *\n * await mcpServer.start(); // starts a stdio transport, OR\n * await mcpServer.start(customMcpTransport); // starts server using supplied transport\n * ```\n *\n * @param ai Your Genkit instance with registered tools and prompts.\n * @param options Configuration metadata for the server.\n * @returns GenkitMcpServer instance.\n */\nexport function createMcpServer(\n ai: Genkit,\n options: McpServerOptions\n): GenkitMcpServer {\n return new GenkitMcpServer(ai, options);\n}\n"],"mappings":"AAiBA;AAAA,EACE;AAAA,OAIK;AACP,SAAS,qBAAqC;AAC9C,SAAS,uBAAuB;AAyCzB,SAAS,cAAc,SAAyB;AACrD,SAAO,IAAI,cAAc,OAAO;AAClC;AAsBO,SAAS,gBAAgB,SAA2B;AACzD,SAAO,IAAI,gBAAgB,OAAO;AACpC;AAiBO,SAAS,gBACd,IACA,SACiB;AACjB,SAAO,IAAI,gBAAgB,IAAI,OAAO;AACxC;","names":[]}
|