mcp-ts-transcript 1.0.2 → 1.0.3

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.
Files changed (2) hide show
  1. package/dist/index.js +29 -9
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -2,7 +2,8 @@
2
2
  import { Server } from "@modelcontextprotocol/sdk/server/index.js";
3
3
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
4
4
  import { CallToolRequestSchema, ListToolsRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
5
- import { readFileSync, writeFileSync } from "fs";
5
+ import { existsSync, writeFileSync } from "fs";
6
+ import { dirname, join } from "path";
6
7
  import * as ts from "typescript";
7
8
  const COMPILER_OPTIONS = {
8
9
  noImplicitAny: false,
@@ -35,15 +36,34 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
35
36
  throw new Error(`Unknown tool: ${request.params.name}`);
36
37
  }
37
38
  const { file_path } = request.params.arguments;
38
- const code = readFileSync(file_path, "utf-8");
39
- const result = ts.transpileModule(code, { compilerOptions: COMPILER_OPTIONS, reportDiagnostics: true });
40
- if (result.diagnostics?.length) {
41
- const errors = result.diagnostics
42
- .map((d) => ts.flattenDiagnosticMessageText(d.messageText, "\n"))
43
- .join("\n");
44
- return { content: [{ type: "text", text: `Compilation errors:\n${errors}` }], isError: true };
39
+ // Find nearest tsconfig.json to get all project files (needed for namespace resolution)
40
+ let dir = dirname(file_path);
41
+ let tsconfigPath = null;
42
+ while (true) {
43
+ const candidate = join(dir, "tsconfig.json");
44
+ if (existsSync(candidate)) {
45
+ tsconfigPath = candidate;
46
+ break;
47
+ }
48
+ const parent = dirname(dir);
49
+ if (parent === dir)
50
+ break;
51
+ dir = parent;
45
52
  }
46
- writeFileSync(file_path.replace(/\.ts$/, ".js"), result.outputText, "utf-8");
53
+ let fileNames = tsconfigPath
54
+ ? ts.parseJsonConfigFileContent(ts.readConfigFile(tsconfigPath, ts.sys.readFile).config, ts.sys, dirname(tsconfigPath)).fileNames
55
+ : [];
56
+ // Ensure the target file is in the program (it may not match the tsconfig include pattern)
57
+ const normalizedTarget = file_path.toLowerCase();
58
+ if (!fileNames.some(f => f.toLowerCase() === normalizedTarget))
59
+ fileNames = [...fileNames, file_path];
60
+ // createProgram loads all project files so namespaces resolve correctly,
61
+ // but we only emit the single target file
62
+ const program = ts.createProgram(fileNames, COMPILER_OPTIONS);
63
+ program.emit(program.getSourceFile(file_path), (fileName, data) => {
64
+ if (fileName.endsWith(".js"))
65
+ writeFileSync(fileName, data, "utf-8");
66
+ });
47
67
  return { content: [{ type: "text", text: "OK" }] };
48
68
  });
49
69
  const transport = new StdioServerTransport();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mcp-ts-transcript",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "MCP server that compiles TypeScript code to JavaScript",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",