@bitbybit-dev/mcp 1.3.2-rc.0
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 +21 -0
- package/README.md +143 -0
- package/dist/context.d.ts +19 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +28 -0
- package/dist/context.js.map +1 -0
- package/dist/descriptions.d.ts +10 -0
- package/dist/descriptions.d.ts.map +1 -0
- package/dist/descriptions.js +48 -0
- package/dist/descriptions.js.map +1 -0
- package/dist/guides-split.d.ts +9 -0
- package/dist/guides-split.d.ts.map +1 -0
- package/dist/guides-split.js +41 -0
- package/dist/guides-split.js.map +1 -0
- package/dist/guides.generated.d.ts +9 -0
- package/dist/guides.generated.d.ts.map +1 -0
- package/dist/guides.generated.js +93 -0
- package/dist/guides.generated.js.map +1 -0
- package/dist/index-loader.d.ts +11 -0
- package/dist/index-loader.d.ts.map +1 -0
- package/dist/index-loader.js +53 -0
- package/dist/index-loader.js.map +1 -0
- package/dist/index-reader.d.ts +27 -0
- package/dist/index-reader.d.ts.map +1 -0
- package/dist/index-reader.js +171 -0
- package/dist/index-reader.js.map +1 -0
- package/dist/index-shape.d.ts +3 -0
- package/dist/index-shape.d.ts.map +1 -0
- package/dist/index-shape.js +17 -0
- package/dist/index-shape.js.map +1 -0
- package/dist/index-types.d.ts +72 -0
- package/dist/index-types.d.ts.map +1 -0
- package/dist/index-types.js +2 -0
- package/dist/index-types.js.map +1 -0
- package/dist/index-url.d.ts +11 -0
- package/dist/index-url.d.ts.map +1 -0
- package/dist/index-url.js +30 -0
- package/dist/index-url.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/installed-version.d.ts +18 -0
- package/dist/installed-version.d.ts.map +1 -0
- package/dist/installed-version.js +67 -0
- package/dist/installed-version.js.map +1 -0
- package/dist/json-schema.d.ts +2 -0
- package/dist/json-schema.d.ts.map +1 -0
- package/dist/json-schema.js +46 -0
- package/dist/json-schema.js.map +1 -0
- package/dist/registry.d.ts +56 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +78 -0
- package/dist/registry.js.map +1 -0
- package/dist/render.d.ts +4 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/render.js +76 -0
- package/dist/render.js.map +1 -0
- package/dist/results.d.ts +5 -0
- package/dist/results.d.ts.map +1 -0
- package/dist/results.js +18 -0
- package/dist/results.js.map +1 -0
- package/dist/server.d.ts +22 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +69 -0
- package/dist/server.js.map +1 -0
- package/dist/stdio.d.ts +3 -0
- package/dist/stdio.d.ts.map +1 -0
- package/dist/stdio.js +64 -0
- package/dist/stdio.js.map +1 -0
- package/dist/tools/describe.d.ts +10 -0
- package/dist/tools/describe.d.ts.map +1 -0
- package/dist/tools/describe.js +39 -0
- package/dist/tools/describe.js.map +1 -0
- package/dist/tools/fetch.d.ts +9 -0
- package/dist/tools/fetch.d.ts.map +1 -0
- package/dist/tools/fetch.js +37 -0
- package/dist/tools/fetch.js.map +1 -0
- package/dist/tools/get-examples.d.ts +17 -0
- package/dist/tools/get-examples.d.ts.map +1 -0
- package/dist/tools/get-examples.js +60 -0
- package/dist/tools/get-examples.js.map +1 -0
- package/dist/tools/get-guide.d.ts +10 -0
- package/dist/tools/get-guide.d.ts.map +1 -0
- package/dist/tools/get-guide.js +64 -0
- package/dist/tools/get-guide.js.map +1 -0
- package/dist/tools/index.d.ts +4 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +19 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/list-namespace.d.ts +10 -0
- package/dist/tools/list-namespace.d.ts.map +1 -0
- package/dist/tools/list-namespace.js +41 -0
- package/dist/tools/list-namespace.js.map +1 -0
- package/dist/tools/search-api.d.ts +16 -0
- package/dist/tools/search-api.d.ts.map +1 -0
- package/dist/tools/search-api.js +44 -0
- package/dist/tools/search-api.js.map +1 -0
- package/dist/tools/search.d.ts +9 -0
- package/dist/tools/search.d.ts.map +1 -0
- package/dist/tools/search.js +31 -0
- package/dist/tools/search.js.map +1 -0
- package/package.json +102 -0
- package/server.json +30 -0
- package/src/__fixtures__/index.sample.json +1916 -0
- package/src/__fixtures__/load.ts +29 -0
- package/src/context.ts +40 -0
- package/src/dependencies.test.ts +72 -0
- package/src/descriptions.ts +62 -0
- package/src/guides-split.ts +46 -0
- package/src/guides.generated.ts +95 -0
- package/src/guides.test.ts +57 -0
- package/src/index-loader.test.ts +160 -0
- package/src/index-loader.ts +61 -0
- package/src/index-reader.ts +179 -0
- package/src/index-shape.ts +17 -0
- package/src/index-types.ts +74 -0
- package/src/index-url.ts +29 -0
- package/src/index.ts +20 -0
- package/src/installed-version.test.ts +79 -0
- package/src/installed-version.ts +80 -0
- package/src/json-schema.test.ts +65 -0
- package/src/json-schema.ts +40 -0
- package/src/network.test.ts +37 -0
- package/src/registry.test.ts +177 -0
- package/src/registry.ts +139 -0
- package/src/render.ts +62 -0
- package/src/results.ts +21 -0
- package/src/server.test.ts +233 -0
- package/src/server.ts +117 -0
- package/src/stdio.test.ts +100 -0
- package/src/stdio.ts +67 -0
- package/src/tools/describe.ts +42 -0
- package/src/tools/docs-tools.test.ts +375 -0
- package/src/tools/fetch.ts +39 -0
- package/src/tools/get-examples.ts +59 -0
- package/src/tools/get-guide.ts +69 -0
- package/src/tools/index.ts +20 -0
- package/src/tools/list-namespace.ts +50 -0
- package/src/tools/search-api.ts +47 -0
- package/src/tools/search.ts +34 -0
package/src/server.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { McpServer, createMcpHandler } from "@modelcontextprotocol/server";
|
|
2
|
+
import type { AuthInfo, StandardSchemaWithJSON } from "@modelcontextprotocol/server";
|
|
3
|
+
import type { z } from "zod";
|
|
4
|
+
import { inputJsonSchema } from "./registry.js";
|
|
5
|
+
import type { AnyToolDefinition, Registry, ToolInput, ToolResult } from "./registry.js";
|
|
6
|
+
|
|
7
|
+
export interface McpServerOptions<TContext> {
|
|
8
|
+
name: string;
|
|
9
|
+
version: string;
|
|
10
|
+
instructions?: string;
|
|
11
|
+
filter?: (definition: AnyToolDefinition<TContext>) => boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface RequestHandlerOptions<TContext> {
|
|
15
|
+
name: string;
|
|
16
|
+
version: string;
|
|
17
|
+
instructions?: string;
|
|
18
|
+
filterFor?: (context: TContext) => (definition: AnyToolDefinition<TContext>) => boolean;
|
|
19
|
+
onError?: (error: Error) => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface RequestHandler<TContext> {
|
|
23
|
+
fetch(request: Request, context: TContext): Promise<Response>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const REQUEST_CONTEXT = "dev.bitbybit/request-context";
|
|
27
|
+
|
|
28
|
+
type TextContent = { type: "text"; text: string };
|
|
29
|
+
type LinkContent = { type: "resource_link"; uri: string; name: string; mimeType?: string; description?: string };
|
|
30
|
+
|
|
31
|
+
function toCallToolResult(result: ToolResult): { content: (TextContent | LinkContent)[]; structuredContent?: Record<string, unknown>; isError?: boolean } {
|
|
32
|
+
const links: LinkContent[] = (result.links ?? []).map((link) => ({
|
|
33
|
+
type: "resource_link",
|
|
34
|
+
uri: link.uri,
|
|
35
|
+
name: link.name,
|
|
36
|
+
...(link.mimeType === undefined ? {} : { mimeType: link.mimeType }),
|
|
37
|
+
...(link.description === undefined ? {} : { description: link.description }),
|
|
38
|
+
}));
|
|
39
|
+
return {
|
|
40
|
+
content: [{ type: "text", text: result.text }, ...links],
|
|
41
|
+
...(result.structured ? { structuredContent: result.structured } : {}),
|
|
42
|
+
...(result.isError ? { isError: true } : {}),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function advertised<TInput extends ToolInput>(input: TInput): StandardSchemaWithJSON<z.input<TInput>, z.output<TInput>> {
|
|
47
|
+
const json = inputJsonSchema(input);
|
|
48
|
+
const standard = input["~standard"] as StandardSchemaWithJSON<z.input<TInput>, z.output<TInput>>["~standard"];
|
|
49
|
+
return { "~standard": { ...standard, jsonSchema: { input: () => json, output: () => json } } };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function bindRegistry<TContext>(
|
|
53
|
+
server: McpServer,
|
|
54
|
+
registry: Registry<TContext>,
|
|
55
|
+
context: TContext,
|
|
56
|
+
filter?: (definition: AnyToolDefinition<TContext>) => boolean,
|
|
57
|
+
): string[] {
|
|
58
|
+
const bound: string[] = [];
|
|
59
|
+
for (const definition of registry.list()) {
|
|
60
|
+
const { handler } = definition;
|
|
61
|
+
if (!handler || (filter && !filter(definition))) continue;
|
|
62
|
+
const { annotations } = definition;
|
|
63
|
+
server.registerTool(
|
|
64
|
+
definition.name,
|
|
65
|
+
{
|
|
66
|
+
title: annotations.title,
|
|
67
|
+
description: definition.description,
|
|
68
|
+
inputSchema: advertised(definition.input),
|
|
69
|
+
annotations: {
|
|
70
|
+
title: annotations.title,
|
|
71
|
+
readOnlyHint: annotations.readOnlyHint,
|
|
72
|
+
destructiveHint: annotations.destructiveHint,
|
|
73
|
+
...(annotations.idempotentHint === undefined ? {} : { idempotentHint: annotations.idempotentHint }),
|
|
74
|
+
...(annotations.openWorldHint === undefined ? {} : { openWorldHint: annotations.openWorldHint }),
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
async (args) => toCallToolResult(await handler(args, context)),
|
|
78
|
+
);
|
|
79
|
+
bound.push(definition.name);
|
|
80
|
+
}
|
|
81
|
+
return bound;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function createMcpServer<TContext>(registry: Registry<TContext>, context: TContext, options: McpServerOptions<TContext>): McpServer {
|
|
85
|
+
const server = new McpServer(
|
|
86
|
+
{ name: options.name, version: options.version },
|
|
87
|
+
options.instructions === undefined ? {} : { instructions: options.instructions },
|
|
88
|
+
);
|
|
89
|
+
bindRegistry(server, registry, context, options.filter);
|
|
90
|
+
return server;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function carrying<TContext>(context: TContext): AuthInfo {
|
|
94
|
+
return { token: "", clientId: "", scopes: [], extra: { [REQUEST_CONTEXT]: context } };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function carried<TContext>(authInfo: AuthInfo | undefined): TContext {
|
|
98
|
+
const context = authInfo?.extra?.[REQUEST_CONTEXT];
|
|
99
|
+
if (context === undefined) throw new Error("The request carries no context; serve it through the request handler's own fetch");
|
|
100
|
+
return context as TContext;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function createRequestHandler<TContext>(registry: Registry<TContext>, options: RequestHandlerOptions<TContext>): RequestHandler<TContext> {
|
|
104
|
+
const handler = createMcpHandler(
|
|
105
|
+
({ authInfo }) => {
|
|
106
|
+
const context = carried<TContext>(authInfo);
|
|
107
|
+
const server = new McpServer(
|
|
108
|
+
{ name: options.name, version: options.version },
|
|
109
|
+
options.instructions === undefined ? {} : { instructions: options.instructions },
|
|
110
|
+
);
|
|
111
|
+
bindRegistry(server, registry, context, options.filterFor?.(context));
|
|
112
|
+
return server;
|
|
113
|
+
},
|
|
114
|
+
{ legacy: "stateless", responseMode: "json", ...(options.onError === undefined ? {} : { onerror: options.onError }) },
|
|
115
|
+
);
|
|
116
|
+
return { fetch: (request, context) => handler.fetch(request, { authInfo: carrying(context) }) };
|
|
117
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { execFileSync, spawn } from "node:child_process";
|
|
2
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
6
|
+
import { FIXTURE_VERSION, fixtureIndex } from "./__fixtures__/load.js";
|
|
7
|
+
|
|
8
|
+
const PACKAGE_DIRECTORY = join(import.meta.dirname, "..");
|
|
9
|
+
const ENTRY = join(PACKAGE_DIRECTORY, "dist", "stdio.js");
|
|
10
|
+
const OWN_VERSION = (JSON.parse(readFileSync(join(PACKAGE_DIRECTORY, "package.json"), "utf8")) as { version: string }).version;
|
|
11
|
+
const BUILD_TIMEOUT_MS = 60_000;
|
|
12
|
+
const SESSION_TIMEOUT_MS = 8_000;
|
|
13
|
+
|
|
14
|
+
interface Session {
|
|
15
|
+
readonly stdout: string[];
|
|
16
|
+
readonly stderr: string;
|
|
17
|
+
readonly exitCode: number | null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let cacheHome = "";
|
|
21
|
+
|
|
22
|
+
function newestSourceChange(): number {
|
|
23
|
+
const sources = readdirSync(join(PACKAGE_DIRECTORY, "src"), { recursive: true, withFileTypes: true })
|
|
24
|
+
.filter((entry) => entry.isFile() && entry.name.endsWith(".ts") && !entry.name.endsWith(".test.ts"))
|
|
25
|
+
.map((entry) => statSync(join(entry.parentPath, entry.name)).mtimeMs);
|
|
26
|
+
return Math.max(...sources);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function buildIsStale(): boolean {
|
|
30
|
+
return !existsSync(ENTRY) || statSync(ENTRY).mtimeMs < newestSourceChange();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
beforeAll(() => {
|
|
34
|
+
if (buildIsStale()) execFileSync("npx", ["tsc", "-p", "tsconfig.build.json"], { cwd: PACKAGE_DIRECTORY, stdio: "ignore", timeout: BUILD_TIMEOUT_MS });
|
|
35
|
+
cacheHome = mkdtempSync(join(tmpdir(), "bitbybit-mcp-stdio-"));
|
|
36
|
+
mkdirSync(join(cacheHome, "bitbybit-mcp"), { recursive: true });
|
|
37
|
+
writeFileSync(join(cacheHome, "bitbybit-mcp", `index-v${FIXTURE_VERSION}.json`), JSON.stringify(fixtureIndex()), "utf8");
|
|
38
|
+
}, BUILD_TIMEOUT_MS);
|
|
39
|
+
|
|
40
|
+
afterAll(() => {
|
|
41
|
+
if (cacheHome) rmSync(cacheHome, { recursive: true, force: true });
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
function run(args: string[], messages: object[]): Promise<Session> {
|
|
45
|
+
return new Promise((resolve, reject) => {
|
|
46
|
+
const child = spawn(process.execPath, [ENTRY, ...args], { env: { ...process.env, XDG_CACHE_HOME: cacheHome, BITBYBIT_VERSION: "" }, stdio: ["pipe", "pipe", "pipe"] });
|
|
47
|
+
let out = "";
|
|
48
|
+
let err = "";
|
|
49
|
+
const timer = setTimeout(() => { child.kill(); reject(new Error(`no exit within ${SESSION_TIMEOUT_MS} ms; stdout: ${out}; stderr: ${err}`)); }, SESSION_TIMEOUT_MS);
|
|
50
|
+
child.stdout.on("data", (chunk: Buffer) => { out += chunk.toString(); });
|
|
51
|
+
child.stderr.on("data", (chunk: Buffer) => { err += chunk.toString(); });
|
|
52
|
+
child.on("close", (code) => { clearTimeout(timer); resolve({ stdout: out.split("\n").filter((line) => line !== ""), stderr: err, exitCode: code }); });
|
|
53
|
+
child.on("error", reject);
|
|
54
|
+
const expected = messages.filter((message) => "id" in message).length;
|
|
55
|
+
let answered = 0;
|
|
56
|
+
child.stdout.on("data", () => {
|
|
57
|
+
answered = out.split("\n").filter((line) => line.includes("\"result\"")).length;
|
|
58
|
+
if (answered >= expected) child.stdin.end();
|
|
59
|
+
});
|
|
60
|
+
for (const message of messages) child.stdin.write(`${JSON.stringify(message)}\n`);
|
|
61
|
+
if (expected === 0) child.stdin.end();
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const INITIALIZE = { jsonrpc: "2.0", id: 1, method: "initialize", params: { protocolVersion: "2025-06-18", capabilities: {}, clientInfo: { name: "test", version: "0.0.0" } } };
|
|
66
|
+
const INITIALIZED = { jsonrpc: "2.0", method: "notifications/initialized" };
|
|
67
|
+
const LIST_TOOLS = { jsonrpc: "2.0", id: 2, method: "tools/list", params: {} };
|
|
68
|
+
|
|
69
|
+
describe("the stdio entry", () => {
|
|
70
|
+
it("serves a cached version over stdio with nothing but JSON-RPC on stdout, and exits when stdin closes", async () => {
|
|
71
|
+
// Act
|
|
72
|
+
const session = await run(["--version", FIXTURE_VERSION], [INITIALIZE, INITIALIZED, LIST_TOOLS]);
|
|
73
|
+
|
|
74
|
+
// Assert
|
|
75
|
+
expect(session.exitCode).toBe(0);
|
|
76
|
+
expect(session.stdout.map((line) => (JSON.parse(line) as { jsonrpc: string }).jsonrpc)).toEqual(["2.0", "2.0"]);
|
|
77
|
+
expect(session.stderr).toContain(`serving the Bitbybit API index for version ${FIXTURE_VERSION} (from flag)`);
|
|
78
|
+
const listed = JSON.parse(session.stdout[1] ?? "{}") as { result: { tools: { name: string }[] } };
|
|
79
|
+
expect(listed.result.tools.map((tool) => tool.name)).toContain("describe");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("prints its own version on stdout for a bare --version and exits", async () => {
|
|
83
|
+
// Act
|
|
84
|
+
const session = await run(["--version"], []);
|
|
85
|
+
|
|
86
|
+
// Assert
|
|
87
|
+
expect(session.exitCode).toBe(0);
|
|
88
|
+
expect(session.stdout).toEqual([OWN_VERSION]);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("prints the help on stderr, leaving stdout empty", async () => {
|
|
92
|
+
// Act
|
|
93
|
+
const session = await run(["--help"], []);
|
|
94
|
+
|
|
95
|
+
// Assert
|
|
96
|
+
expect(session.exitCode).toBe(0);
|
|
97
|
+
expect(session.stdout).toEqual([]);
|
|
98
|
+
expect(session.stderr).toContain("bitbybit-mcp");
|
|
99
|
+
});
|
|
100
|
+
});
|
package/src/stdio.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { StdioServerTransport } from "@modelcontextprotocol/server/stdio";
|
|
4
|
+
import { createDocsRegistry } from "./tools/index.js";
|
|
5
|
+
import { createMcpServer } from "./server.js";
|
|
6
|
+
import { guarded } from "./registry.js";
|
|
7
|
+
import { contextForIndex } from "./context.js";
|
|
8
|
+
import { defaultCacheDir, IndexNotPublishedError, loadIndex } from "./index-loader.js";
|
|
9
|
+
import { detectVersion, flagValue } from "./installed-version.js";
|
|
10
|
+
import type { DetectedVersion } from "./installed-version.js";
|
|
11
|
+
import type { ApiIndex } from "./index-types.js";
|
|
12
|
+
import { GUIDE_PAGE_URL, GUIDES } from "./guides.generated.js";
|
|
13
|
+
import { SERVER_INSTRUCTIONS } from "./descriptions.js";
|
|
14
|
+
|
|
15
|
+
const HELP = `bitbybit-mcp: an MCP server (stdio) documenting the Bitbybit 3D CAD API for one exact version.
|
|
16
|
+
|
|
17
|
+
npx -y @bitbybit-dev/mcp serve the version installed around the working directory, else this package's version
|
|
18
|
+
npx -y @bitbybit-dev/mcp --version <version> serve that exact version
|
|
19
|
+
BITBYBIT_VERSION=<version> npx -y @bitbybit-dev/mcp
|
|
20
|
+
npx -y @bitbybit-dev/mcp --version print this package's version and exit
|
|
21
|
+
--no-cache do not read or write ~/.cache/bitbybit-mcp
|
|
22
|
+
|
|
23
|
+
When the installed version predates the published index, this package's own version is served
|
|
24
|
+
instead and a note says so. Everything this program prints goes to stderr; stdout is the MCP transport.`;
|
|
25
|
+
|
|
26
|
+
function ownVersion(): string {
|
|
27
|
+
const manifest: unknown = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
28
|
+
if (typeof manifest === "object" && manifest !== null && "version" in manifest && typeof manifest.version === "string") return manifest.version;
|
|
29
|
+
throw new Error("The package manifest carries no version");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function loadServed(detected: DetectedVersion, own: string, cacheDir: string | null): Promise<{ index: ApiIndex; served: DetectedVersion }> {
|
|
33
|
+
try {
|
|
34
|
+
return { index: await loadIndex({ version: detected.version, cacheDir }), served: detected };
|
|
35
|
+
} catch (error) {
|
|
36
|
+
if (!(error instanceof IndexNotPublishedError) || detected.source !== "installed" || detected.version === own) throw error;
|
|
37
|
+
console.error(`bitbybit-mcp: ${error.message}`);
|
|
38
|
+
console.error(`bitbybit-mcp: serving this package's version ${own} instead; answers describe ${own}, not the installed ${detected.version}`);
|
|
39
|
+
const served: DetectedVersion = { version: own, source: "own" };
|
|
40
|
+
return { index: await loadIndex({ version: own, cacheDir }), served };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function main(): Promise<void> {
|
|
45
|
+
const argv = process.argv.slice(2);
|
|
46
|
+
if (argv.includes("--help") || argv.includes("-h")) {
|
|
47
|
+
console.error(HELP);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const own = ownVersion();
|
|
51
|
+
if (argv.includes("--version") && flagValue(argv, "--version") === undefined) {
|
|
52
|
+
console.log(own);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const detected = detectVersion({ ownVersion: own, cwd: process.cwd(), env: process.env, argv, warn: (message) => console.error(`bitbybit-mcp: ${message}`) });
|
|
56
|
+
const { index, served } = await loadServed(detected, own, argv.includes("--no-cache") ? null : defaultCacheDir());
|
|
57
|
+
const context = contextForIndex(index, GUIDES, GUIDE_PAGE_URL);
|
|
58
|
+
const registry = guarded(createDocsRegistry(), (error, tool) => console.error(`bitbybit-mcp: ${tool} failed: ${error instanceof Error ? error.message : String(error)}`));
|
|
59
|
+
const server = createMcpServer(registry, context, { name: "bitbybit", version: own, instructions: SERVER_INSTRUCTIONS });
|
|
60
|
+
await server.connect(new StdioServerTransport());
|
|
61
|
+
console.error(`bitbybit-mcp: serving the Bitbybit API index for version ${served.version} (from ${served.source})`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
main().catch((error: unknown) => {
|
|
65
|
+
console.error(`bitbybit-mcp: ${error instanceof Error ? error.message : String(error)}`);
|
|
66
|
+
process.exit(1);
|
|
67
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { ToolDefinition } from "../registry.js";
|
|
3
|
+
import type { DocsContext } from "../context.js";
|
|
4
|
+
import { DESCRIPTIONS } from "../descriptions.js";
|
|
5
|
+
import { ok, failFor } from "../results.js";
|
|
6
|
+
import { renderLine, renderMember } from "../render.js";
|
|
7
|
+
|
|
8
|
+
const MAX_PATH_LENGTH = 256;
|
|
9
|
+
|
|
10
|
+
const input = z.object({
|
|
11
|
+
path: z.string().min(1).max(MAX_PATH_LENGTH).describe("The dotted path of the member, for example occt.shapes.solid.createBox"),
|
|
12
|
+
version: z.string().optional().describe("The API version to describe; omit for the version the server holds"),
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export const describe: ToolDefinition<typeof input, DocsContext> = {
|
|
16
|
+
name: "describe",
|
|
17
|
+
description: DESCRIPTIONS.describe.description,
|
|
18
|
+
annotations: { title: DESCRIPTIONS.describe.title, readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false },
|
|
19
|
+
input,
|
|
20
|
+
where: "server",
|
|
21
|
+
handler: async (args, context) => {
|
|
22
|
+
let resolved;
|
|
23
|
+
try {
|
|
24
|
+
resolved = await context.resolve(args.version);
|
|
25
|
+
} catch (error) {
|
|
26
|
+
return failFor(error);
|
|
27
|
+
}
|
|
28
|
+
const path = args.path.replace(/^bitbybit\./, "");
|
|
29
|
+
const member = resolved.reader.get(path);
|
|
30
|
+
if (!member) {
|
|
31
|
+
const nearest = resolved.reader.nearest(path);
|
|
32
|
+
const text = [
|
|
33
|
+
`${path} is not a member of the Bitbybit API ${resolved.version}.`,
|
|
34
|
+
nearest.length > 0 ? "Nearest existing paths:" : "No similar path exists; try search_api with other words.",
|
|
35
|
+
...nearest.map(renderLine),
|
|
36
|
+
].join("\n");
|
|
37
|
+
return ok(text, { version: resolved.version, notFound: path, nearest: nearest.map((candidate) => candidate.path) });
|
|
38
|
+
}
|
|
39
|
+
const examples = resolved.reader.examplesOf(path);
|
|
40
|
+
return ok(renderMember(member, resolved.version, examples), { version: resolved.version, member, examples });
|
|
41
|
+
},
|
|
42
|
+
};
|