数学运算服务器

一个基于HTTP传输的简单MCP服务器,提供基础数学运算功能,支持API密钥认证和跨域请求。

  • by Tejasf12
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 tejasf12/mcp-math-server-tejas

使用说明

MCP数学服务器

一个简单的模型上下文协议(MCP)服务器,具有HTTP传输,提供基本的数学运算。该服务器演示如何使用Express.js、TypeScript和API密钥身份验证构建MCP服务器。

特性

  • ✅ HTTP传输(非stdio)
  • ✅ API通过Bearer令牌进行密钥认证
  • ✅ 单一数学工具: add_numbers
  • ✅ 具有正确类型定义的TypeScript
  • ✅ 为跨源请求启用CORS
  • ✅ 全面的错误处理
  • ✅ 请求/响应日志记录
  • ✅ 健康检查端点

先决条件

  • Node.js 18+和npm
  • 基本了解REST API和HTTP请求

快速开始

  1. 克隆存储库

    git clone <repository-url>
    cd mcp-math-server
    
  2. 安装依赖项

    npm install
    
  3. 设置环境变量

    cp .env.example .env
    

    编辑 .env 并设置您的API密钥:

    MCP_API_KEY=your-secret-api-key-here
    PORT=3000
    
  4. 构建项目

    npm run build
    
  5. 启动服务器

    npm start
    

    服务器将在以下时间可用 http://localhost:3000

发展

对于在文件更改时自动重启的开发:

npm run dev

API终点

健康检查

GET /health

返回服务器状态(不需要身份验证)。

答复:

{
  "status": "healthy",
  "timestamp": "2024-01-01T00:00:00.000Z"
}

列出可用工具

GET /mcp/tools
Authorization: Bearer <your-api-key>

答复:

{
  "tools": [
    {
      "name": "add_numbers",
      "description": "Add two numbers together",
      "inputSchema": {
        "type": "object",
        "properties": {
          "a": {
            "type": "number",
            "description": "First number to add"
          },
          "b": {
            "type": "number",
            "description": "Second number to add"
          }
        },
        "required": ["a", "b"]
      }
    }
  ]
}

执行工具

POST /mcp/tools/call
Authorization: Bearer <your-api-key>
Content-Type: application/json

请求正文:

{
  "name": "add_numbers",
  "arguments": {
    "a": 5,
    "b": 3
  }
}

答复:

{
  "content": [
    {
      "type": "text",
      "text": "{\"result\": 8}"
    }
  ]
}

认证

所有MCP端点(除 /health)需要使用Bearer令牌进行身份验证:

Authorization: Bearer your-secret-api-key-here

如果身份验证失败,服务器将返回:

{
  "error": "Missing or invalid Authorization header"
}

可用工具

add_numbers

将两个数字相加并返回结果。

参数:

  • a (数字,必填):要添加的第一个数字
  • b (数字,必填):要添加的第二个数字

示例用法:

curl -X POST http://localhost:3000/mcp/tools/call \
  -H "Authorization: Bearer your-secret-api-key-here" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "add_numbers",
    "arguments": {
      "a": 10,
      "b": 20
    }
  }'

答复:

{
  "content": [
    {
      "type": "text",
      "text": "{\"result\": 30}"
    }
  ]
}

错误处理

服务器提供全面的错误处理:

  • 400错误请求:缺少必需的参数
  • 401未经授权:API密钥无效或丢失
  • 404未找到:未知端点
  • 500内部服务器错误:服务器错误

项目结构

mcp-math-server/
├── package.json          # Dependencies and scripts
├── tsconfig.json         # TypeScript configuration
├── .env.example          # Environment variables template
├── .gitignore            # Git ignore rules
├── README.md             # This file
├── src/
│   ├── index.ts          # Main server file
│   └── tools/
│       └── math.ts       # Math tools implementation
└── dist/                 # Compiled JavaScript (generated)

脚本

  • npm run build -将TypeScript编译为JavaScript
  • npm start -启动已编译的服务器
  • npm run dev -使用自动重新加载启动开发服务器
  • npm run clean -删除已编译的文件

安全说明

  • 在生产过程中始终使用随机生成的强API密钥
  • API密钥应保密,不得用于版本控制
  • 在生产环境中使用HTTPS
  • 考虑对生产使用实施限速

测试服务器

您可以使用curl、Postman或任何HTTP客户端测试服务器:

# Health check (no auth required)
curl http://localhost:3000/health

# List tools
curl -H "Authorization: Bearer your-secret-api-key-here" \
     http://localhost:3000/mcp/tools

# Call add_numbers tool
curl -X POST http://localhost:3000/mcp/tools/call \
     -H "Authorization: Bearer your-secret-api-key-here" \
     -H "Content-Type: application/json" \
     -d '{"name": "add_numbers", "arguments": {"a": 15, "b": 25}}'

扩展服务器

要添加新工具,请执行以下操作:

  1. 在中创建新的工具定义 src/tools/ 或添加到 math.ts
  2. 在中注册该工具 ListToolsRequestSchema 处理器
  3. 在中添加工具处理程序 CallToolRequestSchema 处理器
  4. 重建并重新启动服务器

许可证

MIT许可证-有关详细信息,请参阅许可证文件。

贡献

  1. 分叉存储库
  2. 创建要素分支
  3. 进行更改
  4. 如果适用,添加测试
  5. 提交拉取请求

支持

如果您遇到任何问题或有疑问,请在GitHub存储库中打开问题。