feat(mcp): Create nest wrapper for MCP for proper DI (#1708)

This commit is contained in:
jubnl
2026-07-26 11:42:47 +02:00
committed by GitHub
parent 828c1db489
commit 8f3ee53841
45 changed files with 2072 additions and 152 deletions
+10 -1
View File
@@ -15,6 +15,8 @@ import { Client } from '@modelcontextprotocol/sdk/client/index';
import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory';
import { registerResources } from '../../src/mcp/resources';
import { registerTools } from '../../src/mcp/tools';
import { setMcpRegistry } from '../../src/mcp/registry-handoff';
import { createMcpTestRegistry } from './mcp-test-controllers';
export interface McpHarness {
client: Client;
@@ -40,7 +42,14 @@ export async function createMcpHarness(options: McpHarnessOptions): Promise<McpH
const server = new McpServer({ name: 'trek-test', version: '1.0.0' });
if (withResources) registerResources(server, userId);
if (withTools) registerTools(server, userId, scopes ?? null, isStaticToken);
if (withTools) {
// In production bootstrap.ts hands the Nest-discovered registry to
// registerTools after app.init(); the harness has no Nest app, so it
// builds the same registry by hand (see mcp-test-controllers.ts).
// registerTools' own ctx construction stays exercised.
setMcpRegistry(createMcpTestRegistry());
registerTools(server, userId, scopes ?? null, isStaticToken);
}
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
@@ -0,0 +1,23 @@
import { createTestRegistry, type McpRegistry } from '@trek/nest-mcp';
import { db } from '../../src/db/database';
import { trekMcpAccessPolicy } from '../../src/mcp/nest-mcp-policy';
import { DatabaseService } from '../../src/nest/database/database.service';
import { TagsMcp } from '../../src/nest/tags/tags.mcp';
import { TagsService } from '../../src/nest/tags/tags.service';
/**
* Hand-wired counterpart of the boot-time discovery in McpRegistryService,
* for the no-Nest MCP harness. One line per migrated domain — add the new
* @McpController instance here when a domain moves off the legacy registrar
* fan-out. Constructing against the `db` Proxy keeps per-file vi.mock's of
* src/db/database flowing through (same pattern as tags.bridge.ts).
*/
export function createMcpTestRegistry(): McpRegistry {
const dbService = new DatabaseService(db);
return createTestRegistry(
[
new TagsMcp(new TagsService(dbService)),
],
{ accessPolicy: trekMcpAccessPolicy },
);
}
@@ -178,6 +178,21 @@ describe('Tool: update_tag', () => {
expect(result.isError).toBe(true);
});
});
it('blocks demo user', async () => {
process.env.DEMO_MODE = 'true';
const { user } = createUser(testDb, { email: 'demo@nomad.app' });
const r = testDb.prepare('INSERT INTO tags (user_id, name, color) VALUES (?, ?, ?)').run(user.id, 'Demo Tag', '#aaaaaa');
const tagId = r.lastInsertRowid as number;
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'update_tag',
arguments: { tagId, name: 'Blocked' },
});
expect(result.isError).toBe(true);
expect(testDb.prepare('SELECT name FROM tags WHERE id = ?').get(tagId)).toEqual({ name: 'Demo Tag' });
});
});
});
// ---------------------------------------------------------------------------
@@ -199,6 +214,76 @@ describe('Tool: delete_tag', () => {
expect(testDb.prepare('SELECT id FROM tags WHERE id = ?').get(tagId)).toBeUndefined();
});
});
it('returns isError for non-existent tagId', async () => {
const { user } = createUser(testDb);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'delete_tag',
arguments: { tagId: 99999 },
});
expect(result.isError).toBe(true);
});
});
it('blocks demo user', async () => {
process.env.DEMO_MODE = 'true';
const { user } = createUser(testDb, { email: 'demo@nomad.app' });
const r = testDb.prepare('INSERT INTO tags (user_id, name, color) VALUES (?, ?, ?)').run(user.id, 'Demo Tag', '#aaaaaa');
const tagId = r.lastInsertRowid as number;
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'delete_tag',
arguments: { tagId },
});
expect(result.isError).toBe(true);
expect(testDb.prepare('SELECT id FROM tags WHERE id = ?').get(tagId)).toBeDefined();
});
});
});
// ---------------------------------------------------------------------------
// tags scope gating (places:read / places:write, registration-time)
// ---------------------------------------------------------------------------
describe('Tags tools: scope gating', () => {
const TAG_TOOLS = ['list_tags', 'create_tag', 'update_tag', 'delete_tag'];
async function listToolNames(userId: number, scopes: string[] | null): Promise<string[]> {
const h = await createMcpHarness({ userId, withResources: false, scopes });
try {
return (await h.client.listTools()).tools.map((t) => t.name);
} finally {
await h.cleanup();
}
}
it('registers all four tag tools with null scopes (full access)', async () => {
const { user } = createUser(testDb);
const names = await listToolNames(user.id, null);
for (const tool of TAG_TOOLS) expect(names).toContain(tool);
});
it('registers only list_tags with places:read', async () => {
const { user } = createUser(testDb);
const names = await listToolNames(user.id, ['places:read']);
expect(names).toContain('list_tags');
expect(names).not.toContain('create_tag');
expect(names).not.toContain('update_tag');
expect(names).not.toContain('delete_tag');
});
it('registers all four tag tools with places:write (write implies read)', async () => {
const { user } = createUser(testDb);
const names = await listToolNames(user.id, ['places:write']);
for (const tool of TAG_TOOLS) expect(names).toContain(tool);
});
it('registers no tag tools for an unrelated scope', async () => {
const { user } = createUser(testDb);
const names = await listToolNames(user.id, ['budget:read']);
for (const tool of TAG_TOOLS) expect(names).not.toContain(tool);
});
});
// ---------------------------------------------------------------------------