@usalyt/mcp-server 0.0.0-stage → 0.0.2
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/CHANGELOG.md +27 -0
- package/LICENSE +21 -0
- package/NOTICE +31 -0
- package/README.md +116 -2
- package/THIRD_PARTY_NOTICES.md +446 -0
- package/dist/index.d.mts +63 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +131 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +61 -3
- package/src/index.ts +225 -0
- package/src/mcp-server.test.ts +287 -0
- package/tsconfig.json +10 -0
- package/tsdown.config.ts +20 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process";
|
|
2
|
+
import { copyFileSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { dirname, resolve } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
7
|
+
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
8
|
+
import { beforeAll, describe, expect, it } from "vitest";
|
|
9
|
+
|
|
10
|
+
const PACKAGE_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
11
|
+
const PACKAGE_VERSION = (
|
|
12
|
+
JSON.parse(readFileSync(resolve(PACKAGE_ROOT, "package.json"), "utf8")) as { version: string }
|
|
13
|
+
).version;
|
|
14
|
+
const DIST_ENTRY = resolve(PACKAGE_ROOT, "dist/index.mjs");
|
|
15
|
+
// Scratch state under .tmp is intentionally left in place for inspection.
|
|
16
|
+
const SCRATCH_ROOT = resolve(PACKAGE_ROOT, ".tmp/mcp-tests");
|
|
17
|
+
|
|
18
|
+
const require = createRequire(import.meta.url);
|
|
19
|
+
const REGISTRY_PATH = require.resolve("@usalyt/registry/registry.json");
|
|
20
|
+
const PACKAGED_REGISTRY = JSON.parse(readFileSync(REGISTRY_PATH, "utf8")) as {
|
|
21
|
+
items: Array<{ name: string; files: Array<{ path: string; content: string }> }>;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
interface TextContent {
|
|
25
|
+
type: "text";
|
|
26
|
+
text: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function firstText(content: unknown): string {
|
|
30
|
+
const blocks = content as TextContent[];
|
|
31
|
+
const textBlock = blocks.find((block) => block.type === "text");
|
|
32
|
+
if (!textBlock) {
|
|
33
|
+
throw new Error("Tool result has no text content block");
|
|
34
|
+
}
|
|
35
|
+
return textBlock.text;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function withClient(run: (client: Client) => Promise<void>): Promise<void> {
|
|
39
|
+
const transport = new StdioClientTransport({
|
|
40
|
+
command: process.execPath,
|
|
41
|
+
args: [DIST_ENTRY],
|
|
42
|
+
cwd: PACKAGE_ROOT,
|
|
43
|
+
stderr: "pipe"
|
|
44
|
+
});
|
|
45
|
+
const client = new Client({ name: "mcp-server-protocol-test", version: "0.0.1" });
|
|
46
|
+
await client.connect(transport);
|
|
47
|
+
try {
|
|
48
|
+
await run(client);
|
|
49
|
+
} finally {
|
|
50
|
+
await client.close();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Runs a minimal newline-delimited JSON-RPC session and returns every stdout line.
|
|
56
|
+
* Used to prove stdout carries protocol frames only.
|
|
57
|
+
*/
|
|
58
|
+
function runRawSession(): Promise<string[]> {
|
|
59
|
+
return new Promise((resolveLines, reject) => {
|
|
60
|
+
const child: ChildProcessWithoutNullStreams = spawn(process.execPath, [DIST_ENTRY], {
|
|
61
|
+
cwd: PACKAGE_ROOT,
|
|
62
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
63
|
+
});
|
|
64
|
+
let stdout = "";
|
|
65
|
+
const timeout = setTimeout(() => {
|
|
66
|
+
child.kill("SIGTERM");
|
|
67
|
+
reject(new Error(`Raw MCP session timed out. stdout so far: ${stdout}`));
|
|
68
|
+
}, 10_000);
|
|
69
|
+
|
|
70
|
+
child.stdout.on("data", (chunk: Buffer) => {
|
|
71
|
+
stdout += chunk.toString("utf8");
|
|
72
|
+
if (/"id":2[^\n]*\n/.test(stdout)) {
|
|
73
|
+
clearTimeout(timeout);
|
|
74
|
+
child.kill("SIGTERM");
|
|
75
|
+
resolveLines(stdout.split("\n").filter((line) => line.trim() !== ""));
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
child.on("error", (error) => {
|
|
79
|
+
clearTimeout(timeout);
|
|
80
|
+
reject(error);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
child.stdin.write(
|
|
84
|
+
`${JSON.stringify({
|
|
85
|
+
jsonrpc: "2.0",
|
|
86
|
+
id: 1,
|
|
87
|
+
method: "initialize",
|
|
88
|
+
params: {
|
|
89
|
+
protocolVersion: "2025-06-18",
|
|
90
|
+
capabilities: {},
|
|
91
|
+
clientInfo: { name: "raw-harness", version: "0.0.1" }
|
|
92
|
+
}
|
|
93
|
+
})}\n`
|
|
94
|
+
);
|
|
95
|
+
child.stdin.write(
|
|
96
|
+
`${JSON.stringify({ jsonrpc: "2.0", method: "notifications/initialized" })}\n`
|
|
97
|
+
);
|
|
98
|
+
child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id: 2, method: "tools/list" })}\n`);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
interface BrokenInstallationRun {
|
|
103
|
+
code: number | null;
|
|
104
|
+
stdout: string;
|
|
105
|
+
stderr: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
type BrokenInstallation = "missing" | "invalid";
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Copies the executable into a scratch directory whose nearest @usalyt/registry
|
|
112
|
+
* shadow package either omits the registry export or ships malformed JSON. This
|
|
113
|
+
* reproduces an incomplete or corrupted installation without touching the
|
|
114
|
+
* workspace package.
|
|
115
|
+
*/
|
|
116
|
+
function prepareBrokenInstallation(kind: BrokenInstallation): string {
|
|
117
|
+
const root = resolve(SCRATCH_ROOT, `${kind}-registry`);
|
|
118
|
+
const fakePackageDir = resolve(root, "node_modules/@usalyt/registry");
|
|
119
|
+
mkdirSync(fakePackageDir, { recursive: true });
|
|
120
|
+
|
|
121
|
+
const exportedPackageJson: Record<string, unknown> = {
|
|
122
|
+
name: "@usalyt/registry",
|
|
123
|
+
version: "0.0.1",
|
|
124
|
+
type: "module",
|
|
125
|
+
exports: { "./package.json": "./package.json" }
|
|
126
|
+
};
|
|
127
|
+
if (kind === "invalid") {
|
|
128
|
+
exportedPackageJson.exports = {
|
|
129
|
+
"./registry.json": "./registry.json",
|
|
130
|
+
"./package.json": "./package.json"
|
|
131
|
+
};
|
|
132
|
+
writeFileSync(resolve(fakePackageDir, "registry.json"), "{ this is not valid json\n");
|
|
133
|
+
}
|
|
134
|
+
writeFileSync(
|
|
135
|
+
resolve(fakePackageDir, "package.json"),
|
|
136
|
+
`${JSON.stringify(exportedPackageJson, null, 2)}\n`
|
|
137
|
+
);
|
|
138
|
+
copyFileSync(DIST_ENTRY, resolve(root, "index.mjs"));
|
|
139
|
+
return root;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function runBrokenInstallation(kind: BrokenInstallation): Promise<BrokenInstallationRun> {
|
|
143
|
+
const root = prepareBrokenInstallation(kind);
|
|
144
|
+
|
|
145
|
+
return new Promise((resolveRun, reject) => {
|
|
146
|
+
const child = spawn(process.execPath, [resolve(root, "index.mjs")], {
|
|
147
|
+
cwd: root,
|
|
148
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
149
|
+
});
|
|
150
|
+
let stdout = "";
|
|
151
|
+
let stderr = "";
|
|
152
|
+
child.stdout.on("data", (chunk: Buffer) => (stdout += chunk.toString("utf8")));
|
|
153
|
+
child.stderr.on("data", (chunk: Buffer) => (stderr += chunk.toString("utf8")));
|
|
154
|
+
child.on("error", reject);
|
|
155
|
+
child.on("close", (code) => resolveRun({ code, stdout, stderr }));
|
|
156
|
+
child.stdin.end();
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
describe("MCP stdio server", () => {
|
|
161
|
+
beforeAll(() => {
|
|
162
|
+
mkdirSync(SCRATCH_ROOT, { recursive: true });
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("initializes the protocol and advertises the Usalyt identity and package version", async () => {
|
|
166
|
+
await withClient(async (client) => {
|
|
167
|
+
expect(client.getServerVersion()).toMatchObject({ name: "usalyt", version: PACKAGE_VERSION });
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("keeps the existing tool names, descriptions, and input schema", async () => {
|
|
172
|
+
await withClient(async (client) => {
|
|
173
|
+
const { tools } = await client.listTools();
|
|
174
|
+
expect(tools.map((tool) => tool.name)).toEqual(["list_components", "get_component_code"]);
|
|
175
|
+
|
|
176
|
+
const listTool = tools.find((tool) => tool.name === "list_components");
|
|
177
|
+
expect(listTool?.description).toBe(
|
|
178
|
+
"List all UI components and blocks available in the registry"
|
|
179
|
+
);
|
|
180
|
+
expect(listTool?.inputSchema).toMatchObject({ type: "object", properties: {} });
|
|
181
|
+
|
|
182
|
+
const codeTool = tools.find((tool) => tool.name === "get_component_code");
|
|
183
|
+
expect(codeTool?.description).toBe("Get the source code for a specific component");
|
|
184
|
+
expect(codeTool?.inputSchema).toMatchObject({
|
|
185
|
+
type: "object",
|
|
186
|
+
properties: { name: { type: "string" } },
|
|
187
|
+
required: ["name"]
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("lists every packaged registry item from list_components", async () => {
|
|
193
|
+
await withClient(async (client) => {
|
|
194
|
+
const result = await client.callTool({ name: "list_components", arguments: {} });
|
|
195
|
+
expect(result.isError).toBeFalsy();
|
|
196
|
+
const items = JSON.parse(firstText(result.content)) as Array<{ name: string }>;
|
|
197
|
+
expect(items.map((item) => item.name)).toEqual(
|
|
198
|
+
PACKAGED_REGISTRY.items.map((item) => item.name)
|
|
199
|
+
);
|
|
200
|
+
expect(items.length).toBeGreaterThan(0);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("returns the source files for the button component", async () => {
|
|
205
|
+
await withClient(async (client) => {
|
|
206
|
+
const result = await client.callTool({
|
|
207
|
+
name: "get_component_code",
|
|
208
|
+
arguments: { name: "button" }
|
|
209
|
+
});
|
|
210
|
+
expect(result.isError).toBeFalsy();
|
|
211
|
+
const files = JSON.parse(firstText(result.content)) as Array<{
|
|
212
|
+
path: string;
|
|
213
|
+
content: string;
|
|
214
|
+
}>;
|
|
215
|
+
expect(files.map((file) => file.path)).toContain("components/button/button.tsx");
|
|
216
|
+
expect(files.some((file) => file.content.includes("Button"))).toBe(true);
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("returns an error result for an unknown component and keeps serving requests", async () => {
|
|
221
|
+
await withClient(async (client) => {
|
|
222
|
+
const unknown = await client.callTool({
|
|
223
|
+
name: "get_component_code",
|
|
224
|
+
arguments: { name: "does-not-exist" }
|
|
225
|
+
});
|
|
226
|
+
expect(unknown.isError).toBe(true);
|
|
227
|
+
const message = firstText(unknown.content);
|
|
228
|
+
expect(message).toContain("does-not-exist");
|
|
229
|
+
expect(message).toContain("list_components");
|
|
230
|
+
|
|
231
|
+
const followUp = await client.callTool({ name: "list_components", arguments: {} });
|
|
232
|
+
expect(followUp.isError).toBeFalsy();
|
|
233
|
+
expect(JSON.parse(firstText(followUp.content)).length).toBeGreaterThan(0);
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it("returns an error result for a missing or non-string name and keeps serving requests", async () => {
|
|
238
|
+
await withClient(async (client) => {
|
|
239
|
+
const missing = await client.callTool({
|
|
240
|
+
name: "get_component_code",
|
|
241
|
+
arguments: {}
|
|
242
|
+
});
|
|
243
|
+
expect(missing.isError).toBe(true);
|
|
244
|
+
expect(firstText(missing.content)).toContain("name");
|
|
245
|
+
|
|
246
|
+
const wrongType = await client.callTool({
|
|
247
|
+
name: "get_component_code",
|
|
248
|
+
arguments: { name: 42 }
|
|
249
|
+
});
|
|
250
|
+
expect(wrongType.isError).toBe(true);
|
|
251
|
+
|
|
252
|
+
const followUp = await client.callTool({ name: "list_components", arguments: {} });
|
|
253
|
+
expect(followUp.isError).toBeFalsy();
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("writes protocol frames only to stdout", async () => {
|
|
258
|
+
const lines = await runRawSession();
|
|
259
|
+
expect(lines.length).toBeGreaterThan(0);
|
|
260
|
+
for (const line of lines) {
|
|
261
|
+
const message = JSON.parse(line) as { jsonrpc?: string; id?: number };
|
|
262
|
+
expect(message.jsonrpc).toBe("2.0");
|
|
263
|
+
expect(typeof message.id).toBe("number");
|
|
264
|
+
}
|
|
265
|
+
const initialize = lines
|
|
266
|
+
.map((line) => JSON.parse(line) as { id?: number; result?: { serverInfo?: unknown } })
|
|
267
|
+
.find((message) => message.id === 1);
|
|
268
|
+
expect(initialize?.result?.serverInfo).toMatchObject({ name: "usalyt", version: PACKAGE_VERSION });
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it("exits non-zero with an actionable stderr message when the packaged registry is missing", async () => {
|
|
272
|
+
const run = await runBrokenInstallation("missing");
|
|
273
|
+
expect(run.code).not.toBe(0);
|
|
274
|
+
expect(run.stdout).toBe("");
|
|
275
|
+
expect(run.stderr).toMatch(/registry/i);
|
|
276
|
+
expect(run.stderr).toMatch(/@usalyt\/registry\/registry\.json/);
|
|
277
|
+
expect(run.stderr).toMatch(/reinstall|install/i);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it("exits non-zero with an actionable stderr message when the packaged registry is invalid", async () => {
|
|
281
|
+
const run = await runBrokenInstallation("invalid");
|
|
282
|
+
expect(run.code).not.toBe(0);
|
|
283
|
+
expect(run.stdout).toBe("");
|
|
284
|
+
expect(run.stderr).toMatch(/not valid JSON/i);
|
|
285
|
+
expect(run.stderr).toMatch(/reinstall/i);
|
|
286
|
+
});
|
|
287
|
+
});
|
package/tsconfig.json
ADDED
package/tsdown.config.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { chmodSync, readFileSync } from "node:fs";
|
|
2
|
+
import { defineConfig } from "tsdown";
|
|
3
|
+
|
|
4
|
+
const BIN_OUTPUT = "dist/index.mjs";
|
|
5
|
+
const NODE_SHEBANG = "#!/usr/bin/env node";
|
|
6
|
+
|
|
7
|
+
export default defineConfig({
|
|
8
|
+
entry: ["src/index.ts"],
|
|
9
|
+
format: ["esm"],
|
|
10
|
+
dts: true,
|
|
11
|
+
clean: true,
|
|
12
|
+
outDir: "dist",
|
|
13
|
+
onSuccess() {
|
|
14
|
+
const output = readFileSync(BIN_OUTPUT, "utf8");
|
|
15
|
+
if (!output.startsWith(NODE_SHEBANG)) {
|
|
16
|
+
throw new Error(`${BIN_OUTPUT} must start with "${NODE_SHEBANG}" for the usalyt-mcp bin entry.`);
|
|
17
|
+
}
|
|
18
|
+
chmodSync(BIN_OUTPUT, 0o755);
|
|
19
|
+
}
|
|
20
|
+
});
|