MCPcopy Create free account
hub / github.com/VersusControl/devops-ai-guidelines / main

Function main

02-mcp-for-devops/code/08/cmd/server/main.go:21–90  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

19)
20
21func main() {
22 metricsAddr := flag.String("metrics-addr", ":9090", "Prometheus metrics listen address")
23 pprofAddr := flag.String("pprof-addr", "", "pprof listen address (empty = disabled)")
24 rps := flag.Float64("ratelimit-rps", 50, "per-identity requests/second")
25 burst := flag.Int("ratelimit-burst", 100, "per-identity burst")
26 flag.Parse()
27
28 rec := metrics.New("mcp")
29 limiter := ratelimit.New(*rps, *burst, 10*time.Minute)
30
31 srv := perfmcp.New("k8s-mcp-perf", "1.0.0", rec, limiter)
32
33 // Example tool: echo back the request to exercise the middleware.
34 srv.AddTool(
35 mcpgo.NewTool("ping",
36 mcpgo.WithDescription("Health check. Returns 'pong' with the caller identity."),
37 ),
38 func(ctx context.Context, _ mcpgo.CallToolRequest) (*mcpgo.CallToolResult, error) {
39 return mcpgo.NewToolResultText("pong"), nil
40 },
41 )
42
43 // Metrics endpoint.
44 mux := http.NewServeMux()
45 mux.Handle("/metrics", metrics.Handler())
46 mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
47 w.WriteHeader(http.StatusOK)
48 _, _ = w.Write([]byte("ok"))
49 })
50 metricsSrv := &http.Server{
51 Addr: *metricsAddr,
52 Handler: mux,
53 ReadHeaderTimeout: 5 * time.Second,
54 }
55 go func() {
56 log.Printf("metrics listening on %s", *metricsAddr)
57 if err := metricsSrv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
58 log.Printf("metrics server: %v", err)
59 }
60 }()
61
62 // pprof (optional).
63 var prof *profiler.Server
64 if *pprofAddr != "" {
65 prof = profiler.New(*pprofAddr)
66 prof.Start()
67 log.Printf("pprof listening on %s", *pprofAddr)
68 }
69
70 // Graceful shutdown.
71 ctx, cancel := context.WithCancel(context.Background())
72 sigCh := make(chan os.Signal, 1)
73 signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
74 go func() {
75 <-sigCh
76 log.Println("shutting down")
77 shutdown, sc := context.WithTimeout(context.Background(), 5*time.Second)
78 defer sc()

Callers

nothing calls this directly

Calls 4

StartMethod · 0.95
StopMethod · 0.95
AddToolMethod · 0.80
ServeStdioMethod · 0.80

Tested by

no test coverage detected