alibaba/open-code-review · error

MCP server %q not found

Error message

MCP server %q not found

What it means

unsetMCPServer returns this error when cfg.MCPServers is nil, i.e. the config has no MCP servers section at all. It is functionally the same 'not found' condition as the map-lookup miss below, but handles the uninitialized-map case explicitly to avoid a nil-map lookup inconsistency in messaging.

Source

Thrown at cmd/opencodereview/config_cmd.go:269

		return err
	}

	fmt.Printf("Deleted custom provider %q.\n", name)
	if wasActive {
		fmt.Fprintf(os.Stderr, "[ocr] WARNING: active provider was deleted; 'provider' and 'model' have been cleared.\n")
		fmt.Fprintf(os.Stderr, "[ocr] Run 'ocr config provider' to select a new provider.\n")
	}
	return nil
}

func unsetMCPServer(configPath, name string) error {
	cfg, err := loadOrCreateConfig(configPath)
	if err != nil {
		return fmt.Errorf("load config: %w", err)
	}

	if cfg.MCPServers == nil {
		return fmt.Errorf("MCP server %q not found", name)
	}
	if _, exists := cfg.MCPServers[name]; !exists {
		return fmt.Errorf("MCP server %q not found", name)
	}

	delete(cfg.MCPServers, name)
	if len(cfg.MCPServers) == 0 {
		cfg.MCPServers = nil
	}

	if err := saveConfig(configPath, cfg); err != nil {
		return err
	}

	fmt.Printf("Deleted MCP server %q.\n", name)
	return nil
}

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. List configured MCP servers first (e.g. 'ocr config get' or read the config) to confirm the section exists.
  2. Add the MCP server with 'ocr config set mcp <name> ...' before trying to unset it.
  3. If the server was already removed, treat this as success and adjust any scripts to tolerate the error.

Example fix

// before: unsetting on empty config fails
$ ocr config unset mcp myserver

// after: add it first or guard in a script
$ ocr config set mcp myserver --command ./mcp.sh
$ ocr config unset mcp myserver
Defensive patterns

Strategy: validation

Validate before calling

cfg, _ := LoadAppConfig(configPath)
if cfg == nil || cfg.MCPServers == nil {
    return fmt.Errorf("no MCP servers configured at %s", configPath)
}

Type guard

func hasMCPServer(cfg *Config, name string) bool {
    return cfg != nil && cfg.MCPServers != nil
}

Try / catch

if err := unsetMCPServer(configPath, name); err != nil && strings.Contains(err.Error(), "not found") {
    // already absent: treat as success/idempotent
    return nil
}

Prevention

When it happens

Trigger: Running 'ocr config unset mcp <name>' on a config file that has never had any MCP server configured (no "mcpServers" key in the JSON), so cfg.MCPServers is nil.

Common situations: Fresh installation where no MCP server was ever added; typo'd subcommand run before 'ocr config set mcp'; config file written by hand without the mcpServers key.

Related errors


AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02). Data as JSON: /api/errors/6e4062aa6e50e012. Report an issue: GitHub.