MCP Template
A template for building Model Context Protocol servers that allow AI assistants to interact with custom data and services through queryable resources and specialized tools.
README
MCP Template - Build Your Own AI Server
A practical template for creating Model Context Protocol (MCP) servers that enable AI assistants to interact with your data and services.
Overview
This template provides a foundation for building MCP servers - specialized services that AI assistants can connect to for accessing external data, performing operations, and extending their capabilities beyond their training data.
Key Capabilities:
- Expose data as queryable resources
- Provide custom tools for AI assistants to execute
- Handle real-time data operations (CRUD)
- Connect multiple data sources and services
Prerequisites
- Node.js 18+ and npm
- TypeScript knowledge
- Understanding of REST APIs or similar concepts
Quick Start
git clone https://github.com/rhit-bhuwalk/MCP_TEMPLATE.git
cd MCP_TEMPLATE
npm install
npm run build
npm start
This launches a server with sample user data that demonstrates core MCP functionality.
Core Concepts
Resources
Resources represent data collections that AI assistants can query. Think of them as API endpoints that return structured data.
// Register a resource
dataService.registerResource('users', 'User account information');
// AI can now query: "Show me all users" or "Find user with ID 123"
Tools
Tools are functions that AI assistants can execute to perform specific operations on your data.
// Register a tool
server.registerTool(
'create_user',
'Create a new user account',
z.object({
name: z.string(),
email: z.string().email()
}),
async (args) => {
return await dataService.create('mcp://users', args);
}
);
Implementation Guide
1. Define Your Data Structure
Start by defining the shape of your data:
interface Product {
id: string;
name: string;
price: number;
category: string;
inStock: boolean;
}
2. Register Resources
Make your data discoverable to AI assistants:
// In your server setup
dataService.registerResource('products', 'Product inventory data');
// Seed with sample data
const sampleProducts: Product[] = [
{ id: '1', name: 'Laptop', price: 999, category: 'Electronics', inStock: true },
{ id: '2', name: 'Coffee Mug', price: 15, category: 'Kitchen', inStock: false }
];
dataService.seedData('mcp://products', sampleProducts);
3. Add Custom Tools
Create specific operations for your use case:
// Inventory management tool
server.registerTool(
'update_stock_status',
'Update product stock availability',
z.object({
productId: z.string(),
inStock: z.boolean()
}),
async (args) => {
const result = await dataService.update(
'mcp://products',
args.productId,
{ inStock: args.inStock }
);
return { success: true, product: result };
}
);
// Analytics tool
server.registerTool(
'get_category_summary',
'Get inventory summary by category',
z.object({
category: z.string().optional()
}),
async (args) => {
const products = await dataService.queryResource('mcp://products', {
filter: args.category ? { category: args.category } : undefined
});
return {
totalProducts: products.length,
inStock: products.filter(p => p.inStock).length,
outOfStock: products.filter(p => !p.inStock).length,
averagePrice: products.reduce((sum, p) => sum + p.price, 0) / products.length
};
}
);
4. Connect Real Data Sources
Replace in-memory storage with your actual data:
// Example: Connect to a database
class DatabaseDataService extends DataService {
async queryResource(uri: string, query?: any) {
const resourceType = uri.split('://')[1];
switch (resourceType) {
case 'products':
return await this.db.products.findMany({
where: query?.filter || {}
});
case 'orders':
return await this.db.orders.findMany({
include: { items: true }
});
default:
throw new Error(`Unknown resource: ${resourceType}`);
}
}
}
Project Structure
src/
├── core/ # Core MCP server functionality
├── services/ # Data service implementations
├── examples/ # Example implementations
│ └── server.ts # Complete working example
└── index.ts # Main entry point
Start here: src/examples/server.ts contains a complete implementation showing all concepts in practice.
Advanced Patterns
Multi-Resource Operations
server.registerTool(
'process_order',
'Process customer order and update inventory',
z.object({
customerId: z.string(),
productIds: z.array(z.string())
}),
async (args) => {
// Check inventory
const products = await dataService.queryByIds('mcp://products', args.productIds);
// Create order
const order = await dataService.create('mcp://orders', {
customerId: args.customerId,
items: products,
total: products.reduce((sum, p) => sum + p.price, 0)
});
// Update inventory
for (const product of products) {
await dataService.update('mcp://products', product.id, {
inStock: false
});
}
return { orderId: order.id, total: order.total };
}
);
Error Handling and Validation
server.registerTool(
'safe_user_operation',
'Safely perform user operations with validation',
schema,
async (args) => {
try {
// Validate business rules
if (args.email && !isValidEmail(args.email)) {
throw new Error('Invalid email format');
}
const result = await dataService.performOperation(args);
return { success: true, data: result };
} catch (error) {
return {
success: false,
error: error.message,
code: 'VALIDATION_ERROR'
};
}
}
);
Testing Your Server
# Run tests
npm test
# Test with a real AI assistant
npm start
# Connect Claude Desktop or other MCP-compatible client
Deployment Considerations
- Authentication: Add API keys or OAuth for production use
- Rate Limiting: Implement request throttling for high-traffic scenarios
- Data Validation: Always validate inputs from AI assistants
- Logging: Add comprehensive logging for debugging and monitoring
- Error Handling: Provide clear error messages that help AI assistants understand what went wrong
Next Steps
- Examine the examples - Understand the patterns by studying
src/examples/server.ts - Adapt the data models - Replace sample data with your domain objects
- Add domain-specific tools - Create operations that match your business logic
- Connect real data sources - Integrate with databases, APIs, or file systems
- Test with AI assistants - Verify functionality with Claude, ChatGPT, or other MCP clients
This template provides the scaffolding - your domain expertise and data make it valuable.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。