MCP Server Template
A production-ready template for building Model Context Protocol servers using TypeScript and Bun runtime, including schema validation, structured logging, and retry utilities.
README
MCP Server Template
Production-ready MCP server template for building Model Context Protocol servers with TypeScript and Bun runtime.
What is this?
This is a template project for creating MCP servers. It includes:
- TypeScript with strict type checking
- Bun runtime for fast execution
- Zod schema validation
- Structured logging (stderr only)
- Retry utilities with exponential backoff
- Unit and integration test setup
- Production-ready error handling
Installation
bun install
Quick Start
# Start the MCP server
bun run start
# Development with hot reload
bun run dev
MCP Configuration
Add to your MCP client configuration:
{
"mcpServers": {
"mcp-server-template": {
"command": "bun",
"args": ["run", "/path/to/mcp-server-template/src/server.ts"]
}
}
}
Or use the built version:
{
"mcpServers": {
"mcp-server-template": {
"command": "node",
"args": ["/path/to/mcp-server-template/dist/server.js"]
}
}
}
Adding Tools
1. Define Schema
In src/types/index.ts:
import { z } from "zod";
export const MyToolInputSchema = z.object({
param1: z.string().min(1).describe("Parameter description"),
param2: z.number().optional().describe("Optional parameter"),
});
export type MyToolInput = z.infer<typeof MyToolInputSchema>;
2. Add Tool Definition
In src/tools/index.ts:
export const toolDefinitions = [
{
name: "my_tool",
description: "Description of what the tool does",
inputSchema: {
type: "object" as const,
properties: {
param1: { type: "string", description: "Parameter description" },
param2: { type: "number", description: "Optional parameter" },
},
required: ["param1"],
},
},
];
3. Implement Handler
export async function handleMyTool(input: unknown): Promise<CallToolResult> {
try {
const validated = MyToolInputSchema.parse(input);
log.info('Executing tool', { tool: 'my_tool', params: validated });
// Your logic here
const result = { data: "..." };
log.info('Tool completed', { tool: 'my_tool', success: true });
return createSuccessResult(result);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
return createErrorResult(message, "my_tool");
}
}
4. Add to Router
export async function handleToolCall(name: string, args: unknown): Promise<CallToolResult> {
switch (name) {
case "my_tool":
return handleMyTool(args);
default:
return createErrorResult(`Unknown tool: ${name}`, name);
}
}
5. Write Tests
Create tests/unit/my_tool.test.ts:
import { describe, test, expect } from "bun:test";
import { handleMyTool } from "../../src/tools/index.js";
describe("My Tool", () => {
test("should work correctly", async () => {
const result = await handleMyTool({ param1: "test" });
expect(result.isError).toBe(false);
});
});
6. Document Tool
Create docs/tools/my_tool.md:
# my_tool
Description of what the tool does.
## Parameters
| Name | Type | Required | Description |
|------|------|----------|-------------|
| param1 | string | Yes | Description |
## Examples
**Input:**
\`\`\`json
{ "param1": "value" }
\`\`\`
**Output:**
\`\`\`json
{ "data": "result" }
\`\`\`
Available Scripts
| Script | Description |
|---|---|
bun run dev |
Start development server with hot reload |
bun run build |
Build for production |
bun run start |
Run production build |
bun test |
Run unit tests |
bun run test:coverage |
Run tests with coverage |
bun run test:runtime |
Run runtime testcases |
bun run lint |
Run Biome linter |
bun run lint:fix |
Auto-fix lint issues |
bun run format |
Format code with Biome |
bun run typecheck |
Type check without emit |
Configuration
Environment Variables
DEBUG=true- Enable debug logging
Troubleshooting
Server won't start
- Ensure Bun is installed:
bun --version - Check TypeScript errors:
bun run typecheck
Tool execution fails
- Check input schema matches the tool definition
- Enable debug mode:
DEBUG=true bun run start
Development
See DEVELOPMENT.md for development setup.
License
MIT License - see LICENSE for details.
推荐服务器
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 模型以安全和受控的方式获取实时的网络信息。