Sponsored by Deepsite.site

Multi-tenant MCP Server

Created By
brianintegrationapp8 months ago
Multi-tenant service that allows MCP Clients to connect to Integration App's MCP Server
Content

Multi-tenant MCP Server

A multi-tenant Model Context Protocol (MCP) server that enables multiple tenants to connect to Integration App's MCP server with their own credentials.

Features

  • Multi-tenant support with isolated connections
  • Database persistence for tenant information
  • Live tool discovery from Integration App
  • Audit logging
  • Rate limiting and security middleware
  • Health check endpoint
  • Graceful shutdown handling

Prerequisites

  • Node.js >= 18.0.0
  • PostgreSQL database
  • Integration App account and credentials

Environment Variables

Create a .env file with the following variables:

# Server Configuration
PORT=3000
NODE_ENV=development

# Database
DATABASE_URL="postgresql://user:password@localhost:5432/mcp_service"

# Security
ADMIN_API_KEY="your-admin-api-key"

Installation

  1. Clone the repository:
git clone <repository-url>
cd mcp-service
  1. Install dependencies:
npm install
  1. Run database migrations:
npm run migrate
  1. Start the server:
npm start

For development:

npm run dev

API Endpoints

Health Check

GET /health

Create Tenant

POST /api/tenants
Authorization: Bearer <admin-api-key>

{
  "integrationKey": "hubspot|notion|etc",
  "integrationAppToken": "your-integration-app-token"
}

List Tools

POST /api/tenant/:tenantId/tool
Authorization: Bearer <tenant-token>

{
  "method": "listTools"
}

Execute Tool

POST /api/tenant/:tenantId/tool
Authorization: Bearer <tenant-token>

{
  "method": "tool-name",
  "params": {
    // tool parameters
  }
}

Deployment

Heroku

  1. Create a new Heroku app:
heroku create
  1. Add PostgreSQL addon:
heroku addons:create heroku-postgresql:hobby-dev
  1. Set environment variables:
heroku config:set NODE_ENV=production
heroku config:set ADMIN_API_KEY=your-admin-api-key
  1. Deploy:
git push heroku main

Docker

  1. Build the image:
docker build -t mcp-service .
  1. Run the container:
docker run -p 3000:3000 \
  -e DATABASE_URL=postgresql://user:password@host.docker.internal:5432/mcp_service \
  -e ADMIN_API_KEY=your-admin-api-key \
  mcp-service

Development

Database Migrations

Create a new migration:

npx prisma migrate dev --name migration-name

Apply migrations:

npm run migrate

Testing

Run tests:

npm test

Run linter:

npm run lint

Architecture

The server acts as a multi-tenant proxy between MCP clients and Integration App's MCP server:

MCP Client -> Multi-tenant Server -> Integration App MCP Server
                (proxy/router)

Each tenant gets:

  • Isolated MCP client connection
  • Separate Integration App credentials
  • Independent tool discovery
  • Audit logging

Security

  • Rate limiting per tenant
  • CORS protection
  • Helmet security headers
  • Request ID tracking
  • Audit logging
  • Token-based authentication

Monitoring

The /health endpoint provides:

  • Server status
  • Active tenant count
  • Database connection status
  • Memory usage
  • Uptime

License

MIT

MCP Service Documentation

Overview

This service provides a multi-tenant API for managing integrations and tools via the Model Context Protocol (MCP). Each tenant is isolated and can access only their own tools and data.


1. Creating a Tenant

To create a new tenant, you must have an admin token. Use the following steps:

Step 1: Obtain an Admin Token

Send a POST request to /api/auth/token with your admin API key:

curl -X POST https://<your-app-url>/api/auth/token \
  -H "Content-Type: application/json" \
  -d '{"key": "<ADMIN_API_KEY>"}'

Response:

{"token": "<ADMIN_JWT_TOKEN>"}

Step 2: Create the Tenant

Send a POST request to /api/tenants with the admin token in the Authorization header:

curl -X POST https://<your-app-url>/api/tenants \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <ADMIN_JWT_TOKEN>" \
  -d '{
    "name": "<Tenant Name>",
    "integrationKey": "<integration-key>",
    "integrationAppToken": "<integration-app-token>"
  }'

Response:

{
  "tenantId": "tenant-xxxxxxx",
  "apiKey": "<TENANT_API_KEY>",
  ...
}

2. Generating a Tenant Token

To interact with the API as a tenant, you need a tenant JWT token. Generate it using the tenant's API key:

curl -X POST https://<your-app-url>/api/auth/tenant-token \
  -H "Content-Type: application/json" \
  -H "x-api-key: <TENANT_API_KEY>"

Response:

{"token": "<TENANT_JWT_TOKEN>"}

3. Listing Tools for a Tenant

To list the tools available to a tenant, use the following endpoint with both the tenant API key and JWT token:

curl -X POST https://<your-app-url>/api/tenant/<tenantId>/tool \
  -H "Content-Type: application/json" \
  -H "x-api-key: <TENANT_API_KEY>" \
  -H "Authorization: Bearer <TENANT_JWT_TOKEN>" \
  -d '{"method": "listTools"}'

Response:

{
  "tools": [
    { "name": "create-contact", ... },
    ...
  ]
}

4. Executing a Tool as a Tenant

To execute a tool (e.g., create a contact):

curl -X POST https://<your-app-url>/api/tenant/<tenantId>/tool \
  -H "Content-Type: application/json" \
  -H "x-api-key: <TENANT_API_KEY>" \
  -H "Authorization: Bearer <TENANT_JWT_TOKEN>" \
  -d '{
    "method": "create-contact",
    "params": {
      "email": "test.user@example.com",
      "fullName": "Test User"
    }
  }'

Response:

{
  "content": [
    { "type": "text", "text": "{\"id\":\"120688589560\"}" }
  ],
  "isError": false
}

5. Connecting an MCP Client Application

To connect an MCP client as a tenant:

  • Use the tenant's integrationKey and integrationAppToken (from the tenant creation response) to initialize the client.
  • Authenticate API requests with both the tenant API key and the tenant JWT token as shown above.

Example Client Initialization:

const client = new IntegrationAppClient({
  token: '<integrationAppToken>'
});
const integration = await client.integration('<integrationKey>').get();
// ...

6. Troubleshooting

  • Ensure you use the correct tenant id and not tenantId for foreign key references in the database.
  • Both the tenant API key and JWT token are required for all tenant-specific API calls.
  • If you see foreign key errors, check your database schema and migrations.
  • For rate limiting issues, consider setting app.set('trust proxy', 1) if running behind a proxy (see Express rate-limit docs).

7. Example .env File

DATABASE_URL=postgresql://user:password@host:port/dbname
ADMIN_API_KEY=your_admin_api_key

8. Useful Endpoints

  • POST /api/auth/token — Get admin token
  • POST /api/tenants — Create tenant (admin only)
  • POST /api/auth/tenant-token — Get tenant JWT token
  • POST /api/tenant/:tenantId/tool — List or execute tools as a tenant

For more details, see the code in src/server.ts and src/api/auth.ts.

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
DeepChatYour AI Partner on Desktop
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
Playwright McpPlaywright MCP server
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
ChatWiseThe second fastest AI chatbot™
CursorThe AI Code Editor
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
Amap Maps高德地图官方 MCP Server
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
WindsurfThe new purpose-built IDE to harness magic
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
Serper MCP ServerA Serper MCP Server
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
Tavily Mcp