xmcp 0.6.13 → 0.7.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.
Files changed (161) hide show
  1. package/dist/cli.js +8 -8
  2. package/dist/index.js +2 -2
  3. package/dist/runtime/adapter-express.js +2 -2
  4. package/dist/runtime/adapter-fastify.js +2 -2
  5. package/dist/runtime/adapter-nestjs.js +7 -7
  6. package/dist/runtime/adapter-nextjs.js +11 -11
  7. package/dist/runtime/http.js +3 -3
  8. package/dist/runtime/stdio.js +8 -8
  9. package/package.json +10 -11
  10. package/src/auth/api-key.ts +0 -120
  11. package/src/auth/jwt.ts +0 -52
  12. package/src/cli/commands/create/__tests__/create.test.ts +0 -99
  13. package/src/cli/commands/create/index.ts +0 -74
  14. package/src/cli/commands/create/templates/index.ts +0 -4
  15. package/src/cli/commands/create/templates/prompt.ts +0 -30
  16. package/src/cli/commands/create/templates/resource.ts +0 -20
  17. package/src/cli/commands/create/templates/tool.ts +0 -34
  18. package/src/cli/commands/create/templates/widget.ts +0 -33
  19. package/src/cli/utils/naming.ts +0 -39
  20. package/src/cli.ts +0 -125
  21. package/src/client/headers.ts +0 -61
  22. package/src/client/index.ts +0 -180
  23. package/src/client/types.ts +0 -122
  24. package/src/client/utils.ts +0 -133
  25. package/src/cloudflare.ts +0 -6
  26. package/src/compiler/build-telemetry.ts +0 -88
  27. package/src/compiler/client/component-compiler.ts +0 -250
  28. package/src/compiler/client/transpile.ts +0 -21
  29. package/src/compiler/compiler-context.ts +0 -61
  30. package/src/compiler/config/__tests__/config.test.ts +0 -459
  31. package/src/compiler/config/index.ts +0 -62
  32. package/src/compiler/config/injection.ts +0 -210
  33. package/src/compiler/config/schemas/bundler.ts +0 -12
  34. package/src/compiler/config/schemas/experimental/index.ts +0 -22
  35. package/src/compiler/config/schemas/index.ts +0 -20
  36. package/src/compiler/config/schemas/paths.ts +0 -35
  37. package/src/compiler/config/schemas/template.ts +0 -80
  38. package/src/compiler/config/schemas/transport/http.ts +0 -116
  39. package/src/compiler/config/schemas/transport/stdio.ts +0 -31
  40. package/src/compiler/config/schemas/typescript.ts +0 -26
  41. package/src/compiler/config/utils.ts +0 -168
  42. package/src/compiler/generate-env-code.ts +0 -5
  43. package/src/compiler/generate-import-code.ts +0 -187
  44. package/src/compiler/generate-tools-code.ts +0 -174
  45. package/src/compiler/get-bundler-config/get-entries.ts +0 -44
  46. package/src/compiler/get-bundler-config/get-externals.ts +0 -114
  47. package/src/compiler/get-bundler-config/get-injected-variables.ts +0 -46
  48. package/src/compiler/get-bundler-config/index.ts +0 -288
  49. package/src/compiler/get-bundler-config/plugins/index.ts +0 -140
  50. package/src/compiler/get-bundler-config/plugins/types/express.ts +0 -3
  51. package/src/compiler/get-bundler-config/plugins/types/fastify.ts +0 -4
  52. package/src/compiler/get-bundler-config/plugins/types/index.ts +0 -5
  53. package/src/compiler/get-bundler-config/plugins/types/nest.ts +0 -65
  54. package/src/compiler/get-bundler-config/plugins/types/next.ts +0 -52
  55. package/src/compiler/get-bundler-config/resolve-tsconfig-paths.ts +0 -51
  56. package/src/compiler/index.ts +0 -484
  57. package/src/compiler/on-first-build.ts +0 -54
  58. package/src/compiler/parse-xmcp-config.ts +0 -200
  59. package/src/compiler/start-http-server.ts +0 -42
  60. package/src/compiler/utils/__tests__/config-detection.test.ts +0 -307
  61. package/src/compiler/utils/__tests__/path-utils.test.ts +0 -137
  62. package/src/compiler/utils/config-detection.ts +0 -34
  63. package/src/compiler/utils/path-utils.ts +0 -22
  64. package/src/compiler/watcher-recovery.ts +0 -72
  65. package/src/host-bridge.ts +0 -36
  66. package/src/index.ts +0 -94
  67. package/src/platforms/build-cloudflare-output.ts +0 -116
  68. package/src/platforms/build-vercel-output.ts +0 -131
  69. package/src/plugins/index.ts +0 -1
  70. package/src/plugins/x402/index.ts +0 -24
  71. package/src/runtime/adapters/express/index.ts +0 -75
  72. package/src/runtime/adapters/fastify/__tests__/fastify.test.ts +0 -115
  73. package/src/runtime/adapters/fastify/index.ts +0 -78
  74. package/src/runtime/adapters/nestjs/index.ts +0 -16
  75. package/src/runtime/adapters/nestjs/xmcp.controller.ts +0 -34
  76. package/src/runtime/adapters/nestjs/xmcp.guard.ts +0 -101
  77. package/src/runtime/adapters/nestjs/xmcp.oauth.controller.ts +0 -36
  78. package/src/runtime/adapters/nestjs/xmcp.oauth.module.ts +0 -21
  79. package/src/runtime/adapters/nestjs/xmcp.oauth.service.ts +0 -36
  80. package/src/runtime/adapters/nestjs/xmcp.service.ts +0 -96
  81. package/src/runtime/adapters/nestjs/xmcp.utils.ts +0 -11
  82. package/src/runtime/adapters/nextjs/auth.ts +0 -265
  83. package/src/runtime/adapters/nextjs/handler/error-handler.ts +0 -67
  84. package/src/runtime/adapters/nextjs/handler/node-to-web-adapter.ts +0 -143
  85. package/src/runtime/adapters/nextjs/handler/request-converter.ts +0 -74
  86. package/src/runtime/adapters/nextjs/handler/server-lifecycle.ts +0 -69
  87. package/src/runtime/adapters/nextjs/index.ts +0 -82
  88. package/src/runtime/contexts/client-info-context.ts +0 -16
  89. package/src/runtime/contexts/http-request-context.ts +0 -21
  90. package/src/runtime/contexts/http-transport-context.ts +0 -18
  91. package/src/runtime/headers.ts +0 -42
  92. package/src/runtime/middlewares/utils.ts +0 -72
  93. package/src/runtime/platforms/cloudflare/cors.ts +0 -44
  94. package/src/runtime/platforms/cloudflare/middlewares/api-key.ts +0 -102
  95. package/src/runtime/platforms/cloudflare/middlewares/jwt.ts +0 -127
  96. package/src/runtime/platforms/cloudflare/types.ts +0 -23
  97. package/src/runtime/platforms/cloudflare/worker.ts +0 -270
  98. package/src/runtime/templates/home.ts +0 -746
  99. package/src/runtime/transports/http/base-streamable-http.ts +0 -46
  100. package/src/runtime/transports/http/cors/headers.ts +0 -58
  101. package/src/runtime/transports/http/cors/index.ts +0 -34
  102. package/src/runtime/transports/http/index.ts +0 -78
  103. package/src/runtime/transports/http/stateless-streamable-http.ts +0 -311
  104. package/src/runtime/transports/http/web-stateless-http.ts +0 -288
  105. package/src/runtime/transports/stdio/index.ts +0 -109
  106. package/src/runtime/utils/__tests__/client-info.test.ts +0 -146
  107. package/src/runtime/utils/__tests__/tool-extra.test.ts +0 -239
  108. package/src/runtime/utils/client-info.ts +0 -114
  109. package/src/runtime/utils/elicitation.ts +0 -396
  110. package/src/runtime/utils/ext-apps-registry.ts +0 -151
  111. package/src/runtime/utils/mcp-app-protocol.ts +0 -16
  112. package/src/runtime/utils/mcp-app-types.ts +0 -113
  113. package/src/runtime/utils/mcp-host-bridge.ts +0 -473
  114. package/src/runtime/utils/path-to-tool-name.ts +0 -69
  115. package/src/runtime/utils/prompt-loader.ts +0 -151
  116. package/src/runtime/utils/prompts.ts +0 -69
  117. package/src/runtime/utils/react/generate-html.ts +0 -69
  118. package/src/runtime/utils/react/index.ts +0 -6
  119. package/src/runtime/utils/request-tool-names.ts +0 -48
  120. package/src/runtime/utils/resource-loader.ts +0 -151
  121. package/src/runtime/utils/resources.ts +0 -331
  122. package/src/runtime/utils/server.ts +0 -107
  123. package/src/runtime/utils/tool-loader.ts +0 -149
  124. package/src/runtime/utils/tools.ts +0 -183
  125. package/src/runtime/utils/transformers/prompt.ts +0 -120
  126. package/src/runtime/utils/transformers/resource.ts +0 -196
  127. package/src/runtime/utils/transformers/tool.ts +0 -400
  128. package/src/runtime/utils/ui/__tests__/split-meta.test.ts +0 -803
  129. package/src/runtime/utils/ui/flatten-meta.ts +0 -44
  130. package/src/runtime/utils/ui/split-meta.ts +0 -78
  131. package/src/runtime/utils/utils/resource-uri-composer.ts +0 -204
  132. package/src/runtime/utils/validators/index.ts +0 -138
  133. package/src/telemetry/ci-info.ts +0 -6
  134. package/src/telemetry/debug.ts +0 -8
  135. package/src/telemetry/events/definitions.ts +0 -52
  136. package/src/telemetry/events/detached-flush.ts +0 -83
  137. package/src/telemetry/events/post-payload.ts +0 -141
  138. package/src/telemetry/events/tracker.ts +0 -281
  139. package/src/telemetry/index.ts +0 -7
  140. package/src/telemetry/metadata.ts +0 -49
  141. package/src/telemetry/project-id.ts +0 -35
  142. package/src/telemetry/storage.ts +0 -218
  143. package/src/types/adapter.d.ts +0 -5
  144. package/src/types/client-info.ts +0 -20
  145. package/src/types/declarations.ts +0 -37
  146. package/src/types/injected.d.ts +0 -62
  147. package/src/types/middleware.ts +0 -19
  148. package/src/types/prompt.ts +0 -6
  149. package/src/types/resource.ts +0 -15
  150. package/src/types/tool.ts +0 -192
  151. package/src/types/ui-meta.ts +0 -42
  152. package/src/utils/cli-icons.ts +0 -5
  153. package/src/utils/constants.ts +0 -31
  154. package/src/utils/context.ts +0 -105
  155. package/src/utils/detect-package-manager.ts +0 -46
  156. package/src/utils/file-watcher.ts +0 -66
  157. package/src/utils/fs-utils.ts +0 -16
  158. package/src/utils/path-validation.ts +0 -66
  159. package/src/utils/port-utils.ts +0 -43
  160. package/src/utils/spawn-process.ts +0 -49
  161. package/src/utils/subscribable.ts +0 -62
@@ -1,484 +0,0 @@
1
- import { rspack } from "@rspack/core";
2
- import { getRspackConfig } from "./get-bundler-config";
3
- import chalk from "chalk";
4
- import { getConfig } from "./parse-xmcp-config";
5
- import { generateImportCode } from "./generate-import-code";
6
- import {
7
- generateToolsExportCode,
8
- generateToolsTypesCode,
9
- } from "./generate-tools-code";
10
- import fs from "fs";
11
- import { rootFolder, runtimeFolderPath } from "@/utils/constants";
12
- import { createFolder } from "@/utils/fs-utils";
13
- import path from "path";
14
- import { deleteSync } from "del";
15
- import dotenv from "dotenv";
16
- export { type Middleware } from "@/types/middleware";
17
- import { generateEnvCode } from "./generate-env-code";
18
- import { Watcher } from "@/utils/file-watcher";
19
- import { onFirstBuild } from "./on-first-build";
20
- import { greenCheck } from "@/utils/cli-icons";
21
- import {
22
- telemetry,
23
- TelemetryEventName,
24
- TransportType,
25
- AdapterType,
26
- ErrorPhase,
27
- } from "../telemetry";
28
- import { isReactFile } from "../runtime/utils/react";
29
- import { compilerContext } from "./compiler-context";
30
- import { startHttpServer } from "./start-http-server";
31
- import { logBuildFailure, logBuildSuccess } from "./build-telemetry";
32
- import { isValidPath } from "@/utils/path-validation";
33
- import { getResolvedPathsConfig } from "./config/utils";
34
- import { pathToToolName } from "./utils/path-utils";
35
- import { transpileClientComponent } from "./client/transpile";
36
- import { buildCloudflareOutput } from "../platforms/build-cloudflare-output";
37
- import {
38
- addWatchedPath,
39
- removeWatchedPath,
40
- } from "./watcher-recovery";
41
- const { version: XMCP_VERSION } = require("../../package.json");
42
- dotenv.config();
43
-
44
- export type CompilerMode = "development" | "production";
45
-
46
- export interface CompileOptions {
47
- onBuild?: () => void;
48
- }
49
-
50
- export async function compile({ onBuild }: CompileOptions = {}) {
51
- const { mode, toolPaths, promptPaths, resourcePaths, platforms } =
52
- compilerContext.getContext();
53
- const startTime = Date.now();
54
- let compilerStarted = false;
55
-
56
- const xmcpConfig = await getConfig();
57
- compilerContext.setContext({
58
- xmcpConfig: xmcpConfig,
59
- });
60
- const staleImportErrorSuppressions = new Map<string, number>();
61
- let bundlerConfig = getRspackConfig(xmcpConfig);
62
- let lastRestartedBuildHash: string | null = null;
63
- let restartInFlight: Promise<void> | null = null;
64
-
65
- const restartHttpServerSafely = async () => {
66
- if (restartInFlight) {
67
- return restartInFlight;
68
- }
69
-
70
- restartInFlight = (async () => {
71
- await startHttpServer();
72
- })();
73
-
74
- try {
75
- await restartInFlight;
76
- } finally {
77
- restartInFlight = null;
78
- }
79
- };
80
-
81
- if (xmcpConfig.bundler) {
82
- bundlerConfig = xmcpConfig.bundler(bundlerConfig);
83
- }
84
-
85
- const watcher = new Watcher({
86
- // keep the watcher running on dev mode after "onReady"
87
- persistent: mode === "development",
88
- ignored: /(^|[\/\\])\../,
89
- ignoreInitial: false,
90
- });
91
-
92
- // handle tools
93
- let toolsPath = isValidPath(
94
- getResolvedPathsConfig(xmcpConfig).tools,
95
- "tools"
96
- );
97
-
98
- // handle tools
99
- if (toolsPath) {
100
- watcher.watch(`${toolsPath}/**/*.{ts,tsx}`, {
101
- onAdd: async (filePath) => {
102
- addWatchedPath(toolPaths, filePath);
103
- if (compilerStarted) {
104
- await generateCode();
105
- }
106
- },
107
- onUnlink: async (filePath) => {
108
- removeWatchedPath(toolPaths, filePath);
109
- if (compilerStarted) {
110
- await generateCode();
111
- }
112
- },
113
- onChange: async () => {
114
- if (compilerStarted) {
115
- await generateCode();
116
- }
117
- },
118
- });
119
- }
120
-
121
- // handle prompts
122
- let promptsPath = isValidPath(
123
- getResolvedPathsConfig(xmcpConfig).prompts,
124
- "prompts"
125
- );
126
-
127
- // handle prompts
128
- if (promptsPath) {
129
- watcher.watch(`${promptsPath}/**/*.{ts,tsx}`, {
130
- onAdd: async (filePath) => {
131
- addWatchedPath(promptPaths, filePath);
132
- if (compilerStarted) {
133
- await generateCode({ rebuildClientBundles: false });
134
- }
135
- },
136
- onChange: async () => {
137
- if (compilerStarted) {
138
- await generateCode({ rebuildClientBundles: false });
139
- }
140
- },
141
- onUnlink: async (filePath) => {
142
- removeWatchedPath(promptPaths, filePath);
143
- if (compilerStarted) {
144
- await generateCode({ rebuildClientBundles: false });
145
- }
146
- },
147
- });
148
- }
149
-
150
- // handle resources
151
- let resourcesPath = isValidPath(
152
- getResolvedPathsConfig(xmcpConfig).resources,
153
- "resources"
154
- );
155
-
156
- // handle resources
157
- if (resourcesPath) {
158
- watcher.watch(`${resourcesPath}/**/*.{ts,tsx}`, {
159
- onAdd: async (filePath) => {
160
- addWatchedPath(resourcePaths, filePath);
161
- if (compilerStarted) {
162
- await generateCode({ rebuildClientBundles: false });
163
- }
164
- },
165
- onChange: async () => {
166
- if (compilerStarted) {
167
- await generateCode({ rebuildClientBundles: false });
168
- }
169
- },
170
- onUnlink: async (filePath) => {
171
- removeWatchedPath(resourcePaths, filePath);
172
- if (compilerStarted) {
173
- await generateCode({ rebuildClientBundles: false });
174
- }
175
- },
176
- });
177
- }
178
-
179
- // if adapter is not enabled, handle middleware
180
- if (!xmcpConfig.experimental?.adapter) {
181
- // handle middleware
182
- watcher.watch("./src/middleware.ts", {
183
- onAdd: async () => {
184
- compilerContext.setContext({
185
- hasMiddleware: true,
186
- });
187
- if (compilerStarted) {
188
- await generateCode();
189
- }
190
- },
191
- onUnlink: async () => {
192
- compilerContext.setContext({
193
- hasMiddleware: false,
194
- });
195
- if (compilerStarted) {
196
- await generateCode();
197
- }
198
- },
199
- });
200
- }
201
-
202
- // start compiler
203
- watcher.onReady(async () => {
204
- let firstBuild = true;
205
- compilerStarted = true;
206
-
207
- // delete existing runtime folder
208
- deleteSync(runtimeFolderPath);
209
- createFolder(runtimeFolderPath);
210
-
211
- // Generate all code (including client bundles) BEFORE bundler runs
212
- await generateCode();
213
-
214
- rspack(bundlerConfig, async (err, stats) => {
215
- // Track compilation time
216
- let compilationTime: number;
217
- if (stats?.endTime && stats?.startTime) {
218
- compilationTime = stats.endTime - stats.startTime;
219
- } else {
220
- compilationTime = Date.now() - startTime;
221
- }
222
-
223
- const collectBaseTelemetryData = () => {
224
- const reactToolsCount =
225
- Array.from(toolPaths).filter(isReactFile).length;
226
-
227
- return {
228
- duration: compilationTime,
229
- toolsCount: toolPaths.size,
230
- reactToolsCount,
231
- promptsCount: promptPaths.size,
232
- resourcesCount: resourcePaths.size,
233
- transport: xmcpConfig.http ? TransportType.HTTP : TransportType.STDIO,
234
- adapter: xmcpConfig.experimental?.adapter
235
- ? (xmcpConfig.experimental.adapter as AdapterType)
236
- : AdapterType.NONE,
237
- nodeVersion: process.version,
238
- xmcpVersion: XMCP_VERSION,
239
- };
240
- };
241
-
242
- const getOutputSize = () => {
243
- let outputSize = 0;
244
- try {
245
- const distPath = path.join(process.cwd(), "dist");
246
- if (fs.existsSync(distPath)) {
247
- const files = fs.readdirSync(distPath);
248
- files.forEach((file) => {
249
- const filePath = path.join(distPath, file);
250
- const stat = fs.statSync(filePath);
251
- if (stat.isFile()) {
252
- outputSize += stat.size;
253
- }
254
- });
255
- }
256
- } catch (e) {
257
- // Ignore errors getting output size
258
- }
259
- return outputSize;
260
- };
261
-
262
- // Handle errors
263
- if (err || stats?.hasErrors()) {
264
- if (err) {
265
- console.error(err);
266
- }
267
- if (stats?.hasErrors()) {
268
- const statsJson = stats.toJson({
269
- all: false,
270
- errors: true,
271
- warnings: false,
272
- });
273
- const hasOnlyStaleImportMapError =
274
- mode === "development" &&
275
- !!statsJson.errors?.length &&
276
- statsJson.errors.every((error) => {
277
- const message =
278
- typeof error.message === "string" ? error.message : "";
279
- const moduleName =
280
- typeof error.moduleName === "string" ? error.moduleName : "";
281
- return (
282
- (message.includes("Module not found") ||
283
- message.includes("Can't resolve")) &&
284
- (message.includes(".xmcp/import-map.js") ||
285
- moduleName.includes(".xmcp/import-map.js"))
286
- );
287
- });
288
-
289
- if (hasOnlyStaleImportMapError) {
290
- const staleErrors = statsJson.errors ?? [];
291
- const staleSignature = staleErrors
292
- .map((error) =>
293
- typeof error.message === "string" ? error.message : "unknown"
294
- )
295
- .sort()
296
- .join("|");
297
- const staleCount =
298
- (staleImportErrorSuppressions.get(staleSignature) ?? 0) + 1;
299
- staleImportErrorSuppressions.set(staleSignature, staleCount);
300
-
301
- if (staleCount === 1) {
302
- return;
303
- }
304
- }
305
-
306
- console.error(
307
- stats.toString({
308
- colors: true,
309
- chunks: false,
310
- })
311
- );
312
- }
313
-
314
- // Track failed build (only in production)
315
- if (mode === "production") {
316
- const statsJson = stats?.toJson({
317
- all: false,
318
- errors: true,
319
- warnings: false,
320
- });
321
- const statsError = statsJson?.errors?.[0];
322
- const errorType =
323
- (err && err.constructor ? err.constructor.name : undefined) ||
324
- (statsError?.name as string | undefined) ||
325
- (typeof statsError?.moduleName === "string"
326
- ? statsError.moduleName
327
- : undefined) ||
328
- (statsError?.message as string | undefined) ||
329
- "WebpackError";
330
-
331
- logBuildFailure({
332
- ...collectBaseTelemetryData(),
333
- errorPhase: ErrorPhase.WEBPACK,
334
- errorType,
335
- });
336
- }
337
-
338
- return;
339
- }
340
-
341
- // Build succeeded
342
- staleImportErrorSuppressions.clear();
343
- if (firstBuild) {
344
- onFirstBuild(xmcpConfig);
345
- } else {
346
- // on dev mode, bundler will recompile the code, so we need to start the http server after the first one
347
- if (
348
- mode === "development" &&
349
- xmcpConfig["http"] &&
350
- !xmcpConfig.experimental?.adapter
351
- ) {
352
- const currentBuildHash =
353
- typeof stats?.hash === "string" ? stats.hash : null;
354
- const shouldRestart =
355
- currentBuildHash === null
356
- ? lastRestartedBuildHash === null
357
- : currentBuildHash !== lastRestartedBuildHash;
358
-
359
- if (shouldRestart) {
360
- lastRestartedBuildHash = currentBuildHash;
361
- await restartHttpServerSafely();
362
- }
363
- }
364
- }
365
-
366
- if (mode === "development" && platforms.cloudflare) {
367
- try {
368
- await buildCloudflareOutput({ log: firstBuild });
369
- } catch (error) {
370
- console.error(
371
- chalk.red("❌ Failed to sync Cloudflare Workers output:"),
372
- error
373
- );
374
- }
375
- }
376
-
377
- if (mode === "production") {
378
- logBuildSuccess({
379
- ...collectBaseTelemetryData(),
380
- outputSize: getOutputSize(),
381
- });
382
- }
383
-
384
- // user defined callback should fire after every successful build
385
- onBuild?.();
386
-
387
- // Choose color based on compilation time
388
- let timeColor = (str: string) => str;
389
- if (mode === "development") {
390
- if (compilationTime > 1000) {
391
- timeColor = chalk.bold.red;
392
- } else if (compilationTime > 500) {
393
- timeColor = chalk.bold.yellow;
394
- }
395
- }
396
-
397
- console.log(
398
- `${greenCheck} Compiled in ${timeColor(`${compilationTime}ms`)}`
399
- );
400
-
401
- firstBuild = false;
402
- // Compiler callback ends
403
- });
404
- });
405
- }
406
-
407
- /**
408
- * Builds client bundles for all React tool components (.tsx files)
409
- * Returns a map of tool names to their bundle paths
410
- */
411
- async function buildClientBundles(): Promise<Map<string, string> | undefined> {
412
- const { toolPaths } = compilerContext.getContext();
413
- const reactToolPaths = Array.from(toolPaths).filter((toolPath) =>
414
- toolPath.endsWith(".tsx")
415
- );
416
-
417
- if (reactToolPaths.length === 0) {
418
- return undefined;
419
- }
420
-
421
- const entries = new Map<string, string>();
422
- const clientBundles = new Map<string, string>();
423
-
424
- const outputDir = "dist/client";
425
-
426
- for (const reactToolPath of reactToolPaths) {
427
- const toolName = pathToToolName(reactToolPath);
428
- const bundlePath = `${outputDir}/${toolName}.bundle.js`;
429
-
430
- entries.set(toolName, reactToolPath);
431
- clientBundles.set(toolName, bundlePath);
432
- }
433
-
434
- await transpileClientComponent(entries, outputDir);
435
-
436
- return clientBundles;
437
- }
438
-
439
- /**
440
- * Generates all runtime code and builds client bundles if needed
441
- * This centralizes all code generation logic including client bundle building
442
- */
443
- async function generateCode({
444
- rebuildClientBundles = true,
445
- }: {
446
- rebuildClientBundles?: boolean;
447
- } = {}) {
448
- const { clientBundles: currentClientBundles } = compilerContext.getContext();
449
- const clientBundles =
450
- rebuildClientBundles || currentClientBundles === undefined
451
- ? await buildClientBundles()
452
- : currentClientBundles;
453
-
454
- // Store in context for import map generation
455
- compilerContext.setContext({ clientBundles });
456
-
457
- // Generate import map code (includes client bundles)
458
- const fileContent = generateImportCode();
459
- writeFileIfChanged(path.join(runtimeFolderPath, "import-map.js"), fileContent);
460
-
461
- // Generate runtime exports for global access
462
- const runtimeExportsCode = generateEnvCode();
463
- const envFilePath = path.join(rootFolder, "xmcp-env.d.ts");
464
- writeFileIfChanged(envFilePath, runtimeExportsCode);
465
-
466
- // only generating tools files for nextjs adapter mode
467
- const { xmcpConfig } = compilerContext.getContext();
468
- if (xmcpConfig?.experimental?.adapter === "nextjs") {
469
- const toolsCode = generateToolsExportCode();
470
- writeFileIfChanged(path.join(runtimeFolderPath, "tools.js"), toolsCode);
471
- const typesCode = generateToolsTypesCode();
472
- writeFileIfChanged(path.join(runtimeFolderPath, "tools.d.ts"), typesCode);
473
- }
474
- }
475
-
476
- function writeFileIfChanged(filePath: string, content: string) {
477
- if (fs.existsSync(filePath)) {
478
- const currentContent = fs.readFileSync(filePath, "utf8");
479
- if (currentContent === content) {
480
- return;
481
- }
482
- }
483
- fs.writeFileSync(filePath, content);
484
- }
@@ -1,54 +0,0 @@
1
- import { greenCheck } from "../utils/cli-icons";
2
- import { XmcpConfigOutputSchema } from "./config";
3
- import { compilerContext } from "./compiler-context";
4
- import chalk from "chalk";
5
-
6
- export function onFirstBuild(xmcpConfig: XmcpConfigOutputSchema) {
7
- const builtResults = [];
8
-
9
- if (xmcpConfig.stdio) {
10
- builtResults.push(`${greenCheck} Built STDIO server`);
11
- }
12
- if (xmcpConfig["http"]) {
13
- if (xmcpConfig.experimental?.adapter) {
14
- builtResults.push(`${greenCheck} Built Adapter`);
15
- } else {
16
- builtResults.push(`${greenCheck} Built HTTP server`);
17
- }
18
- }
19
-
20
- builtResults.forEach((result) => {
21
- console.log(result);
22
- });
23
-
24
- logBuildSummary();
25
- }
26
-
27
- function logBuildSummary() {
28
- const { toolPaths, promptPaths, resourcePaths } =
29
- compilerContext.getContext();
30
- const toolsCount = toolPaths.size;
31
- const promptsCount = promptPaths.size;
32
- const resourcesCount = resourcePaths.size;
33
-
34
- const parts: string[] = [];
35
- if (toolsCount > 0) {
36
- parts.push(`${chalk.bold(toolsCount)} tool${toolsCount !== 1 ? "s" : ""}`);
37
- }
38
- if (promptsCount > 0) {
39
- parts.push(
40
- `${chalk.bold(promptsCount)} prompt${promptsCount !== 1 ? "s" : ""}`
41
- );
42
- }
43
- if (resourcesCount > 0) {
44
- parts.push(
45
- `${chalk.bold(resourcesCount)} resource${resourcesCount !== 1 ? "s" : ""}`
46
- );
47
- }
48
-
49
- if (parts.length > 0) {
50
- console.log(`${greenCheck} Registered ${parts.join(", ")}`);
51
- } else {
52
- console.log(`${greenCheck} No tools, prompts, or resources registered`);
53
- }
54
- }