The increasing incorporation of renewable energy sources and the emergence of new forms and patterns of electricity consumption are contributing to the upsurge in the complexity of power grids. A bottom-up-agent-based approach is able to handle the new environment, such that the system reliability can be maintained and costs reduced. However, this approach leads to possible conflicting interests between maintaining secure grid operation and the market requirements. This paper proposes a strategy to solve the conflicting interests in order to achieve overall optimal performance in the electricity supply system. The method is based on a cooperative game theory to optimally allocate resources from all (local) actors, i.e., network operators, active producers, and consumers. Via this approach, agent-based functions, for facilitating both network services and energy markets, can be integrated and coordinated. Simulations are performed to verify the proposed concept on a medium voltage 30-bus test network. Results show the effectiveness of the approach in optimally harmonizing functions of power routing and matching.