Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(mcp): under x-qm-actor, list only attested tools and refuse resources/read (GST-359 C1 review L3)

garrytan20ef29a55a43
diff --git a/src/commands/serve-http-mcp.ts b/src/commands/serve-http-mcp.ts
index b12f91df49..2a18803595 100644
--- a/src/commands/serve-http-mcp.ts
+++ b/src/commands/serve-http-mcp.ts
@@ -9,7 +9,7 @@ import type { Express, Request, Response } from 'express';
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
import { DEFAULT_MAX_REQUEST_BODY_SIZE } from '@modelcontextprotocol/sdk/server/requestBody.js';
-import { ListToolsRequestSchema, CallToolRequestSchema, type CallToolRequest } from '@modelcontextprotocol/sdk/types.js';
+import { ListToolsRequestSchema, CallToolRequestSchema, ReadResourceRequestSchema, McpError, ErrorCode, type CallToolRequest } from '@modelcontextprotocol/sdk/types.js';
import { requireBearerAuth } from '@modelcontextprotocol/sdk/server/auth/middleware/bearerAuth.js';
import { opAllowedForBoundClient } from '../core/operations.ts';
import { authTransport } from '../core/ops/contract.ts';
@@ -42,7 +42,12 @@ import {
} from '../mcp/surface.ts';
import { getBrainHotMemoryMeta } from '../core/facts/meta-hook.ts';
import { ON_BEHALF_OF_HEADER } from '../core/access/audience.ts';
-import { ACTOR_HEADER } from '../core/access/actor-model.ts';
+import { ACTOR_HEADER, EXTERNAL_ID_RE } from '../core/access/actor-model.ts';
+import { attestableOp } from '../core/access/actor.ts';
+
+const actorResourceRefusal = () => new McpError(ErrorCode.InvalidRequest,
+ `actor_not_attested: resources are never read as an attested person, so nothing was read. Read resources without ${ACTOR_HEADER}.`,
+ { code: 'actor_not_attested', reason: 'op_not_attested' });
import { serializeError } from '../core/errors.ts';
import { VERSION } from '../version.ts';
import { executeRawJsonb } from '../core/sql-query.ts';
@@ -222,13 +227,15 @@ function createMcpRequestServer(
installCapabilitiesResource(server, async () => {
return { transport: authTransport(authInfo), client_id: authInfo.clientId,
...await resolveAuthCapabilities(authInfo, engine, config), administration: mcpAdministrationGuidance(mcpResourceUrl.toString()) };
- }, createSkillResources(engine, async () => {
+ }, state.actorHeader != null ? undefined : createSkillResources(engine, async () => {
const sourceId = authInfo.sourceId ?? 'default';
const { noGrantFederatedScope } = await import('../core/source-resolver.ts');
return { remote: true, transport: 'http', sourceId, auth: authInfo, config,
localFederatedSourceIds: await noGrantFederatedScope(engine, authInfo.hasSourceGrant, sourceId),
allowedOps: surfaceAllowedOps, surface, surfaceCeiling };
}));
+ // GST-359 C1: resources are never read as an attested person, so a request naming one reads none.
+ if (state.actorHeader != null) server.setRequestHandler(ReadResourceRequestSchema, async () => { throw actorResourceRefusal(); });
server.setRequestHandler(ListToolsRequestSchema, async () => listMcpTools(ctx, state));
server.setRequestHandler(CallToolRequestSchema, async (request) => callMcpTool(ctx, state, request));
return server;
@@ -262,9 +269,11 @@ async function listMcpTools(ctx: ServeHttpContext, state: McpRequestState) {
// Agent contract v1 (A2): the one callability predicate (isCallable) plus
// the bound-client op fence.
const publishGates = publishGatesFromDisabled(mcpOperations, gateDisabled);
+ // GST-359 C1: under a valid x-qm-actor header from an attest credential, only the operations it may run as the person.
+ const attest = state.actorHeader != null && EXTERNAL_ID_RE.test(state.actorHeader) ? authInfo.attest : null;
const visibleOps = mcpOperations.filter(op =>
isCallable(op, { transport: 'http', surface, scopes: authInfo.scopes, publishGates, allowedOps: surfaceAllowedOps })
- && opAllowedForBoundClient(authInfo, op),
+ && opAllowedForBoundClient(authInfo, op) && (!attest || (attest.ops.includes(op.name) && attestableOp(op.name, op))),
);
// WP3 (amendment 14): ONE schema mapper — the inline map this handler
// carried is unified onto buildToolDefs so the byte-pin test covers the
diff --git a/test/e2e/serve-http-qm-actor.test.ts b/test/e2e/serve-http-qm-actor.test.ts
index 8178b46c02..eb4f28d5e7 100644
--- a/test/e2e/serve-http-qm-actor.test.ts
+++ b/test/e2e/serve-http-qm-actor.test.ts
@@ -146,6 +146,19 @@ describeE2E('/mcp attested actor (x-qm-actor) over serve --http', () => {
expect(fix.argv).toEqual(['gbrain', 'auth', 'rescope', '--client', plainClientId, '--attest-actor', 'qm']);
}, 60_000);
+ test('under the header tools/list lists only the attested operations and resources/read refuses', async () => {
+ const list = async (actor?: string) => ((await rpc('qm', { method: 'tools/list', params: {} }, actor)).rpc.result.tools as Array<{ name: string }>).map(t => t.name).sort();
+ expect(await list(ALICE_ID)).toEqual(['get_page', 'search']);
+ const plainList = await list();
+ expect(plainList).toContain('get_page');
+ expect(plainList.length).toBeGreaterThan(2);
+ const read = async (actor?: string) => (await rpc('qm', { method: 'resources/read', params: { uri: 'gbrain://capabilities' } }, actor)).rpc as { result?: { contents: unknown[] }; error?: { message: string; data?: { code?: string } } };
+ const refused = await read(ALICE_ID);
+ expect(refused.result).toBeUndefined();
+ expect(refused.error?.data?.code).toBe('actor_not_attested');
+ expect((await read()).result?.contents.length).toBe(1);
+ }, 60_000);
+
test('unlinking with the CLI refuses the next call', async () => {
expect(cli('access', 'member', 'unlink', '--qm', BOB_ID, '--dry-run')).toContain('Would unlink');
expect((await get('qm', PAGES.bob.slug, BOB_ID)).text).toContain(PAGES.bob.canary);