In this paper, we investigate an optimal consensus problem for multi-agent systems with directed interconnection topologies. Based on a nonlinear distributed coordination rule with switching directed communicating graphs, the considered multi-agent system achieves not only a consensus, but also an optimal one by agreeing within the global solution set of a sum of objective functions corresponding to multiple agents. The optimal solution set convergence and consensus analysis are given respectively with the help of convex analysis and nonsmooth analysis.