A robust template for building Model Context Protocol (MCP) servers using Node.js and the Fastify framework. This template provides a solid foundation for creating MCP-compliant servers with TypeScript support, proper error handling, and session management.
- MCP Protocol Compliance: Full support for Model Context Protocol specifications
- Fastify Framework: High-performance, low-overhead web framework
- TypeScript Support: Type-safe development with comprehensive type definitions
- Node.js 22+
- npm or yarn package manager
- TypeScript knowledge (recommended)
-
Clone the repository
git clone https://github.com/karankraina/mcp-fastify.git cd mcp-fastify
-
Install dependencies
npm install
-
Set up environment variables
cp sample.env .env # Edit .env with your configuration
-
Build the project
npm run build
-
Start the server
npm start
mcp-fastify/
├── src/ # Source TypeScript files
│ ├── server.ts # Main server implementation
│ └── tools/ # MCP tools directory
│ ├── index.ts # Tool registration hub
│ └── divide-numbers.ts # Example divide tool with error handling
├── build/ # Compiled JavaScript output
├── tests/ # Test files
├── package.json # Project configuration
├── tsconfig.json # TypeScript configuration
├── sample.env # Environment variables template
└── README.md # This file
Variable | Description | Default | Required |
---|---|---|---|
PORT |
Server port number | 3001 |
No |
ENVIRONMENT |
Runtime environment (development , production , test ) |
production |
No |
The server supports different logging configurations based on the environment:
- Development: Pretty-printed logs with timestamps
- Production: JSON structured logs
- Test: Disabled logging
To add a new MCP tool:
-
Create a new tool file in
src/tools/
:// src/tools/your-new-tool.ts import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { z } from "zod"; export function registerYourNewTool(server: McpServer) { server.registerTool( "your_tool_name", { title: "Your Tool Title", description: "Description of what your tool does", annotations: { destructiveHint: false, openWorldHint: false, }, inputSchema: { param1: z.string().describe("First parameter"), param2: z.number().describe("Second parameter"), }, }, async ({ param1, param2 }) => { try { // Your tool logic here const result = `Processed: ${param1} with ${param2}`; return { content: [{ type: "text", text: result, }] }; } catch (error) { console.error('Error in your tool:', error); return { content: [{ type: "text", text: `Error: ${error instanceof Error ? error.message : String(error)}`, }] }; } } ); }
-
Register the tool in
src/tools/index.ts
:import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { registerMultiplyNumbersTool } from "./multiply-numbers.js"; import { registerYourNewTool } from "./your-new-tool.js"; export async function registerTools(server: McpServer) { await registerMultiplyNumbersTool(server); await registerYourNewTool(server); }
-
Rebuild and test:
npm run build npm start
Run the test suite:
npm test
Run tests in watch mode:
npm run test:watch
Run tests with coverage:
npm run test:coverage
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Visit the repository at: https://github.com/karankraina/mcp-fastify
This project is licensed under the ISC License - see the LICENSE file for details.
- Anthropic for the Model Context Protocol
- Fastify Team for the excellent web framework
- Contributors and the open-source community