@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.
@@ -0,0 +1,63 @@
1
+ import { Server } from "@modelcontextprotocol/sdk/server/index.js";
2
+ //#region src/index.d.ts
3
+ interface RegistryFile {
4
+ path: string;
5
+ type: string;
6
+ content: string;
7
+ }
8
+ interface RegistryItem {
9
+ name: string;
10
+ type: string;
11
+ title?: string;
12
+ description?: string;
13
+ files: RegistryFile[];
14
+ [key: string]: unknown;
15
+ }
16
+ interface Registry {
17
+ name?: string;
18
+ version?: string;
19
+ items: RegistryItem[];
20
+ [key: string]: unknown;
21
+ }
22
+ declare class RegistryLoadError extends Error {
23
+ constructor(message: string);
24
+ }
25
+ declare function loadRegistry(): Registry;
26
+ declare function createMcpServer(registry?: Registry): Server<{
27
+ method: string;
28
+ params?: {
29
+ [x: string]: unknown;
30
+ _meta?: {
31
+ [x: string]: unknown;
32
+ progressToken?: string | number | undefined;
33
+ "io.modelcontextprotocol/related-task"?: {
34
+ taskId: string;
35
+ } | undefined;
36
+ } | undefined;
37
+ } | undefined;
38
+ }, {
39
+ method: string;
40
+ params?: {
41
+ [x: string]: unknown;
42
+ _meta?: {
43
+ [x: string]: unknown;
44
+ progressToken?: string | number | undefined;
45
+ "io.modelcontextprotocol/related-task"?: {
46
+ taskId: string;
47
+ } | undefined;
48
+ } | undefined;
49
+ } | undefined;
50
+ }, {
51
+ [x: string]: unknown;
52
+ _meta?: {
53
+ [x: string]: unknown;
54
+ progressToken?: string | number | undefined;
55
+ "io.modelcontextprotocol/related-task"?: {
56
+ taskId: string;
57
+ } | undefined;
58
+ } | undefined;
59
+ }>;
60
+ declare function startServer(): Promise<void>;
61
+ //#endregion
62
+ export { Registry, RegistryFile, RegistryItem, RegistryLoadError, createMcpServer, loadRegistry, startServer };
63
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;UAmBiB;EACf;EACA;EACA;;UAGe;EACf;EACA;EACA;EACA;EACA,OAAO;GACN;;UAGc;EACf;EACA;EACA,OAAO;GACN;;cAGU,0BAA0B;EACzB,YAAA;;iBAqDE,gBAAgB;iBAmChB,gBAAgB,WAAU,WAAyB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAoE7C,eAAe"}
package/dist/index.mjs ADDED
@@ -0,0 +1,131 @@
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 package.json
10
+ var version = "0.0.2";
11
+ //#endregion
12
+ //#region src/index.ts
13
+ const SERVER_NAME = "usalyt";
14
+ const SERVER_VERSION = version;
15
+ const REGISTRY_REQUEST = "@usalyt/registry/registry.json";
16
+ var RegistryLoadError = class extends Error {
17
+ constructor(message) {
18
+ super(message);
19
+ this.name = "RegistryLoadError";
20
+ }
21
+ };
22
+ /**
23
+ * Resolves the registry through the installed @usalyt/registry package so the
24
+ * executable works from any working directory. The monorepo-relative lookup is
25
+ * a development-only fallback for checkouts without linked workspace packages.
26
+ */
27
+ function resolveRegistryPath() {
28
+ const require = createRequire(import.meta.url);
29
+ try {
30
+ return require.resolve(REGISTRY_REQUEST);
31
+ } catch (packageError) {
32
+ const developmentFallback = resolve(dirname(fileURLToPath(import.meta.url)), "../../registry/registry.json");
33
+ if (process.env.NODE_ENV !== "production" && existsSync(developmentFallback)) return developmentFallback;
34
+ const reason = packageError instanceof Error ? packageError.message : String(packageError);
35
+ 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}`);
36
+ }
37
+ }
38
+ function assertRegistryShape(value, source) {
39
+ 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.`);
40
+ 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.`);
41
+ return value;
42
+ }
43
+ function loadRegistry() {
44
+ const registryPath = resolveRegistryPath();
45
+ let raw;
46
+ try {
47
+ raw = readFileSync(registryPath, "utf8");
48
+ } catch (error) {
49
+ 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.`);
50
+ }
51
+ let parsed;
52
+ try {
53
+ parsed = JSON.parse(raw);
54
+ } catch (error) {
55
+ 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.`);
56
+ }
57
+ return assertRegistryShape(parsed, registryPath);
58
+ }
59
+ function textResult(text) {
60
+ return { content: [{
61
+ type: "text",
62
+ text
63
+ }] };
64
+ }
65
+ function toolError(text) {
66
+ return {
67
+ content: [{
68
+ type: "text",
69
+ text
70
+ }],
71
+ isError: true
72
+ };
73
+ }
74
+ function createMcpServer(registry = loadRegistry()) {
75
+ const server = new Server({
76
+ name: SERVER_NAME,
77
+ version: SERVER_VERSION
78
+ }, { capabilities: { tools: {} } });
79
+ server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [{
80
+ name: "list_components",
81
+ description: "List all UI components and blocks available in the registry",
82
+ inputSchema: {
83
+ type: "object",
84
+ properties: {}
85
+ }
86
+ }, {
87
+ name: "get_component_code",
88
+ description: "Get the source code for a specific component",
89
+ inputSchema: {
90
+ type: "object",
91
+ properties: { name: { type: "string" } },
92
+ required: ["name"]
93
+ }
94
+ }] }));
95
+ server.setRequestHandler(CallToolRequestSchema, async (request) => {
96
+ if (request.params.name === "list_components") return textResult(JSON.stringify(registry.items, null, 2));
97
+ if (request.params.name === "get_component_code") {
98
+ const componentName = request.params.arguments?.name;
99
+ 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.");
100
+ const item = registry.items.find((candidate) => candidate.name === componentName);
101
+ if (!item) return toolError(`Component "${componentName}" was not found. Call list_components to see the available component names.`);
102
+ return textResult(JSON.stringify(item.files, null, 2));
103
+ }
104
+ throw new Error(`Tool not found: ${request.params.name}`);
105
+ });
106
+ return server;
107
+ }
108
+ async function startServer() {
109
+ const server = createMcpServer(loadRegistry());
110
+ const transport = new StdioServerTransport();
111
+ await server.connect(transport);
112
+ }
113
+ function isExecutableEntryPoint() {
114
+ const entry = process.argv[1];
115
+ if (!entry) return false;
116
+ try {
117
+ return realpathSync(entry) === realpathSync(fileURLToPath(import.meta.url));
118
+ } catch {
119
+ return false;
120
+ }
121
+ }
122
+ if (isExecutableEntryPoint()) startServer().catch((error) => {
123
+ const reason = error instanceof Error ? error.message : String(error);
124
+ process.stderr.write(`usalyt-mcp: ${reason}\n`, () => {
125
+ process.exit(1);
126
+ });
127
+ });
128
+ //#endregion
129
+ export { RegistryLoadError, createMcpServer, loadRegistry, startServer };
130
+
131
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":["packageJson.version"],"sources":["../package.json","../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\nimport packageJson from \"../package.json\" with { type: \"json\" };\n\nconst SERVER_NAME = \"usalyt\";\nconst SERVER_VERSION = packageJson.version;\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":";;;;;;;;;;;;ACeA,MAAM,cAAc;AACpB,MAAM,iBAAiBA;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,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UAAU,OAAO,KAAK;CACpE,QAAQ,OAAO,MAAM,eAAe,OAAO,WAAW;EACpD,QAAQ,KAAK,CAAC;CAChB,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,64 @@
1
1
  {
2
2
  "name": "@usalyt/mcp-server",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.0.2",
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.2"
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.2"
19
+ },
20
+ "license": "MIT",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/Usalyt/ui.git",
24
+ "directory": "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,225 @@
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
+ import packageJson from "../package.json" with { type: "json" };
15
+
16
+ const SERVER_NAME = "usalyt";
17
+ const SERVER_VERSION = packageJson.version;
18
+ const REGISTRY_REQUEST = "@usalyt/registry/registry.json";
19
+
20
+ export interface RegistryFile {
21
+ path: string;
22
+ type: string;
23
+ content: string;
24
+ }
25
+
26
+ export interface RegistryItem {
27
+ name: string;
28
+ type: string;
29
+ title?: string;
30
+ description?: string;
31
+ files: RegistryFile[];
32
+ [key: string]: unknown;
33
+ }
34
+
35
+ export interface Registry {
36
+ name?: string;
37
+ version?: string;
38
+ items: RegistryItem[];
39
+ [key: string]: unknown;
40
+ }
41
+
42
+ export class RegistryLoadError extends Error {
43
+ constructor(message: string) {
44
+ super(message);
45
+ this.name = "RegistryLoadError";
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Resolves the registry through the installed @usalyt/registry package so the
51
+ * executable works from any working directory. The monorepo-relative lookup is
52
+ * a development-only fallback for checkouts without linked workspace packages.
53
+ */
54
+ function resolveRegistryPath(): string {
55
+ const require = createRequire(import.meta.url);
56
+ try {
57
+ return require.resolve(REGISTRY_REQUEST);
58
+ } catch (packageError) {
59
+ const here = dirname(fileURLToPath(import.meta.url));
60
+ const developmentFallback = resolve(here, "../../registry/registry.json");
61
+ if (process.env.NODE_ENV !== "production" && existsSync(developmentFallback)) {
62
+ return developmentFallback;
63
+ }
64
+ const reason = packageError instanceof Error ? packageError.message : String(packageError);
65
+ throw new RegistryLoadError(
66
+ `cannot load the packaged component registry. Expected "${REGISTRY_REQUEST}" to resolve ` +
67
+ `from this installation. Reinstall @usalyt/mcp-server (which depends on @usalyt/registry) ` +
68
+ `or install @usalyt/registry explicitly. Resolution error: ${reason}`
69
+ );
70
+ }
71
+ }
72
+
73
+ function assertRegistryShape(value: unknown, source: string): Registry {
74
+ if (typeof value !== "object" || value === null || !Array.isArray((value as Registry).items)) {
75
+ throw new RegistryLoadError(
76
+ `the packaged component registry at "${source}" is invalid: expected an object with an "items" array. ` +
77
+ `Reinstall @usalyt/registry to restore the packaged registry.`
78
+ );
79
+ }
80
+ for (const item of (value as Registry).items) {
81
+ if (
82
+ typeof item !== "object" ||
83
+ item === null ||
84
+ typeof item.name !== "string" ||
85
+ !Array.isArray(item.files)
86
+ ) {
87
+ throw new RegistryLoadError(
88
+ `the packaged component registry at "${source}" is invalid: every item needs a string "name" ` +
89
+ `and a "files" array. Reinstall @usalyt/registry to restore the packaged registry.`
90
+ );
91
+ }
92
+ }
93
+ return value as Registry;
94
+ }
95
+
96
+ export function loadRegistry(): Registry {
97
+ const registryPath = resolveRegistryPath();
98
+ let raw: string;
99
+ try {
100
+ raw = readFileSync(registryPath, "utf8");
101
+ } catch (error) {
102
+ const reason = error instanceof Error ? error.message : String(error);
103
+ throw new RegistryLoadError(
104
+ `cannot read the packaged component registry at "${registryPath}": ${reason}. ` +
105
+ `Reinstall @usalyt/registry to restore the packaged registry.`
106
+ );
107
+ }
108
+
109
+ let parsed: unknown;
110
+ try {
111
+ parsed = JSON.parse(raw);
112
+ } catch (error) {
113
+ const reason = error instanceof Error ? error.message : String(error);
114
+ throw new RegistryLoadError(
115
+ `the packaged component registry at "${registryPath}" is not valid JSON: ${reason}. ` +
116
+ `Reinstall @usalyt/registry to restore the packaged registry.`
117
+ );
118
+ }
119
+
120
+ return assertRegistryShape(parsed, registryPath);
121
+ }
122
+
123
+ function textResult(text: string): CallToolResult {
124
+ return { content: [{ type: "text", text }] };
125
+ }
126
+
127
+ function toolError(text: string): CallToolResult {
128
+ return { content: [{ type: "text", text }], isError: true };
129
+ }
130
+
131
+ export function createMcpServer(registry: Registry = loadRegistry()) {
132
+ const server = new Server(
133
+ {
134
+ name: SERVER_NAME,
135
+ version: SERVER_VERSION
136
+ },
137
+ {
138
+ capabilities: {
139
+ tools: {}
140
+ }
141
+ }
142
+ );
143
+
144
+ server.setRequestHandler(ListToolsRequestSchema, async () => ({
145
+ tools: [
146
+ {
147
+ name: "list_components",
148
+ description: "List all UI components and blocks available in the registry",
149
+ inputSchema: {
150
+ type: "object",
151
+ properties: {}
152
+ }
153
+ },
154
+ {
155
+ name: "get_component_code",
156
+ description: "Get the source code for a specific component",
157
+ inputSchema: {
158
+ type: "object",
159
+ properties: {
160
+ name: { type: "string" }
161
+ },
162
+ required: ["name"]
163
+ }
164
+ }
165
+ ]
166
+ }));
167
+
168
+ server.setRequestHandler(CallToolRequestSchema, async (request): Promise<CallToolResult> => {
169
+ if (request.params.name === "list_components") {
170
+ return textResult(JSON.stringify(registry.items, null, 2));
171
+ }
172
+
173
+ if (request.params.name === "get_component_code") {
174
+ const componentName = request.params.arguments?.name;
175
+ if (typeof componentName !== "string" || componentName.trim() === "") {
176
+ return toolError(
177
+ `Invalid "name" argument: expected a non-empty component name such as "button". ` +
178
+ `Call list_components to see the available component names.`
179
+ );
180
+ }
181
+
182
+ const item = registry.items.find((candidate) => candidate.name === componentName);
183
+ if (!item) {
184
+ return toolError(
185
+ `Component "${componentName}" was not found. ` +
186
+ `Call list_components to see the available component names.`
187
+ );
188
+ }
189
+
190
+ return textResult(JSON.stringify(item.files, null, 2));
191
+ }
192
+
193
+ throw new Error(`Tool not found: ${request.params.name}`);
194
+ });
195
+
196
+ return server;
197
+ }
198
+
199
+ export async function startServer(): Promise<void> {
200
+ const registry = loadRegistry();
201
+ const server = createMcpServer(registry);
202
+ const transport = new StdioServerTransport();
203
+ await server.connect(transport);
204
+ }
205
+
206
+ function isExecutableEntryPoint(): boolean {
207
+ const entry = process.argv[1];
208
+ if (!entry) {
209
+ return false;
210
+ }
211
+ try {
212
+ return realpathSync(entry) === realpathSync(fileURLToPath(import.meta.url));
213
+ } catch {
214
+ return false;
215
+ }
216
+ }
217
+
218
+ if (isExecutableEntryPoint()) {
219
+ startServer().catch((error: unknown) => {
220
+ const reason = error instanceof Error ? error.message : String(error);
221
+ process.stderr.write(`usalyt-mcp: ${reason}\n`, () => {
222
+ process.exit(1);
223
+ });
224
+ });
225
+ }