(moduleId: string)
| 25 | * @returns Imported tool module with schema and handler |
| 26 | */ |
| 27 | export async function importToolModule(moduleId: string): Promise<ImportedToolModule> { |
| 28 | const cached = moduleCache.get(moduleId); |
| 29 | if (cached) { |
| 30 | return cached; |
| 31 | } |
| 32 | |
| 33 | const packageRoot = getPackageRoot(); |
| 34 | const modulePath = path.join(packageRoot, 'build', `${moduleId}.js`); |
| 35 | const moduleUrl = pathToFileURL(modulePath).href; |
| 36 | |
| 37 | let mod: Record<string, unknown>; |
| 38 | try { |
| 39 | mod = (await import(moduleUrl)) as Record<string, unknown>; |
| 40 | } catch (err) { |
| 41 | throw new Error(`Failed to import tool module '${moduleId}': ${err}`); |
| 42 | } |
| 43 | |
| 44 | if (!mod.schema || typeof mod.handler !== 'function') { |
| 45 | throw new Error( |
| 46 | `Tool module '${moduleId}' does not export the required shape. ` + |
| 47 | `Expected named exports: export const schema = ... and export const handler = ...`, |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | const result: ImportedToolModule = { |
| 52 | schema: mod.schema as ToolSchemaShape, |
| 53 | handler: mod.handler as ( |
| 54 | params: Record<string, unknown>, |
| 55 | ctx?: ToolHandlerContext, |
| 56 | ) => Promise<unknown>, |
| 57 | }; |
| 58 | |
| 59 | moduleCache.set(moduleId, result); |
| 60 | return result; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Reset module cache (for tests). |
no test coverage detected