k8sgpt/pkg/server/example/main.go
Alex Jones e41ffd80d0
feat: add MCP support (#1471)
* feat: first mcp impl

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* chore: update

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* chore: wip

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* chore: switcheed to stdio transport

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* chore: readme

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* feat: fix the linter 🤖

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* feat: fix the linter 🤖

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* feat(mcp): implement MCP server and handler

- Implement MCP server and handler
- Add MCP server to serve
- Add MCP handler to handle MCP requests
- Add MCP server to serve
- Add MCP handler to handle MCP requests

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* feat: consolidating code duplication

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* feat: added http sse support

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* chore: fixed broken tests

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* chore: updated and fixed linter

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* chore: updated and fixed linter

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* chore: updated the linter issues

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

---------

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>
2025-04-29 09:22:44 +01:00

75 lines
1.9 KiB
Go

/*
Copyright 2024 The K8sGPT Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"flag"
"log"
"os"
"os/signal"
"syscall"
"github.com/k8sgpt-ai/k8sgpt/pkg/ai"
"github.com/k8sgpt-ai/k8sgpt/pkg/server"
"go.uber.org/zap"
)
func main() {
// Parse command line flags
port := flag.String("port", "8089", "Port to run the MCP server on")
useHTTP := flag.Bool("http", false, "Enable HTTP mode for MCP server")
flag.Parse()
// Initialize zap logger
logger, err := zap.NewProduction()
if err != nil {
log.Fatalf("Error creating logger: %v", err)
}
defer func() {
if err := logger.Sync(); err != nil {
log.Printf("Error syncing logger: %v", err)
}
}()
// Create AI provider
aiProvider := &ai.AIProvider{
Name: "openai",
Password: os.Getenv("OPENAI_API_KEY"),
Model: "gpt-3.5-turbo",
}
// Create and start MCP server
mcpServer, err := server.NewMCPServer(*port, aiProvider, *useHTTP, logger)
if err != nil {
log.Fatalf("Error creating MCP server: %v", err)
}
// Start the server in a goroutine
go func() {
if err := mcpServer.Start(); err != nil {
log.Fatalf("Error starting MCP server: %v", err)
}
}()
// Handle graceful shutdown
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
<-sigChan
// Cleanup
if err := mcpServer.Close(); err != nil {
log.Printf("Error closing MCP server: %v", err)
}
}