66 "os/exec"
77 "path/filepath"
88
9- "github.com/kagent-dev/kmcp/pkg/cli/internal/manifest"
109 "github.com/spf13/cobra"
10+
11+ "github.com/kagent-dev/kmcp/pkg/cli/internal/manifest"
1112)
1213
1314var runCmd = & cobra.Command {
@@ -29,13 +30,15 @@ Supported frameworks:
2930
3031Examples:
3132 kmcp run --project-dir ./my-project # Run with inspector (default)
32- kmcp run --no-inspector # Run server directly without inspector` ,
33+ kmcp run --no-inspector # Run server directly without inspector
34+ kmcp run --transport http # Run with HTTP transport` ,
3335 RunE : executeRun ,
3436}
3537
3638var (
37- projectDir string
38- noInspector bool
39+ projectDir string
40+ noInspector bool
41+ runTransport string
3942)
4043
4144func init () {
@@ -54,6 +57,12 @@ func init() {
5457 false ,
5558 "Run the server directly without launching the MCP inspector" ,
5659 )
60+ runCmd .Flags ().StringVar (
61+ & runTransport ,
62+ "transport" ,
63+ "stdio" ,
64+ "Transport mode (stdio or http)" ,
65+ )
5766}
5867
5968func executeRun (_ * cobra.Command , _ []string ) error {
@@ -82,6 +91,8 @@ func executeRun(_ *cobra.Command, _ []string) error {
8291 return runMCPGo (projectDir , manifest )
8392 case "typescript" :
8493 return runTypeScript (projectDir , manifest )
94+ case "java" :
95+ return runJava (projectDir , manifest )
8596 default :
8697 return fmt .Errorf ("unsupported framework: %s" , manifest .Framework )
8798 }
@@ -279,3 +290,73 @@ func runTypeScript(projectDir string, manifest *manifest.ProjectManifest) error
279290 // Run the inspector
280291 return runMCPInspector (configPath , manifest .Name , projectDir )
281292}
293+
294+ func runJava (projectDir string , manifest * manifest.ProjectManifest ) error {
295+ // Check if mvn is available
296+ if _ , err := exec .LookPath ("mvn" ); err != nil {
297+ mvnInstallURL := "https://maven.apache.org/install.html"
298+ return fmt .Errorf ("mvn is required to run Java projects locally. Please install Maven: %s" , mvnInstallURL )
299+ }
300+
301+ // Run mvn clean install first to ensure dependencies are up to date
302+ if Verbose {
303+ fmt .Printf ("Running mvn clean install in: %s\n " , projectDir )
304+ }
305+ installCmd := exec .Command ("mvn" , "clean" , "install" , "-DskipTests" )
306+ installCmd .Dir = projectDir
307+ installCmd .Stdout = os .Stdout
308+ installCmd .Stderr = os .Stderr
309+ if err := installCmd .Run (); err != nil {
310+ return fmt .Errorf ("failed to run mvn clean install: %w" , err )
311+ }
312+
313+ // Prepare Maven arguments based on transport mode
314+ mavenArgs := []string {"-q" , "exec:java" , "-Dexec.mainClass=com.example.Main" }
315+ if runTransport == "http" {
316+ mavenArgs = append (mavenArgs , "-Dexec.args=--transport http --host 0.0.0.0 --port 3000" )
317+ }
318+
319+ if noInspector {
320+ // Run the server directly
321+ if runTransport == "http" {
322+ fmt .Printf ("Running server directly: mvn exec:java -Dexec.mainClass=\" com.example.Main\" --transport http --host 0.0.0.0 --port 3000\n " )
323+ fmt .Printf ("Server is running on http://localhost:3000\n " )
324+ fmt .Printf ("Health check: http://localhost:3000/health\n " )
325+ fmt .Printf ("MCP endpoint: http://localhost:3000/mcp\n " )
326+ } else {
327+ fmt .Printf ("Running server directly: mvn exec:java -Dexec.mainClass=\" com.example.Main\" \n " )
328+ fmt .Printf ("Server is running and waiting for MCP protocol input on stdin...\n " )
329+ }
330+ fmt .Printf ("Press Ctrl+C to stop the server\n " )
331+
332+ serverCmd := exec .Command ("mvn" , mavenArgs ... )
333+ serverCmd .Dir = projectDir
334+ serverCmd .Stdout = os .Stdout
335+ serverCmd .Stderr = os .Stderr
336+ serverCmd .Stdin = os .Stdin
337+ return serverCmd .Run ()
338+ }
339+
340+ // Create server configuration for inspector
341+ var serverConfig map [string ]interface {}
342+ if runTransport == "http" {
343+ serverConfig = map [string ]interface {}{
344+ "type" : "streamable-http" ,
345+ "url" : "http://localhost:3000/mcp" ,
346+ }
347+ } else {
348+ serverConfig = map [string ]interface {}{
349+ "command" : "mvn" ,
350+ "args" : mavenArgs ,
351+ }
352+ }
353+
354+ // Create MCP inspector config
355+ configPath := filepath .Join (projectDir , "mcp-server-config.json" )
356+ if err := createMCPInspectorConfig (manifest .Name , serverConfig , configPath ); err != nil {
357+ return err
358+ }
359+
360+ // Run the inspector
361+ return runMCPInspector (configPath , manifest .Name , projectDir )
362+ }
0 commit comments