Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/cli/src/commands/mcp/McpCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {command} from "@tsed/cli-core";
import {mcpServerConnect} from "@tsed/platform-mcp/cli";
import {s} from "@tsed/schema";

import {waitForShutdown} from "./utils/waitForShutdown.js";

const McpSchema = s.object({
http: s.boolean().default(false).description("Run MCP using HTTP server").opt("--http")
});
Expand All @@ -14,5 +16,8 @@ export const McpCommand = command({
const mode = data.http ? "streamable-http" : "stdio";

await mcpServerConnect(mode);

// the injector is destroyed when this handler resolves: stay pending while the server runs
await waitForShutdown(mode);
}
}).token();
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const projectInfoResource = defineResource({
isInitialized: !!projectPackage.preferences?.packageManager,
preferences: {
convention: projectPackage.preferences.convention,
architecture: projectPackage.preferences.architecture,
packageManager: projectPackage.preferences.packageManager,
platform: projectPackage.preferences.platform,
runtime: projectPackage.preferences.runtime
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {PackageManager} from "@tsed/cli-core";
import {s} from "@tsed/schema";

import {ArchitectureConvention} from "../../../interfaces/ArchitectureConvention.js";
import {PlatformType} from "../../../interfaces/PlatformType.js";
import {ProjectConvention} from "../../../interfaces/ProjectConvention.js";

export const ProjectPreferenceSchema = s
.object({
convention: s.string().enum(ProjectConvention).description("Project convention (Ts.ED or Angular style)"),
architecture: s.string().enum(ArchitectureConvention).description("Directory architecture (by type or by feature)"),
packageManager: s.string().enum(PackageManager).description("Used project manager to install dependencies"),
runtime: s
.string()
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/commands/mcp/tools/setWorkspaceTool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe("setWorkspaceTool", () => {
cwd: "/project",
preferences: {
convention: "conv_default",
architecture: "feature",
packageManager: "yarn",
platform: "express",
runtime: "node"
Expand Down Expand Up @@ -49,6 +50,7 @@ describe("setWorkspaceTool", () => {
pkg: {name: "tsed-app"},
preferences: {
convention: "conv_default",
architecture: "feature",
packageManager: "yarn",
platform: "express",
runtime: "node"
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/commands/mcp/tools/setWorkspaceTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const setWorkspaceTool = defineTool({
pkg: projectPackage.toJSON(),
preferences: {
convention: projectPackage.preferences.convention,
architecture: projectPackage.preferences.architecture,
packageManager: projectPackage.preferences.packageManager,
platform: projectPackage.preferences.platform,
runtime: projectPackage.preferences.runtime
Expand Down
39 changes: 39 additions & 0 deletions packages/cli/src/commands/mcp/utils/waitForShutdown.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {EventEmitter} from "node:events";

import {describe, expect, it, vi} from "vitest";

import {waitForShutdown} from "./waitForShutdown.js";

async function isPending(promise: Promise<unknown>) {
const pending = Symbol("pending");

return (await Promise.race([promise, new Promise((resolve) => setTimeout(() => resolve(pending), 10))])) === pending;
}

describe("waitForShutdown", () => {
it("should stay pending while stdin is open (stdio)", async () => {
const stdin = new EventEmitter() as unknown as NodeJS.ReadableStream;

expect(await isPending(waitForShutdown("stdio", stdin))).toBe(true);
});

it.each(["end", "close"])("should resolve when stdin emits %s (stdio)", async (event) => {
const stdin = new EventEmitter();
const done = vi.fn();
const promise = waitForShutdown("stdio", stdin as unknown as NodeJS.ReadableStream).then(done);

stdin.emit(event);
await promise;

expect(done).toHaveBeenCalledTimes(1);
});

it("should never resolve with the HTTP transport", async () => {
const stdin = new EventEmitter();
const promise = waitForShutdown("streamable-http", stdin as unknown as NodeJS.ReadableStream);

stdin.emit("end");

expect(await isPending(promise)).toBe(true);
});
});
17 changes: 17 additions & 0 deletions packages/cli/src/commands/mcp/utils/waitForShutdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Keep the `mcp` command pending while the MCP server is running.
*
* The CLI destroys the injector as soon as a command handler resolves. An MCP server keeps answering requests after
* the connection is established, so the handler must stay pending or every later tool call runs on an empty injector.
*
* - `stdio`: resolves when the client closes stdin.
* - `streamable-http`: never resolves, the HTTP server lives until the process is stopped.
*/
export function waitForShutdown(mode: "stdio" | "streamable-http", stdin: NodeJS.ReadableStream = process.stdin): Promise<void> {
return new Promise<void>((resolve) => {
if (mode === "stdio") {
stdin.once("end", resolve);
stdin.once("close", resolve);
}
});
}
Loading