xmcp 0.6.4 → 0.6.6
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/dist/cli.js +45 -29
- package/dist/compiler/config/index.d.ts +59 -0
- package/dist/compiler/config/schemas/template.d.ts +40 -0
- package/dist/compiler/config/schemas/transport/stdio.d.ts +5 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +4 -4
- package/dist/runtime/adapter-express.js +15 -38
- package/dist/runtime/adapter-nestjs.js +17 -40
- package/dist/runtime/adapter-nextjs.js +16 -39
- package/dist/runtime/http.js +23 -46
- package/dist/runtime/stdio.js +16 -39
- package/dist/types/resource.d.ts +3 -3
- package/dist/types/tool.d.ts +4 -4
- package/dist/types/ui-meta.d.ts +22 -0
- package/package.json +2 -2
- package/src/compiler/client/component-compiler.ts +0 -4
- package/src/compiler/config/__tests__/config.test.ts +36 -0
- package/src/compiler/config/injection.ts +63 -1
- package/src/compiler/config/schemas/template.ts +17 -0
- package/src/compiler/config/schemas/transport/stdio.ts +1 -0
- package/src/compiler/generate-tools-code.ts +21 -1
- package/src/compiler/get-bundler-config/get-injected-variables.ts +3 -0
- package/src/compiler/index.ts +100 -24
- package/src/compiler/on-first-build.ts +34 -50
- package/src/compiler/start-http-server.ts +11 -4
- package/src/compiler/watcher-recovery.ts +72 -0
- package/src/index.ts +2 -1
- package/src/runtime/transports/stdio/index.ts +19 -0
- package/src/runtime/utils/react/generate-html.ts +0 -32
- package/src/runtime/utils/resources.ts +15 -15
- package/src/runtime/utils/server.ts +5 -8
- package/src/runtime/utils/tools.ts +55 -71
- package/src/runtime/utils/transformers/tool.ts +148 -30
- package/src/types/resource.ts +3 -3
- package/src/types/tool.ts +4 -4
- package/src/types/ui-meta.ts +26 -0
- package/dist/types/openai-meta.d.ts +0 -173
- package/src/runtime/utils/openai/flatten-meta.ts +0 -44
- package/src/runtime/utils/openai/split-meta.ts +0 -81
- package/src/runtime/utils/openai-resource-registry.ts +0 -58
- package/src/types/openai-meta.ts +0 -191
package/dist/types/resource.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UIMetadata } from "./ui-meta";
|
|
2
2
|
export interface ResourceMetadata {
|
|
3
3
|
name: string;
|
|
4
4
|
title?: string;
|
|
5
5
|
description?: string;
|
|
6
6
|
mimeType?: string;
|
|
7
7
|
size?: number;
|
|
8
|
-
/** Metadata for the resource.
|
|
8
|
+
/** Metadata for the resource. */
|
|
9
9
|
_meta?: {
|
|
10
|
-
|
|
10
|
+
ui?: UIMetadata;
|
|
11
11
|
[key: string]: unknown;
|
|
12
12
|
};
|
|
13
13
|
[key: string]: any;
|
package/dist/types/tool.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { z } from "zod/v3";
|
|
2
2
|
import type { ZodType as ZodTypeV4, infer as inferV4 } from "zod";
|
|
3
|
-
import {
|
|
3
|
+
import { UIMetadata } from "./ui-meta";
|
|
4
4
|
export interface ToolAnnotations {
|
|
5
5
|
/** Human-readable title for the tool */
|
|
6
6
|
title?: string;
|
|
@@ -21,15 +21,15 @@ export interface ToolMetadata {
|
|
|
21
21
|
description: string;
|
|
22
22
|
/** Optional hints about tool behavior */
|
|
23
23
|
annotations?: ToolAnnotations;
|
|
24
|
-
/** Metadata for the tool.
|
|
24
|
+
/** Metadata for the tool. */
|
|
25
25
|
_meta?: {
|
|
26
|
-
|
|
27
|
-
ui?: OpenAIMetadata;
|
|
26
|
+
ui?: UIMetadata;
|
|
28
27
|
[key: string]: unknown;
|
|
29
28
|
};
|
|
30
29
|
}
|
|
31
30
|
type CompatibleZodType = z.ZodTypeAny | ZodTypeV4<unknown>;
|
|
32
31
|
export type ToolSchema = Record<string, CompatibleZodType>;
|
|
32
|
+
export type ToolOutputSchema = Record<string, CompatibleZodType>;
|
|
33
33
|
/**
|
|
34
34
|
* Extra arguments passed to MCP tool functions.
|
|
35
35
|
*/
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface UIMetadata {
|
|
2
|
+
/**
|
|
3
|
+
* URI template for the widget resource.
|
|
4
|
+
* Auto-generated as `ui://app/{toolName}.html` when omitted.
|
|
5
|
+
*/
|
|
6
|
+
resourceUri?: string;
|
|
7
|
+
/**
|
|
8
|
+
* Content Security Policy configuration for widget resources.
|
|
9
|
+
*/
|
|
10
|
+
csp?: {
|
|
11
|
+
connectDomains?: string[];
|
|
12
|
+
resourceDomains?: string[];
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Optional dedicated origin for this widget.
|
|
16
|
+
*/
|
|
17
|
+
domain?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Visual hint indicating the widget prefers a border.
|
|
20
|
+
*/
|
|
21
|
+
prefersBorder?: boolean;
|
|
22
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xmcp",
|
|
3
3
|
"description": "The MCP framework",
|
|
4
|
-
"version": "0.6.
|
|
4
|
+
"version": "0.6.6",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "xmcp",
|
|
7
7
|
"email": "support@xmcp.dev",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"test:config": "node --import tsx --test src/compiler/config/__tests__/*.test.ts"
|
|
69
69
|
},
|
|
70
70
|
"dependencies": {
|
|
71
|
-
"@modelcontextprotocol/sdk": "^1.
|
|
71
|
+
"@modelcontextprotocol/sdk": "^1.26.0",
|
|
72
72
|
"@rspack/core": "^1.6.7",
|
|
73
73
|
"jose": "^6.1.3",
|
|
74
74
|
"postcss-loader": "^8.2.0",
|
|
@@ -125,10 +125,6 @@ export class ClientComponentCompiler {
|
|
|
125
125
|
root.render(React.createElement(Component, props));
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
if (window.openai !== undefined) {
|
|
129
|
-
render(window.openai.toolInput ?? {});
|
|
130
|
-
}
|
|
131
|
-
|
|
132
128
|
window.addEventListener("message", (event) => {
|
|
133
129
|
if (event.source !== window.parent && event.source !== null) return;
|
|
134
130
|
|
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
injectTypescriptVariables,
|
|
18
18
|
injectAdapterVariables,
|
|
19
19
|
injectStdioVariables,
|
|
20
|
+
injectServerInfoVariables,
|
|
20
21
|
} from "../injection";
|
|
21
22
|
import { configSchema } from "../index";
|
|
22
23
|
|
|
@@ -278,6 +279,41 @@ describe("Config System - Injection Functions", () => {
|
|
|
278
279
|
const variables = injectStdioVariables(undefined);
|
|
279
280
|
assert.deepEqual(variables, {});
|
|
280
281
|
});
|
|
282
|
+
|
|
283
|
+
it("should inject server info with empty icons when icons not configured", () => {
|
|
284
|
+
const config = configSchema.parse({ template: {} });
|
|
285
|
+
const variables = injectServerInfoVariables(config);
|
|
286
|
+
|
|
287
|
+
assert.notEqual(variables.SERVER_INFO, undefined);
|
|
288
|
+
const serverInfo = JSON.parse(variables.SERVER_INFO);
|
|
289
|
+
assert.equal(serverInfo.name, "xmcp server");
|
|
290
|
+
assert.deepEqual(serverInfo.icons, []);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it("should inject server info with user-supplied icons", () => {
|
|
294
|
+
const config = configSchema.parse({
|
|
295
|
+
template: {
|
|
296
|
+
name: "My Server",
|
|
297
|
+
icons: [{ src: "https://example.com/icon.png", mimeType: "image/png" }],
|
|
298
|
+
},
|
|
299
|
+
});
|
|
300
|
+
const variables = injectServerInfoVariables(config);
|
|
301
|
+
|
|
302
|
+
const serverInfo = JSON.parse(variables.SERVER_INFO);
|
|
303
|
+
assert.equal(serverInfo.name, "My Server");
|
|
304
|
+
assert.deepEqual(serverInfo.icons, [
|
|
305
|
+
{ src: "https://example.com/icon.png", mimeType: "image/png" },
|
|
306
|
+
]);
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it("should inject server info with version as a string", () => {
|
|
310
|
+
const config = configSchema.parse({});
|
|
311
|
+
const variables = injectServerInfoVariables(config);
|
|
312
|
+
|
|
313
|
+
const serverInfo = JSON.parse(variables.SERVER_INFO);
|
|
314
|
+
assert.equal(typeof serverInfo.version, "string");
|
|
315
|
+
assert.notEqual(serverInfo.version, "");
|
|
316
|
+
});
|
|
281
317
|
});
|
|
282
318
|
|
|
283
319
|
describe("Config System - Edge Cases", () => {
|
|
@@ -77,10 +77,12 @@ export function injectStdioVariables(
|
|
|
77
77
|
if (!stdioConfig) return {};
|
|
78
78
|
|
|
79
79
|
const debug = typeof stdioConfig === "object" ? stdioConfig.debug : false;
|
|
80
|
+
const silent = typeof stdioConfig === "object" ? stdioConfig.silent : false;
|
|
80
81
|
|
|
81
82
|
return {
|
|
82
83
|
STDIO_CONFIG: JSON.stringify({
|
|
83
84
|
debug,
|
|
85
|
+
silent,
|
|
84
86
|
}),
|
|
85
87
|
};
|
|
86
88
|
}
|
|
@@ -102,9 +104,11 @@ export function injectTemplateVariables(userConfig: XmcpConfigOutputSchema) {
|
|
|
102
104
|
}
|
|
103
105
|
}
|
|
104
106
|
|
|
107
|
+
const { icons: _, ...templateConfigWithoutIcons } = resolvedConfig;
|
|
108
|
+
|
|
105
109
|
return {
|
|
106
110
|
TEMPLATE_CONFIG: JSON.stringify({
|
|
107
|
-
...
|
|
111
|
+
...templateConfigWithoutIcons,
|
|
108
112
|
homePage,
|
|
109
113
|
}),
|
|
110
114
|
};
|
|
@@ -112,6 +116,63 @@ export function injectTemplateVariables(userConfig: XmcpConfigOutputSchema) {
|
|
|
112
116
|
|
|
113
117
|
export type TemplateVariables = ReturnType<typeof injectTemplateVariables>;
|
|
114
118
|
|
|
119
|
+
const MIME_BY_EXT: Record<string, string> = {
|
|
120
|
+
".svg": "image/svg+xml",
|
|
121
|
+
".png": "image/png",
|
|
122
|
+
".jpg": "image/jpeg",
|
|
123
|
+
".jpeg": "image/jpeg",
|
|
124
|
+
".webp": "image/webp",
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
function resolveIconSrc(icon: {
|
|
128
|
+
src: string;
|
|
129
|
+
mimeType?: string;
|
|
130
|
+
sizes?: string[];
|
|
131
|
+
theme?: string;
|
|
132
|
+
}) {
|
|
133
|
+
if (/^https?:\/\/|^data:/.test(icon.src)) return icon;
|
|
134
|
+
|
|
135
|
+
const filePath = resolve(process.cwd(), icon.src);
|
|
136
|
+
if (!existsSync(filePath)) {
|
|
137
|
+
console.warn(`[xmcp] icon file not found: ${filePath}`);
|
|
138
|
+
return icon;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const ext = icon.src.substring(icon.src.lastIndexOf(".")).toLowerCase();
|
|
142
|
+
const mime = icon.mimeType ?? MIME_BY_EXT[ext] ?? "application/octet-stream";
|
|
143
|
+
const data = readFileSync(filePath);
|
|
144
|
+
return { ...icon, src: `data:${mime};base64,${data.toString("base64")}` };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function injectServerInfoVariables(userConfig: XmcpConfigOutputSchema) {
|
|
148
|
+
const templateConfig = getResolvedTemplateConfig(userConfig);
|
|
149
|
+
|
|
150
|
+
let version = "0.0.1";
|
|
151
|
+
try {
|
|
152
|
+
const pkg = JSON.parse(
|
|
153
|
+
readFileSync(resolve(process.cwd(), "package.json"), "utf-8")
|
|
154
|
+
);
|
|
155
|
+
if (pkg.version) version = pkg.version;
|
|
156
|
+
} catch (err) {
|
|
157
|
+
console.warn(`[xmcp] Could not read version from package.json:`, err);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const icons = (templateConfig.icons ?? []).map(resolveIconSrc);
|
|
161
|
+
|
|
162
|
+
const serverInfo: Record<string, unknown> = {
|
|
163
|
+
name: templateConfig.name,
|
|
164
|
+
version,
|
|
165
|
+
description: templateConfig.description,
|
|
166
|
+
icons,
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
return {
|
|
170
|
+
SERVER_INFO: JSON.stringify(serverInfo),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export type ServerInfoVariables = ReturnType<typeof injectServerInfoVariables>;
|
|
175
|
+
|
|
115
176
|
export function injectAdapterVariables(userConfig: XmcpConfigOutputSchema) {
|
|
116
177
|
const experimentalConfig = getResolvedExperimentalConfig(userConfig);
|
|
117
178
|
|
|
@@ -143,5 +204,6 @@ export type InjectedVariables =
|
|
|
143
204
|
| PathsVariables
|
|
144
205
|
| StdioVariables
|
|
145
206
|
| TemplateVariables
|
|
207
|
+
| ServerInfoVariables
|
|
146
208
|
| AdapterVariables
|
|
147
209
|
| TypescriptVariables;
|
|
@@ -12,6 +12,23 @@ const templateConfigBaseSchema = z.object({
|
|
|
12
12
|
description: z
|
|
13
13
|
.string()
|
|
14
14
|
.default("This MCP server was bootstrapped with xmcp."),
|
|
15
|
+
/**
|
|
16
|
+
* Icons for the MCP server, matching the MCP spec's `serverInfo.icons` format.
|
|
17
|
+
* When not provided, defaults to the xmcp logo.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* icons: [{ src: "https://example.com/icon.png", mimeType: "image/png" }]
|
|
21
|
+
*/
|
|
22
|
+
icons: z
|
|
23
|
+
.array(
|
|
24
|
+
z.object({
|
|
25
|
+
src: z.string(),
|
|
26
|
+
mimeType: z.string().optional(),
|
|
27
|
+
sizes: z.array(z.string()).optional(),
|
|
28
|
+
theme: z.enum(["light", "dark"]).optional(),
|
|
29
|
+
})
|
|
30
|
+
)
|
|
31
|
+
.optional(),
|
|
15
32
|
/**
|
|
16
33
|
* Custom home page content for the `/` endpoint.
|
|
17
34
|
*
|
|
@@ -6,6 +6,7 @@ import { z } from "zod/v3";
|
|
|
6
6
|
// Base schema with defaults - used for parsing with defaults applied
|
|
7
7
|
const stdioTransportObjectBaseSchema = z.object({
|
|
8
8
|
debug: z.boolean().default(false),
|
|
9
|
+
silent: z.boolean().default(false),
|
|
9
10
|
});
|
|
10
11
|
|
|
11
12
|
// Input schema - all fields optional for partial configs
|
|
@@ -47,7 +47,7 @@ export async function getTools() {
|
|
|
47
47
|
|
|
48
48
|
for (const toolData of toolsData) {
|
|
49
49
|
const { path, name: defaultName, module } = toolData;
|
|
50
|
-
const { default: handler, metadata, schema } = module;
|
|
50
|
+
const { default: handler, metadata, schema, outputSchema } = module;
|
|
51
51
|
|
|
52
52
|
const toolConfig = {
|
|
53
53
|
name: defaultName,
|
|
@@ -73,6 +73,22 @@ export async function getTools() {
|
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
+
let toolOutputSchema = undefined;
|
|
77
|
+
if (outputSchema && typeof outputSchema === "object" && outputSchema !== null) {
|
|
78
|
+
const isValidOutputSchema = Object.entries(outputSchema).every(([key, val]) => {
|
|
79
|
+
if (typeof key !== "string") return false;
|
|
80
|
+
if (typeof val !== "object" || val === null) return false;
|
|
81
|
+
if (!("parse" in val) || typeof val.parse !== "function") return false;
|
|
82
|
+
return true;
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
if (isValidOutputSchema) {
|
|
86
|
+
toolOutputSchema = outputSchema;
|
|
87
|
+
} else {
|
|
88
|
+
throw new Error(\`Invalid outputSchema for tool "\${toolConfig.name}" at \${path}. Expected Record<string, z.ZodType>\`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
76
92
|
// Make sure tools has annotations with a title
|
|
77
93
|
if (toolConfig.annotations === undefined) {
|
|
78
94
|
toolConfig.annotations = {};
|
|
@@ -85,6 +101,9 @@ export async function getTools() {
|
|
|
85
101
|
registry[toolConfig.name] = {
|
|
86
102
|
description: toolConfig.description,
|
|
87
103
|
inputSchema: z.object(toolSchema || {}),
|
|
104
|
+
outputSchema: toolOutputSchema
|
|
105
|
+
? z.object(toolOutputSchema).strict()
|
|
106
|
+
: undefined,
|
|
88
107
|
execute: async (args, extra) => {
|
|
89
108
|
const result = await handler(args, extra);
|
|
90
109
|
return result;
|
|
@@ -135,6 +154,7 @@ export interface ToolItem {
|
|
|
135
154
|
export interface ToolRegistryItem {
|
|
136
155
|
description: string;
|
|
137
156
|
inputSchema: z.ZodObject<any>;
|
|
157
|
+
outputSchema?: z.ZodObject<any>;
|
|
138
158
|
execute: (args: any) => Promise<any>;
|
|
139
159
|
}
|
|
140
160
|
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
injectPathsVariables,
|
|
8
8
|
injectStdioVariables,
|
|
9
9
|
injectTemplateVariables,
|
|
10
|
+
injectServerInfoVariables,
|
|
10
11
|
injectAdapterVariables,
|
|
11
12
|
injectTypescriptVariables,
|
|
12
13
|
} from "../config/injection";
|
|
@@ -28,6 +29,7 @@ export function getInjectedVariables(
|
|
|
28
29
|
const pathsVariables = injectPathsVariables(xmcpConfig);
|
|
29
30
|
const stdioVariables = injectStdioVariables(xmcpConfig.stdio);
|
|
30
31
|
const templateVariables = injectTemplateVariables(xmcpConfig);
|
|
32
|
+
const serverInfoVariables = injectServerInfoVariables(xmcpConfig);
|
|
31
33
|
const adapterVariables = injectAdapterVariables(xmcpConfig);
|
|
32
34
|
const typescriptVariables = injectTypescriptVariables(xmcpConfig);
|
|
33
35
|
|
|
@@ -37,6 +39,7 @@ export function getInjectedVariables(
|
|
|
37
39
|
...pathsVariables,
|
|
38
40
|
...stdioVariables,
|
|
39
41
|
...templateVariables,
|
|
42
|
+
...serverInfoVariables,
|
|
40
43
|
...adapterVariables,
|
|
41
44
|
...typescriptVariables,
|
|
42
45
|
};
|
package/src/compiler/index.ts
CHANGED
|
@@ -34,6 +34,10 @@ import { getResolvedPathsConfig } from "./config/utils";
|
|
|
34
34
|
import { pathToToolName } from "./utils/path-utils";
|
|
35
35
|
import { transpileClientComponent } from "./client/transpile";
|
|
36
36
|
import { buildCloudflareOutput } from "../platforms/build-cloudflare-output";
|
|
37
|
+
import {
|
|
38
|
+
addWatchedPath,
|
|
39
|
+
removeWatchedPath,
|
|
40
|
+
} from "./watcher-recovery";
|
|
37
41
|
const { version: XMCP_VERSION } = require("../../package.json");
|
|
38
42
|
dotenv.config();
|
|
39
43
|
|
|
@@ -53,7 +57,26 @@ export async function compile({ onBuild }: CompileOptions = {}) {
|
|
|
53
57
|
compilerContext.setContext({
|
|
54
58
|
xmcpConfig: xmcpConfig,
|
|
55
59
|
});
|
|
60
|
+
const staleImportErrorSuppressions = new Map<string, number>();
|
|
56
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
|
+
};
|
|
57
80
|
|
|
58
81
|
if (xmcpConfig.bundler) {
|
|
59
82
|
bundlerConfig = xmcpConfig.bundler(bundlerConfig);
|
|
@@ -75,14 +98,14 @@ export async function compile({ onBuild }: CompileOptions = {}) {
|
|
|
75
98
|
// handle tools
|
|
76
99
|
if (toolsPath) {
|
|
77
100
|
watcher.watch(`${toolsPath}/**/*.{ts,tsx}`, {
|
|
78
|
-
onAdd: async (
|
|
79
|
-
toolPaths
|
|
101
|
+
onAdd: async (filePath) => {
|
|
102
|
+
addWatchedPath(toolPaths, filePath);
|
|
80
103
|
if (compilerStarted) {
|
|
81
104
|
await generateCode();
|
|
82
105
|
}
|
|
83
106
|
},
|
|
84
|
-
onUnlink: async (
|
|
85
|
-
toolPaths
|
|
107
|
+
onUnlink: async (filePath) => {
|
|
108
|
+
removeWatchedPath(toolPaths, filePath);
|
|
86
109
|
if (compilerStarted) {
|
|
87
110
|
await generateCode();
|
|
88
111
|
}
|
|
@@ -104,14 +127,14 @@ export async function compile({ onBuild }: CompileOptions = {}) {
|
|
|
104
127
|
// handle prompts
|
|
105
128
|
if (promptsPath) {
|
|
106
129
|
watcher.watch(`${promptsPath}/**/*.{ts,tsx}`, {
|
|
107
|
-
onAdd: async (
|
|
108
|
-
promptPaths
|
|
130
|
+
onAdd: async (filePath) => {
|
|
131
|
+
addWatchedPath(promptPaths, filePath);
|
|
109
132
|
if (compilerStarted) {
|
|
110
133
|
await generateCode();
|
|
111
134
|
}
|
|
112
135
|
},
|
|
113
|
-
onUnlink: async (
|
|
114
|
-
promptPaths
|
|
136
|
+
onUnlink: async (filePath) => {
|
|
137
|
+
removeWatchedPath(promptPaths, filePath);
|
|
115
138
|
if (compilerStarted) {
|
|
116
139
|
await generateCode();
|
|
117
140
|
}
|
|
@@ -128,14 +151,14 @@ export async function compile({ onBuild }: CompileOptions = {}) {
|
|
|
128
151
|
// handle resources
|
|
129
152
|
if (resourcesPath) {
|
|
130
153
|
watcher.watch(`${resourcesPath}/**/*.{ts,tsx}`, {
|
|
131
|
-
onAdd: async (
|
|
132
|
-
resourcePaths
|
|
154
|
+
onAdd: async (filePath) => {
|
|
155
|
+
addWatchedPath(resourcePaths, filePath);
|
|
133
156
|
if (compilerStarted) {
|
|
134
157
|
await generateCode();
|
|
135
158
|
}
|
|
136
159
|
},
|
|
137
|
-
onUnlink: async (
|
|
138
|
-
resourcePaths
|
|
160
|
+
onUnlink: async (filePath) => {
|
|
161
|
+
removeWatchedPath(resourcePaths, filePath);
|
|
139
162
|
if (compilerStarted) {
|
|
140
163
|
await generateCode();
|
|
141
164
|
}
|
|
@@ -232,6 +255,44 @@ export async function compile({ onBuild }: CompileOptions = {}) {
|
|
|
232
255
|
console.error(err);
|
|
233
256
|
}
|
|
234
257
|
if (stats?.hasErrors()) {
|
|
258
|
+
const statsJson = stats.toJson({
|
|
259
|
+
all: false,
|
|
260
|
+
errors: true,
|
|
261
|
+
warnings: false,
|
|
262
|
+
});
|
|
263
|
+
const hasOnlyStaleImportMapError =
|
|
264
|
+
mode === "development" &&
|
|
265
|
+
!!statsJson.errors?.length &&
|
|
266
|
+
statsJson.errors.every((error) => {
|
|
267
|
+
const message =
|
|
268
|
+
typeof error.message === "string" ? error.message : "";
|
|
269
|
+
const moduleName =
|
|
270
|
+
typeof error.moduleName === "string" ? error.moduleName : "";
|
|
271
|
+
return (
|
|
272
|
+
(message.includes("Module not found") ||
|
|
273
|
+
message.includes("Can't resolve")) &&
|
|
274
|
+
(message.includes(".xmcp/import-map.js") ||
|
|
275
|
+
moduleName.includes(".xmcp/import-map.js"))
|
|
276
|
+
);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
if (hasOnlyStaleImportMapError) {
|
|
280
|
+
const staleErrors = statsJson.errors ?? [];
|
|
281
|
+
const staleSignature = staleErrors
|
|
282
|
+
.map((error) =>
|
|
283
|
+
typeof error.message === "string" ? error.message : "unknown"
|
|
284
|
+
)
|
|
285
|
+
.sort()
|
|
286
|
+
.join("|");
|
|
287
|
+
const staleCount =
|
|
288
|
+
(staleImportErrorSuppressions.get(staleSignature) ?? 0) + 1;
|
|
289
|
+
staleImportErrorSuppressions.set(staleSignature, staleCount);
|
|
290
|
+
|
|
291
|
+
if (staleCount === 1) {
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
235
296
|
console.error(
|
|
236
297
|
stats.toString({
|
|
237
298
|
colors: true,
|
|
@@ -268,8 +329,9 @@ export async function compile({ onBuild }: CompileOptions = {}) {
|
|
|
268
329
|
}
|
|
269
330
|
|
|
270
331
|
// Build succeeded
|
|
332
|
+
staleImportErrorSuppressions.clear();
|
|
271
333
|
if (firstBuild) {
|
|
272
|
-
onFirstBuild(
|
|
334
|
+
onFirstBuild(xmcpConfig);
|
|
273
335
|
} else {
|
|
274
336
|
// on dev mode, bundler will recompile the code, so we need to start the http server after the first one
|
|
275
337
|
if (
|
|
@@ -277,7 +339,17 @@ export async function compile({ onBuild }: CompileOptions = {}) {
|
|
|
277
339
|
xmcpConfig["http"] &&
|
|
278
340
|
!xmcpConfig.experimental?.adapter
|
|
279
341
|
) {
|
|
280
|
-
|
|
342
|
+
const currentBuildHash =
|
|
343
|
+
typeof stats?.hash === "string" ? stats.hash : null;
|
|
344
|
+
const shouldRestart =
|
|
345
|
+
currentBuildHash === null
|
|
346
|
+
? lastRestartedBuildHash === null
|
|
347
|
+
: currentBuildHash !== lastRestartedBuildHash;
|
|
348
|
+
|
|
349
|
+
if (shouldRestart) {
|
|
350
|
+
lastRestartedBuildHash = currentBuildHash;
|
|
351
|
+
await restartHttpServerSafely();
|
|
352
|
+
}
|
|
281
353
|
}
|
|
282
354
|
}
|
|
283
355
|
|
|
@@ -367,25 +439,29 @@ async function generateCode() {
|
|
|
367
439
|
|
|
368
440
|
// Generate import map code (includes client bundles)
|
|
369
441
|
const fileContent = generateImportCode();
|
|
370
|
-
|
|
442
|
+
writeFileIfChanged(path.join(runtimeFolderPath, "import-map.js"), fileContent);
|
|
371
443
|
|
|
372
444
|
// Generate runtime exports for global access
|
|
373
445
|
const runtimeExportsCode = generateEnvCode();
|
|
374
446
|
const envFilePath = path.join(rootFolder, "xmcp-env.d.ts");
|
|
375
|
-
|
|
376
|
-
// Delete existing file if it exists
|
|
377
|
-
if (fs.existsSync(envFilePath)) {
|
|
378
|
-
fs.unlinkSync(envFilePath);
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
fs.writeFileSync(envFilePath, runtimeExportsCode);
|
|
447
|
+
writeFileIfChanged(envFilePath, runtimeExportsCode);
|
|
382
448
|
|
|
383
449
|
// only generating tools files for nextjs adapter mode
|
|
384
450
|
const { xmcpConfig } = compilerContext.getContext();
|
|
385
451
|
if (xmcpConfig?.experimental?.adapter === "nextjs") {
|
|
386
452
|
const toolsCode = generateToolsExportCode();
|
|
387
|
-
|
|
453
|
+
writeFileIfChanged(path.join(runtimeFolderPath, "tools.js"), toolsCode);
|
|
388
454
|
const typesCode = generateToolsTypesCode();
|
|
389
|
-
|
|
455
|
+
writeFileIfChanged(path.join(runtimeFolderPath, "tools.d.ts"), typesCode);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
function writeFileIfChanged(filePath: string, content: string) {
|
|
460
|
+
if (fs.existsSync(filePath)) {
|
|
461
|
+
const currentContent = fs.readFileSync(filePath, "utf8");
|
|
462
|
+
if (currentContent === content) {
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
390
465
|
}
|
|
466
|
+
fs.writeFileSync(filePath, content);
|
|
391
467
|
}
|
|
@@ -1,56 +1,9 @@
|
|
|
1
|
-
import { spawn } from "child_process";
|
|
2
|
-
import { CompilerMode } from ".";
|
|
3
|
-
import { watchdog } from "../utils/spawn-process";
|
|
4
1
|
import { greenCheck } from "../utils/cli-icons";
|
|
5
2
|
import { XmcpConfigOutputSchema } from "./config";
|
|
3
|
+
import { compilerContext } from "./compiler-context";
|
|
4
|
+
import chalk from "chalk";
|
|
6
5
|
|
|
7
|
-
export function onFirstBuild(
|
|
8
|
-
mode: CompilerMode,
|
|
9
|
-
xmcpConfig: XmcpConfigOutputSchema
|
|
10
|
-
) {
|
|
11
|
-
if (mode === "development" && false) {
|
|
12
|
-
// disable inspector for now
|
|
13
|
-
console.log("🔍 Starting inspector...");
|
|
14
|
-
|
|
15
|
-
const inspectorArgs = ["@modelcontextprotocol/inspector@latest"];
|
|
16
|
-
|
|
17
|
-
if (xmcpConfig.stdio) {
|
|
18
|
-
inspectorArgs.push("node", "dist/stdio.js");
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const inspectorProcess = spawn("npx", inspectorArgs, {
|
|
22
|
-
stdio: ["inherit", "pipe", "pipe"],
|
|
23
|
-
shell: true,
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
watchdog(inspectorProcess);
|
|
27
|
-
|
|
28
|
-
// Prefix inspector output with [Inspector]
|
|
29
|
-
inspectorProcess.stdout?.on("data", (data: Buffer) => {
|
|
30
|
-
const lines = data.toString().split("\n");
|
|
31
|
-
lines.forEach((line) => {
|
|
32
|
-
if (line.trim()) {
|
|
33
|
-
if (line.includes("?MCP_PROXY_AUTH_TOKEN")) {
|
|
34
|
-
console.log(`🔍 Inspector started at ${line}`);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
inspectorProcess.stderr?.on("data", (data: Buffer) => {
|
|
41
|
-
const lines = data.toString().split("\n");
|
|
42
|
-
lines.forEach((line) => {
|
|
43
|
-
if (line.trim()) {
|
|
44
|
-
console.error(`[Inspector] ${line}`);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
inspectorProcess.on("error", (err: Error) => {
|
|
50
|
-
console.error("[Inspector] Failed to start inspector:", err);
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
|
|
6
|
+
export function onFirstBuild(xmcpConfig: XmcpConfigOutputSchema) {
|
|
54
7
|
const builtResults = [];
|
|
55
8
|
|
|
56
9
|
if (xmcpConfig.stdio) {
|
|
@@ -67,4 +20,35 @@ export function onFirstBuild(
|
|
|
67
20
|
builtResults.forEach((result) => {
|
|
68
21
|
console.log(result);
|
|
69
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
|
+
}
|
|
70
54
|
}
|
|
@@ -14,10 +14,17 @@ function spawnHttpServer() {
|
|
|
14
14
|
return process;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
async function killProcess(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
async function killProcess(proc: ChildProcess) {
|
|
18
|
+
await new Promise<void>((resolve) => {
|
|
19
|
+
if (proc.exitCode !== null || proc.signalCode !== null) {
|
|
20
|
+
return resolve();
|
|
21
|
+
}
|
|
22
|
+
proc.on("exit", () => resolve());
|
|
23
|
+
try {
|
|
24
|
+
proc.kill("SIGKILL");
|
|
25
|
+
} catch {
|
|
26
|
+
resolve();
|
|
27
|
+
}
|
|
21
28
|
});
|
|
22
29
|
}
|
|
23
30
|
|