Skip to main content
Glama

MCP Server Foundation Template

by joeyheath65

MCP Server Foundation Template

A customizable, production-ready foundation template for building Model Context Protocol (MCP) servers. This template follows MCP best practices and provides a clean, well-structured starting point for creating your own MCP servers.

🌟 Features

  • Dual Transport Support: Both stdio (CLI) and HTTP (SSE) transport modes

  • Comprehensive Structure: Clear separation of tools, resources, and prompts

  • TypeScript: Full type safety with modern TypeScript

  • FastMCP: Built on the FastMCP framework for simplicity and performance

  • Docker Ready: Complete Docker and docker-compose support

  • Well Documented: Extensive documentation for usage, customization, and architecture

  • Extensible: Easy to add custom tools, resources, and prompts

  • Production Ready: Includes error handling, graceful shutdown, and best practices

πŸ“‹ Table of Contents

πŸš€ Quick Start

Prerequisites

  • Node.js 20+ or Bun 1.0+

  • Python 3 (optional, for Python tools)

  • Docker (optional, for containerized deployment)

Installation

Clone and setup:

# Clone this template git clone <your-repo-url> cd mcp-template # Install dependencies bun install # or npm install, yarn install, pnpm install # Make binary executable chmod +x bin/stdio.js

πŸ’» Usage

Native Setup

stdio Transport (CLI Mode)

Start the server in stdio mode for command-line usage:

npm start # or bun run src/index.ts --transport stdio

HTTP Transport (Web Mode)

Start the server in HTTP mode for web integration:

npm run start:http # or bun run src/index.ts --transport http --port 3001

Docker Setup

Using Docker Compose

Development mode (with hot reload):

docker-compose -f docker-compose.dev.yml up

Production mode (optimized):

docker-compose -f docker-compose.prod.yml up

Default mode (both stdio + http):

docker-compose up

Start specific service:

# stdio only docker-compose up mcp-stdio # http only docker-compose up mcp-http # In background docker-compose up -d

Using Docker Directly

# Build for stdio transport docker build --target production-stdio -t mcp-template:stdio . # Build for http transport docker build --target production-http -t mcp-template:http . # Run stdio mode docker run -it mcp-template:stdio # Run http mode with port mapping docker run -d -p 3001:3001 --name mcp-server mcp-template:http

πŸ“– See

πŸ—οΈ Architecture

This template implements the Model Context Protocol (MCP) architecture:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ MCP Client β”‚ (Cursor, Claude Desktop, etc.) β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Transport Layer β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ stdio β”‚ β”‚ HTTP β”‚ β”‚ β”‚ β”‚ (stdin/ β”‚ β”‚ (SSE) β”‚ β”‚ β”‚ β”‚ stdout) β”‚ β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β–Ό β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Data Layer Protocol β”‚ β”‚ (JSON-RPC 2.0 over Transport) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ MCP Server Core β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”β”‚ β”‚ β”‚ Tools β”‚ β”‚Resources β”‚ β”‚Promptsβ”‚β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”˜β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Components:

  1. Transport Layer: Handles communication (stdio or HTTP)

  2. Data Layer: JSON-RPC 2.0 protocol

  3. Server Core: FastMCP framework

  4. Primitives: Tools, Resources, Prompts

See ARCHITECTURE.md for detailed architecture documentation.

πŸ”§ Customization

Adding Tools

Tools are functions that the AI can call to perform actions.

Node.js/TypeScript Tools

Create a new file src/tools/your_tool.ts:

import { FastMcp } from '@fastmcp/core'; import { z } from 'zod'; export function registerYourTool(mcp: FastMcp): void { mcp.tool({ name: 'your_tool_name', description: 'Description of what your tool does', parameters: z.object({ param1: z.string().describe('First parameter'), param2: z.number().optional().describe('Optional parameter'), }), handler: async ({ param1, param2 }) => { // Your tool logic here return { result: `Processed ${param1}`, }; }, }); }

Then register it in src/tools/index.ts:

import { registerYourTool } from './your_tool'; export function registerTools(mcp: FastMcp): void { registerNodeTools(mcp); registerPythonTools(mcp); registerYourTool(mcp); // Add this line }

Python Tools

For Python tools, you can:

  1. Execute Python scripts: Use child_process to run Python scripts

  2. Create a Python MCP proxy: Separate MCP server for Python tools

  3. Use Python execution libraries: Use libraries like python-shell

See src/tools/python.ts for implementation patterns.

Adding Resources

Resources are read-only data sources that the AI can access.

Create a resource in src/resources/your_resource.ts:

import { FastMcp } from '@fastmcp/core'; export function registerYourResource(mcp: FastMcp): void { mcp.resource({ uri: 'your_scheme://path/{param}', name: 'Your Resource Name', description: 'Description of your resource', handler: async ({ param }: { param: string }) => { // Load and return your resource data return { contents: [ { uri: `your_scheme://path/${param}`, mimeType: 'application/json', text: JSON.stringify({ data: 'your data' }, null, 2), }, ], }; }, }); }

Register in src/resources/index.ts.

Adding Prompts

Prompts are template-based messages for the AI.

Create a prompt in src/prompts/your_prompt.ts:

import { FastMcp } from '@fastmcp/core'; export function registerYourPrompt(mcp: FastMcp): void { mcp.prompt({ name: 'your_prompt_name', description: 'Description of your prompt', arguments: [ { name: 'arg1', description: 'First argument', required: true, }, ], handler: async ({ arg1 }) => { return { messages: [ { role: 'user', content: { type: 'text', text: `Your prompt template with ${arg1}`, }, }, ], }; }, }); }

Register in src/prompts/index.ts.

πŸ”Œ Transport Modes

stdio Transport

  • Use Case: CLI tools, local development, Cursor integration

  • Communication: stdin/stdout

  • Network: None (local process communication)

  • Access: Single user, local only

  • Example: AI assistant in terminal

HTTP Transport

  • Use Case: Web apps, remote access, team sharing

  • Communication: Server-Sent Events (SSE)

  • Network: TCP/IP over HTTP

  • Access: Multi-user, remote capable

  • Example: Shared AI tools for team

βš™οΈ Configuration

Environment Variables

The server is configured using environment variables. Get started quickly:

# 1. Copy the example environment file cp env.example .env # 2. Edit .env with your settings nano .env # or your preferred editor # 3. Start the server (variables load automatically) npm start

Quick Reference

Server Settings:

  • TRANSPORT: stdio or http (default: stdio)

  • PORT: HTTP port (default: 3001)

  • HOST: HTTP host binding (default: 0.0.0.0)

Logging:

  • LOG_LEVEL: error, warn, info, debug (default: info)

  • LOG_FORMAT: json or text (default: text)

Security:

  • API_KEY: API authentication key (optional)

  • JWT_SECRET: JWT token secret (optional)

  • ALLOWED_ORIGINS: Comma-separated CORS origins (optional)

Feature Flags:

  • ENABLE_TOOLS: Enable tools (default: true)

  • ENABLE_RESOURCES: Enable resources (default: true)

  • ENABLE_PROMPTS: Enable prompts (default: true)

Tool Execution:

  • PYTHON_PATH: Python executable path (default: python3)

  • NODE_PATH: Node.js executable path (default: node)

  • MAX_TOOL_EXECUTION_TIME: Max execution time in ms (default: 30000)

Using Configuration in Code

import { getConfig } from './config'; const config = getConfig(); console.log(`Running on port ${config.server.port}`);

Full Documentation

πŸ“– See

  • Complete environment variable reference

  • Configuration best practices

  • Cloud deployment configuration

  • Example usage patterns

  • Troubleshooting guide

πŸ› οΈ Development

Development Mode

Auto-reload on file changes:

# stdio dev mode npm run dev # http dev mode npm run dev:http

Scripts

  • npm start - Start in stdio mode

  • npm run start:http - Start in HTTP mode

  • npm run dev - Development mode with auto-reload

  • npm run build - Build TypeScript

  • npm run lint - Run ESLint

  • npm run type-check - Type checking without emit

Project Structure

. β”œβ”€β”€ src/ # Source code β”‚ β”œβ”€β”€ index.ts # Entry point β”‚ β”œβ”€β”€ server.ts # Server core β”‚ β”œβ”€β”€ types.ts # Type definitions β”‚ β”œβ”€β”€ tools/ # MCP tools β”‚ β”‚ β”œβ”€β”€ index.ts β”‚ β”‚ β”œβ”€β”€ node.ts # Node.js tools β”‚ β”‚ └── python.ts # Python tools β”‚ β”œβ”€β”€ resources/ # MCP resources β”‚ β”‚ β”œβ”€β”€ index.ts β”‚ β”‚ └── example.ts β”‚ β”œβ”€β”€ prompts/ # MCP prompts β”‚ β”‚ β”œβ”€β”€ index.ts β”‚ β”‚ └── example.ts β”‚ β”œβ”€β”€ transport/ # Transport implementations β”‚ β”‚ β”œβ”€β”€ stdio.ts β”‚ β”‚ └── http.ts β”‚ β”œβ”€β”€ config/ # Configuration management β”‚ β”‚ └── index.ts β”‚ └── utils/ # Utilities β”‚ └── args.ts β”œβ”€β”€ bin/ # Binary entry points β”‚ └── stdio.js # stdio binary β”œβ”€β”€ docs/ # Documentation β”‚ β”œβ”€β”€ CONFIGURATION.md # Config guide β”‚ └── DOCKER.md # Docker guide β”œβ”€β”€ Dockerfile # Docker image β”œβ”€β”€ docker-compose.yml # Docker Compose (default) β”œβ”€β”€ docker-compose.dev.yml # Docker Compose (dev) β”œβ”€β”€ docker-compose.prod.yml # Docker Compose (prod) β”œβ”€β”€ .dockerignore # Docker ignore patterns β”œβ”€β”€ env.example # Environment template β”œβ”€β”€ package.json # Dependencies β”œβ”€β”€ tsconfig.json # TypeScript config β”œβ”€β”€ eslint.config.js # ESLint config β”œβ”€β”€ Makefile # Convenience commands β”œβ”€β”€ README.md # Main documentation β”œβ”€β”€ QUICK_START.md # Quick start guide β”œβ”€β”€ ARCHITECTURE.md # Architecture docs β”œβ”€β”€ PLANNING.md # Planning docs β”œβ”€β”€ TASK.md # Task tracking β”œβ”€β”€ CONTRIBUTING.md # Contributing guide β”œβ”€β”€ CHANGELOG.md # Change log └── LICENSE # License

See PLANNING.md for development planning and ARCHITECTURE.md for architecture details.

πŸ§ͺ Testing

Local Testing

Test your MCP server with FastMCP CLI:

# Test in development mode npx @fastmcp/core dev src/index.ts # Inspect server capabilities npx @fastmcp/core inspect src/index.ts

Integration Testing

Connect from Cursor:

  1. Open Cursor Settings

  2. Features β†’ MCP Servers β†’ Add new server

  3. Configure:

    • stdio: command: npm start

    • http: url: http://localhost:3001/sse

🚒 Deployment

Docker Deployment

Production deployment:

# Using production compose docker-compose -f docker-compose.prod.yml up -d # Or build and run directly docker build --target production-http -t your-org/mcp-server . docker run -d -p 3001:3001 \ -e LOG_LEVEL=warn \ --name mcp-server \ your-org/mcp-server

Cloud deployment options:

  • Railway: railway up

  • Render: Configure via render.yaml

  • Fly.io: fly launch

  • Kubernetes: Use k8s deployment manifests

πŸ“– See

Cloud Deployment

Deploy to cloud platforms (AWS, GCP, Azure) using Docker or native binaries.

πŸ“š Documentation

🀝 Contributing

Contributions welcome! See the main project for contribution guidelines.

πŸ“„ License

MIT License - see LICENSE file for details

πŸ”— Resources

πŸ™ Acknowledgments

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/joeyheath65/mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server