Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

v0.2.5 — interactive scope approval with approve_scope tool, Telegram inline keyboard

Salman Qureshi7880bf4c1281
diff --git a/package.json b/package.json
index 06e1fde9..76dc4b47 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "@cosmicstack/mercury-agent",
- "version": "0.2.4",
+ "version": "0.2.5",
"description": "Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI or Telegram.",
"type": "module",
"main": "dist/index.js",
diff --git a/src/capabilities/filesystem/approve-scope.ts b/src/capabilities/filesystem/approve-scope.ts
new file mode 100644
index 00000000..42d63ab8
--- /dev/null
+++ b/src/capabilities/filesystem/approve-scope.ts
@@ -0,0 +1,22 @@
+import { tool } from 'ai';
+import { z } from 'zod';
+import { resolve } from 'node:path';
+import type { PermissionManager } from '../permissions.js';
+
+export function createApproveScopeTool(permissions: PermissionManager) {
+ return tool({
+ description: 'Request user approval to add a directory to the allowed filesystem scopes. Use this when a file operation fails due to scope permissions. The user will see an approval prompt (inline keyboard on Telegram, readline on CLI). If approved, the scope is added permanently.',
+ parameters: z.object({
+ path: z.string().describe('The directory path to add to allowed scopes'),
+ mode: z.enum(['read', 'write']).describe('The access mode needed'),
+ }),
+ execute: async ({ path, mode }) => {
+ const resolved = resolve(path);
+ const result = await permissions.requestScopeExternal(resolved, mode);
+ if (result.allowed) {
+ return `Access approved for ${mode} access to ${resolved}`;
+ }
+ return `Access denied for ${mode} access to ${resolved}`;
+ },
+ });
+}
\ No newline at end of file
diff --git a/src/capabilities/filesystem/create-file.ts b/src/capabilities/filesystem/create-file.ts
index b869b824..0233535d 100644
--- a/src/capabilities/filesystem/create-file.ts
+++ b/src/capabilities/filesystem/create-file.ts
@@ -15,7 +15,8 @@ export function createCreateFileTool(permissions: PermissionManager) {
const resolved = resolve(path);
const check = await permissions.checkFsAccess(resolved, 'write');
if (!check.allowed) {
- return `Error: ${check.reason}`;
+ const parentDir = resolve(resolved, '..');
+ return `Error: Permission denied for write access to ${resolved}. Use the approve_scope tool with path="${parentDir}" and mode="write" to request access from the user.`;
}
if (existsSync(resolved)) {
diff --git a/src/capabilities/filesystem/delete-file.ts b/src/capabilities/filesystem/delete-file.ts
index 86aa97a4..b23ea589 100644
--- a/src/capabilities/filesystem/delete-file.ts
+++ b/src/capabilities/filesystem/delete-file.ts
@@ -14,7 +14,8 @@ export function createDeleteFileTool(permissions: PermissionManager) {
const resolved = resolve(path);
const check = await permissions.checkFsAccess(resolved, 'write');
if (!check.allowed) {
- return `Error: ${check.reason}`;
+ const parentDir = resolve(resolved, '..');
+ return `Error: Permission denied for write access to ${resolved}. Use the approve_scope tool with path="${parentDir}" and mode="write" to request access from the user.`;
}
if (!existsSync(resolved)) {
diff --git a/src/capabilities/filesystem/edit-file.ts b/src/capabilities/filesystem/edit-file.ts
index 1116e187..3e55309b 100644
--- a/src/capabilities/filesystem/edit-file.ts
+++ b/src/capabilities/filesystem/edit-file.ts
@@ -17,7 +17,8 @@ export function createEditFileTool(permissions: PermissionManager) {
const fsCheck = await permissions.checkFsAccess(resolved, 'write');
if (!fsCheck.allowed) {
- return `Error: ${fsCheck.reason}`;
+ const parentDir = resolve(resolved, '..');
+ return `Error: Permission denied for write access to ${resolved}. Use the approve_scope tool with path="${parentDir}" and mode="write" to request access from the user.`;
}
try {
diff --git a/src/capabilities/filesystem/index.ts b/src/capabilities/filesystem/index.ts
index 8c7210d1..e88c23d0 100644
--- a/src/capabilities/filesystem/index.ts
+++ b/src/capabilities/filesystem/index.ts
@@ -2,4 +2,7 @@ export { createReadFileTool } from './read-file.js';
export { createWriteFileTool } from './write-file.js';
export { createCreateFileTool } from './create-file.js';
export { createListDirTool } from './list-dir.js';
-export { createDeleteFileTool } from './delete-file.js';
\ No newline at end of file
+export { createDeleteFileTool } from './delete-file.js';
+export { createEditFileTool } from './edit-file.js';
+export { createSendFileTool } from './send-file.js';
+export { createApproveScopeTool } from './approve-scope.js';
\ No newline at end of file
diff --git a/src/capabilities/filesystem/list-dir.ts b/src/capabilities/filesystem/list-dir.ts
index a023613d..8f83953d 100644
--- a/src/capabilities/filesystem/list-dir.ts
+++ b/src/capabilities/filesystem/list-dir.ts
@@ -14,7 +14,7 @@ export function createListDirTool(permissions: PermissionManager) {
const resolved = resolve(path);
const check = await permissions.checkFsAccess(resolved, 'read');
if (!check.allowed) {
- return `Error: ${check.reason}`;
+ return `Error: Permission denied for read access to ${resolved}. Use the approve_scope tool with path="${resolved}" and mode="read" to request access from the user.`;
}
if (!existsSync(resolved)) {
diff --git a/src/capabilities/filesystem/read-file.ts b/src/capabilities/filesystem/read-file.ts
index d95c466f..b5ddf976 100644
--- a/src/capabilities/filesystem/read-file.ts
+++ b/src/capabilities/filesystem/read-file.ts
@@ -14,7 +14,8 @@ export function createReadFileTool(permissions: PermissionManager) {
const resolved = resolve(path);
const check = await permissions.checkFsAccess(resolved, 'read');
if (!check.allowed) {
- return `Error: ${check.reason}`;
+ const parentDir = resolve(resolved, '..');
+ return `Error: Permission denied for read access to ${resolved}. Use the approve_scope tool with path="${parentDir}" and mode="read" to request access from the user.`;
}
if (!existsSync(resolved)) {
diff --git a/src/capabilities/filesystem/send-file.ts b/src/capabilities/filesystem/send-file.ts
index e62f306d..c6e89424 100644
--- a/src/capabilities/filesystem/send-file.ts
+++ b/src/capabilities/filesystem/send-file.ts
@@ -18,7 +18,8 @@ export function createSendFileTool(
const resolved = resolve(path);
const check = await permissions.checkFsAccess(resolved, 'read');
if (!check.allowed) {
- return `Error: ${check.reason}`;
+ const parentDir = resolve(resolved, '..');
+ return `Error: Permission denied for read access to ${resolved}. Use the approve_scope tool with path="${parentDir}" and mode="read" to request access from the user.`;
}
if (!existsSync(resolved)) {
diff --git a/src/capabilities/filesystem/write-file.ts b/src/capabilities/filesystem/write-file.ts
index c5e1d952..de892b35 100644
--- a/src/capabilities/filesystem/write-file.ts
+++ b/src/capabilities/filesystem/write-file.ts
@@ -16,7 +16,8 @@ export function createWriteFileTool(permissions: PermissionManager) {
const resolved = resolve(path);
const check = await permissions.checkFsAccess(resolved, 'write');
if (!check.allowed) {
- return `Error: ${check.reason}`;
+ const parentDir = resolve(resolved, '..');
+ return `Error: Permission denied for write access to ${resolved}. Use the approve_scope tool with path="${parentDir}" and mode="write" to request access from the user.`;
}
if (!existsSync(resolved)) {
diff --git a/src/capabilities/permissions.ts b/src/capabilities/permissions.ts
index ed5d4538..91211508 100644
--- a/src/capabilities/permissions.ts
+++ b/src/capabilities/permissions.ts
@@ -364,21 +364,24 @@ export class PermissionManager {
return undefined;
}
+ async requestScopeExternal(path: string, mode: 'read' | 'write'): Promise<{ allowed: boolean; reason?: string }> {
+ return this.requestScope(path, mode);
+ }
+
private async requestScope(path: string, mode: 'read' | 'write'): Promise<{ allowed: boolean; reason?: string }> {
if (!this.askHandler) {
- return { allowed: false, reason: `No permission to ${mode} ${path}. Add scope to ~/.mercury/permissions.yaml` };
+ return { allowed: false, reason: `Permission denied for ${mode} access to ${path}` };
}
- const response = await this.askHandler(
- `Mercury needs ${mode} access to ${path}. Allow? (y/n/always): `
- );
+ const prompt = `Mercury needs ${mode} access to:\n${path}\n\nAllow access?`;
+ const response = await this.askHandler(prompt);
- if (response.toLowerCase() === 'always') {
+ if (response === 'always') {
this.addScope(path, mode === 'read', mode === 'write');
return { allowed: true };
}
- if (response.toLowerCase() === 'y' || response.toLowerCase() === 'yes') {
+ if (response === 'yes' || response === 'y') {
return { allowed: true };
}
diff --git a/src/capabilities/registry.ts b/src/capabilities/registry.ts
index 46626a8c..8582890a 100644
--- a/src/capabilities/registry.ts
+++ b/src/capabilities/registry.ts
@@ -7,6 +7,7 @@ import { createListDirTool } from './filesystem/list-dir.js';
import { createDeleteFileTool } from './filesystem/delete-file.js';
import { createEditFileTool } from './filesystem/edit-file.js';
import { createSendFileTool } from './filesystem/send-file.js';
+import { createApproveScopeTool } from './filesystem/approve-scope.js';
import { createRunCommandTool } from './shell/run-command.js';
import { createApproveCommandTool } from './shell/approve-command.js';
import { createInstallSkillTool } from './skills/install-skill.js';
@@ -90,6 +91,8 @@ export class CapabilityRegistry {
this.tools.send_file = createSendFileTool(this.permissions, this.sendFileHandler);
}
+ this.tools.approve_scope = createApproveScopeTool(this.permissions);
+
logger.info('Filesystem tools registered');
}