天气信息服务

基于领域驱动设计(DDD)原则构建的天气信息模型上下文协议(MCP)服务器,提供天气数据管理和应用服务。

  • by ranggarifqi
  • C#
  • 本地部署
  • 更新于
在 GitHub 查看 ranggarifqi/mcp-weather

使用说明

MCP气象服务器

一种用于天气信息的模型上下文协议(MCP)服务器,采用领域驱动设计(DDD)原理构建。

项目结构(DDD)

mcp-weather/
├── src/
│   ├── McpWeather.Domain/              # Domain Layer (Core Business Logic)
│   │   ├── Entities/                   # Domain entities with identity
│   │   ├── ValueObjects/               # Immutable value objects
│   │   ├── Repositories/               # Repository interfaces
│   │   ├── Services/                   # Domain services
│   │   └── Events/                     # Domain events
│   │
│   ├── McpWeather.Application/         # Application Layer
│   │   ├── UseCases/                   # Application use cases
│   │   ├── DTOs/                       # Data transfer objects
│   │   ├── Interfaces/                 # Application service interfaces
│   │   └── Services/                   # Application services
│   │
│   ├── McpWeather.Infrastructure/      # Infrastructure Layer
│   │   ├── ExternalServices/           # External API integrations (weather APIs)
│   │   ├── Persistence/                # Data persistence implementations
│   │   └── Configuration/              # Infrastructure configuration
│   │
│   └── McpWeather.Server/              # Presentation Layer
│       └── Program.cs                  # MCP server entry point
│
├── tests/
│   ├── McpWeather.Domain.Tests/        # Domain layer tests
│   ├── McpWeather.Application.Tests/   # Application layer tests
│   ├── McpWeather.Infrastructure.Tests/ # Infrastructure layer tests
│   └── McpWeather.Server.Tests/        # Server layer tests
│
└── docs/                               # Documentation

DDD图层

域层(McpWeather.Domain)

应用程序的核心包含纯业务逻辑,没有外部依赖关系。

  • 实体:具有唯一标识的核心业务对象(例如WeatherForecast、Location)
  • 值对象:没有标识的不可变对象(例如,温度、坐标、风速)
  • 存储库接口:数据访问抽象合同
  • 领域服务:不适合实体的业务逻辑
  • 域事件:域内发生的事件

应用层(McpWeather.Applications)

编排域对象以满足特定于应用程序的用例。

  • 用例:特定于应用程序的业务规则和工作流
  • 按单设计服务:用于在层之间传输数据的数据结构
  • 接口:基础设施服务合同(例如IWeatherApiClient)
  • 应用服务:协调应用程序逻辑和用例

基础设施层(McpWeather.Frastructure)

处理外部问题,并为内层中定义的接口提供实现。

  • 外部服务:Weather API客户端(OpenWeatherMap、WeatherAPI等)
  • 持久性:数据库实现、缓存、存储库实现
  • 配置:基础设施设置、依赖注入配置

服务器层(McpWeather.Server)

MCP服务器的入口点,处理通信协议和依赖注入。

  • MCP服务器实现
  • 依赖注入容器设置
  • 请求/响应处理

依赖关系流

Server → Infrastructure → Application → Domain
        ↑                 ↑              ↑
        └─────────────────┴──────────────┘
          (Dependencies point inward)
  • 域没有依赖关系
  • 应用程序仅依赖于域
  • 基础架构取决于应用程序和域
  • 服务器依赖于所有层,但主要依赖于基础设施

入门指南

先决条件

  • .NET 8.0 SDK或更高版本

建筑

dotnet build

跑步

dotnet run --project src/McpWeather.Server/McpWeather.Server.csproj

测试

运行所有测试:

dotnet test

运行特定层测试:

dotnet test tests/McpWeather.Domain.Tests
dotnet test tests/McpWeather.Application.Tests
dotnet test tests/McpWeather.Infrastructure.Tests
dotnet test tests/McpWeather.Server.Tests

开发指南

DDD原则

  1. 通用语言:在所有层中一致使用领域术语
  2. 限界上下文:将天气相关概念保持在明确的界限内
  3. 实体与价值对象:
    • 将实体用于需要身份和生命周期跟踪的对象
    • 使用值对象来描述特征
  4. 聚合根:定义明确的聚合边界和访问模式
  5. 存储库模式:仅通过存储库访问聚合
  6. 域事件:传达域内的更改

层职责

  • 领域:应该与框架无关,并包含纯C#代码
  • 应用:不应包含业务规则,只应包含编排
  • 基础设施:不应包含业务逻辑
  • 服务器:应精简,专注于协议处理

许可证

待定