@b3os/mcp 0.0.0-stage → 0.0.1
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/LICENSE +202 -0
- package/README.md +76 -2
- package/bin/b3os-mcp.js +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +29 -0
- package/dist/main.js.map +1 -0
- package/dist/server.d.ts +61 -0
- package/dist/server.js +226 -0
- package/dist/server.js.map +1 -0
- package/dist/stdio.d.ts +17 -0
- package/dist/stdio.js +81 -0
- package/dist/stdio.js.map +1 -0
- package/dist/tools.d.ts +56 -0
- package/dist/tools.js +741 -0
- package/dist/tools.js.map +1 -0
- package/package.json +55 -4
- package/src/index.ts +27 -0
- package/src/main.ts +30 -0
- package/src/server.ts +270 -0
- package/src/stdio.ts +86 -0
- package/src/tools.ts +844 -0
package/dist/stdio.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { B3OS } from "@b3os/sdk";
|
|
2
|
+
import { type McpServerOptions } from "./server.ts";
|
|
3
|
+
export interface StdioOptions extends Partial<McpServerOptions> {
|
|
4
|
+
input?: NodeJS.ReadableStream;
|
|
5
|
+
output?: {
|
|
6
|
+
write(s: string): unknown;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
/** The `b3os login` token, when there is one (same file as the CLI: $XDG_CONFIG_HOME/b3os/credentials.json). */
|
|
10
|
+
export declare function storedLogin(): {
|
|
11
|
+
token: string;
|
|
12
|
+
base_url?: string;
|
|
13
|
+
} | undefined;
|
|
14
|
+
/** A client from B3OS_API_KEY (or the node socket / sandbox URL), falling back to the stored CLI login. */
|
|
15
|
+
export declare function defaultClient(): B3OS;
|
|
16
|
+
/** Serves MCP over stdin/stdout until stdin ends. */
|
|
17
|
+
export declare function serveStdio(opts?: StdioOptions): Promise<void>;
|
package/dist/stdio.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// The stdio transport: newline-delimited JSON-RPC on stdin/stdout, logs on stderr only (MCP stdio rules).
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { B3OS, DEFAULT_BASE_URL } from "@b3os/sdk";
|
|
6
|
+
import { McpServer } from "./server.js";
|
|
7
|
+
/** The `b3os login` token, when there is one (same file as the CLI: $XDG_CONFIG_HOME/b3os/credentials.json). */
|
|
8
|
+
export function storedLogin() {
|
|
9
|
+
const file = process.env.B3OS_CREDENTIALS_FILE ||
|
|
10
|
+
join(process.env.XDG_CONFIG_HOME || join(homedir(), ".config"), "b3os", "credentials.json");
|
|
11
|
+
try {
|
|
12
|
+
const c = JSON.parse(readFileSync(file, "utf8"));
|
|
13
|
+
if (!c.token || (c.expires_at && Date.parse(c.expires_at) < Date.now()))
|
|
14
|
+
return undefined;
|
|
15
|
+
return { token: c.token, base_url: c.base_url };
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** A client from B3OS_API_KEY (or the node socket / sandbox URL), falling back to the stored CLI login. */
|
|
22
|
+
export function defaultClient() {
|
|
23
|
+
if (process.env.B3OS_API_KEY)
|
|
24
|
+
return new B3OS();
|
|
25
|
+
const login = storedLogin();
|
|
26
|
+
if (!login)
|
|
27
|
+
return new B3OS();
|
|
28
|
+
// Pass a base URL only when one was chosen, as the SDK reads "a base URL is configured" as a routing decision.
|
|
29
|
+
const stored = login.base_url?.replace(/\/+$/, "");
|
|
30
|
+
const baseUrl = process.env.B3OS_BASE_URL ?? (stored && stored !== DEFAULT_BASE_URL ? stored : undefined);
|
|
31
|
+
return new B3OS(baseUrl ? { apiKey: login.token, baseUrl } : { apiKey: login.token });
|
|
32
|
+
}
|
|
33
|
+
/** Serves MCP over stdin/stdout until stdin ends. */
|
|
34
|
+
export async function serveStdio(opts = {}) {
|
|
35
|
+
const client = opts.client ?? defaultClient();
|
|
36
|
+
const server = new McpServer({ ...opts, client });
|
|
37
|
+
const input = opts.input ?? process.stdin;
|
|
38
|
+
const output = opts.output ?? process.stdout;
|
|
39
|
+
const send = (m) => {
|
|
40
|
+
output.write(`${JSON.stringify(m)}\n`);
|
|
41
|
+
};
|
|
42
|
+
const pending = new Set();
|
|
43
|
+
let buf = "";
|
|
44
|
+
const dispatch = (line) => {
|
|
45
|
+
const t = line.trim();
|
|
46
|
+
if (!t)
|
|
47
|
+
return;
|
|
48
|
+
let msg;
|
|
49
|
+
try {
|
|
50
|
+
msg = JSON.parse(t);
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
send({ jsonrpc: "2.0", id: null, error: { code: -32700, message: "Parse error" } });
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const items = Array.isArray(msg) ? msg : [msg];
|
|
57
|
+
for (const item of items) {
|
|
58
|
+
const p = server.handle(item, send).catch((e) => {
|
|
59
|
+
process.stderr.write(`b3os-mcp: ${e?.stack ?? e}\n`);
|
|
60
|
+
});
|
|
61
|
+
pending.add(p);
|
|
62
|
+
p.finally(() => pending.delete(p));
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
input.setEncoding?.("utf8");
|
|
66
|
+
for await (const chunk of input) {
|
|
67
|
+
buf += typeof chunk === "string" ? chunk : chunk.toString("utf8");
|
|
68
|
+
let nl = buf.indexOf("\n");
|
|
69
|
+
while (nl >= 0) {
|
|
70
|
+
dispatch(buf.slice(0, nl));
|
|
71
|
+
buf = buf.slice(nl + 1);
|
|
72
|
+
nl = buf.indexOf("\n");
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (buf.trim())
|
|
76
|
+
dispatch(buf);
|
|
77
|
+
await Promise.allSettled([...pending]);
|
|
78
|
+
if (!opts.client)
|
|
79
|
+
client.close();
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=stdio.js.map
|
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CAAC,CAAC;QAC3B,OAAO,EAAE,IAAI,CAAC,EAAE,CAAC;YACf,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;YAC3B,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC;YACxB,EAAE,GAAG,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACzB,CAAC;IACH,CAAC;IACD,IAAI,GAAG,CAAC,IAAI,EAAE;QAAE,QAAQ,CAAC,GAAG,CAAC,CAAC;IAC9B,MAAM,OAAO,CAAC,UAAU,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IACvC,IAAI,CAAC,IAAI,CAAC,MAAM;QAAE,MAAM,CAAC,KAAK,EAAE,CAAC;AACnC,CAAC"}
|
package/dist/tools.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type ActionDescription, type B3OS } from "@b3os/sdk";
|
|
2
|
+
export interface JsonSchemaObject {
|
|
3
|
+
type: "object";
|
|
4
|
+
properties: Record<string, unknown>;
|
|
5
|
+
required?: string[];
|
|
6
|
+
additionalProperties?: boolean;
|
|
7
|
+
[k: string]: unknown;
|
|
8
|
+
}
|
|
9
|
+
export interface ToolAnnotations {
|
|
10
|
+
title?: string;
|
|
11
|
+
readOnlyHint?: boolean;
|
|
12
|
+
destructiveHint?: boolean;
|
|
13
|
+
idempotentHint?: boolean;
|
|
14
|
+
openWorldHint?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export type ContentBlock = {
|
|
17
|
+
type: "text";
|
|
18
|
+
text: string;
|
|
19
|
+
} | {
|
|
20
|
+
type: "image";
|
|
21
|
+
data: string;
|
|
22
|
+
mimeType: string;
|
|
23
|
+
} | {
|
|
24
|
+
type: "resource_link";
|
|
25
|
+
uri: string;
|
|
26
|
+
name: string;
|
|
27
|
+
mimeType?: string;
|
|
28
|
+
description?: string;
|
|
29
|
+
};
|
|
30
|
+
export interface ToolResult {
|
|
31
|
+
content: ContentBlock[];
|
|
32
|
+
structuredContent?: Record<string, unknown>;
|
|
33
|
+
isError?: boolean;
|
|
34
|
+
}
|
|
35
|
+
export interface ToolContext {
|
|
36
|
+
signal?: AbortSignal;
|
|
37
|
+
/** Reports progress on long tools (MCP notifications/progress when the client sent a progressToken). */
|
|
38
|
+
progress?: (completed: number, total?: number, message?: string) => void;
|
|
39
|
+
}
|
|
40
|
+
export interface ToolDef {
|
|
41
|
+
name: string;
|
|
42
|
+
title: string;
|
|
43
|
+
description: string;
|
|
44
|
+
inputSchema: JsonSchemaObject;
|
|
45
|
+
annotations: ToolAnnotations;
|
|
46
|
+
call(b3: B3OS, args: Record<string, unknown>, ctx: ToolContext): Promise<ToolResult>;
|
|
47
|
+
}
|
|
48
|
+
/** A failed call as an MCP tool error the model can read and correct (never a protocol error). */
|
|
49
|
+
export declare function errorResult(e: unknown): ToolResult;
|
|
50
|
+
/** The core tool set, in a fixed order (deterministic tools/list for prompt caching). */
|
|
51
|
+
export declare const coreTools: readonly ToolDef[];
|
|
52
|
+
/** One tool per action of the given packs (`github`, or `io.b3os/github`), named by the registry's `mcp_name`. */
|
|
53
|
+
export declare function packTools(b3: B3OS, packs: readonly string[]): Promise<ToolDef[]>;
|
|
54
|
+
export declare function actionTool(d: ActionDescription): ToolDef;
|
|
55
|
+
export declare function toBase64(bytes: Uint8Array): string;
|
|
56
|
+
export declare function fromBase64(s: string): Uint8Array;
|