Skip to content

Commit e5b34ec

Browse files
authored
mcp: adds create and deploy tools (knative#2859)
Signed-off-by: kapil <[email protected]>
1 parent f43b09b commit e5b34ec

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

pkg/mcp/mcp.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mcp
33
import (
44
"context"
55
"fmt"
6+
"os/exec"
67

78
"github.com/mark3labs/mcp-go/mcp"
89
"github.com/mark3labs/mcp-go/server"
@@ -18,13 +19,51 @@ func NewServer() *MCPServer {
1819
"1.0.0",
1920
server.WithToolCapabilities(true),
2021
)
22+
2123
mcpServer.AddTool(
2224
mcp.NewTool("healthcheck",
2325
mcp.WithDescription("Checks if the server is running"),
2426
),
2527
handleHealthCheckTool,
2628
)
2729

30+
mcpServer.AddTool(
31+
mcp.NewTool("create",
32+
mcp.WithDescription("Creates a knative function in the current directory"),
33+
mcp.WithString("name",
34+
mcp.Required(),
35+
mcp.Description("Name of the function to be created"),
36+
),
37+
mcp.WithString("language",
38+
mcp.Required(),
39+
mcp.Description("Language/Runtime of the function to be created"),
40+
),
41+
mcp.WithString("cwd",
42+
mcp.Required(),
43+
mcp.Description("Current working directory of the MCP client"),
44+
),
45+
),
46+
handleCreateTool,
47+
)
48+
49+
mcpServer.AddTool(
50+
mcp.NewTool("deploy",
51+
mcp.WithDescription("Deploys the function to the cluster"),
52+
mcp.WithString("registry",
53+
mcp.Required(),
54+
mcp.Description("Name of the registry to be used to push the function image"),
55+
),
56+
mcp.WithString("cwd",
57+
mcp.Required(),
58+
mcp.Description("Full path of the function to be deployed"),
59+
),
60+
mcp.WithString("builder",
61+
mcp.Required(),
62+
mcp.Description("Builder to be used to build the function image"),
63+
),
64+
),
65+
handleDeployTool,
66+
)
2867
return &MCPServer{
2968
server: mcpServer,
3069
}
@@ -41,3 +80,56 @@ func handleHealthCheckTool(
4180
body := []byte(fmt.Sprintf(`{"message": "%s"}`, "The MCP server is running!"))
4281
return mcp.NewToolResultText(string(body)), nil
4382
}
83+
84+
func handleCreateTool(
85+
ctx context.Context,
86+
request mcp.CallToolRequest,
87+
) (*mcp.CallToolResult, error) {
88+
cwd, err := request.RequireString("cwd")
89+
if err != nil {
90+
return mcp.NewToolResultError(err.Error()), nil
91+
}
92+
name, err := request.RequireString("name")
93+
if err != nil {
94+
return mcp.NewToolResultError(err.Error()), nil
95+
}
96+
language, err := request.RequireString("language")
97+
if err != nil {
98+
return mcp.NewToolResultError(err.Error()), nil
99+
}
100+
101+
cmd := exec.Command("func", "create", "-l", language, name)
102+
cmd.Dir = cwd
103+
out, err := cmd.Output()
104+
if err != nil {
105+
return mcp.NewToolResultError(err.Error()), nil
106+
}
107+
body := []byte(fmt.Sprintf(`{"result": "%s"}`, out))
108+
return mcp.NewToolResultText(string(body)), nil
109+
}
110+
111+
func handleDeployTool(
112+
ctx context.Context,
113+
request mcp.CallToolRequest,
114+
) (*mcp.CallToolResult, error) {
115+
cwd, err := request.RequireString("cwd")
116+
if err != nil {
117+
return mcp.NewToolResultError(err.Error()), nil
118+
}
119+
registry, err := request.RequireString("registry")
120+
if err != nil {
121+
return mcp.NewToolResultError(err.Error()), nil
122+
}
123+
builder, err := request.RequireString("builder")
124+
if err != nil {
125+
return mcp.NewToolResultError(err.Error()), nil
126+
}
127+
cmd := exec.Command("func", "deploy", "--registry", registry, "--builder", builder)
128+
cmd.Dir = cwd
129+
out, err := cmd.Output()
130+
if err != nil {
131+
return mcp.NewToolResultError(err.Error()), nil
132+
}
133+
body := []byte(fmt.Sprintf(`{"result": "%s"}`, out))
134+
return mcp.NewToolResultText(string(body)), nil
135+
}

0 commit comments

Comments
 (0)