Skip to content

Commit e9e88e5

Browse files
authored
mcp: adds build tool (knative#2865)
Signed-off-by: kapil <[email protected]>
1 parent 20479af commit e9e88e5

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

pkg/mcp/mcp.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ func NewServer() *MCPServer {
7272
handleListTool,
7373
)
7474

75+
mcpServer.AddTool(
76+
mcp.NewTool("build",
77+
mcp.WithDescription("Builds the function image in the current directory"),
78+
mcp.WithString("cwd",
79+
mcp.Required(),
80+
mcp.Description("Current working directory of the MCP client"),
81+
),
82+
mcp.WithString("builder",
83+
mcp.Required(),
84+
mcp.Description("Builder to be used to build the function image"),
85+
),
86+
mcp.WithString("registry",
87+
mcp.Required(),
88+
mcp.Description("Name of the registry to be used to push the function image"),
89+
),
90+
),
91+
handleBuildTool,
92+
)
93+
7594
return &MCPServer{
7695
server: mcpServer,
7796
}
@@ -154,3 +173,30 @@ func handleListTool(
154173
body := []byte(fmt.Sprintf(`{"result": "%s"}`, out))
155174
return mcp.NewToolResultText(string(body)), nil
156175
}
176+
177+
func handleBuildTool(
178+
ctx context.Context,
179+
request mcp.CallToolRequest,
180+
) (*mcp.CallToolResult, error) {
181+
cwd, err := request.RequireString("cwd")
182+
if err != nil {
183+
return mcp.NewToolResultError(err.Error()), nil
184+
}
185+
builder, err := request.RequireString("builder")
186+
if err != nil {
187+
return mcp.NewToolResultError(err.Error()), nil
188+
}
189+
registry, err := request.RequireString("registry")
190+
if err != nil {
191+
return mcp.NewToolResultError(err.Error()), nil
192+
}
193+
194+
cmd := exec.Command("func", "build", "--builder", builder, "--registry", registry)
195+
cmd.Dir = cwd
196+
out, err := cmd.Output()
197+
if err != nil {
198+
return mcp.NewToolResultError(err.Error()), nil
199+
}
200+
body := []byte(fmt.Sprintf(`{"result": "%s"}`, out))
201+
return mcp.NewToolResultText(string(body)), nil
202+
}

0 commit comments

Comments
 (0)