CourtsApp MCP服务器
使用三个简单工具在TypeScript中实现模型上下文协议(MCP)服务器的示例。
快速开始
# Install dependencies
npm install
# Build the project
npm run build
# Run the server
npm start服务器将于启动 http://localhost:3000
MCP客户端URL: http://localhost:3000/mcp
工具
此MCP服务器提供三个工具:
1.get_current_time
以多种格式返回当前日期和时间。
参数: 无
示例响应:
{
"timestamp": "2025-11-04T05:46:00.000Z",
"formatted": "11/4/2025, 12:46:00 AM",
"unix": 1730698360000
}2.计算器
执行基本算术运算。
参数:
operation(string):要执行的操作-“加”、“减”、“乘”或“除”a(number):第一个数字b(number):第二个数字
请求示例:
{
"operation": "add",
"a": 5,
"b": 3
}示例响应:
{
"operation": "add",
"a": 5,
"b": 3,
"result": 8,
"expression": "5 + 3 = 8"
}3.是七
检查一个数字是偶数还是奇数。
参数:
number(number):要检查的整数
请求示例:
{
"number": 42
}示例响应:
{
"number": 42,
"isEven": true,
"type": "even"
}安装
npm install构建
npm run build运行服务器
启动HTTP服务器:
npm start或者直接使用Node运行:
node build/index.js默认情况下,服务器将在端口3000上启动。您可以使用port环境变量更改端口:
PORT=8080 npm start服务器公开以下端点:
http://localhost:3000/health-健康检查端点http://localhost:3000/mcp-MCP连接的可流式HTTP端点
发展
要监视更改并自动重建,请执行以下操作:
npm run dev连接到LLM应用程序
此MCP服务器使用 流式HTTP(SHTTP)传输,SSE的现代替代品。此传输可通过web访问,MCP客户端可以通过HTTP连接到服务器。
客户端配置示例
对于支持流式HTTP传输的MCP客户端,请配置:
{
"url": "http://localhost:3000/mcp"
}通过Nginx公开
要使用nginx将此服务器暴露到互联网,请将此配置添加到nginx配置中:
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}然后,客户端可以连接到: http://your-domain.com/mcp
项目结构
CourtsAppMCPServer/
├── src/
│ └── index.ts # Main server implementation
├── build/ # Compiled JavaScript output
├── package.json # Project dependencies
├── tsconfig.json # TypeScript configuration
└── README.md # This file依赖项
@modelcontextprotocol/sdk:TypeScript官方MCP SDKexpress:用于流式HTTP传输的Web服务器框架cors:用于跨源请求的CORS中间件typescript:TypeScript编译器@types/node:Node.js类型定义
部署
部署到VPS/云服务器
# 1. Clone the repository
git clone
cd CourtsAppMCPServer
# 2. Install dependencies
npm install
# 3. Build the project
npm run build
# 4. Install PM2 to keep the server running
npm install -g pm2
# 5. Start with PM2
pm2 start build/index.js --name mcp-server
# 6. Make it restart on reboot
pm2 startup
pm2 save自定义端口
PORT=8080 npm start与ngrok一起使用(用于测试)
# Start the server
npm start
# In another terminal, expose it
ngrok http 3000然后使用ngrok URL+ /mcp 对于您的MCP客户: https://your-ngrok-url.ngrok.io/mcp
许可证
麻省理工学院
