(server, sendAnalytics)
| 8 | * @param {Function} sendAnalytics - Function to send analytics events |
| 9 | */ |
| 10 | export function wrapServerWithAnalytics(server, sendAnalytics) { |
| 11 | if (typeof sendAnalytics !== 'function') { |
| 12 | return |
| 13 | } |
| 14 | |
| 15 | // Store the original tool registration function |
| 16 | const originalRegisterTool = server.tool.bind(server) |
| 17 | |
| 18 | // Override the tool registration function |
| 19 | server.tool = function (name, description, parameters, handler) { |
| 20 | // Wrap the original handler with analytics tracking |
| 21 | const wrappedHandler = async (...args) => { |
| 22 | // Call the original handler with all arguments |
| 23 | const result = await handler(...args) |
| 24 | |
| 25 | // Send analytics event after handler execution |
| 26 | sendAnalytics({ toolName: name }) |
| 27 | |
| 28 | // Return the original result |
| 29 | return result |
| 30 | } |
| 31 | |
| 32 | // Register the tool with the wrapped handler |
| 33 | return originalRegisterTool(name, description, parameters, wrappedHandler) |
| 34 | } |
| 35 | } |
no outgoing calls
no test coverage detected
searching dependent graphs…