Skip to content

WIP feat:MCP Support init #798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions cmd/karpor/app/mcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright The Karpor 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 app

import (
"context"

"github.com/KusionStack/karpor/pkg/infra/search/storage/elasticsearch"
"github.com/KusionStack/karpor/pkg/mcp"
esclient "github.com/elastic/go-elasticsearch/v8"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
)

type mcpOptions struct {
SSEPort string
ElasticSearchAddresses []string
}

func NewMCPOptions() *mcpOptions {
return &mcpOptions{}
}

func (o *mcpOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.SSEPort, "MCP SSE server exposure port", ":7999", "The address expossing the mcp server")
fs.StringSliceVar(&o.ElasticSearchAddresses, "elastic-search-addresses", nil, "The elastic search address")
}

func NewMCPCommand(ctx context.Context) *cobra.Command {
options := NewMCPOptions()
cmd := &cobra.Command{
Use: "mcp",
Short: "start a storage mcp server to enable natural language interaction capabilities with the storage backend",
RunE: func(cmd *cobra.Command, args []string) error {
return mcpRun(ctx, options)
},
}
options.AddFlags(cmd.Flags())
return cmd
}

//nolint:unparam
func mcpRun(ctx context.Context, options *mcpOptions) error {
ctrl.SetLogger(klog.NewKlogr())
log := ctrl.Log.WithName("mcp")

log.Info("Starting MCP SSE server",
"port", options.SSEPort,
"esAddresses", options.ElasticSearchAddresses)

//nolint:contextcheck
es, err := elasticsearch.NewStorage(esclient.Config{
Addresses: options.ElasticSearchAddresses,
})
if err != nil {
log.Error(err, "unable to init elasticsearch client")
return err
}
log.Info("Acquired elasticsearch storage backend", "esStorage", es)

// TODO : integrate mcp-golang SSE server

log.Info("TODO: yet to implement mcp functionality")
log.Info("see /cmd/karpor/app/mcp.go for further directives")
return nil
}
2 changes: 2 additions & 0 deletions cmd/karpor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ func main() {

cmd := app.NewServerCommand(ctx)
syncCmd := app.NewSyncerCommand(ctx)
mcpCmd := app.NewMCPCommand(ctx)
cmd.AddCommand(syncCmd)
cmd.AddCommand(mcpCmd)

code := cli.Run(cmd)
os.Exit(code)
Expand Down
11 changes: 7 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/KusionStack/karpor

go 1.22
go 1.23

toolchain go1.23.5
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line necessary?


require (
github.com/Masterminds/sprig/v3 v3.2.2
Expand All @@ -15,15 +17,16 @@ require (
github.com/go-chi/render v1.0.3
github.com/go-logr/logr v1.2.3
github.com/google/gofuzz v1.2.0
github.com/google/uuid v1.4.0
github.com/google/uuid v1.6.0
github.com/hupe1980/go-huggingface v0.0.15
github.com/mark3labs/mcp-go v0.8.5
github.com/mitchellh/hashstructure/v2 v2.0.2
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852
github.com/pkg/errors v0.9.1
github.com/sashabaranov/go-openai v1.27.0
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
github.com/swaggo/http-swagger/v2 v2.0.2
github.com/swaggo/swag v1.16.2
github.com/xwb1989/sqlparser v0.0.0-20171128062118-da747e0c62c4
Expand Down Expand Up @@ -134,7 +137,7 @@ require (
github.com/smartystreets/goconvey v1.8.1 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/stoewer/go-strcase v1.2.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/swaggo/files/v2 v2.0.0 // indirect
go.etcd.io/etcd/api/v3 v3.5.5 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.5 // indirect
Expand Down
13 changes: 8 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hf
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g=
Expand Down Expand Up @@ -319,6 +319,8 @@ github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN
github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mark3labs/mcp-go v0.8.5 h1:s5oRwQfs83Jim3ZAcQMyUQNHzCEVIuGD12GV8vhJqqc=
github.com/mark3labs/mcp-go v0.8.5/go.mod h1:cjMlBU0cv/cj9kjlgmRhoJ5JREdS7YX83xeIG9Ko/jE=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/matttproud/golang_protobuf_extensions v1.0.2 h1:hAHbPm5IJGijwng3PWk09JkG9WeqChjprR5s9bBZ+OM=
github.com/matttproud/golang_protobuf_extensions v1.0.2/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
Expand Down Expand Up @@ -424,8 +426,9 @@ github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand All @@ -435,8 +438,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/swaggo/files/v2 v2.0.0 h1:hmAt8Dkynw7Ssz46F6pn8ok6YmGZqHSVLZ+HQM7i0kw=
github.com/swaggo/files/v2 v2.0.0/go.mod h1:24kk2Y9NYEJ5lHuCra6iVwkMjIekMCaFq/0JQj66kyM=
github.com/swaggo/http-swagger/v2 v2.0.2 h1:FKCdLsl+sFCx60KFsyM0rDarwiUSZ8DqbfSyIKC9OBg=
Expand Down
4 changes: 4 additions & 0 deletions pkg/mcp/prompts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package mcp

import (
)
4 changes: 4 additions & 0 deletions pkg/mcp/resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package mcp

import (
)
41 changes: 41 additions & 0 deletions pkg/mcp/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright The Karpor 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 mcp

import (
"github.com/KusionStack/karpor/pkg/infra/search/storage"
_ "github.com/KusionStack/karpor/pkg/infra/search/storage/elasticsearch"
_ "github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
_ "sigs.k8s.io/controller-runtime"
)

// NewMCPStorageServer yields a storage initialized MCPServer struct
func NewMCPStorageServer(storageEndpoint storage.Storage, sseBaseURL string, sseAddr string) MCPStorageServer {
mcpServer := server.NewMCPServer("MCP Storage Server", "0.0.1")
sseServer := server.NewSSEServer(mcpServer, sseBaseURL)
return MCPStorageServer{
Storage: storageEndpoint,
MCPServer: mcpServer,
sseServerBaseURL: sseBaseURL,
sseServerBaseURLAddr: sseAddr,
sseServer: sseServer,
}
}

// Serve starts an SSE server on the baseaddr provided
func (m *MCPStorageServer) Serve() error {
return m.sseServer.Start(m.sseServerBaseURLAddr)
}
17 changes: 17 additions & 0 deletions pkg/mcp/tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright The Karpor 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 mcp

import (
)
40 changes: 40 additions & 0 deletions pkg/mcp/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright The Karpor 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 mcp

import (
"github.com/KusionStack/karpor/pkg/infra/search/storage"
_ "github.com/KusionStack/karpor/pkg/infra/search/storage/elasticsearch"
_ "github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)

// MCPServer is the main object that holds the necessary fields and components for the mcp server component to expose the storage backend via resources, tools and prompts
type MCPStorageServer struct {
Storage storage.Storage //the storage backend to be exposed by the MCPStorageServer
MCPServer *server.MCPServer //manages configurations of tools, resources, prompts and their handlers
sseServerBaseURL string //baseURL on which the sse server is exposed
sseServerBaseURLAddr string //port on which the sse server is exposed
sseServer *server.SSEServer //manages configuration of the HTTP based SSE server on which the MCPServer is exposed
}

// MCPToolName is a string tag for an mcp server tool
type MCPToolName string

// MCPResourceName is a string tag for an mcp server resource
type MCPResourceName string

// MCPPromptName is a string tag for an mcp server prompt
type MCPPromptName string