init-xmcp 1.5.0 → 1.6.0
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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +7 -0
- package/README.md +45 -0
- package/dist/__tests__/fetch.test.d.ts +1 -0
- package/dist/__tests__/fetch.test.js +217 -0
- package/dist/helpers/create-fetch-route.d.ts +12 -0
- package/dist/helpers/create-fetch-route.js +129 -0
- package/dist/helpers/detect-framework.d.ts +1 -1
- package/dist/helpers/detect-framework.js +12 -0
- package/dist/helpers/generate-config.js +9 -1
- package/dist/helpers/init.js +14 -2
- package/dist/helpers/update-package.js +19 -6
- package/dist/index.js +44 -8
- package/package.json +1 -1
- package/src/__tests__/fetch.test.ts +294 -0
- package/src/helpers/create-fetch-route.ts +179 -0
- package/src/helpers/detect-framework.ts +21 -1
- package/src/helpers/generate-config.ts +11 -1
- package/src/helpers/init.ts +20 -2
- package/src/helpers/update-package.ts +20 -6
- package/src/index.ts +59 -8
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { Command } from "commander";
|
|
|
3
3
|
import chalk from "chalk";
|
|
4
4
|
import inquirer from "inquirer";
|
|
5
5
|
import { init } from "./helpers/init.js";
|
|
6
|
-
import {
|
|
6
|
+
import { detectFetchCloudflare, isFetchFramework, } from "./helpers/create-fetch-route.js";
|
|
7
7
|
import { detectFramework, detectTypeScript, } from "./helpers/detect-framework.js";
|
|
8
8
|
import { detectPackageManager } from "./helpers/install.js";
|
|
9
9
|
import fs from "fs-extra";
|
|
@@ -28,7 +28,7 @@ const program = new Command()
|
|
|
28
28
|
.option("--skip-tools", "Skip tool creation", false)
|
|
29
29
|
.option("--skip-prompts", "Skip prompt creation", false)
|
|
30
30
|
.option("--skip-resources", "Skip resource creation", false)
|
|
31
|
-
.option("--cf", "Build
|
|
31
|
+
.option("--cf", "Build a Fetch adapter for Cloudflare Workers", false)
|
|
32
32
|
.option("--skip-route", "Skip route creation", false)
|
|
33
33
|
.action(async (options) => {
|
|
34
34
|
console.log(chalk.bold(`\ninit-xmcp@${packageJson.version}`));
|
|
@@ -102,8 +102,26 @@ const program = new Command()
|
|
|
102
102
|
if (detectedFramework === "tanstack" && !options.skipRoute) {
|
|
103
103
|
routePath = options.routePath || "src/routes";
|
|
104
104
|
}
|
|
105
|
-
|
|
106
|
-
|
|
105
|
+
if (detectedFramework === "hono" && !options.skipRoute) {
|
|
106
|
+
routePath = options.routePath || "src/routes";
|
|
107
|
+
}
|
|
108
|
+
if (detectedFramework === "sveltekit" && !options.skipRoute) {
|
|
109
|
+
routePath = options.routePath || "src/routes/mcp";
|
|
110
|
+
}
|
|
111
|
+
if (!options.skipRoute) {
|
|
112
|
+
const defaults = {
|
|
113
|
+
nuxt: "server/routes",
|
|
114
|
+
"react-router": "app/routes",
|
|
115
|
+
astro: "src/pages",
|
|
116
|
+
};
|
|
117
|
+
if (detectedFramework in defaults) {
|
|
118
|
+
routePath =
|
|
119
|
+
options.routePath ||
|
|
120
|
+
defaults[detectedFramework];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const cloudflare = isFetchFramework(detectedFramework) &&
|
|
124
|
+
(options.cf || detectFetchCloudflare(projectRoot, detectedFramework));
|
|
107
125
|
// determine package manager
|
|
108
126
|
let packageManager;
|
|
109
127
|
if (detectedPackageManager) {
|
|
@@ -151,7 +169,8 @@ const program = new Command()
|
|
|
151
169
|
choices: componentChoices,
|
|
152
170
|
});
|
|
153
171
|
}
|
|
154
|
-
if ((detectedFramework === "nextjs" ||
|
|
172
|
+
if ((detectedFramework === "nextjs" ||
|
|
173
|
+
isFetchFramework(detectedFramework)) &&
|
|
155
174
|
!options.skipRoute) {
|
|
156
175
|
prompts.push({
|
|
157
176
|
type: "input",
|
|
@@ -200,7 +219,8 @@ const program = new Command()
|
|
|
200
219
|
resourcesPath = undefined;
|
|
201
220
|
}
|
|
202
221
|
}
|
|
203
|
-
if ((detectedFramework === "nextjs" ||
|
|
222
|
+
if ((detectedFramework === "nextjs" ||
|
|
223
|
+
isFetchFramework(detectedFramework)) &&
|
|
204
224
|
!options.skipRoute &&
|
|
205
225
|
answers.routePath) {
|
|
206
226
|
routePath = answers.routePath;
|
|
@@ -285,7 +305,7 @@ const program = new Command()
|
|
|
285
305
|
console.log(` • ${resourcesPath}/(users)/[userId]/profile.ts`);
|
|
286
306
|
}
|
|
287
307
|
if (routePath) {
|
|
288
|
-
console.log(` • ${routePath}/${detectedFramework === "
|
|
308
|
+
console.log(` • ${routePath}/${detectedFramework === "sveltekit" ? "+server.ts" : isFetchFramework(detectedFramework) ? "mcp.ts" : "route.ts"}`);
|
|
289
309
|
}
|
|
290
310
|
if (detectedFramework === "nestjs") {
|
|
291
311
|
const hasSrcFolder = fs.existsSync(path.join(projectRoot, "src"));
|
|
@@ -295,8 +315,24 @@ const program = new Command()
|
|
|
295
315
|
}
|
|
296
316
|
console.log(chalk.blue("\n❯ Files updated:"));
|
|
297
317
|
console.log(` • package.json`);
|
|
298
|
-
|
|
318
|
+
if (!isFetchFramework(detectedFramework))
|
|
319
|
+
console.log(` • tsconfig.json`);
|
|
299
320
|
console.log(chalk.blue("\nNext steps:"));
|
|
321
|
+
if (detectedFramework === "hono") {
|
|
322
|
+
if (routePath) {
|
|
323
|
+
console.log(`Mount the router from ${routePath}/mcp.ts in your Hono app:\n\napp.route("/mcp", mcp);\n`);
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
console.log('Import xmcpHandler from .xmcp/adapter/index.js and register app.all("/mcp", (c) => xmcpHandler(c.req.raw)).');
|
|
327
|
+
}
|
|
328
|
+
console.log("Keep your existing Hono dev/build commands after the xmcp commands. See https://xmcp.dev/docs/adapters/hono");
|
|
329
|
+
}
|
|
330
|
+
if (detectedFramework === "nuxt") {
|
|
331
|
+
console.log("Add .xmcp to nitro.externals.inline in nuxt.config.ts so Nitro bundles the adapter during development. See https://xmcp.dev/docs/adapters/nuxt");
|
|
332
|
+
}
|
|
333
|
+
if (detectedFramework === "react-router" && routePath) {
|
|
334
|
+
console.log(`Register ${routePath}/mcp.ts as a resource route in your route config (no default component). See https://xmcp.dev/docs/adapters/react-router`);
|
|
335
|
+
}
|
|
300
336
|
// code integration for express projects
|
|
301
337
|
if (detectedFramework === "express") {
|
|
302
338
|
const integrationCode = `import { xmcpHandler } from '@xmcp/adapter';
|
package/package.json
CHANGED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, it } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import fs from "fs-extra";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import os from "node:os";
|
|
6
|
+
import { spawnSync } from "node:child_process";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { detectFramework } from "../helpers/detect-framework.js";
|
|
9
|
+
import {
|
|
10
|
+
createFetchRoute,
|
|
11
|
+
detectFetchCloudflare,
|
|
12
|
+
getFetchRoute,
|
|
13
|
+
} from "../helpers/create-fetch-route.js";
|
|
14
|
+
import { updatePackageJson } from "../helpers/update-package.js";
|
|
15
|
+
|
|
16
|
+
for (const framework of [
|
|
17
|
+
"hono",
|
|
18
|
+
"sveltekit",
|
|
19
|
+
"nuxt",
|
|
20
|
+
"react-router",
|
|
21
|
+
"astro",
|
|
22
|
+
] as const) {
|
|
23
|
+
describe(`${framework} initialization`, () => {
|
|
24
|
+
let root: string;
|
|
25
|
+
const dependency = {
|
|
26
|
+
hono: "hono",
|
|
27
|
+
sveltekit: "@sveltejs/kit",
|
|
28
|
+
nuxt: "nuxt",
|
|
29
|
+
"react-router": "@react-router/dev",
|
|
30
|
+
astro: "astro",
|
|
31
|
+
}[framework];
|
|
32
|
+
const directory = {
|
|
33
|
+
hono: "src/routes",
|
|
34
|
+
sveltekit: "src/routes/mcp",
|
|
35
|
+
nuxt: "server/routes",
|
|
36
|
+
"react-router": "app/routes",
|
|
37
|
+
astro: "src/pages",
|
|
38
|
+
}[framework];
|
|
39
|
+
const customDirectory =
|
|
40
|
+
framework === "astro"
|
|
41
|
+
? "custom/pages/api"
|
|
42
|
+
: framework === "nuxt"
|
|
43
|
+
? "custom/routes/api"
|
|
44
|
+
: "custom/routes/api/mcp";
|
|
45
|
+
beforeEach(() => {
|
|
46
|
+
root = fs.mkdtempSync(path.join(os.tmpdir(), `xmcp-${framework}-`));
|
|
47
|
+
});
|
|
48
|
+
afterEach(() => fs.removeSync(root));
|
|
49
|
+
|
|
50
|
+
function fixture(extraDependencies = {}) {
|
|
51
|
+
fs.writeJsonSync(path.join(root, "package.json"), {
|
|
52
|
+
dependencies: { [dependency]: "*", ...extraDependencies },
|
|
53
|
+
devDependencies: { typescript: "*" },
|
|
54
|
+
scripts: {
|
|
55
|
+
dev: "host dev",
|
|
56
|
+
build: "host build",
|
|
57
|
+
deploy: "host deploy",
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
fs.writeJsonSync(path.join(root, "tsconfig.json"), {
|
|
61
|
+
extends: "./host-generated/tsconfig.json",
|
|
62
|
+
compilerOptions: { paths: { "$lib/*": ["src/lib/*"] } },
|
|
63
|
+
});
|
|
64
|
+
const bin = path.join(root, "bin");
|
|
65
|
+
fs.ensureDirSync(bin);
|
|
66
|
+
fs.writeFileSync(path.join(bin, "npm"), "#!/bin/sh\nexit 0\n", {
|
|
67
|
+
mode: 0o755,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function cli(...args: string[]) {
|
|
72
|
+
return spawnSync(
|
|
73
|
+
process.execPath,
|
|
74
|
+
[
|
|
75
|
+
fileURLToPath(new URL("../index.js", import.meta.url)),
|
|
76
|
+
"--yes",
|
|
77
|
+
"--package-manager",
|
|
78
|
+
"npm",
|
|
79
|
+
...args,
|
|
80
|
+
],
|
|
81
|
+
{
|
|
82
|
+
cwd: root,
|
|
83
|
+
encoding: "utf8",
|
|
84
|
+
env: {
|
|
85
|
+
...process.env,
|
|
86
|
+
PATH: `${path.join(root, "bin")}${path.delimiter}${process.env.PATH}`,
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
it("detects dependencies and devDependencies before generic server frameworks", () => {
|
|
93
|
+
for (const section of ["dependencies", "devDependencies"]) {
|
|
94
|
+
fs.writeJsonSync(path.join(root, "package.json"), {
|
|
95
|
+
[section]: {
|
|
96
|
+
[dependency]: "*",
|
|
97
|
+
fastify: "*",
|
|
98
|
+
...(framework !== "hono" ? { hono: "*" } : {}),
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
assert.equal(detectFramework(root), framework);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("infers Workers from framework hosting dependencies", () => {
|
|
106
|
+
fixture();
|
|
107
|
+
assert.equal(detectFetchCloudflare(root, framework), false);
|
|
108
|
+
for (const section of ["dependencies", "devDependencies"]) {
|
|
109
|
+
for (const pkg of [
|
|
110
|
+
"@cloudflare/vite-plugin",
|
|
111
|
+
{
|
|
112
|
+
hono: "wrangler",
|
|
113
|
+
sveltekit: "@sveltejs/adapter-cloudflare",
|
|
114
|
+
nuxt: "wrangler",
|
|
115
|
+
"react-router": "@cloudflare/vite-plugin",
|
|
116
|
+
astro: "@astrojs/cloudflare",
|
|
117
|
+
}[framework],
|
|
118
|
+
]) {
|
|
119
|
+
fs.writeJsonSync(path.join(root, "package.json"), {
|
|
120
|
+
[section]: { [pkg]: "*" },
|
|
121
|
+
});
|
|
122
|
+
assert.equal(detectFetchCloudflare(root, framework), true);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("generates default and custom routes with relative imports", () => {
|
|
128
|
+
for (const routeDirectory of [directory, customDirectory]) {
|
|
129
|
+
createFetchRoute(root, framework, routeDirectory);
|
|
130
|
+
const route = getFetchRoute(root, framework, routeDirectory);
|
|
131
|
+
assert.equal(
|
|
132
|
+
path.resolve(path.dirname(route.file), route.adapterImport),
|
|
133
|
+
path.join(root, ".xmcp/adapter/index.js")
|
|
134
|
+
);
|
|
135
|
+
const content = fs.readFileSync(route.file, "utf8");
|
|
136
|
+
if (framework === "hono") {
|
|
137
|
+
assert.match(
|
|
138
|
+
content,
|
|
139
|
+
/mcp.all\("\/", \(c\) => xmcpHandler\(c.req.raw\)\)/
|
|
140
|
+
);
|
|
141
|
+
} else if (framework === "sveltekit") {
|
|
142
|
+
for (const method of ["GET", "POST", "DELETE"])
|
|
143
|
+
assert.ok(
|
|
144
|
+
content.includes(`export const ${method}: RequestHandler`)
|
|
145
|
+
);
|
|
146
|
+
assert.equal(
|
|
147
|
+
route.endpoint,
|
|
148
|
+
routeDirectory === directory ? "/mcp" : "/api/mcp"
|
|
149
|
+
);
|
|
150
|
+
} else if (framework === "nuxt") {
|
|
151
|
+
assert.match(content, /defineEventHandler/);
|
|
152
|
+
assert.match(content, /toWebRequest\(event\)/);
|
|
153
|
+
} else if (framework === "react-router") {
|
|
154
|
+
assert.match(content, /export function loader/);
|
|
155
|
+
assert.match(content, /export function action/);
|
|
156
|
+
assert.doesNotMatch(content, /export default/);
|
|
157
|
+
} else {
|
|
158
|
+
assert.match(content, /export const prerender = false/);
|
|
159
|
+
assert.match(content, /export const ALL: APIRoute/);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("preserves existing TypeScript and JavaScript routes", () => {
|
|
165
|
+
const route = getFetchRoute(root, framework, directory);
|
|
166
|
+
for (const file of [route.file, route.file.replace(/\.ts$/, ".js")]) {
|
|
167
|
+
fs.outputFileSync(file, "existing route");
|
|
168
|
+
assert.throws(
|
|
169
|
+
() => createFetchRoute(root, framework, directory),
|
|
170
|
+
/--skip-route/
|
|
171
|
+
);
|
|
172
|
+
assert.equal(fs.readFileSync(file, "utf8"), "existing route");
|
|
173
|
+
fs.removeSync(file);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("preserves framework-specific route variants", () => {
|
|
178
|
+
const variants = {
|
|
179
|
+
hono: ".js",
|
|
180
|
+
sveltekit: ".js",
|
|
181
|
+
nuxt: ".post.ts",
|
|
182
|
+
"react-router": ".tsx",
|
|
183
|
+
astro: ".astro",
|
|
184
|
+
};
|
|
185
|
+
const file = getFetchRoute(root, framework, directory).file.replace(
|
|
186
|
+
/\.ts$/,
|
|
187
|
+
variants[framework]
|
|
188
|
+
);
|
|
189
|
+
fs.outputFileSync(file, "existing route");
|
|
190
|
+
assert.throws(
|
|
191
|
+
() => createFetchRoute(root, framework, directory),
|
|
192
|
+
/--skip-route/
|
|
193
|
+
);
|
|
194
|
+
assert.equal(fs.readFileSync(file, "utf8"), "existing route");
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("prepends adapter builds and preserves host and deployment scripts", () => {
|
|
198
|
+
for (const cloudflare of [false, true]) {
|
|
199
|
+
fixture();
|
|
200
|
+
updatePackageJson(root, { framework, cloudflare });
|
|
201
|
+
const { scripts } = fs.readJsonSync(path.join(root, "package.json"));
|
|
202
|
+
const flag = cloudflare ? " --cf" : "";
|
|
203
|
+
assert.equal(scripts.build, `xmcp build${flag} && host build`);
|
|
204
|
+
assert.equal(
|
|
205
|
+
scripts.dev,
|
|
206
|
+
`xmcp build${flag} && (xmcp dev${flag} & host dev)`
|
|
207
|
+
);
|
|
208
|
+
assert.equal(scripts.deploy, "host deploy");
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("initializes noninteractively and preserves the host tsconfig", () => {
|
|
213
|
+
fixture();
|
|
214
|
+
const tsconfig = fs.readFileSync(
|
|
215
|
+
path.join(root, "tsconfig.json"),
|
|
216
|
+
"utf8"
|
|
217
|
+
);
|
|
218
|
+
const result = cli();
|
|
219
|
+
assert.equal(result.status, 0, result.stdout + result.stderr);
|
|
220
|
+
if (framework === "nuxt")
|
|
221
|
+
assert.match(result.stdout, /nitro\.externals\.inline/);
|
|
222
|
+
assert.ok(fs.existsSync(getFetchRoute(root, framework, directory).file));
|
|
223
|
+
assert.equal(
|
|
224
|
+
fs.readFileSync(path.join(root, "tsconfig.json"), "utf8"),
|
|
225
|
+
tsconfig
|
|
226
|
+
);
|
|
227
|
+
assert.match(
|
|
228
|
+
fs.readFileSync(path.join(root, "xmcp.config.ts"), "utf8"),
|
|
229
|
+
new RegExp(`adapter: "${framework}"`)
|
|
230
|
+
);
|
|
231
|
+
for (const folder of ["tools", "prompts", "resources"])
|
|
232
|
+
assert.ok(fs.existsSync(path.join(root, folder)));
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("honors custom route directories", () => {
|
|
236
|
+
fixture();
|
|
237
|
+
const result = cli("--route-path", customDirectory);
|
|
238
|
+
assert.equal(result.status, 0, result.stdout + result.stderr);
|
|
239
|
+
assert.ok(
|
|
240
|
+
fs.existsSync(getFetchRoute(root, framework, customDirectory).file)
|
|
241
|
+
);
|
|
242
|
+
if (["sveltekit", "nuxt", "astro"].includes(framework))
|
|
243
|
+
assert.match(
|
|
244
|
+
fs.readFileSync(path.join(root, "xmcp.config.ts"), "utf8"),
|
|
245
|
+
/endpoint: "\/api\/mcp"/
|
|
246
|
+
);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("rejects conflicts before installing or changing configuration", () => {
|
|
250
|
+
fixture();
|
|
251
|
+
fs.outputFileSync(
|
|
252
|
+
getFetchRoute(root, framework, directory).file,
|
|
253
|
+
"keep me"
|
|
254
|
+
);
|
|
255
|
+
const result = cli();
|
|
256
|
+
assert.equal(result.status, 1);
|
|
257
|
+
assert.match(result.stderr, /MCP route already exists/);
|
|
258
|
+
assert.equal(fs.existsSync(path.join(root, "xmcp.config.ts")), false);
|
|
259
|
+
assert.equal(
|
|
260
|
+
fs.readJsonSync(path.join(root, "package.json")).scripts.dev,
|
|
261
|
+
"host dev"
|
|
262
|
+
);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it("honors skip flags and explicit Workers mode", () => {
|
|
266
|
+
fixture();
|
|
267
|
+
const result = cli(
|
|
268
|
+
"--cf",
|
|
269
|
+
"--skip-route",
|
|
270
|
+
"--skip-tools",
|
|
271
|
+
"--skip-prompts",
|
|
272
|
+
"--skip-resources"
|
|
273
|
+
);
|
|
274
|
+
assert.equal(result.status, 0, result.stdout + result.stderr);
|
|
275
|
+
assert.equal(
|
|
276
|
+
fs.existsSync(getFetchRoute(root, framework, directory).file),
|
|
277
|
+
false
|
|
278
|
+
);
|
|
279
|
+
for (const folder of ["tools", "prompts", "resources"])
|
|
280
|
+
assert.equal(fs.existsSync(path.join(root, folder)), false);
|
|
281
|
+
assert.equal(
|
|
282
|
+
fs.readJsonSync(path.join(root, "package.json")).scripts.build,
|
|
283
|
+
"xmcp build --cf && host build"
|
|
284
|
+
);
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
it("excludes SvelteKit route groups from endpoint metadata", () => {
|
|
290
|
+
assert.equal(
|
|
291
|
+
getFetchRoute("/app", "sveltekit", "src/routes/(api)/mcp").endpoint,
|
|
292
|
+
"/mcp"
|
|
293
|
+
);
|
|
294
|
+
});
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import type { Framework } from "./detect-framework.js";
|
|
4
|
+
|
|
5
|
+
export type RoutedFetchFramework =
|
|
6
|
+
| "hono"
|
|
7
|
+
| "sveltekit"
|
|
8
|
+
| "nuxt"
|
|
9
|
+
| "react-router"
|
|
10
|
+
| "astro";
|
|
11
|
+
|
|
12
|
+
export function isRoutedFetchFramework(
|
|
13
|
+
framework: Framework
|
|
14
|
+
): framework is RoutedFetchFramework {
|
|
15
|
+
return isFetchFramework(framework) && framework !== "tanstack";
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function isFetchFramework(framework?: Framework): boolean {
|
|
19
|
+
return [
|
|
20
|
+
"tanstack",
|
|
21
|
+
"hono",
|
|
22
|
+
"sveltekit",
|
|
23
|
+
"nuxt",
|
|
24
|
+
"react-router",
|
|
25
|
+
"astro",
|
|
26
|
+
].includes(framework ?? "");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function detectFetchCloudflare(
|
|
30
|
+
projectRoot: string,
|
|
31
|
+
framework: Framework
|
|
32
|
+
): boolean {
|
|
33
|
+
const pkg = fs.readJsonSync(path.join(projectRoot, "package.json"));
|
|
34
|
+
const dependencies = { ...pkg.dependencies, ...pkg.devDependencies };
|
|
35
|
+
return !!(
|
|
36
|
+
dependencies["@cloudflare/vite-plugin"] ||
|
|
37
|
+
(framework === "sveltekit" &&
|
|
38
|
+
dependencies["@sveltejs/adapter-cloudflare"]) ||
|
|
39
|
+
(["hono", "nuxt"].includes(framework) && dependencies.wrangler) ||
|
|
40
|
+
(framework === "astro" && dependencies["@astrojs/cloudflare"])
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function getFetchRoute(
|
|
45
|
+
projectRoot: string,
|
|
46
|
+
framework: RoutedFetchFramework,
|
|
47
|
+
directory: string
|
|
48
|
+
) {
|
|
49
|
+
const file = path.resolve(
|
|
50
|
+
projectRoot,
|
|
51
|
+
directory,
|
|
52
|
+
framework === "sveltekit" ? "+server.ts" : "mcp.ts"
|
|
53
|
+
);
|
|
54
|
+
let adapterImport = path
|
|
55
|
+
.relative(
|
|
56
|
+
path.dirname(file),
|
|
57
|
+
path.join(projectRoot, ".xmcp/adapter/index.js")
|
|
58
|
+
)
|
|
59
|
+
.split(path.sep)
|
|
60
|
+
.join("/");
|
|
61
|
+
if (!adapterImport.startsWith(".")) adapterImport = `./${adapterImport}`;
|
|
62
|
+
// Route groups in SvelteKit do not contribute URL segments. A custom routes
|
|
63
|
+
// root cannot be inferred here; the handler itself does not depend on this URL.
|
|
64
|
+
const segments = directory.replace(/\\/g, "/").split("/");
|
|
65
|
+
const routesIndex = segments.lastIndexOf("routes");
|
|
66
|
+
let endpoint = "/mcp";
|
|
67
|
+
if (framework === "sveltekit" && routesIndex >= 0) {
|
|
68
|
+
endpoint = `/${segments
|
|
69
|
+
.slice(routesIndex + 1)
|
|
70
|
+
.filter((segment) => segment && !/^\(.*\)$/.test(segment))
|
|
71
|
+
.join("/")}`;
|
|
72
|
+
} else if (framework === "nuxt" || framework === "astro") {
|
|
73
|
+
const rootIndex = segments.lastIndexOf(
|
|
74
|
+
framework === "astro" ? "pages" : "routes"
|
|
75
|
+
);
|
|
76
|
+
const apiIndex = framework === "nuxt" ? segments.lastIndexOf("api") : -1;
|
|
77
|
+
const prefix =
|
|
78
|
+
rootIndex >= 0
|
|
79
|
+
? segments.slice(rootIndex + 1)
|
|
80
|
+
: apiIndex >= 0
|
|
81
|
+
? segments.slice(apiIndex)
|
|
82
|
+
: [];
|
|
83
|
+
endpoint = `/${[...prefix.filter(Boolean), "mcp"].join("/")}`;
|
|
84
|
+
}
|
|
85
|
+
return { file, adapterImport, endpoint };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function assertFetchRouteAvailable(
|
|
89
|
+
projectRoot: string,
|
|
90
|
+
framework: RoutedFetchFramework,
|
|
91
|
+
directory: string
|
|
92
|
+
) {
|
|
93
|
+
const { file } = getFetchRoute(projectRoot, framework, directory);
|
|
94
|
+
const extensions =
|
|
95
|
+
framework === "react-router"
|
|
96
|
+
? ["ts", "tsx", "js", "jsx"]
|
|
97
|
+
: framework === "astro"
|
|
98
|
+
? ["ts", "js", "astro"]
|
|
99
|
+
: ["ts", "js"];
|
|
100
|
+
const candidates = extensions.map((extension) =>
|
|
101
|
+
file.replace(/\.ts$/, `.${extension}`)
|
|
102
|
+
);
|
|
103
|
+
if (framework === "nuxt") {
|
|
104
|
+
for (const method of [
|
|
105
|
+
"get",
|
|
106
|
+
"post",
|
|
107
|
+
"delete",
|
|
108
|
+
"put",
|
|
109
|
+
"patch",
|
|
110
|
+
"head",
|
|
111
|
+
"options",
|
|
112
|
+
]) {
|
|
113
|
+
for (const extension of extensions)
|
|
114
|
+
candidates.push(file.replace(/\.ts$/, `.${method}.${extension}`));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
for (const candidate of candidates) {
|
|
118
|
+
if (fs.existsSync(candidate)) {
|
|
119
|
+
throw new Error(
|
|
120
|
+
`MCP route already exists: ${candidate}. Use --skip-route to keep it, or choose another --route-path.`
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function createFetchRoute(
|
|
127
|
+
projectRoot: string,
|
|
128
|
+
framework: RoutedFetchFramework,
|
|
129
|
+
directory: string
|
|
130
|
+
) {
|
|
131
|
+
assertFetchRouteAvailable(projectRoot, framework, directory);
|
|
132
|
+
const { file, adapterImport } = getFetchRoute(
|
|
133
|
+
projectRoot,
|
|
134
|
+
framework,
|
|
135
|
+
directory
|
|
136
|
+
);
|
|
137
|
+
const importHandler = `import { xmcpHandler } from ${JSON.stringify(adapterImport)};`;
|
|
138
|
+
const templates: Record<RoutedFetchFramework, string> = {
|
|
139
|
+
hono: `import { Hono } from "hono";
|
|
140
|
+
${importHandler}
|
|
141
|
+
|
|
142
|
+
const mcp = new Hono();
|
|
143
|
+
mcp.all("/", (c) => xmcpHandler(c.req.raw));
|
|
144
|
+
|
|
145
|
+
export default mcp;
|
|
146
|
+
`,
|
|
147
|
+
sveltekit: `import type { RequestHandler } from "./$types";
|
|
148
|
+
${importHandler}
|
|
149
|
+
|
|
150
|
+
export const GET: RequestHandler = ({ request }) => xmcpHandler(request);
|
|
151
|
+
export const POST: RequestHandler = ({ request }) => xmcpHandler(request);
|
|
152
|
+
export const DELETE: RequestHandler = ({ request }) => xmcpHandler(request);
|
|
153
|
+
`,
|
|
154
|
+
nuxt: `${importHandler}
|
|
155
|
+
|
|
156
|
+
export default defineEventHandler((event) => xmcpHandler(toWebRequest(event)));
|
|
157
|
+
`,
|
|
158
|
+
"react-router": `import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
|
|
159
|
+
${importHandler}
|
|
160
|
+
|
|
161
|
+
export function loader({ request }: LoaderFunctionArgs) {
|
|
162
|
+
return xmcpHandler(request);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function action({ request }: ActionFunctionArgs) {
|
|
166
|
+
return xmcpHandler(request);
|
|
167
|
+
}
|
|
168
|
+
`,
|
|
169
|
+
astro: `import type { APIRoute } from "astro";
|
|
170
|
+
${importHandler}
|
|
171
|
+
|
|
172
|
+
export const prerender = false;
|
|
173
|
+
export const ALL: APIRoute = ({ request }) => xmcpHandler(request);
|
|
174
|
+
`,
|
|
175
|
+
};
|
|
176
|
+
const content = templates[framework];
|
|
177
|
+
fs.ensureDirSync(path.dirname(file));
|
|
178
|
+
fs.writeFileSync(file, content, { flag: "wx" });
|
|
179
|
+
}
|
|
@@ -8,7 +8,12 @@ export type Framework =
|
|
|
8
8
|
| "nestjs"
|
|
9
9
|
| "express"
|
|
10
10
|
| "fastify"
|
|
11
|
-
| "tanstack"
|
|
11
|
+
| "tanstack"
|
|
12
|
+
| "hono"
|
|
13
|
+
| "sveltekit"
|
|
14
|
+
| "nuxt"
|
|
15
|
+
| "react-router"
|
|
16
|
+
| "astro";
|
|
12
17
|
|
|
13
18
|
export function detectFramework(projectRoot: string): Framework {
|
|
14
19
|
const packageJson = JSON.parse(
|
|
@@ -35,6 +40,21 @@ export function detectFramework(projectRoot: string): Framework {
|
|
|
35
40
|
return "tanstack";
|
|
36
41
|
}
|
|
37
42
|
|
|
43
|
+
for (const [dependency, framework] of [
|
|
44
|
+
["@sveltejs/kit", "sveltekit"],
|
|
45
|
+
["nuxt", "nuxt"],
|
|
46
|
+
["@react-router/dev", "react-router"],
|
|
47
|
+
["astro", "astro"],
|
|
48
|
+
["hono", "hono"],
|
|
49
|
+
] as const) {
|
|
50
|
+
if (
|
|
51
|
+
packageJson.dependencies?.[dependency] ||
|
|
52
|
+
packageJson.devDependencies?.[dependency]
|
|
53
|
+
) {
|
|
54
|
+
return framework;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
38
58
|
// Check for Fastify
|
|
39
59
|
if (
|
|
40
60
|
packageJson.dependencies?.fastify ||
|
|
@@ -30,7 +30,17 @@ const config: XmcpConfig = {
|
|
|
30
30
|
resources: ${resourcesPath ? `"${resourcesPath}"` : "false"},
|
|
31
31
|
},`;
|
|
32
32
|
|
|
33
|
-
if (
|
|
33
|
+
if (
|
|
34
|
+
[
|
|
35
|
+
"nextjs",
|
|
36
|
+
"tanstack",
|
|
37
|
+
"hono",
|
|
38
|
+
"sveltekit",
|
|
39
|
+
"nuxt",
|
|
40
|
+
"react-router",
|
|
41
|
+
"astro",
|
|
42
|
+
].includes(frameworkAdapter)
|
|
43
|
+
) {
|
|
34
44
|
configContent += `
|
|
35
45
|
typescript: {
|
|
36
46
|
skipTypeCheck: true,
|
package/src/helpers/init.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertFetchRouteAvailable,
|
|
3
|
+
createFetchRoute,
|
|
4
|
+
getFetchRoute,
|
|
5
|
+
isFetchFramework,
|
|
6
|
+
isRoutedFetchFramework,
|
|
7
|
+
} from "./create-fetch-route.js";
|
|
1
8
|
import { generateConfig } from "./generate-config.js";
|
|
2
9
|
import { install } from "./install.js";
|
|
3
10
|
import { updatePackageJson } from "./update-package.js";
|
|
@@ -44,10 +51,16 @@ export async function init(options: InitOptions) {
|
|
|
44
51
|
if (framework === "tanstack" && routePath) {
|
|
45
52
|
assertTanstackRouteAvailable(projectRoot, routePath);
|
|
46
53
|
}
|
|
54
|
+
const hasFetchRoute = isRoutedFetchFramework(framework);
|
|
55
|
+
if (hasFetchRoute && routePath) {
|
|
56
|
+
assertFetchRouteAvailable(projectRoot, framework, routePath);
|
|
57
|
+
}
|
|
47
58
|
const endpoint =
|
|
48
59
|
framework === "tanstack" && routePath
|
|
49
60
|
? getTanstackRoute(projectRoot, routePath).endpoint
|
|
50
|
-
:
|
|
61
|
+
: hasFetchRoute && routePath
|
|
62
|
+
? getFetchRoute(projectRoot, framework, routePath).endpoint
|
|
63
|
+
: undefined;
|
|
51
64
|
generateConfig(
|
|
52
65
|
projectRoot,
|
|
53
66
|
framework,
|
|
@@ -61,7 +74,8 @@ export async function init(options: InitOptions) {
|
|
|
61
74
|
|
|
62
75
|
updatePackageJson(projectRoot, { framework, cloudflare });
|
|
63
76
|
|
|
64
|
-
|
|
77
|
+
// Fetch routes use relative imports. Preserve host-generated paths and includes.
|
|
78
|
+
if (!isFetchFramework(framework)) updateTsConfig(projectRoot);
|
|
65
79
|
|
|
66
80
|
updateGitignore(projectRoot);
|
|
67
81
|
|
|
@@ -85,6 +99,10 @@ export async function init(options: InitOptions) {
|
|
|
85
99
|
createTanstackRoute(projectRoot, routePath);
|
|
86
100
|
}
|
|
87
101
|
|
|
102
|
+
if (hasFetchRoute && routePath) {
|
|
103
|
+
createFetchRoute(projectRoot, framework, routePath);
|
|
104
|
+
}
|
|
105
|
+
|
|
88
106
|
if (framework === "nestjs") {
|
|
89
107
|
createNestJsModule(projectRoot);
|
|
90
108
|
}
|