-
Notifications
You must be signed in to change notification settings - Fork 99
feat(modes): per-mode MCP server restrictions (allowlist) #453
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
74703b6
1036aa4
8eeaa29
948708e
0845b67
a24e87e
1a5018b
778d95d
95a880e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { modeConfigSchema } from "../mode.js" | ||
|
|
||
| describe("modeConfigSchema allowedMcpServers", () => { | ||
| const baseModeConfig = { | ||
| slug: "test-mode", | ||
| name: "Test Mode", | ||
| roleDefinition: "A test mode", | ||
| groups: ["read" as const], | ||
| } | ||
|
|
||
| it("should accept valid allowedMcpServers array of strings", () => { | ||
| const result = modeConfigSchema.safeParse({ | ||
| ...baseModeConfig, | ||
| allowedMcpServers: ["server1", "server2"], | ||
| }) | ||
| expect(result.success).toBe(true) | ||
| if (result.success) { | ||
| expect(result.data.allowedMcpServers).toEqual(["server1", "server2"]) | ||
| } | ||
| }) | ||
|
|
||
| it("should accept missing/undefined allowedMcpServers", () => { | ||
| const result = modeConfigSchema.safeParse(baseModeConfig) | ||
| expect(result.success).toBe(true) | ||
| if (result.success) { | ||
| expect(result.data.allowedMcpServers).toBeUndefined() | ||
| } | ||
| }) | ||
|
|
||
| it("should accept empty allowedMcpServers array", () => { | ||
| const result = modeConfigSchema.safeParse({ | ||
| ...baseModeConfig, | ||
| allowedMcpServers: [], | ||
| }) | ||
| expect(result.success).toBe(true) | ||
| if (result.success) { | ||
| expect(result.data.allowedMcpServers).toEqual([]) | ||
| } | ||
| }) | ||
|
|
||
| it("should reject non-string array items", () => { | ||
| const result = modeConfigSchema.safeParse({ | ||
| ...baseModeConfig, | ||
| allowedMcpServers: [123, 456], | ||
| }) | ||
| expect(result.success).toBe(false) | ||
| }) | ||
|
|
||
| it("should reject non-array value", () => { | ||
| const result = modeConfigSchema.safeParse({ | ||
| ...baseModeConfig, | ||
| allowedMcpServers: "server1", | ||
| }) | ||
| expect(result.success).toBe(false) | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -220,6 +220,9 @@ export function applyModelToolCustomization( | |
| * @param codeIndexManager - Code index manager for codebase_search feature check | ||
| * @param settings - Additional settings for tool filtering (includes modelInfo for model-specific customization) | ||
| * @param mcpHub - MCP hub for checking available resources | ||
| * @param allowedMcpServers - Optional allowlist of MCP server names for the current mode. When | ||
| * provided, the resource-availability check only considers servers in this list, so a mode that | ||
| * restricts MCP servers cannot retain `access_mcp_resource` based on resources from disallowed servers. | ||
| * @returns Filtered array of tools allowed for the mode | ||
| */ | ||
| export function filterNativeToolsForMode( | ||
|
|
@@ -230,6 +233,7 @@ export function filterNativeToolsForMode( | |
| codeIndexManager?: CodeIndexManager, | ||
| settings?: Record<string, any>, | ||
| mcpHub?: McpHub, | ||
| allowedMcpServers?: string[], | ||
| ): OpenAI.Chat.ChatCompletionTool[] { | ||
| // Get mode configuration and all tools for this mode | ||
| const modeSlug = mode ?? defaultModeSlug | ||
|
|
@@ -301,8 +305,13 @@ export function filterNativeToolsForMode( | |
| } | ||
| } | ||
|
|
||
| // Conditionally exclude access_mcp_resource if MCP is not enabled or there are no resources | ||
| if (!mcpHub || !hasAnyMcpResources(mcpHub)) { | ||
| // Conditionally exclude access_mcp_resource if MCP is not enabled or there are no resources. | ||
| // When the mode restricts MCP servers via allowedMcpServers, only resources from allowed | ||
| // servers count — otherwise a restricted mode could still read resources from disallowed servers. | ||
| // Fall back to the mode config's own allowlist when the caller omits the parameter, so the | ||
| // restriction is enforced regardless of call site (defense in depth). | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The "defense in depth" framing here is great, but right now the defense only has one layer — the listing/filtering layer. Had we considered adding an invocation-time guard as well? The existing |
||
| const effectiveAllowedMcpServers = allowedMcpServers ?? modeConfig.allowedMcpServers | ||
| if (!mcpHub || !hasAnyMcpResources(mcpHub, effectiveAllowedMcpServers)) { | ||
| allowedToolNames.delete("access_mcp_resource") | ||
| } | ||
|
|
||
|
|
@@ -330,10 +339,18 @@ export function filterNativeToolsForMode( | |
| } | ||
|
|
||
| /** | ||
| * Helper function to check if any MCP server has resources available | ||
| * Helper function to check if any MCP server has resources available. | ||
| * | ||
| * When `allowedServers` is provided, only servers whose name is in the allowlist are considered. | ||
| * This keeps the `access_mcp_resource` availability check consistent with the mode's MCP server | ||
| * allowlist so a restricted mode cannot retain the tool based on resources from disallowed servers. | ||
| */ | ||
| function hasAnyMcpResources(mcpHub: McpHub): boolean { | ||
| const servers = mcpHub.getServers() | ||
| function hasAnyMcpResources(mcpHub: McpHub, allowedServers?: string[]): boolean { | ||
| let servers = mcpHub.getServers() | ||
| if (allowedServers) { | ||
| const allowSet = new Set(allowedServers) | ||
| servers = servers.filter((server) => allowSet.has(server.name)) | ||
| } | ||
| return servers.some((server) => server.resources && server.resources.length > 0) | ||
| } | ||
|
|
||
|
|
||
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.
nit:
new Set(allowedMcpServers)is reconstructed for every server inside the.filter(). The two sibling call sites in this PR (filter-tools-for-mode.ts:351andmcp_server.ts:23) both hoist the Set outside the lambda.