@vpxa/kb 0.1.29 → 0.1.30
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/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{readFileSync as e}from"node:fs";import{dirname as t,resolve as n}from"node:path";import{fileURLToPath as r}from"node:url";import{createLogger as i,serializeError as a}from"../../core/dist/index.js";import{parseArgs as o}from"node:util";const s=t(r(import.meta.url)),c=(()=>{try{let t=n(s,`..`,`..`,`..`,`package.json`);return JSON.parse(e(t,`utf-8`)).version??`0.0.0`}catch{return`0.0.0`}})(),l=i(`server`),{values:u}=o({options:{transport:{type:`string`,default:process.env.KB_TRANSPORT??`stdio`},port:{type:`string`,default:process.env.KB_PORT??`3210`}}});async function d(){if(process.on(`unhandledRejection`,e=>{l.error(`Unhandled rejection`,a(e))}),l.info(`Starting MCP Knowledge Base server`,{version:c}),u.transport===`http`){let[{default:e},{loadConfig:t},{registerDashboardRoutes:n,resolveDashboardDir:r}]=await Promise.all([import(`express`),import(`./config.js`),import(`./dashboard-static.js`)]),i=t();l.info(`Config loaded`,{sourceCount:i.sources.length,storePath:i.store.path});let o=e();o.use(e.json());let s=Number(u.port);o.use((e,t,n)=>{if(t.setHeader(`Access-Control-Allow-Origin`,process.env.KB_CORS_ORIGIN??`http://localhost:${s}`),t.setHeader(`Access-Control-Allow-Methods`,`GET, POST, DELETE, OPTIONS`),t.setHeader(`Access-Control-Allow-Headers`,`Content-Type, Authorization`),e.method===`OPTIONS`){t.status(204).end();return}n()}),n(o,r(),l),o.get(`/health`,(e,t)=>{t.json({status:`ok`})});let c=!1,d=null,f=null,p=Promise.resolve();o.post(`/mcp`,async(e,t)=>{if(!c||!d||!f){t.status(503).json({jsonrpc:`2.0`,error:{code:-32603,message:`Server initializing — please retry in a few seconds`},id:null});return}let n=p,r;p=new Promise(e=>{r=e}),await n;try{let n=new f({sessionIdGenerator:void 0});await d.connect(n),await n.handleRequest(e,t,e.body),n.close()}catch(e){if(l.error(`MCP handler error`,a(e)),!t.headersSent){let n=e instanceof Error?e.message:String(e),r=n.includes(`Not Acceptable`);t.status(r?406:500).json({jsonrpc:`2.0`,error:{code:r?-32e3:-32603,message:r?n:`Internal server error`},id:null})}}finally{r()}}),o.get(`/mcp`,(e,t)=>{t.writeHead(405).end(JSON.stringify({jsonrpc:`2.0`,error:{code:-32e3,message:`Method not allowed.`},id:null}))}),o.delete(`/mcp`,(e,t)=>{t.writeHead(405).end(JSON.stringify({jsonrpc:`2.0`,error:{code:-32e3,message:`Method not allowed.`},id:null}))});let m=o.listen(s,`127.0.0.1`,()=>{l.info(`MCP server listening`,{url:`http://127.0.0.1:${s}/mcp`,port:s}),setTimeout(async()=>{try{let[{createLazyServer:e,ALL_TOOL_NAMES:t},{StreamableHTTPServerTransport:n},{checkForUpdates:r}]=await Promise.all([import(`./server.js`),import(`@modelcontextprotocol/sdk/server/streamableHttp.js`),import(`./version-check.js`)]);r();let o=e(i);d=o.server,f=n,c=!0,l.info(`MCP server configured (lazy — KB initializing in background)`,{toolCount:t.length,resourceCount:2}),o.startInit(),process.env.KB_AUTO_INDEX===`true`?o.ready.then(async()=>{try{let e=i.sources.map(e=>e.path).join(`, `);l.info(`Running initial index`,{sourcePaths:e}),await o.runInitialIndex(),l.info(`Initial index complete`)}catch(e){l.error(`Initial index failed; will retry on kb_reindex`,a(e))}}).catch(e=>l.error(`KB init or indexing failed`,a(e))):(o.ready.catch(e=>l.error(`KB initialization failed`,a(e))),l.info(`Auto-index disabled in HTTP mode (set KB_AUTO_INDEX=true to enable)`))}catch(e){l.error(`Failed to load server modules`,a(e))}},100)}),h=async e=>{l.info(`Shutdown signal received`,{signal:e}),m.close(),d&&await d.close(),process.exit(0)};process.on(`SIGINT`,()=>h(`SIGINT`)),process.on(`SIGTERM`,()=>h(`SIGTERM`))}else{let[{loadConfig:e,reconfigureForWorkspace:t},{createLazyServer:n},{checkForUpdates:i},{RootsListChangedNotificationSchema:o}]=await Promise.all([import(`./config.js`),import(`./server.js`),import(`./version-check.js`),import(`@modelcontextprotocol/sdk/types.js`)]),s=e();l.info(`Config loaded`,{sourceCount:s.sources.length,storePath:s.store.path}),i();let{server:c,startInit:u,ready:d,runInitialIndex:f}=n(s),{StdioServerTransport:p}=await import(`@modelcontextprotocol/sdk/server/stdio.js`),m=new p;await c.connect(m),l.info(`MCP server started`,{transport:`stdio`});let h=e=>{if(e.length===0)return!1;let n=e[0].uri,i=n.startsWith(`file://`)?r(n):n;return l.info(`MCP roots resolved`,{rootUri:n,rootPath:i,rootCount:e.length}),t(s,i),!0},g=!1;try{g=h((await c.server.listRoots()).roots),g||l.info(`No MCP roots yet; waiting for roots/list_changed notification`)}catch(e){l.warn(`MCP roots/list not supported by client; using cwd fallback`,{cwd:process.cwd(),...a(e)}),g=!0}g||=await new Promise(e=>{let t=setTimeout(()=>{l.warn(`Timed out waiting for MCP roots/list_changed; using cwd fallback`,{cwd:process.cwd()}),e(!1)},5e3);c.server.setNotificationHandler(o,async()=>{clearTimeout(t);try{e(h((await c.server.listRoots()).roots))}catch(t){l.warn(`roots/list retry failed after notification`,a(t)),e(!1)}})}),u(),d.catch(e=>{l.error(`Initialization failed — server will continue with limited tools`,a(e))}),process.env.KB_AUTO_INDEX===`false`?l.warn(`Auto-index disabled; use kb_reindex to index manually`):f().catch(e=>l.error(`Initial index failed`,a(e)))}}d().catch(e=>{l.error(`Fatal error`,a(e)),process.exit(1)});export{};
|