| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package main
- import (
- "os"
- "os/signal"
- "syscall"
- "time"
- "github.com/spf13/cobra"
- "gogs.netdome.biz/paul/cheapo/internal/mcpserver"
- )
- // version is the value reported to MCP clients; overridable via -ldflags.
- var version = "dev"
- func mcpCmd() *cobra.Command {
- var (
- httpAddr string
- httpPath string
- token string
- stateless bool
- sessionTimeout time.Duration
- )
- cmd := &cobra.Command{
- Use: "mcp",
- Short: "MCP server (HTTP or stdio) for agents and MCP clients",
- Long: `Exposes the free games data as an MCP server.
- Tools:
- list_offers Mirrors "cheapo list" — mode: free (default), upcoming,
- deals; plus store, country, locale
- The server is stateless: every call returns the full current state;
- deduplication ("what is new?") is the client's job.
- Without --http the server runs over stdio (for clients that start the
- binary themselves). With --http it runs as an HTTP service (Streamable
- HTTP) — the mode for remote clients, multiple users and monitoring.
- Examples:
- cheapo mcp --http 127.0.0.1:8080
- cheapo mcp --http 0.0.0.0:8080 --token "$CHEAPO_MCP_TOKEN"
- cheapo mcp # stdio
- Region/locale/stores come from the global flags (--country, --locale,
- --store) and apply as defaults for the tool calls.`,
- Args: cobra.NoArgs,
- RunE: func(cmd *cobra.Command, _ []string) error {
- ctx, stop := signal.NotifyContext(cmd.Context(), syscall.SIGINT, syscall.SIGTERM)
- defer stop()
- // CHEAPO_MCP_TOKEN may also come from a .env file (godotenv);
- // real environment variables take precedence.
- loadEnv()
- srv := mcpserver.New(mcpserver.Options{
- Store: flagStore,
- Country: flagCountry,
- Locale: flagLocale,
- Version: version,
- })
- if httpAddr == "" {
- return srv.Run(ctx)
- }
- if token == "" {
- token = os.Getenv("CHEAPO_MCP_TOKEN")
- }
- return srv.RunHTTP(ctx, httpAddr, mcpserver.HTTPOptions{
- Path: httpPath,
- Token: token,
- Stateless: stateless,
- SessionTimeout: sessionTimeout,
- })
- },
- }
- cmd.Flags().StringVar(&httpAddr, "http", "", "HTTP address (e.g. 127.0.0.1:8080); empty = stdio")
- cmd.Flags().StringVar(&httpPath, "path", "/mcp", "HTTP path of the MCP endpoint")
- cmd.Flags().StringVar(&token, "token", "", "Bearer token for HTTP (default: $CHEAPO_MCP_TOKEN)")
- cmd.Flags().BoolVar(&stateless, "stateless", false, "HTTP without sessions (for proxies/load balancers)")
- cmd.Flags().DurationVar(&sessionTimeout, "session-timeout", 30*time.Minute, "Idle timeout for HTTP sessions")
- return cmd
- }
|