mcp-ts-transcript 1.0.0 → 1.0.1

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 +52 -22
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -2,22 +2,24 @@
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
7
  import * as ts from "typescript";
6
8
  const server = new Server({ name: "ts-transcript", version: "1.0.0" }, { capabilities: { tools: {} } });
7
9
  server.setRequestHandler(ListToolsRequestSchema, async () => ({
8
10
  tools: [
9
11
  {
10
12
  name: "compile_typescript",
11
- description: "Receives TypeScript code, compiles it and returns the JavaScript output.",
13
+ description: "Compiles a single TypeScript file using the nearest tsconfig.json options.",
12
14
  inputSchema: {
13
15
  type: "object",
14
16
  properties: {
15
- code: {
17
+ file_path: {
16
18
  type: "string",
17
- description: "TypeScript source code to compile",
19
+ description: "Absolute path to the .ts file to compile",
18
20
  },
19
21
  },
20
- required: ["code"],
22
+ required: ["file_path"],
21
23
  },
22
24
  },
23
25
  ],
@@ -26,28 +28,56 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
26
28
  if (request.params.name !== "compile_typescript") {
27
29
  throw new Error(`Unknown tool: ${request.params.name}`);
28
30
  }
29
- const { code } = request.params.arguments;
30
- const result = ts.transpileModule(code, {
31
- compilerOptions: {
32
- target: ts.ScriptTarget.ES2020,
33
- module: ts.ModuleKind.CommonJS,
34
- esModuleInterop: true,
35
- strict: false,
36
- },
37
- reportDiagnostics: true,
38
- });
39
- if (result.diagnostics && result.diagnostics.length > 0) {
40
- const errors = result.diagnostics
41
- .map((d) => ts.flattenDiagnosticMessageText(d.messageText, "\n"))
42
- .join("\n");
31
+ const { file_path } = request.params.arguments;
32
+ // Find nearest tsconfig.json
33
+ let dir = dirname(file_path);
34
+ let tsconfigPath = null;
35
+ while (true) {
36
+ const candidate = join(dir, "tsconfig.json");
37
+ if (existsSync(candidate)) {
38
+ tsconfigPath = candidate;
39
+ break;
40
+ }
41
+ const parent = dirname(dir);
42
+ if (parent === dir)
43
+ break;
44
+ dir = parent;
45
+ }
46
+ if (!tsconfigPath) {
43
47
  return {
44
- content: [{ type: "text", text: `Compilation errors:\n${errors}` }],
48
+ content: [{ type: "text", text: `tsconfig.json not found for: ${file_path}` }],
45
49
  isError: true,
46
50
  };
47
51
  }
48
- return {
49
- content: [{ type: "text", text: result.outputText }],
50
- };
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
+ }
80
+ return { content: [{ type: "text", text: "OK" }] };
51
81
  });
52
82
  const transport = new StdioServerTransport();
53
83
  await server.connect(transport);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mcp-ts-transcript",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "MCP server that compiles TypeScript code to JavaScript",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",