Basic template, authored by Claude

This commit is contained in:
Justin Spahr-Summers 2024-11-18 13:16:04 +00:00
commit 0c5b091517
No known key found for this signature in database
10 changed files with 1511 additions and 0 deletions

2
.gitignore vendored
View file

@ -128,3 +128,5 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
build/

2
.npmrc Normal file
View file

@ -0,0 +1,2 @@
registry="https://registry.npmjs.org/"
@modelcontextprotocol:registry="https://registry.npmjs.org/"

1264
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

36
package.json Normal file
View file

@ -0,0 +1,36 @@
{
"name": "@modelcontextprotocol/create-server",
"version": "0.1.0",
"description": "CLI tool to create new MCP servers",
"license": "MIT",
"author": "Anthropic, PBC (https://anthropic.com)",
"homepage": "https://modelcontextprotocol.github.io",
"bugs": "https://github.com/modelcontextprotocol/create-typescript-server/issues",
"type": "module",
"bin": {
"create-mcp-server": "build/index.js"
},
"files": [
"build",
"templates"
],
"scripts": {
"build": "tsc && shx chmod +x build/index.js",
"prepare": "npm run build"
},
"dependencies": {
"@modelcontextprotocol/sdk": "0.6.0",
"chalk": "^5.3.0",
"commander": "^12.0.0",
"ejs": "^3.1.9",
"inquirer": "^9.2.15",
"ora": "^8.0.1"
},
"devDependencies": {
"@types/ejs": "^3.1.5",
"@types/inquirer": "^9.0.7",
"@types/node": "^20.11.24",
"shx": "^0.3.4",
"typescript": "^5.3.3"
}
}

92
src/index.ts Normal file
View file

@ -0,0 +1,92 @@
#!/usr/bin/env node
import { Command } from "commander";
import inquirer from "inquirer";
import { fileURLToPath } from "url";
import path from "path";
import fs from "fs/promises";
import chalk from "chalk";
import ora from "ora";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
async function createServer(directory: string, options: any = {}) {
const questions = [
{
type: "input",
name: "name",
message: "What is the name of your MCP server?",
default: path.basename(directory),
when: !options.name,
},
{
type: "input",
name: "description",
message: "What is the description of your server?",
default: "A Model Context Protocol server",
when: !options.description,
},
];
const answers = await inquirer.prompt(questions);
const config = {
name: options.name || answers.name,
description: options.description || answers.description,
};
const spinner = ora("Creating MCP server...").start();
try {
// Create project directory
await fs.mkdir(directory, { recursive: true });
// Copy template files
const templateDir = path.join(__dirname, "../templates");
const files = await fs.readdir(templateDir, { recursive: true });
for (const file of files) {
const sourcePath = path.join(templateDir, file);
const stats = await fs.stat(sourcePath);
if (!stats.isFile()) continue;
const targetPath = path.join(directory, file);
const targetDir = path.dirname(targetPath);
// Create subdirectories if needed
await fs.mkdir(targetDir, { recursive: true });
// Read and process template file
let content = await fs.readFile(sourcePath, "utf-8");
// Replace template variables
content = content.replace(/\{\{name\}\}/g, config.name);
content = content.replace(/\{\{description\}\}/g, config.description);
// Write processed file
await fs.writeFile(targetPath, content);
}
spinner.succeed(chalk.green("MCP server created successfully!"));
// Print next steps
console.log("\nNext steps:");
console.log(chalk.cyan(` cd ${directory}`));
console.log(chalk.cyan(" npm install"));
console.log(chalk.cyan(" npm run dev\n"));
} catch (error) {
spinner.fail(chalk.red("Failed to create MCP server"));
console.error(error);
process.exit(1);
}
}
const program = new Command()
.name("create-mcp-server")
.description("Create a new MCP server")
.argument("[directory]", "Directory to create the server in", ".")
.option("-n, --name <name>", "Name of the server")
.option("-d, --description <description>", "Description of the server")
.action(createServer);
program.parse();

3
templates/basic/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
node_modules/
build/
*.log

View file

@ -0,0 +1,18 @@
{
"name": "<%= name %>",
"version": "0.1.0",
"description": "<%= description %>",
"type": "module",
"scripts": {
"build": "tsc",
"dev": "tsc --watch",
"start": "node build/index.js"
},
"dependencies": {
"@modelcontextprotocol/sdk": "^0.5.0"
},
"devDependencies": {
"@types/node": "^20.11.24",
"typescript": "^5.3.3"
}
}

View file

@ -0,0 +1,63 @@
#!/usr/bin/env node
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
ListResourcesRequestSchema,
ListToolsRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
const server = new Server(
{
name: "<%= name %>",
version: "0.1.0",
},
{
capabilities: {
resources: {},
tools: {},
},
},
);
server.setRequestHandler(ListResourcesRequestSchema, async () => {
return {
resources: [
{
uri: "example://hello",
mimeType: "text/plain",
name: "Example resource",
},
],
};
});
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [
{
name: "example",
description: "An example tool",
inputSchema: {
type: "object",
properties: {
message: {
type: "string",
description: "Message to echo",
},
},
},
},
],
};
});
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
}
main().catch((error) => {
console.error("Server error:", error);
process.exit(1);
});

View file

@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "Node16",
"moduleResolution": "Node16",
"outDir": "./build",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}

16
tsconfig.json Normal file
View file

@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "Node16",
"moduleResolution": "Node16",
"outDir": "./build",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}