@hasna/brains 0.0.24 → 0.0.26

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.
@@ -1,16 +1,18 @@
1
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>;
2
+ export declare const DEFAULT_MCP_HTTP_PORT = 8802;
3
+ export declare const MCP_HTTP_HOST = "127.0.0.1";
4
+ export declare const MCP_SERVICE_NAME = "brains";
5
+ export declare function isHttpMode(args: string[]): boolean;
6
+ export declare function isStdioMode(args: string[]): boolean;
7
+ export declare function resolveMcpHttpPort(args: string[]): number;
8
+ export declare function healthPayload(name?: string): {
9
+ status: string;
10
+ name: string;
10
11
  };
11
- export declare function startMcpHttpServer(buildServer: () => Server, options?: {
12
- port?: number;
13
- host?: string;
14
- serviceName?: string;
15
- }): Promise<McpHttpServerHandle>;
12
+ export declare function handleMcpRequest(req: Request, buildServer: () => Server): Promise<Response>;
13
+ export declare function startMcpHttpServer(options: {
14
+ name: string;
15
+ port: number;
16
+ buildServer: () => Server;
17
+ }): ReturnType<typeof Bun.serve>;
16
18
  //# sourceMappingURL=http.d.ts.map
@@ -1 +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,GA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OnB;AAED,wBAAgB,kBAAkB,CAAC,OAAO,EAAE;IAC1C,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,MAAM,CAAC;CAC3B,GAAG,UAAU,CAAC,OAAO,GAAG,CAAC,KAAK,CAAC,CAoB/B"}
package/dist/mcp/index.js CHANGED
@@ -24120,97 +24120,52 @@ var McpFinetuneStatusSchema = exports_external2.object({
24120
24120
  });
24121
24121
 
24122
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
- }
24123
+ import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js";
24124
+ var DEFAULT_MCP_HTTP_PORT = 8802;
24125
+ var MCP_HTTP_HOST = "127.0.0.1";
24126
+ var MCP_SERVICE_NAME = "brains";
24127
+ function isStdioMode(args) {
24128
+ return args.includes("--stdio") || process.env.MCP_STDIO === "1";
24129
+ }
24130
+ function resolveMcpHttpPort(args) {
24131
+ const portIdx = args.indexOf("--port");
24132
+ if (portIdx >= 0 && args[portIdx + 1]) {
24133
+ return Number(args[portIdx + 1]);
24134
+ }
24135
+ const envPort = process.env.MCP_HTTP_PORT;
24136
+ if (envPort)
24137
+ return Number(envPort);
24138
24138
  return DEFAULT_MCP_HTTP_PORT;
24139
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;
24140
+ function healthPayload(name = MCP_SERVICE_NAME) {
24141
+ return { status: "ok", name };
24146
24142
  }
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);
24143
+ async function handleMcpRequest(req, buildServer) {
24144
+ const transport = new WebStandardStreamableHTTPServerTransport({
24145
+ sessionIdGenerator: undefined
24146
+ });
24147
+ const server = buildServer();
24148
+ await server.connect(transport);
24149
+ return transport.handleRequest(req);
24156
24150
  }
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;
24151
+ function startMcpHttpServer(options) {
24152
+ const { name, port, buildServer } = options;
24153
+ const server = Bun.serve({
24154
+ hostname: MCP_HTTP_HOST,
24155
+ port,
24156
+ async fetch(req) {
24157
+ const url = new URL(req.url);
24158
+ if (url.pathname === "/health" && req.method === "GET") {
24159
+ return Response.json(healthPayload(name));
24168
24160
  }
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
- }));
24161
+ if (url.pathname === "/mcp") {
24162
+ return handleMcpRequest(req, buildServer);
24197
24163
  }
24164
+ return new Response("Not Found", { status: 404 });
24198
24165
  }
24199
24166
  });
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
- };
24167
+ console.error(`${name}-mcp HTTP listening on http://${MCP_HTTP_HOST}:${port}/mcp`);
24168
+ return server;
24214
24169
  }
24215
24170
 
24216
24171
  // src/mcp/index.ts
@@ -24703,19 +24658,24 @@ async function main() {
24703
24658
  `);
24704
24659
  return;
24705
24660
  }
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
- });
24661
+ const args = process.argv.slice(2);
24662
+ if (isStdioMode(args)) {
24663
+ await startMcpServer();
24716
24664
  return;
24717
24665
  }
24718
- await startMcpServer();
24666
+ const handle = startMcpHttpServer({
24667
+ name: "brains",
24668
+ port: resolveMcpHttpPort(args),
24669
+ buildServer
24670
+ });
24671
+ process.on("SIGINT", () => {
24672
+ handle.stop();
24673
+ process.exit(0);
24674
+ });
24675
+ process.on("SIGTERM", () => {
24676
+ handle.stop();
24677
+ process.exit(0);
24678
+ });
24719
24679
  }
24720
24680
  if (import.meta.main) {
24721
24681
  main().catch((error) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hasna/brains",
3
- "version": "0.0.24",
3
+ "version": "0.0.26",
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",