src / mcpBridge.ts
import fs from "fs";
import path from "path";
import { ensureSetup, getRepoDir, getVenvDir } from "./setup";
let client: any = null;
let transport: any = null;
let currentApiKey: string | null = null;
export async function getMcpClient(apiKey: string, onStatus: (msg: string) => void): Promise<any> {
if (client && transport && currentApiKey === apiKey) {
return client;
}
if (transport) {
try { await transport.close(); } catch (e) {}
}
client = null;
// 確保環境已建置 (如果 npm install 已跑過 postinstall,這裡會瞬間通過)
await ensureSetup(onStatus);
const IS_WIN = process.platform === "win32";
const venvBin = IS_WIN ? path.join(getVenvDir(), "Scripts") : path.join(getVenvDir(), "bin");
// 動態尋找正確的執行檔名稱
let serverExe = "";
const possibleExes = ["tradingview-mcp", "tradingview-mcp-server"];
for (const name of possibleExes) {
const exePath = path.join(venvBin, IS_WIN ? `${name}.exe` : name);
if (fs.existsSync(exePath)) {
serverExe = exePath;
break;
}
}
if (!serverExe) {
throw new Error("Could not find tradingview-mcp executable in virtual environment. Please check pip install logs.");
}
const { Client } = await import("@modelcontextprotocol/sdk/client/index.js");
const { StdioClientTransport } = await import("@modelcontextprotocol/sdk/client/stdio.js");
transport = new StdioClientTransport({
command: serverExe,
env: {
...process.env,
MARKETAUX_API_TOKEN: apiKey // 傳遞 API Key
}
});
client = new Client({
name: "tradingview-mcp-client",
version: "1.0.0"
});
try {
await client.connect(transport);
currentApiKey = apiKey;
} catch (err: any) {
throw new Error(`Failed to connect to MCP server: ${err.message}`);
}
return client;
}src / mcpBridge.ts
import fs from "fs";
import path from "path";
import { ensureSetup, getRepoDir, getVenvDir } from "./setup";
let client: any = null;
let transport: any = null;
let currentApiKey: string | null = null;
export async function getMcpClient(apiKey: string, onStatus: (msg: string) => void): Promise<any> {
if (client && transport && currentApiKey === apiKey) {
return client;
}
if (transport) {
try { await transport.close(); } catch (e) {}
}
client = null;
// 確保環境已建置 (如果 npm install 已跑過 postinstall,這裡會瞬間通過)
await ensureSetup(onStatus);
const IS_WIN = process.platform === "win32";
const venvBin = IS_WIN ? path.join(getVenvDir(), "Scripts") : path.join(getVenvDir(), "bin");
// 動態尋找正確的執行檔名稱
let serverExe = "";
const possibleExes = ["tradingview-mcp", "tradingview-mcp-server"];
for (const name of possibleExes) {
const exePath = path.join(venvBin, IS_WIN ? `${name}.exe` : name);
if (fs.existsSync(exePath)) {
serverExe = exePath;
break;
}
}
if (!serverExe) {
throw new Error("Could not find tradingview-mcp executable in virtual environment. Please check pip install logs.");
}
const { Client } = await import("@modelcontextprotocol/sdk/client/index.js");
const { StdioClientTransport } = await import("@modelcontextprotocol/sdk/client/stdio.js");
transport = new StdioClientTransport({
command: serverExe,
env: {
...process.env,
MARKETAUX_API_TOKEN: apiKey // 傳遞 API Key
}
});
client = new Client({
name: "tradingview-mcp-client",
version: "1.0.0"
});
try {
await client.connect(transport);
currentApiKey = apiKey;
} catch (err: any) {
throw new Error(`Failed to connect to MCP server: ${err.message}`);
}
return client;
}