A production-ready Model Context Protocol (MCP) server template that connects to any OpenAI-compatible API backend. Build AI agent tools in minutes, not hours.
The Model Context Protocol is the 2026 standard for connecting AI models to external tools and data sources. This template gives you a battle-tested starting point for building MCP servers that work with any OpenAI-compatible API β including XiDao, OpenRouter, Together AI, LiteLLM, and self-hosted vLLM/Ollama instances.
- π OpenAI-Compatible β Works with any provider via
base_urlswitching - π οΈ Pre-built Tools β File operations, web search, code execution, database queries
- π Authentication β API key, OAuth, and custom auth middleware
- π Observability β Built-in OpenTelemetry tracing and structured logging
- π§ͺ Test Suite β Comprehensive tests with mock backends
- π³ Docker Ready β One-command deployment with Docker Compose
- π Type Safety β Full TypeScript type definitions for all tool schemas
- π Hot Reload β Development mode with automatic restart on file changes
git clone https://github.com/XidaoApi/mcp-server-template.git
cd mcp-server-template
npm installcp .env.example .env
# Edit .env with your API settings:
# OPENAI_API_KEY=***
# OPENAI_BASE_URL=https://api.xidao.online/v1 # or any OpenAI-compatible endpoint
# OPENAI_MODEL=gpt-5# Development (with hot reload)
npm run dev
# Production
npm run build && npm start
# Docker
docker compose up -dmcp-server-template/
βββ src/
β βββ server.ts # MCP server entry point
β βββ tools/ # Tool implementations
β β βββ file-ops.ts # File read/write/search
β β βββ web-search.ts # Web search via multiple providers
β β βββ code-exec.ts # Sandboxed code execution
β β βββ db-query.ts # Database query tool
β βββ auth/ # Authentication middleware
β β βββ api-key.ts # API key validation
β β βββ oauth.ts # OAuth 2.0 flow
β βββ llm/ # LLM integration layer
β β βββ client.ts # OpenAI-compatible client
β β βββ router.ts # Multi-provider routing
β β βββ fallback.ts # Automatic failover
β βββ telemetry/ # Observability
β βββ tracing.ts # OpenTelemetry spans
β βββ metrics.ts # Prometheus metrics
βββ tests/
βββ docker-compose.yml
βββ Dockerfile
βββ package.json
| Tool | Description | Example |
|---|---|---|
read_file |
Read file contents | Read config files, source code |
write_file |
Write to files | Generate reports, save data |
search_files |
Search files by pattern | Find functions, grep logs |
web_search |
Search the web | Research topics, verify facts |
execute_code |
Run code in sandbox | Test snippets, data processing |
db_query |
Query databases | SQL, MongoDB, Redis |
http_request |
Make HTTP calls | API integrations, webhooks |
Add your own tools in src/tools/:
// src/tools/my-tool.ts
import { Tool, ToolSchema } from '@modelcontextprotocol/sdk';
export const myTool: Tool = {
name: 'my_custom_tool',
description: 'Does something useful',
inputSchema: {
type: 'object',
properties: {
query: { type: 'string', description: 'Input query' },
},
required: ['query'],
},
async execute(params) {
// Your implementation
return { result: `Processed: ${params.query}` };
},
};Register it in src/server.ts:
import { myTool } from './tools/my-tool';
server.addTool(myTool);The template includes a built-in router for using multiple LLM providers:
// src/llm/router.ts
const router = new LLMRouter({
providers: [
{ name: 'xidao', baseUrl: 'https://api.xidao.online/v1', priority: 1 },
{ name: 'openai', baseUrl: 'https://api.openai.com/v1', priority: 2 },
{ name: 'ollama', baseUrl: 'http://localhost:11434/v1', priority: 3 },
],
strategy: 'cost-optimized', // or 'latency-optimized', 'round-robin'
fallback: true,
});| Variable | Description | Default |
|---|---|---|
OPENAI_API_KEY |
API key for the LLM provider | Required |
OPENAI_BASE_URL |
OpenAI-compatible API endpoint | https://api.openai.com/v1 |
OPENAI_MODEL |
Model to use | gpt-5 |
MCP_PORT |
Server port | 3000 |
MCP_LOG_LEVEL |
Logging level | info |
OTEL_ENDPOINT |
OpenTelemetry collector URL | β |
AUTH_MODE |
Authentication mode | api-key |
services:
mcp-server:
build: .
ports:
- "3000:3000"
environment:
- OPENAI_API_KEY=***
- OPENAI_BASE_URL=${OPENAI_BASE_URL}
restart: unless-stoppedkubectl apply -f k8s/See k8s/ directory for manifests.
# Run all tests
npm test
# Run with coverage
npm run test:coverage
# Run specific test suite
npm test -- --grep "file-ops"- MCP Specification β The official MCP docs
- MCP SDK β TypeScript SDK
- XiDao API Gateway β OpenAI-compatible API gateway with multi-provider routing, failover, and cost optimization
MIT β see LICENSE for details.