@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,165 @@
1
+ /**
2
+ * Copyright 2024 Google LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { Client } from '@modelcontextprotocol/sdk/client/index.js';
18
+ import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
19
+ import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
20
+ import * as assert from 'assert';
21
+ import express from 'express';
22
+ import { Genkit, genkit, z } from 'genkit';
23
+ import { logger } from 'genkit/logging';
24
+ import getPort from 'get-port';
25
+ import * as http from 'http';
26
+ import { afterEach, beforeEach, describe, it } from 'node:test';
27
+ import { createMcpServer } from '../src/index.js';
28
+ import { GenkitMcpServer } from '../src/server.js';
29
+ import { defineEchoModel } from './fakes.js';
30
+
31
+ logger.setLogLevel('debug');
32
+
33
+ describe('createMcpServer', async () => {
34
+ let ai: Genkit;
35
+ let mcpServer: GenkitMcpServer;
36
+ let mcpHttpServer: http.Server;
37
+ let port: number;
38
+ let client: Client;
39
+
40
+ beforeEach(async () => {
41
+ ai = genkit({});
42
+ defineEchoModel(ai);
43
+
44
+ ai.definePrompt({
45
+ name: 'testPrompt',
46
+ model: 'echoModel',
47
+ prompt: 'prompt says: {{input}}',
48
+ });
49
+
50
+ ai.defineTool(
51
+ {
52
+ name: 'testTool',
53
+ description: 'test tool',
54
+ inputSchema: z.object({ foo: z.string() }),
55
+ },
56
+ async (input) => `yep ${JSON.stringify(input)}`
57
+ );
58
+
59
+ mcpServer = createMcpServer(ai, { name: 'test-server', version: '0.0.1' });
60
+ await mcpServer.setup();
61
+
62
+ const app = express();
63
+ let transport: SSEServerTransport | null = null;
64
+
65
+ app.get('/sse', (req, res) => {
66
+ transport = new SSEServerTransport('/messages', res);
67
+ mcpServer.server!.connect(transport);
68
+ });
69
+
70
+ app.post('/messages', (req, res) => {
71
+ if (transport) {
72
+ transport.handlePostMessage(req, res);
73
+ }
74
+ });
75
+
76
+ port = await getPort();
77
+ let serverResolver;
78
+ const serverPromise = new Promise((r) => {
79
+ serverResolver = r;
80
+ });
81
+ mcpHttpServer = app.listen(port, () => {
82
+ console.log(`MCP server listening on http://localhost:${port}`);
83
+ serverResolver();
84
+ });
85
+ await serverPromise; // wait for server to start up
86
+
87
+ client = new Client({ name: 'test', version: '0.0.1' });
88
+ await client.connect(
89
+ new SSEClientTransport(new URL(`http://localhost:${port}/sse`))
90
+ );
91
+ });
92
+
93
+ afterEach(async () => {
94
+ mcpHttpServer.close();
95
+ await mcpServer.server?.close();
96
+ await client?.close();
97
+ });
98
+
99
+ describe('tools', () => {
100
+ it('should list tools', async () => {
101
+ const r = await client.listTools();
102
+
103
+ assert.deepStrictEqual(r.tools, [
104
+ {
105
+ description: 'test tool',
106
+ inputSchema: {
107
+ $schema: 'http://json-schema.org/draft-07/schema#',
108
+ additionalProperties: true,
109
+ properties: {
110
+ foo: {
111
+ type: 'string',
112
+ },
113
+ },
114
+ required: ['foo'],
115
+ type: 'object',
116
+ },
117
+ name: 'testTool',
118
+ },
119
+ ]);
120
+ });
121
+
122
+ it('should call the tool', async () => {
123
+ const response = await client.callTool({
124
+ name: 'testTool',
125
+ arguments: {
126
+ foo: 'bar',
127
+ },
128
+ });
129
+ assert.deepStrictEqual(response, {
130
+ content: [
131
+ {
132
+ text: 'yep {"foo":"bar"}',
133
+ type: 'text',
134
+ },
135
+ ],
136
+ });
137
+ });
138
+ });
139
+
140
+ describe('prompts', () => {
141
+ it('should list prompts', async () => {
142
+ const r = await client.listPrompts();
143
+ assert.deepStrictEqual(r.prompts, [{ name: 'testPrompt' }]);
144
+ });
145
+
146
+ it('should render prompt', async () => {
147
+ const prompt = await client.getPrompt({
148
+ name: 'testPrompt',
149
+ arguments: {
150
+ input: 'hello',
151
+ },
152
+ });
153
+
154
+ assert.deepStrictEqual(prompt.messages, [
155
+ {
156
+ content: {
157
+ text: 'prompt says: hello',
158
+ type: 'text',
159
+ },
160
+ role: 'user',
161
+ },
162
+ ]);
163
+ });
164
+ });
165
+ });