mcp-ts-transcript 1.0.1 → 1.0.2
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.
- package/dist/index.js +11 -44
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2,15 +2,21 @@
|
|
|
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 {
|
|
6
|
-
import { dirname, join, relative, resolve } from "path";
|
|
5
|
+
import { readFileSync, writeFileSync } from "fs";
|
|
7
6
|
import * as ts from "typescript";
|
|
7
|
+
const COMPILER_OPTIONS = {
|
|
8
|
+
noImplicitAny: false,
|
|
9
|
+
noEmitOnError: false,
|
|
10
|
+
removeComments: true,
|
|
11
|
+
sourceMap: false,
|
|
12
|
+
target: ts.ScriptTarget.ES5,
|
|
13
|
+
};
|
|
8
14
|
const server = new Server({ name: "ts-transcript", version: "1.0.0" }, { capabilities: { tools: {} } });
|
|
9
15
|
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
10
16
|
tools: [
|
|
11
17
|
{
|
|
12
18
|
name: "compile_typescript",
|
|
13
|
-
description: "Compiles a
|
|
19
|
+
description: "Compiles a TypeScript file to JavaScript.",
|
|
14
20
|
inputSchema: {
|
|
15
21
|
type: "object",
|
|
16
22
|
properties: {
|
|
@@ -29,54 +35,15 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
|
29
35
|
throw new Error(`Unknown tool: ${request.params.name}`);
|
|
30
36
|
}
|
|
31
37
|
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) {
|
|
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
38
|
const code = readFileSync(file_path, "utf-8");
|
|
58
|
-
const result = ts.transpileModule(code, { compilerOptions:
|
|
39
|
+
const result = ts.transpileModule(code, { compilerOptions: COMPILER_OPTIONS, reportDiagnostics: true });
|
|
59
40
|
if (result.diagnostics?.length) {
|
|
60
41
|
const errors = result.diagnostics
|
|
61
42
|
.map((d) => ts.flattenDiagnosticMessageText(d.messageText, "\n"))
|
|
62
43
|
.join("\n");
|
|
63
44
|
return { content: [{ type: "text", text: `Compilation errors:\n${errors}` }], isError: true };
|
|
64
45
|
}
|
|
65
|
-
|
|
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
|
-
}
|
|
46
|
+
writeFileSync(file_path.replace(/\.ts$/, ".js"), result.outputText, "utf-8");
|
|
80
47
|
return { content: [{ type: "text", text: "OK" }] };
|
|
81
48
|
});
|
|
82
49
|
const transport = new StdioServerTransport();
|