@agimon-ai/log-sink-mcp 0.6.0 → 0.7.0

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.
package/dist/cli.cjs CHANGED
@@ -582,8 +582,8 @@ window.addEventListener('beforeunload', () => {
582
582
  color: #555;
583
583
  margin-top: 0.25rem;
584
584
  }
585
- `)})]}),(0,h.jsx)(`body`,{children:t})]})}function ge(e){let n=new d.Hono,r=e.get(t.m.LogQueryService);return n.get(`/`,async e=>{try{let t=await r.getActiveServices(),n=await r.getStatistics(),i=await r.filterByLevel([g.INFO,g.WARN,g.ERROR,g.FATAL],100),a={totalLogs:n.reduce((e,t)=>e+t.count,0),errors:n.filter(e=>e.level===`error`||e.level===`fatal`).reduce((e,t)=>e+t.count,0),services:new Set(n.map(e=>e.service)).size};return e.html((0,h.jsx)(R,{title:`Log Dashboard`,children:(0,h.jsx)(he,{initialLogs:i,services:t,stats:a})}))}catch(t){return console.error(`Failed to render dashboard:`,t),e.html((0,h.jsx)(R,{title:`Log Dashboard - Error`,children:(0,h.jsxs)(`div`,{style:`padding: 2rem; text-align: center;`,children:[(0,h.jsx)(`h1`,{children:`Failed to load dashboard`}),(0,h.jsx)(`p`,{children:t instanceof Error?t.message:String(t)}),(0,h.jsx)(`a`,{href:`/`,style:`color: #2196f3; text-decoration: underline;`,children:`Retry`})]})}),500)}}),n}const z=5*1024*1024,B=2e3,V=8e3,_e=n.z.object({timestamp:n.z.string().datetime().optional(),level:n.z.enum([`trace`,`debug`,`info`,`warn`,`error`,`fatal`]),message:n.z.string().min(1).max(V),traceId:n.z.string().regex(/^[0-9a-f]{32}$/i,`traceId must be 32 hex characters`).optional(),spanId:n.z.string().regex(/^[0-9a-f]{16}$/i,`spanId must be 16 hex characters`).optional(),parentSpanId:n.z.string().regex(/^[0-9a-f]{16}$/i,`parentSpanId must be 16 hex characters`).optional(),service:n.z.string().min(1).max(200),hostname:n.z.string().max(255).optional(),pid:n.z.number().int().nonnegative().optional(),metadata:n.z.record(n.z.string(),n.z.unknown()).optional(),errorType:n.z.string().max(255).optional(),errorMessage:n.z.string().max(8e3).optional(),errorStack:n.z.string().max(32e3).optional()}),ve=n.z.object({logs:n.z.array(_e).min(1).max(1e3)});function H(e){if(!e||!/^\d+$/.test(e))throw Error(`OTLP timestamp must be a numeric unix-nanoseconds string`);let t=Number(BigInt(e)/BigInt(1e6));return new Date(t)}function U(e,t){return typeof e==`string`&&RegExp(`^[0-9a-f]{${t}}$`,`i`).test(e)}function W(e){if(!e)return null;let t=Number(e);return!Number.isFinite(t)||t<=0?null:t>z?`Request body exceeds ${z} bytes`:null}async function G(e){try{return await e.req.json()}catch(e){throw Error(`Invalid JSON body: ${e instanceof Error?e.message:String(e)}`)}}function ye(e){if(!Array.isArray(e.resourceSpans)||e.resourceSpans.length===0)throw Error(`Invalid OTLP trace request: missing resourceSpans`);let t=0;for(let n of e.resourceSpans){if(!Array.isArray(n.scopeSpans))throw Error(`Invalid OTLP trace request: scopeSpans must be an array`);for(let e of n.scopeSpans){if(!Array.isArray(e.spans))throw Error(`Invalid OTLP trace request: spans must be an array`);for(let n of e.spans){if(t+=1,t>B)throw Error(`OTLP trace request exceeds ${B} spans`);if(!U(n.traceId,32))throw Error(`Invalid OTLP trace request: traceId must be 32 hex characters`);if(!U(n.spanId,16))throw Error(`Invalid OTLP trace request: spanId must be 16 hex characters`);if(n.parentSpanId&&!U(n.parentSpanId,16))throw Error(`Invalid OTLP trace request: parentSpanId must be 16 hex characters`);if(!n.name||n.name.length>V)throw Error(`Invalid OTLP trace request: span name is missing or too long`);H(n.startTimeUnixNano)}}}}function be(e){if(!Array.isArray(e.resourceLogs)||e.resourceLogs.length===0)throw Error(`Invalid OTLP logs request: missing resourceLogs`);let t=0;for(let n of e.resourceLogs){if(!Array.isArray(n.scopeLogs))throw Error(`Invalid OTLP logs request: scopeLogs must be an array`);for(let e of n.scopeLogs){if(!Array.isArray(e.logRecords))throw Error(`Invalid OTLP logs request: logRecords must be an array`);for(let n of e.logRecords){if(t+=1,t>B)throw Error(`OTLP logs request exceeds ${B} log records`);if(n.traceId&&!U(n.traceId,32))throw Error(`Invalid OTLP logs request: traceId must be 32 hex characters`);if(n.spanId&&!U(n.spanId,16))throw Error(`Invalid OTLP logs request: spanId must be 16 hex characters`);if(n.severityText&&n.severityText.length>32)throw Error(`Invalid OTLP logs request: severityText is too long`);H(n.timeUnixNano)}}}}function xe(e){return(e.resource?.attributes?.find(e=>e.key===`service.name`))?.value?.stringValue||`unknown-service`}function K(e,t){let n=e?.find(e=>e.key===t);if(!n)return;let{value:r}=n;if(r.stringValue!==void 0)return r.stringValue;if(r.intValue!==void 0)return r.intValue;if(r.doubleValue!==void 0)return String(r.doubleValue);if(r.boolValue!==void 0)return String(r.boolValue)}function Se(e){return(e.resource?.attributes?.find(e=>e.key===`service.name`))?.value?.stringValue||`unknown-service`}function Ce(e){return e===void 0?`info`:e<=4?`trace`:e<=8?`debug`:e<=12?`info`:e<=16?`warn`:e<=20?`error`:`fatal`}function we(e,t){let n=H(e.timeUnixNano),r=e.severityText?.toLowerCase()||Ce(e.severityNumber),i=``;if(e.body?.stringValue)i=e.body.stringValue;else if(e.body?.kvlistValue){let t={};for(let n of e.body.kvlistValue.values){let e=n.value.stringValue??n.value.intValue??n.value.doubleValue??n.value.boolValue;t[n.key]=e}i=JSON.stringify(t)}let a={},o=null,s=null,c=null;if(e.attributes)for(let t of e.attributes){let e=t.value.stringValue??t.value.intValue??t.value.doubleValue??t.value.boolValue;t.key===`exception.type`?o=String(e):t.key===`exception.message`?s=String(e):t.key===`exception.stacktrace`?c=String(e):a[t.key]=e}return{timestamp:n,level:r,message:i||`[LOG] ${t}`,traceId:e.traceId??null,spanId:e.spanId??null,parentSpanId:null,service:t,hostname:null,pid:null,metadata:Object.keys(a).length>0?a:null,errorType:o,errorMessage:s,errorStack:c}}function Te(e,t){let n=H(e.startTimeUnixNano),r=e.status?.code,i=`info`;r===2?i=`error`:r===1&&(i=`info`);let a=K(e.attributes,`exception.type`),o=K(e.attributes,`exception.message`),s=K(e.attributes,`exception.stacktrace`),c={};if(e.attributes)for(let t of e.attributes){let e=t.value.stringValue??t.value.intValue??t.value.doubleValue??t.value.boolValue;c[t.key]=e}return c.spanName=e.name,c.spanKind=e.kind,e.status?.message&&(c.statusMessage=e.status.message),{timestamp:n,level:i,message:`[SPAN] ${e.name}`,traceId:e.traceId,spanId:e.spanId,parentSpanId:e.parentSpanId??null,service:t,hostname:null,pid:null,metadata:c,errorType:a??null,errorMessage:o??null,errorStack:s??null}}function Ee(e){let n=new d.Hono,r=e.get(t.m.LogStorageService);n.use(`*`,(0,p.cors)({origin:e=>e??null,credentials:!0,allowMethods:[`GET`,`POST`,`OPTIONS`],allowHeaders:[`Content-Type`,`Accept`,`Authorization`]})),n.get(`/health`,async e=>{try{return e.json({status:`healthy`,service:`log-sink-mcp-http`,serviceName:`log-sink-mcp-http`,timestamp:new Date().toISOString()})}catch(t){return e.json({status:`unhealthy`,error:t instanceof Error?t.message:String(t)},503)}});let i=(0,f.rateLimiter)({windowMs:60*1e3,limit:100,standardHeaders:`draft-6`,keyGenerator:e=>e.req.header(`x-forwarded-for`)||e.req.header(`x-real-ip`)||`unknown`});n.post(`/logs`,i,async e=>{try{let t=W(e.req.header(`content-length`));if(t)return e.json({success:!1,error:t},413);let n=await G(e),i=ve.safeParse(n);if(!i.success)return e.json({success:!1,error:`Validation failed`,details:i.error.issues},400);let{logs:a}=i.data,o=a.map(e=>({...e,timestamp:e.timestamp?new Date(e.timestamp):new Date})),s=await r.insertBatch(o);return e.json({success:!0,count:s.length,message:`Successfully inserted ${s.length} logs`},201)}catch(t){let n=t instanceof Error?t.message:String(t);return n.startsWith(`Invalid JSON body`)?e.json({success:!1,error:n},400):(console.error(`Failed to insert logs:`,t),e.json({success:!1,error:`Failed to insert logs`},500))}}),n.post(`/v1/traces`,i,async e=>{try{let t=W(e.req.header(`content-length`));if(t)return e.json({success:!1,error:t},413);let n=await G(e);ye(n);let i=[];for(let e of n.resourceSpans){let t=xe(e);for(let n of e.scopeSpans)for(let e of n.spans){let n=Te(e,t);i.push(n)}}let a=await r.insertBatch(i);return e.json({success:!0,count:a.length,message:`Successfully inserted ${a.length} trace spans as logs`},201)}catch(t){let n=t instanceof Error?t.message:String(t);return n.startsWith(`Invalid JSON body`)||n.startsWith(`Invalid OTLP`)||n.startsWith(`OTLP`)?e.json({success:!1,error:n},400):(console.error(`Failed to insert OTLP traces:`,t),e.json({success:!1,error:`Failed to insert OTLP traces`},500))}}),n.post(`/v1/logs`,i,async e=>{try{let t=W(e.req.header(`content-length`));if(t)return e.json({success:!1,error:t},413);let n=await G(e);be(n);let i=[];for(let e of n.resourceLogs){let t=Se(e);for(let n of e.scopeLogs)for(let e of n.logRecords){let n=we(e,t);i.push(n)}}let a=await r.insertBatch(i);return e.json({success:!0,count:a.length,message:`Successfully inserted ${a.length} OTLP logs`},201)}catch(t){let n=t instanceof Error?t.message:String(t);return n.startsWith(`Invalid JSON body`)||n.startsWith(`Invalid OTLP`)||n.startsWith(`OTLP`)?e.json({success:!1,error:n},400):(console.error(`Failed to insert OTLP logs:`,t),e.json({success:!1,error:`Failed to insert OTLP logs`},500))}});let a=oe(e);n.route(`/api`,a);let o=ge(e);return n.route(`/`,o),n}function q(e,t){if(e===void 0||e===``)return t;let n=Number.parseInt(e,10);if(!Number.isFinite(n)||n<=0)throw Error(`Invalid database retention value: ${e}`);return n}function J(e={}){let t=q(e.dbMaxBytes??process.env.LOG_SINK_DB_MAX_BYTES,268435456),n=q(e.dbTargetBytes??process.env.LOG_SINK_DB_TARGET_BYTES,Math.floor(t*.9));return{maxBytes:t,targetBytes:Math.min(n,t),intervalMs:q(e.dbRetentionIntervalMs??process.env.LOG_SINK_DB_RETENTION_INTERVAL_MS,3e5),batchSize:q(e.dbRetentionBatchSize??process.env.LOG_SINK_DB_RETENTION_BATCH_SIZE,1e3)}}const De=String(s.DEFAULT_PORT_RANGE.min),Oe=t.f(),ke=new l.Command(`http-serve`).description(`Start HTTP server for log ingestion with configurable port, database path, and in-memory mode`).option(`-p, --port <port>`,`Port to listen on`,De).option(`--db-path <path>`,`Path to SQLite database file`,Oe).option(`--in-memory`,`Use in-memory database (for testing)`,!1).option(`--db-max-bytes <bytes>`,`Maximum SQLite database size before trimming`).option(`--db-target-bytes <bytes>`,`Target SQLite database size after trimming`).option(`--db-retention-interval-ms <ms>`,`How often to check database size`,`300000`).option(`--db-retention-batch-size <count>`,`Number of oldest log rows to delete per trim batch`,`1000`).option(`--registry-path <path>`,`Custom registry path or directory for service discovery`).action(async e=>{let n;try{let r=Number.parseInt(e.port,10),i={min:s.DEFAULT_PORT_RANGE.min,max:Math.max(s.DEFAULT_PORT_RANGE.max??4999,r)},a=process.env.NODE_ENV||`development`,o=t.p(process.cwd()),l=`log-sink-mcp-http`;e.registryPath&&(process.env.PORT_REGISTRY_PATH=e.registryPath,process.env.PROCESS_REGISTRY_PATH=(0,c.resolveSiblingRegistryPath)(e.registryPath,`processes.json`)??process.env.PROCESS_REGISTRY_PATH);let d=new s.PortRegistryService(process.env.PORT_REGISTRY_PATH),f=t.u(),p=f.get(t.m.LogStorageService),m=f.get(t.m.LogRetentionService);await p.initializeDatabase(e.dbPath,e.inMemory);let h=m.startSizeRetentionMonitor(J({dbMaxBytes:e.dbMaxBytes,dbTargetBytes:e.dbTargetBytes,dbRetentionIntervalMs:e.dbRetentionIntervalMs,dbRetentionBatchSize:e.dbRetentionBatchSize})),g=Ee(f),_=await d.reservePort({repositoryPath:o,serviceName:l,serviceType:`tool`,environment:a,preferredPort:r,pid:process.pid,host:`127.0.0.1`,force:!0,portRange:i,metadata:{healthCheckUrl:`http://localhost:${r}/health`,dbPath:e.dbPath}});if(!_.success||!_.record)throw Error(_.error||`Failed to reserve service in global registry`);let v=_.record.port;if(v!==r){let t=await d.reservePort({repositoryPath:o,serviceName:l,serviceType:`tool`,environment:a,preferredPort:v,pid:process.pid,host:`127.0.0.1`,force:!0,portRange:i,metadata:{healthCheckUrl:`http://localhost:${v}/health`,dbPath:e.dbPath}});if(!t.success||!t.record)throw Error(t.error||`Failed to update service metadata in registry`);_=t}if(!_.record)throw Error(`Port reservation lost before startup`);let y=_.record.port;n=await(0,c.createProcessLease)({repositoryPath:o,serviceName:l,serviceType:`tool`,environment:a,pid:process.pid,port:y,host:`127.0.0.1`,command:process.argv[1],args:process.argv.slice(2),metadata:{dbPath:e.dbPath,transport:`http`}});let b;try{b=(0,u.serve)({fetch:g.fetch,port:y})}catch(e){try{await n.release({kill:!1})}catch{}throw e}let x=async()=>{try{b.close();try{await n?.release({kill:!1})}catch{}h(),await p.disconnect(),process.exit(0)}catch{process.exit(1)}};process.on(`SIGINT`,()=>x()),process.on(`SIGTERM`,()=>x())}catch{try{await n?.release({kill:!1})}catch{}process.exit(1)}}),Ae=t.f(),Y=new Set(Object.values(g)),je=new Set([`level`,`service`,`both`]),Me={stdout:e=>console.log(e),stderr:e=>console.error(e)};function X(e){let t=Number.parseInt(e,10);if(!Number.isInteger(t)||t<=0)throw new l.InvalidArgumentError(`Expected a positive integer.`);return t}function Ne(e){if(!je.has(e))throw new l.InvalidArgumentError(`Expected one of: 'level', 'service', 'both'.`);return e}function Z(e){if(!(!e||e.length===0))return e.map(e=>{if(!Y.has(e))throw new l.InvalidArgumentError(`Invalid log level '${e}'. Expected one of: ${Array.from(Y).join(`, `)}`);return e})}function Pe(e){return e.content.map(e=>e.type===`text`?e.text:JSON.stringify(e)).join(`
586
- `)}function Q(e){return e.option(`--db-path <path>`,`Path to SQLite database file`,Ae).option(`--in-memory`,`Use in-memory database`,!1)}async function Fe(e,n){let r=t.u(),i=r.get(t.m.LogStorageService);await i.initializeDatabase(e.dbPath,e.inMemory);try{return await n(r)}finally{await i.disconnect()}}async function Ie(e,t,n,r=Me){try{let i=await Fe(e,async e=>t(e).execute(n)),a=Pe(i);return i.isError?(r.stderr(a),1):(r.stdout(a),0)}catch(e){return r.stderr(`Error executing logs command: ${e instanceof Error?e.message:String(e)}`),1}}async function $(e,t,n){let r=await Ie(e,t,n);r!==0&&process.exit(r)}function Le(e,t){if(!(!e&&!t)){if(!e||!t)throw new l.InvalidArgumentError(`Provide both --start-time and --end-time together.`);return{start:e,end:t}}}function Re(){let e=new l.Command(`logs`).description(`Run log analysis commands that mirror MCP tool capabilities`),n=Q(new l.Command(`query`).alias(`query-logs`).description(`Query and filter log entries by level, trace ID, service, or time range`).option(`--level <level...>`,`Log level(s) to filter by`).option(`--trace-id <traceId>`,`Trace ID to filter by`).option(`--service <service...>`,`Service name(s) to filter by`).option(`--start-time <iso8601>`,`Start time for log range (ISO 8601)`).option(`--end-time <iso8601>`,`End time for log range (ISO 8601)`).option(`--limit <number>`,`Maximum number of log entries to return`,X).action(async e=>{await $(e,e=>new t.i(e),{level:Z(e.level),traceId:e.traceId,service:e.service,startTime:e.startTime,endTime:e.endTime,limit:e.limit})})),r=Q(new l.Command(`search`).alias(`search-logs`).description(`Search log entries with full-text search and optional filters`).argument(`<search-query>`,`FTS5 search query`).option(`--mode <mode>`,`Search strategy: fts, semantic, or hybrid`,`hybrid`).option(`--service <service...>`,`Service name(s) to filter by`).option(`--level <level...>`,`Log level(s) to filter by`).option(`--start-time <iso8601>`,`Start time for log range (ISO 8601)`).option(`--end-time <iso8601>`,`End time for log range (ISO 8601)`).option(`--limit <number>`,`Maximum number of search results to return`,X).action(async(e,n)=>{await $(n,e=>new t.r(e),{searchQuery:e,mode:n.mode,service:n.service,level:Z(n.level),startTime:n.startTime,endTime:n.endTime,limit:n.limit})})),i=Q(new l.Command(`trace`).alias(`get-trace-timeline`).description(`Get the complete trace timeline for a trace ID`).argument(`<trace-id>`,`Trace ID to inspect`).action(async(e,n)=>{await $(n,e=>new t.a(e),{traceId:e})})),a=Q(new l.Command(`analyze-errors`).alias(`errors`).description(`Analyze error patterns and group similar errors`).option(`--trace-id <traceId>`,`Optional trace ID to scope the analysis`).option(`--start-time <iso8601>`,`Start time for error analysis (ISO 8601)`).option(`--end-time <iso8601>`,`End time for error analysis (ISO 8601)`).option(`--limit <number>`,`Maximum number of error entries to analyze`,X).action(async e=>{await $(e,e=>new t.l(e),{traceId:e.traceId,timeRange:Le(e.startTime,e.endTime),limit:e.limit})})),o=Q(new l.Command(`stats`).alias(`get-log-stats`).description(`Get aggregated log statistics grouped by level, service, or both`).option(`--start-time <iso8601>`,`Start time for statistics (ISO 8601)`).option(`--end-time <iso8601>`,`End time for statistics (ISO 8601)`).option(`--group-by <groupBy>`,`Group statistics by level, service, or both`,Ne).action(async e=>{await $(e,e=>new t.s(e),{startTime:e.startTime,endTime:e.endTime,groupBy:e.groupBy})})),s=Q(new l.Command(`services`).alias(`get-services`).description(`Get the list of unique services present in the log database`).action(async e=>{await $(e,e=>new t.o(e),{})})),c=Q(new l.Command(`clear`).alias(`clear-logs`).description(`Clear all logs from the database`).action(async e=>{await $(e,e=>new t.c(e),{})}));return e.addCommand(n).addCommand(r).addCommand(i).addCommand(a).addCommand(o).addCommand(s).addCommand(c)}const ze=Re();function Be(){return t.f()}const Ve=new l.Command(`mcp-serve`).description(`Start MCP server with stdio transport`).option(`--cleanup`,`Stop HTTP server on MCP server shutdown`,!1).option(`--db-path <path>`,`Path to SQLite database file`,Be()).option(`--in-memory`,`Use in-memory database (for testing)`,!1).option(`--db-max-bytes <bytes>`,`Maximum SQLite database size before trimming`).option(`--db-target-bytes <bytes>`,`Target SQLite database size after trimming`).option(`--db-retention-interval-ms <ms>`,`How often to check database size`,`300000`).option(`--db-retention-batch-size <count>`,`Number of oldest log rows to delete per trim batch`,`1000`).option(`--registry-path <path>`,`Custom registry path or directory for service discovery`).option(`--registry-dir <path>`,`Custom registry directory for service discovery`).action(async e=>{let n;try{let r=e.registryPath||e.registryDir;r&&(process.env.PORT_REGISTRY_PATH=r,process.env.PROCESS_REGISTRY_PATH=(0,c.resolveSiblingRegistryPath)(r,`processes.json`)??process.env.PROCESS_REGISTRY_PATH);let i=t.u(),a=i.get(t.m.LogStorageService),o=i.get(t.m.LogRetentionService),s=i.get(t.m.HttpServerManager);await a.initializeDatabase(e.dbPath,e.inMemory);let l=o.startSizeRetentionMonitor(J({dbMaxBytes:e.dbMaxBytes,dbTargetBytes:e.dbTargetBytes,dbRetentionIntervalMs:e.dbRetentionIntervalMs,dbRetentionBatchSize:e.dbRetentionBatchSize})),u=new t.t(t.n(i));n=await(0,c.createProcessLease)({repositoryPath:process.cwd(),serviceName:`log-sink-mcp-mcp-serve`,serviceType:`tool`,environment:process.env.NODE_ENV||`development`,pid:process.pid,host:`127.0.0.1`,command:process.argv[1],args:process.argv.slice(2),metadata:{transport:`stdio`,command:`mcp-serve`}});let d=s.ensureRunning(void 0,e.dbPath),f;d.then(t=>{t.running&&(f=s.startHealthMonitor(e.dbPath))}).catch(()=>void 0);let p=async t=>{console.error(`\nReceived ${t}, shutting down gracefully...`);try{f?.(),await u.stop(),e.cleanup&&(await d.catch(()=>({running:!1}))).running&&(await s.stop(),console.error(`HTTP server stopped`)),l(),await a.disconnect(),await n?.release(),process.exit(0)}catch(e){console.error(`Error during shutdown:`,e),process.exit(1)}};process.on(`SIGINT`,()=>p(`SIGINT`)),process.on(`SIGTERM`,()=>p(`SIGTERM`)),await u.start()}catch(e){await n?.release().catch(()=>void 0),console.error(`Failed to start MCP server:`,e),process.exit(1)}}),He=new l.Command(`start`).description(`Start HTTP and/or MCP servers with singleton coordination`).option(`--mcp-only`,`Start only the MCP server (stdio transport)`,!1).option(`--http-only`,`Start only the HTTP server`,!1).option(`-p, --port <port>`,`Port for HTTP server`,String(s.DEFAULT_PORT_RANGE.min)).option(`--db-path <path>`,`Path to SQLite database file`,t.f()).option(`--in-memory`,`Use in-memory database (for testing)`,!1).option(`--db-max-bytes <bytes>`,`Maximum SQLite database size before trimming`).option(`--db-target-bytes <bytes>`,`Target SQLite database size after trimming`).option(`--db-retention-interval-ms <ms>`,`How often to check database size`,`300000`).option(`--db-retention-batch-size <count>`,`Number of oldest log rows to delete per trim batch`,`1000`).option(`--registry-path <path>`,`Custom registry path or directory for service discovery`).option(`--registry-dir <path>`,`Custom registry directory for service discovery`).action(async e=>{let n;try{let r=e.registryPath||e.registryDir;r&&(process.env.PORT_REGISTRY_PATH=r,process.env.PROCESS_REGISTRY_PATH=(0,c.resolveSiblingRegistryPath)(r,`processes.json`)??process.env.PROCESS_REGISTRY_PATH),e.dbMaxBytes&&(process.env.LOG_SINK_DB_MAX_BYTES=e.dbMaxBytes),e.dbTargetBytes&&(process.env.LOG_SINK_DB_TARGET_BYTES=e.dbTargetBytes),e.dbRetentionIntervalMs&&(process.env.LOG_SINK_DB_RETENTION_INTERVAL_MS=e.dbRetentionIntervalMs),e.dbRetentionBatchSize&&(process.env.LOG_SINK_DB_RETENTION_BATCH_SIZE=e.dbRetentionBatchSize);let i=t.u(),a=i.get(t.m.LogStorageService);await a.initializeDatabase(e.dbPath,e.inMemory);let o=Number.parseInt(e.port,10),s=!e.mcpOnly,l=!e.httpOnly,u=i.get(t.m.LogRetentionService),d=l?u.startSizeRetentionMonitor(J({dbMaxBytes:e.dbMaxBytes,dbTargetBytes:e.dbTargetBytes,dbRetentionIntervalMs:e.dbRetentionIntervalMs,dbRetentionBatchSize:e.dbRetentionBatchSize})):()=>void 0;if(console.log(`🚀 Starting log-sink-mcp services...`),console.log(` Database: ${e.inMemory?`In-Memory`:e.dbPath}`),s){let n=await i.get(t.m.HttpServerManager).ensureRunning(o,e.dbPath);n.running?(console.log(`✓ HTTP Server: Running on http://localhost:${n.port} (PID: ${n.pid})`),console.log(` Health check: http://localhost:${n.port}/health`),console.log(` Log ingestion: POST http://localhost:${n.port}/logs`)):(console.error(`✗ HTTP Server failed to start: ${n.error}`),l||process.exit(1))}if(l){n=await(0,c.createProcessLease)({repositoryPath:process.cwd(),serviceName:`log-sink-mcp-start`,serviceType:`tool`,environment:process.env.NODE_ENV||`development`,pid:process.pid,host:`127.0.0.1`,command:process.argv[1],args:process.argv.slice(2),metadata:{transport:`stdio`,command:`start`}}),console.log(`✓ MCP Server: Starting with stdio transport...`);let e=new t.t(t.n(i));await e.start(),console.log(`✓ MCP Server: Ready for connections`);let r=async r=>{console.log(`\n\n${r} received. Shutting down gracefully...`);try{await e.stop(),console.log(`✓ MCP server stopped`),s&&(await i.get(t.m.HttpServerManager).stop(),console.log(`✓ HTTP server stopped`)),d(),await a.disconnect(),console.log(`✓ Database disconnected`),await n?.release(),console.log(`✓ Process registry entry released`),console.log(`Goodbye! 👋`),process.exit(0)}catch(e){console.error(`Error during shutdown:`,e),process.exit(1)}};process.on(`SIGINT`,()=>r(`SIGINT`)),process.on(`SIGTERM`,()=>r(`SIGTERM`)),console.log(`
585
+ `)})]}),(0,h.jsx)(`body`,{children:t})]})}function ge(e){let n=new d.Hono,r=e.get(t.m.LogQueryService);return n.get(`/`,async e=>{try{let t=await r.getActiveServices(),n=await r.getStatistics(),i=await r.filterByLevel([g.INFO,g.WARN,g.ERROR,g.FATAL],100),a={totalLogs:n.reduce((e,t)=>e+t.count,0),errors:n.filter(e=>e.level===`error`||e.level===`fatal`).reduce((e,t)=>e+t.count,0),services:new Set(n.map(e=>e.service)).size};return e.html((0,h.jsx)(R,{title:`Log Dashboard`,children:(0,h.jsx)(he,{initialLogs:i,services:t,stats:a})}))}catch(t){return console.error(`Failed to render dashboard:`,t),e.html((0,h.jsx)(R,{title:`Log Dashboard - Error`,children:(0,h.jsxs)(`div`,{style:`padding: 2rem; text-align: center;`,children:[(0,h.jsx)(`h1`,{children:`Failed to load dashboard`}),(0,h.jsx)(`p`,{children:t instanceof Error?t.message:String(t)}),(0,h.jsx)(`a`,{href:`/`,style:`color: #2196f3; text-decoration: underline;`,children:`Retry`})]})}),500)}}),n}const z=5*1024*1024,B=2e3,V=8e3,_e=n.z.object({timestamp:n.z.string().datetime().optional(),level:n.z.enum([`trace`,`debug`,`info`,`warn`,`error`,`fatal`]),message:n.z.string().min(1).max(V),traceId:n.z.string().regex(/^[0-9a-f]{32}$/i,`traceId must be 32 hex characters`).optional(),spanId:n.z.string().regex(/^[0-9a-f]{16}$/i,`spanId must be 16 hex characters`).optional(),parentSpanId:n.z.string().regex(/^[0-9a-f]{16}$/i,`parentSpanId must be 16 hex characters`).optional(),service:n.z.string().min(1).max(200),hostname:n.z.string().max(255).optional(),pid:n.z.number().int().nonnegative().optional(),metadata:n.z.record(n.z.string(),n.z.unknown()).optional(),errorType:n.z.string().max(255).optional(),errorMessage:n.z.string().max(8e3).optional(),errorStack:n.z.string().max(32e3).optional()}),ve=n.z.object({logs:n.z.array(_e).min(1).max(1e3)});function H(e){if(!e||!/^\d+$/.test(e))throw Error(`OTLP timestamp must be a numeric unix-nanoseconds string`);let t=Number(BigInt(e)/BigInt(1e6));return new Date(t)}function U(e,t){return typeof e==`string`&&RegExp(`^[0-9a-f]{${t}}$`,`i`).test(e)}function W(e){if(!e)return null;let t=Number(e);return!Number.isFinite(t)||t<=0?null:t>z?`Request body exceeds ${z} bytes`:null}async function G(e){try{return await e.req.json()}catch(e){throw Error(`Invalid JSON body: ${e instanceof Error?e.message:String(e)}`)}}function ye(e){if(!Array.isArray(e.resourceSpans)||e.resourceSpans.length===0)throw Error(`Invalid OTLP trace request: missing resourceSpans`);let t=0;for(let n of e.resourceSpans){if(!Array.isArray(n.scopeSpans))throw Error(`Invalid OTLP trace request: scopeSpans must be an array`);for(let e of n.scopeSpans){if(!Array.isArray(e.spans))throw Error(`Invalid OTLP trace request: spans must be an array`);for(let n of e.spans){if(t+=1,t>B)throw Error(`OTLP trace request exceeds ${B} spans`);if(!U(n.traceId,32))throw Error(`Invalid OTLP trace request: traceId must be 32 hex characters`);if(!U(n.spanId,16))throw Error(`Invalid OTLP trace request: spanId must be 16 hex characters`);if(n.parentSpanId&&!U(n.parentSpanId,16))throw Error(`Invalid OTLP trace request: parentSpanId must be 16 hex characters`);if(!n.name||n.name.length>V)throw Error(`Invalid OTLP trace request: span name is missing or too long`);H(n.startTimeUnixNano)}}}}function be(e){if(!Array.isArray(e.resourceLogs)||e.resourceLogs.length===0)throw Error(`Invalid OTLP logs request: missing resourceLogs`);let t=0;for(let n of e.resourceLogs){if(!Array.isArray(n.scopeLogs))throw Error(`Invalid OTLP logs request: scopeLogs must be an array`);for(let e of n.scopeLogs){if(!Array.isArray(e.logRecords))throw Error(`Invalid OTLP logs request: logRecords must be an array`);for(let n of e.logRecords){if(t+=1,t>B)throw Error(`OTLP logs request exceeds ${B} log records`);if(n.traceId&&!U(n.traceId,32))throw Error(`Invalid OTLP logs request: traceId must be 32 hex characters`);if(n.spanId&&!U(n.spanId,16))throw Error(`Invalid OTLP logs request: spanId must be 16 hex characters`);if(n.severityText&&n.severityText.length>32)throw Error(`Invalid OTLP logs request: severityText is too long`);H(n.timeUnixNano)}}}}function xe(e){return(e.resource?.attributes?.find(e=>e.key===`service.name`))?.value?.stringValue||`unknown-service`}function K(e,t){let n=e?.find(e=>e.key===t);if(!n)return;let{value:r}=n;if(r.stringValue!==void 0)return r.stringValue;if(r.intValue!==void 0)return r.intValue;if(r.doubleValue!==void 0)return String(r.doubleValue);if(r.boolValue!==void 0)return String(r.boolValue)}function Se(e){return(e.resource?.attributes?.find(e=>e.key===`service.name`))?.value?.stringValue||`unknown-service`}function Ce(e){return e===void 0?`info`:e<=4?`trace`:e<=8?`debug`:e<=12?`info`:e<=16?`warn`:e<=20?`error`:`fatal`}function we(e,t){let n=H(e.timeUnixNano),r=e.severityText?.toLowerCase()||Ce(e.severityNumber),i=``;if(e.body?.stringValue)i=e.body.stringValue;else if(e.body?.kvlistValue){let t={};for(let n of e.body.kvlistValue.values){let e=n.value.stringValue??n.value.intValue??n.value.doubleValue??n.value.boolValue;t[n.key]=e}i=JSON.stringify(t)}let a={},o=null,s=null,c=null;if(e.attributes)for(let t of e.attributes){let e=t.value.stringValue??t.value.intValue??t.value.doubleValue??t.value.boolValue;t.key===`exception.type`?o=String(e):t.key===`exception.message`?s=String(e):t.key===`exception.stacktrace`?c=String(e):a[t.key]=e}return{timestamp:n,level:r,message:i||`[LOG] ${t}`,traceId:e.traceId??null,spanId:e.spanId??null,parentSpanId:null,service:t,hostname:null,pid:null,metadata:Object.keys(a).length>0?a:null,errorType:o,errorMessage:s,errorStack:c}}function Te(e,t){let n=H(e.startTimeUnixNano),r=e.status?.code,i=`info`;r===2?i=`error`:r===1&&(i=`info`);let a=K(e.attributes,`exception.type`),o=K(e.attributes,`exception.message`),s=K(e.attributes,`exception.stacktrace`),c={};if(e.attributes)for(let t of e.attributes){let e=t.value.stringValue??t.value.intValue??t.value.doubleValue??t.value.boolValue;c[t.key]=e}return c.spanName=e.name,c.spanKind=e.kind,e.status?.message&&(c.statusMessage=e.status.message),{timestamp:n,level:i,message:`[SPAN] ${e.name}`,traceId:e.traceId,spanId:e.spanId,parentSpanId:e.parentSpanId??null,service:t,hostname:null,pid:null,metadata:c,errorType:a??null,errorMessage:o??null,errorStack:s??null}}function Ee(e){let n=new d.Hono,r=e.get(t.m.LogStorageService);n.use(`*`,(0,p.cors)({origin:e=>e??null,credentials:!0,allowMethods:[`GET`,`POST`,`OPTIONS`],allowHeaders:[`Content-Type`,`Accept`,`Authorization`]})),n.get(`/health`,async e=>{try{return e.json({status:`healthy`,service:`log-sink-mcp-http`,serviceName:`log-sink-mcp-http`,timestamp:new Date().toISOString()})}catch(t){return e.json({status:`unhealthy`,error:t instanceof Error?t.message:String(t)},503)}});let i=(0,f.rateLimiter)({windowMs:60*1e3,limit:100,standardHeaders:`draft-6`,keyGenerator:e=>e.req.header(`x-forwarded-for`)||e.req.header(`x-real-ip`)||`unknown`});n.post(`/logs`,i,async e=>{try{let t=W(e.req.header(`content-length`));if(t)return e.json({success:!1,error:t},413);let n=await G(e),i=ve.safeParse(n);if(!i.success)return e.json({success:!1,error:`Validation failed`,details:i.error.issues},400);let{logs:a}=i.data,o=a.map(e=>({...e,timestamp:e.timestamp?new Date(e.timestamp):new Date})),s=await r.insertBatch(o);return e.json({success:!0,count:s.length,message:`Successfully inserted ${s.length} logs`},201)}catch(t){let n=t instanceof Error?t.message:String(t);return n.startsWith(`Invalid JSON body`)?e.json({success:!1,error:n},400):(console.error(`Failed to insert logs:`,t),e.json({success:!1,error:`Failed to insert logs`},500))}}),n.post(`/v1/traces`,i,async e=>{try{let t=W(e.req.header(`content-length`));if(t)return e.json({success:!1,error:t},413);let n=await G(e);ye(n);let i=[];for(let e of n.resourceSpans){let t=xe(e);for(let n of e.scopeSpans)for(let e of n.spans){let n=Te(e,t);i.push(n)}}let a=await r.insertBatch(i);return e.json({success:!0,count:a.length,message:`Successfully inserted ${a.length} trace spans as logs`},201)}catch(t){let n=t instanceof Error?t.message:String(t);return n.startsWith(`Invalid JSON body`)||n.startsWith(`Invalid OTLP`)||n.startsWith(`OTLP`)?e.json({success:!1,error:n},400):(console.error(`Failed to insert OTLP traces:`,t),e.json({success:!1,error:`Failed to insert OTLP traces`},500))}}),n.post(`/v1/logs`,i,async e=>{try{let t=W(e.req.header(`content-length`));if(t)return e.json({success:!1,error:t},413);let n=await G(e);be(n);let i=[];for(let e of n.resourceLogs){let t=Se(e);for(let n of e.scopeLogs)for(let e of n.logRecords){let n=we(e,t);i.push(n)}}let a=await r.insertBatch(i);return e.json({success:!0,count:a.length,message:`Successfully inserted ${a.length} OTLP logs`},201)}catch(t){let n=t instanceof Error?t.message:String(t);return n.startsWith(`Invalid JSON body`)||n.startsWith(`Invalid OTLP`)||n.startsWith(`OTLP`)?e.json({success:!1,error:n},400):(console.error(`Failed to insert OTLP logs:`,t),e.json({success:!1,error:`Failed to insert OTLP logs`},500))}});let a=oe(e);n.route(`/api`,a);let o=ge(e);return n.route(`/`,o),n}function q(e,t){if(e===void 0||e===``)return t;let n=Number.parseInt(e,10);if(!Number.isFinite(n)||n<=0)throw Error(`Invalid database retention value: ${e}`);return n}function J(e={}){let t=q(e.dbMaxBytes??process.env.LOG_SINK_DB_MAX_BYTES,268435456),n=q(e.dbTargetBytes??process.env.LOG_SINK_DB_TARGET_BYTES,Math.floor(t*.9));return{maxBytes:t,targetBytes:Math.min(n,t),intervalMs:q(e.dbRetentionIntervalMs??process.env.LOG_SINK_DB_RETENTION_INTERVAL_MS,3e5),batchSize:q(e.dbRetentionBatchSize??process.env.LOG_SINK_DB_RETENTION_BATCH_SIZE,1e3)}}const De=String(s.DEFAULT_PORT_RANGE.min),Oe=t.f(),ke=new l.Command(`http-serve`).description(`Start HTTP server for log ingestion with configurable port, database path, and in-memory mode`).option(`-p, --port <port>`,`Port to listen on`,De).option(`--db-path <path>`,`Path to SQLite database file`,Oe).option(`--in-memory`,`Use in-memory database (for testing)`,!1).option(`--db-max-bytes <bytes>`,`Maximum SQLite database size before trimming`).option(`--db-target-bytes <bytes>`,`Target SQLite database size after trimming`).option(`--db-retention-interval-ms <ms>`,`How often to check database size`,`300000`).option(`--db-retention-batch-size <count>`,`Number of oldest log rows to delete per trim batch`,`1000`).option(`--registry-path <path>`,`Custom registry path or directory for service discovery`).action(async e=>{let n;try{let r=Number.parseInt(e.port,10),i={min:s.DEFAULT_PORT_RANGE.min,max:Math.max(s.DEFAULT_PORT_RANGE.max??4999,r)},a=process.env.NODE_ENV||`development`,o=t.p(process.cwd()),l=`log-sink-mcp-http`;e.registryPath&&(process.env.PORT_REGISTRY_PATH=e.registryPath,process.env.PROCESS_REGISTRY_PATH=(0,c.resolveSiblingRegistryPath)(e.registryPath,`processes.json`)??process.env.PROCESS_REGISTRY_PATH);let d=new s.PortRegistryService(process.env.PORT_REGISTRY_PATH),f=t.u(),p=f.get(t.m.LogStorageService),m=f.get(t.m.LogRetentionService);await p.initializeDatabase(e.dbPath,e.inMemory);let h=m.startSizeRetentionMonitor(J({dbMaxBytes:e.dbMaxBytes,dbTargetBytes:e.dbTargetBytes,dbRetentionIntervalMs:e.dbRetentionIntervalMs,dbRetentionBatchSize:e.dbRetentionBatchSize})),g=Ee(f),_=await d.reservePort({repositoryPath:o,serviceName:l,serviceType:`tool`,environment:a,preferredPort:r,pid:process.pid,host:`127.0.0.1`,force:!0,portRange:i,metadata:{healthCheckUrl:`http://localhost:${r}/health`,dbPath:e.dbPath}});if(!_.success||!_.record)throw Error(_.error||`Failed to reserve service in global registry`);let v=_.record.port;if(v!==r){let t=await d.reservePort({repositoryPath:o,serviceName:l,serviceType:`tool`,environment:a,preferredPort:v,pid:process.pid,host:`127.0.0.1`,force:!0,portRange:i,metadata:{healthCheckUrl:`http://localhost:${v}/health`,dbPath:e.dbPath}});if(!t.success||!t.record)throw Error(t.error||`Failed to update service metadata in registry`);_=t}if(!_.record)throw Error(`Port reservation lost before startup`);let y=_.record.port;n=await(0,c.createProcessLease)({repositoryPath:o,serviceName:l,serviceType:`tool`,environment:a,pid:process.pid,port:y,host:`127.0.0.1`,command:process.argv[1],args:process.argv.slice(2),metadata:{dbPath:e.dbPath,transport:`http`}});let b;try{b=(0,u.serve)({fetch:g.fetch,port:y})}catch(e){try{await n.release({kill:!1})}catch{}throw e}let x=async()=>{try{b.close();try{await n?.release({kill:!1})}catch{}h(),await p.disconnect(),process.exit(0)}catch{process.exit(1)}};process.on(`SIGINT`,()=>x()),process.on(`SIGTERM`,()=>x())}catch{try{await n?.release({kill:!1})}catch{}process.exit(1)}}),Ae=t.f(),Y=new Set(Object.values(g)),je=new Set([`level`,`service`,`both`]),Me={stdout:e=>console.log(e),stderr:e=>console.error(e)};function X(e){let t=Number.parseInt(e,10);if(!Number.isInteger(t)||t<=0)throw new l.InvalidArgumentError(`Expected a positive integer.`);return t}function Ne(e){if(!je.has(e))throw new l.InvalidArgumentError(`Expected one of: 'level', 'service', 'both'.`);return e}function Z(e,t=[]){if(!Y.has(e))throw new l.InvalidArgumentError(`Invalid log level '${e}'. Expected one of: ${Array.from(Y).join(`, `)}`);return[...t,e]}function Pe(e){return e.content.map(e=>e.type===`text`?e.text:JSON.stringify(e)).join(`
586
+ `)}function Q(e){return e.option(`--db-path <path>`,`Path to SQLite database file`,Ae).option(`--in-memory`,`Use in-memory database`,!1)}async function Fe(e,n){let r=t.u(),i=r.get(t.m.LogStorageService);await i.initializeDatabase(e.dbPath,e.inMemory);try{return await n(r)}finally{await i.disconnect()}}async function Ie(e,t,n,r=Me){try{let i=await Fe(e,async e=>t(e).execute(n)),a=Pe(i);return i.isError?(r.stderr(a),1):(r.stdout(a),0)}catch(e){return r.stderr(`Error executing logs command: ${e instanceof Error?e.message:String(e)}`),1}}async function $(e,t,n){let r=await Ie(e,t,n);r!==0&&process.exit(r)}function Le(e,t){if(!(!e&&!t)){if(!e||!t)throw new l.InvalidArgumentError(`Provide both --start-time and --end-time together.`);return{start:e,end:t}}}function Re(){let e=new l.Command(`logs`).description(`Run log analysis commands that mirror MCP tool capabilities`),n=Q(new l.Command(`query`).alias(`query-logs`).description(`Query and filter log entries by level, trace ID, service, or time range`).option(`--level <level...>`,`Log level(s) to filter by`,Z).option(`--trace-id <traceId>`,`Trace ID to filter by`).option(`--service <service...>`,`Service name(s) to filter by`).option(`--start-time <iso8601>`,`Start time for log range (ISO 8601)`).option(`--end-time <iso8601>`,`End time for log range (ISO 8601)`).option(`--limit <number>`,`Maximum number of log entries to return`,X).action(async e=>{await $(e,e=>new t.i(e),{level:e.level,traceId:e.traceId,service:e.service,startTime:e.startTime,endTime:e.endTime,limit:e.limit})})),r=Q(new l.Command(`search`).alias(`search-logs`).description(`Search log entries with full-text search and optional filters`).argument(`<search-query>`,`FTS5 search query`).option(`--mode <mode>`,`Search strategy: fts, semantic, or hybrid`,`hybrid`).option(`--service <service...>`,`Service name(s) to filter by`).option(`--level <level...>`,`Log level(s) to filter by`,Z).option(`--start-time <iso8601>`,`Start time for log range (ISO 8601)`).option(`--end-time <iso8601>`,`End time for log range (ISO 8601)`).option(`--limit <number>`,`Maximum number of search results to return`,X).action(async(e,n)=>{await $(n,e=>new t.r(e),{searchQuery:e,mode:n.mode,service:n.service,level:n.level,startTime:n.startTime,endTime:n.endTime,limit:n.limit})})),i=Q(new l.Command(`trace`).alias(`get-trace-timeline`).description(`Get the complete trace timeline for a trace ID`).argument(`<trace-id>`,`Trace ID to inspect`).action(async(e,n)=>{await $(n,e=>new t.a(e),{traceId:e})})),a=Q(new l.Command(`analyze-errors`).alias(`errors`).description(`Analyze error patterns and group similar errors`).option(`--trace-id <traceId>`,`Optional trace ID to scope the analysis`).option(`--start-time <iso8601>`,`Start time for error analysis (ISO 8601)`).option(`--end-time <iso8601>`,`End time for error analysis (ISO 8601)`).option(`--limit <number>`,`Maximum number of error entries to analyze`,X).action(async function(e){let n;try{n=Le(e.startTime,e.endTime)}catch(e){this.error(e instanceof Error?e.message:String(e))}await $(e,e=>new t.l(e),{traceId:e.traceId,timeRange:n,limit:e.limit})})),o=Q(new l.Command(`stats`).alias(`get-log-stats`).description(`Get aggregated log statistics grouped by level, service, or both`).option(`--start-time <iso8601>`,`Start time for statistics (ISO 8601)`).option(`--end-time <iso8601>`,`End time for statistics (ISO 8601)`).option(`--group-by <groupBy>`,`Group statistics by level, service, or both`,Ne).action(async e=>{await $(e,e=>new t.s(e),{startTime:e.startTime,endTime:e.endTime,groupBy:e.groupBy})})),s=Q(new l.Command(`services`).alias(`get-services`).description(`Get the list of unique services present in the log database`).action(async e=>{await $(e,e=>new t.o(e),{})})),c=Q(new l.Command(`clear`).alias(`clear-logs`).description(`Clear all logs from the database`).action(async e=>{await $(e,e=>new t.c(e),{})}));return e.addCommand(n).addCommand(r).addCommand(i).addCommand(a).addCommand(o).addCommand(s).addCommand(c)}const ze=Re();function Be(){return t.f()}const Ve=new l.Command(`mcp-serve`).description(`Start MCP server with stdio transport`).option(`--cleanup`,`Stop HTTP server on MCP server shutdown`,!1).option(`--db-path <path>`,`Path to SQLite database file`,Be()).option(`--in-memory`,`Use in-memory database (for testing)`,!1).option(`--db-max-bytes <bytes>`,`Maximum SQLite database size before trimming`).option(`--db-target-bytes <bytes>`,`Target SQLite database size after trimming`).option(`--db-retention-interval-ms <ms>`,`How often to check database size`,`300000`).option(`--db-retention-batch-size <count>`,`Number of oldest log rows to delete per trim batch`,`1000`).option(`--registry-path <path>`,`Custom registry path or directory for service discovery`).option(`--registry-dir <path>`,`Custom registry directory for service discovery`).action(async e=>{let n;try{let r=e.registryPath||e.registryDir;r&&(process.env.PORT_REGISTRY_PATH=r,process.env.PROCESS_REGISTRY_PATH=(0,c.resolveSiblingRegistryPath)(r,`processes.json`)??process.env.PROCESS_REGISTRY_PATH);let i=t.u(),a=i.get(t.m.LogStorageService),o=i.get(t.m.LogRetentionService),s=i.get(t.m.HttpServerManager);await a.initializeDatabase(e.dbPath,e.inMemory);let l=o.startSizeRetentionMonitor(J({dbMaxBytes:e.dbMaxBytes,dbTargetBytes:e.dbTargetBytes,dbRetentionIntervalMs:e.dbRetentionIntervalMs,dbRetentionBatchSize:e.dbRetentionBatchSize})),u=new t.t(t.n(i));n=await(0,c.createProcessLease)({repositoryPath:process.cwd(),serviceName:`log-sink-mcp-mcp-serve`,serviceType:`tool`,environment:process.env.NODE_ENV||`development`,pid:process.pid,host:`127.0.0.1`,command:process.argv[1],args:process.argv.slice(2),metadata:{transport:`stdio`,command:`mcp-serve`}});let d=s.ensureRunning(void 0,e.dbPath),f;d.then(t=>{t.running&&(f=s.startHealthMonitor(e.dbPath))}).catch(()=>void 0);let p=async t=>{console.error(`\nReceived ${t}, shutting down gracefully...`);try{f?.(),await u.stop(),e.cleanup&&(await d.catch(()=>({running:!1}))).running&&(await s.stop(),console.error(`HTTP server stopped`)),l(),await a.disconnect(),await n?.release(),process.exit(0)}catch(e){console.error(`Error during shutdown:`,e),process.exit(1)}};process.on(`SIGINT`,()=>p(`SIGINT`)),process.on(`SIGTERM`,()=>p(`SIGTERM`)),await u.start()}catch(e){await n?.release().catch(()=>void 0),console.error(`Failed to start MCP server:`,e),process.exit(1)}}),He=new l.Command(`start`).description(`Start HTTP and/or MCP servers with singleton coordination`).option(`--mcp-only`,`Start only the MCP server (stdio transport)`,!1).option(`--http-only`,`Start only the HTTP server`,!1).option(`-p, --port <port>`,`Port for HTTP server`,String(s.DEFAULT_PORT_RANGE.min)).option(`--db-path <path>`,`Path to SQLite database file`,t.f()).option(`--in-memory`,`Use in-memory database (for testing)`,!1).option(`--db-max-bytes <bytes>`,`Maximum SQLite database size before trimming`).option(`--db-target-bytes <bytes>`,`Target SQLite database size after trimming`).option(`--db-retention-interval-ms <ms>`,`How often to check database size`,`300000`).option(`--db-retention-batch-size <count>`,`Number of oldest log rows to delete per trim batch`,`1000`).option(`--registry-path <path>`,`Custom registry path or directory for service discovery`).option(`--registry-dir <path>`,`Custom registry directory for service discovery`).action(async e=>{let n;try{let r=e.registryPath||e.registryDir;r&&(process.env.PORT_REGISTRY_PATH=r,process.env.PROCESS_REGISTRY_PATH=(0,c.resolveSiblingRegistryPath)(r,`processes.json`)??process.env.PROCESS_REGISTRY_PATH),e.dbMaxBytes&&(process.env.LOG_SINK_DB_MAX_BYTES=e.dbMaxBytes),e.dbTargetBytes&&(process.env.LOG_SINK_DB_TARGET_BYTES=e.dbTargetBytes),e.dbRetentionIntervalMs&&(process.env.LOG_SINK_DB_RETENTION_INTERVAL_MS=e.dbRetentionIntervalMs),e.dbRetentionBatchSize&&(process.env.LOG_SINK_DB_RETENTION_BATCH_SIZE=e.dbRetentionBatchSize);let i=t.u(),a=i.get(t.m.LogStorageService);await a.initializeDatabase(e.dbPath,e.inMemory);let o=Number.parseInt(e.port,10),s=!e.mcpOnly,l=!e.httpOnly,u=i.get(t.m.LogRetentionService),d=l?u.startSizeRetentionMonitor(J({dbMaxBytes:e.dbMaxBytes,dbTargetBytes:e.dbTargetBytes,dbRetentionIntervalMs:e.dbRetentionIntervalMs,dbRetentionBatchSize:e.dbRetentionBatchSize})):()=>void 0;if(console.log(`🚀 Starting log-sink-mcp services...`),console.log(` Database: ${e.inMemory?`In-Memory`:e.dbPath}`),s){let n=await i.get(t.m.HttpServerManager).ensureRunning(o,e.dbPath);n.running?(console.log(`✓ HTTP Server: Running on http://localhost:${n.port} (PID: ${n.pid})`),console.log(` Health check: http://localhost:${n.port}/health`),console.log(` Log ingestion: POST http://localhost:${n.port}/logs`)):(console.error(`✗ HTTP Server failed to start: ${n.error}`),l||process.exit(1))}if(l){n=await(0,c.createProcessLease)({repositoryPath:process.cwd(),serviceName:`log-sink-mcp-start`,serviceType:`tool`,environment:process.env.NODE_ENV||`development`,pid:process.pid,host:`127.0.0.1`,command:process.argv[1],args:process.argv.slice(2),metadata:{transport:`stdio`,command:`start`}}),console.log(`✓ MCP Server: Starting with stdio transport...`);let e=new t.t(t.n(i));await e.start(),console.log(`✓ MCP Server: Ready for connections`);let r=async r=>{console.log(`\n\n${r} received. Shutting down gracefully...`);try{await e.stop(),console.log(`✓ MCP server stopped`),s&&(await i.get(t.m.HttpServerManager).stop(),console.log(`✓ HTTP server stopped`)),d(),await a.disconnect(),console.log(`✓ Database disconnected`),await n?.release(),console.log(`✓ Process registry entry released`),console.log(`Goodbye! 👋`),process.exit(0)}catch(e){console.error(`Error during shutdown:`,e),process.exit(1)}};process.on(`SIGINT`,()=>r(`SIGINT`)),process.on(`SIGTERM`,()=>r(`SIGTERM`)),console.log(`
587
587
  Press Ctrl+C to stop the server`)}else console.log(`
588
588
  ✓ HTTP server started in background`),console.log(`Use "log-sink-mcp stop" to stop the HTTP server`),n&&await n.release().catch(()=>void 0),process.exit(0)}catch(e){n&&await n.release().catch(()=>void 0),console.error(`Error starting services:`,e),process.exit(1)}}),Ue=new l.Command(`status`).description(`Show status of HTTP server and diagnostics`).action(async()=>{try{console.log(`📊 log-sink-mcp Status
589
589
  `),console.log(`─`.repeat(50));let e=await t.u().get(t.m.HttpServerManager).getStatus();e.running?(console.log(`HTTP Server: 🟢 Running`),console.log(` PID: ${e.pid}`),console.log(` Port: ${e.port}`),console.log(` Health: http://localhost:${e.port}/health`)):(console.log(`HTTP Server: 🔴 Not Running`),e.error&&console.log(` Error: ${e.error}`)),console.log(``);try{let e=t.f(),n=((await i.stat(e)).size/1024/1024).toFixed(2);console.log(`Database: ${e} (${n} MB)`)}catch{console.log(`Database: Not found or not accessible`)}console.log(`─`.repeat(50)),process.exit(0)}catch(e){console.error(`Error getting status:`,e),process.exit(1)}}),We=new l.Command(`stop`).description(`Stop HTTP server and clean up registry/PID files`).action(async()=>{try{console.log(`🛑 Stopping log-sink-mcp services...`),await t.u().get(t.m.HttpServerManager).stop()?(console.log(`✓ HTTP server stopped`),console.log(`✓ Registry and PID files cleaned up`)):console.log(`ℹ No HTTP server was running`),console.log(`Done! 👋`),process.exit(0)}catch(e){console.error(`Error stopping services:`,e),process.exit(1)}}),Ge=(0,a.dirname)((0,o.fileURLToPath)(require(`url`).pathToFileURL(__filename).href)),Ke=JSON.parse((0,r.readFileSync)((0,a.join)(Ge,`../package.json`),`utf-8`));async function qe(){let e=new l.Command;e.name(`log-sink-mcp`).description(`Log sink MCP server with HTTP ingestion and AI analysis`).version(Ke.version),e.addCommand(ie),e.addCommand(He),e.addCommand(We),e.addCommand(Ue),e.addCommand(ke),e.addCommand(Ve),e.addCommand(ze),await e.parseAsync(process.argv)}qe();