add excludeTools flag to settings.json config (#957)

This commit is contained in:
JingboWang1997-1 2025-06-11 14:32:23 -07:00 committed by GitHub
parent 122678cc09
commit 6ecdecbdcc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 24 additions and 1 deletions

View File

@ -64,6 +64,9 @@ When you create a `.gemini/settings.json` file for project-specific settings, or
- **Description:** Allows you to specify a list of core tool names that should be made available to the model. This can be used to restrict or customize the set of built-in tools. - **Description:** Allows you to specify a list of core tool names that should be made available to the model. This can be used to restrict or customize the set of built-in tools.
- **Example:** `"coreTools": ["ReadFileTool", "GlobTool", "SearchText"]`. - **Example:** `"coreTools": ["ReadFileTool", "GlobTool", "SearchText"]`.
- **Behavior:** If this setting is provided, only the listed tools will be available for the model to use. If omitted, all default core tools are available. See [Built-in Tools](../core/tools-api.md#built-in-tools) for a list of core tools. You can also specify the alternative internal tool names used by the model, e.g. `read_file`, and you can get a full listing for that by simply asking the model "what tools do you have?". - **Behavior:** If this setting is provided, only the listed tools will be available for the model to use. If omitted, all default core tools are available. See [Built-in Tools](../core/tools-api.md#built-in-tools) for a list of core tools. You can also specify the alternative internal tool names used by the model, e.g. `read_file`, and you can get a full listing for that by simply asking the model "what tools do you have?".
- **`excludeTools`** (array of strings, optional):
- **Description:** Allows you to specify a list of core tool names that should be excluded from the model.
- **Example:** `"excludeTools": ["run_shell_command", "glob"]`.
- **`autoAccept`** (boolean, optional): - **`autoAccept`** (boolean, optional):
- **Description:** Controls whether the CLI automatically accepts and executes tool calls that are considered safe (e.g., read-only operations) without explicit user confirmation. - **Description:** Controls whether the CLI automatically accepts and executes tool calls that are considered safe (e.g., read-only operations) without explicit user confirmation.

View File

@ -44,6 +44,7 @@ vi.mock('@gemini-cli/core', async () => {
getQuestion: () => params.question, getQuestion: () => params.question,
getFullContext: () => params.fullContext, getFullContext: () => params.fullContext,
getCoreTools: () => params.coreTools, getCoreTools: () => params.coreTools,
getExcludeTools: () => params.excludeTools,
getToolDiscoveryCommand: () => params.toolDiscoveryCommand, getToolDiscoveryCommand: () => params.toolDiscoveryCommand,
getToolCallCommand: () => params.toolCallCommand, getToolCallCommand: () => params.toolCallCommand,
getMcpServerCommand: () => params.mcpServerCommand, getMcpServerCommand: () => params.mcpServerCommand,

View File

@ -175,6 +175,7 @@ export async function loadCliConfig(
question: argv.prompt || '', question: argv.prompt || '',
fullContext: argv.all_files || false, fullContext: argv.all_files || false,
coreTools: settings.coreTools || undefined, coreTools: settings.coreTools || undefined,
excludeTools: settings.excludeTools || undefined,
toolDiscoveryCommand: settings.toolDiscoveryCommand, toolDiscoveryCommand: settings.toolDiscoveryCommand,
toolCallCommand: settings.toolCallCommand, toolCallCommand: settings.toolCallCommand,
mcpServerCommand: settings.mcpServerCommand, mcpServerCommand: settings.mcpServerCommand,

View File

@ -29,6 +29,7 @@ export interface Settings {
theme?: string; theme?: string;
sandbox?: boolean | string; sandbox?: boolean | string;
coreTools?: string[]; coreTools?: string[];
excludeTools?: string[];
toolDiscoveryCommand?: string; toolDiscoveryCommand?: string;
toolCallCommand?: string; toolCallCommand?: string;
mcpServerCommand?: string; mcpServerCommand?: string;

View File

@ -65,6 +65,7 @@ export interface ConfigParameters {
question?: string; question?: string;
fullContext?: boolean; fullContext?: boolean;
coreTools?: string[]; coreTools?: string[];
excludeTools?: string[];
toolDiscoveryCommand?: string; toolDiscoveryCommand?: string;
toolCallCommand?: string; toolCallCommand?: string;
mcpServerCommand?: string; mcpServerCommand?: string;
@ -95,6 +96,7 @@ export class Config {
private readonly question: string | undefined; private readonly question: string | undefined;
private readonly fullContext: boolean; private readonly fullContext: boolean;
private readonly coreTools: string[] | undefined; private readonly coreTools: string[] | undefined;
private readonly excludeTools: string[] | undefined;
private readonly toolDiscoveryCommand: string | undefined; private readonly toolDiscoveryCommand: string | undefined;
private readonly toolCallCommand: string | undefined; private readonly toolCallCommand: string | undefined;
private readonly mcpServerCommand: string | undefined; private readonly mcpServerCommand: string | undefined;
@ -126,6 +128,7 @@ export class Config {
this.question = params.question; this.question = params.question;
this.fullContext = params.fullContext ?? false; this.fullContext = params.fullContext ?? false;
this.coreTools = params.coreTools; this.coreTools = params.coreTools;
this.excludeTools = params.excludeTools;
this.toolDiscoveryCommand = params.toolDiscoveryCommand; this.toolDiscoveryCommand = params.toolDiscoveryCommand;
this.toolCallCommand = params.toolCallCommand; this.toolCallCommand = params.toolCallCommand;
this.mcpServerCommand = params.mcpServerCommand; this.mcpServerCommand = params.mcpServerCommand;
@ -210,6 +213,10 @@ export class Config {
return this.coreTools; return this.coreTools;
} }
getExcludeTools(): string[] | undefined {
return this.excludeTools;
}
getToolDiscoveryCommand(): string | undefined { getToolDiscoveryCommand(): string | undefined {
return this.toolDiscoveryCommand; return this.toolDiscoveryCommand;
} }
@ -360,12 +367,22 @@ export function createToolRegistry(config: Config): Promise<ToolRegistry> {
const tools = config.getCoreTools() const tools = config.getCoreTools()
? new Set(config.getCoreTools()) ? new Set(config.getCoreTools())
: undefined; : undefined;
const excludeTools = config.getExcludeTools()
? new Set(config.getExcludeTools())
: undefined;
// helper to create & register core tools that are enabled // helper to create & register core tools that are enabled
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
const registerCoreTool = (ToolClass: any, ...args: unknown[]) => { const registerCoreTool = (ToolClass: any, ...args: unknown[]) => {
// check both the tool name (.Name) and the class name (.name) // check both the tool name (.Name) and the class name (.name)
if (!tools || tools.has(ToolClass.Name) || tools.has(ToolClass.name)) { if (
// coreTools contain tool name
(!tools || tools.has(ToolClass.Name) || tools.has(ToolClass.name)) &&
// excludeTools don't contain tool name
(!excludeTools ||
(!excludeTools.has(ToolClass.Name) &&
!excludeTools.has(ToolClass.name)))
) {
registry.registerTool(new ToolClass(...args)); registry.registerTool(new ToolClass(...args));
} }
}; };