forked from GoogleCloudPlatform/kubectl-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp.go
More file actions
126 lines (113 loc) · 3.28 KB
/
mcp.go
File metadata and controls
126 lines (113 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2025 Google LLC
//
// 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 (
"context"
"fmt"
"github.com/GoogleCloudPlatform/kubectl-ai/pkg/tools"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
"k8s.io/klog/v2"
)
type kubectlMCPServer struct {
kubectlConfig string
server *server.MCPServer
tools tools.Tools
workDir string
}
func newKubectlMCPServer(ctx context.Context, kubectlConfig string, tools tools.Tools, workDir string) (*kubectlMCPServer, error) {
s := &kubectlMCPServer{
kubectlConfig: kubectlConfig,
workDir: workDir,
server: server.NewMCPServer(
"kubectl-ai",
"0.0.1",
server.WithToolCapabilities(true),
),
tools: tools,
}
for _, tool := range s.tools.AllTools() {
toolDefn := tool.FunctionDefinition()
toolInputSchema, err := toolDefn.Parameters.ToRawSchema()
if err != nil {
return nil, fmt.Errorf("converting tool schema to json.RawMessage: %w", err)
}
s.server.AddTool(mcp.NewToolWithRawSchema(
toolDefn.Name,
toolDefn.Description,
toolInputSchema,
), s.handleToolCall)
}
return s, nil
}
func (s *kubectlMCPServer) Serve(ctx context.Context) error {
return server.ServeStdio(s.server)
}
func (s *kubectlMCPServer) handleToolCall(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
log := klog.FromContext(ctx)
name := request.Params.Name
command := request.Params.Arguments["command"].(string)
modifiesResource := request.Params.Arguments["modifies_resource"].(string)
log.Info("Received tool call", "tool", name, "command", command, "modifies_resource", modifiesResource)
ctx = context.WithValue(ctx, "kubeconfig", s.kubectlConfig)
ctx = context.WithValue(ctx, "work_dir", s.workDir)
tool := tools.Lookup(name)
if tool == nil {
return &mcp.CallToolResult{
Content: []mcp.Content{
mcp.TextContent{
Type: "text",
Text: fmt.Sprintf("Error: Tool %s not found", name),
},
},
}, nil
}
output, err := tool.Run(ctx, map[string]any{
"command": command,
})
if err != nil {
log.Error(err, "Error running tool call")
return &mcp.CallToolResult{
Content: []mcp.Content{
mcp.TextContent{
Type: "text",
Text: fmt.Sprintf("Error: %v", err),
},
},
IsError: true,
}, nil
}
result, err := tools.ToolResultToMap(output)
if err != nil {
log.Error(err, "Error converting tool call output to result")
return &mcp.CallToolResult{
Content: []mcp.Content{
mcp.TextContent{
Type: "text",
Text: fmt.Sprintf("Error: %v", err),
},
},
IsError: true,
}, nil
}
log.Info("Tool call output", "tool", name, "result", result)
return &mcp.CallToolResult{
Content: []mcp.Content{
mcp.TextContent{
Type: "text",
Text: fmt.Sprintf("%v", result),
},
},
}, nil
}