Repository navigation
Opt-in: use the WebSocket password OBS saved #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| * Modified for the independent OBS MCP project in August 2026. | ||
| * See NOTICE.md and Git history for authorship and change dates. | ||
| * SPDX-License-Identifier: GPL-2.0-only | ||
| */ | ||
| import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { obsWebSocketConfigPaths, readObsConfigPassword, resolveObsPassword } from "./obs-config.js"; | ||
|
|
||
| const SECRET = "s3cret-from-obs"; | ||
| let directory: string; | ||
| let stderr: ReturnType<typeof vi.spyOn>; | ||
|
|
||
| function configFile(contents: string): string { | ||
| const path = join(directory, `config-${Math.random()}.json`); | ||
| writeFileSync(path, contents); | ||
| return path; | ||
| } | ||
|
|
||
| function logged(): string { | ||
| return stderr.mock.calls.map((call: unknown[]) => String(call[0])).join("\n"); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| directory = mkdtempSync(join(tmpdir(), "obs-mcp-config-")); | ||
| stderr = vi.spyOn(console, "error").mockImplementation(() => undefined); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| rmSync(directory, { recursive: true, force: true }); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe("obsWebSocketConfigPaths", () => { | ||
| it("follows each platform's OBS config directory", () => { | ||
| expect(obsWebSocketConfigPaths({}, "darwin", "/Users/me")).toEqual([ | ||
| "/Users/me/Library/Application Support/obs-studio/plugin_config/obs-websocket/config.json", | ||
| ]); | ||
| expect(obsWebSocketConfigPaths({ APPDATA: "C:/Users/me/AppData/Roaming" }, "win32", "C:/Users/me")[0]) | ||
| .toContain(join("C:/Users/me/AppData/Roaming", "obs-studio")); | ||
| expect(obsWebSocketConfigPaths({ XDG_CONFIG_HOME: "/xdg" }, "linux", "/home/me")).toEqual([ | ||
| "/xdg/obs-studio/plugin_config/obs-websocket/config.json", | ||
| "/home/me/.var/app/com.obsproject.Studio/config/obs-studio/plugin_config/obs-websocket/config.json", | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("readObsConfigPassword", () => { | ||
| it("returns the first readable password", () => { | ||
| const path = configFile(JSON.stringify({ auth_required: true, server_password: SECRET })); | ||
|
|
||
| expect(readObsConfigPassword([join(directory, "missing.json"), path])) | ||
| .toEqual({ kind: "password", password: SECRET, path }); | ||
| }); | ||
|
|
||
| it("reports that authentication is off", () => { | ||
| const path = configFile(JSON.stringify({ auth_required: false, server_password: SECRET })); | ||
|
|
||
| expect(readObsConfigPassword([path])).toEqual({ kind: "no-auth", path }); | ||
| }); | ||
|
|
||
| it("skips malformed files and empty passwords", () => { | ||
| const paths = [configFile("{not json"), configFile(JSON.stringify({ server_password: "" }))]; | ||
|
|
||
| expect(readObsConfigPassword(paths)).toEqual({ kind: "not-found", searched: paths }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("resolveObsPassword", () => { | ||
| const found = () => ({ kind: "password" as const, password: SECRET, path: "/obs/config.json" }); | ||
|
|
||
| it("prefers OBS_WEBSOCKET_PASSWORD and ignores the config file", () => { | ||
| const read = vi.fn(found); | ||
|
|
||
| expect(resolveObsPassword({ OBS_WEBSOCKET_PASSWORD: "env", OBS_MCP_READ_OBS_CONFIG: "1" }, read)).toBe("env"); | ||
| expect(read).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("does not read OBS's config unless asked to", () => { | ||
| const read = vi.fn(found); | ||
|
|
||
| expect(resolveObsPassword({}, read)).toBeNull(); | ||
| expect(read).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("uses OBS's password when enabled, logging only where it came from", () => { | ||
| expect(resolveObsPassword({ OBS_MCP_READ_OBS_CONFIG: "true" }, found)).toBe(SECRET); | ||
| expect(logged()).toContain("/obs/config.json"); | ||
| expect(logged()).not.toContain(SECRET); | ||
| }); | ||
|
|
||
| it("explains where it looked when nothing was found", () => { | ||
| const result = resolveObsPassword( | ||
| { OBS_MCP_READ_OBS_CONFIG: "yes" }, | ||
| () => ({ kind: "not-found", searched: ["/a.json", "/b.json"] }), | ||
| ); | ||
|
|
||
| expect(result).toBeNull(); | ||
| expect(logged()).toContain("/a.json, /b.json"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| * Modified for the independent OBS MCP project in August 2026. | ||
| * See NOTICE.md and Git history for authorship and change dates. | ||
| * SPDX-License-Identifier: GPL-2.0-only | ||
| */ | ||
| import { readFileSync } from "node:fs"; | ||
| import { homedir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { logger } from "./logger.js"; | ||
|
|
||
| const CONFIG_SUBPATH = ["obs-studio", "plugin_config", "obs-websocket", "config.json"]; | ||
|
|
||
| function isEnabled(value: string | undefined): boolean { | ||
| return ["1", "true", "yes"].includes((value ?? "").trim().toLowerCase()); | ||
| } | ||
|
|
||
| /** | ||
| * Where obs-websocket keeps config.json, most likely first. OBS stores it | ||
| * under its user config directory (os_get_config_path), and the Linux | ||
| * Flatpak build keeps that inside its sandbox. | ||
| */ | ||
| export function obsWebSocketConfigPaths( | ||
| env: NodeJS.ProcessEnv = process.env, | ||
| platform: NodeJS.Platform = process.platform, | ||
| home = homedir(), | ||
| ): string[] { | ||
| if (platform === "darwin") return [join(home, "Library", "Application Support", ...CONFIG_SUBPATH)]; | ||
| if (platform === "win32") { | ||
| return [join(env.APPDATA ?? join(home, "AppData", "Roaming"), ...CONFIG_SUBPATH)]; | ||
| } | ||
| return [ | ||
| join(env.XDG_CONFIG_HOME || join(home, ".config"), ...CONFIG_SUBPATH), | ||
| join(home, ".var", "app", "com.obsproject.Studio", "config", ...CONFIG_SUBPATH), | ||
| ]; | ||
| } | ||
|
|
||
| export type ObsConfigPassword = | ||
| | { kind: "password"; password: string; path: string } | ||
| | { kind: "no-auth"; path: string } | ||
| | { kind: "not-found"; searched: string[] }; | ||
|
|
||
| type ReadFile = (path: string) => string; | ||
|
|
||
| /** | ||
| * Reads the password OBS itself uses from obs-websocket's config.json. | ||
| * Unreadable or malformed files count as not found; the error text never | ||
| * includes file contents. | ||
| */ | ||
| export function readObsConfigPassword( | ||
| paths: readonly string[] = obsWebSocketConfigPaths(), | ||
| readFile: ReadFile = (path) => readFileSync(path, "utf8"), | ||
| ): ObsConfigPassword { | ||
| for (const path of paths) { | ||
| let config: unknown; | ||
| try { | ||
| config = JSON.parse(readFile(path)); | ||
| } catch { | ||
| continue; | ||
| } | ||
| if (typeof config !== "object" || config === null) continue; | ||
| const { auth_required: authRequired, server_password: password } = config as Record<string, unknown>; | ||
| if (authRequired === false) return { kind: "no-auth", path }; | ||
| if (typeof password === "string" && password.length > 0) return { kind: "password", password, path }; | ||
| } | ||
| return { kind: "not-found", searched: [...paths] }; | ||
| } | ||
|
|
||
| /** | ||
| * OBS_WEBSOCKET_PASSWORD wins. Otherwise, with OBS_MCP_READ_OBS_CONFIG | ||
| * enabled, falls back to the password in OBS's own config so a local agent | ||
| * needs no copy of it. Logs where the password came from, never the value. | ||
| */ | ||
| export function resolveObsPassword( | ||
| env: NodeJS.ProcessEnv = process.env, | ||
| read: () => ObsConfigPassword = () => readObsConfigPassword(obsWebSocketConfigPaths(env)), | ||
| ): string | null { | ||
| if (env.OBS_WEBSOCKET_PASSWORD) return env.OBS_WEBSOCKET_PASSWORD; | ||
| if (!isEnabled(env.OBS_MCP_READ_OBS_CONFIG)) return null; | ||
|
|
||
| const result = read(); | ||
| switch (result.kind) { | ||
| case "password": | ||
| logger.log(`Using the OBS WebSocket password from ${result.path}`); | ||
| return result.password; | ||
| case "no-auth": | ||
| logger.log(`OBS WebSocket authentication is off according to ${result.path}`); | ||
| return null; | ||
| case "not-found": | ||
| logger.error(`OBS_MCP_READ_OBS_CONFIG is set, but no OBS WebSocket password was found in: ${result.searched.join(", ")}`); | ||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Linux systems retaining both native and Flatpak OBS profiles, a stale native config with
auth_required: falsecauses this early return before the active Flatpak config is examined. If the running Flatpak OBS instance requires the password stored in the second candidate path, the client is constructed without it and every connection attempt fails authentication. Continue checking the remaining candidate paths for a password rather than treating the first no-auth file as authoritative.Useful? React with 馃憤聽/ 馃憥.