@hasna/brains 0.0.23 → 0.0.24

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.
@@ -0,0 +1,16 @@
1
+ import type { Server } from "@modelcontextprotocol/sdk/server/index.js";
2
+ export declare const MCP_HTTP_SERVICE_NAME = "brains";
3
+ export declare const DEFAULT_MCP_HTTP_PORT = 8801;
4
+ export declare function isHttpMode(argv?: string[], env?: NodeJS.ProcessEnv): boolean;
5
+ export declare function resolveMcpHttpPort(argv?: string[], env?: NodeJS.ProcessEnv): number;
6
+ export type McpHttpServerHandle = {
7
+ port: number;
8
+ host: string;
9
+ close: () => Promise<void>;
10
+ };
11
+ export declare function startMcpHttpServer(buildServer: () => Server, options?: {
12
+ port?: number;
13
+ host?: string;
14
+ serviceName?: string;
15
+ }): Promise<McpHttpServerHandle>;
16
+ //# sourceMappingURL=http.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/mcp/http.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AAExE,eAAO,MAAM,qBAAqB,WAAW,CAAC;AAC9C,eAAO,MAAM,qBAAqB,OAAO,CAAC;AAE1C,wBAAgB,UAAU,CACxB,IAAI,GAAE,MAAM,EAAiB,EAC7B,GAAG,GAAE,MAAM,CAAC,UAAwB,GACnC,OAAO,CAET;AAED,wBAAgB,kBAAkB,CAChC,IAAI,GAAE,MAAM,EAAiB,EAC7B,GAAG,GAAE,MAAM,CAAC,UAAwB,GACnC,MAAM,CASR;AAoBD,MAAM,MAAM,mBAAmB,GAAG;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC5B,CAAC;AAEF,wBAAsB,kBAAkB,CACtC,WAAW,EAAE,MAAM,MAAM,EACzB,OAAO,CAAC,EAAE;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE,GAC/D,OAAO,CAAC,mBAAmB,CAAC,CAwE9B"}
@@ -5,7 +5,7 @@ export declare const MCP_SERVER_INFO: {
5
5
  readonly name: "brains";
6
6
  readonly version: string;
7
7
  };
8
- export declare function createMcpServer(): Server<{
8
+ export declare function buildServer(): Server<{
9
9
  method: string;
10
10
  params?: {
11
11
  [x: string]: unknown;
@@ -39,6 +39,8 @@ export declare function createMcpServer(): Server<{
39
39
  } | undefined;
40
40
  } | undefined;
41
41
  }>;
42
+ /** @deprecated Use buildServer() */
43
+ export declare const createMcpServer: typeof buildServer;
42
44
  export declare function startMcpServer(transport?: StdioServerTransport): Promise<Server<{
43
45
  method: string;
44
46
  params?: {
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/mcp/index.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AACnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AAqDjF,eAAO,MAAM,eAAe;;;CAGlB,CAAC;AAEX,wBAAgB,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6e9B;AAED,wBAAsB,cAAc,CAAC,SAAS,uBAA6B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAW1E"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/mcp/index.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AACnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AAsDjF,eAAO,MAAM,eAAe;;;CAGlB,CAAC;AAEX,wBAAgB,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmf1B;AAED,oCAAoC;AACpC,eAAO,MAAM,eAAe,oBAAc,CAAC;AAqB3C,wBAAsB,cAAc,CAAC,SAAS,uBAA6B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAI1E"}
package/dist/mcp/index.js CHANGED
@@ -24119,6 +24119,100 @@ var McpFinetuneStatusSchema = exports_external2.object({
24119
24119
  provider: ProviderSchema
24120
24120
  });
24121
24121
 
24122
+ // src/mcp/http.ts
24123
+ import { createServer } from "http";
24124
+ import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
24125
+ var MCP_HTTP_SERVICE_NAME = "brains";
24126
+ var DEFAULT_MCP_HTTP_PORT = 8801;
24127
+ function isHttpMode(argv = process.argv, env = process.env) {
24128
+ return argv.includes("--http") || env.MCP_HTTP === "1";
24129
+ }
24130
+ function resolveMcpHttpPort(argv = process.argv, env = process.env) {
24131
+ const portIdx = argv.indexOf("--port");
24132
+ if (portIdx !== -1 && argv[portIdx + 1]) {
24133
+ return parsePort(argv[portIdx + 1], "--port");
24134
+ }
24135
+ if (env.MCP_HTTP_PORT) {
24136
+ return parsePort(env.MCP_HTTP_PORT, "MCP_HTTP_PORT");
24137
+ }
24138
+ return DEFAULT_MCP_HTTP_PORT;
24139
+ }
24140
+ function parsePort(raw, source) {
24141
+ const parsed = Number(raw);
24142
+ if (!Number.isInteger(parsed) || parsed < 0 || parsed > 65535) {
24143
+ throw new Error(`Invalid ${source} value "${raw}". Expected 0-65535.`);
24144
+ }
24145
+ return parsed;
24146
+ }
24147
+ async function readJsonBody(req) {
24148
+ const chunks = [];
24149
+ for await (const chunk of req) {
24150
+ chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
24151
+ }
24152
+ const text2 = Buffer.concat(chunks).toString("utf8");
24153
+ if (!text2)
24154
+ return;
24155
+ return JSON.parse(text2);
24156
+ }
24157
+ async function startMcpHttpServer(buildServer, options) {
24158
+ const host = options?.host ?? "127.0.0.1";
24159
+ const requestedPort = options?.port ?? resolveMcpHttpPort();
24160
+ const serviceName = options?.serviceName ?? MCP_HTTP_SERVICE_NAME;
24161
+ const httpServer = createServer(async (req, res) => {
24162
+ try {
24163
+ const url = new URL(req.url ?? "/", `http://${req.headers.host ?? "localhost"}`);
24164
+ if (req.method === "GET" && url.pathname === "/health") {
24165
+ res.writeHead(200, { "Content-Type": "application/json" });
24166
+ res.end(JSON.stringify({ status: "ok", name: serviceName }));
24167
+ return;
24168
+ }
24169
+ if (url.pathname !== "/mcp") {
24170
+ res.writeHead(404, { "Content-Type": "text/plain" });
24171
+ res.end("Not Found");
24172
+ return;
24173
+ }
24174
+ const server = buildServer();
24175
+ const transport = new StreamableHTTPServerTransport({
24176
+ sessionIdGenerator: undefined
24177
+ });
24178
+ await server.connect(transport);
24179
+ let parsedBody;
24180
+ if (req.method === "POST") {
24181
+ parsedBody = await readJsonBody(req);
24182
+ }
24183
+ await transport.handleRequest(req, res, parsedBody);
24184
+ res.on("close", () => {
24185
+ transport.close();
24186
+ server.close();
24187
+ });
24188
+ } catch (error) {
24189
+ console.error(`[${serviceName}-mcp] HTTP error:`, error);
24190
+ if (!res.headersSent) {
24191
+ res.writeHead(500, { "Content-Type": "application/json" });
24192
+ res.end(JSON.stringify({
24193
+ jsonrpc: "2.0",
24194
+ error: { code: -32603, message: "Internal server error" },
24195
+ id: null
24196
+ }));
24197
+ }
24198
+ }
24199
+ });
24200
+ await new Promise((resolve3, reject) => {
24201
+ httpServer.once("error", reject);
24202
+ httpServer.listen(requestedPort, host, () => resolve3());
24203
+ });
24204
+ const addr = httpServer.address();
24205
+ const port = typeof addr === "object" && addr ? addr.port : requestedPort;
24206
+ console.error(`[${serviceName}-mcp] Streamable HTTP listening on http://${host}:${port}/mcp`);
24207
+ return {
24208
+ port,
24209
+ host,
24210
+ close: () => new Promise((resolve3, reject) => {
24211
+ httpServer.close((err) => err ? reject(err) : resolve3());
24212
+ })
24213
+ };
24214
+ }
24215
+
24122
24216
  // src/mcp/index.ts
24123
24217
  function getProvider(provider) {
24124
24218
  if (provider === "openai")
@@ -24148,7 +24242,7 @@ var MCP_SERVER_INFO = {
24148
24242
  name: "brains",
24149
24243
  version: getPackageVersion()
24150
24244
  };
24151
- function createMcpServer() {
24245
+ function buildServer() {
24152
24246
  const server = new Server(MCP_SERVER_INFO, { capabilities: { tools: {} } });
24153
24247
  server.setRequestHandler(ListToolsRequestSchema, async () => ({
24154
24248
  tools: [
@@ -24573,21 +24667,65 @@ function createMcpServer() {
24573
24667
  };
24574
24668
  }
24575
24669
  });
24576
- return server;
24577
- }
24578
- async function startMcpServer(transport = new StdioServerTransport) {
24579
- const server = createMcpServer();
24580
24670
  try {
24581
24671
  registerCloudTools(server, "brains");
24582
24672
  } catch {}
24673
+ return server;
24674
+ }
24675
+ var createMcpServer = buildServer;
24676
+ function hasFlag(...flags) {
24677
+ return process.argv.some((arg) => flags.includes(arg));
24678
+ }
24679
+ function printHelp() {
24680
+ process.stdout.write(`Usage: brains-mcp [options]
24681
+
24682
+ Brains MCP server (stdio transport by default)
24683
+
24684
+ Options:
24685
+ --http Serve MCP over Streamable HTTP (127.0.0.1)
24686
+ --port <number> HTTP port (default: 8801, env: MCP_HTTP_PORT)
24687
+ -h, --help Show help
24688
+ -V, --version Show version
24689
+ `);
24690
+ }
24691
+ async function startMcpServer(transport = new StdioServerTransport) {
24692
+ const server = buildServer();
24583
24693
  await server.connect(transport);
24584
24694
  return server;
24585
24695
  }
24586
- if (import.meta.main) {
24696
+ async function main() {
24697
+ if (hasFlag("--help", "-h")) {
24698
+ printHelp();
24699
+ return;
24700
+ }
24701
+ if (hasFlag("--version", "-V")) {
24702
+ process.stdout.write(`${MCP_SERVER_INFO.version}
24703
+ `);
24704
+ return;
24705
+ }
24706
+ if (isHttpMode()) {
24707
+ const handle = await startMcpHttpServer(buildServer, {
24708
+ port: resolveMcpHttpPort()
24709
+ });
24710
+ process.on("SIGINT", () => {
24711
+ handle.close().finally(() => process.exit(0));
24712
+ });
24713
+ process.on("SIGTERM", () => {
24714
+ handle.close().finally(() => process.exit(0));
24715
+ });
24716
+ return;
24717
+ }
24587
24718
  await startMcpServer();
24588
24719
  }
24720
+ if (import.meta.main) {
24721
+ main().catch((error) => {
24722
+ console.error("MCP server error:", error);
24723
+ process.exit(1);
24724
+ });
24725
+ }
24589
24726
  export {
24590
24727
  startMcpServer,
24591
24728
  createMcpServer,
24729
+ buildServer,
24592
24730
  MCP_SERVER_INFO
24593
24731
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hasna/brains",
3
- "version": "0.0.23",
3
+ "version": "0.0.24",
4
4
  "description": "Fine-tuned model tracker and trainer — wraps OpenAI + Thinker Labs, gathers training data from todos/mementos/conversations/sessions",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",