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 38 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
7467e36
feat(MCP): mcp SSE init (elasticsearch storage)
rajp152k Feb 15, 2025
0b0764d
chore(fmt): gofumpted mcp.go
rajp152k Feb 16, 2025
cd14298
chore(logging): ctrl, klog setup for uniformity
rajp152k Feb 16, 2025
7285ace
chore(esclient): mcp elasticsearch client setup
rajp152k Feb 16, 2025
557456c
chore(format): make format
rajp152k Feb 16, 2025
2e440c3
fix(cilint): unused contexts and vars fix
rajp152k Feb 17, 2025
320e8b5
fix(lint): golangci-lint govet fix
rajp152k Feb 17, 2025
d5b5b7a
fix(err): error return for esclient failure
rajp152k Feb 23, 2025
0d52bb5
feat(pkg/mcp): structure init for pkg/mcp
rajp152k Feb 23, 2025
1831e4e
chore(mod): go mod tidy post get mcp-golang
rajp152k Feb 23, 2025
481ea31
feat(mcp): MCPServer type init
rajp152k Feb 23, 2025
4003a96
fix(mcp-go): mcp-golang -> mcp-go
rajp152k Feb 23, 2025
11433b7
feat(mcptypes): basic MCP entity type aliases
rajp152k Feb 23, 2025
972d247
feat(mcp): MCPStorageServer struct,New, Serve init
rajp152k Feb 23, 2025
f042255
feat(mcp): prompts,tools,resources init
rajp152k Feb 23, 2025
900ffda
Merge remote-tracking branch 'origin/main' into mcp-support
rajp152k May 7, 2025
5294e1c
[skip ci] tdd base
rajp152k May 7, 2025
2bd0e2d
aider.org-init
rajp152k May 8, 2025
b048caf
code overview updates
rajp152k May 8, 2025
94c91b3
code overview updates
rajp152k May 8, 2025
089b94f
code overview updates
rajp152k May 8, 2025
d9e960c
pre phase 1 consolidation
rajp152k May 8, 2025
6fb4515
tdd scratch pad init
rajp152k May 8, 2025
834d5c8
tdd scope
rajp152k May 9, 2025
f985b09
karpor license init
rajp152k May 12, 2025
f0111e1
temporary err suppression
rajp152k May 12, 2025
31d317f
iteration scratch cache thoughts
rajp152k May 12, 2025
460a625
phase-1 init
rajp152k May 12, 2025
07d6c81
mcp-go overview
rajp152k May 12, 2025
f388e97
phase 1 mcp-go understanding init
rajp152k May 12, 2025
5e075a1
MCPStorageServer handles multiple storage backends
rajp152k May 12, 2025
fb97972
MCPStorageServerConstructure Update
rajp152k May 12, 2025
b39e5ff
comment updates
rajp152k May 12, 2025
99136b2
todo for multiple storage backends
rajp152k May 12, 2025
a48a348
fix[type]: pkg/mcp/server.go storageEndpoints
rajp152k May 12, 2025
40127dc
human thought updates
rajp152k May 12, 2025
e0f289d
TODO updates
rajp152k May 12, 2025
59a1d1d
[skip ci] plan and todos updated
rajp152k May 12, 2025
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
89 changes: 89 additions & 0 deletions cmd/karpor/app/mcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// 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"
_ "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
// TODO update mcpOptions to use the generic storage interface
// should be able handle accept multiple storage backends
// ElasticSearchAddresses []string
}

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

func (o *mcpOptions) AddFlags(fs *pflag.FlagSet) {
// TODO chart out how to handle multiple generic storage backends
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
}

// TODO update mcpOptions to use the generic storage interface
//nolint:unparam
func mcpRun(_ context.Context, options *mcpOptions) error {
ctrl.SetLogger(klog.NewKlogr())
log := ctrl.Log.WithName("mcp")

//TODO update so that this receives generic storage backends (more than 1)
log.Info("Starting MCP SSE server",
"port", options.SSEPort, )


//TODO update to use the generic storage interface for initialization
//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 pickup syncer operations patterns for running the mcp server from app/syncer.go

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
18 changes: 18 additions & 0 deletions pkg/mcp/prompts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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 (
)
18 changes: 18 additions & 0 deletions pkg/mcp/resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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 (
)
55 changes: 55 additions & 0 deletions pkg/mcp/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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 new MCPStorageServer configuration
func NewMCPStorageServer(storageEndpoints []storage.Storage, sseBaseURL string, sseAddr string) MCPStorageServer {
mcpServer := server.NewMCPServer("MCP Storage Server", "0.0.1")
sseServer := server.NewSSEServer(mcpServer, sseBaseURL)
return MCPStorageServer{
Storages: storageEndpoints,
MCPServer: mcpServer,
sseServerBaseURL: sseBaseURL,
sseServerBaseURLAddr: sseAddr,
sseServer: sseServer,
}
}


// TODO Modular design : storage backends should be pluggable
// design MCPStorage interface that should be satisfied by the tools
// all resource, tool, prompt handlers should use this MCPStorage interface
// Read-only access to the storage backends needs to be configured
// the server need not

// for exposing a storage backend (DB queries): will be registering them as resources
// https://github.com/mark3labs/mcp-go/blob/main/README.md#resources

// Tools are supposed to have side effects and are not needed as of now in our case
// Prompts can be integrated later on, when patterns emerge

// Serve starts an SSE server on the baseaddr provided
// TODO integrate this Serve into the main cmd
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 {
Storages []storage.Storage
MCPServer *server.MCPServer //config for the mcp server
sseServerBaseURL string
sseServerBaseURLAddr string
sseServer *server.SSEServer //SSE Server config
}

// Type Alias to tag MCPServer Tools
type MCPToolName string

// Type Alias to tag MCPServer Resources
type MCPResourceName string

// Type Alias to tag MCPServer Prompts
type MCPPromptName string