* Polls the Document Engine API until it's ready * @returns Promise that resolves with the client when ready
()
| 38 | */ |
| 39 | async function waitForDocumentEngine(): Promise<DocumentEngineClient> { |
| 40 | const env = getEnvironment(); |
| 41 | const maxRetries = env.DOCUMENT_ENGINE_POLL_MAX_RETRIES; |
| 42 | const retryDelay = env.DOCUMENT_ENGINE_POLL_RETRY_DELAY; |
| 43 | let attempts = 0; |
| 44 | |
| 45 | while (attempts < maxRetries) { |
| 46 | try { |
| 47 | logger.info( |
| 48 | null, |
| 49 | `Attempting to connect to Document Engine (attempt ${attempts + 1}/${maxRetries})` |
| 50 | ); |
| 51 | const client = await getDocumentEngineClient(); |
| 52 | |
| 53 | // Test the connection with a health check |
| 54 | await client.get('/healthcheck'); |
| 55 | |
| 56 | logger.info(null, 'Document Engine is ready! Connection established successfully.'); |
| 57 | return client; |
| 58 | } catch (error) { |
| 59 | attempts++; |
| 60 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 61 | |
| 62 | if (attempts >= maxRetries) { |
| 63 | logger.error( |
| 64 | null, |
| 65 | `Failed to connect to Document Engine after ${maxRetries} attempts. Last error: ${errorMessage}` |
| 66 | ); |
| 67 | throw new Error( |
| 68 | `Document Engine connection failed after ${maxRetries} attempts: ${errorMessage}` |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | logger.warning( |
| 73 | null, |
| 74 | `Document Engine not ready yet (attempt ${attempts}/${maxRetries}): ${errorMessage}. Retrying in ${retryDelay}ms...` |
| 75 | ); |
| 76 | await new Promise(resolve => setTimeout(resolve, retryDelay)); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | throw new Error('Unexpected error in waitForDocumentEngine'); |
| 81 | } |
| 82 | |
| 83 | const client = await waitForDocumentEngine(); |
| 84 | |
| 85 | function createMCPServer(): McpServer { |
no test coverage detected