Repository navigation
Turn tool groups on and off at runtime #15
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
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,82 @@ | ||
| /* | ||
| * 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 { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { resultText, startMcpHarness, type McpHarness } from "../../test/support/mcp-harness.js"; | ||
| import { parseToolFilter } from "./toolsets.js"; | ||
|
|
||
| let harness: McpHarness | undefined; | ||
| let listChanges = 0; | ||
|
|
||
| async function start(env: Record<string, string>): Promise<McpHarness> { | ||
| harness = await startMcpHarness({ filter: parseToolFilter(env) }); | ||
| harness.mcpClient.setNotificationHandler("notifications/tools/list_changed", async () => { | ||
| listChanges += 1; | ||
| }); | ||
| harness.fakeObs.respondWith("GetRecordStatus", () => ({ outputActive: false })); | ||
| return harness; | ||
| } | ||
|
|
||
| async function toolNames(): Promise<string[]> { | ||
| return (await harness!.mcpClient.listTools()).tools.map(({ name }) => name); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| vi.spyOn(console, "error").mockImplementation(() => undefined); | ||
| listChanges = 0; | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await harness?.close(); | ||
| harness = undefined; | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe("dynamic tool groups", () => { | ||
| it("parses OBS_MCP_DYNAMIC_TOOLSETS, starting with general unless groups are named", () => { | ||
| expect(parseToolFilter({ OBS_MCP_DYNAMIC_TOOLSETS: "true" })).toMatchObject({ dynamic: true, groups: new Set(["general"]) }); | ||
| expect(parseToolFilter({ OBS_MCP_DYNAMIC_TOOLSETS: "1", OBS_MCP_TOOLSETS: "record" }).groups).toEqual(new Set(["record"])); | ||
| expect(parseToolFilter({}).dynamic).toBe(false); | ||
| }); | ||
|
|
||
| it("starts small, then enables and disables groups with a list-changed notification each time", async () => { | ||
| await start({ OBS_MCP_DYNAMIC_TOOLSETS: "true" }); | ||
| const initial = await toolNames(); | ||
| expect(initial).toEqual(expect.arrayContaining(["obs-get-status", "obs-list-toolsets", "obs-enable-toolset", "obs-disable-toolset"])); | ||
| expect(initial).not.toContain("obs-get-record-status"); | ||
| expect(initial.length).toBeLessThan(30); | ||
|
|
||
| const list = await harness!.call("obs-list-toolsets"); | ||
| expect(resultText(list)).toMatch(/^on general \(\d+\/\d+\)/m); | ||
| expect(resultText(list)).toMatch(/^off record \(0\/\d+\)/m); | ||
|
|
||
| const enabled = await harness!.call("obs-enable-toolset", { groups: ["record"] }); | ||
| expect(resultText(enabled)).toMatch(/Enabled \d+ tool\(s\) in record/); | ||
| await vi.waitFor(() => expect(listChanges).toBeGreaterThan(0)); | ||
| expect(await toolNames()).toContain("obs-get-record-status"); | ||
| expect((await harness!.call("obs-get-record-status")).isError).toBeFalsy(); | ||
|
|
||
| await harness!.call("obs-disable-toolset", { groups: ["record"] }); | ||
| expect(await toolNames()).not.toContain("obs-get-record-status"); | ||
| await expect(harness!.call("obs-get-record-status")).rejects.toThrow("disabled"); | ||
| expect(resultText(await harness!.call("obs-disable-toolset", { groups: ["record"] }))).toContain("already off"); | ||
| }); | ||
|
|
||
| it("enables only read-only tools in read-only mode", async () => { | ||
| await start({ OBS_MCP_DYNAMIC_TOOLSETS: "true", OBS_MCP_READ_ONLY: "true" }); | ||
|
|
||
| await harness!.call("obs-enable-toolset", { groups: ["record"] }); | ||
|
|
||
| const names = await toolNames(); | ||
| expect(names).toContain("obs-get-record-status"); | ||
| expect(names).not.toContain("obs-start-record"); | ||
| }); | ||
|
|
||
| it("adds no group tools without OBS_MCP_DYNAMIC_TOOLSETS", async () => { | ||
| await start({}); | ||
|
|
||
| expect(await toolNames()).not.toContain("obs-enable-toolset"); | ||
| }); | ||
| }); |
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,104 @@ | ||
| /* | ||
| * 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 type { CallToolResult, McpServer } from "@modelcontextprotocol/server"; | ||
| import { z } from "zod"; | ||
| import { READ_ONLY_TOOL } from "./request-tool.js"; | ||
| import { TOOL_GROUPS, type RegisteredTool, type ToolGroup } from "./toolsets.js"; | ||
|
|
||
| const GROUP_DESCRIPTIONS: Record<ToolGroup, string> = { | ||
| general: "status, version, statistics, hotkeys, vendor requests, custom events, sleep", | ||
| scenes: "scenes, canvases, program and preview scene, obs-apply-scene", | ||
| "scene-items": "scene items, groups, transforms, ordering, locking, blend modes, snapshots and restore", | ||
| sources: "source screenshots and active state", | ||
| inputs: "inputs, input settings and properties, audio, deinterlacing, obs-capture-window", | ||
| media: "media input playback", | ||
| filters: "source filters", | ||
| transitions: "transitions, overrides, the T-Bar", | ||
| record: "recording, chapters, preflight, clips, watched takes, trimming, contact sheets", | ||
| stream: "streaming and captions", | ||
| outputs: "virtual camera, replay buffer, and generic outputs", | ||
| config: "profiles, scene collections, video settings, persistent data", | ||
| ui: "studio mode, dialogs, projectors", | ||
| protocol: "describing and calling any OBS request, request batches", | ||
| }; | ||
|
|
||
| const GroupList = z.array(z.enum(TOOL_GROUPS)).min(1); | ||
|
|
||
| function summary(registry: readonly RegisteredTool[]) { | ||
| return TOOL_GROUPS.map((group) => { | ||
| const tools = registry.filter((tool) => tool.group === group && tool.handle); | ||
| const enabled = tools.filter(({ handle }) => handle!.enabled).length; | ||
| return { group, enabled, total: tools.length, covers: GROUP_DESCRIPTIONS[group] }; | ||
| }).filter(({ total }) => total > 0); | ||
| } | ||
|
|
||
| function setGroups(registry: readonly RegisteredTool[], groups: readonly ToolGroup[], enabled: boolean): string[] { | ||
| const changed: string[] = []; | ||
| for (const tool of registry) { | ||
| if (!tool.handle || !groups.includes(tool.group) || tool.handle.enabled === enabled) continue; | ||
| if (enabled) tool.handle.enable(); | ||
| else tool.handle.disable(); | ||
| changed.push(tool.name); | ||
| } | ||
| return changed; | ||
| } | ||
|
|
||
| /** | ||
| * With OBS_MCP_DYNAMIC_TOOLSETS, the session starts with a few groups and | ||
| * these tools turn the others on and off. Each change sends | ||
| * notifications/tools/list_changed, so the client reloads the tool list. | ||
| */ | ||
| export function initialize(server: McpServer, registry: readonly RegisteredTool[]): void { | ||
| server.registerTool( | ||
| "obs-list-toolsets", | ||
| { | ||
| title: "List Tool Groups", | ||
| description: "List the groups of OBS tools, what each covers, and how many of its tools are enabled. " | ||
| + "Enable a group with obs-enable-toolset before using its tools", | ||
| inputSchema: z.object({}), | ||
| annotations: READ_ONLY_TOOL, | ||
| }, | ||
| async (): Promise<CallToolResult> => { | ||
| const groups = summary(registry); | ||
| return { | ||
| content: [{ | ||
| type: "text", | ||
| text: groups.map(({ group, enabled, total, covers }) => ( | ||
| `${enabled === total ? "on " : enabled === 0 ? "off" : "part"} ${group} (${enabled}/${total}): ${covers}` | ||
| )).join("\n"), | ||
| }], | ||
| structuredContent: { groups }, | ||
| }; | ||
| }, | ||
| ); | ||
|
|
||
| const register = (enabled: boolean) => server.registerTool( | ||
| enabled ? "obs-enable-toolset" : "obs-disable-toolset", | ||
| { | ||
| title: enabled ? "Enable Tool Groups" : "Disable Tool Groups", | ||
| description: enabled | ||
| ? "Turn on groups of OBS tools for this session; the client reloads its tool list. See obs-list-toolsets" | ||
| : "Turn off groups of OBS tools for this session to keep the tool list short", | ||
| inputSchema: z.object({ groups: GroupList.describe("Groups from obs-list-toolsets") }), | ||
| annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false }, | ||
| }, | ||
| async ({ groups }): Promise<CallToolResult> => { | ||
| const changed = setGroups(registry, groups, enabled); | ||
| const verb = enabled ? "Enabled" : "Disabled"; | ||
| return { | ||
| content: [{ | ||
| type: "text", | ||
| text: changed.length > 0 | ||
| ? `${verb} ${changed.length} tool(s) in ${groups.join(", ")}: ${changed.join(", ")}` | ||
| : `Nothing to change: ${groups.join(", ")} ${enabled ? "already on" : "already off"}`, | ||
| }], | ||
| structuredContent: { groups, changed }, | ||
| }; | ||
| }, | ||
| ); | ||
| register(true); | ||
| register(false); | ||
| } | ||
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
Oops, something went wrong.
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.
When enabling or disabling a group containing multiple tools, this loop invokes an SDK handle once per tool; each
enable()ordisable()emitsnotifications/tools/list_changed. A client that responds by callingtools/listwill therefore reload the full tool inventory dozens鈥攁nd potentially up to 168鈥攖imes for oneobs-enable-toolsetcall. Batch or suppress these per-tool notifications and emit a single list-change notification after the entire update.Useful? React with 馃憤聽/ 馃憥.