xmcp 0.6.5 → 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 -25
- 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 +1 -1
- package/dist/index.js +4 -4
- package/dist/runtime/adapter-express.js +12 -12
- package/dist/runtime/adapter-nestjs.js +14 -14
- package/dist/runtime/adapter-nextjs.js +13 -13
- package/dist/runtime/http.js +20 -20
- package/dist/runtime/stdio.js +13 -13
- package/dist/types/tool.d.ts +1 -0
- package/package.json +1 -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 +21 -1
- package/src/compiler/get-bundler-config/get-injected-variables.ts +3 -0
- package/src/compiler/index.ts +99 -23
- package/src/compiler/start-http-server.ts +11 -4
- package/src/compiler/watcher-recovery.ts +72 -0
- package/src/index.ts +1 -0
- package/src/runtime/transports/stdio/index.ts +19 -0
- package/src/runtime/utils/server.ts +3 -5
- package/src/runtime/utils/tools.ts +22 -3
- package/src/runtime/utils/transformers/tool.ts +127 -9
- package/src/types/tool.ts +1 -0
package/dist/types/tool.d.ts
CHANGED
|
@@ -29,6 +29,7 @@ export interface ToolMetadata {
|
|
|
29
29
|
}
|
|
30
30
|
type CompatibleZodType = z.ZodTypeAny | ZodTypeV4<unknown>;
|
|
31
31
|
export type ToolSchema = Record<string, CompatibleZodType>;
|
|
32
|
+
export type ToolOutputSchema = Record<string, CompatibleZodType>;
|
|
32
33
|
/**
|
|
33
34
|
* Extra arguments passed to MCP tool functions.
|
|
34
35
|
*/
|
package/package.json
CHANGED
|
@@ -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,6 +329,7 @@ export async function compile({ onBuild }: CompileOptions = {}) {
|
|
|
268
329
|
}
|
|
269
330
|
|
|
270
331
|
// Build succeeded
|
|
332
|
+
staleImportErrorSuppressions.clear();
|
|
271
333
|
if (firstBuild) {
|
|
272
334
|
onFirstBuild(xmcpConfig);
|
|
273
335
|
} else {
|
|
@@ -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
|
}
|
|
@@ -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
|
@@ -52,6 +52,25 @@ class StdioTransport {
|
|
|
52
52
|
|
|
53
53
|
// @ts-expect-error: injected by compiler
|
|
54
54
|
const debug = STDIO_CONFIG.debug || false;
|
|
55
|
+
// @ts-expect-error: injected by compiler
|
|
56
|
+
const silent = STDIO_CONFIG.silent || false;
|
|
57
|
+
|
|
58
|
+
if (silent) {
|
|
59
|
+
// Redirect all console methods to stderr so they don't interfere with
|
|
60
|
+
// the MCP stdio protocol on stdout.
|
|
61
|
+
const stderrConsole = new console.Console(process.stderr, process.stderr);
|
|
62
|
+
const methods = [
|
|
63
|
+
"log", "debug", "info", "warn", "error",
|
|
64
|
+
"dir", "table", "trace", "assert",
|
|
65
|
+
"time", "timeEnd", "timeLog",
|
|
66
|
+
"count", "countReset",
|
|
67
|
+
"group", "groupEnd", "groupCollapsed",
|
|
68
|
+
"clear",
|
|
69
|
+
] as const;
|
|
70
|
+
for (const method of methods) {
|
|
71
|
+
(console as any)[method] = (stderrConsole as any)[method].bind(stderrConsole);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
55
74
|
|
|
56
75
|
createServer().then((mcpServer) => {
|
|
57
76
|
const stdioTransport = new StdioTransport(mcpServer, debug);
|
|
@@ -15,6 +15,7 @@ import { uIResourceRegistry } from "./ext-apps-registry";
|
|
|
15
15
|
export type ToolFile = {
|
|
16
16
|
metadata: ToolMetadata;
|
|
17
17
|
schema: ZodRawShape;
|
|
18
|
+
outputSchema?: ZodRawShape;
|
|
18
19
|
default: UserToolHandler;
|
|
19
20
|
};
|
|
20
21
|
|
|
@@ -48,11 +49,8 @@ export const injectedResources = INJECTED_RESOURCES as Record<
|
|
|
48
49
|
() => Promise<ResourceFile>
|
|
49
50
|
>;
|
|
50
51
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
name: "MCP Server",
|
|
54
|
-
version: "0.0.1",
|
|
55
|
-
} as const satisfies Implementation;
|
|
52
|
+
// @ts-expect-error: injected by compiler
|
|
53
|
+
export const INJECTED_CONFIG = SERVER_INFO as Implementation;
|
|
56
54
|
|
|
57
55
|
/* Loads all modules and injects them into the server */
|
|
58
56
|
// would be better as a class and use dependency injection perhaps
|
|
@@ -8,7 +8,7 @@ import { isReactFile } from "./react";
|
|
|
8
8
|
import { uIResourceRegistry } from "./ext-apps-registry";
|
|
9
9
|
import { flattenMeta, hasUIMeta } from "./ui/flatten-meta";
|
|
10
10
|
import { splitUIMetaNested } from "./ui/split-meta";
|
|
11
|
-
import { isPaidHandler, getX402Registry } from "
|
|
11
|
+
import { isPaidHandler, getX402Registry } from "@/plugins/x402";
|
|
12
12
|
|
|
13
13
|
/** Validates if a value is a valid Zod schema object */
|
|
14
14
|
export function isZodRawShape(value: unknown): value is ZodRawShape {
|
|
@@ -43,7 +43,7 @@ export function addToolsToServer(
|
|
|
43
43
|
description: "No description provided",
|
|
44
44
|
};
|
|
45
45
|
|
|
46
|
-
const { default: handler, metadata, schema } = toolModule;
|
|
46
|
+
const { default: handler, metadata, schema, outputSchema } = toolModule;
|
|
47
47
|
|
|
48
48
|
if (typeof metadata === "object" && metadata !== null) {
|
|
49
49
|
Object.assign(toolConfig, metadata);
|
|
@@ -67,6 +67,17 @@ export function addToolsToServer(
|
|
|
67
67
|
);
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
let toolOutputSchema: ZodRawShape | undefined;
|
|
71
|
+
if (outputSchema !== undefined && outputSchema !== null) {
|
|
72
|
+
if (isZodRawShape(outputSchema)) {
|
|
73
|
+
toolOutputSchema = outputSchema;
|
|
74
|
+
} else {
|
|
75
|
+
throw new Error(
|
|
76
|
+
`Invalid outputSchema for tool "${toolConfig.name}" at ${path}. Expected Record<string, z.ZodType>`
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
70
81
|
// Make sure tools has annotations with a title
|
|
71
82
|
if (toolConfig.annotations === undefined) {
|
|
72
83
|
toolConfig.annotations = {};
|
|
@@ -136,7 +147,12 @@ export function addToolsToServer(
|
|
|
136
147
|
},
|
|
137
148
|
});
|
|
138
149
|
} else {
|
|
139
|
-
transformedHandler = transformToolHandler(
|
|
150
|
+
transformedHandler = transformToolHandler(
|
|
151
|
+
handler,
|
|
152
|
+
meta,
|
|
153
|
+
toolOutputSchema,
|
|
154
|
+
toolConfig.name
|
|
155
|
+
);
|
|
140
156
|
}
|
|
141
157
|
|
|
142
158
|
const toolConfigFormatted = {
|
|
@@ -145,6 +161,9 @@ export function addToolsToServer(
|
|
|
145
161
|
// Build the object schema using the project's Zod instance to avoid
|
|
146
162
|
// cross-instance v3 shape issues in tools/list JSON schema generation.
|
|
147
163
|
inputSchema: z.object(toolSchema),
|
|
164
|
+
outputSchema: toolOutputSchema
|
|
165
|
+
? z.object(toolOutputSchema).strict()
|
|
166
|
+
: undefined,
|
|
148
167
|
annotations: toolConfig.annotations,
|
|
149
168
|
_meta: flattenedToolMeta, // Use flattened metadata for MCP protocol
|
|
150
169
|
};
|