A robust Node.js/TypeScript backend API server that acts as a gateway between web applications and Model Context Protocol (MCP) servers. This service provides AI-powered functionality through OpenAI integration and seamless MCP tool execution capabilities.
- MCP Integration: Full Model Context Protocol client implementation with automatic reconnection
- AI Services: OpenAI integration for intelligent message processing and tool orchestration
- RESTful API: Clean, documented REST endpoints for AI and MCP operations
- Security: Built-in security middleware including CORS, Helmet, and rate limiting
- Logging: Comprehensive logging with Winston for monitoring and debugging
- TypeScript: Full TypeScript support with strict type checking
- Error Handling: Robust error handling and graceful shutdown procedures
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β Web Client βββββΆβ MCP Gateway βββββΆβ MCP Server β
β β β (This Service) β β β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β OpenAI API β
β β
ββββββββββββββββββββ
src/
βββ controllers/ # Request handlers
β βββ aiController.ts # AI-related operations
β βββ mcpController.ts # MCP tool operations
βββ middleware/ # Express middleware
β βββ errorHandler.ts # Error handling
β βββ validation.ts # Request validation
β βββ rate_limit.ts # Rate limiting
βββ routes/ # API route definitions
β βββ aiRoutes.ts # AI endpoints
β βββ mcpRoutes.ts # MCP endpoints
βββ services/ # Business logic
β βββ mcpProxyService.ts # MCP client management
β βββ openaiService.ts # OpenAI integration
β βββ toolRegistryService.ts # Tool synchronization
βββ types/ # TypeScript type definitions
βββ utils/ # Utility functions
β βββ logger.ts # Logging configuration
βββ app.ts # Main application entry point
- Node.js >= 18.0.0
- npm or yarn package manager
- Access to OpenAI API
- MCP server running and accessible
-
Clone the repository
git clone https://github.com/OneWhistle/dowhistle-mcp-gateway cd dowhistle-mcp-gateway
-
Install dependencies
npm install
-
Environment Configuration Create a
.env
file in the root directory:# Server Configuration PORT=3001 NODE_ENV=development # OpenAI Configuration OPENAI_API_KEY=your_openai_api_key_here OPENAI_MODEL=gpt-4o-mini OPENAI_TEMPERATURE=0.7 # MCP Server Configuration MCP_SERVER_URL=http://localhost:8000 # CORS Configuration ALLOWED_ORIGINS=http://localhost:5173,http://localhost:8080,http://localhost:3000
For local development with hot-reloading:
-
Ensure your
.env
file is configured (e.g.,OPENAI_API_KEY
,MCP_SERVER_URL
,ALLOWED_ORIGINS
). -
Start the development service (this will use
Dockerfile.dev
)docker-compose -f docker-compose.yml -f docker-compose.override.yml up --build
This command will build the
dev
image and start themcp-gateway-dev
container with hot-reloading. -
Stop the development service
docker-compose -f docker-compose.yml -f docker-compose.override.yml down
For production deployment:
-
Ensure your
.env
file is configured with the necessary environment variables (e.g.,OPENAI_API_KEY
,MCP_SERVER_URL
,ALLOWED_ORIGINS
). -
Start the production service
docker-compose up --build -d
This will build the
prod
image and start themcp-gateway
service in detached mode. -
Stop the service
docker-compose down
npm run lint # Run ESLint
npm test # Run tests
GET /health
- Server health status
POST /api/ai/process-message
- Process user messages with AI
POST /api/mcp/connect
- Connect to MCP serverPOST /api/mcp/disconnect
- Disconnect from MCP serverGET /api/mcp/status
- Get MCP connection statusGET /api/mcp/health
- MCP health checkGET /api/mcp/tools
- List available MCP toolsGET /api/mcp/resources
- Get MCP resourcesPOST /api/mcp/tools/:toolName
- Execute specific MCP tool
Variable | Description | Default | Required |
---|---|---|---|
PORT |
Server port | 3001 | No |
NODE_ENV |
Environment mode | development | No |
OPENAI_API_KEY |
OpenAI API key | - | Yes |
OPENAI_MODEL |
OpenAI model to use | gpt-4o-mini | No |
OPENAI_TEMPERATURE |
AI response creativity | 0.7 | No |
MCP_SERVER_URL |
MCP server URL | - | Yes |
ALLOWED_ORIGINS |
CORS allowed origins | localhost variants | No |
- AI endpoints: 50 requests per 15 minutes per IP
- MCP endpoints: 100 requests per 15 minutes per IP
- Helmet: Security headers and CSP configuration
- CORS: Configurable cross-origin resource sharing
- Rate Limiting: Protection against abuse
- Input Validation: Request payload validation
- Error Handling: Secure error responses
The service uses Winston for logging with the following levels:
- Error: Application errors and failures
- Warn: Warning conditions
- Info: General information and requests
- Debug: Detailed debugging information
Logs are written to both console and log files in the logs/
directory.
npm test
Tests are configured with Jest and cover the main functionality of the service.
The service can be containerized and deployed using Docker and Docker Compose.
-
Build the Docker image
docker build -t dowhistle-mcp-gateway .
-
Run the Docker container
docker run -p 3001:3001 dowhistle-mcp-gateway
-
Ensure your
.env
file is configured with the necessary environment variables (e.g.,OPENAI_API_KEY
,MCP_SERVER_URL
,ALLOWED_ORIGINS
). Docker Compose will pick these up. -
Start the service using Docker Compose
docker-compose up -d
This will build the image (if not already built) and start the
mcp-gateway
service in detached mode. -
Stop the service
docker-compose down
- TypeScript with strict mode enabled
- ESLint for code quality
- Prettier for code formatting
- Create new service classes in
src/services/
- Add controllers in
src/controllers/
- Define routes in
src/routes/
- Update types in
src/types/
- Add validation middleware if needed
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For support and questions:
- Create an issue in the repository
- Check the logs for debugging information
- Verify environment configuration
- Initial release
- MCP client integration
- OpenAI service integration
- RESTful API endpoints
- Security middleware
- Comprehensive logging