GO-WEATHER-MCP
概述
这个项目实现了一个MCP服务器,该服务器能够根据输入的城市提供天气信息,同时还实现了一个具有相同功能的REST API。该项目是使用Go语言开发的,并且 开放天气API 作为信息来源。
MCP(模型上下文协议)服务器是一种后端应用程序,负责管理和提供人工智能模型运行和响应请求所需的上下文信息。
这段代码是为了学习Golang中的MCP服务器概念和Clean Architecture(整洁架构)而开发的。
______________________________________________________________________
特点/功能
MCP工具:
- 按城市获取天气(检索指定城市的当前天气。)
______________________________________________________________________
项目结构
└── go-weather-mcp/
├── cmd
│ └── server
├── go.mod
├── go.sum
└── internal
├── api
├── app
├── config
├── domain
├── infra
└── mcp项目索引
GO-WEATHER-MCP/
__root__
go.mod
go.sum
cmd
server
main.go ❯ Entrypoint to the server
internal
mcp
server.go ❯ MCP Server configuration
config
config.go
infra
openweathermap
geocode_mapper.go
dto.go
weather_mapper.go
client.go
geocode_repository.go
weather_repository.go
api
http
router.go
handler.go
domain
weather
repository.go
usecase.go
entity.go
geocode
repository.go
usecase.go
entity.go
app
weather
service.go
geocode
service.go
______________________________________________________________________
入门指南/开始使用
先决条件
在开始使用 go-weather-mcp 之前,请确保您的运行环境满足以下要求:
- 去(表示命令或建议)1.21或更新版本
- 开放天气API密钥
安装
使用以下方法之一安装 go-weather-mcp:
从源代码构建:
- 克隆 go-weather-mcp 仓库:
❯ git clone https://github.com/alvarolspeixoto/go-weather-mcp- 导航到项目目录:
❯ cd go-weather-mcp- 安装项目依赖项:
复制并粘贴.env.example文件到.env文件中,并在其中填入你的API密钥 OPENWEATHER_API_KEY 变量。
使用 go modules
❯ go build使用方法
使用以下命令运行 go-weather-mcp: 使用 go modules
❯ go run ./cmd/server --mode={mode} --port={port}- {模式}:
mcp(仅用于运行MCP服务器),http(仅限HTTP服务器)和dual(运行两个服务器)。 - {端口} 仅在使用时才需要
http或者dual模式。
为了在Claude Desktop中测试MCP服务器,您应该在您的(环境中)对其进行配置 claude_desktop_config.jsonWindows 示例:
{
"mcpServers": {
"weather": {
"command": "C:\\Users\\alvar\\OneDrive\\Documentos\\dev\\go-mcp\\go-weather-mcp.exe",
"args": [],
"env": {
"MODE": "mcp",
"OPENWEATHER_API_KEY": "your-open-weather-api-key"
}
}
}
}
______________________________________________________________________
