@bluehive/sdk-mcp 0.1.0-alpha.2

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 (139) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +188 -0
  3. package/compat.d.mts +56 -0
  4. package/compat.d.mts.map +1 -0
  5. package/compat.d.ts +56 -0
  6. package/compat.d.ts.map +1 -0
  7. package/compat.js +382 -0
  8. package/compat.js.map +1 -0
  9. package/compat.mjs +373 -0
  10. package/compat.mjs.map +1 -0
  11. package/dynamic-tools.d.mts +12 -0
  12. package/dynamic-tools.d.mts.map +1 -0
  13. package/dynamic-tools.d.ts +12 -0
  14. package/dynamic-tools.d.ts.map +1 -0
  15. package/dynamic-tools.js +135 -0
  16. package/dynamic-tools.js.map +1 -0
  17. package/dynamic-tools.mjs +132 -0
  18. package/dynamic-tools.mjs.map +1 -0
  19. package/index.d.mts +3 -0
  20. package/index.d.mts.map +1 -0
  21. package/index.d.ts +3 -0
  22. package/index.d.ts.map +1 -0
  23. package/index.js +86 -0
  24. package/index.js.map +1 -0
  25. package/index.mjs +84 -0
  26. package/index.mjs.map +1 -0
  27. package/options.d.mts +14 -0
  28. package/options.d.mts.map +1 -0
  29. package/options.d.ts +14 -0
  30. package/options.d.ts.map +1 -0
  31. package/options.js +296 -0
  32. package/options.js.map +1 -0
  33. package/options.mjs +290 -0
  34. package/options.mjs.map +1 -0
  35. package/package.json +121 -0
  36. package/server.d.mts +47 -0
  37. package/server.d.mts.map +1 -0
  38. package/server.d.ts +47 -0
  39. package/server.d.ts.map +1 -0
  40. package/server.js +114 -0
  41. package/server.js.map +1 -0
  42. package/server.mjs +101 -0
  43. package/server.mjs.map +1 -0
  44. package/src/compat.ts +478 -0
  45. package/src/dynamic-tools.ts +153 -0
  46. package/src/index.ts +104 -0
  47. package/src/options.ts +319 -0
  48. package/src/server.ts +145 -0
  49. package/src/tools/database/check-health-database.ts +31 -0
  50. package/src/tools/fax/list-providers-fax.ts +31 -0
  51. package/src/tools/fax/retrieve-status-fax.ts +36 -0
  52. package/src/tools/fax/send-fax.ts +76 -0
  53. package/src/tools/health/check-health.ts +31 -0
  54. package/src/tools/index.ts +83 -0
  55. package/src/tools/providers/lookup-providers.ts +49 -0
  56. package/src/tools/types.ts +103 -0
  57. package/src/tools/version/retrieve-version.ts +31 -0
  58. package/src/tools.ts +1 -0
  59. package/src/tsconfig.json +11 -0
  60. package/tools/database/check-health-database.d.mts +45 -0
  61. package/tools/database/check-health-database.d.mts.map +1 -0
  62. package/tools/database/check-health-database.d.ts +45 -0
  63. package/tools/database/check-health-database.d.ts.map +1 -0
  64. package/tools/database/check-health-database.js +27 -0
  65. package/tools/database/check-health-database.js.map +1 -0
  66. package/tools/database/check-health-database.mjs +23 -0
  67. package/tools/database/check-health-database.mjs.map +1 -0
  68. package/tools/fax/list-providers-fax.d.mts +45 -0
  69. package/tools/fax/list-providers-fax.d.mts.map +1 -0
  70. package/tools/fax/list-providers-fax.d.ts +45 -0
  71. package/tools/fax/list-providers-fax.d.ts.map +1 -0
  72. package/tools/fax/list-providers-fax.js +27 -0
  73. package/tools/fax/list-providers-fax.js.map +1 -0
  74. package/tools/fax/list-providers-fax.mjs +23 -0
  75. package/tools/fax/list-providers-fax.mjs.map +1 -0
  76. package/tools/fax/retrieve-status-fax.d.mts +45 -0
  77. package/tools/fax/retrieve-status-fax.d.mts.map +1 -0
  78. package/tools/fax/retrieve-status-fax.d.ts +45 -0
  79. package/tools/fax/retrieve-status-fax.d.ts.map +1 -0
  80. package/tools/fax/retrieve-status-fax.js +32 -0
  81. package/tools/fax/retrieve-status-fax.js.map +1 -0
  82. package/tools/fax/retrieve-status-fax.mjs +28 -0
  83. package/tools/fax/retrieve-status-fax.mjs.map +1 -0
  84. package/tools/fax/send-fax.d.mts +45 -0
  85. package/tools/fax/send-fax.d.mts.map +1 -0
  86. package/tools/fax/send-fax.d.ts +45 -0
  87. package/tools/fax/send-fax.d.ts.map +1 -0
  88. package/tools/fax/send-fax.js +72 -0
  89. package/tools/fax/send-fax.js.map +1 -0
  90. package/tools/fax/send-fax.mjs +68 -0
  91. package/tools/fax/send-fax.mjs.map +1 -0
  92. package/tools/health/check-health.d.mts +45 -0
  93. package/tools/health/check-health.d.mts.map +1 -0
  94. package/tools/health/check-health.d.ts +45 -0
  95. package/tools/health/check-health.d.ts.map +1 -0
  96. package/tools/health/check-health.js +27 -0
  97. package/tools/health/check-health.js.map +1 -0
  98. package/tools/health/check-health.mjs +23 -0
  99. package/tools/health/check-health.mjs.map +1 -0
  100. package/tools/index.d.mts +10 -0
  101. package/tools/index.d.mts.map +1 -0
  102. package/tools/index.d.ts +10 -0
  103. package/tools/index.d.ts.map +1 -0
  104. package/tools/index.js +67 -0
  105. package/tools/index.js.map +1 -0
  106. package/tools/index.mjs +60 -0
  107. package/tools/index.mjs.map +1 -0
  108. package/tools/providers/lookup-providers.d.mts +45 -0
  109. package/tools/providers/lookup-providers.d.mts.map +1 -0
  110. package/tools/providers/lookup-providers.d.ts +45 -0
  111. package/tools/providers/lookup-providers.d.ts.map +1 -0
  112. package/tools/providers/lookup-providers.js +45 -0
  113. package/tools/providers/lookup-providers.js.map +1 -0
  114. package/tools/providers/lookup-providers.mjs +41 -0
  115. package/tools/providers/lookup-providers.mjs.map +1 -0
  116. package/tools/types.d.mts +51 -0
  117. package/tools/types.d.mts.map +1 -0
  118. package/tools/types.d.ts +51 -0
  119. package/tools/types.d.ts.map +1 -0
  120. package/tools/types.js +46 -0
  121. package/tools/types.js.map +1 -0
  122. package/tools/types.mjs +42 -0
  123. package/tools/types.mjs.map +1 -0
  124. package/tools/version/retrieve-version.d.mts +45 -0
  125. package/tools/version/retrieve-version.d.mts.map +1 -0
  126. package/tools/version/retrieve-version.d.ts +45 -0
  127. package/tools/version/retrieve-version.d.ts.map +1 -0
  128. package/tools/version/retrieve-version.js +27 -0
  129. package/tools/version/retrieve-version.js.map +1 -0
  130. package/tools/version/retrieve-version.mjs +23 -0
  131. package/tools/version/retrieve-version.mjs.map +1 -0
  132. package/tools.d.mts +2 -0
  133. package/tools.d.mts.map +1 -0
  134. package/tools.d.ts +2 -0
  135. package/tools.d.ts.map +1 -0
  136. package/tools.js +18 -0
  137. package/tools.js.map +1 -0
  138. package/tools.mjs +2 -0
  139. package/tools.mjs.map +1 -0
package/src/compat.ts ADDED
@@ -0,0 +1,478 @@
1
+ import { Tool } from '@modelcontextprotocol/sdk/types.js';
2
+ import { Endpoint } from './tools';
3
+
4
+ export interface ClientCapabilities {
5
+ topLevelUnions: boolean;
6
+ validJson: boolean;
7
+ refs: boolean;
8
+ unions: boolean;
9
+ formats: boolean;
10
+ toolNameLength: number | undefined;
11
+ }
12
+
13
+ export const defaultClientCapabilities: ClientCapabilities = {
14
+ topLevelUnions: true,
15
+ validJson: true,
16
+ refs: true,
17
+ unions: true,
18
+ formats: true,
19
+ toolNameLength: undefined,
20
+ };
21
+
22
+ export type ClientType = 'openai-agents' | 'claude' | 'claude-code' | 'cursor';
23
+
24
+ // Client presets for compatibility
25
+ // Note that these could change over time as models get better, so this is
26
+ // a best effort.
27
+ export const knownClients: Record<ClientType, ClientCapabilities> = {
28
+ 'openai-agents': {
29
+ topLevelUnions: false,
30
+ validJson: true,
31
+ refs: true,
32
+ unions: true,
33
+ formats: true,
34
+ toolNameLength: undefined,
35
+ },
36
+ claude: {
37
+ topLevelUnions: true,
38
+ validJson: false,
39
+ refs: true,
40
+ unions: true,
41
+ formats: true,
42
+ toolNameLength: undefined,
43
+ },
44
+ 'claude-code': {
45
+ topLevelUnions: false,
46
+ validJson: true,
47
+ refs: true,
48
+ unions: true,
49
+ formats: true,
50
+ toolNameLength: undefined,
51
+ },
52
+ cursor: {
53
+ topLevelUnions: false,
54
+ validJson: true,
55
+ refs: false,
56
+ unions: false,
57
+ formats: false,
58
+ toolNameLength: 50,
59
+ },
60
+ };
61
+
62
+ /**
63
+ * Attempts to parse strings into JSON objects
64
+ */
65
+ export function parseEmbeddedJSON(args: Record<string, unknown>, schema: Record<string, unknown>) {
66
+ let updated = false;
67
+ const newArgs: Record<string, unknown> = Object.assign({}, args);
68
+
69
+ for (const [key, value] of Object.entries(newArgs)) {
70
+ if (typeof value === 'string') {
71
+ try {
72
+ const parsed = JSON.parse(value);
73
+ newArgs[key] = parsed;
74
+ updated = true;
75
+ } catch (e) {
76
+ // Not valid JSON, leave as is
77
+ }
78
+ }
79
+ }
80
+
81
+ if (updated) {
82
+ return newArgs;
83
+ }
84
+
85
+ return args;
86
+ }
87
+
88
+ export type JSONSchema = {
89
+ type?: string;
90
+ properties?: Record<string, JSONSchema>;
91
+ required?: string[];
92
+ anyOf?: JSONSchema[];
93
+ $ref?: string;
94
+ $defs?: Record<string, JSONSchema>;
95
+ [key: string]: any;
96
+ };
97
+
98
+ /**
99
+ * Truncates tool names to the specified length while ensuring uniqueness.
100
+ * If truncation would cause duplicate names, appends a number to make them unique.
101
+ */
102
+ export function truncateToolNames(names: string[], maxLength: number): Map<string, string> {
103
+ if (maxLength <= 0) {
104
+ return new Map();
105
+ }
106
+
107
+ const renameMap = new Map<string, string>();
108
+ const usedNames = new Set<string>();
109
+
110
+ const toTruncate = names.filter((name) => name.length > maxLength);
111
+
112
+ if (toTruncate.length === 0) {
113
+ return renameMap;
114
+ }
115
+
116
+ const willCollide =
117
+ new Set(toTruncate.map((name) => name.slice(0, maxLength - 1))).size < toTruncate.length;
118
+
119
+ if (!willCollide) {
120
+ for (const name of toTruncate) {
121
+ const truncatedName = name.slice(0, maxLength);
122
+ renameMap.set(name, truncatedName);
123
+ }
124
+ } else {
125
+ const baseLength = maxLength - 1;
126
+
127
+ for (const name of toTruncate) {
128
+ const baseName = name.slice(0, baseLength);
129
+ let counter = 1;
130
+
131
+ while (usedNames.has(baseName + counter)) {
132
+ counter++;
133
+ }
134
+
135
+ const finalName = baseName + counter;
136
+ renameMap.set(name, finalName);
137
+ usedNames.add(finalName);
138
+ }
139
+ }
140
+
141
+ return renameMap;
142
+ }
143
+
144
+ /**
145
+ * Removes top-level unions from a tool by splitting it into multiple tools,
146
+ * one for each variant in the union.
147
+ */
148
+ export function removeTopLevelUnions(tool: Tool): Tool[] {
149
+ const inputSchema = tool.inputSchema as JSONSchema;
150
+ const variants = inputSchema.anyOf;
151
+
152
+ if (!variants || !Array.isArray(variants) || variants.length === 0) {
153
+ return [tool];
154
+ }
155
+
156
+ const defs = inputSchema.$defs || {};
157
+
158
+ return variants.map((variant, index) => {
159
+ const variantSchema: JSONSchema = {
160
+ ...inputSchema,
161
+ ...variant,
162
+ type: 'object',
163
+ properties: {
164
+ ...(inputSchema.properties || {}),
165
+ ...(variant.properties || {}),
166
+ },
167
+ };
168
+
169
+ delete variantSchema.anyOf;
170
+
171
+ if (!variantSchema['description']) {
172
+ variantSchema['description'] = tool.description;
173
+ }
174
+
175
+ const usedDefs = findUsedDefs(variant, defs);
176
+ if (Object.keys(usedDefs).length > 0) {
177
+ variantSchema.$defs = usedDefs;
178
+ } else {
179
+ delete variantSchema.$defs;
180
+ }
181
+
182
+ return {
183
+ ...tool,
184
+ name: `${tool.name}_${toSnakeCase(variant['title'] || `variant${index + 1}`)}`,
185
+ description: variant['description'] || tool.description,
186
+ inputSchema: variantSchema,
187
+ } as Tool;
188
+ });
189
+ }
190
+
191
+ function findUsedDefs(
192
+ schema: JSONSchema,
193
+ defs: Record<string, JSONSchema>,
194
+ visited: Set<string> = new Set(),
195
+ ): Record<string, JSONSchema> {
196
+ const usedDefs: Record<string, JSONSchema> = {};
197
+
198
+ if (typeof schema !== 'object' || schema === null) {
199
+ return usedDefs;
200
+ }
201
+
202
+ if (schema.$ref) {
203
+ const refParts = schema.$ref.split('/');
204
+ if (refParts[0] === '#' && refParts[1] === '$defs' && refParts[2]) {
205
+ const defName = refParts[2];
206
+ const def = defs[defName];
207
+ if (def && !visited.has(schema.$ref)) {
208
+ usedDefs[defName] = def;
209
+ visited.add(schema.$ref);
210
+ Object.assign(usedDefs, findUsedDefs(def, defs, visited));
211
+ visited.delete(schema.$ref);
212
+ }
213
+ }
214
+ return usedDefs;
215
+ }
216
+
217
+ for (const key in schema) {
218
+ if (key !== '$defs' && typeof schema[key] === 'object' && schema[key] !== null) {
219
+ Object.assign(usedDefs, findUsedDefs(schema[key] as JSONSchema, defs, visited));
220
+ }
221
+ }
222
+
223
+ return usedDefs;
224
+ }
225
+
226
+ // Export for testing
227
+ export { findUsedDefs };
228
+
229
+ /**
230
+ * Inlines all $refs in a schema, eliminating $defs.
231
+ * If a circular reference is detected, the circular property is removed.
232
+ */
233
+ export function inlineRefs(schema: JSONSchema): JSONSchema {
234
+ if (!schema || typeof schema !== 'object') {
235
+ return schema;
236
+ }
237
+
238
+ const clonedSchema = { ...schema };
239
+ const defs: Record<string, JSONSchema> = schema.$defs || {};
240
+
241
+ delete clonedSchema.$defs;
242
+
243
+ const result = inlineRefsRecursive(clonedSchema, defs, new Set<string>());
244
+ // The top level can never be null
245
+ return result === null ? {} : result;
246
+ }
247
+
248
+ function inlineRefsRecursive(
249
+ schema: JSONSchema,
250
+ defs: Record<string, JSONSchema>,
251
+ refPath: Set<string>,
252
+ ): JSONSchema | null {
253
+ if (!schema || typeof schema !== 'object') {
254
+ return schema;
255
+ }
256
+
257
+ if (Array.isArray(schema)) {
258
+ return schema.map((item) => {
259
+ const processed = inlineRefsRecursive(item, defs, refPath);
260
+ return processed === null ? {} : processed;
261
+ }) as JSONSchema;
262
+ }
263
+
264
+ const result = { ...schema };
265
+
266
+ if ('$ref' in result && typeof result.$ref === 'string') {
267
+ if (result.$ref.startsWith('#/$defs/')) {
268
+ const refName = result.$ref.split('/').pop() as string;
269
+ const def = defs[refName];
270
+
271
+ // If we've already seen this ref in our path, we have a circular reference
272
+ if (refPath.has(result.$ref)) {
273
+ // For circular references, we completely remove the property
274
+ // by returning null. The parent will remove it.
275
+ return null;
276
+ }
277
+
278
+ if (def) {
279
+ const newRefPath = new Set(refPath);
280
+ newRefPath.add(result.$ref);
281
+
282
+ const inlinedDef = inlineRefsRecursive({ ...def }, defs, newRefPath);
283
+
284
+ if (inlinedDef === null) {
285
+ return { ...result };
286
+ }
287
+
288
+ // Merge the inlined definition with the original schema's properties
289
+ // but preserve things like description, etc.
290
+ const { $ref, ...rest } = result;
291
+ return { ...inlinedDef, ...rest };
292
+ }
293
+ }
294
+
295
+ // Keep external refs as-is
296
+ return result;
297
+ }
298
+
299
+ for (const key in result) {
300
+ if (result[key] && typeof result[key] === 'object') {
301
+ const processed = inlineRefsRecursive(result[key] as JSONSchema, defs, refPath);
302
+ if (processed === null) {
303
+ // Remove properties that would cause circular references
304
+ delete result[key];
305
+ } else {
306
+ result[key] = processed;
307
+ }
308
+ }
309
+ }
310
+
311
+ return result;
312
+ }
313
+
314
+ /**
315
+ * Removes anyOf fields from a schema, using only the first variant.
316
+ */
317
+ export function removeAnyOf(schema: JSONSchema): JSONSchema {
318
+ if (!schema || typeof schema !== 'object') {
319
+ return schema;
320
+ }
321
+
322
+ if (Array.isArray(schema)) {
323
+ return schema.map((item) => removeAnyOf(item)) as JSONSchema;
324
+ }
325
+
326
+ const result = { ...schema };
327
+
328
+ if ('anyOf' in result && Array.isArray(result.anyOf) && result.anyOf.length > 0) {
329
+ const firstVariant = result.anyOf[0];
330
+
331
+ if (firstVariant && typeof firstVariant === 'object') {
332
+ // Special handling for properties to ensure deep merge
333
+ if (firstVariant.properties && result.properties) {
334
+ result.properties = {
335
+ ...result.properties,
336
+ ...(firstVariant.properties as Record<string, JSONSchema>),
337
+ };
338
+ } else if (firstVariant.properties) {
339
+ result.properties = { ...firstVariant.properties };
340
+ }
341
+
342
+ for (const key in firstVariant) {
343
+ if (key !== 'properties') {
344
+ result[key] = firstVariant[key];
345
+ }
346
+ }
347
+ }
348
+
349
+ delete result.anyOf;
350
+ }
351
+
352
+ for (const key in result) {
353
+ if (result[key] && typeof result[key] === 'object') {
354
+ result[key] = removeAnyOf(result[key] as JSONSchema);
355
+ }
356
+ }
357
+
358
+ return result;
359
+ }
360
+
361
+ /**
362
+ * Removes format fields from a schema and appends them to the description.
363
+ */
364
+ export function removeFormats(schema: JSONSchema, formatsCapability: boolean): JSONSchema {
365
+ if (formatsCapability) {
366
+ return schema;
367
+ }
368
+
369
+ if (!schema || typeof schema !== 'object') {
370
+ return schema;
371
+ }
372
+
373
+ if (Array.isArray(schema)) {
374
+ return schema.map((item) => removeFormats(item, formatsCapability)) as JSONSchema;
375
+ }
376
+
377
+ const result = { ...schema };
378
+
379
+ if ('format' in result && typeof result['format'] === 'string') {
380
+ const formatStr = `(format: "${result['format']}")`;
381
+
382
+ if ('description' in result && typeof result['description'] === 'string') {
383
+ result['description'] = `${result['description']} ${formatStr}`;
384
+ } else {
385
+ result['description'] = formatStr;
386
+ }
387
+
388
+ delete result['format'];
389
+ }
390
+
391
+ for (const key in result) {
392
+ if (result[key] && typeof result[key] === 'object') {
393
+ result[key] = removeFormats(result[key] as JSONSchema, formatsCapability);
394
+ }
395
+ }
396
+
397
+ return result;
398
+ }
399
+
400
+ /**
401
+ * Applies all compatibility transformations to the endpoints based on the provided capabilities.
402
+ */
403
+ export function applyCompatibilityTransformations(
404
+ endpoints: Endpoint[],
405
+ capabilities: ClientCapabilities,
406
+ ): Endpoint[] {
407
+ let transformedEndpoints = [...endpoints];
408
+
409
+ // Handle top-level unions first as this changes tool names
410
+ if (!capabilities.topLevelUnions) {
411
+ const newEndpoints: Endpoint[] = [];
412
+
413
+ for (const endpoint of transformedEndpoints) {
414
+ const variantTools = removeTopLevelUnions(endpoint.tool);
415
+
416
+ if (variantTools.length === 1) {
417
+ newEndpoints.push(endpoint);
418
+ } else {
419
+ for (const variantTool of variantTools) {
420
+ newEndpoints.push({
421
+ ...endpoint,
422
+ tool: variantTool,
423
+ });
424
+ }
425
+ }
426
+ }
427
+
428
+ transformedEndpoints = newEndpoints;
429
+ }
430
+
431
+ if (capabilities.toolNameLength) {
432
+ const toolNames = transformedEndpoints.map((endpoint) => endpoint.tool.name);
433
+ const renameMap = truncateToolNames(toolNames, capabilities.toolNameLength);
434
+
435
+ transformedEndpoints = transformedEndpoints.map((endpoint) => ({
436
+ ...endpoint,
437
+ tool: {
438
+ ...endpoint.tool,
439
+ name: renameMap.get(endpoint.tool.name) ?? endpoint.tool.name,
440
+ },
441
+ }));
442
+ }
443
+
444
+ if (!capabilities.refs || !capabilities.unions || !capabilities.formats) {
445
+ transformedEndpoints = transformedEndpoints.map((endpoint) => {
446
+ let schema = endpoint.tool.inputSchema as JSONSchema;
447
+
448
+ if (!capabilities.refs) {
449
+ schema = inlineRefs(schema);
450
+ }
451
+
452
+ if (!capabilities.unions) {
453
+ schema = removeAnyOf(schema);
454
+ }
455
+
456
+ if (!capabilities.formats) {
457
+ schema = removeFormats(schema, capabilities.formats);
458
+ }
459
+
460
+ return {
461
+ ...endpoint,
462
+ tool: {
463
+ ...endpoint.tool,
464
+ inputSchema: schema as typeof endpoint.tool.inputSchema,
465
+ },
466
+ };
467
+ });
468
+ }
469
+
470
+ return transformedEndpoints;
471
+ }
472
+
473
+ function toSnakeCase(str: string): string {
474
+ return str
475
+ .replace(/\s+/g, '_')
476
+ .replace(/([a-z])([A-Z])/g, '$1_$2')
477
+ .toLowerCase();
478
+ }
@@ -0,0 +1,153 @@
1
+ import BlueHive from '@bluehive/sdk';
2
+ import { Endpoint, asTextContentResult, ToolCallResult } from './tools/types';
3
+ import { zodToJsonSchema } from 'zod-to-json-schema';
4
+ import { z } from 'zod';
5
+ import { Cabidela } from '@cloudflare/cabidela';
6
+
7
+ function zodToInputSchema(schema: z.ZodSchema) {
8
+ return {
9
+ type: 'object' as const,
10
+ ...(zodToJsonSchema(schema) as any),
11
+ };
12
+ }
13
+
14
+ /**
15
+ * A list of tools that expose all the endpoints in the API dynamically.
16
+ *
17
+ * Instead of exposing every endpoint as it's own tool, which uses up too many tokens for LLMs to use at once,
18
+ * we expose a single tool that can be used to search for endpoints by name, resource, operation, or tag, and then
19
+ * a generic endpoint that can be used to invoke any endpoint with the provided arguments.
20
+ *
21
+ * @param endpoints - The endpoints to include in the list.
22
+ */
23
+ export function dynamicTools(endpoints: Endpoint[]): Endpoint[] {
24
+ const listEndpointsSchema = z.object({
25
+ search_query: z
26
+ .string()
27
+ .optional()
28
+ .describe(
29
+ 'An optional search query to filter the endpoints by. Provide a partial name, resource, operation, or tag to filter the endpoints returned.',
30
+ ),
31
+ });
32
+
33
+ const listEndpointsTool = {
34
+ metadata: {
35
+ resource: 'dynamic_tools',
36
+ operation: 'read' as const,
37
+ tags: [],
38
+ },
39
+ tool: {
40
+ name: 'list_api_endpoints',
41
+ description: 'List or search for all endpoints in the BlueHive TypeScript API',
42
+ inputSchema: zodToInputSchema(listEndpointsSchema),
43
+ },
44
+ handler: async (client: BlueHive, args: Record<string, unknown> | undefined): Promise<ToolCallResult> => {
45
+ const query = args && listEndpointsSchema.parse(args).search_query?.trim();
46
+
47
+ const filteredEndpoints =
48
+ query && query.length > 0 ?
49
+ endpoints.filter((endpoint) => {
50
+ const fieldsToMatch = [
51
+ endpoint.tool.name,
52
+ endpoint.tool.description,
53
+ endpoint.metadata.resource,
54
+ endpoint.metadata.operation,
55
+ ...endpoint.metadata.tags,
56
+ ];
57
+ return fieldsToMatch.some((field) => field && field.toLowerCase().includes(query.toLowerCase()));
58
+ })
59
+ : endpoints;
60
+
61
+ return asTextContentResult({
62
+ tools: filteredEndpoints.map(({ tool, metadata }) => ({
63
+ name: tool.name,
64
+ description: tool.description,
65
+ resource: metadata.resource,
66
+ operation: metadata.operation,
67
+ tags: metadata.tags,
68
+ })),
69
+ });
70
+ },
71
+ };
72
+
73
+ const getEndpointSchema = z.object({
74
+ endpoint: z.string().describe('The name of the endpoint to get the schema for.'),
75
+ });
76
+ const getEndpointTool = {
77
+ metadata: {
78
+ resource: 'dynamic_tools',
79
+ operation: 'read' as const,
80
+ tags: [],
81
+ },
82
+ tool: {
83
+ name: 'get_api_endpoint_schema',
84
+ description:
85
+ 'Get the schema for an endpoint in the BlueHive TypeScript API. You can use the schema returned by this tool to invoke an endpoint with the `invoke_api_endpoint` tool.',
86
+ inputSchema: zodToInputSchema(getEndpointSchema),
87
+ },
88
+ handler: async (client: BlueHive, args: Record<string, unknown> | undefined) => {
89
+ if (!args) {
90
+ throw new Error('No endpoint provided');
91
+ }
92
+ const endpointName = getEndpointSchema.parse(args).endpoint;
93
+
94
+ const endpoint = endpoints.find((e) => e.tool.name === endpointName);
95
+ if (!endpoint) {
96
+ throw new Error(`Endpoint ${endpointName} not found`);
97
+ }
98
+ return asTextContentResult(endpoint.tool);
99
+ },
100
+ };
101
+
102
+ const invokeEndpointSchema = z.object({
103
+ endpoint_name: z.string().describe('The name of the endpoint to invoke.'),
104
+ args: z
105
+ .record(z.string(), z.any())
106
+ .describe(
107
+ 'The arguments to pass to the endpoint. This must match the schema returned by the `get_api_endpoint_schema` tool.',
108
+ ),
109
+ });
110
+
111
+ const invokeEndpointTool = {
112
+ metadata: {
113
+ resource: 'dynamic_tools',
114
+ operation: 'write' as const,
115
+ tags: [],
116
+ },
117
+ tool: {
118
+ name: 'invoke_api_endpoint',
119
+ description:
120
+ 'Invoke an endpoint in the BlueHive TypeScript API. Note: use the `list_api_endpoints` tool to get the list of endpoints and `get_api_endpoint_schema` tool to get the schema for an endpoint.',
121
+ inputSchema: zodToInputSchema(invokeEndpointSchema),
122
+ },
123
+ handler: async (client: BlueHive, args: Record<string, unknown> | undefined): Promise<ToolCallResult> => {
124
+ if (!args) {
125
+ throw new Error('No endpoint provided');
126
+ }
127
+ const { success, data, error } = invokeEndpointSchema.safeParse(args);
128
+ if (!success) {
129
+ throw new Error(`Invalid arguments for endpoint. ${error?.format()}`);
130
+ }
131
+ const { endpoint_name, args: endpointArgs } = data;
132
+
133
+ const endpoint = endpoints.find((e) => e.tool.name === endpoint_name);
134
+ if (!endpoint) {
135
+ throw new Error(
136
+ `Endpoint ${endpoint_name} not found. Use the \`list_api_endpoints\` tool to get the list of available endpoints.`,
137
+ );
138
+ }
139
+
140
+ try {
141
+ // Try to validate the arguments for a better error message
142
+ const cabidela = new Cabidela(endpoint.tool.inputSchema, { fullErrors: true });
143
+ cabidela.validate(endpointArgs);
144
+ } catch (error) {
145
+ throw new Error(`Invalid arguments for endpoint ${endpoint_name}:\n${error}`);
146
+ }
147
+
148
+ return await endpoint.handler(client, endpointArgs);
149
+ },
150
+ };
151
+
152
+ return [getEndpointTool, listEndpointsTool, invokeEndpointTool];
153
+ }