天气数据服务

一个基于Model Context Protocol (MCP)的天气数据服务实现,提供标准化的天气信息访问接口。

  • by mitchallen
  • JavaScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 mitchallen/mcp-101

使用说明

MCP气象服务器

使用TypeScript构建的天气数据服务的模型上下文协议(MCP)服务器实现。

备注:此演示返回 模拟 天气数据和 非真实数据。返回实时数据对读者来说是一项练习。

🌤️ 概述

该项目使用模型上下文协议(MCP)SDK实现了一个天气服务器,通过一个定义良好的接口提供对天气信息的标准化访问。

📋 先决条件

  • Node.js (v18或更高版本)
  • npm (v8或更高)
  • TypeScript 发展的知识

🚀 快速开始

安装

# Clone the repository
git clone <your-repo-url>
cd mcp-101

# Install dependencies
npm install
# or using Makefile
make install

发展

# Start development server with hot reload
npm run dev
# or
make dev

# Run in watch mode
npm run watch
# or
make watch

生产

# Build the project
npm run build
# or
make build

# Start the server
npm start
# or
make start

# Build and start in one command
make start-prod

📦 可用脚本

NPM脚本

  • npm run build -将TypeScript编译为JavaScript
  • npm run dev -使用tsx运行开发服务器
  • npm start -启动已编译的应用程序
  • npm run watch -运行时注意文件自动重启
  • npm test -运行测试套件

Makefile命令

跑 make help 查看所有可用命令:

发展

  • make dev -使用热重新加载启动开发服务器
  • make watch -在监视模式下运行
  • make build -构建项目
  • make test -运行测试
  • make typecheck -运行TypeScript类型检查

维护

  • make clean -清理构建工件
  • make clean-deps -清理节点模块
  • make reset -完全重置(清理+重新安装+重建)
  • make verify -完整的验证流程

公用事业

  • make outdated -检查过时的软件包
  • make update -更新依赖关系
  • make package -创建可分发包

🏗️ 项目结构

mcp-101/
├── src/
│   ├── index.ts          # Main server entry point
│   └── test.ts           # Test suite
├── dist/                 # Compiled JavaScript output
├── node_modules/         # Dependencies
├── package.json          # Project configuration
├── tsconfig.json         # TypeScript configuration
├── Makefile             # Build and development commands
├── .gitignore           # Git ignore rules
└── README.md            # Project documentation

🔧 配置

TypeScript配置

该项目使用带有ES模块的TypeScript。配置已完成 tsconfig.json.

软件包配置

  • 类型:ES模块("type": "module")
  • 主入口: dist/index.js
  • 二进制: weather-server 全局安装后可用的命令

🧪 测试

# Run tests
npm test
# or
make test

# Run tests in watch mode
make test-watch

🖥️ 使用Claude Desktop(Mac)进行测试

先决条件

  1. 克劳德桌面 安装在Mac上
  2. 内置MCP服务器:运行 make build 编译您的服务器

配置步骤

  1. 构建您的服务器:

    make build
    
  2. 找到Claude桌面配置:

在Mac上,

  • Claude>设置>开发人员>编辑配置
  1. 创建或编辑配置文件:

在Finder文件窗口中,右键单击以在首选编辑器中编辑此文件:

claude_desktop_config.json
  1. 添加您的MCP服务器配置:

    {
      "mcpServers": {
        "weather-server": {
          "command": "node",
          "args": ["/absolute/path/to/your/mcp-101/dist/index.js"],
          "env": {
            "NODE_ENV": "production"
          }
        }
      }
    }
    

    重要:替换 /absolute/path/to/your/mcp-101/ 使用您的实际项目路径:

    # Get your current path
    pwd
    # Example result: /Users/username/projects/mcp/mcp-101
    
  2. 替代方案:使用npm全局安装:

    # Install globally (after building)
    npm install -g .
    
    # Then use in config:
    {
      "mcpServers": {
        "weather-server": {
          "command": "weather-server"
        }
      }
    }
    

测试步骤

  1. 重新启动克劳德桌面 完全:

    # Quit Claude Desktop completely
    # Then relaunch from Applications
    
  2. 验证服务器连接:

    • 打开克劳德桌面
    • 在界面中查找MCP服务器指示灯
    • 检查是否有任何错误消息
  3. 测试天气功能:

    • 询问克劳德各地的天气情况
    • 尝试不同的天气相关查询
    • 验证响应是否来自您的MCP服务器

克劳德桌面集成故障排除

常见问题

  1. 服务器未连接:

    # Check if your server runs standalone
    node dist/index.js
    
    # Verify the path in config is correct
    ls -la /absolute/path/to/your/mcp-101/dist/index.js
    
  2. 权限问题:

    # Make sure the file is executable
    chmod +x dist/index.js
    
  3. 配置文件问题:

    # Validate JSON syntax
    cat ~/Library/Application\ Support/Claude/claude_desktop_config.json | python3 -m json.tool
    

调试配置

为了调试,您可以在配置中添加日志记录:

{
  "mcpServers": {
    "weather-server": {
      "command": "node",
      "args": ["/absolute/path/to/your/mcp-101/dist/index.js"],
      "env": {
        "NODE_ENV": "development",
        "DEBUG": "mcp:*"
      }
    }
  }
}

查看日志

  1. 克劳德桌面日志:

    # Check Console app for Claude Desktop logs
    # Or check system logs:
    log show --predicate 'process == "Claude Desktop"' --last 1h
    
  2. 您的服务器日志:

    • 将console.log语句添加到您的 src/index.ts
    • 重建与 make build
    • 重新启动克劳德桌面

使用Claude Desktop的开发工作流程

  1. 进行更改 到您的服务器代码
  2. 重建: make build
  3. 重新启动克劳德桌面 重新加载服务器
  4. 测试 克劳德桌面的变化
  5. 重复 根据需要

快速设置脚本

创建设置脚本以便于测试:

# create setup-claude.sh
#!/bin/bash
echo "Building MCP server..."
make build

echo "Current project path:"
pwd

echo ""
echo "Add this to your Claude Desktop config:"
echo "~/Library/Application Support/Claude/claude_desktop_config.json"
echo ""
echo "{"
echo "  \"mcpServers\": {"
echo "    \"weather-server\": {"
echo "      \"command\": \"node\","
echo "      \"args\": [\"$(pwd)/dist/index.js\"]"
echo "    }"
echo "  }"
echo "}"
echo ""
echo "Then restart Claude Desktop to test!"
# Make it executable and run
chmod +x setup-claude.sh
./setup-claude.sh

🛠️ 开发工作流程

  1. 开始开发: make dev
  2. 进行更改:在中编辑文件 src/
  3. 测试更改: make test
  4. 类型检查: make typecheck
  5. 构建: make build
  6. 验证: make verify (满管运行)

📚 模型上下文协议(MCP)

此服务器实现了模型上下文协议,该协议提供:

  • 人工智能模型与外部系统之间的标准化通信
  • 资源发现和访问模式
  • 工具调用功能
  • 结构化数据交换

在以下网址了解有关MCP的更多信息 官方文件.

🌐 天气服务器功能

此实现通过MCP接口提供与天气相关的功能,包括:

  • 气象数据检索
  • 基于位置的查询
  • 标准化响应格式

🔧 添加功能

装订和格式化(可选)

要添加代码质量工具,请执行以下操作:

# Install ESLint
npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin

# Install Prettier
npm install --save-dev prettier

# Then use
make lint    # Lint code
make format  # Format code

📦 生产大楼

# Create production build
make build

# Create distributable package
make package

# Full verification before deployment
make verify

🔍 故障排除

常见问题

  1. 构建错误:运行 make clean 然后 make build
  2. 依赖性问题:运行 make reset 清洁并重新安装
  3. 类型错误:运行 make typecheck 查看详细的类型问题

调试

# Check project status
make git-status

# View what would be cleaned
make git-clean

# Check for outdated dependencies
make outdated

🤝 贡献

  1. 分叉存储库
  2. 创建要素分支: git checkout -b feature-name
  3. 进行更改
  4. 运行验证: make verify
  5. 提交您的更改: git commit -am 'Add feature'
  6. 推到分支: git push origin feature-name
  7. 提交拉取请求

📝 许可证

该项目根据ISC许可证获得许可。

🔗 链接

📧 支持

对于问题和疑问:

  1. 检查存储库中的现有问题
  2. 创建包含详细信息的新问题
  3. 包括重现任何错误的步骤

内置于❤️ 使用TypeScript和模型上下文协议