mcp-ts-transcript 1.0.1 → 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 +25 -38
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -2,15 +2,22 @@
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 { existsSync, readFileSync, writeFileSync } from "fs";
6
- import { dirname, join, relative, resolve } from "path";
5
+ import { existsSync, writeFileSync } from "fs";
6
+ import { dirname, join } from "path";
7
7
  import * as ts from "typescript";
8
+ const COMPILER_OPTIONS = {
9
+ noImplicitAny: false,
10
+ noEmitOnError: false,
11
+ removeComments: true,
12
+ sourceMap: false,
13
+ target: ts.ScriptTarget.ES5,
14
+ };
8
15
  const server = new Server({ name: "ts-transcript", version: "1.0.0" }, { capabilities: { tools: {} } });
9
16
  server.setRequestHandler(ListToolsRequestSchema, async () => ({
10
17
  tools: [
11
18
  {
12
19
  name: "compile_typescript",
13
- description: "Compiles a single TypeScript file using the nearest tsconfig.json options.",
20
+ description: "Compiles a TypeScript file to JavaScript.",
14
21
  inputSchema: {
15
22
  type: "object",
16
23
  properties: {
@@ -29,7 +36,7 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
29
36
  throw new Error(`Unknown tool: ${request.params.name}`);
30
37
  }
31
38
  const { file_path } = request.params.arguments;
32
- // Find nearest tsconfig.json
39
+ // Find nearest tsconfig.json to get all project files (needed for namespace resolution)
33
40
  let dir = dirname(file_path);
34
41
  let tsconfigPath = null;
35
42
  while (true) {
@@ -43,40 +50,20 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
43
50
  break;
44
51
  dir = parent;
45
52
  }
46
- if (!tsconfigPath) {
47
- return {
48
- content: [{ type: "text", text: `tsconfig.json not found for: ${file_path}` }],
49
- isError: true,
50
- };
51
- }
52
- // Parse tsconfig compiler options
53
- const configFile = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
54
- const parsed = ts.parseJsonConfigFileContent(configFile.config, ts.sys, dirname(tsconfigPath));
55
- const options = parsed.options;
56
- // Compile only the target file
57
- const code = readFileSync(file_path, "utf-8");
58
- const result = ts.transpileModule(code, { compilerOptions: options, reportDiagnostics: true });
59
- if (result.diagnostics?.length) {
60
- const errors = result.diagnostics
61
- .map((d) => ts.flattenDiagnosticMessageText(d.messageText, "\n"))
62
- .join("\n");
63
- return { content: [{ type: "text", text: `Compilation errors:\n${errors}` }], isError: true };
64
- }
65
- // Determine output path respecting outDir / rootDir
66
- const projectRoot = dirname(tsconfigPath);
67
- const outDir = options.outDir ? resolve(options.outDir) : null;
68
- const rootDir = options.rootDir ? resolve(options.rootDir) : projectRoot;
69
- let outPath;
70
- if (outDir) {
71
- outPath = join(outDir, relative(rootDir, file_path)).replace(/\.ts$/, ".js");
72
- }
73
- else {
74
- outPath = file_path.replace(/\.ts$/, ".js");
75
- }
76
- writeFileSync(outPath, result.outputText, "utf-8");
77
- if (result.sourceMapText) {
78
- writeFileSync(outPath + ".map", result.sourceMapText, "utf-8");
79
- }
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
+ });
80
67
  return { content: [{ type: "text", text: "OK" }] };
81
68
  });
82
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.1",
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",