Skip to content

Commit 4d22684

Browse files
authored
init mcp client test (#1429)
1 parent df09f1e commit 4d22684

File tree

5 files changed

+654
-44
lines changed

5 files changed

+654
-44
lines changed

pkgs/defang/cli.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ buildGoModule {
77
pname = "defang-cli";
88
version = "git";
99
src = ../../src;
10-
vendorHash = "sha256-2BQght2PFjXOwV7K7C74hc3yFvIS46N15qoUmkxZAe8="; # TODO: use fetchFromGitHub
10+
vendorHash = "sha256-m8xBhpKS6Tvoik2WZbkWgM52/FkCEhbdFNRwByi30zw="; # TODO: use fetchFromGitHub
1111

1212
subPackages = [ "cmd/cli" ];
1313

src/cmd/cli/command/mcp.go

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ import (
88
cliClient "github.com/DefangLabs/defang/src/pkg/cli/client"
99
"github.com/DefangLabs/defang/src/pkg/login"
1010
"github.com/DefangLabs/defang/src/pkg/mcp"
11-
"github.com/DefangLabs/defang/src/pkg/mcp/prompts"
12-
"github.com/DefangLabs/defang/src/pkg/mcp/resources"
13-
"github.com/DefangLabs/defang/src/pkg/mcp/tools"
1411
"github.com/DefangLabs/defang/src/pkg/term"
1512
"github.com/mark3labs/mcp-go/server"
1613
"github.com/spf13/cobra"
@@ -42,48 +39,14 @@ var mcpServerCmd = &cobra.Command{
4239
term.DefaultTerm = term.NewTerm(os.Stdin, logFile, logFile)
4340
}
4441

45-
// Setup knowledge base
46-
term.Debug("Setting up knowledge base")
47-
if err := mcp.SetupKnowledgeBase(); err != nil {
48-
return fmt.Errorf("failed to setup knowledge base: %w", err)
49-
}
42+
cluster := getCluster()
5043

5144
// Create a new MCP server
5245
term.Debug("Creating MCP server")
53-
s := server.NewMCPServer(
54-
"Deploy with Defang",
55-
RootCmd.Version,
56-
server.WithResourceCapabilities(true, true), // Enable resource management and notifications
57-
server.WithPromptCapabilities(true), // Enable interactive prompts
58-
server.WithToolCapabilities(true), // Enable dynamic tool list updates
59-
server.WithInstructions(`
60-
Defang provides tools for deploying web applications to cloud providers (AWS, GCP, Digital Ocean) using a compose.yaml file.
61-
62-
There are a number of available tools to help with deployment, configuration, and manage applications deployed with Defang.
63-
64-
deploy - This tool deploys a web application to the cloud using the compose.yaml file in the application's working directory.
65-
destroy - This tool spins down and removes a deployed project from the cloud, cleaning up all associated resources.
66-
estimate - This tool estimates the cost of running a deployed application based on its resource usage and cloud provider pricing.
67-
services - This tool lists all running services for a deployed application, providing status and resource usage information
68-
list_configs - This tool lists all configuration variables for a deployed application, allowing you to view current settings.
69-
remove_config - This tool removes a configuration variable for a deployed application, allowing you to clean up unused settings.
70-
set_config - This tool sets or updates configuration variables for a deployed application, allowing you to manage environment variables and secrets.
71-
`),
72-
)
73-
74-
cluster := getCluster()
75-
76-
// Setup resources
77-
term.Debug("Setting up resources")
78-
resources.SetupResources(s)
79-
80-
//setup prompts
81-
term.Debug("Setting up prompts")
82-
prompts.SetupPrompts(s, cluster, &providerID)
83-
84-
// Setup tools
85-
term.Debug("Setting up tools")
86-
tools.SetupTools(s, cluster, authPort, &providerID)
46+
s, err := mcp.NewDefangMCPServer(RootCmd.Version, cluster, authPort, &providerID)
47+
if err != nil {
48+
return fmt.Errorf("failed to create MCP server: %w", err)
49+
}
8750

8851
// Start auth server for docker login flow
8952
if authPort != 0 {

src/pkg/mcp/mcp_server.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package mcp
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/DefangLabs/defang/src/pkg/mcp/prompts"
7+
"github.com/DefangLabs/defang/src/pkg/mcp/resources"
8+
"github.com/DefangLabs/defang/src/pkg/mcp/tools"
9+
"github.com/DefangLabs/defang/src/pkg/term"
10+
"github.com/mark3labs/mcp-go/server"
11+
12+
// NewDefangMCPServer returns a new MCPServer instance with all resources, tools, and prompts registered.
13+
cliClient "github.com/DefangLabs/defang/src/pkg/cli/client"
14+
)
15+
16+
func NewDefangMCPServer(version string, cluster string, authPort int, providerID *cliClient.ProviderID) (*server.MCPServer, error) {
17+
instructions := `Defang provides tools for deploying web applications to cloud providers (AWS, GCP, Digital Ocean) using a compose.yaml file.
18+
19+
There are a number of available tools to help with deployment, configuration, and manage applications deployed with Defang.
20+
21+
deploy - This tool deploys a web application to the cloud using the compose.yaml file in the application's working directory.
22+
destroy - This tool spins down and removes a deployed project from the cloud, cleaning up all associated resources.
23+
estimate - This tool estimates the cost of running a deployed application based on its resource usage and cloud provider pricing.
24+
services - This tool lists all running services for a deployed application, providing status and resource usage information
25+
list_configs - This tool lists all configuration variables for a deployed application, allowing you to view current settings.
26+
remove_config - This tool removes a configuration variable for a deployed application, allowing you to clean up unused settings.
27+
set_config - This tool sets or updates configuration variables for a deployed application, allowing you to manage environment variables and secrets.`
28+
29+
// Setup knowledge base
30+
term.Debug("Setting up knowledge base")
31+
if err := SetupKnowledgeBase(); err != nil {
32+
return nil, fmt.Errorf("failed to setup knowledge base: %w", err)
33+
}
34+
35+
s := server.NewMCPServer(
36+
"Deploy with Defang",
37+
version,
38+
server.WithResourceCapabilities(true, true),
39+
server.WithPromptCapabilities(true),
40+
server.WithToolCapabilities(true),
41+
server.WithInstructions(instructions),
42+
)
43+
44+
// Setup resources
45+
term.Debug("Setting up resources")
46+
resources.SetupResources(s)
47+
48+
//setup prompts
49+
term.Debug("Setting up prompts")
50+
prompts.SetupPrompts(s, cluster, providerID)
51+
52+
// Setup tools
53+
term.Debug("Setting up tools")
54+
tools.SetupTools(s, cluster, authPort, providerID)
55+
return s, nil
56+
}

0 commit comments

Comments
 (0)