Skip to content

Commit b47f127

Browse files
suryaiyer95claude
andcommitted
fix: add input validation for non-interactive mcp add
- Validate server name is non-empty (reject whitespace-only) - Use `trim().split(/\s+/).filter(Boolean)` for command parsing instead of naive `split(" ")` that breaks on multiple spaces - Add `URL.canParse()` validation for remote server URLs, matching interactive mode's validation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8b7ab63 commit b47f127

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

  • packages/opencode/src/cli/cmd

packages/opencode/src/cli/cmd/mcp.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,11 @@ export const McpAddCommand = cmd({
453453
async fn() {
454454
// Non-interactive mode: all required args provided via flags
455455
if (args.name && args.type) {
456+
if (!args.name.trim()) {
457+
console.error("MCP server name cannot be empty")
458+
process.exit(1)
459+
}
460+
456461
const useGlobal = args.global || Instance.project.vcs !== "git"
457462
const configPath = await resolveConfigPath(
458463
useGlobal ? Global.Path.config : Instance.worktree,
@@ -462,19 +467,23 @@ export const McpAddCommand = cmd({
462467
let mcpConfig: Config.Mcp
463468

464469
if (args.type === "local") {
465-
if (!args.command) {
470+
if (!args.command?.trim()) {
466471
console.error("--command is required for local type")
467472
process.exit(1)
468473
}
469474
mcpConfig = {
470475
type: "local",
471-
command: args.command.split(" "),
476+
command: args.command.trim().split(/\s+/).filter(Boolean),
472477
}
473478
} else {
474479
if (!args.url) {
475480
console.error("--url is required for remote type")
476481
process.exit(1)
477482
}
483+
if (!URL.canParse(args.url)) {
484+
console.error(`Invalid URL: ${args.url}`)
485+
process.exit(1)
486+
}
478487

479488
const headers: Record<string, string> = {}
480489
if (args.header) {

0 commit comments

Comments
 (0)