MCPcopy Create free account
hub / github.com/docsagent/docsagent / findPort

Method findPort

src/index.ts:60–82  ·  view source on GitHub ↗

* Scans for an existing DocsAgent service or an available port.

()

Source from the content-addressed store, hash-verified

58 * Scans for an existing DocsAgent service or an available port.
59 */
60 private async findPort(): Promise<number> {
61 const START_PORT = 18688;
62 // We scan a reasonable range first for an existing service
63 for (let p = START_PORT; p < START_PORT + 20; p++) {
64 try {
65 const controller = new AbortController();
66 const timeoutId = setTimeout(() => controller.abort(), 100);
67 const res = await fetch(`http://localhost:${p}/status`, { signal: controller.signal });
68 clearTimeout(timeoutId);
69 if (res.ok) return p;
70 } catch {
71 // Not responding or not DocsAgent
72 }
73 }
74
75 // If no existing service, find the first available port
76 let p = START_PORT;
77 while (p < 65535) {
78 if (await this.isPortAvailable(p)) return p;
79 p++;
80 }
81 throw new Error("No available ports found");
82 }
83
84 /**
85 * Starts the C++ background process and waits for the HTTP interface to be ready.

Callers 1

startEngineMethod · 0.95

Calls 1

isPortAvailableMethod · 0.95

Tested by

no test coverage detected