Applied prettier formatting to cmd/mcp.ts

This commit is contained in:
Netanel Draiman 2025-07-05 07:38:15 +03:00
parent 397444ef37
commit 61324642d3

View file

@ -98,7 +98,8 @@ export const McpAddCommand = cmd({
alias: "H",
type: "string",
array: true,
describe: "Set HTTP headers for SSE transport (e.g. -H \"X-Api-Key: abc123\")",
describe:
'Set HTTP headers for SSE transport (e.g. -H "X-Api-Key: abc123")',
default: [],
}),
handler: async (args) => {
@ -107,7 +108,9 @@ export const McpAddCommand = cmd({
for (const envVar of args.env) {
const [key, ...valueParts] = envVar.split("=")
if (!key || valueParts.length === 0) {
UI.error(`Invalid environment variable format: ${envVar}. Use KEY=VALUE format.`)
UI.error(
`Invalid environment variable format: ${envVar}. Use KEY=VALUE format.`,
)
return
}
environment[key] = valueParts.join("=")
@ -126,7 +129,9 @@ export const McpAddCommand = cmd({
// Determine server type based on transport and URL
const serverType = args.transport === "stdio" ? "local" : "remote"
const isUrl = args.commandOrUrl.startsWith("http://") || args.commandOrUrl.startsWith("https://")
const isUrl =
args.commandOrUrl.startsWith("http://") ||
args.commandOrUrl.startsWith("https://")
// Validate transport constraints
if (args.transport === "stdio") {
@ -145,17 +150,22 @@ export const McpAddCommand = cmd({
return
}
if (args.args.length > 0) {
UI.error(`${args.transport} transport doesn't accept additional arguments`)
UI.error(
`${args.transport} transport doesn't accept additional arguments`,
)
return
}
if (Object.keys(environment).length > 0) {
UI.error(`${args.transport} transport doesn't support environment variables`)
UI.error(
`${args.transport} transport doesn't support environment variables`,
)
return
}
}
// Create config
const mcpConfig: Config.Mcp = serverType === "remote"
const mcpConfig: Config.Mcp =
serverType === "remote"
? {
type: "remote",
url: args.commandOrUrl,
@ -168,12 +178,14 @@ export const McpAddCommand = cmd({
}
// Determine config path based on scope
const configPath = args.scope === "user"
const configPath =
args.scope === "user"
? path.join(Global.Path.config, "config.json")
: path.join(process.cwd(), "opencode.json")
// Load current config
const currentConfig = args.scope === "user"
const currentConfig =
args.scope === "user"
? await Config.global()
: await loadProjectConfig(configPath)
@ -187,7 +199,9 @@ export const McpAddCommand = cmd({
await Bun.write(configPath, JSON.stringify(updatedConfig, null, 2))
UI.println(`Added MCP server "${args.name}" (${args.transport}) to ${args.scope} config`)
UI.println(
`Added MCP server "${args.name}" (${args.transport}) to ${args.scope} config`,
)
},
})
@ -210,12 +224,14 @@ export const McpRemoveCommand = cmd({
}),
handler: async (args) => {
// Determine config path based on scope
const configPath = args.scope === "user"
const configPath =
args.scope === "user"
? path.join(Global.Path.config, "config.json")
: path.join(process.cwd(), "opencode.json")
// Load current config
const currentConfig = args.scope === "user"
const currentConfig =
args.scope === "user"
? await Config.global()
: await loadProjectConfig(configPath)
@ -244,8 +260,10 @@ export const McpListCommand = cmd({
const projectConfigPath = path.join(process.cwd(), "opencode.json")
const projectConfig = await loadProjectConfig(projectConfigPath)
const hasGlobalServers = globalConfig.mcp && Object.keys(globalConfig.mcp).length > 0
const hasProjectServers = projectConfig.mcp && Object.keys(projectConfig.mcp).length > 0
const hasGlobalServers =
globalConfig.mcp && Object.keys(globalConfig.mcp).length > 0
const hasProjectServers =
projectConfig.mcp && Object.keys(projectConfig.mcp).length > 0
if (!hasGlobalServers && !hasProjectServers) {
UI.println("No MCP servers configured")
@ -262,7 +280,10 @@ export const McpListCommand = cmd({
UI.println(` ${name} (${mcpConfig.type})${status}`)
if (mcpConfig.type === "local") {
UI.println(` Command: ${mcpConfig.command.join(" ")}`)
if (mcpConfig.environment && Object.keys(mcpConfig.environment).length > 0) {
if (
mcpConfig.environment &&
Object.keys(mcpConfig.environment).length > 0
) {
UI.println(` Environment:`)
for (const [key, value] of Object.entries(mcpConfig.environment)) {
UI.println(` ${key}=${value}`)
@ -285,7 +306,10 @@ export const McpListCommand = cmd({
UI.println(` ${name} (${mcpConfig.type})${status}`)
if (mcpConfig.type === "local") {
UI.println(` Command: ${mcpConfig.command.join(" ")}`)
if (mcpConfig.environment && Object.keys(mcpConfig.environment).length > 0) {
if (
mcpConfig.environment &&
Object.keys(mcpConfig.environment).length > 0
) {
UI.println(` Environment:`)
for (const [key, value] of Object.entries(mcpConfig.environment)) {
UI.println(` ${key}=${value}`)
@ -304,8 +328,7 @@ export const McpGetCommand = cmd({
command: "get <name>",
describe: "Get details about an MCP server",
builder: (yargs) =>
yargs
.positional("name", {
yargs.positional("name", {
type: "string",
describe: "Name of the MCP server",
demandOption: true,
@ -341,7 +364,10 @@ export const McpGetCommand = cmd({
if (foundConfig.type === "local") {
UI.println(`Command: ${foundConfig.command.join(" ")}`)
if (foundConfig.environment && Object.keys(foundConfig.environment).length > 0) {
if (
foundConfig.environment &&
Object.keys(foundConfig.environment).length > 0
) {
UI.println(`Environment variables:`)
for (const [key, value] of Object.entries(foundConfig.environment)) {
UI.println(` ${key}=${value}`)
@ -387,7 +413,7 @@ export const McpAddJsonCommand = cmd({
// Infer type and transform to match schema
let mcpConfig
if ('command' in jsonConfig) {
if ("command" in jsonConfig) {
// Transform stdio transport format
const { type, command, args, env, ...rest } = jsonConfig
@ -398,30 +424,34 @@ export const McpAddJsonCommand = cmd({
}
mcpConfig = Config.Mcp.parse({
type: 'local',
type: "local",
command: commandArray,
...(env && { environment: env }),
...rest
...rest,
})
} else if ('url' in jsonConfig) {
} else if ("url" in jsonConfig) {
// Transform sse transport format
const { type, ...rest } = jsonConfig
mcpConfig = Config.Mcp.parse({
type: 'remote',
...rest
type: "remote",
...rest,
})
} else {
UI.error("Invalid MCP configuration: Unable to determine transport type from JSON. Must include either 'command' for stdio or 'url' for sse.")
UI.error(
"Invalid MCP configuration: Unable to determine transport type from JSON. Must include either 'command' for stdio or 'url' for sse.",
)
return
}
// Determine config path based on scope
const configPath = args.scope === "user"
const configPath =
args.scope === "user"
? path.join(Global.Path.config, "config.json")
: path.join(process.cwd(), "opencode.json")
// Load current config
const currentConfig = args.scope === "user"
const currentConfig =
args.scope === "user"
? await Config.global()
: await loadProjectConfig(configPath)
@ -435,7 +465,9 @@ export const McpAddJsonCommand = cmd({
await Bun.write(configPath, JSON.stringify(updatedConfig, null, 2))
UI.println(`Added MCP server "${args.name}" (${mcpConfig.type}) to ${args.scope} config`)
UI.println(
`Added MCP server "${args.name}" (${mcpConfig.type}) to ${args.scope} config`,
)
} catch (error) {
if (error instanceof SyntaxError) {
UI.error(`Invalid JSON: ${error.message}`)
@ -472,12 +504,14 @@ export const McpEnableCommand = cmd({
}),
handler: async (args) => {
// Determine config path based on scope
const configPath = args.scope === "user"
const configPath =
args.scope === "user"
? path.join(Global.Path.config, "config.json")
: path.join(process.cwd(), "opencode.json")
// Load current config
const currentConfig = args.scope === "user"
const currentConfig =
args.scope === "user"
? await Config.global()
: await loadProjectConfig(configPath)
@ -522,12 +556,14 @@ export const McpDisableCommand = cmd({
}),
handler: async (args) => {
// Determine config path based on scope
const configPath = args.scope === "user"
const configPath =
args.scope === "user"
? path.join(Global.Path.config, "config.json")
: path.join(process.cwd(), "opencode.json")
// Load current config
const currentConfig = args.scope === "user"
const currentConfig =
args.scope === "user"
? await Config.global()
: await loadProjectConfig(configPath)