mcp-backend-server

JauryAbreu/mcp-backend-server

3.1

If you are the rightful owner of mcp-backend-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.

A professional Model Context Protocol (MCP) backend server built with TypeScript, implementing SOLID principles and Clean Architecture patterns.

MCP Backend Server

Overview

A professional Model Context Protocol (MCP) backend server built with TypeScript, implementing SOLID principles and Clean Architecture patterns. Features dual access modes: native MCP protocol for development and HTTP REST API for external integrations.

โœจ Key Features

  • ๐Ÿ—๏ธ SOLID Architecture: Full compliance with all 5 SOLID principles
  • ๐Ÿ”ง Clean Architecture: Hexagonal (Ports & Adapters) pattern implementation
  • ๐Ÿš€ Dual Access: MCP protocol + HTTP REST API wrapper
  • ๐Ÿ” Type Safety: Full TypeScript implementation with strict mode
  • โœ… Comprehensive Testing: Unit tests with 100% coverage
  • ๐Ÿ“ Domain-Driven: Rich domain models with validation
  • ๐Ÿ’‰ Dependency Injection: IoC container for loose coupling
  • ๐Ÿ“Š Structured Logging: Centralized logging with different levels
  • ๐Ÿ”’ Input Validation: Zod schema validation for all endpoints

๐Ÿ›๏ธ Architecture Overview

SOLID Principles Implementation

โœ… Single Responsibility Principle (SRP)
  • Controllers: Only handle HTTP requests/responses
  • Services: Pure business logic implementation
  • Repositories: Data access abstraction
  • Validators: Input validation only
  • Models: Domain entity representation
โœ… Open/Closed Principle (OCP)
  • Interface-based design allows extension without modification
  • Plugin architecture for new tools and resources
  • Strategy pattern for different implementations
โœ… Liskov Substitution Principle (LSP)
  • All implementations properly substitute their interfaces
  • HttpClient can be replaced with any IHttpClient implementation
  • Logger implementations are fully interchangeable
โœ… Interface Segregation Principle (ISP)
  • Focused interfaces (IHttpClient, ILogger, IResourceService)
  • No forced dependencies on unused methods
  • Client-specific interface design
โœ… Dependency Inversion Principle (DIP)
  • High-level modules don't depend on low-level modules
  • Both depend on abstractions (interfaces)
  • ServiceContainer manages all dependencies

๐Ÿ—๏ธ Layer Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    ADAPTERS LAYER                           โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚  HTTP REST API  โ”‚  โ”‚   MCP Protocol  โ”‚  โ”‚ Controllers  โ”‚ โ”‚
โ”‚  โ”‚   (Express)     โ”‚  โ”‚     (stdio)     โ”‚  โ”‚   Routes     โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                  APPLICATION LAYER                          โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚    Services     โ”‚  โ”‚   Use Cases     โ”‚  โ”‚   Handlers   โ”‚ โ”‚
โ”‚  โ”‚ (Business Logic)โ”‚  โ”‚ (Orchestration) โ”‚  โ”‚ (MCP Tools)  โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                    DOMAIN LAYER                             โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚    Entities     โ”‚  โ”‚  Value Objects  โ”‚  โ”‚  Interfaces  โ”‚ โ”‚
โ”‚  โ”‚ (User, Post,    โ”‚  โ”‚  (Validation)   โ”‚  โ”‚ (Contracts)  โ”‚ โ”‚
โ”‚  โ”‚  Comment)       โ”‚  โ”‚                 โ”‚  โ”‚              โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                INFRASTRUCTURE LAYER                         โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚   HTTP Client   โ”‚  โ”‚     Logger      โ”‚  โ”‚   External   โ”‚ โ”‚
โ”‚  โ”‚ (node-fetch)    โ”‚  โ”‚   (Console)     โ”‚  โ”‚     APIs     โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“ Project Structure

src/
โ”œโ”€โ”€ adapters/                    # ๐Ÿ”Œ External interfaces
โ”‚   โ”œโ”€โ”€ controllers/            # HTTP request handlers
โ”‚   โ”‚   โ”œโ”€โ”€ resourceController.ts
โ”‚   โ”‚   โ”œโ”€โ”€ toolController.ts
โ”‚   โ”‚   โ””โ”€โ”€ index.ts
โ”‚   โ”œโ”€โ”€ routes/                 # Route definitions
โ”‚   โ”‚   โ””โ”€โ”€ httpRoutes.ts
โ”‚   โ”œโ”€โ”€ validators/             # Input validation schemas
โ”‚   โ”‚   โ””โ”€โ”€ httpValidators.ts
โ”‚   โ”œโ”€โ”€ httpAdapter.ts          # Express server setup
โ”‚   โ”œโ”€โ”€ mcpAdapter.ts          # MCP SDK wrapper
โ”‚   โ””โ”€โ”€ mcpServerAdapter.ts    # MCP server configuration
โ”œโ”€โ”€ application/                # ๐ŸŽฏ Business logic
โ”‚   โ”œโ”€โ”€ services/              # Core business services
โ”‚   โ”‚   โ”œโ”€โ”€ resourceService.ts # Resource operations
โ”‚   โ”‚   โ””โ”€โ”€ toolService.ts     # Tool operations
โ”‚   โ”œโ”€โ”€ interfaces/            # Dependency contracts
โ”‚   โ”‚   โ””โ”€โ”€ index.ts          # All interfaces
โ”‚   โ”œโ”€โ”€ container/             # Dependency injection
โ”‚   โ”‚   โ””โ”€โ”€ serviceContainer.ts
โ”‚   โ”œโ”€โ”€ resources/             # MCP resource handlers
โ”‚   โ”‚   โ””โ”€โ”€ resourceHandlers.ts
โ”‚   โ”œโ”€โ”€ tools/                 # MCP tool handlers
โ”‚   โ”‚   โ””โ”€โ”€ toolHandlers.ts
โ”‚   โ””โ”€โ”€ index.ts              # Layer exports
โ”œโ”€โ”€ domain/                     # ๐ŸŽญ Business entities
โ”‚   โ”œโ”€โ”€ user.ts               # User entity & validation
โ”‚   โ”œโ”€โ”€ post.ts               # Post entity & validation
โ”‚   โ”œโ”€โ”€ comment.ts            # Comment entity & validation
โ”‚   โ””โ”€โ”€ index.ts              # Domain exports
โ”œโ”€โ”€ infrastructure/            # ๐Ÿ”ง Technical implementations
โ”‚   โ”œโ”€โ”€ httpClient.ts         # HTTP client implementation
โ”‚   โ”œโ”€โ”€ logger.ts             # Logging implementation
โ”‚   โ”œโ”€โ”€ fetchUtil.ts          # Legacy utility (deprecated)
โ”‚   โ””โ”€โ”€ index.ts              # Infrastructure exports
โ”œโ”€โ”€ server.ts                 # MCP server entry point
โ””โ”€โ”€ httpServer.ts            # HTTP server entry point

๐Ÿš€ Quick Start

Prerequisites

  • Node.js โ‰ฅ 18.0.0
  • npm or yarn

Installation

# Clone and install dependencies
git clone <repository-url>
cd mcp-example
npm install

Development Commands

# Build the project
npm run build

# Start MCP server (for MCP Inspector)
npm start

# Start HTTP REST API server
npm run start:http

# Run tests with coverage
npm test

# Lint and fix code
npm run lint
npm run format

๐Ÿ”Œ API Endpoints

Base URL: http://localhost:3001

๐Ÿ“Š Resources

  • GET /api/users - Fetch all users
  • GET /api/posts - Fetch all posts
  • GET /api/comments?postId=<id> - Fetch comments (with optional filtering)

๐Ÿ”ง Tools

  • POST /api/calculate - Mathematical operations
  • POST /api/temperature - Temperature conversion

๐Ÿ“– Documentation

  • GET /health - Health check
  • GET /api/docs - Complete API documentation

Example Requests

Calculate
curl -X POST http://localhost:3001/api/calculate \
  -H "Content-Type: application/json" \
  -d '{"a": 10, "b": 5, "operation": "add"}'
Temperature Conversion
curl -X POST http://localhost:3001/api/temperature \
  -H "Content-Type: application/json" \
  -d '{"value": 25, "fromUnit": "celsius", "toUnit": "fahrenheit"}'

๐Ÿงช Testing

Test Coverage

  • Unit Tests: All services, controllers, and utilities
  • Integration Tests: Complete HTTP API workflow
  • Domain Tests: Entity validation and transformation
  • Infrastructure Tests: External integrations
# Run all tests
npm test

# Run tests in watch mode
npm test -- --watch

# Run specific test file
npm test -- --testPathPattern=resourceController

๐Ÿ”ง Configuration

Environment Variables

# Copy example configuration
cp .env.example .env

# Available variables
NODE_ENV=development          # Environment mode
PORT=3001                    # HTTP server port
API_BASE_URL=https://...     # External API URL
LOG_LEVEL=debug              # Logging level

๐Ÿš€ Deployment

Production Build

npm run build
NODE_ENV=production node dist/httpServer.js

Docker Support

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY dist/ ./dist/
EXPOSE 3001
CMD ["node", "dist/httpServer.js"]

๐Ÿ”ง Extension Guide

Adding New Resources

  1. Create Domain Model (src/domain/)
  2. Implement Service (src/application/services/)
  3. Add Controller (src/adapters/controllers/)
  4. Register Route (src/adapters/routes/)
  5. Add Validation (src/adapters/validators/)
  6. Write Tests (tests/)

Adding New Tools

  1. Implement Service Logic (src/application/services/)
  2. Create MCP Handler (src/application/tools/)
  3. Add HTTP Controller (src/adapters/controllers/)
  4. Define Validation Schema (src/adapters/validators/)
  5. Register Routes (src/adapters/routes/)

Custom Implementations

// Example: Custom HTTP Client
export class CustomHttpClient implements IHttpClient {
  async fetch(url: string, options?: any): Promise<any> {
    // Your implementation
  }
}

// Register in ServiceContainer
const customClient = new CustomHttpClient();
container.register('httpClient', customClient);

๐Ÿ“Š Monitoring & Logging

Log Levels

  • DEBUG: Detailed execution information
  • INFO: General operational messages
  • WARN: Warning conditions
  • ERROR: Error conditions

Sample Log Output

[INFO] Successfully fetched 10 users
[DEBUG] Processing GET /api/users request
[WARN] Invalid query parameters for comments request
[ERROR] Failed to fetch posts: HTTP 500

๐Ÿค Contributing

  1. Code Style: Follow existing TypeScript/ESLint configuration
  2. Testing: Maintain 100% test coverage
  3. Documentation: Update README for new features
  4. Architecture: Follow SOLID principles and Clean Architecture
  5. Commits: Use conventional commit messages

๐Ÿ“‹ Development Checklist

  • SOLID Compliance: Each class has single responsibility
  • Interface Segregation: No fat interfaces
  • Dependency Injection: Use ServiceContainer
  • Type Safety: Strict TypeScript mode
  • Input Validation: Zod schemas for all inputs
  • Error Handling: Proper error propagation
  • Logging: Structured logging throughout
  • Testing: Unit + integration tests
  • Documentation: Comments in English

๐Ÿ“š Technology Stack

Core

  • TypeScript 5.9.2 - Type-safe JavaScript
  • Node.js 18+ - Runtime environment
  • Zod 3.25.76 - Schema validation

MCP Integration

  • @modelcontextprotocol/sdk 1.17.4 - MCP protocol support

HTTP Server

  • Express 5.1.0 - Web application framework
  • CORS 2.8.5 - Cross-origin resource sharing

Testing

  • Jest 30.0.5 - Testing framework
  • Supertest 7.1.4 - HTTP integration testing
  • ts-jest 29.4.1 - TypeScript support for Jest

Development Tools

  • ESLint 9.34.0 - Code linting
  • Prettier 3.6.2 - Code formatting
  • TypeScript ESLint - TypeScript-specific linting

๐Ÿ‘ค Author

Jaury Abreu
๐Ÿ“ง abreuj03@thryv.com
๐Ÿข Thryv, Inc.

๐Ÿ“„ License

ISC License - see LICENSE file for details.

๐Ÿ”„ Version History

v1.0.0

  • โœ… Initial release with SOLID architecture
  • โœ… Clean Architecture implementation
  • โœ… Dual MCP/HTTP access modes
  • โœ… Comprehensive test coverage
  • โœ… Full TypeScript implementation
  • โœ… Dependency injection container
  • โœ… Structured logging system