runMCPServer starts the MCP server on stdio or HTTP transport
(ctx context.Context, port int, cmdPath string, validateActor bool)
| 69 | |
| 70 | // runMCPServer starts the MCP server on stdio or HTTP transport |
| 71 | func runMCPServer(ctx context.Context, port int, cmdPath string, validateActor bool) error { |
| 72 | mcpServerEnv := withNonInteractiveCIEnv(nil) |
| 73 | |
| 74 | // Get actor from environment variable |
| 75 | actor := os.Getenv("GITHUB_ACTOR") |
| 76 | |
| 77 | if validateActor { |
| 78 | mcpLog.Printf("Actor validation enabled (--validate-actor flag)") |
| 79 | } |
| 80 | |
| 81 | if actor != "" { |
| 82 | mcpLog.Printf("Using actor: %s", actor) |
| 83 | } else { |
| 84 | mcpLog.Print("No actor specified (GITHUB_ACTOR environment variable)") |
| 85 | } |
| 86 | |
| 87 | if port > 0 { |
| 88 | mcpLog.Printf("Starting MCP server on HTTP port %d", port) |
| 89 | } else { |
| 90 | mcpLog.Print("Starting MCP server with stdio transport") |
| 91 | } |
| 92 | |
| 93 | // Determine, log, and validate the binary path only if --cmd flag is not provided |
| 94 | // When --cmd is provided, the user explicitly specified the binary path to use |
| 95 | if cmdPath == "" { |
| 96 | // Attempt to detect the binary path and assign it to cmdPath |
| 97 | // This ensures createMCPServer receives the actual binary path instead of falling back to "gh aw" |
| 98 | detectedPath, err := logAndValidateBinaryPath() |
| 99 | if err == nil && detectedPath != "" { |
| 100 | cmdPath = detectedPath |
| 101 | mcpLog.Printf("Using detected binary path: %s", cmdPath) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Log current working directory |
| 106 | if cwd, err := os.Getwd(); err == nil { |
| 107 | mcpLog.Printf("Current working directory: %s", cwd) |
| 108 | } else { |
| 109 | mcpLog.Printf("WARNING: Failed to get current working directory: %v", err) |
| 110 | } |
| 111 | |
| 112 | // Validate that the CLI and secrets are properly configured |
| 113 | // Note: Validation failures are logged as warnings but don't prevent server startup |
| 114 | // This allows the server to start in test environments or non-repository directories |
| 115 | if err := validateMCPServerConfiguration(ctx, cmdPath, mcpServerEnv); err != nil { |
| 116 | mcpLog.Printf("Configuration validation warning: %v", err) |
| 117 | } |
| 118 | |
| 119 | // Pre-cache lock-file manifests at startup, before any agent can modify the working tree. |
| 120 | // The cache is serialised to a temp file so that each compile subprocess invocation |
| 121 | // can reference it via --prior-manifest-file. |
| 122 | manifestCacheFile := "" |
| 123 | manifestCache := CollectLockFileManifests("") |
| 124 | if len(manifestCache) > 0 { |
| 125 | cacheFile, err := WritePriorManifestFile(manifestCache) |
| 126 | if err != nil { |
| 127 | mcpLog.Printf("Failed to write manifest cache file: %v (safe update will fall back to git HEAD / filesystem)", err) |
| 128 | } else { |
no test coverage detected