|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "kagent.dev/kmcp/pkg/manifest" |
| 12 | +) |
| 13 | + |
| 14 | +var runCmd = &cobra.Command{ |
| 15 | + Use: "run", |
| 16 | + Short: "Run MCP server locally", |
| 17 | + Long: `Run an MCP server locally using the Model Context Protocol inspector. |
| 18 | +
|
| 19 | +This command will: |
| 20 | +1. Load the kmcp.yaml configuration from the project directory |
| 21 | +2. Determine the framework type and create appropriate configuration |
| 22 | +3. Run the MCP server using the Model Context Protocol inspector |
| 23 | +
|
| 24 | +Supported frameworks: |
| 25 | +- fastmcp-python: Requires uv to be installed |
| 26 | +- mcp-go: Requires Go to be installed |
| 27 | +
|
| 28 | +Examples: |
| 29 | + kmcp run --project-dir ./my-project # Run from specific directory`, |
| 30 | + RunE: executeRun, |
| 31 | +} |
| 32 | + |
| 33 | +var ( |
| 34 | + projectDir string |
| 35 | +) |
| 36 | + |
| 37 | +func init() { |
| 38 | + rootCmd.AddCommand(runCmd) |
| 39 | + |
| 40 | + runCmd.Flags().StringVarP( |
| 41 | + &projectDir, |
| 42 | + "project-dir", |
| 43 | + "d", |
| 44 | + "", |
| 45 | + "Project directory to use (default: current directory)", |
| 46 | + ) |
| 47 | +} |
| 48 | + |
| 49 | +func executeRun(_ *cobra.Command, _ []string) error { |
| 50 | + projectDir, err := getProjectDir() |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + manifest, err := getProjectManifest(projectDir) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + // Check if npx is installed |
| 61 | + if err := checkNpxInstalled(); err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + // Determine framework and create configuration |
| 66 | + switch manifest.Framework { |
| 67 | + case "fastmcp-python": |
| 68 | + return runFastMCPPython(projectDir, manifest) |
| 69 | + case "mcp-go": |
| 70 | + return runMCPGo(projectDir, manifest) |
| 71 | + default: |
| 72 | + return fmt.Errorf("unsupported framework: %s", manifest.Framework) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func checkNpxInstalled() error { |
| 77 | + cmd := exec.Command("npx", "--version") |
| 78 | + if err := cmd.Run(); err != nil { |
| 79 | + return fmt.Errorf("npx is required to run the modelcontextinstaller. Please install Node.js and npm to get npx") |
| 80 | + } |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +// createMCPInspectorConfig creates an MCP inspector configuration file |
| 85 | +func createMCPInspectorConfig(serverName string, serverConfig map[string]interface{}, configPath string) error { |
| 86 | + config := map[string]interface{}{ |
| 87 | + "mcpServers": map[string]interface{}{ |
| 88 | + serverName: serverConfig, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + configData, err := json.MarshalIndent(config, "", " ") |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("failed to marshal config: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + if err := os.WriteFile(configPath, configData, 0644); err != nil { |
| 98 | + return fmt.Errorf("failed to write mcp-server-config.json: %w", err) |
| 99 | + } |
| 100 | + |
| 101 | + if verbose { |
| 102 | + fmt.Printf("Created mcp-server-config.json: %s\n", configPath) |
| 103 | + } |
| 104 | + |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +// runMCPInspector runs the MCP inspector with the given configuration |
| 109 | +func runMCPInspector(configPath, serverName string, workingDir string) error { |
| 110 | + args := []string{ |
| 111 | + "@modelcontextprotocol/inspector", |
| 112 | + "--config", configPath, |
| 113 | + "--server", serverName, |
| 114 | + } |
| 115 | + |
| 116 | + if verbose { |
| 117 | + fmt.Printf("Running: npx %s\n", args) |
| 118 | + } |
| 119 | + |
| 120 | + cmd := exec.Command("npx", args...) |
| 121 | + if workingDir != "" { |
| 122 | + cmd.Dir = workingDir |
| 123 | + } |
| 124 | + cmd.Stdout = os.Stdout |
| 125 | + cmd.Stderr = os.Stderr |
| 126 | + |
| 127 | + // Run synchronously |
| 128 | + return cmd.Run() |
| 129 | +} |
| 130 | + |
| 131 | +func runFastMCPPython(projectDir string, manifest *manifest.ProjectManifest) error { |
| 132 | + // Check if uv is available |
| 133 | + if _, err := exec.LookPath("uv"); err != nil { |
| 134 | + uvInstallURL := "https://docs.astral.sh/uv/getting-started/installation/" |
| 135 | + return fmt.Errorf( |
| 136 | + "uv is required for this command to run fastmcp-python projects locally. Please install uv: %s", uvInstallURL, |
| 137 | + ) |
| 138 | + } |
| 139 | + |
| 140 | + // Run uv sync first |
| 141 | + if verbose { |
| 142 | + fmt.Printf("Running uv sync in: %s\n", projectDir) |
| 143 | + } |
| 144 | + syncCmd := exec.Command("uv", "sync") |
| 145 | + syncCmd.Dir = projectDir |
| 146 | + syncCmd.Stdout = os.Stdout |
| 147 | + syncCmd.Stderr = os.Stderr |
| 148 | + if err := syncCmd.Run(); err != nil { |
| 149 | + return fmt.Errorf("failed to run uv sync: %w", err) |
| 150 | + } |
| 151 | + |
| 152 | + // Create server configuration for local execution |
| 153 | + serverConfig := map[string]interface{}{ |
| 154 | + "command": "uv", |
| 155 | + "args": []string{"run", "python", "src/main.py"}, |
| 156 | + } |
| 157 | + |
| 158 | + // Create MCP inspector config |
| 159 | + configPath := filepath.Join(projectDir, "mcp-server-config.json") |
| 160 | + if err := createMCPInspectorConfig(manifest.Name, serverConfig, configPath); err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + |
| 164 | + // Run the inspector |
| 165 | + return runMCPInspector(configPath, manifest.Name, projectDir) |
| 166 | +} |
| 167 | + |
| 168 | +func runMCPGo(projectDir string, manifest *manifest.ProjectManifest) error { |
| 169 | + // Check if go is available |
| 170 | + if _, err := exec.LookPath("go"); err != nil { |
| 171 | + goInstallURL := "https://golang.org/doc/install" |
| 172 | + return fmt.Errorf("go is required to run mcp-go projects locally. Please install Go: %s", goInstallURL) |
| 173 | + } |
| 174 | + |
| 175 | + // Run go mod tidy first to ensure dependencies are up to date |
| 176 | + if verbose { |
| 177 | + fmt.Printf("Running go mod tidy in: %s\n", projectDir) |
| 178 | + } |
| 179 | + tidyCmd := exec.Command("go", "mod", "tidy") |
| 180 | + tidyCmd.Dir = projectDir |
| 181 | + tidyCmd.Stdout = os.Stdout |
| 182 | + tidyCmd.Stderr = os.Stderr |
| 183 | + if err := tidyCmd.Run(); err != nil { |
| 184 | + return fmt.Errorf("failed to run go mod tidy: %w", err) |
| 185 | + } |
| 186 | + |
| 187 | + // Create server configuration for local execution |
| 188 | + serverConfig := map[string]interface{}{ |
| 189 | + "command": "go", |
| 190 | + "args": []string{"run", "main.go"}, |
| 191 | + } |
| 192 | + |
| 193 | + // Create MCP inspector config |
| 194 | + configPath := filepath.Join(projectDir, "mcp-server-config.json") |
| 195 | + if err := createMCPInspectorConfig(manifest.Name, serverConfig, configPath); err != nil { |
| 196 | + return err |
| 197 | + } |
| 198 | + |
| 199 | + // Run the inspector |
| 200 | + return runMCPInspector(configPath, manifest.Name, projectDir) |
| 201 | +} |
| 202 | + |
| 203 | +func getProjectDir() (string, error) { |
| 204 | + // Determine project directory |
| 205 | + dir := projectDir |
| 206 | + if dir == "" { |
| 207 | + // Use current working directory |
| 208 | + var err error |
| 209 | + dir, err = os.Getwd() |
| 210 | + if err != nil { |
| 211 | + return "", fmt.Errorf("failed to get current directory: %w", err) |
| 212 | + } |
| 213 | + } else { |
| 214 | + // Convert relative path to absolute path |
| 215 | + if !filepath.IsAbs(dir) { |
| 216 | + cwd, err := os.Getwd() |
| 217 | + if err != nil { |
| 218 | + return "", fmt.Errorf("failed to get current directory: %w", err) |
| 219 | + } |
| 220 | + dir = filepath.Join(cwd, dir) |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + if verbose { |
| 225 | + fmt.Printf("Using project directory: %s\n", dir) |
| 226 | + } |
| 227 | + |
| 228 | + return dir, nil |
| 229 | +} |
| 230 | + |
| 231 | +func getProjectManifest(projectDir string) (*manifest.ProjectManifest, error) { |
| 232 | + // Check if kmcp.yaml exists |
| 233 | + manager := manifest.NewManager(projectDir) |
| 234 | + if !manager.Exists() { |
| 235 | + return nil, fmt.Errorf("this directory is not an mcp-server directory: kmcp.yaml not found") |
| 236 | + } |
| 237 | + |
| 238 | + // Load the manifest |
| 239 | + manifest, err := manager.Load() |
| 240 | + if err != nil { |
| 241 | + return nil, fmt.Errorf("failed to load kmcp.yaml: %w", err) |
| 242 | + } |
| 243 | + |
| 244 | + return manifest, nil |
| 245 | +} |
0 commit comments