msanvarov/nest-rest-mongo-boilerplate
Tentang
π± backend with nest (typescript), mongoose, and authentication
Keamanan dan privasiPertumbuhan bintang terukur
- dalam 24 jam
- 0
- dalam 7 hari
- 0
- dalam 15 hari
- 0
- dalam 1 bulan
- Mengumpulkan riwayat
Perubahan bersih pada periode yang tumpang tindih, bukan total harian. Pengukuran yang belum tersedia dibiarkan kosong. Bintang menunjukkan perhatian, bukan kualitas.
README.md
AsliAPI Starter (Mongo)
A progressive Node.js framework for building efficient and scalable server-side applications, paired with Mongoose and a built-in Model Context Protocol (MCP) module for AI workflows.
Table of Contents
- Description
- Stack
- Prerequisites
- Deployment
- Environment Configuration
- MCP Module
- Web Framework
- Testing
- TypeDocs
- Open API
- Mongoose
- Logs
- Contribution
Description
This boilerplate is made to quickly prototype backend applications. It comes with authentication, authorization, logging, CRUD features, MongoDB persistence via Mongoose, and a built-in Model Context Protocol server + client out of the box.
Stack
| Layer | Tech |
|---|---|
| Runtime | Node.js 20+ |
| API | NestJS 11 (Fastify adapter), Mongoose 8, Passport JWT, nest-access-control, Swagger |
| AI / MCP | @modelcontextprotocol/sdk 1.x (server + client over Streamable HTTP) |
| Database | MongoDB (any 6+ release) |
| Tooling | TypeScript 5.6, ESLint 9, Prettier 3, Jest 29 |
Prerequisites
Non-Docker
- Node.js 20+.
- A local or hosted MongoDB instance (Community edition, or Atlas).
Docker
- Docker Desktop running locally.
Deployment
Manual Deployment without Docker
- Clone the repo:
git clone https://github.com/msanvarov/nest-rest-mongo-boilerplate. - Install dependencies:
npm install. - Copy the env template:
cp .env.example .envand fill in valid values. - Start the app in development mode:
npm run start:dev(served on http://localhost:9000).
Deploying with Docker
docker compose up -d
This brings up the API on http://localhost:9000 and MongoDB on the standard 27017 port.
Environment Configuration
By default, the application reads every env var from .env.
- APP_ENV β
developmentorproduction. Drives logging. - APP_URL β base URL for the application (used by the demo
GET /handler). - PORT β HTTP port; defaults to 9000.
- WEBTOKEN_SECRET_KEY β random secret used to sign JWTs.
- WEBTOKEN_EXPIRATION_TIME β seconds before a JWT expires (default 2400).
- DB_URL β MongoDB connection string.
- MCP_SERVER_NAME / MCP_SERVER_VERSION β identity reported by the local MCP server.
- MCP_CLIENT_REMOTES β comma-separated list of external MCP server endpoints to consume as a client (Streamable HTTP URLs).
- MCP_ALLOWED_MODELS β comma-separated whitelist of mongoose model names the MCP tools are allowed to query. Defaults to
Profile.
MCP Module
This project ships with a first-class Model Context Protocol integration so the API can both expose itself to AI clients and consume other MCP servers.
What's included
| Piece | Path | Role |
|---|---|---|
McpServerService |
src/modules/mcp/mcp-server.service.ts |
In-process MCP server with mongo-aware tools and resources. |
McpClientService |
src/modules/mcp/mcp-client.service.ts |
Connects to every endpoint in MCP_CLIENT_REMOTES and mirrors their inventory. |
McpGatewayService |
src/modules/mcp/mcp-gateway.service.ts |
Unifies the local + remote inventory, routes invocations, runs the chat preview. |
McpController |
src/modules/mcp/mcp.controller.ts |
REST surface plus the native Streamable HTTP transport. |
Mongo-aware tools
| Tool | Purpose |
|---|---|
list-collections |
List every mongoose model the server is allowed to inspect (controlled by MCP_ALLOWED_MODELS). |
count-documents |
Count documents in an allowed model that match a filter. |
find-documents |
Run a find() on an allowed model with optional filter/projection. Limit clamped to 100. |
aggregate-pipeline |
Run an aggregation pipeline against an allowed model. A trailing $limit 100 stage is appended. |
get-profile |
Convenience wrapper to look up a profile by username. |
Resources: mcp://nest/schema/profile, mcp://nest/server-info.
The whitelist prevents arbitrary collection access. Edit
MCP_ALLOWED_MODELSto opt new models in.
Endpoints
| Method | Path | Purpose |
|---|---|---|
| GET | /api/mcp/inventory |
Returns servers + tools + resources + prompts as JSON. |
| POST | /api/mcp/invoke |
Body: { serverId, name, arguments } β invokes a tool. |
| GET | /api/mcp/resource |
Query: serverId, uri β reads a resource. |
| POST | /api/mcp/chat |
Body: { message, toolCall?, history? } β chat preview entry point. |
| ANY | /api/mcp/transport |
Native MCP Streamable HTTP endpoint for AI clients. |
Connecting an external MCP client
Point a Streamable-HTTP-aware MCP client (Claude Desktop, Claude Code, mcp-cli, etc.) at http://localhost:9000/api/mcp/transport. The server advertises MCP_SERVER_NAME@MCP_SERVER_VERSION and exposes the registered tools and resources.
Consuming external MCP servers
Set MCP_CLIENT_REMOTES to a comma-separated list of Streamable HTTP URLs. On startup the API connects to each, mirrors their inventory into /api/mcp/inventory, and routes invocations through the same /api/mcp/invoke and /api/mcp/chat endpoints.
Web Framework
The API runs on Fastify via @nestjs/platform-fastify. The previous version of this boilerplate shipped both Express and Fastify adapters in dependencies; this revision drops the Express path since the modern @fastify/* plugins cover the same surface with better performance.
Testing
Docker
docker exec -it nest-mongo-api npm run test
Non-Docker
npm run test
npm run test:e2e
npm run test:cov
TypeDocs
npm run typedocs builds API documentation into the docs/ folder and serves it locally on http://localhost:8080.
Open API
Swagger UI is served at /api/docs. Configuration lives in src/main.ts.
Mongoose
Connection setup is in src/modules/app/app.module.ts. The Profile model lives in src/modules/profile/profile.model.ts. To add a model, define the schema, register it via MongooseModule.forFeature, and add the model name to MCP_ALLOWED_MODELS if you want it accessible to MCP clients.
Logs
Winston is wired up via nest-winston. In development the API logs to the console with timestamps; in production it adds a JSON error file at logs/error.log and a daily-rotated archive under logs/application-YYYY-MM-DD.log.
Contribution
PRs are appreciated.
License
Nest is MIT licensed.
Jelajahi proyek terkait
Memuatβ¦