@tsed/cli-mcp 7.0.0-beta.3 → 7.0.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
1
  import { injectable } from "@tsed/cli-core";
2
+ import { isArrowFn } from "@tsed/core";
2
3
  import { DIContext, injector, logger, runInContext } from "@tsed/di";
3
4
  import { JsonSchema } from "@tsed/schema";
4
5
  import { v4 } from "uuid";
@@ -38,7 +39,7 @@ export function defineTool(options) {
38
39
  .type("CLI_MCP_TOOLS")
39
40
  .factory(() => ({
40
41
  ...options,
41
- inputSchema: toZod(options.inputSchema),
42
+ inputSchema: toZod(isArrowFn(options.inputSchema) ? options.inputSchema() : options.inputSchema),
42
43
  outputSchema: toZod(options.outputSchema),
43
44
  async handler(args, extra) {
44
45
  const $ctx = new DIContext({
@@ -54,6 +55,21 @@ export function defineTool(options) {
54
55
  return options.handler(args, extra);
55
56
  });
56
57
  }
58
+ catch (er) {
59
+ $ctx.logger.error({
60
+ event: "MCP_TOOL_ERROR",
61
+ tool: options.name,
62
+ error_message: er.message,
63
+ stack: er.stack
64
+ });
65
+ return {
66
+ content: [],
67
+ structuredContent: {
68
+ code: "E_MCP_TOOL_ERROR",
69
+ message: er.message
70
+ }
71
+ };
72
+ }
57
73
  finally {
58
74
  // Ensure per-invocation context is destroyed to avoid leaks
59
75
  try {
package/lib/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  export * from "./fn/definePrompt.js";
2
2
  export * from "./fn/defineResource.js";
3
3
  export * from "./fn/defineTool.js";
4
- export * from "./services/CLIMCPServer.js";
4
+ export * from "./services/McpServerFactory.js";
@@ -4,7 +4,7 @@ import { mcpStdioServer } from "./McpStdioServer.js";
4
4
  import { mcpStreamableServer } from "./McpStreamableServer.js";
5
5
  export const MCP_SERVER = injectable(McpServer)
6
6
  .factory(() => {
7
- const mode = constant("mcp.mode");
7
+ const defaultMode = constant("mcp.mode");
8
8
  const name = constant("name");
9
9
  const server = new McpServer({
10
10
  name,
@@ -17,7 +17,7 @@ export const MCP_SERVER = injectable(McpServer)
17
17
  });
18
18
  const resources = constant("resources", []);
19
19
  resources.map((token) => {
20
- const { name, uri, template, handler, ...opts } = inject(token);
20
+ const { name, handler, uri, template, ...opts } = inject(token);
21
21
  server.registerResource(name, (uri || template), opts, handler);
22
22
  });
23
23
  const prompts = constant("prompts", []);
@@ -27,15 +27,14 @@ export const MCP_SERVER = injectable(McpServer)
27
27
  });
28
28
  return {
29
29
  server,
30
- async connect() {
31
- logger().info({ event: "MCP_SERVER_CONNECT" });
30
+ async connect(mode = defaultMode) {
32
31
  if (mode === "streamable-http") {
32
+ logger().info({ event: "MCP_SERVER_CONNECT", mode });
33
33
  await mcpStreamableServer(server);
34
34
  }
35
35
  else {
36
36
  await mcpStdioServer(server);
37
37
  }
38
- logger().info({ event: "MCP_SERVER_CONNECTED" });
39
38
  }
40
39
  };
41
40
  })
@@ -1,5 +1,7 @@
1
+ import { logger } from "@tsed/di";
1
2
  export async function mcpStdioServer(server) {
2
3
  const { StdioServerTransport } = await import("@modelcontextprotocol/sdk/server/stdio.js");
3
4
  const transport = new StdioServerTransport();
5
+ logger().stop();
4
6
  return server.connect(transport);
5
7
  }
@@ -1,3 +1,4 @@
1
+ import { logger } from "@tsed/di";
1
2
  export async function mcpStreamableServer(server) {
2
3
  const { StreamableHTTPServerTransport } = await import("@modelcontextprotocol/sdk/server/streamableHttp.js");
3
4
  // @ts-ignore
@@ -17,12 +18,23 @@ export async function mcpStreamableServer(server) {
17
18
  await transport.handleRequest(req, res, req.body);
18
19
  });
19
20
  const port = parseInt(process.env.PORT || "3000");
20
- app
21
- .listen(port, () => {
22
- console.log(`Demo MCP Server running on http://localhost:${port}/mcp`);
23
- })
24
- .on("error", (error) => {
25
- console.error("Server error:", error);
26
- process.exit(1);
21
+ return new Promise((resolve, reject) => {
22
+ app
23
+ .listen(port, () => {
24
+ logger().info({
25
+ event: "MCP_STREAMABLE_SERVER",
26
+ state: "OK",
27
+ message: `Running http://localhost:${port}/mcp`
28
+ });
29
+ })
30
+ .on("close", () => resolve(true))
31
+ .on("error", (error) => {
32
+ logger().error({
33
+ event: "MCP_STREAMABLE_SERVER",
34
+ state: "KO",
35
+ message: error.message
36
+ });
37
+ reject(error);
38
+ });
27
39
  });
28
40
  }