xmcp 0.6.5 → 0.6.7
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/README.md +32 -0
- package/dist/cli.js +58 -40
- package/dist/compiler/config/index.d.ts +106 -47
- package/dist/compiler/config/schemas/template.d.ts +40 -0
- package/dist/compiler/config/schemas/transport/http.d.ts +82 -82
- package/dist/compiler/config/schemas/transport/stdio.d.ts +5 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +5 -5
- package/dist/runtime/adapter-express.js +40 -23
- package/dist/runtime/adapter-nestjs.js +40 -23
- package/dist/runtime/adapter-nextjs.js +39 -22
- package/dist/runtime/contexts/http-request-context.d.ts +3 -1
- package/dist/runtime/headers.js +1 -1
- package/dist/runtime/http.js +48 -31
- package/dist/runtime/stdio.js +19 -13
- package/dist/runtime/utils/elicitation.d.ts +4 -0
- package/dist/runtime/utils/ext-apps-registry.d.ts +132 -0
- package/dist/runtime/utils/path-to-tool-name.d.ts +20 -0
- package/dist/runtime/utils/prompt-loader.d.ts +11 -0
- package/dist/runtime/utils/prompts.d.ts +8 -0
- package/dist/runtime/utils/react/generate-html.d.ts +1 -0
- package/dist/runtime/utils/react/index.d.ts +4 -0
- package/dist/runtime/utils/resource-loader.d.ts +11 -0
- package/dist/runtime/utils/resources.d.ts +4 -0
- package/dist/runtime/utils/server.d.ts +35 -0
- package/dist/runtime/utils/tool-loader.d.ts +11 -0
- package/dist/runtime/utils/tools.d.ts +11 -0
- package/dist/runtime/utils/transformers/prompt.d.ts +32 -0
- package/dist/runtime/utils/transformers/resource.d.ts +26 -0
- package/dist/runtime/utils/transformers/tool.d.ts +30 -0
- package/dist/runtime/utils/ui/flatten-meta.d.ts +6 -0
- package/dist/runtime/utils/ui/split-meta.d.ts +17 -0
- package/dist/runtime/utils/utils/resource-uri-composer.d.ts +60 -0
- package/dist/runtime/utils/validators/index.d.ts +13 -0
- package/dist/types/client-info.d.ts +12 -0
- package/dist/types/tool.d.ts +70 -14
- package/dist/utils/context.d.ts +1 -1
- package/package.json +9 -1
- package/src/client/index.ts +4 -1
- 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 +24 -6
- package/src/compiler/get-bundler-config/get-injected-variables.ts +3 -0
- package/src/compiler/get-bundler-config/plugins/index.ts +0 -1
- package/src/compiler/index.ts +123 -30
- package/src/compiler/start-http-server.ts +11 -4
- package/src/compiler/watcher-recovery.ts +72 -0
- package/src/index.ts +9 -1
- package/src/runtime/adapters/express/index.ts +45 -47
- package/src/runtime/adapters/nestjs/xmcp.service.ts +4 -3
- package/src/runtime/adapters/nextjs/handler/node-to-web-adapter.ts +40 -6
- package/src/runtime/adapters/nextjs/handler/request-converter.ts +20 -0
- package/src/runtime/adapters/nextjs/handler/server-lifecycle.ts +8 -5
- package/src/runtime/adapters/nextjs/index.ts +41 -25
- package/src/runtime/contexts/http-request-context.ts +2 -0
- package/src/runtime/headers.ts +37 -2
- package/src/runtime/platforms/cloudflare/cors.ts +0 -1
- package/src/runtime/platforms/cloudflare/worker.ts +46 -44
- package/src/runtime/transports/http/index.ts +0 -4
- package/src/runtime/transports/http/stateless-streamable-http.ts +131 -233
- package/src/runtime/transports/stdio/index.ts +18 -1
- package/src/runtime/utils/__tests__/client-info.test.ts +80 -0
- package/src/runtime/utils/__tests__/tool-extra.test.ts +110 -0
- package/src/runtime/utils/client-info.ts +71 -0
- package/src/runtime/utils/elicitation.ts +396 -0
- package/src/runtime/utils/prompt-loader.ts +151 -0
- package/src/runtime/utils/resource-loader.ts +151 -0
- package/src/runtime/utils/resources.ts +0 -8
- package/src/runtime/utils/server.ts +30 -42
- package/src/runtime/utils/tool-loader.ts +149 -0
- package/src/runtime/utils/tools.ts +31 -9
- package/src/runtime/utils/transformers/tool.ts +149 -10
- package/src/types/client-info.ts +12 -0
- package/src/types/injected.d.ts +62 -0
- package/src/types/tool.ts +100 -17
- package/src/utils/context.ts +2 -2
|
@@ -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
|
|
@@ -33,7 +33,7 @@ export function generateToolsExportCode(): string {
|
|
|
33
33
|
|
|
34
34
|
${importStatements}
|
|
35
35
|
|
|
36
|
-
/**
|
|
36
|
+
/**
|
|
37
37
|
* Runtime-accessible tools function that works from any context.
|
|
38
38
|
* Generated at build time - always up to date with discovered tools.
|
|
39
39
|
* @returns {Promise<ToolRegistry>}
|
|
@@ -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,
|
|
@@ -65,7 +65,7 @@ export async function getTools() {
|
|
|
65
65
|
if (!("parse" in val) || typeof val.parse !== "function") return false;
|
|
66
66
|
return true;
|
|
67
67
|
});
|
|
68
|
-
|
|
68
|
+
|
|
69
69
|
if (isValidSchema) {
|
|
70
70
|
toolSchema = schema;
|
|
71
71
|
} else {
|
|
@@ -73,10 +73,24 @@ export async function getTools() {
|
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
|
|
77
|
-
if (
|
|
78
|
-
|
|
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
|
+
}
|
|
79
90
|
}
|
|
91
|
+
|
|
92
|
+
// Make sure tools has annotations with a title — shallow copy to avoid mutating shared objects
|
|
93
|
+
toolConfig.annotations = { ...(toolConfig.annotations ?? {}) };
|
|
80
94
|
if (toolConfig.annotations.title === undefined) {
|
|
81
95
|
toolConfig.annotations.title = toolConfig.name;
|
|
82
96
|
}
|
|
@@ -85,6 +99,9 @@ export async function getTools() {
|
|
|
85
99
|
registry[toolConfig.name] = {
|
|
86
100
|
description: toolConfig.description,
|
|
87
101
|
inputSchema: z.object(toolSchema || {}),
|
|
102
|
+
outputSchema: toolOutputSchema
|
|
103
|
+
? z.object(toolOutputSchema).strict()
|
|
104
|
+
: undefined,
|
|
88
105
|
execute: async (args, extra) => {
|
|
89
106
|
const result = await handler(args, extra);
|
|
90
107
|
return result;
|
|
@@ -135,6 +152,7 @@ export interface ToolItem {
|
|
|
135
152
|
export interface ToolRegistryItem {
|
|
136
153
|
description: string;
|
|
137
154
|
inputSchema: z.ZodObject<any>;
|
|
155
|
+
outputSchema?: z.ZodObject<any>;
|
|
138
156
|
execute: (args: any) => Promise<any>;
|
|
139
157
|
}
|
|
140
158
|
|
|
@@ -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,16 +127,21 @@ 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
|
-
await generateCode();
|
|
133
|
+
await generateCode({ rebuildClientBundles: false });
|
|
111
134
|
}
|
|
112
135
|
},
|
|
113
|
-
|
|
114
|
-
promptPaths.delete(path);
|
|
136
|
+
onChange: async () => {
|
|
115
137
|
if (compilerStarted) {
|
|
116
|
-
await generateCode();
|
|
138
|
+
await generateCode({ rebuildClientBundles: false });
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
onUnlink: async (filePath) => {
|
|
142
|
+
removeWatchedPath(promptPaths, filePath);
|
|
143
|
+
if (compilerStarted) {
|
|
144
|
+
await generateCode({ rebuildClientBundles: false });
|
|
117
145
|
}
|
|
118
146
|
},
|
|
119
147
|
});
|
|
@@ -128,16 +156,21 @@ export async function compile({ onBuild }: CompileOptions = {}) {
|
|
|
128
156
|
// handle resources
|
|
129
157
|
if (resourcesPath) {
|
|
130
158
|
watcher.watch(`${resourcesPath}/**/*.{ts,tsx}`, {
|
|
131
|
-
onAdd: async (
|
|
132
|
-
resourcePaths
|
|
159
|
+
onAdd: async (filePath) => {
|
|
160
|
+
addWatchedPath(resourcePaths, filePath);
|
|
133
161
|
if (compilerStarted) {
|
|
134
|
-
await generateCode();
|
|
162
|
+
await generateCode({ rebuildClientBundles: false });
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
onChange: async () => {
|
|
166
|
+
if (compilerStarted) {
|
|
167
|
+
await generateCode({ rebuildClientBundles: false });
|
|
135
168
|
}
|
|
136
169
|
},
|
|
137
|
-
onUnlink: async (
|
|
138
|
-
resourcePaths
|
|
170
|
+
onUnlink: async (filePath) => {
|
|
171
|
+
removeWatchedPath(resourcePaths, filePath);
|
|
139
172
|
if (compilerStarted) {
|
|
140
|
-
await generateCode();
|
|
173
|
+
await generateCode({ rebuildClientBundles: false });
|
|
141
174
|
}
|
|
142
175
|
},
|
|
143
176
|
});
|
|
@@ -232,6 +265,44 @@ export async function compile({ onBuild }: CompileOptions = {}) {
|
|
|
232
265
|
console.error(err);
|
|
233
266
|
}
|
|
234
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
|
+
|
|
235
306
|
console.error(
|
|
236
307
|
stats.toString({
|
|
237
308
|
colors: true,
|
|
@@ -268,6 +339,7 @@ export async function compile({ onBuild }: CompileOptions = {}) {
|
|
|
268
339
|
}
|
|
269
340
|
|
|
270
341
|
// Build succeeded
|
|
342
|
+
staleImportErrorSuppressions.clear();
|
|
271
343
|
if (firstBuild) {
|
|
272
344
|
onFirstBuild(xmcpConfig);
|
|
273
345
|
} else {
|
|
@@ -277,7 +349,17 @@ export async function compile({ onBuild }: CompileOptions = {}) {
|
|
|
277
349
|
xmcpConfig["http"] &&
|
|
278
350
|
!xmcpConfig.experimental?.adapter
|
|
279
351
|
) {
|
|
280
|
-
|
|
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
|
+
}
|
|
281
363
|
}
|
|
282
364
|
}
|
|
283
365
|
|
|
@@ -358,34 +440,45 @@ async function buildClientBundles(): Promise<Map<string, string> | undefined> {
|
|
|
358
440
|
* Generates all runtime code and builds client bundles if needed
|
|
359
441
|
* This centralizes all code generation logic including client bundle building
|
|
360
442
|
*/
|
|
361
|
-
async function generateCode(
|
|
362
|
-
|
|
363
|
-
|
|
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;
|
|
364
453
|
|
|
365
454
|
// Store in context for import map generation
|
|
366
455
|
compilerContext.setContext({ clientBundles });
|
|
367
456
|
|
|
368
457
|
// Generate import map code (includes client bundles)
|
|
369
458
|
const fileContent = generateImportCode();
|
|
370
|
-
|
|
459
|
+
writeFileIfChanged(path.join(runtimeFolderPath, "import-map.js"), fileContent);
|
|
371
460
|
|
|
372
461
|
// Generate runtime exports for global access
|
|
373
462
|
const runtimeExportsCode = generateEnvCode();
|
|
374
463
|
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);
|
|
464
|
+
writeFileIfChanged(envFilePath, runtimeExportsCode);
|
|
382
465
|
|
|
383
466
|
// only generating tools files for nextjs adapter mode
|
|
384
467
|
const { xmcpConfig } = compilerContext.getContext();
|
|
385
468
|
if (xmcpConfig?.experimental?.adapter === "nextjs") {
|
|
386
469
|
const toolsCode = generateToolsExportCode();
|
|
387
|
-
|
|
470
|
+
writeFileIfChanged(path.join(runtimeFolderPath, "tools.js"), toolsCode);
|
|
388
471
|
const typesCode = generateToolsTypesCode();
|
|
389
|
-
|
|
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
|
+
}
|
|
390
482
|
}
|
|
483
|
+
fs.writeFileSync(filePath, content);
|
|
391
484
|
}
|
|
@@ -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
|
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
|
|
4
|
+
function normalizeSlashes(value: string): string {
|
|
5
|
+
return value.replace(/\\/g, "/");
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function stripLeadingDotSlash(value: string): string {
|
|
9
|
+
return value.replace(/^\.\//, "");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function toProjectRelativePath(
|
|
13
|
+
filePath: string,
|
|
14
|
+
cwd: string = process.cwd()
|
|
15
|
+
): string {
|
|
16
|
+
const relativePath = path.isAbsolute(filePath)
|
|
17
|
+
? path.relative(cwd, filePath)
|
|
18
|
+
: filePath;
|
|
19
|
+
|
|
20
|
+
return stripLeadingDotSlash(normalizeSlashes(relativePath));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function addWatchedPath(
|
|
24
|
+
pathSet: Set<string>,
|
|
25
|
+
filePath: string,
|
|
26
|
+
cwd: string = process.cwd()
|
|
27
|
+
) {
|
|
28
|
+
pathSet.add(toProjectRelativePath(filePath, cwd));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function removeWatchedPath(
|
|
32
|
+
pathSet: Set<string>,
|
|
33
|
+
filePath: string,
|
|
34
|
+
cwd: string = process.cwd()
|
|
35
|
+
) {
|
|
36
|
+
const normalizedRawPath = stripLeadingDotSlash(normalizeSlashes(filePath));
|
|
37
|
+
const normalizedRelativePath = toProjectRelativePath(filePath, cwd);
|
|
38
|
+
|
|
39
|
+
const normalizedCandidates = new Set<string>([
|
|
40
|
+
normalizedRawPath,
|
|
41
|
+
normalizedRelativePath,
|
|
42
|
+
]);
|
|
43
|
+
|
|
44
|
+
for (const currentPath of Array.from(pathSet)) {
|
|
45
|
+
const normalizedCurrentPath = stripLeadingDotSlash(
|
|
46
|
+
normalizeSlashes(currentPath)
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
if (normalizedCandidates.has(normalizedCurrentPath)) {
|
|
50
|
+
pathSet.delete(currentPath);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function pruneMissingWatchedPaths(
|
|
56
|
+
pathSet: Set<string>,
|
|
57
|
+
cwd: string = process.cwd()
|
|
58
|
+
): boolean {
|
|
59
|
+
let removed = false;
|
|
60
|
+
|
|
61
|
+
for (const currentPath of Array.from(pathSet)) {
|
|
62
|
+
const normalized = stripLeadingDotSlash(normalizeSlashes(currentPath));
|
|
63
|
+
const absolutePath = path.resolve(cwd, normalized);
|
|
64
|
+
|
|
65
|
+
if (!fs.existsSync(absolutePath)) {
|
|
66
|
+
pathSet.delete(currentPath);
|
|
67
|
+
removed = true;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return removed;
|
|
72
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import dotenv from "dotenv";
|
|
2
|
-
export type {
|
|
2
|
+
export type {
|
|
3
|
+
Middleware,
|
|
4
|
+
WebMiddleware,
|
|
5
|
+
WebMiddlewareContext,
|
|
6
|
+
} from "./types/middleware";
|
|
3
7
|
dotenv.config();
|
|
4
8
|
|
|
5
9
|
export type {
|
|
6
10
|
ToolMetadata,
|
|
7
11
|
ToolSchema,
|
|
12
|
+
ToolOutputSchema,
|
|
8
13
|
ToolExtraArguments,
|
|
9
14
|
InferSchema,
|
|
15
|
+
ElicitResult,
|
|
10
16
|
} from "./types/tool";
|
|
17
|
+
export type { McpClientInfo } from "./types/client-info";
|
|
11
18
|
export type { PromptMetadata } from "./types/prompt";
|
|
12
19
|
export type { ResourceMetadata } from "./types/resource";
|
|
13
20
|
export type { UIMetadata } from "./types/ui-meta";
|
|
@@ -20,6 +27,7 @@ export { jwtAuthMiddleware } from "./auth/jwt";
|
|
|
20
27
|
export { createContext } from "./utils/context";
|
|
21
28
|
|
|
22
29
|
export { completable } from "@modelcontextprotocol/sdk/server/completable";
|
|
30
|
+
export { UrlElicitationRequiredError } from "@modelcontextprotocol/sdk/types";
|
|
23
31
|
|
|
24
32
|
export {
|
|
25
33
|
createHTTPClient,
|