init-xmcp 0.6.0 → 0.6.1-canary.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/dist/__tests__/generate-config.test.js +39 -9
- package/dist/helpers/create-nestjs-module.d.ts +6 -0
- package/dist/helpers/create-nestjs-module.js +118 -0
- package/dist/helpers/detect-framework.d.ts +1 -1
- package/dist/helpers/detect-framework.js +9 -4
- package/dist/helpers/generate-config.js +5 -17
- package/dist/helpers/init.js +4 -0
- package/dist/index.js +26 -2
- package/package.json +1 -1
- package/src/__tests__/generate-config.test.ts +68 -15
- package/src/helpers/create-nestjs-module.ts +129 -0
- package/src/helpers/detect-framework.ts +11 -5
- package/src/helpers/generate-config.ts +5 -20
- package/src/helpers/init.ts +5 -0
- package/src/index.ts +32 -2
package/.turbo/turbo-build.log
CHANGED
|
@@ -14,45 +14,75 @@ describe("generateConfig", () => {
|
|
|
14
14
|
afterEach(() => {
|
|
15
15
|
fs.removeSync(tempDir);
|
|
16
16
|
});
|
|
17
|
-
it("should generate config
|
|
17
|
+
it("should generate config with all paths set to false when all paths are skipped", () => {
|
|
18
18
|
generateConfig(projectPath, "nextjs");
|
|
19
19
|
const configPath = path.join(projectPath, "xmcp.config.ts");
|
|
20
20
|
assert(fs.existsSync(configPath), "Config file should be created");
|
|
21
21
|
const configContent = fs.readFileSync(configPath, "utf-8");
|
|
22
|
-
assert(
|
|
22
|
+
assert(configContent.includes("paths: {"), "Should include paths object");
|
|
23
|
+
assert(configContent.includes("tools: false"), "Should include tools: false");
|
|
24
|
+
assert(configContent.includes("prompts: false"), "Should include prompts: false");
|
|
25
|
+
assert(configContent.includes("resources: false"), "Should include resources: false");
|
|
23
26
|
const pathsMatches = configContent.match(/paths:/g);
|
|
24
|
-
assert(
|
|
27
|
+
assert(pathsMatches && pathsMatches.length === 1, "Should contain exactly one paths object");
|
|
25
28
|
});
|
|
26
|
-
it("should generate config with
|
|
29
|
+
it("should generate config with tools path set and prompts/resources as false", () => {
|
|
27
30
|
generateConfig(projectPath, "nextjs", "./tools");
|
|
28
31
|
const configPath = path.join(projectPath, "xmcp.config.ts");
|
|
29
32
|
assert(fs.existsSync(configPath), "Config file should be created");
|
|
30
33
|
const configContent = fs.readFileSync(configPath, "utf-8");
|
|
31
34
|
assert(configContent.includes("paths: {"), "Should include paths object");
|
|
32
35
|
assert(configContent.includes('tools: "./tools"'), "Should include tools path");
|
|
33
|
-
assert(
|
|
36
|
+
assert(configContent.includes("prompts: false"), "Should include prompts: false");
|
|
37
|
+
assert(configContent.includes("resources: false"), "Should include resources: false");
|
|
34
38
|
const pathsMatches = configContent.match(/paths:/g);
|
|
35
39
|
assert(pathsMatches && pathsMatches.length === 1, "Should contain exactly one paths object");
|
|
36
40
|
});
|
|
37
|
-
it("should generate config with
|
|
41
|
+
it("should generate config with prompts path set and tools/resources as false", () => {
|
|
38
42
|
generateConfig(projectPath, "nextjs", undefined, "./prompts");
|
|
39
43
|
const configPath = path.join(projectPath, "xmcp.config.ts");
|
|
40
44
|
assert(fs.existsSync(configPath), "Config file should be created");
|
|
41
45
|
const configContent = fs.readFileSync(configPath, "utf-8");
|
|
42
46
|
assert(configContent.includes("paths: {"), "Should include paths object");
|
|
43
47
|
assert(configContent.includes('prompts: "./prompts"'), "Should include prompts path");
|
|
44
|
-
assert(
|
|
48
|
+
assert(configContent.includes("tools: false"), "Should include tools: false");
|
|
49
|
+
assert(configContent.includes("resources: false"), "Should include resources: false");
|
|
45
50
|
const pathsMatches = configContent.match(/paths:/g);
|
|
46
51
|
assert(pathsMatches && pathsMatches.length === 1, "Should contain exactly one paths object");
|
|
47
52
|
});
|
|
48
|
-
it("should generate config with paths
|
|
49
|
-
generateConfig(projectPath, "nextjs", "./tools", "./prompts");
|
|
53
|
+
it("should generate config with all paths set to their string values when all are provided", () => {
|
|
54
|
+
generateConfig(projectPath, "nextjs", "./tools", "./prompts", "./resources");
|
|
50
55
|
const configPath = path.join(projectPath, "xmcp.config.ts");
|
|
51
56
|
assert(fs.existsSync(configPath), "Config file should be created");
|
|
52
57
|
const configContent = fs.readFileSync(configPath, "utf-8");
|
|
53
58
|
assert(configContent.includes("paths: {"), "Should include paths object");
|
|
54
59
|
assert(configContent.includes('tools: "./tools"'), "Should include tools path");
|
|
55
60
|
assert(configContent.includes('prompts: "./prompts"'), "Should include prompts path");
|
|
61
|
+
assert(configContent.includes('resources: "./resources"'), "Should include resources path");
|
|
62
|
+
const pathsMatches = configContent.match(/paths:/g);
|
|
63
|
+
assert(pathsMatches && pathsMatches.length === 1, "Should contain exactly one paths object");
|
|
64
|
+
});
|
|
65
|
+
it("should generate config with only resources path set and tools/prompts as false", () => {
|
|
66
|
+
generateConfig(projectPath, "nextjs", undefined, undefined, "./resources");
|
|
67
|
+
const configPath = path.join(projectPath, "xmcp.config.ts");
|
|
68
|
+
assert(fs.existsSync(configPath), "Config file should be created");
|
|
69
|
+
const configContent = fs.readFileSync(configPath, "utf-8");
|
|
70
|
+
assert(configContent.includes("paths: {"), "Should include paths object");
|
|
71
|
+
assert(configContent.includes("tools: false"), "Should include tools: false");
|
|
72
|
+
assert(configContent.includes("prompts: false"), "Should include prompts: false");
|
|
73
|
+
assert(configContent.includes('resources: "./resources"'), "Should include resources path");
|
|
74
|
+
const pathsMatches = configContent.match(/paths:/g);
|
|
75
|
+
assert(pathsMatches && pathsMatches.length === 1, "Should contain exactly one paths object");
|
|
76
|
+
});
|
|
77
|
+
it("should generate config with mixed paths (tools and resources set, prompts false)", () => {
|
|
78
|
+
generateConfig(projectPath, "nextjs", "./tools", undefined, "./resources");
|
|
79
|
+
const configPath = path.join(projectPath, "xmcp.config.ts");
|
|
80
|
+
assert(fs.existsSync(configPath), "Config file should be created");
|
|
81
|
+
const configContent = fs.readFileSync(configPath, "utf-8");
|
|
82
|
+
assert(configContent.includes("paths: {"), "Should include paths object");
|
|
83
|
+
assert(configContent.includes('tools: "./tools"'), "Should include tools path");
|
|
84
|
+
assert(configContent.includes("prompts: false"), "Should include prompts: false");
|
|
85
|
+
assert(configContent.includes('resources: "./resources"'), "Should include resources path");
|
|
56
86
|
const pathsMatches = configContent.match(/paths:/g);
|
|
57
87
|
assert(pathsMatches && pathsMatches.length === 1, "Should contain exactly one paths object");
|
|
58
88
|
});
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import chalk from "chalk";
|
|
4
|
+
/**
|
|
5
|
+
* Create xmcp module folder with controller, module, and filter
|
|
6
|
+
* for NestJS projects
|
|
7
|
+
* @param projectRoot - Project root directory
|
|
8
|
+
*/
|
|
9
|
+
export function createNestJsModule(projectRoot) {
|
|
10
|
+
// Check if src folder exists - NestJS projects typically have src/
|
|
11
|
+
const hasSrcFolder = fs.existsSync(path.join(projectRoot, "src"));
|
|
12
|
+
const xmcpPath = hasSrcFolder ? "src/xmcp" : "xmcp";
|
|
13
|
+
const xmcpDirPath = path.join(projectRoot, xmcpPath);
|
|
14
|
+
try {
|
|
15
|
+
// Create xmcp folder
|
|
16
|
+
fs.ensureDirSync(xmcpDirPath);
|
|
17
|
+
// Create xmcp.filter.ts
|
|
18
|
+
const filterFilePath = path.join(xmcpDirPath, "xmcp.filter.ts");
|
|
19
|
+
fs.writeFileSync(filterFilePath, filterTemplate);
|
|
20
|
+
console.log(chalk.green(`Created filter: ${xmcpPath}/xmcp.filter.ts`));
|
|
21
|
+
// Create xmcp.controller.ts
|
|
22
|
+
const controllerFilePath = path.join(xmcpDirPath, "xmcp.controller.ts");
|
|
23
|
+
fs.writeFileSync(controllerFilePath, controllerTemplate);
|
|
24
|
+
console.log(chalk.green(`Created controller: ${xmcpPath}/xmcp.controller.ts`));
|
|
25
|
+
// Create xmcp.module.ts
|
|
26
|
+
const moduleFilePath = path.join(xmcpDirPath, "xmcp.module.ts");
|
|
27
|
+
fs.writeFileSync(moduleFilePath, moduleTemplate);
|
|
28
|
+
console.log(chalk.green(`Created module: ${xmcpPath}/xmcp.module.ts`));
|
|
29
|
+
// Create xmcp.auth.ts
|
|
30
|
+
const authFilePath = path.join(xmcpDirPath, "xmcp.auth.ts");
|
|
31
|
+
fs.writeFileSync(authFilePath, authTemplate);
|
|
32
|
+
console.log(chalk.green(`Created auth guard: ${xmcpPath}/xmcp.auth.ts`));
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
console.error(chalk.red(`Failed to create NestJS module: ${error}`));
|
|
36
|
+
process.exit(1);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const filterTemplate = `import { ExceptionFilter, Catch, ArgumentsHost, Logger } from "@nestjs/common";
|
|
40
|
+
import { Response } from "express";
|
|
41
|
+
|
|
42
|
+
@Catch()
|
|
43
|
+
export class McpExceptionFilter implements ExceptionFilter {
|
|
44
|
+
private readonly logger = new Logger(McpExceptionFilter.name);
|
|
45
|
+
|
|
46
|
+
catch(exception: unknown, host: ArgumentsHost) {
|
|
47
|
+
const ctx = host.switchToHttp();
|
|
48
|
+
const response = ctx.getResponse<Response>();
|
|
49
|
+
|
|
50
|
+
this.logger.error(
|
|
51
|
+
"MCP request failed",
|
|
52
|
+
exception instanceof Error ? exception.stack : String(exception)
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
if (!response.headersSent) {
|
|
56
|
+
response.status(500).json({
|
|
57
|
+
jsonrpc: "2.0",
|
|
58
|
+
error: {
|
|
59
|
+
code: -32603,
|
|
60
|
+
message: "Internal server error",
|
|
61
|
+
},
|
|
62
|
+
id: null,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
`;
|
|
68
|
+
const controllerTemplate = `import { Controller, UseFilters } from "@nestjs/common";
|
|
69
|
+
import { XmcpController } from "@xmcp/adapter";
|
|
70
|
+
import { McpExceptionFilter } from "./xmcp.filter";
|
|
71
|
+
|
|
72
|
+
@Controller("mcp")
|
|
73
|
+
@UseFilters(McpExceptionFilter)
|
|
74
|
+
export class McpController extends XmcpController {}
|
|
75
|
+
`;
|
|
76
|
+
const moduleTemplate = `import { Module } from "@nestjs/common";
|
|
77
|
+
import { XmcpService, OAuthModule } from "@xmcp/adapter";
|
|
78
|
+
import { McpController } from "./xmcp.controller";
|
|
79
|
+
import { McpExceptionFilter } from "./xmcp.filter";
|
|
80
|
+
import { McpAuthGuard } from "./xmcp.auth";
|
|
81
|
+
|
|
82
|
+
const config = {
|
|
83
|
+
authorizationServers: [process.env.OAUTH_ISSUER!],
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
@Module({
|
|
87
|
+
imports: [OAuthModule.forRoot(config)],
|
|
88
|
+
controllers: [McpController],
|
|
89
|
+
providers: [XmcpService, McpExceptionFilter, McpAuthGuard],
|
|
90
|
+
exports: [XmcpService],
|
|
91
|
+
})
|
|
92
|
+
export class XmcpModule {}
|
|
93
|
+
`;
|
|
94
|
+
const authTemplate = `import { createMcpAuthGuard } from "@xmcp/adapter";
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* MCP Auth Guard configuration.
|
|
98
|
+
*
|
|
99
|
+
* To enable authentication:
|
|
100
|
+
* 1. Add McpAuthGuard to providers in xmcp.module.ts
|
|
101
|
+
* 2. Add @UseGuards(McpAuthGuard) to xmcp.controller.ts
|
|
102
|
+
*/
|
|
103
|
+
export const McpAuthGuard = createMcpAuthGuard({
|
|
104
|
+
verifyToken: async (token) => {
|
|
105
|
+
// TODO: Implement your token verification logic
|
|
106
|
+
// Example with JWT:
|
|
107
|
+
// const decoded = jwt.verify(token, process.env.JWT_SECRET!) as jwt.JwtPayload;
|
|
108
|
+
// return {
|
|
109
|
+
// clientId: decoded.sub || "unknown",
|
|
110
|
+
// scopes: decoded.scope?.split(" ") || [],
|
|
111
|
+
// expiresAt: decoded.exp,
|
|
112
|
+
// };
|
|
113
|
+
|
|
114
|
+
throw new Error("Token verification not implemented");
|
|
115
|
+
},
|
|
116
|
+
required: false, // Set to true to require authentication
|
|
117
|
+
});
|
|
118
|
+
`;
|
|
@@ -3,12 +3,17 @@ import fs from "fs-extra";
|
|
|
3
3
|
import path from "path";
|
|
4
4
|
import { readTsConfigFile } from "../utils/read-config-file.js";
|
|
5
5
|
export function detectFramework(projectRoot) {
|
|
6
|
-
// check if project has been initialized with nextjs or default to express
|
|
7
6
|
const packageJson = JSON.parse(fs.readFileSync(path.join(projectRoot, "package.json"), "utf-8"));
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
// Check for NestJS
|
|
8
|
+
if (packageJson.dependencies?.["@nestjs/core"] || packageJson.devDependencies?.["@nestjs/core"]) {
|
|
9
|
+
return "nestjs";
|
|
10
10
|
}
|
|
11
|
-
|
|
11
|
+
// Check for Next.js
|
|
12
|
+
if (packageJson.dependencies?.next || packageJson.devDependencies?.next) {
|
|
13
|
+
return "nextjs";
|
|
14
|
+
}
|
|
15
|
+
// Default to express
|
|
16
|
+
return "express";
|
|
12
17
|
}
|
|
13
18
|
export function detectTypeScript(projectRoot) {
|
|
14
19
|
// check if project has been initialized with typescript or default to javascript
|
|
@@ -14,24 +14,12 @@ const config: XmcpConfig = {
|
|
|
14
14
|
experimental: {
|
|
15
15
|
adapter: "${frameworkAdapter}",
|
|
16
16
|
},`;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
tools: "${toolsPath}",`;
|
|
23
|
-
}
|
|
24
|
-
if (promptsPath) {
|
|
25
|
-
configContent += `
|
|
26
|
-
prompts: "${promptsPath}",`;
|
|
27
|
-
}
|
|
28
|
-
if (resourcesPath) {
|
|
29
|
-
configContent += `
|
|
30
|
-
resources: "${resourcesPath}",`;
|
|
31
|
-
}
|
|
32
|
-
configContent += `
|
|
17
|
+
configContent += `
|
|
18
|
+
paths: {
|
|
19
|
+
tools: ${toolsPath ? `"${toolsPath}"` : "false"},
|
|
20
|
+
prompts: ${promptsPath ? `"${promptsPath}"` : "false"},
|
|
21
|
+
resources: ${resourcesPath ? `"${resourcesPath}"` : "false"},
|
|
33
22
|
},`;
|
|
34
|
-
}
|
|
35
23
|
if (frameworkAdapter === "nextjs") {
|
|
36
24
|
configContent += `
|
|
37
25
|
typescript: {
|
package/dist/helpers/init.js
CHANGED
|
@@ -7,6 +7,7 @@ import { createTool } from "./create-tool.js";
|
|
|
7
7
|
import { createRoute } from "./create-handler.js";
|
|
8
8
|
import { createPrompt } from "./create-prompt.js";
|
|
9
9
|
import { createResources } from "./create-resources.js";
|
|
10
|
+
import { createNestJsModule } from "./create-nestjs-module.js";
|
|
10
11
|
export async function init(options) {
|
|
11
12
|
const { projectRoot, framework, toolsPath, promptsPath, resourcesPath, routePath, packageManager, version, } = options;
|
|
12
13
|
generateConfig(projectRoot, framework, toolsPath, promptsPath, resourcesPath);
|
|
@@ -26,4 +27,7 @@ export async function init(options) {
|
|
|
26
27
|
if (framework === "nextjs" && routePath) {
|
|
27
28
|
createRoute(projectRoot, routePath);
|
|
28
29
|
}
|
|
30
|
+
if (framework === "nestjs") {
|
|
31
|
+
createNestJsModule(projectRoot);
|
|
32
|
+
}
|
|
29
33
|
}
|
package/dist/index.js
CHANGED
|
@@ -278,12 +278,18 @@ const program = new Command()
|
|
|
278
278
|
if (routePath) {
|
|
279
279
|
console.log(` • ${routePath}/route.ts`);
|
|
280
280
|
}
|
|
281
|
+
if (detectedFramework === "nestjs") {
|
|
282
|
+
const hasSrcFolder = fs.existsSync(path.join(projectRoot, "src"));
|
|
283
|
+
const xmcpPath = hasSrcFolder ? "src/xmcp" : "xmcp";
|
|
284
|
+
console.log(` • ${xmcpPath}/xmcp.controller.ts`);
|
|
285
|
+
console.log(` • ${xmcpPath}/xmcp.module.ts`);
|
|
286
|
+
}
|
|
281
287
|
console.log(chalk.blue("\n❯ Files updated:"));
|
|
282
288
|
console.log(` • package.json`);
|
|
283
289
|
console.log(` • tsconfig.json`);
|
|
284
290
|
console.log(chalk.blue("\nNext steps:"));
|
|
285
|
-
// code integration for express projects
|
|
286
|
-
if (detectedFramework !== "nextjs") {
|
|
291
|
+
// code integration for express projects
|
|
292
|
+
if (detectedFramework !== "nextjs" && detectedFramework !== "nestjs") {
|
|
287
293
|
const integrationCode = `import { xmcpHandler } from '@xmcp/adapter';
|
|
288
294
|
|
|
289
295
|
myApp.post("/mcp", xmcpHandler);
|
|
@@ -298,6 +304,24 @@ myApp.get("/mcp", xmcpHandler);`;
|
|
|
298
304
|
});
|
|
299
305
|
console.log(chalk.green(chalk.bgBlack(` ${" ".repeat(maxLength)}`)));
|
|
300
306
|
}
|
|
307
|
+
// code integration for NestJS projects
|
|
308
|
+
if (detectedFramework === "nestjs") {
|
|
309
|
+
const integrationCode = `import { XmcpModule } from "./xmcp/xmcp.module";
|
|
310
|
+
|
|
311
|
+
@Module({
|
|
312
|
+
imports: [XmcpModule],
|
|
313
|
+
})
|
|
314
|
+
export class AppModule {}`;
|
|
315
|
+
const lines = integrationCode.split("\n");
|
|
316
|
+
const maxLength = Math.max(...lines.map((line) => line.length)) + 2;
|
|
317
|
+
console.log("\nTo get started with xmcp in your NestJS application, add this to your app.module.ts:\n");
|
|
318
|
+
console.log(chalk.green(chalk.bgBlack(` ${" ".repeat(maxLength)}`)));
|
|
319
|
+
lines.forEach((line) => {
|
|
320
|
+
const padding = " ".repeat(maxLength - line.length);
|
|
321
|
+
console.log(chalk.green(chalk.bgBlack(` ${line}${padding}`)));
|
|
322
|
+
});
|
|
323
|
+
console.log(chalk.green(chalk.bgBlack(` ${" ".repeat(maxLength)}`)));
|
|
324
|
+
}
|
|
301
325
|
console.log(` \nRun "${packageManager} run dev" to start the development server\n`);
|
|
302
326
|
}
|
|
303
327
|
catch (error) {
|
package/package.json
CHANGED
|
@@ -18,23 +18,26 @@ describe("generateConfig", () => {
|
|
|
18
18
|
fs.removeSync(tempDir);
|
|
19
19
|
});
|
|
20
20
|
|
|
21
|
-
it("should generate config
|
|
21
|
+
it("should generate config with all paths set to false when all paths are skipped", () => {
|
|
22
22
|
generateConfig(projectPath, "nextjs");
|
|
23
23
|
|
|
24
24
|
const configPath = path.join(projectPath, "xmcp.config.ts");
|
|
25
25
|
assert(fs.existsSync(configPath), "Config file should be created");
|
|
26
26
|
|
|
27
27
|
const configContent = fs.readFileSync(configPath, "utf-8");
|
|
28
|
-
assert(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
);
|
|
28
|
+
assert(configContent.includes("paths: {"), "Should include paths object");
|
|
29
|
+
assert(configContent.includes("tools: false"), "Should include tools: false");
|
|
30
|
+
assert(configContent.includes("prompts: false"), "Should include prompts: false");
|
|
31
|
+
assert(configContent.includes("resources: false"), "Should include resources: false");
|
|
32
32
|
|
|
33
33
|
const pathsMatches = configContent.match(/paths:/g);
|
|
34
|
-
assert(
|
|
34
|
+
assert(
|
|
35
|
+
pathsMatches && pathsMatches.length === 1,
|
|
36
|
+
"Should contain exactly one paths object"
|
|
37
|
+
);
|
|
35
38
|
});
|
|
36
39
|
|
|
37
|
-
it("should generate config with
|
|
40
|
+
it("should generate config with tools path set and prompts/resources as false", () => {
|
|
38
41
|
generateConfig(projectPath, "nextjs", "./tools");
|
|
39
42
|
|
|
40
43
|
const configPath = path.join(projectPath, "xmcp.config.ts");
|
|
@@ -46,10 +49,8 @@ describe("generateConfig", () => {
|
|
|
46
49
|
configContent.includes('tools: "./tools"'),
|
|
47
50
|
"Should include tools path"
|
|
48
51
|
);
|
|
49
|
-
assert(
|
|
50
|
-
|
|
51
|
-
"Should not include prompts path"
|
|
52
|
-
);
|
|
52
|
+
assert(configContent.includes("prompts: false"), "Should include prompts: false");
|
|
53
|
+
assert(configContent.includes("resources: false"), "Should include resources: false");
|
|
53
54
|
|
|
54
55
|
const pathsMatches = configContent.match(/paths:/g);
|
|
55
56
|
assert(
|
|
@@ -58,7 +59,7 @@ describe("generateConfig", () => {
|
|
|
58
59
|
);
|
|
59
60
|
});
|
|
60
61
|
|
|
61
|
-
it("should generate config with
|
|
62
|
+
it("should generate config with prompts path set and tools/resources as false", () => {
|
|
62
63
|
generateConfig(projectPath, "nextjs", undefined, "./prompts");
|
|
63
64
|
|
|
64
65
|
const configPath = path.join(projectPath, "xmcp.config.ts");
|
|
@@ -70,7 +71,8 @@ describe("generateConfig", () => {
|
|
|
70
71
|
configContent.includes('prompts: "./prompts"'),
|
|
71
72
|
"Should include prompts path"
|
|
72
73
|
);
|
|
73
|
-
assert(
|
|
74
|
+
assert(configContent.includes("tools: false"), "Should include tools: false");
|
|
75
|
+
assert(configContent.includes("resources: false"), "Should include resources: false");
|
|
74
76
|
|
|
75
77
|
const pathsMatches = configContent.match(/paths:/g);
|
|
76
78
|
assert(
|
|
@@ -79,8 +81,8 @@ describe("generateConfig", () => {
|
|
|
79
81
|
);
|
|
80
82
|
});
|
|
81
83
|
|
|
82
|
-
it("should generate config with paths
|
|
83
|
-
generateConfig(projectPath, "nextjs", "./tools", "./prompts");
|
|
84
|
+
it("should generate config with all paths set to their string values when all are provided", () => {
|
|
85
|
+
generateConfig(projectPath, "nextjs", "./tools", "./prompts", "./resources");
|
|
84
86
|
|
|
85
87
|
const configPath = path.join(projectPath, "xmcp.config.ts");
|
|
86
88
|
assert(fs.existsSync(configPath), "Config file should be created");
|
|
@@ -95,6 +97,57 @@ describe("generateConfig", () => {
|
|
|
95
97
|
configContent.includes('prompts: "./prompts"'),
|
|
96
98
|
"Should include prompts path"
|
|
97
99
|
);
|
|
100
|
+
assert(
|
|
101
|
+
configContent.includes('resources: "./resources"'),
|
|
102
|
+
"Should include resources path"
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
const pathsMatches = configContent.match(/paths:/g);
|
|
106
|
+
assert(
|
|
107
|
+
pathsMatches && pathsMatches.length === 1,
|
|
108
|
+
"Should contain exactly one paths object"
|
|
109
|
+
);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("should generate config with only resources path set and tools/prompts as false", () => {
|
|
113
|
+
generateConfig(projectPath, "nextjs", undefined, undefined, "./resources");
|
|
114
|
+
|
|
115
|
+
const configPath = path.join(projectPath, "xmcp.config.ts");
|
|
116
|
+
assert(fs.existsSync(configPath), "Config file should be created");
|
|
117
|
+
|
|
118
|
+
const configContent = fs.readFileSync(configPath, "utf-8");
|
|
119
|
+
assert(configContent.includes("paths: {"), "Should include paths object");
|
|
120
|
+
assert(configContent.includes("tools: false"), "Should include tools: false");
|
|
121
|
+
assert(configContent.includes("prompts: false"), "Should include prompts: false");
|
|
122
|
+
assert(
|
|
123
|
+
configContent.includes('resources: "./resources"'),
|
|
124
|
+
"Should include resources path"
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const pathsMatches = configContent.match(/paths:/g);
|
|
128
|
+
assert(
|
|
129
|
+
pathsMatches && pathsMatches.length === 1,
|
|
130
|
+
"Should contain exactly one paths object"
|
|
131
|
+
);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("should generate config with mixed paths (tools and resources set, prompts false)", () => {
|
|
135
|
+
generateConfig(projectPath, "nextjs", "./tools", undefined, "./resources");
|
|
136
|
+
|
|
137
|
+
const configPath = path.join(projectPath, "xmcp.config.ts");
|
|
138
|
+
assert(fs.existsSync(configPath), "Config file should be created");
|
|
139
|
+
|
|
140
|
+
const configContent = fs.readFileSync(configPath, "utf-8");
|
|
141
|
+
assert(configContent.includes("paths: {"), "Should include paths object");
|
|
142
|
+
assert(
|
|
143
|
+
configContent.includes('tools: "./tools"'),
|
|
144
|
+
"Should include tools path"
|
|
145
|
+
);
|
|
146
|
+
assert(configContent.includes("prompts: false"), "Should include prompts: false");
|
|
147
|
+
assert(
|
|
148
|
+
configContent.includes('resources: "./resources"'),
|
|
149
|
+
"Should include resources path"
|
|
150
|
+
);
|
|
98
151
|
|
|
99
152
|
const pathsMatches = configContent.match(/paths:/g);
|
|
100
153
|
assert(
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import chalk from "chalk";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Create xmcp module folder with controller, module, and filter
|
|
7
|
+
* for NestJS projects
|
|
8
|
+
* @param projectRoot - Project root directory
|
|
9
|
+
*/
|
|
10
|
+
export function createNestJsModule(projectRoot: string): void {
|
|
11
|
+
// Check if src folder exists - NestJS projects typically have src/
|
|
12
|
+
const hasSrcFolder = fs.existsSync(path.join(projectRoot, "src"));
|
|
13
|
+
const xmcpPath = hasSrcFolder ? "src/xmcp" : "xmcp";
|
|
14
|
+
const xmcpDirPath = path.join(projectRoot, xmcpPath);
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
// Create xmcp folder
|
|
18
|
+
fs.ensureDirSync(xmcpDirPath);
|
|
19
|
+
|
|
20
|
+
// Create xmcp.filter.ts
|
|
21
|
+
const filterFilePath = path.join(xmcpDirPath, "xmcp.filter.ts");
|
|
22
|
+
fs.writeFileSync(filterFilePath, filterTemplate);
|
|
23
|
+
console.log(chalk.green(`Created filter: ${xmcpPath}/xmcp.filter.ts`));
|
|
24
|
+
|
|
25
|
+
// Create xmcp.controller.ts
|
|
26
|
+
const controllerFilePath = path.join(xmcpDirPath, "xmcp.controller.ts");
|
|
27
|
+
fs.writeFileSync(controllerFilePath, controllerTemplate);
|
|
28
|
+
console.log(
|
|
29
|
+
chalk.green(`Created controller: ${xmcpPath}/xmcp.controller.ts`)
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
// Create xmcp.module.ts
|
|
33
|
+
const moduleFilePath = path.join(xmcpDirPath, "xmcp.module.ts");
|
|
34
|
+
fs.writeFileSync(moduleFilePath, moduleTemplate);
|
|
35
|
+
console.log(chalk.green(`Created module: ${xmcpPath}/xmcp.module.ts`));
|
|
36
|
+
|
|
37
|
+
// Create xmcp.auth.ts
|
|
38
|
+
const authFilePath = path.join(xmcpDirPath, "xmcp.auth.ts");
|
|
39
|
+
fs.writeFileSync(authFilePath, authTemplate);
|
|
40
|
+
console.log(chalk.green(`Created auth guard: ${xmcpPath}/xmcp.auth.ts`));
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.error(chalk.red(`Failed to create NestJS module: ${error}`));
|
|
43
|
+
process.exit(1);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const filterTemplate = `import { ExceptionFilter, Catch, ArgumentsHost, Logger } from "@nestjs/common";
|
|
48
|
+
import { Response } from "express";
|
|
49
|
+
|
|
50
|
+
@Catch()
|
|
51
|
+
export class McpExceptionFilter implements ExceptionFilter {
|
|
52
|
+
private readonly logger = new Logger(McpExceptionFilter.name);
|
|
53
|
+
|
|
54
|
+
catch(exception: unknown, host: ArgumentsHost) {
|
|
55
|
+
const ctx = host.switchToHttp();
|
|
56
|
+
const response = ctx.getResponse<Response>();
|
|
57
|
+
|
|
58
|
+
this.logger.error(
|
|
59
|
+
"MCP request failed",
|
|
60
|
+
exception instanceof Error ? exception.stack : String(exception)
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
if (!response.headersSent) {
|
|
64
|
+
response.status(500).json({
|
|
65
|
+
jsonrpc: "2.0",
|
|
66
|
+
error: {
|
|
67
|
+
code: -32603,
|
|
68
|
+
message: "Internal server error",
|
|
69
|
+
},
|
|
70
|
+
id: null,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
`;
|
|
76
|
+
|
|
77
|
+
const controllerTemplate = `import { Controller, UseFilters } from "@nestjs/common";
|
|
78
|
+
import { XmcpController } from "@xmcp/adapter";
|
|
79
|
+
import { McpExceptionFilter } from "./xmcp.filter";
|
|
80
|
+
|
|
81
|
+
@Controller("mcp")
|
|
82
|
+
@UseFilters(McpExceptionFilter)
|
|
83
|
+
export class McpController extends XmcpController {}
|
|
84
|
+
`;
|
|
85
|
+
|
|
86
|
+
const moduleTemplate = `import { Module } from "@nestjs/common";
|
|
87
|
+
import { XmcpService, OAuthModule } from "@xmcp/adapter";
|
|
88
|
+
import { McpController } from "./xmcp.controller";
|
|
89
|
+
import { McpExceptionFilter } from "./xmcp.filter";
|
|
90
|
+
import { McpAuthGuard } from "./xmcp.auth";
|
|
91
|
+
|
|
92
|
+
const config = {
|
|
93
|
+
authorizationServers: [process.env.OAUTH_ISSUER!],
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
@Module({
|
|
97
|
+
imports: [OAuthModule.forRoot(config)],
|
|
98
|
+
controllers: [McpController],
|
|
99
|
+
providers: [XmcpService, McpExceptionFilter, McpAuthGuard],
|
|
100
|
+
exports: [XmcpService],
|
|
101
|
+
})
|
|
102
|
+
export class XmcpModule {}
|
|
103
|
+
`;
|
|
104
|
+
|
|
105
|
+
const authTemplate = `import { createMcpAuthGuard } from "@xmcp/adapter";
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* MCP Auth Guard configuration.
|
|
109
|
+
*
|
|
110
|
+
* To enable authentication:
|
|
111
|
+
* 1. Add McpAuthGuard to providers in xmcp.module.ts
|
|
112
|
+
* 2. Add @UseGuards(McpAuthGuard) to xmcp.controller.ts
|
|
113
|
+
*/
|
|
114
|
+
export const McpAuthGuard = createMcpAuthGuard({
|
|
115
|
+
verifyToken: async (token) => {
|
|
116
|
+
// TODO: Implement your token verification logic
|
|
117
|
+
// Example with JWT:
|
|
118
|
+
// const decoded = jwt.verify(token, process.env.JWT_SECRET!) as jwt.JwtPayload;
|
|
119
|
+
// return {
|
|
120
|
+
// clientId: decoded.sub || "unknown",
|
|
121
|
+
// scopes: decoded.scope?.split(" ") || [],
|
|
122
|
+
// expiresAt: decoded.exp,
|
|
123
|
+
// };
|
|
124
|
+
|
|
125
|
+
throw new Error("Token verification not implemented");
|
|
126
|
+
},
|
|
127
|
+
required: false, // Set to true to require authentication
|
|
128
|
+
});
|
|
129
|
+
`;
|
|
@@ -3,19 +3,25 @@ import fs from "fs-extra";
|
|
|
3
3
|
import path from "path";
|
|
4
4
|
import { readTsConfigFile } from "../utils/read-config-file.js";
|
|
5
5
|
|
|
6
|
-
export type Framework = "nextjs" | "express";
|
|
6
|
+
export type Framework = "nextjs" | "nestjs" | "express";
|
|
7
7
|
|
|
8
8
|
export function detectFramework(projectRoot: string): Framework {
|
|
9
|
-
// check if project has been initialized with nextjs or default to express
|
|
10
9
|
const packageJson = JSON.parse(
|
|
11
10
|
fs.readFileSync(path.join(projectRoot, "package.json"), "utf-8")
|
|
12
11
|
);
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
// Check for NestJS
|
|
14
|
+
if (packageJson.dependencies?.["@nestjs/core"] || packageJson.devDependencies?.["@nestjs/core"]) {
|
|
15
|
+
return "nestjs";
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
// Check for Next.js
|
|
19
|
+
if (packageJson.dependencies?.next || packageJson.devDependencies?.next) {
|
|
20
|
+
return "nextjs";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Default to express
|
|
24
|
+
return "express";
|
|
19
25
|
}
|
|
20
26
|
|
|
21
27
|
export function detectTypeScript(projectRoot: string): boolean {
|
|
@@ -22,27 +22,12 @@ const config: XmcpConfig = {
|
|
|
22
22
|
adapter: "${frameworkAdapter}",
|
|
23
23
|
},`;
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
configContent += `
|
|
31
|
-
tools: "${toolsPath}",`;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (promptsPath) {
|
|
35
|
-
configContent += `
|
|
36
|
-
prompts: "${promptsPath}",`;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (resourcesPath) {
|
|
40
|
-
configContent += `
|
|
41
|
-
resources: "${resourcesPath}",`;
|
|
42
|
-
}
|
|
43
|
-
configContent += `
|
|
25
|
+
configContent += `
|
|
26
|
+
paths: {
|
|
27
|
+
tools: ${toolsPath ? `"${toolsPath}"` : "false"},
|
|
28
|
+
prompts: ${promptsPath ? `"${promptsPath}"` : "false"},
|
|
29
|
+
resources: ${resourcesPath ? `"${resourcesPath}"` : "false"},
|
|
44
30
|
},`;
|
|
45
|
-
}
|
|
46
31
|
|
|
47
32
|
if (frameworkAdapter === "nextjs") {
|
|
48
33
|
configContent += `
|
package/src/helpers/init.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { Framework } from "./detect-framework.js";
|
|
|
8
8
|
import { createRoute } from "./create-handler.js";
|
|
9
9
|
import { createPrompt } from "./create-prompt.js";
|
|
10
10
|
import { createResources } from "./create-resources.js";
|
|
11
|
+
import { createNestJsModule } from "./create-nestjs-module.js";
|
|
11
12
|
|
|
12
13
|
interface InitOptions {
|
|
13
14
|
projectRoot: string;
|
|
@@ -57,4 +58,8 @@ export async function init(options: InitOptions) {
|
|
|
57
58
|
if (framework === "nextjs" && routePath) {
|
|
58
59
|
createRoute(projectRoot, routePath);
|
|
59
60
|
}
|
|
61
|
+
|
|
62
|
+
if (framework === "nestjs") {
|
|
63
|
+
createNestJsModule(projectRoot);
|
|
64
|
+
}
|
|
60
65
|
}
|
package/src/index.ts
CHANGED
|
@@ -351,14 +351,21 @@ const program = new Command()
|
|
|
351
351
|
console.log(` • ${routePath}/route.ts`);
|
|
352
352
|
}
|
|
353
353
|
|
|
354
|
+
if (detectedFramework === "nestjs") {
|
|
355
|
+
const hasSrcFolder = fs.existsSync(path.join(projectRoot, "src"));
|
|
356
|
+
const xmcpPath = hasSrcFolder ? "src/xmcp" : "xmcp";
|
|
357
|
+
console.log(` • ${xmcpPath}/xmcp.controller.ts`);
|
|
358
|
+
console.log(` • ${xmcpPath}/xmcp.module.ts`);
|
|
359
|
+
}
|
|
360
|
+
|
|
354
361
|
console.log(chalk.blue("\n❯ Files updated:"));
|
|
355
362
|
console.log(` • package.json`);
|
|
356
363
|
console.log(` • tsconfig.json`);
|
|
357
364
|
|
|
358
365
|
console.log(chalk.blue("\nNext steps:"));
|
|
359
366
|
|
|
360
|
-
// code integration for express projects
|
|
361
|
-
if (detectedFramework !== "nextjs") {
|
|
367
|
+
// code integration for express projects
|
|
368
|
+
if (detectedFramework !== "nextjs" && detectedFramework !== "nestjs") {
|
|
362
369
|
const integrationCode = `import { xmcpHandler } from '@xmcp/adapter';
|
|
363
370
|
|
|
364
371
|
myApp.post("/mcp", xmcpHandler);
|
|
@@ -378,6 +385,29 @@ myApp.get("/mcp", xmcpHandler);`;
|
|
|
378
385
|
console.log(chalk.green(chalk.bgBlack(` ${" ".repeat(maxLength)}`)));
|
|
379
386
|
}
|
|
380
387
|
|
|
388
|
+
// code integration for NestJS projects
|
|
389
|
+
if (detectedFramework === "nestjs") {
|
|
390
|
+
const integrationCode = `import { XmcpModule } from "./xmcp/xmcp.module";
|
|
391
|
+
|
|
392
|
+
@Module({
|
|
393
|
+
imports: [XmcpModule],
|
|
394
|
+
})
|
|
395
|
+
export class AppModule {}`;
|
|
396
|
+
|
|
397
|
+
const lines = integrationCode.split("\n");
|
|
398
|
+
const maxLength = Math.max(...lines.map((line) => line.length)) + 2;
|
|
399
|
+
|
|
400
|
+
console.log(
|
|
401
|
+
"\nTo get started with xmcp in your NestJS application, add this to your app.module.ts:\n"
|
|
402
|
+
);
|
|
403
|
+
console.log(chalk.green(chalk.bgBlack(` ${" ".repeat(maxLength)}`)));
|
|
404
|
+
lines.forEach((line) => {
|
|
405
|
+
const padding = " ".repeat(maxLength - line.length);
|
|
406
|
+
console.log(chalk.green(chalk.bgBlack(` ${line}${padding}`)));
|
|
407
|
+
});
|
|
408
|
+
console.log(chalk.green(chalk.bgBlack(` ${" ".repeat(maxLength)}`)));
|
|
409
|
+
}
|
|
410
|
+
|
|
381
411
|
console.log(
|
|
382
412
|
` \nRun "${packageManager} run dev" to start the development server\n`
|
|
383
413
|
);
|