mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-22 06:56:35 +00:00
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import type { McpContext, McpRegistry } from '../src';
|
|
import { Client } from '@modelcontextprotocol/sdk/client/index';
|
|
import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory';
|
|
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp';
|
|
|
|
/** Test context shape — stands in for whatever a host app augments McpContext with. */
|
|
export interface TestCtx {
|
|
userId?: number;
|
|
canRead?: boolean;
|
|
canWrite?: boolean;
|
|
allow?: boolean;
|
|
}
|
|
|
|
export const asCtx = (ctx: TestCtx): McpContext => ctx as McpContext;
|
|
|
|
export interface AttachHarness {
|
|
client: Client;
|
|
server: McpServer;
|
|
cleanup: () => Promise<void>;
|
|
}
|
|
|
|
/** Bare McpServer + Client over an in-memory transport with the registry attached. */
|
|
export async function createAttachHarness(registry: McpRegistry, ctx: TestCtx): Promise<AttachHarness> {
|
|
const server = new McpServer({ name: 'nest-mcp-test', version: '1.0.0' });
|
|
registry.attach(server, asCtx(ctx));
|
|
|
|
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
|
const client = new Client({ name: 'test-client', version: '1.0.0' });
|
|
await server.connect(serverTransport);
|
|
await client.connect(clientTransport);
|
|
|
|
const cleanup = async () => {
|
|
try {
|
|
await client.close();
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
try {
|
|
await server.close();
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
};
|
|
return { client, server, cleanup };
|
|
}
|