Skip to content

Commit b2244b1

Browse files
committed
fix: use InMemoryTransport for reliable MCP tests
Replace stdio-based MCP test client with InMemoryTransport to avoid race condition in MCP SDK (modelcontextprotocol/typescript-sdk#1049). - Add openMcpTestClientInMemory() using InMemoryTransport.createLinkedPair() - Rewrite tests to use in-memory transport (no process spawning) - Re-enable MCP tests in CI workflow - Add tests for expected tools and resources
1 parent 2b26909 commit b2244b1

3 files changed

Lines changed: 95 additions & 42 deletions

File tree

‎.github/workflows/run-tests.yml‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,8 @@ jobs:
6868
env:
6969
CI: true
7070

71-
# TODO: Re-enable when MCP SDK race condition is fixed
72-
# See: https://github.com/modelcontextprotocol/typescript-sdk/issues/1049
73-
# - name: Run MCP smoke tests
74-
# run: |
75-
# bun test apps/cli/tests/mcp.test.ts
76-
# bun run apps/cli/src/index.ts mcp test
77-
# env:
78-
# CI: true
71+
- name: Run MCP tests
72+
run: bun test apps/cli/tests/mcp.test.ts
73+
env:
74+
CI: true
75+
JACK_TELEMETRY_DISABLED: '1'

‎apps/cli/src/mcp/test-utils.ts‎

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
22
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
3+
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
4+
import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
5+
import { createMcpServer } from "./server.ts";
6+
import type { McpServerOptions } from "./types.ts";
37

48
export interface McpClientOptions {
59
command: string;
@@ -12,12 +16,50 @@ export interface McpClientOptions {
1216

1317
export interface McpTestClient {
1418
client: Client;
15-
transport: StdioClientTransport;
19+
transport: Transport;
1620
getStderr(): string;
1721
close(): Promise<void>;
1822
}
1923

20-
export async function openMcpTestClient(options: McpClientOptions): Promise<McpTestClient> {
24+
/**
25+
* Open an MCP test client using in-memory transport.
26+
* This is the recommended approach for testing - no process spawning,
27+
* no race conditions, deterministic behavior.
28+
*/
29+
export async function openMcpTestClientInMemory(
30+
serverOptions: McpServerOptions = {},
31+
): Promise<McpTestClient> {
32+
// Create linked transport pair
33+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
34+
35+
// Create and connect server
36+
const { server } = await createMcpServer(serverOptions);
37+
await server.connect(serverTransport);
38+
39+
// Create and connect client
40+
const client = new Client({
41+
name: "jack-mcp-test",
42+
version: "0.1.0",
43+
});
44+
await client.connect(clientTransport);
45+
46+
return {
47+
client,
48+
transport: clientTransport,
49+
getStderr: () => "(in-memory transport - no stderr)",
50+
close: async () => {
51+
await clientTransport.close();
52+
await serverTransport.close();
53+
},
54+
};
55+
}
56+
57+
/**
58+
* Open an MCP test client using stdio transport (spawns a child process).
59+
* This tests the full stdio path but is prone to race conditions.
60+
* Use openMcpTestClientInMemory() for reliable testing.
61+
*/
62+
export async function openMcpTestClientStdio(options: McpClientOptions): Promise<McpTestClient> {
2163
const transport = new StdioClientTransport({
2264
command: options.command,
2365
args: options.args ?? [],
@@ -51,6 +93,9 @@ export async function openMcpTestClient(options: McpClientOptions): Promise<McpT
5193
};
5294
}
5395

96+
// Legacy alias for backwards compatibility
97+
export const openMcpTestClient = openMcpTestClientStdio;
98+
5499
export function parseMcpToolResult(toolResult: {
55100
content?: Array<{ type: string; text?: string }>;
56101
[key: string]: unknown;

‎apps/cli/tests/mcp.test.ts‎

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,61 @@
11
import { mkdtemp, rm } from "node:fs/promises";
22
import { tmpdir } from "node:os";
33
import { join } from "node:path";
4-
import { fileURLToPath } from "node:url";
5-
import { test } from "bun:test";
4+
import { test, expect } from "bun:test";
65
import {
6+
openMcpTestClientInMemory,
77
callMcpListProjects,
8-
openMcpTestClient,
98
verifyMcpToolsAndResources,
109
} from "../src/mcp/test-utils.ts";
1110

12-
const cliRoot = fileURLToPath(new URL("../", import.meta.url));
13-
14-
// TODO: Fix flaky test - MCP SDK has race condition in StdioClientTransport
15-
// See: https://github.com/modelcontextprotocol/typescript-sdk/issues/1049
16-
test.skip("jack mcp serve exposes tools without deploying", async () => {
11+
test("MCP server exposes tools and resources", async () => {
1712
const projectDir = await mkdtemp(join(tmpdir(), "jack-mcp-test-"));
18-
const configDir = await mkdtemp(join(tmpdir(), "jack-config-"));
19-
const clientEnv = {
20-
...process.env,
21-
CI: "1",
22-
JACK_TELEMETRY_DISABLED: "1",
23-
JACK_CONFIG_DIR: configDir,
24-
};
25-
26-
let client: Awaited<ReturnType<typeof openMcpTestClient>> | null = null;
13+
14+
let client: Awaited<ReturnType<typeof openMcpTestClientInMemory>> | null = null;
2715
try {
28-
client = await openMcpTestClient({
29-
command: "bun",
30-
args: ["run", "src/index.ts", "mcp", "serve", "--project", projectDir],
31-
cwd: cliRoot,
32-
env: clientEnv,
33-
});
16+
// Use in-memory transport - no process spawning, no race conditions
17+
client = await openMcpTestClientInMemory({ projectPath: projectDir });
3418

19+
// Verify tools and resources are exposed
3520
await verifyMcpToolsAndResources(client.client);
21+
22+
// Test list_projects tool
3623
const projects = await callMcpListProjects(client.client, "local");
37-
if (!Array.isArray(projects)) {
38-
throw new Error("list_projects returned unexpected data");
39-
}
40-
} catch (err) {
41-
// Log stderr to help debug CI failures
42-
const stderr = client?.getStderr() ?? "(no client)";
43-
console.error("MCP test failed. Server stderr:", stderr);
44-
throw err;
24+
expect(Array.isArray(projects)).toBe(true);
4525
} finally {
4626
await client?.close();
4727
await rm(projectDir, { recursive: true, force: true });
48-
await rm(configDir, { recursive: true, force: true });
28+
}
29+
});
30+
31+
test("MCP server has expected tools", async () => {
32+
const client = await openMcpTestClientInMemory();
33+
34+
try {
35+
const tools = await client.client.listTools();
36+
const toolNames = tools.tools?.map((t) => t.name) ?? [];
37+
38+
// Core tools should always be present
39+
expect(toolNames).toContain("create_project");
40+
expect(toolNames).toContain("deploy_project");
41+
expect(toolNames).toContain("get_project_status");
42+
expect(toolNames).toContain("list_projects");
43+
expect(toolNames).toContain("execute_sql");
44+
} finally {
45+
await client.close();
46+
}
47+
});
48+
49+
test("MCP server has expected resources", async () => {
50+
const client = await openMcpTestClientInMemory();
51+
52+
try {
53+
const resources = await client.client.listResources();
54+
const resourceUris = resources.resources?.map((r) => r.uri) ?? [];
55+
56+
expect(resourceUris).toContain("agents://context");
57+
expect(resourceUris).toContain("jack://capabilities");
58+
} finally {
59+
await client.close();
4960
}
5061
});

0 commit comments

Comments
 (0)