@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.
Files changed (88) hide show
  1. package/LICENSE +203 -0
  2. package/README.md +205 -0
  3. package/examples/client/index.js +36 -0
  4. package/examples/client/package.json +25 -0
  5. package/examples/server/index.js +46 -0
  6. package/examples/server/package.json +18 -0
  7. package/examples/server/prompts/port_code.prompt +13 -0
  8. package/lib/client/client.d.mts +177 -0
  9. package/lib/client/client.d.ts +177 -0
  10. package/lib/client/client.js +282 -0
  11. package/lib/client/client.js.map +1 -0
  12. package/lib/client/client.mjs +267 -0
  13. package/lib/client/client.mjs.map +1 -0
  14. package/lib/client/host.d.mts +202 -0
  15. package/lib/client/host.d.ts +202 -0
  16. package/lib/client/host.js +392 -0
  17. package/lib/client/host.js.map +1 -0
  18. package/lib/client/host.mjs +368 -0
  19. package/lib/client/host.mjs.map +1 -0
  20. package/lib/client/index.d.mts +9 -0
  21. package/lib/client/index.d.ts +9 -0
  22. package/lib/client/index.js +32 -0
  23. package/lib/client/index.js.map +1 -0
  24. package/lib/client/index.mjs +7 -0
  25. package/lib/client/index.mjs.map +1 -0
  26. package/lib/index.d.mts +12 -0
  27. package/lib/index.d.ts +12 -0
  28. package/lib/index.js +48 -0
  29. package/lib/index.js.map +1 -0
  30. package/lib/index.mjs +22 -0
  31. package/lib/index.mjs.map +1 -0
  32. package/lib/server.d.mts +188 -0
  33. package/lib/server.d.ts +188 -0
  34. package/lib/server.js +280 -0
  35. package/lib/server.js.map +1 -0
  36. package/lib/server.mjs +249 -0
  37. package/lib/server.mjs.map +1 -0
  38. package/lib/util/index.d.mts +11 -0
  39. package/lib/util/index.d.ts +11 -0
  40. package/lib/util/index.js +29 -0
  41. package/lib/util/index.js.map +1 -0
  42. package/lib/util/index.mjs +5 -0
  43. package/lib/util/index.mjs.map +1 -0
  44. package/lib/util/message.d.mts +43 -0
  45. package/lib/util/message.d.ts +43 -0
  46. package/lib/util/message.js +61 -0
  47. package/lib/util/message.js.map +1 -0
  48. package/lib/util/message.mjs +36 -0
  49. package/lib/util/message.mjs.map +1 -0
  50. package/lib/util/prompts.d.mts +45 -0
  51. package/lib/util/prompts.d.ts +45 -0
  52. package/lib/util/prompts.js +147 -0
  53. package/lib/util/prompts.js.map +1 -0
  54. package/lib/util/prompts.mjs +123 -0
  55. package/lib/util/prompts.mjs.map +1 -0
  56. package/lib/util/resource.d.mts +28 -0
  57. package/lib/util/resource.d.ts +28 -0
  58. package/lib/util/resource.js +116 -0
  59. package/lib/util/resource.js.map +1 -0
  60. package/lib/util/resource.mjs +95 -0
  61. package/lib/util/resource.mjs.map +1 -0
  62. package/lib/util/tools.d.mts +37 -0
  63. package/lib/util/tools.d.ts +37 -0
  64. package/lib/util/tools.js +120 -0
  65. package/lib/util/tools.js.map +1 -0
  66. package/lib/util/tools.mjs +95 -0
  67. package/lib/util/tools.mjs.map +1 -0
  68. package/lib/util/transport.d.mts +39 -0
  69. package/lib/util/transport.d.ts +39 -0
  70. package/lib/util/transport.js +63 -0
  71. package/lib/util/transport.js.map +1 -0
  72. package/lib/util/transport.mjs +29 -0
  73. package/lib/util/transport.mjs.map +1 -0
  74. package/package.json +57 -0
  75. package/src/client/client.ts +414 -0
  76. package/src/client/host.ts +485 -0
  77. package/src/client/index.ts +29 -0
  78. package/src/index.ts +114 -0
  79. package/src/server.ts +330 -0
  80. package/src/util/index.ts +20 -0
  81. package/src/util/message.ts +72 -0
  82. package/src/util/prompts.ts +223 -0
  83. package/src/util/resource.ts +141 -0
  84. package/src/util/tools.ts +164 -0
  85. package/src/util/transport.ts +67 -0
  86. package/tests/fakes.ts +221 -0
  87. package/tests/host_test.ts +609 -0
  88. package/tests/server_test.ts +165 -0
@@ -0,0 +1,37 @@
1
+ import { Client } from '@modelcontextprotocol/sdk/client/index.js';
2
+ import { Genkit, ToolAction } from 'genkit';
3
+
4
+ /**
5
+ * Copyright 2024 Google LLC
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ /**
21
+ * Lookup all tools available in the server and register each as a Genkit tool.
22
+ */
23
+ declare function registerAllTools(ai: Genkit, client: Client, params: {
24
+ name: string;
25
+ serverName: string;
26
+ rawToolResponses?: boolean;
27
+ }): Promise<void>;
28
+ /**
29
+ * Lookup all tools available in the server and fetches as a Genkit dynamic tool.
30
+ */
31
+ declare function fetchDynamicTools(ai: Genkit, client: Client, params: {
32
+ name: string;
33
+ serverName: string;
34
+ rawToolResponses?: boolean;
35
+ }): Promise<ToolAction[]>;
36
+
37
+ export { fetchDynamicTools, registerAllTools };
@@ -0,0 +1,37 @@
1
+ import { Client } from '@modelcontextprotocol/sdk/client/index.js';
2
+ import { Genkit, ToolAction } from 'genkit';
3
+
4
+ /**
5
+ * Copyright 2024 Google LLC
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ /**
21
+ * Lookup all tools available in the server and register each as a Genkit tool.
22
+ */
23
+ declare function registerAllTools(ai: Genkit, client: Client, params: {
24
+ name: string;
25
+ serverName: string;
26
+ rawToolResponses?: boolean;
27
+ }): Promise<void>;
28
+ /**
29
+ * Lookup all tools available in the server and fetches as a Genkit dynamic tool.
30
+ */
31
+ declare function fetchDynamicTools(ai: Genkit, client: Client, params: {
32
+ name: string;
33
+ serverName: string;
34
+ rawToolResponses?: boolean;
35
+ }): Promise<ToolAction[]>;
36
+
37
+ export { fetchDynamicTools, registerAllTools };
@@ -0,0 +1,120 @@
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 tools_exports = {};
20
+ __export(tools_exports, {
21
+ fetchDynamicTools: () => fetchDynamicTools,
22
+ registerAllTools: () => registerAllTools
23
+ });
24
+ module.exports = __toCommonJS(tools_exports);
25
+ var import_genkit = require("genkit");
26
+ var import_logging = require("genkit/logging");
27
+ const toText = (c) => c.map((p) => p.text || "").join("");
28
+ function processResult(result) {
29
+ if (result.isError) return { error: toText(result.content) };
30
+ if (result.content.every((c) => !!c.text)) {
31
+ const text = toText(result.content);
32
+ if (text.trim().startsWith("{") || text.trim().startsWith("[")) {
33
+ try {
34
+ return JSON.parse(text);
35
+ } catch (e) {
36
+ return text;
37
+ }
38
+ }
39
+ return text;
40
+ }
41
+ if (result.content.length === 1) return result.content[0];
42
+ return result;
43
+ }
44
+ function registerTool(ai, client, tool, params) {
45
+ import_logging.logger.debug(
46
+ `[MCP] Registering tool '${params.name}/${tool.name}'' from server '${params.serverName}'`
47
+ );
48
+ ai.defineTool(
49
+ {
50
+ name: `${params.serverName}/${tool.name}`,
51
+ description: tool.description || "",
52
+ inputJsonSchema: tool.inputSchema,
53
+ outputSchema: import_genkit.z.any(),
54
+ metadata: { mcp: { _meta: tool._meta || {} } }
55
+ },
56
+ async (args) => {
57
+ import_logging.logger.debug(
58
+ `[MCP] Calling MCP tool '${params.serverName}/${tool.name}' with arguments`,
59
+ JSON.stringify(args)
60
+ );
61
+ const result = await client.callTool({
62
+ name: tool.name,
63
+ arguments: args
64
+ });
65
+ if (params.rawToolResponses) return result;
66
+ return processResult(result);
67
+ }
68
+ );
69
+ }
70
+ function createDynamicTool(ai, client, tool, params) {
71
+ return ai.dynamicTool(
72
+ {
73
+ name: `${params.serverName}/${tool.name}`,
74
+ description: tool.description || "",
75
+ inputJsonSchema: tool.inputSchema,
76
+ outputSchema: import_genkit.z.any(),
77
+ metadata: { mcp: { _meta: tool._meta || {} } }
78
+ },
79
+ async (args, { context }) => {
80
+ import_logging.logger.debug(
81
+ `[MCP] calling tool '${params.serverName}/${tool.name}' in host '${params.name}'`
82
+ );
83
+ const result = await client.callTool({
84
+ name: tool.name,
85
+ arguments: args,
86
+ _meta: context?.mcp?._meta
87
+ });
88
+ if (params.rawToolResponses) return result;
89
+ return processResult(result);
90
+ }
91
+ );
92
+ }
93
+ async function registerAllTools(ai, client, params) {
94
+ let cursor;
95
+ while (true) {
96
+ const { nextCursor, tools } = await client.listTools({ cursor });
97
+ tools.forEach((t) => registerTool(ai, client, t, params));
98
+ cursor = nextCursor;
99
+ if (!cursor) break;
100
+ }
101
+ }
102
+ async function fetchDynamicTools(ai, client, params) {
103
+ let cursor;
104
+ let allTools = [];
105
+ while (true) {
106
+ const { nextCursor, tools } = await client.listTools({ cursor });
107
+ allTools.push(
108
+ ...tools.map((t) => createDynamicTool(ai, client, t, params))
109
+ );
110
+ cursor = nextCursor;
111
+ if (!cursor) break;
112
+ }
113
+ return allTools;
114
+ }
115
+ // Annotate the CommonJS export names for ESM import in node:
116
+ 0 && (module.exports = {
117
+ fetchDynamicTools,
118
+ registerAllTools
119
+ });
120
+ //# sourceMappingURL=tools.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/util/tools.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 { Client } from '@modelcontextprotocol/sdk/client/index.js';\nimport type {\n CallToolResult,\n Tool,\n} from '@modelcontextprotocol/sdk/types.js' with { 'resolution-mode': 'import' };\nimport { Genkit, JSONSchema7, ToolAction, z } from 'genkit';\nimport { logger } from 'genkit/logging';\n\nconst toText = (c: CallToolResult['content']) =>\n c.map((p) => p.text || '').join('');\n\nfunction processResult(result: CallToolResult) {\n if (result.isError) return { error: toText(result.content) };\n if (result.content.every((c) => !!c.text)) {\n const text = toText(result.content);\n if (text.trim().startsWith('{') || text.trim().startsWith('[')) {\n try {\n return JSON.parse(text);\n } catch (e) {\n return text;\n }\n }\n return text;\n }\n if (result.content.length === 1) return result.content[0];\n return result;\n}\n\n/**\n * Registers a single MCP tool as a Genkit tool.\n * It defines a new Genkit tool action that, when called, will\n * interact with the MCP client to execute the corresponding MCP tool.\n *\n * @param ai The Genkit instance to define the tool on.\n * @param client The MCP client instance used to interact with the MCP server.\n * @param tool The MCP Tool object to register.\n * @param params Contains the Genkit client name, MCP server name for namespacing,\n * and a flag for raw tool responses.\n */\nfunction registerTool(\n ai: Genkit,\n client: Client,\n tool: Tool,\n params: { serverName: string; name: string; rawToolResponses?: boolean }\n) {\n logger.debug(\n `[MCP] Registering tool '${params.name}/${tool.name}'' from server '${params.serverName}'`\n );\n ai.defineTool(\n {\n name: `${params.serverName}/${tool.name}`,\n description: tool.description || '',\n inputJsonSchema: tool.inputSchema as JSONSchema7,\n outputSchema: z.any(),\n metadata: { mcp: { _meta: tool._meta || {} } },\n },\n async (args) => {\n logger.debug(\n `[MCP] Calling MCP tool '${params.serverName}/${tool.name}' with arguments`,\n JSON.stringify(args)\n );\n const result = await client.callTool({\n name: tool.name,\n arguments: args,\n });\n if (params.rawToolResponses) return result;\n return processResult(result as CallToolResult);\n }\n );\n}\n\n/**\n * Creates a Genkit dynamic tool action for a given MCP tool.\n * This is similar to `registerTool` but returns the `ToolAction` directly\n * instead of defining it on the Genkit instance.\n *\n * @param ai The Genkit instance, used for creating the dynamic tool.\n * @param client The MCP client instance.\n * @param tool The MCP Tool object.\n * @param params Configuration parameters including namespacing and raw response flag.\n * @returns A Genkit `ToolAction` representing the MCP tool.\n */\nfunction createDynamicTool(\n ai: Genkit,\n client: Client,\n tool: Tool,\n params: { serverName: string; name: string; rawToolResponses?: boolean }\n): ToolAction {\n return ai.dynamicTool(\n {\n name: `${params.serverName}/${tool.name}`,\n description: tool.description || '',\n inputJsonSchema: tool.inputSchema as JSONSchema7,\n outputSchema: z.any(),\n metadata: { mcp: { _meta: tool._meta || {} } },\n },\n async (args, { context }) => {\n logger.debug(\n `[MCP] calling tool '${params.serverName}/${tool.name}' in host '${params.name}'`\n );\n const result = await client.callTool({\n name: tool.name,\n arguments: args,\n _meta: context?.mcp?._meta,\n });\n if (params.rawToolResponses) return result;\n return processResult(result as CallToolResult);\n }\n );\n}\n\n/**\n * Lookup all tools available in the server and register each as a Genkit tool.\n */\nexport async function registerAllTools(\n ai: Genkit,\n client: Client,\n params: { name: string; serverName: string; rawToolResponses?: boolean }\n): Promise<void> {\n let cursor: string | undefined;\n while (true) {\n const { nextCursor, tools } = await client.listTools({ cursor });\n tools.forEach((t) => registerTool(ai, client, t, params));\n cursor = nextCursor;\n if (!cursor) break;\n }\n}\n\n/**\n * Lookup all tools available in the server and fetches as a Genkit dynamic tool.\n */\nexport async function fetchDynamicTools(\n ai: Genkit,\n client: Client,\n params: { name: string; serverName: string; rawToolResponses?: boolean }\n): Promise<ToolAction[]> {\n let cursor: string | undefined;\n let allTools: ToolAction[] = [];\n while (true) {\n const { nextCursor, tools } = await client.listTools({ cursor });\n allTools.push(\n ...tools.map((t) => createDynamicTool(ai, client, t, params))\n );\n cursor = nextCursor;\n if (!cursor) break;\n }\n return allTools;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAqBA,oBAAmD;AACnD,qBAAuB;AAEvB,MAAM,SAAS,CAAC,MACd,EAAE,IAAI,CAAC,MAAM,EAAE,QAAQ,EAAE,EAAE,KAAK,EAAE;AAEpC,SAAS,cAAc,QAAwB;AAC7C,MAAI,OAAO,QAAS,QAAO,EAAE,OAAO,OAAO,OAAO,OAAO,EAAE;AAC3D,MAAI,OAAO,QAAQ,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,IAAI,GAAG;AACzC,UAAM,OAAO,OAAO,OAAO,OAAO;AAClC,QAAI,KAAK,KAAK,EAAE,WAAW,GAAG,KAAK,KAAK,KAAK,EAAE,WAAW,GAAG,GAAG;AAC9D,UAAI;AACF,eAAO,KAAK,MAAM,IAAI;AAAA,MACxB,SAAS,GAAG;AACV,eAAO;AAAA,MACT;AAAA,IACF;AACA,WAAO;AAAA,EACT;AACA,MAAI,OAAO,QAAQ,WAAW,EAAG,QAAO,OAAO,QAAQ,CAAC;AACxD,SAAO;AACT;AAaA,SAAS,aACP,IACA,QACA,MACA,QACA;AACA,wBAAO;AAAA,IACL,2BAA2B,OAAO,IAAI,IAAI,KAAK,IAAI,mBAAmB,OAAO,UAAU;AAAA,EACzF;AACA,KAAG;AAAA,IACD;AAAA,MACE,MAAM,GAAG,OAAO,UAAU,IAAI,KAAK,IAAI;AAAA,MACvC,a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AC;AAC/D,UAAM,QAAQ,CAAC,MAAM,aAAa,IAAI,QAAQ,GAAG,MAAM,CAAC;AACxD,aAAS;AACT,QAAI,CAAC,OAAQ;AAAA,EACf;AACF;AAKA,eAAsB,kBACpB,IACA,QACA,QACuB;AACvB,MAAI;AACJ,MAAI,WAAyB,CAAC;AAC9B,SAAO,MAAM;AACX,UAAM,EAAE,YAAY,MAAM,IAAI,MAAM,OAAO,UAAU,EAAE,OAAO,CAAC;AAC/D,aAAS;AAAA,MACP,GAAG,MAAM,IAAI,CAAC,MAAM,kBAAkB,IAAI,QAAQ,GAAG,MAAM,CAAC;AAAA,IAC9D;AACA,aAAS;AACT,QAAI,CAAC,OAAQ;AAAA,EACf;AACA,SAAO;AACT;","names":[]}
@@ -0,0 +1,95 @@
1
+ import { z } from "genkit";
2
+ import { logger } from "genkit/logging";
3
+ const toText = (c) => c.map((p) => p.text || "").join("");
4
+ function processResult(result) {
5
+ if (result.isError) return { error: toText(result.content) };
6
+ if (result.content.every((c) => !!c.text)) {
7
+ const text = toText(result.content);
8
+ if (text.trim().startsWith("{") || text.trim().startsWith("[")) {
9
+ try {
10
+ return JSON.parse(text);
11
+ } catch (e) {
12
+ return text;
13
+ }
14
+ }
15
+ return text;
16
+ }
17
+ if (result.content.length === 1) return result.content[0];
18
+ return result;
19
+ }
20
+ function registerTool(ai, client, tool, params) {
21
+ logger.debug(
22
+ `[MCP] Registering tool '${params.name}/${tool.name}'' from server '${params.serverName}'`
23
+ );
24
+ ai.defineTool(
25
+ {
26
+ name: `${params.serverName}/${tool.name}`,
27
+ description: tool.description || "",
28
+ inputJsonSchema: tool.inputSchema,
29
+ outputSchema: z.any(),
30
+ metadata: { mcp: { _meta: tool._meta || {} } }
31
+ },
32
+ async (args) => {
33
+ logger.debug(
34
+ `[MCP] Calling MCP tool '${params.serverName}/${tool.name}' with arguments`,
35
+ JSON.stringify(args)
36
+ );
37
+ const result = await client.callTool({
38
+ name: tool.name,
39
+ arguments: args
40
+ });
41
+ if (params.rawToolResponses) return result;
42
+ return processResult(result);
43
+ }
44
+ );
45
+ }
46
+ function createDynamicTool(ai, client, tool, params) {
47
+ return ai.dynamicTool(
48
+ {
49
+ name: `${params.serverName}/${tool.name}`,
50
+ description: tool.description || "",
51
+ inputJsonSchema: tool.inputSchema,
52
+ outputSchema: z.any(),
53
+ metadata: { mcp: { _meta: tool._meta || {} } }
54
+ },
55
+ async (args, { context }) => {
56
+ logger.debug(
57
+ `[MCP] calling tool '${params.serverName}/${tool.name}' in host '${params.name}'`
58
+ );
59
+ const result = await client.callTool({
60
+ name: tool.name,
61
+ arguments: args,
62
+ _meta: context?.mcp?._meta
63
+ });
64
+ if (params.rawToolResponses) return result;
65
+ return processResult(result);
66
+ }
67
+ );
68
+ }
69
+ async function registerAllTools(ai, client, params) {
70
+ let cursor;
71
+ while (true) {
72
+ const { nextCursor, tools } = await client.listTools({ cursor });
73
+ tools.forEach((t) => registerTool(ai, client, t, params));
74
+ cursor = nextCursor;
75
+ if (!cursor) break;
76
+ }
77
+ }
78
+ async function fetchDynamicTools(ai, client, params) {
79
+ let cursor;
80
+ let allTools = [];
81
+ while (true) {
82
+ const { nextCursor, tools } = await client.listTools({ cursor });
83
+ allTools.push(
84
+ ...tools.map((t) => createDynamicTool(ai, client, t, params))
85
+ );
86
+ cursor = nextCursor;
87
+ if (!cursor) break;
88
+ }
89
+ return allTools;
90
+ }
91
+ export {
92
+ fetchDynamicTools,
93
+ registerAllTools
94
+ };
95
+ //# sourceMappingURL=tools.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/util/tools.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 { Client } from '@modelcontextprotocol/sdk/client/index.js';\nimport type {\n CallToolResult,\n Tool,\n} from '@modelcontextprotocol/sdk/types.js' with { 'resolution-mode': 'import' };\nimport { Genkit, JSONSchema7, ToolAction, z } from 'genkit';\nimport { logger } from 'genkit/logging';\n\nconst toText = (c: CallToolResult['content']) =>\n c.map((p) => p.text || '').join('');\n\nfunction processResult(result: CallToolResult) {\n if (result.isError) return { error: toText(result.content) };\n if (result.content.every((c) => !!c.text)) {\n const text = toText(result.content);\n if (text.trim().startsWith('{') || text.trim().startsWith('[')) {\n try {\n return JSON.parse(text);\n } catch (e) {\n return text;\n }\n }\n return text;\n }\n if (result.content.length === 1) return result.content[0];\n return result;\n}\n\n/**\n * Registers a single MCP tool as a Genkit tool.\n * It defines a new Genkit tool action that, when called, will\n * interact with the MCP client to execute the corresponding MCP tool.\n *\n * @param ai The Genkit instance to define the tool on.\n * @param client The MCP client instance used to interact with the MCP server.\n * @param tool The MCP Tool object to register.\n * @param params Contains the Genkit client name, MCP server name for namespacing,\n * and a flag for raw tool responses.\n */\nfunction registerTool(\n ai: Genkit,\n client: Client,\n tool: Tool,\n params: { serverName: string; name: string; rawToolResponses?: boolean }\n) {\n logger.debug(\n `[MCP] Registering tool '${params.name}/${tool.name}'' from server '${params.serverName}'`\n );\n ai.defineTool(\n {\n name: `${params.serverName}/${tool.name}`,\n description: tool.description || '',\n inputJsonSchema: tool.inputSchema as JSONSchema7,\n outputSchema: z.any(),\n metadata: { mcp: { _meta: tool._meta || {} } },\n },\n async (args) => {\n logger.debug(\n `[MCP] Calling MCP tool '${params.serverName}/${tool.name}' with arguments`,\n JSON.stringify(args)\n );\n const result = await client.callTool({\n name: tool.name,\n arguments: args,\n });\n if (params.rawToolResponses) return result;\n return processResult(result as CallToolResult);\n }\n );\n}\n\n/**\n * Creates a Genkit dynamic tool action for a given MCP tool.\n * This is similar to `registerTool` but returns the `ToolAction` directly\n * instead of defining it on the Genkit instance.\n *\n * @param ai The Genkit instance, used for creating the dynamic tool.\n * @param client The MCP client instance.\n * @param tool The MCP Tool object.\n * @param params Configuration parameters including namespacing and raw response flag.\n * @returns A Genkit `ToolAction` representing the MCP tool.\n */\nfunction createDynamicTool(\n ai: Genkit,\n client: Client,\n tool: Tool,\n params: { serverName: string; name: string; rawToolResponses?: boolean }\n): ToolAction {\n return ai.dynamicTool(\n {\n name: `${params.serverName}/${tool.name}`,\n description: tool.description || '',\n inputJsonSchema: tool.inputSchema as JSONSchema7,\n outputSchema: z.any(),\n metadata: { mcp: { _meta: tool._meta || {} } },\n },\n async (args, { context }) => {\n logger.debug(\n `[MCP] calling tool '${params.serverName}/${tool.name}' in host '${params.name}'`\n );\n const result = await client.callTool({\n name: tool.name,\n arguments: args,\n _meta: context?.mcp?._meta,\n });\n if (params.rawToolResponses) return result;\n return processResult(result as CallToolResult);\n }\n );\n}\n\n/**\n * Lookup all tools available in the server and register each as a Genkit tool.\n */\nexport async function registerAllTools(\n ai: Genkit,\n client: Client,\n params: { name: string; serverName: string; rawToolResponses?: boolean }\n): Promise<void> {\n let cursor: string | undefined;\n while (true) {\n const { nextCursor, tools } = await client.listTools({ cursor });\n tools.forEach((t) => registerTool(ai, client, t, params));\n cursor = nextCursor;\n if (!cursor) break;\n }\n}\n\n/**\n * Lookup all tools available in the server and fetches as a Genkit dynamic tool.\n */\nexport async function fetchDynamicTools(\n ai: Genkit,\n client: Client,\n params: { name: string; serverName: string; rawToolResponses?: boolean }\n): Promise<ToolAction[]> {\n let cursor: string | undefined;\n let allTools: ToolAction[] = [];\n while (true) {\n const { nextCursor, tools } = await client.listTools({ cursor });\n allTools.push(\n ...tools.map((t) => createDynamicTool(ai, client, t, params))\n );\n cursor = nextCursor;\n if (!cursor) break;\n }\n return allTools;\n}\n"],"mappings":"AAqBA,SAA0C,SAAS;AACnD,SAAS,cAAc;AAEvB,MAAM,SAAS,CAAC,MACd,EAAE,IAAI,CA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
+ import { McpServerConfig } from '../client/client.mjs';
2
+ import { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';
3
+ import '@modelcontextprotocol/sdk/client/index.js';
4
+ import '@modelcontextprotocol/sdk/client/stdio.js';
5
+ import '@modelcontextprotocol/sdk/client/streamableHttp.js';
6
+ import '@modelcontextprotocol/sdk/types.js';
7
+ import 'genkit';
8
+
9
+ /**
10
+ * Copyright 2025 Google LLC
11
+ *
12
+ * Licensed under the Apache License, Version 2.0 (the "License");
13
+ * you may not use this file except in compliance with the License.
14
+ * You may obtain a copy of the License at
15
+ *
16
+ * http://www.apache.org/licenses/LICENSE-2.0
17
+ *
18
+ * Unless required by applicable law or agreed to in writing, software
19
+ * distributed under the License is distributed on an "AS IS" BASIS,
20
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
+ * See the License for the specific language governing permissions and
22
+ * limitations under the License.
23
+ */
24
+
25
+ /**
26
+ * Creates an MCP transport instance based on the provided server configuration.
27
+ * It supports creating SSE, Stdio, or using a pre-configured custom transport.
28
+ *
29
+ * @param config The configuration for the MCP server, determining the type of transport to create.
30
+ * @returns A Promise resolving to an object containing the created `Transport` instance
31
+ * (or `null` if configuration is invalid) and a string indicating the `type` of transport.
32
+ * @throws May throw an error if essential MCP SDK components cannot be imported.
33
+ */
34
+ declare function transportFrom(config: McpServerConfig, sessionId?: string): Promise<{
35
+ transport: Transport | null;
36
+ type: string;
37
+ }>;
38
+
39
+ export { transportFrom };
@@ -0,0 +1,39 @@
1
+ import { McpServerConfig } from '../client/client.js';
2
+ import { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';
3
+ import '@modelcontextprotocol/sdk/client/index.js';
4
+ import '@modelcontextprotocol/sdk/client/stdio.js';
5
+ import '@modelcontextprotocol/sdk/client/streamableHttp.js';
6
+ import '@modelcontextprotocol/sdk/types.js';
7
+ import 'genkit';
8
+
9
+ /**
10
+ * Copyright 2025 Google LLC
11
+ *
12
+ * Licensed under the Apache License, Version 2.0 (the "License");
13
+ * you may not use this file except in compliance with the License.
14
+ * You may obtain a copy of the License at
15
+ *
16
+ * http://www.apache.org/licenses/LICENSE-2.0
17
+ *
18
+ * Unless required by applicable law or agreed to in writing, software
19
+ * distributed under the License is distributed on an "AS IS" BASIS,
20
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
+ * See the License for the specific language governing permissions and
22
+ * limitations under the License.
23
+ */
24
+
25
+ /**
26
+ * Creates an MCP transport instance based on the provided server configuration.
27
+ * It supports creating SSE, Stdio, or using a pre-configured custom transport.
28
+ *
29
+ * @param config The configuration for the MCP server, determining the type of transport to create.
30
+ * @returns A Promise resolving to an object containing the created `Transport` instance
31
+ * (or `null` if configuration is invalid) and a string indicating the `type` of transport.
32
+ * @throws May throw an error if essential MCP SDK components cannot be imported.
33
+ */
34
+ declare function transportFrom(config: McpServerConfig, sessionId?: string): Promise<{
35
+ transport: Transport | null;
36
+ type: string;
37
+ }>;
38
+
39
+ export { transportFrom };
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var transport_exports = {};
30
+ __export(transport_exports, {
31
+ transportFrom: () => transportFrom
32
+ });
33
+ module.exports = __toCommonJS(transport_exports);
34
+ async function transportFrom(config, sessionId) {
35
+ if ("transport" in config && config.transport) {
36
+ return { transport: config.transport, type: "custom" };
37
+ }
38
+ if ("url" in config && config.url) {
39
+ const { url, ...httpConfig } = config;
40
+ const { StreamableHTTPClientTransport } = await import("@modelcontextprotocol/sdk/client/streamableHttp.js");
41
+ return {
42
+ transport: new StreamableHTTPClientTransport(new URL(url), {
43
+ ...httpConfig,
44
+ sessionId
45
+ }),
46
+ type: "http"
47
+ };
48
+ }
49
+ if ("command" in config && config.command) {
50
+ const stdioConfig = { ...config };
51
+ const { StdioClientTransport } = await import("@modelcontextprotocol/sdk/client/stdio.js");
52
+ return {
53
+ transport: new StdioClientTransport(stdioConfig),
54
+ type: "stdio"
55
+ };
56
+ }
57
+ return { transport: null, type: "unknown" };
58
+ }
59
+ // Annotate the CommonJS export names for ESM import in node:
60
+ 0 && (module.exports = {
61
+ transportFrom
62
+ });
63
+ //# sourceMappingURL=transport.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/util/transport.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 { McpServerConfig } from '../client/client.js';\nimport type { StdioServerParameters, Transport } from '../client/index.js';\n\n/**\n * Creates an MCP transport instance based on the provided server configuration.\n * It supports creating SSE, Stdio, or using a pre-configured custom transport.\n *\n * @param config The configuration for the MCP server, determining the type of transport to create.\n * @returns A Promise resolving to an object containing the created `Transport` instance\n * (or `null` if configuration is invalid) and a string indicating the `type` of transport.\n * @throws May throw an error if essential MCP SDK components cannot be imported.\n */\nexport async function transportFrom(\n config: McpServerConfig,\n sessionId?: string\n): Promise<{\n transport: Transport | null;\n type: string;\n}> {\n // Handle pre-configured transport first\n if ('transport' in config && config.transport) {\n return { transport: config.transport, type: 'custom' };\n }\n // Handle SSE config\n if ('url' in config && config.url) {\n const { url, ...httpConfig } = config;\n const { StreamableHTTPClientTransport } = await import(\n '@modelcontextprotocol/sdk/client/streamableHttp.js'\n );\n return {\n transport: new StreamableHTTPClientTransport(new URL(url), {\n ...httpConfig,\n sessionId,\n }),\n type: 'http',\n };\n }\n // Handle Stdio config\n if ('command' in config && config.command) {\n // Create a copy and remove McpServerControls properties\n const stdioConfig = { ...config };\n const { StdioClientTransport } = await import(\n '@modelcontextprotocol/sdk/client/stdio.js'\n );\n return {\n transport: new StdioClientTransport(stdioConfig as StdioServerParameters),\n type: 'stdio',\n };\n }\n return { transport: null, type: 'unknown' };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA4BA,eAAsB,cACpB,QACA,WAIC;AAED,MAAI,eAAe,UAAU,OAAO,WAAW;AAC7C,WAAO,EAAE,WAAW,OAAO,WAAW,MAAM,SAAS;AAAA,EACvD;AAEA,MAAI,SAAS,UAAU,OAAO,KAAK;AACjC,UAAM,EAAE,KAAK,GAAG,WAAW,IAAI;AAC/B,UAAM,EAAE,8BAA8B,IAAI,MAAM,OAC9C,oDACF;AACA,WAAO;AAAA,MACL,WAAW,IAAI,8BAA8B,IAAI,IAAI,GAAG,GAAG;AAAA,QACzD,GAAG;AAAA,QACH;AAAA,MACF,CAAC;AAAA,MACD,MAAM;AAAA,IACR;AAAA,EACF;AAEA,MAAI,aAAa,UAAU,OAAO,SAAS;AAEzC,UAAM,cAAc,EAAE,GAAG,OAAO;AAChC,UAAM,EAAE,qBAAqB,IAAI,MAAM,OACrC,2CACF;AACA,WAAO;AAAA,MACL,WAAW,IAAI,qBAAqB,WAAoC;AAAA,MACxE,MAAM;AAAA,IACR;AAAA,EACF;AACA,SAAO,EAAE,WAAW,MAAM,MAAM,UAAU;AAC5C;","names":[]}
@@ -0,0 +1,29 @@
1
+ async function transportFrom(config, sessionId) {
2
+ if ("transport" in config && config.transport) {
3
+ return { transport: config.transport, type: "custom" };
4
+ }
5
+ if ("url" in config && config.url) {
6
+ const { url, ...httpConfig } = config;
7
+ const { StreamableHTTPClientTransport } = await import("@modelcontextprotocol/sdk/client/streamableHttp.js");
8
+ return {
9
+ transport: new StreamableHTTPClientTransport(new URL(url), {
10
+ ...httpConfig,
11
+ sessionId
12
+ }),
13
+ type: "http"
14
+ };
15
+ }
16
+ if ("command" in config && config.command) {
17
+ const stdioConfig = { ...config };
18
+ const { StdioClientTransport } = await import("@modelcontextprotocol/sdk/client/stdio.js");
19
+ return {
20
+ transport: new StdioClientTransport(stdioConfig),
21
+ type: "stdio"
22
+ };
23
+ }
24
+ return { transport: null, type: "unknown" };
25
+ }
26
+ export {
27
+ transportFrom
28
+ };
29
+ //# sourceMappingURL=transport.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/util/transport.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 { McpServerConfig } from '../client/client.js';\nimport type { StdioServerParameters, Transport } from '../client/index.js';\n\n/**\n * Creates an MCP transport instance based on the provided server configuration.\n * It supports creating SSE, Stdio, or using a pre-configured custom transport.\n *\n * @param config The configuration for the MCP server, determining the type of transport to create.\n * @returns A Promise resolving to an object containing the created `Transport` instance\n * (or `null` if configuration is invalid) and a string indicating the `type` of transport.\n * @throws May throw an error if essential MCP SDK components cannot be imported.\n */\nexport async function transportFrom(\n config: McpServerConfig,\n sessionId?: string\n): Promise<{\n transport: Transport | null;\n type: string;\n}> {\n // Handle pre-configured transport first\n if ('transport' in config && config.transport) {\n return { transport: config.transport, type: 'custom' };\n }\n // Handle SSE config\n if ('url' in config && config.url) {\n const { url, ...httpConfig } = config;\n const { StreamableHTTPClientTransport } = await import(\n '@modelcontextprotocol/sdk/client/streamableHttp.js'\n );\n return {\n transport: new StreamableHTTPClientTransport(new URL(url), {\n ...httpConfig,\n sessionId,\n }),\n type: 'http',\n };\n }\n // Handle Stdio config\n if ('command' in config && config.command) {\n // Create a copy and remove McpServerControls properties\n const stdioConfig = { ...config };\n const { StdioClientTransport } = await import(\n '@modelcontextprotocol/sdk/client/stdio.js'\n );\n return {\n transport: new StdioClientTransport(stdioConfig as StdioServerParameters),\n type: 'stdio',\n };\n }\n return { transport: null, type: 'unknown' };\n}\n"],"mappings":"AA4BA,eAAsB,cACpB,QACA,WAIC;AAED,MAAI,eAAe,UAAU,OAAO,WAAW;AAC7C,WAAO,EAAE,WAAW,OAAO,WAAW,MAAM,SAAS;AAAA,EACvD;AAEA,MAAI,SAAS,UAAU,OAAO,KAAK;AACjC,UAAM,EAAE,KAAK,GAAG,WAAW,IAAI;AAC/B,UAAM,EAAE,8BAA8B,IAAI,MAAM,OAC9C,oDACF;AACA,WAAO;AAAA,MACL,WAAW,IAAI,8BAA8B,IAAI,IAAI,GAAG,GAAG;AAAA,QACzD,GAAG;AAAA,QACH;AAAA,MACF,CAAC;AAAA,MACD,MAAM;AAAA,IACR;AAAA,EACF;AAEA,MAAI,aAAa,UAAU,OAAO,SAAS;AAEzC,UAAM,cAAc,EAAE,GAAG,OAAO;AAChC,UAAM,EAAE,qBAAqB,IAAI,MAAM,OACrC,2CACF;AACA,WAAO;AAAA,MACL,WAAW,IAAI,qBAAqB,WAAoC;AAAA,MACxE,MAAM;AAAA,IACR;AAAA,EACF;AACA,SAAO,EAAE,WAAW,MAAM,MAAM,UAAU;AAC5C;","names":[]}
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@genkit-ai/mcp",
3
+ "keywords": [
4
+ "genkit",
5
+ "genkit-plugin",
6
+ "mcp",
7
+ "model-context-protocol",
8
+ "ai",
9
+ "genai",
10
+ "generative-ai"
11
+ ],
12
+ "version": "1.14.1-rc.0",
13
+ "description": "A Genkit plugin that provides interoperability between Genkit and Model Context Protocol (MCP). Both client and server use cases are supported.",
14
+ "main": "./lib/index.js",
15
+ "types": "./lib/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "require": "./lib/index.js",
19
+ "import": "./lib/index.mjs",
20
+ "types": "./lib/index.d.ts",
21
+ "default": "./lib/index.js"
22
+ }
23
+ },
24
+ "type": "commonjs",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "https://github.com/firebase/genkit.git",
28
+ "directory": "js/plugins/mcp"
29
+ },
30
+ "author": "genkit",
31
+ "license": "Apache-2.0",
32
+ "peerDependencies": {
33
+ "@modelcontextprotocol/sdk": "^1.13.0",
34
+ "genkit": "^1.14.1"
35
+ },
36
+ "devDependencies": {
37
+ "get-port": "^5.1.0",
38
+ "express": "^5.1.0",
39
+ "@jest/globals": "^29.7.0",
40
+ "@types/node": "^20.11.16",
41
+ "jest": "^29.7.0",
42
+ "npm-run-all": "^4.1.5",
43
+ "rimraf": "^6.0.1",
44
+ "ts-jest": "^29.1.2",
45
+ "tsup": "^8.3.5",
46
+ "tsx": "^4.19.2",
47
+ "typescript": "^5.3.0"
48
+ },
49
+ "scripts": {
50
+ "check": "tsc",
51
+ "compile": "tsup-node",
52
+ "build:clean": "rimraf ./lib",
53
+ "build": "npm-run-all build:clean check compile",
54
+ "build:watch": "tsup-node --watch",
55
+ "test": "tsx --test ./tests/*_test.ts"
56
+ }
57
+ }