init-xmcp 1.5.0 → 1.7.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 +9 -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/.turbo/turbo-build.log
CHANGED
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# init-xmcp
|
|
2
2
|
|
|
3
|
+
## 1.7.0
|
|
4
|
+
|
|
5
|
+
## 1.6.0
|
|
6
|
+
|
|
7
|
+
### Minor Changes
|
|
8
|
+
|
|
9
|
+
- 5948dda: Add Hono and SvelteKit adapters for Node.js and Cloudflare Workers, sharing the stateless Fetch runtime with TanStack Start. Add framework detection, route generation, build ordering, and Workers setup to init-xmcp while preserving host TypeScript configuration.
|
|
10
|
+
- 5948dda: Add Nuxt, React Router Framework Mode, and Astro adapters using the shared stateless Fetch runtime, with automatic route setup and Node.js/Cloudflare Workers support.
|
|
11
|
+
|
|
3
12
|
## 1.5.0
|
|
4
13
|
|
|
5
14
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -57,3 +57,48 @@ examples, manual setup, authentication, and CORS.
|
|
|
57
57
|
## License
|
|
58
58
|
|
|
59
59
|
This project is licensed under the MIT License - see the [LICENSE](<[LICENSE](https://github.com/basementstudio/xmcp/blob/main/license.md)>) file for details.
|
|
60
|
+
|
|
61
|
+
## Hono and SvelteKit
|
|
62
|
+
|
|
63
|
+
`hono` and `@sveltejs/kit` are detected in dependencies or dev dependencies.
|
|
64
|
+
Hono setup creates `src/routes/mcp.ts`; mount its default router in your app with
|
|
65
|
+
`app.route("/mcp", mcp)`. SvelteKit setup creates `src/routes/mcp/+server.ts`.
|
|
66
|
+
Both use relative adapter imports and preserve the host's TypeScript configuration.
|
|
67
|
+
|
|
68
|
+
`--route-path` selects the directory containing `mcp.ts` (Hono) or `+server.ts`
|
|
69
|
+
(SvelteKit). Existing TypeScript or JavaScript route files are preserved and
|
|
70
|
+
reported as conflicts. `--skip-route`, other component skip flags, and `--yes`
|
|
71
|
+
remain available.
|
|
72
|
+
|
|
73
|
+
Workers mode is inferred from `@cloudflare/vite-plugin`, Hono's `wrangler`, or
|
|
74
|
+
SvelteKit's `@sveltejs/adapter-cloudflare`. `--cf` selects it explicitly. Existing
|
|
75
|
+
host and deploy scripts are preserved; xmcp builds run before the host build and
|
|
76
|
+
before starting development watchers. Apps without host scripts must provide
|
|
77
|
+
their own Hono server command. Hosting configuration remains the app's responsibility.
|
|
78
|
+
|
|
79
|
+
See the [Hono guide](https://xmcp.dev/docs/adapters/hono) and
|
|
80
|
+
[SvelteKit guide](https://xmcp.dev/docs/adapters/sveltekit) for Node and Workers setup.
|
|
81
|
+
|
|
82
|
+
## Nuxt, React Router, and Astro
|
|
83
|
+
|
|
84
|
+
Initialization detects `nuxt`, `@react-router/dev` (Framework Mode), and `astro`.
|
|
85
|
+
Default routes are `server/routes/mcp.ts`, `app/routes/mcp.ts`, and
|
|
86
|
+
`src/pages/mcp.ts`, respectively. React Router users must register the generated
|
|
87
|
+
resource route in their existing route configuration. Astro users must have a
|
|
88
|
+
server adapter; the generated endpoint disables prerendering.
|
|
89
|
+
|
|
90
|
+
These adapters use the same Fetch handler and relative imports as Hono and
|
|
91
|
+
SvelteKit. They preserve TypeScript configuration and existing host commands.
|
|
92
|
+
`--route-path`, `--skip-route`, component skip flags, and `--yes` work for all three.
|
|
93
|
+
Nuxt also detects conflicts with method-specific route files.
|
|
94
|
+
|
|
95
|
+
Workers mode is inferred from `@cloudflare/vite-plugin`, Nuxt's `wrangler`, or
|
|
96
|
+
Astro's `@astrojs/cloudflare`; `--cf` overrides detection. Configure the host's
|
|
97
|
+
Workers integration and `nodejs_compat` separately. See the
|
|
98
|
+
[Nuxt](https://xmcp.dev/docs/adapters/nuxt),
|
|
99
|
+
[React Router](https://xmcp.dev/docs/adapters/react-router), and
|
|
100
|
+
[Astro](https://xmcp.dev/docs/adapters/astro) guides and runnable examples.
|
|
101
|
+
|
|
102
|
+
For Nuxt, include `.xmcp` in `nitro.externals.inline` in your existing Nuxt config
|
|
103
|
+
so Nitro bundles and watches the generated adapter during development. The CLI
|
|
104
|
+
prints this step; see the [Nuxt guide](https://xmcp.dev/docs/adapters/nuxt).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,217 @@
|
|
|
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 { createFetchRoute, detectFetchCloudflare, getFetchRoute, } from "../helpers/create-fetch-route.js";
|
|
10
|
+
import { updatePackageJson } from "../helpers/update-package.js";
|
|
11
|
+
for (const framework of [
|
|
12
|
+
"hono",
|
|
13
|
+
"sveltekit",
|
|
14
|
+
"nuxt",
|
|
15
|
+
"react-router",
|
|
16
|
+
"astro",
|
|
17
|
+
]) {
|
|
18
|
+
describe(`${framework} initialization`, () => {
|
|
19
|
+
let root;
|
|
20
|
+
const dependency = {
|
|
21
|
+
hono: "hono",
|
|
22
|
+
sveltekit: "@sveltejs/kit",
|
|
23
|
+
nuxt: "nuxt",
|
|
24
|
+
"react-router": "@react-router/dev",
|
|
25
|
+
astro: "astro",
|
|
26
|
+
}[framework];
|
|
27
|
+
const directory = {
|
|
28
|
+
hono: "src/routes",
|
|
29
|
+
sveltekit: "src/routes/mcp",
|
|
30
|
+
nuxt: "server/routes",
|
|
31
|
+
"react-router": "app/routes",
|
|
32
|
+
astro: "src/pages",
|
|
33
|
+
}[framework];
|
|
34
|
+
const customDirectory = framework === "astro"
|
|
35
|
+
? "custom/pages/api"
|
|
36
|
+
: framework === "nuxt"
|
|
37
|
+
? "custom/routes/api"
|
|
38
|
+
: "custom/routes/api/mcp";
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
root = fs.mkdtempSync(path.join(os.tmpdir(), `xmcp-${framework}-`));
|
|
41
|
+
});
|
|
42
|
+
afterEach(() => fs.removeSync(root));
|
|
43
|
+
function fixture(extraDependencies = {}) {
|
|
44
|
+
fs.writeJsonSync(path.join(root, "package.json"), {
|
|
45
|
+
dependencies: { [dependency]: "*", ...extraDependencies },
|
|
46
|
+
devDependencies: { typescript: "*" },
|
|
47
|
+
scripts: {
|
|
48
|
+
dev: "host dev",
|
|
49
|
+
build: "host build",
|
|
50
|
+
deploy: "host deploy",
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
fs.writeJsonSync(path.join(root, "tsconfig.json"), {
|
|
54
|
+
extends: "./host-generated/tsconfig.json",
|
|
55
|
+
compilerOptions: { paths: { "$lib/*": ["src/lib/*"] } },
|
|
56
|
+
});
|
|
57
|
+
const bin = path.join(root, "bin");
|
|
58
|
+
fs.ensureDirSync(bin);
|
|
59
|
+
fs.writeFileSync(path.join(bin, "npm"), "#!/bin/sh\nexit 0\n", {
|
|
60
|
+
mode: 0o755,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
function cli(...args) {
|
|
64
|
+
return spawnSync(process.execPath, [
|
|
65
|
+
fileURLToPath(new URL("../index.js", import.meta.url)),
|
|
66
|
+
"--yes",
|
|
67
|
+
"--package-manager",
|
|
68
|
+
"npm",
|
|
69
|
+
...args,
|
|
70
|
+
], {
|
|
71
|
+
cwd: root,
|
|
72
|
+
encoding: "utf8",
|
|
73
|
+
env: {
|
|
74
|
+
...process.env,
|
|
75
|
+
PATH: `${path.join(root, "bin")}${path.delimiter}${process.env.PATH}`,
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
it("detects dependencies and devDependencies before generic server frameworks", () => {
|
|
80
|
+
for (const section of ["dependencies", "devDependencies"]) {
|
|
81
|
+
fs.writeJsonSync(path.join(root, "package.json"), {
|
|
82
|
+
[section]: {
|
|
83
|
+
[dependency]: "*",
|
|
84
|
+
fastify: "*",
|
|
85
|
+
...(framework !== "hono" ? { hono: "*" } : {}),
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
assert.equal(detectFramework(root), framework);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
it("infers Workers from framework hosting dependencies", () => {
|
|
92
|
+
fixture();
|
|
93
|
+
assert.equal(detectFetchCloudflare(root, framework), false);
|
|
94
|
+
for (const section of ["dependencies", "devDependencies"]) {
|
|
95
|
+
for (const pkg of [
|
|
96
|
+
"@cloudflare/vite-plugin",
|
|
97
|
+
{
|
|
98
|
+
hono: "wrangler",
|
|
99
|
+
sveltekit: "@sveltejs/adapter-cloudflare",
|
|
100
|
+
nuxt: "wrangler",
|
|
101
|
+
"react-router": "@cloudflare/vite-plugin",
|
|
102
|
+
astro: "@astrojs/cloudflare",
|
|
103
|
+
}[framework],
|
|
104
|
+
]) {
|
|
105
|
+
fs.writeJsonSync(path.join(root, "package.json"), {
|
|
106
|
+
[section]: { [pkg]: "*" },
|
|
107
|
+
});
|
|
108
|
+
assert.equal(detectFetchCloudflare(root, framework), true);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
it("generates default and custom routes with relative imports", () => {
|
|
113
|
+
for (const routeDirectory of [directory, customDirectory]) {
|
|
114
|
+
createFetchRoute(root, framework, routeDirectory);
|
|
115
|
+
const route = getFetchRoute(root, framework, routeDirectory);
|
|
116
|
+
assert.equal(path.resolve(path.dirname(route.file), route.adapterImport), path.join(root, ".xmcp/adapter/index.js"));
|
|
117
|
+
const content = fs.readFileSync(route.file, "utf8");
|
|
118
|
+
if (framework === "hono") {
|
|
119
|
+
assert.match(content, /mcp.all\("\/", \(c\) => xmcpHandler\(c.req.raw\)\)/);
|
|
120
|
+
}
|
|
121
|
+
else if (framework === "sveltekit") {
|
|
122
|
+
for (const method of ["GET", "POST", "DELETE"])
|
|
123
|
+
assert.ok(content.includes(`export const ${method}: RequestHandler`));
|
|
124
|
+
assert.equal(route.endpoint, routeDirectory === directory ? "/mcp" : "/api/mcp");
|
|
125
|
+
}
|
|
126
|
+
else if (framework === "nuxt") {
|
|
127
|
+
assert.match(content, /defineEventHandler/);
|
|
128
|
+
assert.match(content, /toWebRequest\(event\)/);
|
|
129
|
+
}
|
|
130
|
+
else if (framework === "react-router") {
|
|
131
|
+
assert.match(content, /export function loader/);
|
|
132
|
+
assert.match(content, /export function action/);
|
|
133
|
+
assert.doesNotMatch(content, /export default/);
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
assert.match(content, /export const prerender = false/);
|
|
137
|
+
assert.match(content, /export const ALL: APIRoute/);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
it("preserves existing TypeScript and JavaScript routes", () => {
|
|
142
|
+
const route = getFetchRoute(root, framework, directory);
|
|
143
|
+
for (const file of [route.file, route.file.replace(/\.ts$/, ".js")]) {
|
|
144
|
+
fs.outputFileSync(file, "existing route");
|
|
145
|
+
assert.throws(() => createFetchRoute(root, framework, directory), /--skip-route/);
|
|
146
|
+
assert.equal(fs.readFileSync(file, "utf8"), "existing route");
|
|
147
|
+
fs.removeSync(file);
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
it("preserves framework-specific route variants", () => {
|
|
151
|
+
const variants = {
|
|
152
|
+
hono: ".js",
|
|
153
|
+
sveltekit: ".js",
|
|
154
|
+
nuxt: ".post.ts",
|
|
155
|
+
"react-router": ".tsx",
|
|
156
|
+
astro: ".astro",
|
|
157
|
+
};
|
|
158
|
+
const file = getFetchRoute(root, framework, directory).file.replace(/\.ts$/, variants[framework]);
|
|
159
|
+
fs.outputFileSync(file, "existing route");
|
|
160
|
+
assert.throws(() => createFetchRoute(root, framework, directory), /--skip-route/);
|
|
161
|
+
assert.equal(fs.readFileSync(file, "utf8"), "existing route");
|
|
162
|
+
});
|
|
163
|
+
it("prepends adapter builds and preserves host and deployment scripts", () => {
|
|
164
|
+
for (const cloudflare of [false, true]) {
|
|
165
|
+
fixture();
|
|
166
|
+
updatePackageJson(root, { framework, cloudflare });
|
|
167
|
+
const { scripts } = fs.readJsonSync(path.join(root, "package.json"));
|
|
168
|
+
const flag = cloudflare ? " --cf" : "";
|
|
169
|
+
assert.equal(scripts.build, `xmcp build${flag} && host build`);
|
|
170
|
+
assert.equal(scripts.dev, `xmcp build${flag} && (xmcp dev${flag} & host dev)`);
|
|
171
|
+
assert.equal(scripts.deploy, "host deploy");
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
it("initializes noninteractively and preserves the host tsconfig", () => {
|
|
175
|
+
fixture();
|
|
176
|
+
const tsconfig = fs.readFileSync(path.join(root, "tsconfig.json"), "utf8");
|
|
177
|
+
const result = cli();
|
|
178
|
+
assert.equal(result.status, 0, result.stdout + result.stderr);
|
|
179
|
+
if (framework === "nuxt")
|
|
180
|
+
assert.match(result.stdout, /nitro\.externals\.inline/);
|
|
181
|
+
assert.ok(fs.existsSync(getFetchRoute(root, framework, directory).file));
|
|
182
|
+
assert.equal(fs.readFileSync(path.join(root, "tsconfig.json"), "utf8"), tsconfig);
|
|
183
|
+
assert.match(fs.readFileSync(path.join(root, "xmcp.config.ts"), "utf8"), new RegExp(`adapter: "${framework}"`));
|
|
184
|
+
for (const folder of ["tools", "prompts", "resources"])
|
|
185
|
+
assert.ok(fs.existsSync(path.join(root, folder)));
|
|
186
|
+
});
|
|
187
|
+
it("honors custom route directories", () => {
|
|
188
|
+
fixture();
|
|
189
|
+
const result = cli("--route-path", customDirectory);
|
|
190
|
+
assert.equal(result.status, 0, result.stdout + result.stderr);
|
|
191
|
+
assert.ok(fs.existsSync(getFetchRoute(root, framework, customDirectory).file));
|
|
192
|
+
if (["sveltekit", "nuxt", "astro"].includes(framework))
|
|
193
|
+
assert.match(fs.readFileSync(path.join(root, "xmcp.config.ts"), "utf8"), /endpoint: "\/api\/mcp"/);
|
|
194
|
+
});
|
|
195
|
+
it("rejects conflicts before installing or changing configuration", () => {
|
|
196
|
+
fixture();
|
|
197
|
+
fs.outputFileSync(getFetchRoute(root, framework, directory).file, "keep me");
|
|
198
|
+
const result = cli();
|
|
199
|
+
assert.equal(result.status, 1);
|
|
200
|
+
assert.match(result.stderr, /MCP route already exists/);
|
|
201
|
+
assert.equal(fs.existsSync(path.join(root, "xmcp.config.ts")), false);
|
|
202
|
+
assert.equal(fs.readJsonSync(path.join(root, "package.json")).scripts.dev, "host dev");
|
|
203
|
+
});
|
|
204
|
+
it("honors skip flags and explicit Workers mode", () => {
|
|
205
|
+
fixture();
|
|
206
|
+
const result = cli("--cf", "--skip-route", "--skip-tools", "--skip-prompts", "--skip-resources");
|
|
207
|
+
assert.equal(result.status, 0, result.stdout + result.stderr);
|
|
208
|
+
assert.equal(fs.existsSync(getFetchRoute(root, framework, directory).file), false);
|
|
209
|
+
for (const folder of ["tools", "prompts", "resources"])
|
|
210
|
+
assert.equal(fs.existsSync(path.join(root, folder)), false);
|
|
211
|
+
assert.equal(fs.readJsonSync(path.join(root, "package.json")).scripts.build, "xmcp build --cf && host build");
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
it("excludes SvelteKit route groups from endpoint metadata", () => {
|
|
216
|
+
assert.equal(getFetchRoute("/app", "sveltekit", "src/routes/(api)/mcp").endpoint, "/mcp");
|
|
217
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Framework } from "./detect-framework.js";
|
|
2
|
+
export type RoutedFetchFramework = "hono" | "sveltekit" | "nuxt" | "react-router" | "astro";
|
|
3
|
+
export declare function isRoutedFetchFramework(framework: Framework): framework is RoutedFetchFramework;
|
|
4
|
+
export declare function isFetchFramework(framework?: Framework): boolean;
|
|
5
|
+
export declare function detectFetchCloudflare(projectRoot: string, framework: Framework): boolean;
|
|
6
|
+
export declare function getFetchRoute(projectRoot: string, framework: RoutedFetchFramework, directory: string): {
|
|
7
|
+
file: string;
|
|
8
|
+
adapterImport: string;
|
|
9
|
+
endpoint: string;
|
|
10
|
+
};
|
|
11
|
+
export declare function assertFetchRouteAvailable(projectRoot: string, framework: RoutedFetchFramework, directory: string): void;
|
|
12
|
+
export declare function createFetchRoute(projectRoot: string, framework: RoutedFetchFramework, directory: string): void;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
export function isRoutedFetchFramework(framework) {
|
|
4
|
+
return isFetchFramework(framework) && framework !== "tanstack";
|
|
5
|
+
}
|
|
6
|
+
export function isFetchFramework(framework) {
|
|
7
|
+
return [
|
|
8
|
+
"tanstack",
|
|
9
|
+
"hono",
|
|
10
|
+
"sveltekit",
|
|
11
|
+
"nuxt",
|
|
12
|
+
"react-router",
|
|
13
|
+
"astro",
|
|
14
|
+
].includes(framework ?? "");
|
|
15
|
+
}
|
|
16
|
+
export function detectFetchCloudflare(projectRoot, framework) {
|
|
17
|
+
const pkg = fs.readJsonSync(path.join(projectRoot, "package.json"));
|
|
18
|
+
const dependencies = { ...pkg.dependencies, ...pkg.devDependencies };
|
|
19
|
+
return !!(dependencies["@cloudflare/vite-plugin"] ||
|
|
20
|
+
(framework === "sveltekit" &&
|
|
21
|
+
dependencies["@sveltejs/adapter-cloudflare"]) ||
|
|
22
|
+
(["hono", "nuxt"].includes(framework) && dependencies.wrangler) ||
|
|
23
|
+
(framework === "astro" && dependencies["@astrojs/cloudflare"]));
|
|
24
|
+
}
|
|
25
|
+
export function getFetchRoute(projectRoot, framework, directory) {
|
|
26
|
+
const file = path.resolve(projectRoot, directory, framework === "sveltekit" ? "+server.ts" : "mcp.ts");
|
|
27
|
+
let adapterImport = path
|
|
28
|
+
.relative(path.dirname(file), path.join(projectRoot, ".xmcp/adapter/index.js"))
|
|
29
|
+
.split(path.sep)
|
|
30
|
+
.join("/");
|
|
31
|
+
if (!adapterImport.startsWith("."))
|
|
32
|
+
adapterImport = `./${adapterImport}`;
|
|
33
|
+
// Route groups in SvelteKit do not contribute URL segments. A custom routes
|
|
34
|
+
// root cannot be inferred here; the handler itself does not depend on this URL.
|
|
35
|
+
const segments = directory.replace(/\\/g, "/").split("/");
|
|
36
|
+
const routesIndex = segments.lastIndexOf("routes");
|
|
37
|
+
let endpoint = "/mcp";
|
|
38
|
+
if (framework === "sveltekit" && routesIndex >= 0) {
|
|
39
|
+
endpoint = `/${segments
|
|
40
|
+
.slice(routesIndex + 1)
|
|
41
|
+
.filter((segment) => segment && !/^\(.*\)$/.test(segment))
|
|
42
|
+
.join("/")}`;
|
|
43
|
+
}
|
|
44
|
+
else if (framework === "nuxt" || framework === "astro") {
|
|
45
|
+
const rootIndex = segments.lastIndexOf(framework === "astro" ? "pages" : "routes");
|
|
46
|
+
const apiIndex = framework === "nuxt" ? segments.lastIndexOf("api") : -1;
|
|
47
|
+
const prefix = rootIndex >= 0
|
|
48
|
+
? segments.slice(rootIndex + 1)
|
|
49
|
+
: apiIndex >= 0
|
|
50
|
+
? segments.slice(apiIndex)
|
|
51
|
+
: [];
|
|
52
|
+
endpoint = `/${[...prefix.filter(Boolean), "mcp"].join("/")}`;
|
|
53
|
+
}
|
|
54
|
+
return { file, adapterImport, endpoint };
|
|
55
|
+
}
|
|
56
|
+
export function assertFetchRouteAvailable(projectRoot, framework, directory) {
|
|
57
|
+
const { file } = getFetchRoute(projectRoot, framework, directory);
|
|
58
|
+
const extensions = framework === "react-router"
|
|
59
|
+
? ["ts", "tsx", "js", "jsx"]
|
|
60
|
+
: framework === "astro"
|
|
61
|
+
? ["ts", "js", "astro"]
|
|
62
|
+
: ["ts", "js"];
|
|
63
|
+
const candidates = extensions.map((extension) => file.replace(/\.ts$/, `.${extension}`));
|
|
64
|
+
if (framework === "nuxt") {
|
|
65
|
+
for (const method of [
|
|
66
|
+
"get",
|
|
67
|
+
"post",
|
|
68
|
+
"delete",
|
|
69
|
+
"put",
|
|
70
|
+
"patch",
|
|
71
|
+
"head",
|
|
72
|
+
"options",
|
|
73
|
+
]) {
|
|
74
|
+
for (const extension of extensions)
|
|
75
|
+
candidates.push(file.replace(/\.ts$/, `.${method}.${extension}`));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
for (const candidate of candidates) {
|
|
79
|
+
if (fs.existsSync(candidate)) {
|
|
80
|
+
throw new Error(`MCP route already exists: ${candidate}. Use --skip-route to keep it, or choose another --route-path.`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export function createFetchRoute(projectRoot, framework, directory) {
|
|
85
|
+
assertFetchRouteAvailable(projectRoot, framework, directory);
|
|
86
|
+
const { file, adapterImport } = getFetchRoute(projectRoot, framework, directory);
|
|
87
|
+
const importHandler = `import { xmcpHandler } from ${JSON.stringify(adapterImport)};`;
|
|
88
|
+
const templates = {
|
|
89
|
+
hono: `import { Hono } from "hono";
|
|
90
|
+
${importHandler}
|
|
91
|
+
|
|
92
|
+
const mcp = new Hono();
|
|
93
|
+
mcp.all("/", (c) => xmcpHandler(c.req.raw));
|
|
94
|
+
|
|
95
|
+
export default mcp;
|
|
96
|
+
`,
|
|
97
|
+
sveltekit: `import type { RequestHandler } from "./$types";
|
|
98
|
+
${importHandler}
|
|
99
|
+
|
|
100
|
+
export const GET: RequestHandler = ({ request }) => xmcpHandler(request);
|
|
101
|
+
export const POST: RequestHandler = ({ request }) => xmcpHandler(request);
|
|
102
|
+
export const DELETE: RequestHandler = ({ request }) => xmcpHandler(request);
|
|
103
|
+
`,
|
|
104
|
+
nuxt: `${importHandler}
|
|
105
|
+
|
|
106
|
+
export default defineEventHandler((event) => xmcpHandler(toWebRequest(event)));
|
|
107
|
+
`,
|
|
108
|
+
"react-router": `import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
|
|
109
|
+
${importHandler}
|
|
110
|
+
|
|
111
|
+
export function loader({ request }: LoaderFunctionArgs) {
|
|
112
|
+
return xmcpHandler(request);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function action({ request }: ActionFunctionArgs) {
|
|
116
|
+
return xmcpHandler(request);
|
|
117
|
+
}
|
|
118
|
+
`,
|
|
119
|
+
astro: `import type { APIRoute } from "astro";
|
|
120
|
+
${importHandler}
|
|
121
|
+
|
|
122
|
+
export const prerender = false;
|
|
123
|
+
export const ALL: APIRoute = ({ request }) => xmcpHandler(request);
|
|
124
|
+
`,
|
|
125
|
+
};
|
|
126
|
+
const content = templates[framework];
|
|
127
|
+
fs.ensureDirSync(path.dirname(file));
|
|
128
|
+
fs.writeFileSync(file, content, { flag: "wx" });
|
|
129
|
+
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export type Framework = "nextjs" | "nestjs" | "express" | "fastify" | "tanstack";
|
|
1
|
+
export type Framework = "nextjs" | "nestjs" | "express" | "fastify" | "tanstack" | "hono" | "sveltekit" | "nuxt" | "react-router" | "astro";
|
|
2
2
|
export declare function detectFramework(projectRoot: string): Framework;
|
|
3
3
|
export declare function detectTypeScript(projectRoot: string): boolean;
|
|
@@ -17,6 +17,18 @@ export function detectFramework(projectRoot) {
|
|
|
17
17
|
packageJson.devDependencies?.["@tanstack/react-start"]) {
|
|
18
18
|
return "tanstack";
|
|
19
19
|
}
|
|
20
|
+
for (const [dependency, framework] of [
|
|
21
|
+
["@sveltejs/kit", "sveltekit"],
|
|
22
|
+
["nuxt", "nuxt"],
|
|
23
|
+
["@react-router/dev", "react-router"],
|
|
24
|
+
["astro", "astro"],
|
|
25
|
+
["hono", "hono"],
|
|
26
|
+
]) {
|
|
27
|
+
if (packageJson.dependencies?.[dependency] ||
|
|
28
|
+
packageJson.devDependencies?.[dependency]) {
|
|
29
|
+
return framework;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
20
32
|
// Check for Fastify
|
|
21
33
|
if (packageJson.dependencies?.fastify ||
|
|
22
34
|
packageJson.devDependencies?.fastify) {
|
|
@@ -20,7 +20,15 @@ const config: XmcpConfig = {
|
|
|
20
20
|
prompts: ${promptsPath ? `"${promptsPath}"` : "false"},
|
|
21
21
|
resources: ${resourcesPath ? `"${resourcesPath}"` : "false"},
|
|
22
22
|
},`;
|
|
23
|
-
if (
|
|
23
|
+
if ([
|
|
24
|
+
"nextjs",
|
|
25
|
+
"tanstack",
|
|
26
|
+
"hono",
|
|
27
|
+
"sveltekit",
|
|
28
|
+
"nuxt",
|
|
29
|
+
"react-router",
|
|
30
|
+
"astro",
|
|
31
|
+
].includes(frameworkAdapter)) {
|
|
24
32
|
configContent += `
|
|
25
33
|
typescript: {
|
|
26
34
|
skipTypeCheck: true,
|
package/dist/helpers/init.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { assertFetchRouteAvailable, createFetchRoute, getFetchRoute, isFetchFramework, isRoutedFetchFramework, } from "./create-fetch-route.js";
|
|
1
2
|
import { generateConfig } from "./generate-config.js";
|
|
2
3
|
import { install } from "./install.js";
|
|
3
4
|
import { updatePackageJson } from "./update-package.js";
|
|
@@ -14,13 +15,21 @@ export async function init(options) {
|
|
|
14
15
|
if (framework === "tanstack" && routePath) {
|
|
15
16
|
assertTanstackRouteAvailable(projectRoot, routePath);
|
|
16
17
|
}
|
|
18
|
+
const hasFetchRoute = isRoutedFetchFramework(framework);
|
|
19
|
+
if (hasFetchRoute && routePath) {
|
|
20
|
+
assertFetchRouteAvailable(projectRoot, framework, routePath);
|
|
21
|
+
}
|
|
17
22
|
const endpoint = framework === "tanstack" && routePath
|
|
18
23
|
? getTanstackRoute(projectRoot, routePath).endpoint
|
|
19
|
-
:
|
|
24
|
+
: hasFetchRoute && routePath
|
|
25
|
+
? getFetchRoute(projectRoot, framework, routePath).endpoint
|
|
26
|
+
: undefined;
|
|
20
27
|
generateConfig(projectRoot, framework, toolsPath, promptsPath, resourcesPath, endpoint);
|
|
21
28
|
await install(projectRoot, packageManager, version);
|
|
22
29
|
updatePackageJson(projectRoot, { framework, cloudflare });
|
|
23
|
-
|
|
30
|
+
// Fetch routes use relative imports. Preserve host-generated paths and includes.
|
|
31
|
+
if (!isFetchFramework(framework))
|
|
32
|
+
updateTsConfig(projectRoot);
|
|
24
33
|
updateGitignore(projectRoot);
|
|
25
34
|
if (toolsPath) {
|
|
26
35
|
createTool(projectRoot, toolsPath);
|
|
@@ -37,6 +46,9 @@ export async function init(options) {
|
|
|
37
46
|
if (framework === "tanstack" && routePath) {
|
|
38
47
|
createTanstackRoute(projectRoot, routePath);
|
|
39
48
|
}
|
|
49
|
+
if (hasFetchRoute && routePath) {
|
|
50
|
+
createFetchRoute(projectRoot, framework, routePath);
|
|
51
|
+
}
|
|
40
52
|
if (framework === "nestjs") {
|
|
41
53
|
createNestJsModule(projectRoot);
|
|
42
54
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isFetchFramework } from "./create-fetch-route.js";
|
|
1
2
|
import path from "path";
|
|
2
3
|
import fs from "fs-extra";
|
|
3
4
|
/**
|
|
@@ -13,8 +14,18 @@ export function updatePackageJson(projectPath, options = {}) {
|
|
|
13
14
|
}
|
|
14
15
|
packageJson.devDependencies = packageJson.devDependencies ?? {};
|
|
15
16
|
packageJson.devDependencies["@xmcp-dev/compiler"] ??= "latest";
|
|
16
|
-
const
|
|
17
|
-
const
|
|
17
|
+
const fetchFramework = isFetchFramework(options.framework);
|
|
18
|
+
const hostCommands = {
|
|
19
|
+
tanstack: "vite",
|
|
20
|
+
sveltekit: "vite",
|
|
21
|
+
nuxt: "nuxt",
|
|
22
|
+
"react-router": "react-router",
|
|
23
|
+
astro: "astro",
|
|
24
|
+
};
|
|
25
|
+
const host = options.framework && options.framework in hostCommands
|
|
26
|
+
? hostCommands[options.framework]
|
|
27
|
+
: undefined;
|
|
28
|
+
const platformFlag = fetchFramework && options.cloudflare ? " --cf" : "";
|
|
18
29
|
const build = `xmcp build${platformFlag}`;
|
|
19
30
|
const dev = `xmcp dev${platformFlag}`;
|
|
20
31
|
// prepend commands to existing scripts - prevent overwriting
|
|
@@ -22,16 +33,18 @@ export function updatePackageJson(projectPath, options = {}) {
|
|
|
22
33
|
packageJson.scripts.build = `${build} && ${packageJson.scripts.build}`;
|
|
23
34
|
}
|
|
24
35
|
else {
|
|
25
|
-
packageJson.scripts.build =
|
|
36
|
+
packageJson.scripts.build = host ? `${build} && ${host} build` : build;
|
|
26
37
|
}
|
|
27
38
|
if (packageJson.scripts.dev) {
|
|
28
|
-
packageJson.scripts.dev =
|
|
39
|
+
packageJson.scripts.dev = fetchFramework
|
|
29
40
|
? `${build} && (${dev} & ${packageJson.scripts.dev})`
|
|
30
41
|
: `${dev} & ${packageJson.scripts.dev}`;
|
|
31
42
|
}
|
|
32
43
|
else {
|
|
33
|
-
packageJson.scripts.dev =
|
|
34
|
-
?
|
|
44
|
+
packageJson.scripts.dev = fetchFramework
|
|
45
|
+
? host
|
|
46
|
+
? `${build} && (${dev} & ${host} dev)`
|
|
47
|
+
: `${build} && ${dev}`
|
|
35
48
|
: dev;
|
|
36
49
|
}
|
|
37
50
|
fs.writeJsonSync(packageJsonPath, packageJson, { spaces: 2 });
|