Skip to content

Commit 833ddfc

Browse files
committed
mcp: add
1 parent e44e055 commit 833ddfc

6 files changed

Lines changed: 209 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
client/build
1010
bringyourctl/bringyourctl
1111
api/api
12+
mcp/mcp
1213
taskworker/taskworker
1314
gpt/gpt
1415
connect/connect

mcp/.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"cSpell.words": [
3+
"glog"
4+
]
5+
}

mcp/Dockerfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM ubuntu:22.04
2+
# https://www.docker.com/blog/faster-multi-platform-builds-dockerfile-cross-compilation-guide/
3+
4+
ARG TARGETPLATFORM
5+
ARG warp_env
6+
ENV WARP_ENV=$warp_env
7+
8+
# see https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
9+
RUN apt-get update && apt-get install -y \
10+
ca-certificates \
11+
&& rm -rf /var/lib/apt/lists/*
12+
RUN update-ca-certificates
13+
14+
COPY build/$TARGETPLATFORM/mcp /usr/local/sbin/bringyour-mcp
15+
16+
RUN ln -s /srv/warp/vault/.aws /root/.aws
17+
18+
STOPSIGNAL SIGTERM
19+
CMD ["/usr/local/sbin/bringyour-mcp", "-p", "80"]

mcp/Makefile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
GOBUILD=go build -trimpath
3+
4+
all: init clean build
5+
6+
clean:
7+
rm -rf build
8+
9+
build:
10+
env GOEXPERIMENT=greenteagc CGO_ENABLED=0 GOOS=linux GOARCH=arm64 ${GOBUILD} -o build/linux/arm64/
11+
env GOEXPERIMENT=greenteagc CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ${GOBUILD} -o build/linux/amd64/
12+
env GOEXPERIMENT=greenteagc CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 ${GOBUILD} -o build/darwin/arm64/
13+
env GOEXPERIMENT=greenteagc CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 ${GOBUILD} -o build/darwin/amd64/
14+
15+
warp_build:
16+
$(MAKE) all
17+
docker buildx build --progress=plain \
18+
--build-arg warp_env=${WARP_ENV} \
19+
--platform linux/arm64/v8,linux/amd64 \
20+
-t ${WARP_DOCKER_NAMESPACE}/${WARP_DOCKER_IMAGE}:${WARP_DOCKER_VERSION} \
21+
--push \
22+
.
23+
24+
init:
25+
go clean -cache
26+
go clean -modcache

mcp/main.go

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package main
2+
3+
import (
4+
"context"
5+
// "fmt"
6+
// "net/http"
7+
"os"
8+
"syscall"
9+
10+
// "net"
11+
// "errors"
12+
"net"
13+
"runtime"
14+
"strconv"
15+
16+
"time"
17+
18+
"github.com/docopt/docopt-go"
19+
// "github.com/prometheus/client_golang/prometheus"
20+
// "github.com/prometheus/client_golang/prometheus/push"
21+
22+
"github.com/urnetwork/glog"
23+
24+
"github.com/urnetwork/server"
25+
"github.com/urnetwork/server/router"
26+
)
27+
28+
const DrainTimeout = 60 * time.Second
29+
30+
func main() {
31+
usage := `BringYour API server.
32+
33+
Usage:
34+
api [--port=<port>]
35+
api -h | --help
36+
api --version
37+
38+
Options:
39+
-h --help Show this screen.
40+
--version Show version.
41+
-p --port=<port> Listen port [default: 80].`
42+
43+
opts, err := docopt.ParseArgs(usage, os.Args[1:], server.RequireVersion())
44+
if err != nil {
45+
panic(err)
46+
}
47+
48+
quitEvent := server.NewEventWithContext(context.Background())
49+
defer quitEvent.Set()
50+
51+
closeFn := quitEvent.SetOnSignals(syscall.SIGQUIT, syscall.SIGTERM)
52+
defer closeFn()
53+
54+
ctx, cancel := context.WithCancel(context.Background())
55+
defer cancel()
56+
57+
// drain on sigterm
58+
go server.HandleError(func() {
59+
defer cancel()
60+
select {
61+
case <-ctx.Done():
62+
return
63+
case <-quitEvent.Ctx.Done():
64+
select {
65+
case <-ctx.Done():
66+
return
67+
case <-time.After(DrainTimeout):
68+
}
69+
}
70+
})
71+
72+
// Debugging
73+
go server.HandleError(func() {
74+
for {
75+
select {
76+
case <-ctx.Done():
77+
return
78+
case <-time.After(30 * time.Second):
79+
}
80+
81+
glog.Infof("[api]goroutines=%d/%d\n", runtime.NumGoroutine(), runtime.GOMAXPROCS(0))
82+
}
83+
})
84+
85+
routes := []*router.Route{
86+
router.NewRoute("GET", "/status", router.WarpStatus),
87+
}
88+
89+
// server.().Printf("%s\n", opts)
90+
91+
port, _ := opts.Int("--port")
92+
93+
server.Warmup()
94+
95+
glog.Infof(
96+
"[api]serving %s %s on *:%d\n",
97+
server.RequireEnv(),
98+
server.RequireVersion(),
99+
port,
100+
)
101+
102+
// if os.Getenv("SKIP_METRICS") == "" {
103+
// pushMetrics := push.New("https://push-gateway.cluster.bringyour.dev", "my_job").
104+
// Gatherer(prometheus.DefaultGatherer).
105+
// Grouping("warp_block", server.RequireBlock()).
106+
// Grouping("warp_env", server.RequireEnv()).
107+
// Grouping("warp_version", server.RequireVersion()).
108+
// Grouping("warp_service", server.RequireService()).
109+
// Grouping("warp_config_version", server.RequireConfigVersion()).
110+
// Grouping("warp_host", server.RequireHost())
111+
112+
// go func() {
113+
// for {
114+
// select {
115+
// case <-quitEvent.Ctx.Done():
116+
// return
117+
// case <-time.NewTicker(30 * time.Second).C:
118+
// err := pushMetrics.Push()
119+
// if err != nil {
120+
// glog.Errorf("[api]pushMetrics.Push = %s\n", err)
121+
// }
122+
// }
123+
// }
124+
// }()
125+
// }
126+
127+
listenIpv4, _, listenPort := server.RequireListenIpPort(port)
128+
129+
reusePort := false
130+
131+
httpServerOptions := server.HttpServerOptions{
132+
ReadTimeout: 15 * time.Second,
133+
WriteTimeout: 30 * time.Second,
134+
IdleTimeout: 5 * time.Minute,
135+
}
136+
137+
err = server.HttpListenAndServeWithReusePort(
138+
ctx,
139+
net.JoinHostPort(listenIpv4, strconv.Itoa(listenPort)),
140+
router.NewRouter(ctx, routes),
141+
reusePort,
142+
httpServerOptions,
143+
)
144+
if err != nil {
145+
panic(err)
146+
}
147+
glog.Infof("[api]close\n")
148+
}

mcp/run-local.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
export WARP_HOST="127.0.0.1"; \
4+
export WARP_BLOCK="local"; \
5+
export WARP_SERVICE="api"; \
6+
export WARP_VERSION="0.0.0-local"; \
7+
export WARP_ENV="local"; \
8+
export BRINGYOUR_POSTGRES_HOSTNAME="local-pg.bringyour.com"; \
9+
export BRINGYOUR_REDIS_HOSTNAME="local-redis.bringyour.com"; \
10+
go run . "$@"

0 commit comments

Comments
 (0)