Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.
fix(mcp): isolate resource consent for external event contexts
Seth Wilsond5855b714c54
diff --git a/src/capabilities/mcp/resources.test.ts b/src/capabilities/mcp/resources.test.tsindex a700eea6..ea25b56a 100644--- a/src/capabilities/mcp/resources.test.ts+++ b/src/capabilities/mcp/resources.test.ts@@ -20,7 +20,7 @@ const { McpManager } = await import('./manager.js');const { CapabilityRegistry } = await import('../registry.js');const { WebChannel } = await import('../../channels/web.js');const { filterBotTools } = await import('../../bots/registry-factory.js');-const { makeContext } = await import('../permission-context.js');+const { makeContext, deriveChildContext, isSubsetContext } = await import('../permission-context.js');const { validateMcpConfig } = await import('./config.js');const { registerSecretValue } = await import('../../utils/redact.js');const FIXTURE = fileURLToPath(new URL('./fixtures/resource-fixture-server.mjs', import.meta.url));@@ -261,6 +261,56 @@ describe('bounded MCP resources through the real SDK', () => {expect(h.calls().some((c) => c.method === 'resources/read')).toBe(false);});+ it.each(['list_mcp_resources', 'read_mcp_resource'] as const)('isolates non-remembering %s consent from the owner and other event turns', async (tool) => {+ const h = await harness({}, { answer: 'always' });+ const pm = h.registry.permissions;+ const args = tool === 'read_mcp_resource' ? { server: 'docs', uri: 'docs://alpha' } : { server: 'docs' };+ const run = (context: ReturnType<typeof makeContext>) => pm.withContext(context, () => h.run(tool, args));+ const requests = () => h.calls().filter((call) => call.method === (tool === 'read_mcp_resource' ? 'resources/read' : 'resources/list')).length;+ const owner = makeContext({ channelType: 'web', channelId: 'shared' });+ expect(owner.rememberApprovals).toBe(true);+ expect(await run(owner)).not.toContain('denied');+ h.ask.mockResolvedValue('no');+ expect(await run(owner)).not.toContain('denied');+ expect(h.ask).toHaveBeenCalledTimes(1);+ expect(requests()).toBe(2);++ const external = makeContext({ ...owner, rememberApprovals: false });+ const child = deriveChildContext(external);+ expect(child.rememberApprovals).toBe(false);+ expect(isSubsetContext(child, external)).toBe(true);+ expect(isSubsetContext(owner, external)).toBe(false);+ expect(await run(external)).toContain('denied');+ expect(await run(child)).toContain('denied');+ expect(h.ask).toHaveBeenCalledTimes(3);+ expect(requests()).toBe(2);++ const fresh = () => makeContext({ channelType: 'web', channelId: 'event-only', rememberApprovals: false });+ h.ask.mockResolvedValue('always');+ expect(await run(fresh())).not.toContain('denied');+ expect(await run(fresh())).not.toContain('denied');+ expect(h.ask).toHaveBeenCalledTimes(5);+ expect(requests()).toBe(4);+ h.ask.mockResolvedValue('no');+ expect(await run(fresh())).toContain('denied');+ expect(await run(makeContext({ channelType: 'web', channelId: 'event-only' }))).toContain('denied');+ expect(h.ask).toHaveBeenCalledTimes(7);+ expect(requests()).toBe(4);+ expect(await run(owner)).not.toContain('denied');+ expect(h.ask).toHaveBeenCalledTimes(7);+ expect(requests()).toBe(5);+ });++ it('preserves exact configured URI approval in a non-remembering context', async () => {+ const h = await harness({ resources: { allow: ['*'], autoApprove: ['docs://alpha'] } });+ await h.registry.permissions.withContext(makeContext({ channelType: 'web', channelId: 'event', rememberApprovals: false }), async () => {+ expect(await h.run('read_mcp_resource', { server: 'docs', uri: 'docs://alpha' })).toContain('Reference text');+ expect(h.ask).not.toHaveBeenCalled();+ expect(await h.run('read_mcp_resource', { server: 'docs', uri: 'docs://beta' })).toContain('denied');+ });+ expect(h.calls().filter((call) => call.method === 'resources/read').map((call) => call.params?.uri)).toEqual(['docs://alpha']);+ });+it('times out once without retrying a read', async () => {const h = await harness({ timeoutMs: 30 }, { answer: 'yes' });expect(await h.run('read_mcp_resource', { server: 'docs', uri: 'docs://slow' })).toMatch(/timed out/i);diff --git a/src/capabilities/permission-context.ts b/src/capabilities/permission-context.tsindex 901ea46d..0d38a814 100644--- a/src/capabilities/permission-context.ts+++ b/src/capabilities/permission-context.ts@@ -27,6 +27,8 @@ export interface ContextScope {export interface PermissionContext {readonly channelType: string;readonly channelId: string;+ /** External event turns neither consume nor create remembered conversation approvals. */+ readonly rememberApprovals: boolean;readonly senderRole: 'admin' | 'member' | undefined;/** Allow-all: approved-scope actions and non-blocked commands run without prompts. */readonly autoApprove: boolean;@@ -53,6 +55,7 @@ export function makeContext(fields: Partial<PermissionContext> = {}): PermissionchannelType: fields.channelType ?? 'cli',channelId: fields.channelId ?? 'cli',senderRole: fields.senderRole,+ rememberApprovals: fields.rememberApprovals ?? true,autoApprove: fields.autoApprove ?? false,autoApproveOrigin: fields.autoApprove ? (fields.autoApproveOrigin ?? 'session') : null,allowedTools: fields.allowedTools ? Object.freeze([...fields.allowedTools]) : undefined,@@ -119,6 +122,7 @@ export function deriveChildContext(parent: PermissionContext, req: ChildContextRchannelType: req.channelType ?? parent.channelType,channelId: req.channelId ?? parent.channelId,senderRole: parent.senderRole,+ rememberApprovals: parent.rememberApprovals,autoApprove: inheritAllow,autoApproveOrigin: inheritAllow ? 'session' : null,allowedTools,@@ -130,6 +134,7 @@ export function deriveChildContext(parent: PermissionContext, req: ChildContextR/** True when `child` grants nothing `parent` does not (used by tests and assertions). */export function isSubsetContext(child: PermissionContext, parent: PermissionContext): boolean {if (child.autoApprove && !parent.autoApprove) return false;+ if (child.rememberApprovals && !parent.rememberApprovals) return false;if (parent.allowedTools) {if (!child.allowedTools) return false;if (child.allowedTools.some((t) => !parent.allowedTools!.includes(t))) return false;diff --git a/src/capabilities/permissions.ts b/src/capabilities/permissions.tsindex 637e8bfc..882bd296 100644--- a/src/capabilities/permissions.ts+++ b/src/capabilities/permissions.ts@@ -1166,7 +1166,7 @@ export class PermissionManager {// used, so concurrent turns retain their own approval identity.const channel = JSON.stringify([ctx.channelType, ctx.channelId]);const key = JSON.stringify([serverId, operation, operation === 'read' ? uri : null]);- if (this.approvedMcpResourcesByContext.get(channel)?.has(key)) return { allowed: true };+ if (ctx.rememberApprovals && this.approvedMcpResourcesByContext.get(channel)?.has(key)) return { allowed: true };if (!this.askHandler || ctx.channelType === 'internal') {return { allowed: false, reason: 'MCP resources need owner approval; unattended reads require the exact URI in resources.autoApprove' };}@@ -1175,7 +1175,7 @@ export class PermissionManager {{ alwaysAsk: true },);if (signal?.aborted) return { allowed: false, reason: 'MCP resource request was cancelled' };- if (answer === 'always') {+ if (answer === 'always' && ctx.rememberApprovals) {const approved = this.approvedMcpResourcesByContext.get(channel) ?? new Set<string>();approved.add(key);this.approvedMcpResourcesByContext.set(channel, approved);