@usalyt/mcp-server 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/CHANGELOG.md +20 -0
- package/LICENSE +674 -0
- package/NOTICE +32 -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 +128 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +61 -3
- package/src/index.ts +223 -0
- package/src/mcp-server.test.ts +284 -0
- package/tsconfig.json +10 -0
- package/tsdown.config.ts +20 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
4
|
+
import { dirname, resolve } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
7
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
8
|
+
import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
9
|
+
//#region src/index.ts
|
|
10
|
+
const SERVER_NAME = "usalyt";
|
|
11
|
+
const SERVER_VERSION = "0.0.1";
|
|
12
|
+
const REGISTRY_REQUEST = "@usalyt/registry/registry.json";
|
|
13
|
+
var RegistryLoadError = class extends Error {
|
|
14
|
+
constructor(message) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = "RegistryLoadError";
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Resolves the registry through the installed @usalyt/registry package so the
|
|
21
|
+
* executable works from any working directory. The monorepo-relative lookup is
|
|
22
|
+
* a development-only fallback for checkouts without linked workspace packages.
|
|
23
|
+
*/
|
|
24
|
+
function resolveRegistryPath() {
|
|
25
|
+
const require = createRequire(import.meta.url);
|
|
26
|
+
try {
|
|
27
|
+
return require.resolve(REGISTRY_REQUEST);
|
|
28
|
+
} catch (packageError) {
|
|
29
|
+
const developmentFallback = resolve(dirname(fileURLToPath(import.meta.url)), "../../registry/registry.json");
|
|
30
|
+
if (process.env.NODE_ENV !== "production" && existsSync(developmentFallback)) return developmentFallback;
|
|
31
|
+
const reason = packageError instanceof Error ? packageError.message : String(packageError);
|
|
32
|
+
throw new RegistryLoadError(`cannot load the packaged component registry. Expected "${REGISTRY_REQUEST}" to resolve from this installation. Reinstall @usalyt/mcp-server (which depends on @usalyt/registry) or install @usalyt/registry explicitly. Resolution error: ${reason}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function assertRegistryShape(value, source) {
|
|
36
|
+
if (typeof value !== "object" || value === null || !Array.isArray(value.items)) throw new RegistryLoadError(`the packaged component registry at "${source}" is invalid: expected an object with an "items" array. Reinstall @usalyt/registry to restore the packaged registry.`);
|
|
37
|
+
for (const item of value.items) if (typeof item !== "object" || item === null || typeof item.name !== "string" || !Array.isArray(item.files)) throw new RegistryLoadError(`the packaged component registry at "${source}" is invalid: every item needs a string "name" and a "files" array. Reinstall @usalyt/registry to restore the packaged registry.`);
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
function loadRegistry() {
|
|
41
|
+
const registryPath = resolveRegistryPath();
|
|
42
|
+
let raw;
|
|
43
|
+
try {
|
|
44
|
+
raw = readFileSync(registryPath, "utf8");
|
|
45
|
+
} catch (error) {
|
|
46
|
+
throw new RegistryLoadError(`cannot read the packaged component registry at "${registryPath}": ${error instanceof Error ? error.message : String(error)}. Reinstall @usalyt/registry to restore the packaged registry.`);
|
|
47
|
+
}
|
|
48
|
+
let parsed;
|
|
49
|
+
try {
|
|
50
|
+
parsed = JSON.parse(raw);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
throw new RegistryLoadError(`the packaged component registry at "${registryPath}" is not valid JSON: ${error instanceof Error ? error.message : String(error)}. Reinstall @usalyt/registry to restore the packaged registry.`);
|
|
53
|
+
}
|
|
54
|
+
return assertRegistryShape(parsed, registryPath);
|
|
55
|
+
}
|
|
56
|
+
function textResult(text) {
|
|
57
|
+
return { content: [{
|
|
58
|
+
type: "text",
|
|
59
|
+
text
|
|
60
|
+
}] };
|
|
61
|
+
}
|
|
62
|
+
function toolError(text) {
|
|
63
|
+
return {
|
|
64
|
+
content: [{
|
|
65
|
+
type: "text",
|
|
66
|
+
text
|
|
67
|
+
}],
|
|
68
|
+
isError: true
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function createMcpServer(registry = loadRegistry()) {
|
|
72
|
+
const server = new Server({
|
|
73
|
+
name: SERVER_NAME,
|
|
74
|
+
version: SERVER_VERSION
|
|
75
|
+
}, { capabilities: { tools: {} } });
|
|
76
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [{
|
|
77
|
+
name: "list_components",
|
|
78
|
+
description: "List all UI components and blocks available in the registry",
|
|
79
|
+
inputSchema: {
|
|
80
|
+
type: "object",
|
|
81
|
+
properties: {}
|
|
82
|
+
}
|
|
83
|
+
}, {
|
|
84
|
+
name: "get_component_code",
|
|
85
|
+
description: "Get the source code for a specific component",
|
|
86
|
+
inputSchema: {
|
|
87
|
+
type: "object",
|
|
88
|
+
properties: { name: { type: "string" } },
|
|
89
|
+
required: ["name"]
|
|
90
|
+
}
|
|
91
|
+
}] }));
|
|
92
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
93
|
+
if (request.params.name === "list_components") return textResult(JSON.stringify(registry.items, null, 2));
|
|
94
|
+
if (request.params.name === "get_component_code") {
|
|
95
|
+
const componentName = request.params.arguments?.name;
|
|
96
|
+
if (typeof componentName !== "string" || componentName.trim() === "") return toolError("Invalid \"name\" argument: expected a non-empty component name such as \"button\". Call list_components to see the available component names.");
|
|
97
|
+
const item = registry.items.find((candidate) => candidate.name === componentName);
|
|
98
|
+
if (!item) return toolError(`Component "${componentName}" was not found. Call list_components to see the available component names.`);
|
|
99
|
+
return textResult(JSON.stringify(item.files, null, 2));
|
|
100
|
+
}
|
|
101
|
+
throw new Error(`Tool not found: ${request.params.name}`);
|
|
102
|
+
});
|
|
103
|
+
return server;
|
|
104
|
+
}
|
|
105
|
+
async function startServer() {
|
|
106
|
+
const server = createMcpServer(loadRegistry());
|
|
107
|
+
const transport = new StdioServerTransport();
|
|
108
|
+
await server.connect(transport);
|
|
109
|
+
}
|
|
110
|
+
function isExecutableEntryPoint() {
|
|
111
|
+
const entry = process.argv[1];
|
|
112
|
+
if (!entry) return false;
|
|
113
|
+
try {
|
|
114
|
+
return realpathSync(entry) === realpathSync(fileURLToPath(import.meta.url));
|
|
115
|
+
} catch {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (isExecutableEntryPoint()) startServer().catch((error) => {
|
|
120
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
121
|
+
process.stderr.write(`usalyt-mcp: ${reason}\n`, () => {
|
|
122
|
+
process.exit(1);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
//#endregion
|
|
126
|
+
export { RegistryLoadError, createMcpServer, loadRegistry, startServer };
|
|
127
|
+
|
|
128
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["#!/usr/bin/env node\nimport { existsSync, readFileSync, realpathSync } from \"node:fs\";\nimport { createRequire } from \"node:module\";\nimport { dirname, resolve } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport { Server } from \"@modelcontextprotocol/sdk/server/index.js\";\nimport { StdioServerTransport } from \"@modelcontextprotocol/sdk/server/stdio.js\";\nimport {\n CallToolRequestSchema,\n ListToolsRequestSchema,\n type CallToolResult\n} from \"@modelcontextprotocol/sdk/types.js\";\n\nconst SERVER_NAME = \"usalyt\";\nconst SERVER_VERSION = \"0.0.1\";\nconst REGISTRY_REQUEST = \"@usalyt/registry/registry.json\";\n\nexport interface RegistryFile {\n path: string;\n type: string;\n content: string;\n}\n\nexport interface RegistryItem {\n name: string;\n type: string;\n title?: string;\n description?: string;\n files: RegistryFile[];\n [key: string]: unknown;\n}\n\nexport interface Registry {\n name?: string;\n version?: string;\n items: RegistryItem[];\n [key: string]: unknown;\n}\n\nexport class RegistryLoadError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"RegistryLoadError\";\n }\n}\n\n/**\n * Resolves the registry through the installed @usalyt/registry package so the\n * executable works from any working directory. The monorepo-relative lookup is\n * a development-only fallback for checkouts without linked workspace packages.\n */\nfunction resolveRegistryPath(): string {\n const require = createRequire(import.meta.url);\n try {\n return require.resolve(REGISTRY_REQUEST);\n } catch (packageError) {\n const here = dirname(fileURLToPath(import.meta.url));\n const developmentFallback = resolve(here, \"../../registry/registry.json\");\n if (process.env.NODE_ENV !== \"production\" && existsSync(developmentFallback)) {\n return developmentFallback;\n }\n const reason = packageError instanceof Error ? packageError.message : String(packageError);\n throw new RegistryLoadError(\n `cannot load the packaged component registry. Expected \"${REGISTRY_REQUEST}\" to resolve ` +\n `from this installation. Reinstall @usalyt/mcp-server (which depends on @usalyt/registry) ` +\n `or install @usalyt/registry explicitly. Resolution error: ${reason}`\n );\n }\n}\n\nfunction assertRegistryShape(value: unknown, source: string): Registry {\n if (typeof value !== \"object\" || value === null || !Array.isArray((value as Registry).items)) {\n throw new RegistryLoadError(\n `the packaged component registry at \"${source}\" is invalid: expected an object with an \"items\" array. ` +\n `Reinstall @usalyt/registry to restore the packaged registry.`\n );\n }\n for (const item of (value as Registry).items) {\n if (\n typeof item !== \"object\" ||\n item === null ||\n typeof item.name !== \"string\" ||\n !Array.isArray(item.files)\n ) {\n throw new RegistryLoadError(\n `the packaged component registry at \"${source}\" is invalid: every item needs a string \"name\" ` +\n `and a \"files\" array. Reinstall @usalyt/registry to restore the packaged registry.`\n );\n }\n }\n return value as Registry;\n}\n\nexport function loadRegistry(): Registry {\n const registryPath = resolveRegistryPath();\n let raw: string;\n try {\n raw = readFileSync(registryPath, \"utf8\");\n } catch (error) {\n const reason = error instanceof Error ? error.message : String(error);\n throw new RegistryLoadError(\n `cannot read the packaged component registry at \"${registryPath}\": ${reason}. ` +\n `Reinstall @usalyt/registry to restore the packaged registry.`\n );\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch (error) {\n const reason = error instanceof Error ? error.message : String(error);\n throw new RegistryLoadError(\n `the packaged component registry at \"${registryPath}\" is not valid JSON: ${reason}. ` +\n `Reinstall @usalyt/registry to restore the packaged registry.`\n );\n }\n\n return assertRegistryShape(parsed, registryPath);\n}\n\nfunction textResult(text: string): CallToolResult {\n return { content: [{ type: \"text\", text }] };\n}\n\nfunction toolError(text: string): CallToolResult {\n return { content: [{ type: \"text\", text }], isError: true };\n}\n\nexport function createMcpServer(registry: Registry = loadRegistry()) {\n const server = new Server(\n {\n name: SERVER_NAME,\n version: SERVER_VERSION\n },\n {\n capabilities: {\n tools: {}\n }\n }\n );\n\n server.setRequestHandler(ListToolsRequestSchema, async () => ({\n tools: [\n {\n name: \"list_components\",\n description: \"List all UI components and blocks available in the registry\",\n inputSchema: {\n type: \"object\",\n properties: {}\n }\n },\n {\n name: \"get_component_code\",\n description: \"Get the source code for a specific component\",\n inputSchema: {\n type: \"object\",\n properties: {\n name: { type: \"string\" }\n },\n required: [\"name\"]\n }\n }\n ]\n }));\n\n server.setRequestHandler(CallToolRequestSchema, async (request): Promise<CallToolResult> => {\n if (request.params.name === \"list_components\") {\n return textResult(JSON.stringify(registry.items, null, 2));\n }\n\n if (request.params.name === \"get_component_code\") {\n const componentName = request.params.arguments?.name;\n if (typeof componentName !== \"string\" || componentName.trim() === \"\") {\n return toolError(\n `Invalid \"name\" argument: expected a non-empty component name such as \"button\". ` +\n `Call list_components to see the available component names.`\n );\n }\n\n const item = registry.items.find((candidate) => candidate.name === componentName);\n if (!item) {\n return toolError(\n `Component \"${componentName}\" was not found. ` +\n `Call list_components to see the available component names.`\n );\n }\n\n return textResult(JSON.stringify(item.files, null, 2));\n }\n\n throw new Error(`Tool not found: ${request.params.name}`);\n });\n\n return server;\n}\n\nexport async function startServer(): Promise<void> {\n const registry = loadRegistry();\n const server = createMcpServer(registry);\n const transport = new StdioServerTransport();\n await server.connect(transport);\n}\n\nfunction isExecutableEntryPoint(): boolean {\n const entry = process.argv[1];\n if (!entry) {\n return false;\n }\n try {\n return realpathSync(entry) === realpathSync(fileURLToPath(import.meta.url));\n } catch {\n return false;\n }\n}\n\nif (isExecutableEntryPoint()) {\n startServer().catch((error: unknown) => {\n const reason = error instanceof Error ? error.message : String(error);\n process.stderr.write(`usalyt-mcp: ${reason}\\n`, () => {\n process.exit(1);\n });\n });\n}\n"],"mappings":";;;;;;;;;AAaA,MAAM,cAAc;AACpB,MAAM,iBAAiB;AACvB,MAAM,mBAAmB;AAwBzB,IAAa,oBAAb,cAAuC,MAAM;CAC3C,YAAY,SAAiB;EAC3B,MAAM,OAAO;EACb,KAAK,OAAO;CACd;AACF;;;;;;AAOA,SAAS,sBAA8B;CACrC,MAAM,UAAU,cAAc,OAAO,KAAK,GAAG;CAC7C,IAAI;EACF,OAAO,QAAQ,QAAQ,gBAAgB;CACzC,SAAS,cAAc;EAErB,MAAM,sBAAsB,QADf,QAAQ,cAAc,OAAO,KAAK,GAAG,CACX,GAAG,8BAA8B;EACxE,IAAI,QAAQ,IAAI,aAAa,gBAAgB,WAAW,mBAAmB,GACzE,OAAO;EAET,MAAM,SAAS,wBAAwB,QAAQ,aAAa,UAAU,OAAO,YAAY;EACzF,MAAM,IAAI,kBACR,0DAA0D,iBAAiB,kKAEZ,QACjE;CACF;AACF;AAEA,SAAS,oBAAoB,OAAgB,QAA0B;CACrE,IAAI,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAS,MAAmB,KAAK,GACzF,MAAM,IAAI,kBACR,uCAAuC,OAAO,qHAEhD;CAE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|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Model Context Protocol (MCP) server for Antigravity, Cursor, and Claude Code AI agents",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.mjs",
|
|
7
|
+
"module": "./dist/index.mjs",
|
|
8
|
+
"types": "./dist/index.d.mts",
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
11
|
+
"@usalyt/registry": "^0.0.1"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"@types/node": "^26.1.1",
|
|
15
|
+
"tsdown": "^0.22.14",
|
|
16
|
+
"typescript": "^6.0.0",
|
|
17
|
+
"vitest": "^4.1.10",
|
|
18
|
+
"@usalyt/tsconfig": "0.0.1"
|
|
19
|
+
},
|
|
20
|
+
"license": "GPL-3.0-only",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/Usalyt/ui.git",
|
|
24
|
+
"directory": "usalyt/packages/mcp-server"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/Usalyt/ui",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/Usalyt/ui/issues"
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=20"
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"bin": {
|
|
37
|
+
"usalyt-mcp": "./dist/index.mjs"
|
|
38
|
+
},
|
|
39
|
+
"exports": {
|
|
40
|
+
".": {
|
|
41
|
+
"types": "./dist/index.d.mts",
|
|
42
|
+
"import": "./dist/index.mjs"
|
|
43
|
+
},
|
|
44
|
+
"./package.json": "./package.json"
|
|
45
|
+
},
|
|
46
|
+
"files": [
|
|
47
|
+
"dist",
|
|
48
|
+
"src",
|
|
49
|
+
"tsdown.config.ts",
|
|
50
|
+
"tsconfig.json",
|
|
51
|
+
"README.md",
|
|
52
|
+
"LICENSE",
|
|
53
|
+
"NOTICE",
|
|
54
|
+
"THIRD_PARTY_NOTICES.md",
|
|
55
|
+
"CHANGELOG.md"
|
|
56
|
+
],
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "tsdown",
|
|
59
|
+
"dev": "tsdown --watch",
|
|
60
|
+
"test:unit": "vitest run",
|
|
61
|
+
"test": "pnpm build && vitest run",
|
|
62
|
+
"type-check": "tsc --noEmit"
|
|
63
|
+
}
|
|
6
64
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { dirname, resolve } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
7
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
8
|
+
import {
|
|
9
|
+
CallToolRequestSchema,
|
|
10
|
+
ListToolsRequestSchema,
|
|
11
|
+
type CallToolResult
|
|
12
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
13
|
+
|
|
14
|
+
const SERVER_NAME = "usalyt";
|
|
15
|
+
const SERVER_VERSION = "0.0.1";
|
|
16
|
+
const REGISTRY_REQUEST = "@usalyt/registry/registry.json";
|
|
17
|
+
|
|
18
|
+
export interface RegistryFile {
|
|
19
|
+
path: string;
|
|
20
|
+
type: string;
|
|
21
|
+
content: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface RegistryItem {
|
|
25
|
+
name: string;
|
|
26
|
+
type: string;
|
|
27
|
+
title?: string;
|
|
28
|
+
description?: string;
|
|
29
|
+
files: RegistryFile[];
|
|
30
|
+
[key: string]: unknown;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface Registry {
|
|
34
|
+
name?: string;
|
|
35
|
+
version?: string;
|
|
36
|
+
items: RegistryItem[];
|
|
37
|
+
[key: string]: unknown;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class RegistryLoadError extends Error {
|
|
41
|
+
constructor(message: string) {
|
|
42
|
+
super(message);
|
|
43
|
+
this.name = "RegistryLoadError";
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Resolves the registry through the installed @usalyt/registry package so the
|
|
49
|
+
* executable works from any working directory. The monorepo-relative lookup is
|
|
50
|
+
* a development-only fallback for checkouts without linked workspace packages.
|
|
51
|
+
*/
|
|
52
|
+
function resolveRegistryPath(): string {
|
|
53
|
+
const require = createRequire(import.meta.url);
|
|
54
|
+
try {
|
|
55
|
+
return require.resolve(REGISTRY_REQUEST);
|
|
56
|
+
} catch (packageError) {
|
|
57
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
58
|
+
const developmentFallback = resolve(here, "../../registry/registry.json");
|
|
59
|
+
if (process.env.NODE_ENV !== "production" && existsSync(developmentFallback)) {
|
|
60
|
+
return developmentFallback;
|
|
61
|
+
}
|
|
62
|
+
const reason = packageError instanceof Error ? packageError.message : String(packageError);
|
|
63
|
+
throw new RegistryLoadError(
|
|
64
|
+
`cannot load the packaged component registry. Expected "${REGISTRY_REQUEST}" to resolve ` +
|
|
65
|
+
`from this installation. Reinstall @usalyt/mcp-server (which depends on @usalyt/registry) ` +
|
|
66
|
+
`or install @usalyt/registry explicitly. Resolution error: ${reason}`
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function assertRegistryShape(value: unknown, source: string): Registry {
|
|
72
|
+
if (typeof value !== "object" || value === null || !Array.isArray((value as Registry).items)) {
|
|
73
|
+
throw new RegistryLoadError(
|
|
74
|
+
`the packaged component registry at "${source}" is invalid: expected an object with an "items" array. ` +
|
|
75
|
+
`Reinstall @usalyt/registry to restore the packaged registry.`
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
for (const item of (value as Registry).items) {
|
|
79
|
+
if (
|
|
80
|
+
typeof item !== "object" ||
|
|
81
|
+
item === null ||
|
|
82
|
+
typeof item.name !== "string" ||
|
|
83
|
+
!Array.isArray(item.files)
|
|
84
|
+
) {
|
|
85
|
+
throw new RegistryLoadError(
|
|
86
|
+
`the packaged component registry at "${source}" is invalid: every item needs a string "name" ` +
|
|
87
|
+
`and a "files" array. Reinstall @usalyt/registry to restore the packaged registry.`
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return value as Registry;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function loadRegistry(): Registry {
|
|
95
|
+
const registryPath = resolveRegistryPath();
|
|
96
|
+
let raw: string;
|
|
97
|
+
try {
|
|
98
|
+
raw = readFileSync(registryPath, "utf8");
|
|
99
|
+
} catch (error) {
|
|
100
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
101
|
+
throw new RegistryLoadError(
|
|
102
|
+
`cannot read the packaged component registry at "${registryPath}": ${reason}. ` +
|
|
103
|
+
`Reinstall @usalyt/registry to restore the packaged registry.`
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
let parsed: unknown;
|
|
108
|
+
try {
|
|
109
|
+
parsed = JSON.parse(raw);
|
|
110
|
+
} catch (error) {
|
|
111
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
112
|
+
throw new RegistryLoadError(
|
|
113
|
+
`the packaged component registry at "${registryPath}" is not valid JSON: ${reason}. ` +
|
|
114
|
+
`Reinstall @usalyt/registry to restore the packaged registry.`
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return assertRegistryShape(parsed, registryPath);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function textResult(text: string): CallToolResult {
|
|
122
|
+
return { content: [{ type: "text", text }] };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function toolError(text: string): CallToolResult {
|
|
126
|
+
return { content: [{ type: "text", text }], isError: true };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function createMcpServer(registry: Registry = loadRegistry()) {
|
|
130
|
+
const server = new Server(
|
|
131
|
+
{
|
|
132
|
+
name: SERVER_NAME,
|
|
133
|
+
version: SERVER_VERSION
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
capabilities: {
|
|
137
|
+
tools: {}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
143
|
+
tools: [
|
|
144
|
+
{
|
|
145
|
+
name: "list_components",
|
|
146
|
+
description: "List all UI components and blocks available in the registry",
|
|
147
|
+
inputSchema: {
|
|
148
|
+
type: "object",
|
|
149
|
+
properties: {}
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
name: "get_component_code",
|
|
154
|
+
description: "Get the source code for a specific component",
|
|
155
|
+
inputSchema: {
|
|
156
|
+
type: "object",
|
|
157
|
+
properties: {
|
|
158
|
+
name: { type: "string" }
|
|
159
|
+
},
|
|
160
|
+
required: ["name"]
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
]
|
|
164
|
+
}));
|
|
165
|
+
|
|
166
|
+
server.setRequestHandler(CallToolRequestSchema, async (request): Promise<CallToolResult> => {
|
|
167
|
+
if (request.params.name === "list_components") {
|
|
168
|
+
return textResult(JSON.stringify(registry.items, null, 2));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (request.params.name === "get_component_code") {
|
|
172
|
+
const componentName = request.params.arguments?.name;
|
|
173
|
+
if (typeof componentName !== "string" || componentName.trim() === "") {
|
|
174
|
+
return toolError(
|
|
175
|
+
`Invalid "name" argument: expected a non-empty component name such as "button". ` +
|
|
176
|
+
`Call list_components to see the available component names.`
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const item = registry.items.find((candidate) => candidate.name === componentName);
|
|
181
|
+
if (!item) {
|
|
182
|
+
return toolError(
|
|
183
|
+
`Component "${componentName}" was not found. ` +
|
|
184
|
+
`Call list_components to see the available component names.`
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return textResult(JSON.stringify(item.files, null, 2));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
throw new Error(`Tool not found: ${request.params.name}`);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
return server;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export async function startServer(): Promise<void> {
|
|
198
|
+
const registry = loadRegistry();
|
|
199
|
+
const server = createMcpServer(registry);
|
|
200
|
+
const transport = new StdioServerTransport();
|
|
201
|
+
await server.connect(transport);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function isExecutableEntryPoint(): boolean {
|
|
205
|
+
const entry = process.argv[1];
|
|
206
|
+
if (!entry) {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
try {
|
|
210
|
+
return realpathSync(entry) === realpathSync(fileURLToPath(import.meta.url));
|
|
211
|
+
} catch {
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (isExecutableEntryPoint()) {
|
|
217
|
+
startServer().catch((error: unknown) => {
|
|
218
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
219
|
+
process.stderr.write(`usalyt-mcp: ${reason}\n`, () => {
|
|
220
|
+
process.exit(1);
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
}
|