generate-mcp-server-template
Create customizable MCP server templates for Typescript or Python with options for basic, tools-only, resources-only, or complete features to streamline server setup and diagnostics.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| features | Yes | Features to include in the template | |
| serverName | Yes | Name for the MCP server | |
| serverType | Yes | Type of server to generate |
Implementation Reference
- src/index.ts:658-962 (handler)The main handler function for the 'generate-mcp-server-template' tool. It generates complete MCP server template code in either TypeScript or Python based on the provided serverName, serverType, and features parameters. Includes multiple template variants (basic, tools-only, resources-only, complete) with embedded setup instructions.async ({ serverName, serverType, features }) => { try { let template = ""; if (serverType === "typescript") { if (features === "basic" || features === "tools-only") { template = ` import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { z } from "zod"; // Create server with clear naming const server = new McpServer({ name: "${serverName}", version: "1.0.0" }); // Add a simple echo tool server.tool( "echo", { message: z.string().describe("Message to echo back") }, async ({ message }) => { try { return { content: [{ type: "text", text: \`Echo: \${message}\` }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: \`Error: \${error.message}\` }] }; } } ); // Connect transport async function main() { const transport = new StdioServerTransport(); await server.connect(transport); console.error("${serverName} started"); } main().catch(console.error);`; } else if (features === "resources-only") { template = ` import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; // Create server with clear naming const server = new McpServer({ name: "${serverName}", version: "1.0.0" }); // Add a static resource server.resource( "static-resource", "static://example", async (uri) => ({ contents: [{ uri: uri.href, text: "This is a static resource example." }] }) ); // Add a dynamic resource with template server.resource( "dynamic-resource", new ResourceTemplate("dynamic://{param}", { list: undefined }), async (uri, { param }) => ({ contents: [{ uri: uri.href, text: \`This is a dynamic resource with parameter: \${param}\` }] }) ); // Connect transport async function main() { const transport = new StdioServerTransport(); await server.connect(transport); console.error("${serverName} started"); } main().catch(console.error);`; } else { // complete template = ` import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { z } from "zod"; // Create server with clear naming const server = new McpServer({ name: "${serverName}", version: "1.0.0" }); // Add a tool server.tool( "echo", { message: z.string().describe("Message to echo back") }, async ({ message }) => { try { return { content: [{ type: "text", text: \`Echo: \${message}\` }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: \`Error: \${error.message}\` }] }; } } ); // Add a static resource server.resource( "static-resource", "static://example", async (uri) => ({ contents: [{ uri: uri.href, text: "This is a static resource example." }] }) ); // Add a dynamic resource with template server.resource( "dynamic-resource", new ResourceTemplate("dynamic://{param}", { list: undefined }), async (uri, { param }) => ({ contents: [{ uri: uri.href, text: \`This is a dynamic resource with parameter: \${param}\` }] }) ); // Add a prompt template server.prompt( "greet", { name: z.string().describe("Name to greet") }, ({ name }) => ({ messages: [{ role: "user", content: { type: "text", text: \`Please greet \${name} in a friendly way.\` } }] }) ); // Connect transport async function main() { const transport = new StdioServerTransport(); await server.connect(transport); console.error("${serverName} started"); } main().catch(console.error);`; } } else { // python if (features === "basic" || features === "tools-only") { template = ` from mcp.server.fastmcp import FastMCP # Create MCP server mcp = FastMCP("${serverName}") @mcp.tool() async def echo(message: str) -> str: """Echo a message back. Args: message: Message to echo back """ try: return f"Echo: {message}" except Exception as e: return f"Error: {str(e)}" if __name__ == "__main__": # Run the server mcp.run(transport='stdio')`; } else if (features === "resources-only") { template = ` from mcp.server.fastmcp import FastMCP import mcp.types as types from typing import List # Create MCP server mcp = FastMCP("${serverName}") @mcp.resource("static://example") async def static_resource() -> str: """Return a static resource.""" return "This is a static resource example." @mcp.resource("dynamic://{param}") async def dynamic_resource(param: str) -> str: """Return a dynamic resource. Args: param: Parameter value """ return f"This is a dynamic resource with parameter: {param}" if __name__ == "__main__": # Run the server mcp.run(transport='stdio')`; } else { // complete template = ` from mcp.server.fastmcp import FastMCP import mcp.types as types from typing import List # Create MCP server mcp = FastMCP("${serverName}") @mcp.tool() async def echo(message: str) -> str: """Echo a message back. Args: message: Message to echo back """ try: return f"Echo: {message}" except Exception as e: return f"Error: {str(e)}" @mcp.resource("static://example") async def static_resource() -> str: """Return a static resource.""" return "This is a static resource example." @mcp.resource("dynamic://{param}") async def dynamic_resource(param: str) -> str: """Return a dynamic resource. Args: param: Parameter value """ return f"This is a dynamic resource with parameter: {param}" @mcp.prompt() async def greet(name: str) -> List[types.PromptMessage]: """Define a greeting prompt. Args: name: Name to greet """ return [ types.PromptMessage( role="user", content=types.TextContent( type="text", text=f"Please greet {name} in a friendly way." ) ) ] if __name__ == "__main__": # Run the server mcp.run(transport='stdio')`; } } return { content: [{ type: "text", text: `# ${serverName} Template (${serverType})\n\nHere's a ${features} template for your MCP server:\n\n\`\`\`${serverType === "typescript" ? "typescript" : "python"}\n${template}\n\`\`\`\n\n## Setup Instructions\n\n${serverType === "typescript" ? `1. Create a new directory for your project 2. Initialize a new npm project: \`npm init -y\` 3. Install dependencies: \`npm install @modelcontextprotocol/sdk zod\` 4. Install dev dependencies: \`npm install -D typescript @types/node\` 5. Create a tsconfig.json file 6. Save the above code to src/index.ts 7. Compile with TypeScript: \`npx tsc\` 8. Run the server: \`node build/index.js\`` : `1. Create a new directory for your project 2. Create a virtual environment: \`python -m venv venv\` 3. Activate the virtual environment: \`source venv/bin/activate\` (Linux/Mac) or \`venv\\Scripts\\activate\` (Windows) 4. Install dependencies: \`pip install mcp\` 5. Save the above code to server.py 6. Run the server: \`python server.py\``}` }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error generating template: ${error.message}` }] }; } }
- src/index.ts:653-657 (schema)Zod schema defining the input parameters for the tool: serverName (string), serverType (enum: typescript/python), features (enum: basic/tools-only/resources-only/complete).{ serverName: z.string().describe("Name for the MCP server"), serverType: z.enum(["typescript", "python"]).describe("Type of server to generate"), features: z.enum(["basic", "tools-only", "resources-only", "complete"]).describe("Features to include in the template") },
- src/index.ts:651-963 (registration)Registration of the 'generate-mcp-server-template' tool on the McpServer instance using server.tool(name, inputSchema, handlerFn).server.tool( "generate-mcp-server-template", { serverName: z.string().describe("Name for the MCP server"), serverType: z.enum(["typescript", "python"]).describe("Type of server to generate"), features: z.enum(["basic", "tools-only", "resources-only", "complete"]).describe("Features to include in the template") }, async ({ serverName, serverType, features }) => { try { let template = ""; if (serverType === "typescript") { if (features === "basic" || features === "tools-only") { template = ` import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { z } from "zod"; // Create server with clear naming const server = new McpServer({ name: "${serverName}", version: "1.0.0" }); // Add a simple echo tool server.tool( "echo", { message: z.string().describe("Message to echo back") }, async ({ message }) => { try { return { content: [{ type: "text", text: \`Echo: \${message}\` }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: \`Error: \${error.message}\` }] }; } } ); // Connect transport async function main() { const transport = new StdioServerTransport(); await server.connect(transport); console.error("${serverName} started"); } main().catch(console.error);`; } else if (features === "resources-only") { template = ` import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; // Create server with clear naming const server = new McpServer({ name: "${serverName}", version: "1.0.0" }); // Add a static resource server.resource( "static-resource", "static://example", async (uri) => ({ contents: [{ uri: uri.href, text: "This is a static resource example." }] }) ); // Add a dynamic resource with template server.resource( "dynamic-resource", new ResourceTemplate("dynamic://{param}", { list: undefined }), async (uri, { param }) => ({ contents: [{ uri: uri.href, text: \`This is a dynamic resource with parameter: \${param}\` }] }) ); // Connect transport async function main() { const transport = new StdioServerTransport(); await server.connect(transport); console.error("${serverName} started"); } main().catch(console.error);`; } else { // complete template = ` import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { z } from "zod"; // Create server with clear naming const server = new McpServer({ name: "${serverName}", version: "1.0.0" }); // Add a tool server.tool( "echo", { message: z.string().describe("Message to echo back") }, async ({ message }) => { try { return { content: [{ type: "text", text: \`Echo: \${message}\` }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: \`Error: \${error.message}\` }] }; } } ); // Add a static resource server.resource( "static-resource", "static://example", async (uri) => ({ contents: [{ uri: uri.href, text: "This is a static resource example." }] }) ); // Add a dynamic resource with template server.resource( "dynamic-resource", new ResourceTemplate("dynamic://{param}", { list: undefined }), async (uri, { param }) => ({ contents: [{ uri: uri.href, text: \`This is a dynamic resource with parameter: \${param}\` }] }) ); // Add a prompt template server.prompt( "greet", { name: z.string().describe("Name to greet") }, ({ name }) => ({ messages: [{ role: "user", content: { type: "text", text: \`Please greet \${name} in a friendly way.\` } }] }) ); // Connect transport async function main() { const transport = new StdioServerTransport(); await server.connect(transport); console.error("${serverName} started"); } main().catch(console.error);`; } } else { // python if (features === "basic" || features === "tools-only") { template = ` from mcp.server.fastmcp import FastMCP # Create MCP server mcp = FastMCP("${serverName}") @mcp.tool() async def echo(message: str) -> str: """Echo a message back. Args: message: Message to echo back """ try: return f"Echo: {message}" except Exception as e: return f"Error: {str(e)}" if __name__ == "__main__": # Run the server mcp.run(transport='stdio')`; } else if (features === "resources-only") { template = ` from mcp.server.fastmcp import FastMCP import mcp.types as types from typing import List # Create MCP server mcp = FastMCP("${serverName}") @mcp.resource("static://example") async def static_resource() -> str: """Return a static resource.""" return "This is a static resource example." @mcp.resource("dynamic://{param}") async def dynamic_resource(param: str) -> str: """Return a dynamic resource. Args: param: Parameter value """ return f"This is a dynamic resource with parameter: {param}" if __name__ == "__main__": # Run the server mcp.run(transport='stdio')`; } else { // complete template = ` from mcp.server.fastmcp import FastMCP import mcp.types as types from typing import List # Create MCP server mcp = FastMCP("${serverName}") @mcp.tool() async def echo(message: str) -> str: """Echo a message back. Args: message: Message to echo back """ try: return f"Echo: {message}" except Exception as e: return f"Error: {str(e)}" @mcp.resource("static://example") async def static_resource() -> str: """Return a static resource.""" return "This is a static resource example." @mcp.resource("dynamic://{param}") async def dynamic_resource(param: str) -> str: """Return a dynamic resource. Args: param: Parameter value """ return f"This is a dynamic resource with parameter: {param}" @mcp.prompt() async def greet(name: str) -> List[types.PromptMessage]: """Define a greeting prompt. Args: name: Name to greet """ return [ types.PromptMessage( role="user", content=types.TextContent( type="text", text=f"Please greet {name} in a friendly way." ) ) ] if __name__ == "__main__": # Run the server mcp.run(transport='stdio')`; } } return { content: [{ type: "text", text: `# ${serverName} Template (${serverType})\n\nHere's a ${features} template for your MCP server:\n\n\`\`\`${serverType === "typescript" ? "typescript" : "python"}\n${template}\n\`\`\`\n\n## Setup Instructions\n\n${serverType === "typescript" ? `1. Create a new directory for your project 2. Initialize a new npm project: \`npm init -y\` 3. Install dependencies: \`npm install @modelcontextprotocol/sdk zod\` 4. Install dev dependencies: \`npm install -D typescript @types/node\` 5. Create a tsconfig.json file 6. Save the above code to src/index.ts 7. Compile with TypeScript: \`npx tsc\` 8. Run the server: \`node build/index.js\`` : `1. Create a new directory for your project 2. Create a virtual environment: \`python -m venv venv\` 3. Activate the virtual environment: \`source venv/bin/activate\` (Linux/Mac) or \`venv\\Scripts\\activate\` (Windows) 4. Install dependencies: \`pip install mcp\` 5. Save the above code to server.py 6. Run the server: \`python server.py\``}` }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error generating template: ${error.message}` }] }; } } );