模型上下文协议测试服务器

一个用于测试和演示的模型上下文协议服务器实现,提供BMI计算、天气服务和回声工具等功能。

  • by nabadeep25
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 nabadeep25/mcp-server-nodejs

使用说明

MCP测试服务器

一种模型上下文协议(MCP)服务器实现,具有用于测试和演示的多种工具。

概述

此服务器提供三个主要工具:

  • 体重指数计算器:根据体重和身高计算体重指数
  • 气象局:获取任何城市的天气信息(使用API关键支持)
  • 回声工具:用于测试的简单回声功能

特性

  • 使用TypeScript和Express.js构建
  • MCP的流式HTTP传输
  • 基于环境的配置
  • 热重载开发支持
  • 模块化工具架构

先决条件

  • Node.js(v18或更高版本)
  • npm或yarn包管理器

安装

  1. 克隆或下载此存储库

  2. 安装依赖项:

    npm install
    
  3. 复制环境示例文件:

    cp .env.example .env
    
  4. (可选)将天气API密钥添加到 .env:

    WEATHER_API_KEY=your_openweathermap_api_key_here
    

构建并运行

发展模式

# Build and start with hot reload
npm run dev

生产模式

# Build the project
npm run build

# Start the server
npm start

服务器将于启动 http://localhost:3000 (或您指定的端口 PORT 环境变量)。

API端点

MCP服务器可在以下位置访问:

POST http://localhost:3000/mcp

可用工具

1.BMI计算器(calculate-bmi)

根据体重和身高计算体重指数。

参数:

  • weightKg (数量):重量(公斤)
  • heightM (数字):高度(米)

2.气象局(get-weather)

获取一个城市的当前天气信息。

参数:

  • city (string):城市名称
  • country (字符串,可选):国家代码(例如“IN”、“US”)

3.回声工具(echo)

用于测试目的的简单回声功能。

参数:

  • message (string):要回显的消息

测试服务器

使用MCP检查器

对于用于测试MCP服务器的GUI界面:

  1. 启动服务器:

    npm start
    
  2. 在另一个终端中,运行MCP检查器:

    npm run inspector
    

VS代码集成

如果您使用的是支持MCP的VS Code,请将此服务器添加到您的设置中:

{
  "mcp": {
    "servers": {
      "mcp-test-server": {
        "url": "http://localhost:3000/mcp"
      }
    }
  }
}

项目结构

├── src/
│   ├── index.ts          # Main server file
│   └── tools/            # Tool implementations
│       ├── bmi.ts        # BMI calculator tool
│       ├── weather.ts    # Weather service tool
│       └── echo.ts       # Echo tool
├── build/                # Compiled JavaScript files
├── package.json          # Project configuration
├── tsconfig.json         # TypeScript configuration
├── .env.example          # Environment variables example
└── README.md             # This file

发展

添加新工具

  1. 在中创建新的工具文件 src/tools/:

    import { CallToolResult } from '@modelcontextprotocol/sdk/types.js';
    import { z } from 'zod';
    
    export const myTool = {
      name: 'my-tool',
      schema: {
        param1: z.string(),
        param2: z.number(),
      },
      handler: async ({ param1, param2 }: { param1: string; param2: number }): Promise<CallToolResult> => {
        return {
          content: [
            {
              type: 'text',
              text: `Result: ${param1} - ${param2}`,
            },
          ],
        };
      },
    };
    
  2. 导入并注册 src/index.ts:

    import { myTool } from './tools/my-tool.js';
    
    // In the getServer() function:
    server.tool(
      myTool.name,
      'Description of my tool',
      myTool.schema,
      myTool.handler
    );
    

可用脚本

  • npm run build -将TypeScript编译为JavaScript
  • npm run watch -注意更改并重新编译
  • npm run dev -热装开发模式
  • npm start -启动已编译的服务器
  • npm run inspector -启动MCP检查器