Class: PromptObjects::CLI::ServeCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/prompt_objects/cli/serve_command.rb

Overview

serve command: run an environment as a web or MCP server.

Instance Method Summary collapse

Instance Method Details

#helpObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/prompt_objects/cli/serve_command.rb', line 98

def help
  prog = Helpers.program_name
  puts <<~HELP
    Usage: #{prog} serve [options] <environment>

    Run an environment as a server, exposing it through different interfaces.

    Arguments:
      environment  Environment name or path to environment directory

    Options:
      --web            Run as web server with React UI (default)
      --mcp            Run as MCP server (stdio transport, for Claude Desktop/Cursor)
      --port PORT      Port for web server (default: 3000)
      --host HOST      Host for web server (default: localhost)
      --open           Open browser automatically
      --help, -h       Show this help message

    Examples:
      #{prog} serve my-assistant              # Web UI (default)
      #{prog} serve my-assistant --port 4000  # Custom port
      #{prog} serve --mcp my-assistant        # MCP server mode
      #{prog} serve ./my-environment          # Serve from path

    Claude Desktop Configuration (MCP mode):
      Add to claude_desktop_config.json:
      {
        "mcpServers": {
          "my-assistant": {
            "command": "#{prog}",
            "args": ["serve", "--mcp", "my-assistant"]
          }
        }
      }
  HELP
end

#run(args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/prompt_objects/cli/serve_command.rb', line 7

def run(args)
  options = {
    web: false,
    mcp: false,
    port: 3000,
    host: "localhost",
    open: false,
    environment: nil
  }

  skip_next = false
  args.each_with_index do |arg, i|
    if skip_next
      skip_next = false
      next
    end

    case arg
    when "--web"
      options[:web] = true
    when "--mcp"
      options[:mcp] = true
    when "--port"
      options[:port] = args[i + 1].to_i
      skip_next = true
    when "--host"
      options[:host] = args[i + 1]
      skip_next = true
    when "--open", "-o"
      options[:open] = true
    when "--help", "-h"
      help
      exit 0
    else
      # Assume it's the environment
      options[:environment] = arg unless arg.start_with?("-")
    end
  end

  unless options[:environment]
    puts "Error: environment name or path required"
    puts "Run '#{Helpers.program_name} serve --help' for usage"
    exit 1
  end

  # Default to web if no interface specified
  options[:web] = true unless options[:mcp]

  # Redirect stderr to a log file to avoid polluting MCP stdio
  if options[:mcp] && ENV["PROMPT_OBJECTS_LOG"]
    $stderr.reopen(ENV["PROMPT_OBJECTS_LOG"], "a")
    $stderr.sync = true
  end

  # Resolve environment
  env_path = Helpers.resolve_environment(options[:environment])
  unless env_path
    $stderr.puts "Error: environment '#{options[:environment]}' not found"
    exit 1
  end

  # Load the runtime
  runtime = PromptObjects::Runtime.new(env_path: env_path)

  # Load all prompt objects
  Helpers.load_all_objects(runtime, env_path)

  if options[:mcp]
    connector = PromptObjects::Connectors::MCP.new(runtime: runtime)
    connector.start
  elsif options[:web]
    require "prompt_objects/server"

    # Rebuild frontend to ensure built assets match source
    rebuild_frontend

    # Open browser if requested
    if options[:open]
      url = "http://#{options[:host]}:#{options[:port]}"
      Helpers.open_in_browser(url)
    end

    PromptObjects::Server.start(
      runtime: runtime,
      host: options[:host],
      port: options[:port],
      env_path: env_path
    )
  end
end