-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5deccb1
commit 34140d3
Showing
3 changed files
with
131 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* eslint-disable no-console */ | ||
import chalk from "chalk"; | ||
import { Command } from "commander"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
import os from "os"; | ||
|
||
import client from "../sdk/client/client"; | ||
Check warning on line 8 in js/src/cli/mcp.ts
|
||
import { getOpenAPIClient } from "../sdk/utils/config"; | ||
|
||
type ErrorWithMessage = { | ||
message: string; | ||
}; | ||
|
||
export default class MCPCommand { | ||
private program: Command; | ||
|
||
constructor(program: Command) { | ||
this.program = program; | ||
|
||
const command = this.program | ||
.command("mcp") | ||
.argument("<url>", "The app to use") | ||
.option("--client <client>", "Client to use (claude, windsurf)", "claude") | ||
.description("MCP command for app integration"); | ||
|
||
command.action(this.handleAction.bind(this)); | ||
} | ||
|
||
private async handleAction( | ||
url: string, | ||
options: { client: string } | ||
): Promise<void> { | ||
|
||
getOpenAPIClient(); | ||
const clientType = options.client; | ||
|
||
// Validate client type | ||
if (!["claude", "windsurf"].includes(clientType)) { | ||
console.log( | ||
chalk.red("❌ Error: Invalid client type specified") | ||
); | ||
console.log(chalk.yellow("Please use one of these supported clients:")); | ||
console.log(chalk.yellow("- claude")); | ||
console.log(chalk.yellow("- windsurf")); | ||
return; | ||
} | ||
|
||
try { | ||
console.log(chalk.cyan("📝 Configuration Details:")); | ||
console.log(` URL: ${chalk.green(url)}`); | ||
console.log(` Client: ${chalk.green(clientType)}\n`); | ||
|
||
const mcpUrl = url; | ||
const command = `npx -y supergateway --sse "${mcpUrl}"`; | ||
|
||
console.log(chalk.cyan("💾 Saving configurations...")); | ||
|
||
this.saveMcpConfig(url, clientType, mcpUrl, command); | ||
|
||
console.log(chalk.cyan(`\n🚀 All done! Please restart ${clientType} for changes to take effect\n`)); | ||
|
||
} catch (error) { | ||
console.log(chalk.red("\n❌ Error occurred while setting up MCP:")); | ||
console.log(chalk.red(` ${(error as ErrorWithMessage).message}`)); | ||
console.log(chalk.yellow("\nPlease try again or contact support if the issue persists.\n")); | ||
return; | ||
} | ||
} | ||
|
||
private saveMcpConfig( | ||
url: string, | ||
clientType: string, | ||
mcpUrl: string, | ||
command: string | ||
Check warning on line 75 in js/src/cli/mcp.ts
|
||
): void { | ||
const config = { | ||
command: "npx", | ||
args: ["-y", "supergateway", "--sse", mcpUrl] | ||
}; | ||
|
||
if (clientType === "claude") { | ||
let configDir; | ||
let configPath; | ||
|
||
if (os.platform() === "darwin") { | ||
configDir = path.join(os.homedir(), 'Library', 'Application Support', 'Claude'); | ||
configPath = path.join(configDir, 'claude_desktop_config.json'); | ||
} else if (os.platform() === "win32") { | ||
configDir = path.join(process.env.APPDATA || '', 'Claude'); | ||
configPath = path.join(configDir, 'claude_desktop_config.json'); | ||
} else { | ||
console.log(chalk.yellow("\n⚠️ Claude Desktop is not supported on this platform.")); | ||
return; | ||
} | ||
|
||
if (!fs.existsSync(configDir)) { | ||
fs.mkdirSync(configDir, { recursive: true }); | ||
} | ||
|
||
fs.writeFileSync(configPath, JSON.stringify({ | ||
mcpServers: { [url]: config } | ||
}, null, 2)); | ||
|
||
console.log(chalk.green(`✅ Configuration saved to: ${configPath}`)); | ||
|
||
} else if (clientType === "windsurf") { | ||
const configDir = path.join(os.homedir(), '.codeium', 'windsurf'); | ||
const configPath = path.join(configDir, 'mcp_config.json'); | ||
|
||
if (!fs.existsSync(configDir)) { | ||
fs.mkdirSync(configDir, { recursive: true }); | ||
} | ||
|
||
let windsurfConfig = { mcpServers: {} }; | ||
if (fs.existsSync(configPath)) { | ||
try { | ||
windsurfConfig = JSON.parse(fs.readFileSync(configPath, 'utf8')); | ||
if (!windsurfConfig.mcpServers) windsurfConfig.mcpServers = {}; | ||
} catch (error) { | ||
Check warning on line 120 in js/src/cli/mcp.ts
|
||
console.log(chalk.yellow("⚠️ Creating new config file")); | ||
} | ||
} | ||
|
||
windsurfConfig.mcpServers[url] = config; | ||
fs.writeFileSync(configPath, JSON.stringify(windsurfConfig, null, 2)); | ||
console.log(chalk.green(`✅ Configuration saved to: ${configPath}`)); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.