mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 23:27:31 +00:00
feat(mcp): audit MCP tool calls at the registry seam
This commit is contained in:
@@ -458,6 +458,55 @@ describe('MCP transport parity pins (Nest-hosted /mcp)', () => {
|
||||
expect(second.text).not.toContain('Deprecated authentication');
|
||||
});
|
||||
|
||||
it('MCP-P13 — an authorized tools/call writes exactly one mcp.tool_call audit row', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const { accessToken, clientId } = mintOauthToken(user.id, MCP_AUDIENCE);
|
||||
const sessionId = await createSession(accessToken);
|
||||
testDb.prepare("DELETE FROM audit_log WHERE action = 'mcp.tool_call'").run();
|
||||
|
||||
const call = await request(app)
|
||||
.post('/mcp')
|
||||
.set('Authorization', `Bearer ${accessToken}`)
|
||||
.set('mcp-session-id', sessionId)
|
||||
.set('Accept', 'application/json, text/event-stream')
|
||||
.send({ jsonrpc: '2.0', method: 'tools/call', id: 3, params: { name: 'list_trips', arguments: {} } });
|
||||
expect(call.status).toBe(200);
|
||||
|
||||
const rows = testDb.prepare(
|
||||
"SELECT user_id, action, resource, details FROM audit_log WHERE action = 'mcp.tool_call'",
|
||||
).all() as Array<{ user_id: number; action: string; resource: string; details: string }>;
|
||||
expect(rows).toHaveLength(1);
|
||||
expect(rows[0].user_id).toBe(user.id);
|
||||
expect(rows[0].resource).toBe('list_trips');
|
||||
expect(JSON.parse(rows[0].details)).toEqual({ clientId });
|
||||
});
|
||||
|
||||
it('MCP-P14 — tools/list and resource reads write no mcp.tool_call rows', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const token = generateToken(user.id);
|
||||
const sessionId = await createSession(token);
|
||||
testDb.prepare("DELETE FROM audit_log WHERE action = 'mcp.tool_call'").run();
|
||||
|
||||
const list = await request(app)
|
||||
.post('/mcp')
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.set('mcp-session-id', sessionId)
|
||||
.set('Accept', 'application/json, text/event-stream')
|
||||
.send({ jsonrpc: '2.0', method: 'tools/list', id: 2, params: {} });
|
||||
expect(list.status).toBe(200);
|
||||
|
||||
const read = await request(app)
|
||||
.post('/mcp')
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.set('mcp-session-id', sessionId)
|
||||
.set('Accept', 'application/json, text/event-stream')
|
||||
.send({ jsonrpc: '2.0', method: 'resources/list', id: 3, params: {} });
|
||||
expect(read.status).toBe(200);
|
||||
|
||||
const rows = testDb.prepare("SELECT id FROM audit_log WHERE action = 'mcp.tool_call'").all();
|
||||
expect(rows).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('MCP-P10 — /mcp bodies stay raw: an initialize payload over the global 100kb cap succeeds', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const big = {
|
||||
|
||||
@@ -14,6 +14,7 @@ function makeSession(overrides: Partial<McpSession> = {}): McpSession {
|
||||
clientId: null,
|
||||
isStaticToken: false,
|
||||
lastActivity: Date.now(),
|
||||
lastClientIp: null,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import type { AuthService } from '../../../src/nest/auth/auth.service';
|
||||
import type { TokenService } from '../../../src/nest/tokens/token.service';
|
||||
import type { OauthService } from '../../../src/nest/oauth/oauth.service';
|
||||
import type { AddonsService } from '../../../src/nest/addons/addons.service';
|
||||
import type { AuditService } from '../../../src/nest/audit/audit.service';
|
||||
import type { McpRegistryService } from '@trek/nest-mcp';
|
||||
import { IS_PUBLIC } from '../../../src/nest/auth/public.decorator';
|
||||
|
||||
@@ -39,8 +40,9 @@ function makeService(overrides: {
|
||||
const tokens = { verifyMcpToken: vi.fn(() => null), ...overrides.tokens } as unknown as TokenService;
|
||||
const oauth = { getUserByAccessToken: vi.fn(() => null), ...overrides.oauth } as unknown as OauthService;
|
||||
const addons = { isAddonEnabled: vi.fn(() => true), ...overrides.addons } as unknown as AddonsService;
|
||||
const audit = { writeAudit: vi.fn() } as unknown as AuditService;
|
||||
const registry = { attach: vi.fn() } as unknown as McpRegistryService;
|
||||
return { svc: new McpTransportService(auth, tokens, oauth, addons, registry), auth, tokens, oauth, addons };
|
||||
return { svc: new McpTransportService(auth, tokens, oauth, addons, audit, registry), auth, tokens, oauth, addons, audit };
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
Reference in New Issue
Block a user