数据服务服务器

一个基于Model Context Protocol (MCP)的模块化数据服务服务器,提供实时天气数据,并设计为可轻松扩展到其他数据源。

  • by ParthibanRajasekaran
  • HTML
  • 本地部署
  • 更新于
在 GitHub 查看 ParthibanRajasekaran/mcp-weather

使用说明

MCP数据服务器�

一个全面的模型上下文协议(MCP)服务器,提供各种数据服务,从实时天气数据开始,旨在轻松扩展到其他数据源。

🚀 特性

  • 模块化架构:易于扩展新的数据服务
  • 实时天气数据:获取任何城市的当前天气状况
  • MCP协议合规性:与模型上下文协议完全兼容
  • TypeScript支持:用TypeScript编写,以提高类型安全性
  • 标准运输:使用标准输入/输出进行通信
  • 可扩展设计:为新闻、金融、体育和其他数据服务做好准备

📋 先决条件

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

🛠️ 安装

  1. 克隆存储库:
git clone https://github.com/ParthibanRajasekaran/mcp-weather.git
cd mcp-weather
  1. 安装依赖项:
npm install
  1. 构建项目(可选):
npm run build

🎯 用法

运行服务器

发展模式

npm run dev

生产模式

npm run build
npm start

MCP配置

将以下配置添加到MCP客户端的配置文件中(.vscode/mcp.json):

{
    "servers": {
        "mcp-data-server": {
            "type": "stdio",
            "command": "npx",
            "args": [
                "-y",
                "tsx",
                "src/main.ts"
            ]
        }
    }
}

🔧 可用服务

气象局

getWeather

获取指定城市的当前天气数据。

参数:

  • city (string):获取天气的城市名称

示例用法:

// MCP client call
const weather = await mcpClient.callTool("getWeather", { city: "London" });

响应格式:

{
  "latitude": 51.51147,
  "longitude": -0.13078308,
  "current": {
    "time": "2025-07-08T06:15",
    "temperature_2m": 13.9,
    "apparent_temperature": 11,
    "is_day": 1,
    "rain": 0
  },
  "hourly": {
    "time": [...],
    "temperature_2m": [...]
  }
}

🔮 未来服务(计划中)

  • 新闻服务:从各种来源获取最新消息
  • 金融服务:股票价格、市场数据、加密货币
  • 体育服务:实时比分、球队统计数据、赛程
  • 社交媒体服务:热门话题、社交指标
  • 地图服务:位置数据、方向、地点

🏗️ 建筑

项目结构

mcp-weather/
├── src/
│   ├── main.ts              # Main server entry point
│   ├── services/            # Data service implementations
│   │   └── weather.ts       # Weather service
│   ├── types/               # TypeScript type definitions
│   │   ├── weather.ts       # Weather-related types
│   │   └── service.ts       # Base service interfaces
│   └── utils/               # Utility functions
│       └── registry.ts      # Service registry
├── .vscode/
│   └── mcp.json            # MCP client configuration
├── package.json            # Project dependencies and scripts
├── tsconfig.json           # TypeScript configuration
└── README.md              # This file

传输层

服务器使用 StdioServerTransport 用于通信:

  • 输入:标准输入(stdin)
  • 输出:标准输出(stdout)
  • 协议:基于标准输入的JSON-RPC
  • 益处:简单、可靠且得到广泛支持

服务体系结构

每个服务都遵循此模式:

// 1. Define types
interface ServiceInput { /* ... */ }
interface ServiceOutput { /* ... */ }

// 2. Create service class
class MyService {
    async getData(input: ServiceInput): Promise<string> {
        // Implementation
    }
}

// 3. Register with MCP server
server.tool("myTool", "Description", schema, handler);

🤖 AI助手集成

克劳德桌面(现在可用)

您的MCP服务器可与Claude Desktop开箱即用!看 docs/AI_INTEGRATION.md 有关设置说明。

GitHub Copilot(即将推出)

GitHub Copilot还不支持MCP,但你的服务器已经准备好了!看 docs/GITHUB_COPILOT_INTEGRATION.md 用于:

  • 当前使用VS代码扩展的解决方法
  • 添加MCP支持后会发生什么
  • 集成模式示例

VS代码扩展示例

结账 examples/vscode-extension/ 今天,我们来举一个将MCP服务器与VS Code集成的工作示例!

🔍 MCP检查器集成

对于调试和开发,您可以使用MCP检查器:

  1. 安装MCP检查器:
npm install -g @modelcontextprotocol/inspector
  1. 运行检查器:
npx @modelcontextprotocol/inspector npx tsx src/main.ts
  1. 在浏览器中打开检查器 http://localhost:5173

🧪 发展

添加新服务

  1. 创建类型定义 (src/types/myservice.ts):
export interface MyServiceInput {
    query: string;
}

export const MyServiceSchema = z.object({
    query: z.string().describe("Your query parameter")
});
  1. 实施服务 (src/services/myservice.ts):
export class MyService {
    async getData(input: MyServiceInput): Promise<string> {
        // Your implementation
        return "Service response";
    }
}
  1. 注册工具 (in src/main.ts):
server.tool(
    "myTool",
    "Description of my tool",
    MyServiceSchema,
    async ({ query }: { query: string }) => {
        const result = await myService.getData({ query });
        return {
            content: [{ type: "text", text: result }]
        };
    }
);

开发脚本

  • npm run dev -以热重载的开发模式运行
  • npm run build -构建TypeScript项目
  • npm start -运行已建项目
  • npm test -运行所有测试
  • npm run test:watch -在监视模式下运行测试
  • npm run test:coverage -使用覆盖率报告运行测试

🌐 API详细信息

API气象局

  • 地理编码: https://geocoding-api.open-meteo.com/v1/search
  • 天气: https://api.open-meteo.com/v1/forecast
  • 速率限制:免费层,无需身份验证
  • 模型:UKMO无缝(英国气象局)

🤝 贡献

我们欢迎为新的数据服务做出贡献!方法如下:

  1. 分叉存储库
  2. 创建要素分支: git checkout -b feature/new-service
  3. 按照上述架构添加您的服务
  4. 添加测试和文档
  5. 提交您的更改: git commit -m 'Add new service'
  6. 推到分支: git push origin feature/new-service
  7. 打开拉取请求

服务指南

  • 每个服务都应该包含在自己的文件中
  • 使用TypeScript实现类型安全
  • 包括适当的错误处理
  • 为公共方法添加JSDoc注释
  • 遵循现有代码样式

📄 许可证

此项目根据ISC许可证获得许可-请参阅 许可证 文件以获取详细信息。

🙏 致谢

📞 支持

如果您遇到任何问题或有疑问:

  1. 检查 问题 页
  2. 创建包含详细信息的新问题
  3. 联系人:rajasekaran.parthiban7@gmail.com

🔄 更新日志

v1.0.0

  • 首次发布天气服务
  • 模块化架构,易于扩展
  • MCP协议合规性
  • TypeScript实现
  • 标准运输支持

由以下材料制成❤️ 靠近 帕提班·拉贾塞卡兰 |已准备好扩展到任何数据服务! const服务器=新McpServer({ 名称:“MCP气象服务器”, 版本:“1.0.0”, description:“提供天气数据的服务器” });


## 🔧 Tools Available

### `getWeather`

Retrieves current weather conditions and forecasts for a specified city.

**Parameters:**
- `city` (string): The name of the city to get weather data for

**Returns:**
- Current temperature, apparent temperature, and conditions
- Hourly temperature forecast for the next 7 days
- Location coordinates and timezone information

**Example Usage:**
```typescript
// Through MCP client
const weatherData = await mcpClient.callTool("getWeather", { city: "London" });

📁 项目结构

mcp-weather/
├── .vscode/
│   └── mcp.json              # MCP server configuration
├── weather/
│   └── main.ts               # Main server implementation
├── package.json              # Dependencies and scripts
├── tsconfig.json             # TypeScript configuration
└── README.md                 # This file

🔍 MCP检查器集成

要使用MCP检查器进行调试和开发:

  1. 配置MCP客户端:将服务器添加到MCP配置文件中(.vscode/mcp.json):
{
    "servers": {
        "my-weather-server": {
            "type": "stdio",
            "command": "npx",
            "args": ["-y", "tsx", "weather/main.ts"]
        }
    }
}
  1. 发射检查器:可以使用MCP兼容的工具和检查员检查服务器。

  2. 调试模式:使用开发服务器进行实时调试:

npm run dev

🌐 API集成

该服务器与两个Open Meteo API集成:

地理编码API

  • 端点: https://geocoding-api.open-meteo.com/v1/search
  • 目的:将城市名称转换为坐标
  • 特性:多语言支持,模糊匹配

天气API

  • 端点: https://api.open-meteo.com/v1/forecast
  • 目的:使用坐标检索天气数据
  • 模型:英国气象局无缝模型(ukmo_seamless)
  • 数据:当前状况+每小时预测

🛠️ 发展

可用脚本

# Development server with hot reload
npm run dev

# Build TypeScript to JavaScript
npm run build

# Start production server
npm start

# Run tests
npm test

添加新功能

  1. 新工具:使用将工具添加到服务器 server.tool() 方法
  2. 增强数据:扩展天气API调用以包含更多参数
  3. 错误处理:改进边缘情况下的错误处理

🌍 使用示例

基本天气查询

// Get weather for London
const result = await getWeather({ city: "London" });

// Current conditions
console.log(`Temperature: ${result.current.temperature_2m}°C`);
console.log(`Feels like: ${result.current.apparent_temperature}°C`);
console.log(`Rain: ${result.current.rain}mm`);

多城市比较

const cities = ["London", "Paris", "New York", "Tokyo"];
const weatherData = await Promise.all(
    cities.map(city => getWeather({ city }))
);

🔐 错误处理

服务器包括全面的错误处理:

  • 无效城市:为不存在的城市返回有用的错误消息
  • API故障:妥善处理网络问题
  • 数据验证:使用Zod模式进行输入验证

📊 数据模式

当前天气响应

interface WeatherResponse {
    latitude: number;
    longitude: number;
    timezone: string;
    current: {
        time: string;
        temperature_2m: number;
        apparent_temperature: number;
        is_day: number;
        rain: number;
    };
    hourly: {
        time: string[];
        temperature_2m: number[];
    };
}

🤝 贡献

  1. 分叉存储库
  2. 创建要素分支(git checkout -b feature/amazing-feature)
  3. 提交您的更改(git commit -m 'Add amazing feature')
  4. 推到分支(git push origin feature/amazing-feature)
  5. 打开拉取请求

📝 许可证

此项目根据ISC许可证获得许可-请参阅 许可证 文件以获取详细信息。

🙏 致谢

📞 支持

如有疑问或问题:


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