@hasna/brains 0.0.23 → 0.0.25

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,17 @@
1
+ import type { Server } from "@modelcontextprotocol/sdk/server/index.js";
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 resolveMcpHttpPort(args: string[]): number;
7
+ export declare function healthPayload(name?: string): {
8
+ status: string;
9
+ name: string;
10
+ };
11
+ export declare function handleMcpRequest(req: Request, buildServer: () => Server): Promise<Response>;
12
+ export declare function startMcpHttpServer(options: {
13
+ name: string;
14
+ port: number;
15
+ buildServer: () => Server;
16
+ }): ReturnType<typeof Bun.serve>;
17
+ //# sourceMappingURL=http.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/mcp/http.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AAExE,eAAO,MAAM,qBAAqB,OAAO,CAAC;AAC1C,eAAO,MAAM,aAAa,cAAc,CAAC;AACzC,eAAO,MAAM,gBAAgB,WAAW,CAAC;AAEzC,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAElD;AAED,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAQzD;AAED,wBAAgB,aAAa,CAAC,IAAI,GAAE,MAAyB,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAE/F;AAED,wBAAsB,gBAAgB,CACpC,GAAG,EAAE,OAAO,EACZ,WAAW,EAAE,MAAM,MAAM,GACxB,OAAO,CAAC,QAAQ,CAAC,CAOnB;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"}
@@ -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,55 @@ var McpFinetuneStatusSchema = exports_external2.object({
24119
24119
  provider: ProviderSchema
24120
24120
  });
24121
24121
 
24122
+ // src/mcp/http.ts
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 isHttpMode(args) {
24128
+ return args.includes("--http") || process.env.MCP_HTTP === "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
+ return DEFAULT_MCP_HTTP_PORT;
24139
+ }
24140
+ function healthPayload(name = MCP_SERVICE_NAME) {
24141
+ return { status: "ok", name };
24142
+ }
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);
24150
+ }
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));
24160
+ }
24161
+ if (url.pathname === "/mcp") {
24162
+ return handleMcpRequest(req, buildServer);
24163
+ }
24164
+ return new Response("Not Found", { status: 404 });
24165
+ }
24166
+ });
24167
+ console.error(`${name}-mcp HTTP listening on http://${MCP_HTTP_HOST}:${port}/mcp`);
24168
+ return server;
24169
+ }
24170
+
24122
24171
  // src/mcp/index.ts
24123
24172
  function getProvider(provider) {
24124
24173
  if (provider === "openai")
@@ -24148,7 +24197,7 @@ var MCP_SERVER_INFO = {
24148
24197
  name: "brains",
24149
24198
  version: getPackageVersion()
24150
24199
  };
24151
- function createMcpServer() {
24200
+ function buildServer() {
24152
24201
  const server = new Server(MCP_SERVER_INFO, { capabilities: { tools: {} } });
24153
24202
  server.setRequestHandler(ListToolsRequestSchema, async () => ({
24154
24203
  tools: [
@@ -24573,21 +24622,70 @@ function createMcpServer() {
24573
24622
  };
24574
24623
  }
24575
24624
  });
24576
- return server;
24577
- }
24578
- async function startMcpServer(transport = new StdioServerTransport) {
24579
- const server = createMcpServer();
24580
24625
  try {
24581
24626
  registerCloudTools(server, "brains");
24582
24627
  } catch {}
24628
+ return server;
24629
+ }
24630
+ var createMcpServer = buildServer;
24631
+ function hasFlag(...flags) {
24632
+ return process.argv.some((arg) => flags.includes(arg));
24633
+ }
24634
+ function printHelp() {
24635
+ process.stdout.write(`Usage: brains-mcp [options]
24636
+
24637
+ Brains MCP server (stdio transport by default)
24638
+
24639
+ Options:
24640
+ --http Serve MCP over Streamable HTTP (127.0.0.1)
24641
+ --port <number> HTTP port (default: 8801, env: MCP_HTTP_PORT)
24642
+ -h, --help Show help
24643
+ -V, --version Show version
24644
+ `);
24645
+ }
24646
+ async function startMcpServer(transport = new StdioServerTransport) {
24647
+ const server = buildServer();
24583
24648
  await server.connect(transport);
24584
24649
  return server;
24585
24650
  }
24586
- if (import.meta.main) {
24651
+ async function main() {
24652
+ if (hasFlag("--help", "-h")) {
24653
+ printHelp();
24654
+ return;
24655
+ }
24656
+ if (hasFlag("--version", "-V")) {
24657
+ process.stdout.write(`${MCP_SERVER_INFO.version}
24658
+ `);
24659
+ return;
24660
+ }
24661
+ const args = process.argv.slice(2);
24662
+ if (isHttpMode(args)) {
24663
+ const handle = startMcpHttpServer({
24664
+ name: "brains",
24665
+ port: resolveMcpHttpPort(args),
24666
+ buildServer
24667
+ });
24668
+ process.on("SIGINT", () => {
24669
+ handle.stop();
24670
+ process.exit(0);
24671
+ });
24672
+ process.on("SIGTERM", () => {
24673
+ handle.stop();
24674
+ process.exit(0);
24675
+ });
24676
+ return;
24677
+ }
24587
24678
  await startMcpServer();
24588
24679
  }
24680
+ if (import.meta.main) {
24681
+ main().catch((error) => {
24682
+ console.error("MCP server error:", error);
24683
+ process.exit(1);
24684
+ });
24685
+ }
24589
24686
  export {
24590
24687
  startMcpServer,
24591
24688
  createMcpServer,
24689
+ buildServer,
24592
24690
  MCP_SERVER_INFO
24593
24691
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hasna/brains",
3
- "version": "0.0.23",
3
+ "version": "0.0.25",
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",