Skip to content

Commit 2c2edee

Browse files
committed
support server-sent events
1 parent 037c5f0 commit 2c2edee

File tree

5 files changed

+93
-8
lines changed

5 files changed

+93
-8
lines changed

Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM golang:1.24-alpine3.21 AS builder
2+
3+
WORKDIR /app
4+
5+
COPY go.mod go.sum ./
6+
7+
RUN go mod download
8+
9+
COPY . .
10+
11+
RUN go build -o weather-mcp-server ./cmd/weather-mcp-server
12+
13+
FROM gcr.io/distroless/base-debian12
14+
15+
WORKDIR /app
16+
17+
COPY --from=builder /app/weather-mcp-server .
18+
19+
USER nonroot:nonroot
20+
21+
EXPOSE 8000
22+
23+
ENTRYPOINT ["./weather-mcp-server", "--address", "0.0.0.0:8000"]

README.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<div align="center">
2-
<img alt="example output template" src="./assets/weather.svg">
2+
<img alt="example output template" src="assets/weather.svg">
33

44
<h1>Weather API MCP Server</h1>
55

66
[![license](https://img.shields.io/badge/license-MIT-red.svg)](LICENSE)
77
[![go version](https://img.shields.io/github/go-mod/go-version/TuanKiri/weather-mcp-server)](go.mod)
88
[![go report](https://goreportcard.com/badge/github.com/TuanKiri/weather-mcp-server)](https://goreportcard.com/report/github.com/TuanKiri/weather-mcp-server)
9+
[![build](https://github.com/TuanKiri/weather-mcp-server/workflows/Build/badge.svg)](https://github.com/TuanKiri/weather-mcp-server/actions?workflow=Build)
910

1011
<strong>[Report Bug](https://github.com/TuanKiri/weather-mcp-server/issues/new?assignees=&labels=bug&projects=&template=bug_report.yml)</strong> | <strong>[Request Feature](https://github.com/TuanKiri/weather-mcp-server/issues/new?assignees=&labels=enhancement&projects=&template=feature_request.yml)</strong>
1112

@@ -14,13 +15,15 @@
1415
A lightweight Model Context Protocol (MCP) server that enables AI assistants like Claude to retrieve and interpret real-time weather data.
1516

1617
<div align="center">
17-
<img alt="demo example" src="./assets/weather.gif" width="480">
18+
<img alt="demo example" src="assets/weather.gif" width="480">
1819
</div>
1920

2021
## Installing on Claude Desktop
2122

2223
To use your MCP server with Claude Desktop, add it to your Claude configuration:
2324

25+
#### 1. Local mode
26+
2427
```json
2528
{
2629
"mcpServers": {
@@ -35,7 +38,19 @@ To use your MCP server with Claude Desktop, add it to your Claude configuration:
3538
}
3639
```
3740

38-
You can get your API key in your personal account at [weatherapi](https://www.weatherapi.com/my/).
41+
You can get an API key from your personal account on [WeatherAPI](https://www.weatherapi.com/my/).
42+
43+
#### 2. Remote mode
44+
45+
```json
46+
{
47+
"mcpServers": {
48+
"weather-mcp-server": {
49+
"url": "http://host:port/sse"
50+
}
51+
}
52+
}
53+
```
3954

4055
## Build from source
4156

@@ -45,6 +60,22 @@ You can use `go` to build the binary in the `cmd/github-mcp-server` directory.
4560
go build -o weather-mcp-server ./cmd/weather-mcp-server
4661
```
4762

63+
## Using MCP with Docker Containers
64+
65+
#### 1. Build the Docker image:
66+
67+
```shell
68+
docker build -t weather-mcp-server .
69+
```
70+
71+
#### 2. Run the Docker Container:
72+
73+
```shell
74+
docker run -e WEATHER_API_KEY=your-api-key -d --name weather-mcp-server -p 8000:8000 weather-mcp-server
75+
```
76+
77+
Replace `your-api-key` with your actual [WeatherAPI](https://www.weatherapi.com/my/) API key.
78+
4879
## Tools
4980

5081
- **current_weather** - Gets the current weather for a city
@@ -77,4 +108,4 @@ Please follow the [contribution guidelines](.github/CONTRIBUTING.md).
77108

78109
## License
79110

80-
This MCP server is licensed under the MIT License.
111+
This MCP server is licensed under the [MIT License](LICENSE).

cmd/weather-mcp-server/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"flag"
45
"log"
56
"os"
67
"time"
@@ -9,7 +10,11 @@ import (
910
)
1011

1112
func main() {
12-
cfg := server.Config{
13+
addr := flag.String("address", "", "The host and port to start the sse server")
14+
flag.Parse()
15+
16+
cfg := &server.Config{
17+
ListenAddr: *addr,
1318
WeatherAPIKey: os.Getenv("WEATHER_API_KEY"),
1419
WeatherAPITimeout: 1 * time.Second,
1520
}
@@ -18,7 +23,7 @@ func main() {
1823
log.Fatal(err)
1924
}
2025

21-
if err := server.Run(&cfg); err != nil {
26+
if err := server.Run(cfg); err != nil {
2227
log.Fatal(err)
2328
}
2429
}

internal/server/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
)
77

88
type Config struct {
9+
ListenAddr string
910
WeatherAPIKey string
1011
WeatherAPITimeout time.Duration
1112
}

internal/server/server.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package server
22

33
import (
4+
"context"
45
"embed"
56
"html/template"
7+
"log"
8+
"os/signal"
9+
"syscall"
610

711
"github.com/mark3labs/mcp-go/server"
812

@@ -30,13 +34,34 @@ func Run(cfg *Config) error {
3034
server.WithLogging(),
3135
)
3236

33-
serverTools := []tools.ToolFunc{
37+
toolFuncs := []tools.ToolFunc{
3438
tools.CurrentWeather,
3539
}
3640

37-
for _, tool := range serverTools {
41+
for _, tool := range toolFuncs {
3842
s.AddTool(tool(svc))
3943
}
4044

45+
if cfg.ListenAddr != "" {
46+
return serveSSE(s, cfg.ListenAddr)
47+
}
48+
4149
return server.ServeStdio(s)
4250
}
51+
52+
func serveSSE(s *server.MCPServer, addr string) error {
53+
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
54+
defer stop()
55+
56+
srv := server.NewSSEServer(s)
57+
58+
go func() {
59+
if err := srv.Start(addr); err != nil {
60+
log.Fatal(err)
61+
}
62+
}()
63+
64+
<-ctx.Done()
65+
66+
return srv.Shutdown(context.TODO())
67+
}

0 commit comments

Comments
 (0)