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 usersGET /api/posts
- Fetch all postsGET /api/comments?postId=<id>
- Fetch comments (with optional filtering)
๐ง Tools
POST /api/calculate
- Mathematical operationsPOST /api/temperature
- Temperature conversion
๐ Documentation
GET /health
- Health checkGET /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
- Create Domain Model (
src/domain/
) - Implement Service (
src/application/services/
) - Add Controller (
src/adapters/controllers/
) - Register Route (
src/adapters/routes/
) - Add Validation (
src/adapters/validators/
) - Write Tests (
tests/
)
Adding New Tools
- Implement Service Logic (
src/application/services/
) - Create MCP Handler (
src/application/tools/
) - Add HTTP Controller (
src/adapters/controllers/
) - Define Validation Schema (
src/adapters/validators/
) - 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
- Code Style: Follow existing TypeScript/ESLint configuration
- Testing: Maintain 100% test coverage
- Documentation: Update README for new features
- Architecture: Follow SOLID principles and Clean Architecture
- 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