From 514b3b2c4a872af9901594408003e5cc2faba707 Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 16 Jul 2025 00:46:00 -0700 Subject: [PATCH] Add readonly and no-tools modes with tool permission filtering --- package-lock.json | 4 +- spec/modes.md | 11 ++++++ src/args.ts | 10 +++++ src/index.ts | 10 +++-- src/streamChatResponse.ts | 8 ++++ src/tools/exit.ts | 3 +- src/tools/fetch.ts | 1 + src/tools/index.ts | 68 ++++++++++++++++++++++++--------- src/tools/listFiles.ts | 3 +- src/tools/readFile.ts | 3 +- src/tools/runTerminalCommand.ts | 3 +- src/tools/searchCode.ts | 3 +- src/tools/types.ts | 3 +- src/tools/viewDiff.ts | 3 +- src/tools/writeFile.ts | 3 +- 15 files changed, 104 insertions(+), 32 deletions(-) create mode 100644 spec/modes.md diff --git a/package-lock.json b/package-lock.json index b1280ca12..4572a8284 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@continuedev/cli", - "version": "1.3.3", + "version": "1.3.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@continuedev/cli", - "version": "1.3.3", + "version": "1.3.5", "license": "Apache-2.0", "dependencies": { "@continuedev/config-yaml": "^1.0.96", diff --git a/spec/modes.md b/spec/modes.md new file mode 100644 index 000000000..ae9af1d78 --- /dev/null +++ b/spec/modes.md @@ -0,0 +1,11 @@ +# Modes + +Modes are flags that can be passed to the CLI for pre-configured tool permissions. The following modes are available: + +## `readonly` + +If `cn` is called with the `--readonly` flag, then only readonly tools are given to the model. Tools that can write to disk or make edits are left out. Each tool definition is marked with `readonly: boolean` to determine this. + +## `no-tools` + +If `cn` is called with the `--no-tools` flag, then no tools should be given to the model at all. diff --git a/src/args.ts b/src/args.ts index 0560d068b..ec20826f1 100644 --- a/src/args.ts +++ b/src/args.ts @@ -3,6 +3,8 @@ export interface CommandLineArgs { configPath?: string; prompt?: string; // Optional prompt argument resume?: boolean; // Resume from last session + readonly?: boolean; // Only allow readonly tools + noTools?: boolean; // Disable all tools } /** @@ -26,6 +28,14 @@ export function parseArgs(): CommandLineArgs { result.resume = true; } + if (args.includes("--readonly")) { + result.readonly = true; + } + + if (args.includes("--no-tools")) { + result.noTools = true; + } + // Get config path from --config flag const configIndex = args.indexOf("--config"); if (configIndex !== -1 && configIndex + 1 < args.length) { diff --git a/src/index.ts b/src/index.ts index bd16a295f..4fe125e30 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,13 +7,13 @@ import { logout } from "./commands/logout.js"; import { getVersion } from "./version.js"; // Add global error handlers to prevent uncaught errors from crashing the process -process.on('unhandledRejection', (reason, promise) => { - console.error('Unhandled Rejection at:', promise, 'reason:', reason); +process.on("unhandledRejection", (reason, promise) => { + console.error("Unhandled Rejection at:", promise, "reason:", reason); // Don't exit the process, just log the error }); -process.on('uncaughtException', (error) => { - console.error('Uncaught Exception:', error); +process.on("uncaughtException", (error) => { + console.error("Uncaught Exception:", error); // Don't exit the process, just log the error }); @@ -30,6 +30,8 @@ program .option("--headless", "Run in headless mode (non-interactive)") .option("--config ", "Path to configuration file") .option("--resume", "Resume from last session") + .option("--readonly", "Only allow readonly tools") + .option("--no-tools", "Disable all tools") .action(async (prompt, options) => { await chat(prompt, options); }); diff --git a/src/streamChatResponse.ts b/src/streamChatResponse.ts index 1c22404a0..144ccccf4 100644 --- a/src/streamChatResponse.ts +++ b/src/streamChatResponse.ts @@ -15,6 +15,13 @@ import { chatCompletionStreamWithBackoff } from "./util/exponentialBackoff.js"; dotenv.config(); export function getAllTools() { + const args = parseArgs(); + + // If no-tools mode is enabled, return empty array + if (args.noTools) { + return []; + } + const allTools: ChatCompletionTool[] = BUILTIN_TOOLS.map((tool) => ({ type: "function" as const, function: { @@ -35,6 +42,7 @@ export function getAllTools() { }, })); + // Add MCP tools if not in no-tools mode const mcpTools = MCPService.getInstance()?.getTools() ?? []; allTools.push( ...mcpTools.map((tool) => ({ diff --git a/src/tools/exit.ts b/src/tools/exit.ts index 999600d6a..f51f545d6 100644 --- a/src/tools/exit.ts +++ b/src/tools/exit.ts @@ -6,7 +6,8 @@ export const exitTool: Tool = { description: "Exit the current process with status code 1, indicating a failure or error", parameters: {}, + readonly: false, run: async (args: { dirpath: string }): Promise => { process.exit(1); }, -}; +}; \ No newline at end of file diff --git a/src/tools/fetch.ts b/src/tools/fetch.ts index 84fd9de01..d2a7c897c 100644 --- a/src/tools/fetch.ts +++ b/src/tools/fetch.ts @@ -45,6 +45,7 @@ export const fetchTool: Tool = { required: false, }, }, + readonly: true, run: async (args: { url: string; timeout?: number }): Promise => { const { url, timeout = 10000 } = args; diff --git a/src/tools/index.ts b/src/tools/index.ts index 6b1b3fb1b..7a2c16bca 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -12,7 +12,7 @@ import { writeFileTool } from "./writeFile.js"; export type { Tool, ToolParameters }; -export const BUILTIN_TOOLS: Tool[] = [ +const ALL_BUILTIN_TOOLS: Tool[] = [ readFileTool, writeFileTool, listFilesTool, @@ -24,8 +24,22 @@ export const BUILTIN_TOOLS: Tool[] = [ ...(parseArgs().isHeadless ? [exitTool] : []), ]; +export const BUILTIN_TOOLS: Tool[] = (() => { + const args = parseArgs(); + + if (args.noTools) { + return []; + } + + if (args.readonly) { + return ALL_BUILTIN_TOOLS.filter((tool) => tool.readonly === true); + } + + return ALL_BUILTIN_TOOLS; +})(); + export function getToolDisplayName(toolName: string): string { - const tool = BUILTIN_TOOLS.find(t => t.name === toolName); + const tool = BUILTIN_TOOLS.find((t) => t.name === toolName); return tool?.displayName || toolName; } @@ -100,21 +114,39 @@ export async function executeToolCall(toolCall: { name: string; arguments: Record; }): Promise { - const allTools: Tool[] = [ - ...BUILTIN_TOOLS, - ...(MCPService.getInstance() - ?.getTools() - .map((t) => ({ - name: t.name, - displayName: t.name.replace("mcp__", "").replace("ide__", ""), - description: t.description ?? "", - parameters: convertInputSchemaToParameters(t.inputSchema), - run: async (args: any) => { - const result = await MCPService.getInstance()?.runTool(t.name, args); - return JSON.stringify(result?.content) ?? ""; - }, - })) || []), - ]; + const args = parseArgs(); + + let mcpTools: Tool[] = []; + + // Don't load MCP tools if no-tools mode is enabled + if (!args.noTools) { + mcpTools = + MCPService.getInstance() + ?.getTools() + .map((t) => ({ + name: t.name, + displayName: t.name.replace("mcp__", "").replace("ide__", ""), + description: t.description ?? "", + parameters: convertInputSchemaToParameters(t.inputSchema), + readonly: undefined, // MCP tools don't have readonly property, so we include them in readonly mode + run: async (args: any) => { + const result = await MCPService.getInstance()?.runTool( + t.name, + args + ); + return JSON.stringify(result?.content) ?? ""; + }, + })) || []; + + // Filter MCP tools in readonly mode - include them since they don't have readonly property + if (args.readonly) { + // MCP tools are included in readonly mode since they don't have an explicit readonly property + // This is a design decision - you may want to exclude them entirely or handle them differently + } + } + + const allTools: Tool[] = [...BUILTIN_TOOLS, ...mcpTools]; + const tool = allTools.find((t) => t.name === toolCall.name); if (!tool) { @@ -138,4 +170,4 @@ export async function executeToolCall(toolCall: { error instanceof Error ? error.message : String(error) }`; } -} \ No newline at end of file +} diff --git a/src/tools/listFiles.ts b/src/tools/listFiles.ts index 80010e58d..86182d5ce 100644 --- a/src/tools/listFiles.ts +++ b/src/tools/listFiles.ts @@ -14,6 +14,7 @@ export const listFilesTool: Tool = { required: true, }, }, + readonly: true, run: async (args: { dirpath: string }): Promise => { try { if (!fs.existsSync(args.dirpath)) { @@ -40,4 +41,4 @@ export const listFilesTool: Tool = { }`; } }, -}; +}; \ No newline at end of file diff --git a/src/tools/readFile.ts b/src/tools/readFile.ts index 45ed8aa5b..95769aa71 100644 --- a/src/tools/readFile.ts +++ b/src/tools/readFile.ts @@ -12,6 +12,7 @@ export const readFileTool: Tool = { required: true, }, }, + readonly: true, run: async (args: { filepath: string }): Promise => { try { if (!fs.existsSync(args.filepath)) { @@ -23,4 +24,4 @@ export const readFileTool: Tool = { return `Error reading file: ${error instanceof Error ? error.message : String(error)}`; } }, -}; +}; \ No newline at end of file diff --git a/src/tools/runTerminalCommand.ts b/src/tools/runTerminalCommand.ts index e753e8a31..b4161c6cc 100644 --- a/src/tools/runTerminalCommand.ts +++ b/src/tools/runTerminalCommand.ts @@ -12,6 +12,7 @@ export const runTerminalCommandTool: Tool = { required: true, }, }, + readonly: false, run: async ({ command }: { command: string }): Promise => { return new Promise((resolve, reject) => { exec(command, (error, stdout, stderr) => { @@ -27,4 +28,4 @@ export const runTerminalCommandTool: Tool = { }); }); }, -}; +}; \ No newline at end of file diff --git a/src/tools/searchCode.ts b/src/tools/searchCode.ts index fe6073203..ca9cdff21 100644 --- a/src/tools/searchCode.ts +++ b/src/tools/searchCode.ts @@ -26,6 +26,7 @@ export const searchCodeTool: Tool = { required: false, }, }, + readonly: true, run: async (args: { pattern: string; path?: string; @@ -67,4 +68,4 @@ export const searchCodeTool: Tool = { return `Error searching code: ${error instanceof Error ? error.message : String(error)}`; } }, -}; +}; \ No newline at end of file diff --git a/src/tools/types.ts b/src/tools/types.ts index 86074f13b..65b2a0981 100644 --- a/src/tools/types.ts +++ b/src/tools/types.ts @@ -13,4 +13,5 @@ export interface Tool { description: string; parameters: ToolParameters; run: (args: any) => Promise; -} + readonly?: boolean; // Indicates if the tool is readonly +} \ No newline at end of file diff --git a/src/tools/viewDiff.ts b/src/tools/viewDiff.ts index ebc9abab2..a312ebbc1 100644 --- a/src/tools/viewDiff.ts +++ b/src/tools/viewDiff.ts @@ -17,6 +17,7 @@ export const viewDiffTool: Tool = { required: false, }, }, + readonly: true, run: async (args: { path?: string }): Promise => { try { const repoPath = args.path || process.cwd(); @@ -49,4 +50,4 @@ export const viewDiffTool: Tool = { return `Error running git diff: ${error instanceof Error ? error.message : String(error)}`; } }, -}; +}; \ No newline at end of file diff --git a/src/tools/writeFile.ts b/src/tools/writeFile.ts index 77c75f9bf..f432d5e79 100644 --- a/src/tools/writeFile.ts +++ b/src/tools/writeFile.ts @@ -31,6 +31,7 @@ export const writeFileTool: Tool = { required: true, }, }, + readonly: false, run: async (args: { filepath: string; content: string }): Promise => { try { const dirPath = path.dirname(args.filepath); @@ -58,4 +59,4 @@ export const writeFileTool: Tool = { return `Error writing to file: ${error instanceof Error ? error.message : String(error)}`; } }, -}; +}; \ No newline at end of file