* Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
*/
#include "server/server_manager.h"
#include <iomanip>
#include <iostream>
#include <nlohmann/json.hpp>
#include <random>
#include <regex>
#include <sstream>
#include <stdexcept>
#include <unordered_map>
#include "event/event_system.h"
#include "mcp_auth.h"
#include "mcp_log.h"
#include "shared/thread_utils.h"
#include "server/transport/streamable_http_server_transport.h"
#include "transport/stdio_transport.h"
#define URL_REGEX_GROUP_HOST 2
#define URL_REGEX_GROUP_PORT 3
#define URL_REGEX_GROUP_URI 4
#define PORT_MIN 1
namespace Mcp {
constexpr size_t MAX_BATCH_SIZE = 32;
constexpr int MAX_PORT_NUMBER = 65535;
constexpr int QUEUE_CAPACITY = 4096;
ServerManager::ServerManager(const ServerConfig& config, const StreamableHttpServerConfig& transportConfig)
: config_(config), streamableConfig_(transportConfig), isStdio_{false},
authenticator_(transportConfig.authenticator), authorizer_(transportConfig.authorizer) {}
ServerManager::ServerManager(const ServerConfig& config) : config_(config) {}
void ServerManager::ThreadMain(int id)
{
SetCurrentThreadName("MCP-Worker-" + std::to_string(id));
MCP_LOG(MCP_LOG_LEVEL_INFO, "Protocol thread " + std::to_string(id) + " started");
eventSystems_[id]->Start(false);
MCP_LOG(MCP_LOG_LEVEL_INFO, "Protocol thread " + std::to_string(id) + " event loop ended");
}
ServerManager::~ServerManager()
{
}
void ServerManager::HttpServerManagerStart()
{
Http::HttpServerManagerConfig httpConfig;
std::string endpoint = streamableConfig_.endpoint;
std::string host{};
uint16_t port{0};
std::string uri{};
if (endpoint.empty()) {
MCP_LOG(MCP_LOG_LEVEL_ERROR, "No endpoint specified for transport");
throw std::runtime_error("No endpoint specified for transport");
}
std::regex url_regex(R"(^(https?:\/\/)?([^:\/\s]+):(\d+)(\/.*)?$)");
std::smatch matches;
if (std::regex_match(endpoint, matches, url_regex)) {
host = matches[URL_REGEX_GROUP_HOST].str();
if (host.empty()) {
MCP_LOG(MCP_LOG_LEVEL_ERROR, "Host is required in endpoint: " + endpoint);
throw std::runtime_error("Host is required in endpoint: " + endpoint);
}
try {
int portInt = std::stoi(matches[URL_REGEX_GROUP_PORT].str());
if (portInt < PORT_MIN || portInt > MAX_PORT_NUMBER) {
MCP_LOG(MCP_LOG_LEVEL_ERROR, "Port must be between " + std::to_string(PORT_MIN) +
" and " + std::to_string(MAX_PORT_NUMBER) +
" in endpoint: " + endpoint);
throw std::runtime_error("Port must be between " + std::to_string(PORT_MIN) +
" and " + std::to_string(MAX_PORT_NUMBER) +
" in endpoint: " + endpoint);
}
port = static_cast<uint16_t>(portInt);
} catch (const std::exception& e) {
MCP_LOG(MCP_LOG_LEVEL_ERROR, "Failed to parse port from endpoint '" + endpoint + "': " + e.what());
throw std::runtime_error("Failed to parse port from endpoint '" + endpoint + "': " + e.what());
}
if (matches[URL_REGEX_GROUP_URI].matched) {
uri = matches[URL_REGEX_GROUP_URI].str();
if (!uri.empty() && uri[0] != '/') {
uri = "/" + uri;
}
} else {
uri = "/";
}
MCP_LOG(MCP_LOG_LEVEL_INFO, "Parsed endpoint - Host: " + host + ", Port: " +
std::to_string(port) + ", URI: " + uri);
}
httpConfig.host = host;
httpConfig.port = port;
httpConfig.tlsConfig_ = streamableConfig_.tlsConfig;
httpConfig.ioThreadNum = streamableConfig_.ioThreads;
httpConfig.routeMap[uri] = [this](const Http::HttpRequest& request, RequestContext& ctx) {
DispatchRequest(request, ctx);
};
httpServerManager_ = std::make_unique<Http::HttpServerManager>(httpConfig);
try {
httpServerManager_->Start();
} catch (const std::exception& e) {
MCP_LOG(MCP_LOG_LEVEL_ERROR, "Failed to start HTTP server manager: " + std::string(e.what()));
throw;
}
MCP_LOG(MCP_LOG_LEVEL_INFO, "HTTP server manager started successfully");
}
void ServerManager::StdioServerManagerStart()
{
std::shared_ptr<ServerTransport> stdioTransport_ = std::make_shared<StdioServerTransport>();
stdioSession_ = std::make_shared<ServerSession>(stdioTransport_, config_, GenerateSessionId());
stdioSession_->SetServerCapabilities(config_.capabilities);
if (requestCallback_) {
stdioSession_->SetIncomingRequestCallback(requestCallback_);
}
stdioTransport_->Listen();
}
void ServerManager::Start()
{
if (isStdio_) {
StdioServerManagerStart();
return;
}
uint32_t numThreads = config_.workerThreads;
MCP_LOG(MCP_LOG_LEVEL_INFO, "Starting ServerManager with " + std::to_string(numThreads) + " worker threads");
eventSystems_.reserve(numThreads);
workerThreads_.reserve(numThreads);
threadSessions_.resize(numThreads);
threadQueues_.reserve(numThreads);
threadQueueEventIds_.resize(numThreads, -1);
notifyArgs_.reserve(numThreads);
for (uint32_t i = 0; i < numThreads; ++i) {
auto eventSystem = std::make_unique<EventSystem>(true, i);
if (!eventSystem->Init()) {
MCP_LOG(MCP_LOG_LEVEL_ERROR, "Failed to initialize EventSystem for thread " + std::to_string(i));
throw std::runtime_error("Failed to initialize EventSystem for thread " + std::to_string(i));
}
auto queue = std::make_unique<MPSCQueue<WorkerMessage>>(QUEUE_CAPACITY);
auto notifyArg = std::make_shared<NotifyEventArg>();
notifyArg->threadId = static_cast<int>(i);
notifyArg->eventSystem = eventSystem.get();
notifyArgs_.push_back(notifyArg);
int eventId = eventSystem->CreateNotifyEventId(
[this](int fd, short events, void* arg) { HandleQueueNotification(fd, events, arg); },
notifyArg.get(),
true);
if (eventId == -1) {
MCP_LOG(MCP_LOG_LEVEL_ERROR, "Failed to create event notification for thread " + std::to_string(i));
throw std::runtime_error("Failed to create event notification for thread " + std::to_string(i));
}
threadQueueEventIds_[i] = eventId;
notifyArg->eventId = eventId;
eventSystems_.push_back(std::move(eventSystem));
threadQueues_.push_back(std::move(queue));
}
HttpServerManagerStart();
for (uint32_t i = 0; i < numThreads; ++i) {
workerThreads_.emplace_back(&ServerManager::ThreadMain, this, i);
}
MCP_LOG(MCP_LOG_LEVEL_INFO, "ServerManager started successfully with " +
std::to_string(numThreads) + " worker threads");
}
void ServerManager::StdioServerManagerStop()
{
MCP_LOG(MCP_LOG_LEVEL_INFO, "Stopping Stdio ServerManager...");
if (stdioSession_) {
stdioSession_.reset();
}
if (stdioTransport_) {
stdioTransport_->Terminate();
stdioTransport_.reset();
}
MCP_LOG(MCP_LOG_LEVEL_INFO, "Stdio ServerManager stopped successfully.");
}
void ServerManager::Stop()
{
if (isStdio_) {
StdioServerManagerStop();
return;
}
MCP_LOG(MCP_LOG_LEVEL_INFO, "Stopping ServerManager...");
for (size_t i = 0; i < threadQueueEventIds_.size(); ++i) {
if (threadQueueEventIds_[i] != -1 && eventSystems_[i]) {
eventSystems_[i]->CloseNotifyEventId(threadQueueEventIds_[i]);
}
}
for (auto& eventSystem : eventSystems_) {
if (eventSystem) {
eventSystem->Stop();
}
}
for (auto& thread : workerThreads_) {
if (thread.joinable()) {
thread.join();
}
}
httpServerManager_->Stop();
MCP_LOG(MCP_LOG_LEVEL_INFO, "ServerManager stopped successfully.");
}
void ServerManager::SetIncomingRequestCallback(IncomingRequestCallback callback)
{
requestCallback_ = callback;
}
std::shared_ptr<ServerSession> ServerManager::NewSession(const std::string& sessionId)
{
std::shared_ptr<ServerTransport> transport = std::make_shared<StreamableHttpServerTransport>(
sessionId, streamableConfig_.isJsonResponseEnabled, false);
if (transport == nullptr) {
MCP_LOG(MCP_LOG_LEVEL_ERROR, "Failed to create transport layer.");
throw std::runtime_error("Failed to create transport layer.");
}
transport->Listen();
auto session = std::make_shared<ServerSession>(transport, config_, sessionId, false);
if (session == nullptr) {
MCP_LOG(MCP_LOG_LEVEL_ERROR, "Failed to create session layer.");
throw std::runtime_error("Failed to create session layer.");
}
session->SetServerCapabilities(config_.capabilities);
if (requestCallback_) {
session->SetIncomingRequestCallback(requestCallback_);
}
return session;
}
std::shared_ptr<ServerSession> ServerManager::GetSession(const std::string& sessionId) const
{
int threadId = GetThreadIdForSession(sessionId);
if (threadId >= 0 && threadId < static_cast<int>(threadSessions_.size())) {
auto it = threadSessions_[threadId].find(sessionId);
if (it != threadSessions_[threadId].end()) {
return it->second;
}
}
return nullptr;
}
int ServerManager::GetThreadIdForSession(const std::string& sessionId) const
{
std::hash<std::string> hasher;
return static_cast<int>(hasher(sessionId) % config_.workerThreads);
}
void ServerManager::HandleRequest(const HttpRequest& request, RequestContext& context)
{
if (authenticator_ != nullptr) {
AuthenticationResult authResult = authenticator_->Authenticate(request.headers);
if (!authResult.authenticated) {
MCP_LOG(MCP_LOG_LEVEL_WARN,
std::string("Authentication failed: ") +
authResult.errorDescription.value_or("Unknown error"));
HttpResponse response;
response.statusCode = Http::HTTP_STATUS_UNAUTHORIZED;
response.statusText = "Unauthorized";
response.headers[Http::CONTENT_TYPE_HEADER] = Http::CONTENT_TYPE_JSON;
nlohmann::json errorJson;
errorJson["error"] = authResult.errorDescription.value_or("Authentication failed");
response.body = errorJson.dump();
if (context.httpSendFunc) {
context.httpSendFunc(response, context);
}
return;
}
if (authorizer_ != nullptr && !authorizer_->Authorize(authResult)) {
MCP_LOG(MCP_LOG_LEVEL_WARN, "Authorization failed: insufficient scopes");
HttpResponse response;
response.statusCode = Http::HTTP_STATUS_FORBIDDEN;
response.statusText = "Forbidden";
response.headers[Http::CONTENT_TYPE_HEADER] = Http::CONTENT_TYPE_JSON;
nlohmann::json errorJson;
errorJson["error"] = "Insufficient permissions";
response.body = errorJson.dump();
if (context.httpSendFunc) {
context.httpSendFunc(response, context);
}
return;
}
MCP_LOG(MCP_LOG_LEVEL_DEBUG, "Authentication and authorization successful");
}
if (streamableConfig_.stateless) {
HandleRequestStateless(request, context);
} else {
HandleRequestStateful(request, context);
}
}
void ServerManager::HandleRequestStateless(const HttpRequest& request, RequestContext& context)
{
std::shared_ptr<ServerTransport> transport = std::make_shared<StreamableHttpServerTransport>(
"", streamableConfig_.isJsonResponseEnabled, true);
auto session = std::make_shared<ServerSession>(transport, config_, context.sessionId, true);
session->SetServerCapabilities(config_.capabilities);
if (requestCallback_) {
session->SetIncomingRequestCallback(requestCallback_);
}
session->HandleRequest(request, context);
}
void ServerManager::HandleRequestStateful(const HttpRequest& request, RequestContext& context)
{
int threadId = GetThreadIdForSession(context.sessionId);
MCP_LOG(MCP_LOG_LEVEL_INFO, "Handling request for session " + context.sessionId +
" in thread " + std::to_string(threadId));
std::shared_ptr<ServerSession> session = nullptr;
auto& sessions =
const_cast<std::unordered_map<std::string, std::shared_ptr<ServerSession>>&>(threadSessions_[threadId]);
auto it = sessions.find(context.sessionId);
if (it != sessions.end()) {
session = std::dynamic_pointer_cast<ServerSession>(it->second);
} else {
session = NewSession(context.sessionId);
sessions[context.sessionId] = session;
}
session->HandleRequest(request, context);
}
std::string ServerManager::GenerateSessionId() const
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 15);
std::uniform_int_distribution<> dis2(8, 11);
std::stringstream ss;
for (int i = 0; i < 32; i++) {
if (i == 8 || i == 12 || i == 16 || i == 20) {
ss << "-";
}
int v;
if (i == 12) {
v = 4;
} else if (i == 16) {
v = dis2(gen);
} else {
v = dis(gen);
}
ss << std::hex << v;
}
return ss.str();
}
void ServerManager::DispatchRequest(const HttpRequest& request, RequestContext& context)
{
if (context.sessionId.empty()) {
context.sessionId = GenerateSessionId();
}
MCP_LOG(MCP_LOG_LEVEL_INFO, "Get request from connection " +
std::to_string(context.connectionId) + " with session ID: " + context.sessionId);
int threadId = GetThreadIdForSession(context.sessionId);
auto& queue = threadQueues_[threadId];
DispatchRequestMsg requestMsg{request, context};
WorkerMessage msg{MessageType::REQUEST, requestMsg};
if (queue->Push(msg)) {
if (eventSystems_[threadId] && threadQueueEventIds_[threadId] != -1) {
if (eventSystems_[threadId]->NotifyEventId(threadQueueEventIds_[threadId])) {
MCP_LOG(MCP_LOG_LEVEL_INFO, "Dispatched request to thread " +
std::to_string(threadId) + " for session " + context.sessionId);
} else {
MCP_LOG(MCP_LOG_LEVEL_ERROR, "Failed to notify thread " +
std::to_string(threadId) + " for session " + context.sessionId);
}
}
} else {
MCP_LOG(MCP_LOG_LEVEL_ERROR, "Failed to dispatch request to thread " +
std::to_string(threadId) + " for session " + context.sessionId);
}
}
void ServerManager::HandleQueueNotification(int fd, short events, void* arg)
{
NotifyEventArg* notifyEventArg = static_cast<NotifyEventArg*>(arg);
if (notifyEventArg == nullptr) {
MCP_LOG(MCP_LOG_LEVEL_ERROR, "NotifyEventArg is null in HandleQueueNotification");
return;
}
uint64_t eventValue;
while (notifyEventArg->eventSystem->ReadEventFd(fd, eventValue)) {
}
auto& queue = threadQueues_[notifyEventArg->threadId];
WorkerMessage msg;
size_t processedCount = 0;
size_t errorCount = 0;
while (processedCount < MAX_BATCH_SIZE && queue->TryPop(msg)) {
try {
if (msg.type == MessageType::REQUEST) {
auto& requestMsg = std::get<DispatchRequestMsg>(msg.data);
HandleRequest(requestMsg.request, requestMsg.context);
} else if (msg.type == MessageType::RESPONSE) {
auto& responseMsg = std::get<DispatchResponseMsg>(msg.data);
HandleResponse(responseMsg);
}
} catch (const std::exception& e) {
MCP_LOG(MCP_LOG_LEVEL_ERROR, "Error handling message in thread " +
std::to_string(notifyEventArg->threadId) + ": " + e.what());
++errorCount;
}
++processedCount;
}
if (processedCount > 0 || errorCount > 0) {
MCP_LOG(MCP_LOG_LEVEL_DEBUG, "Thread " + std::to_string(notifyEventArg->threadId) + " processed " +
std::to_string(processedCount) + " messages (" +std::to_string(errorCount) + " errors) in batch");
}
if (processedCount >= MAX_BATCH_SIZE && !queue->Empty()) {
notifyEventArg->eventSystem->NotifyEventId(notifyEventArg->eventId);
MCP_LOG(MCP_LOG_LEVEL_DEBUG, "Thread " + std::to_string(notifyEventArg->threadId) +
" re-triggered notification for remaining messages");
}
}
void ServerManager::HandleResponse(const DispatchResponseMsg& responseMsg)
{
RequestContext mutableContext = responseMsg.context;
auto session = responseMsg.sessionKeepAlive;
if (!session) {
session = GetSession(responseMsg.sessionId);
}
if (!session) {
MCP_LOG(MCP_LOG_LEVEL_ERROR, std::string("Session not found for response: ") + mutableContext.sessionId);
return;
}
session->SendResponse(responseMsg.requestId, responseMsg.result, mutableContext);
MCP_LOG(MCP_LOG_LEVEL_DEBUG, std::string("Sent response for request in session ") + mutableContext.sessionId);
}
bool ServerManager::DispatchResponse(const RequestId& requestId, std::shared_ptr<Result> result,
const RequestContext& context,
std::shared_ptr<ServerSession> sessionKeepAlive)
{
if (isStdio_) {
MCP_LOG(MCP_LOG_LEVEL_WARN, "DispatchResponse not supported in stdio mode");
return false;
}
int threadId = GetThreadIdForSession(context.sessionId);
DispatchResponseMsg responseMsg{requestId, result, std::move(sessionKeepAlive), context, context.sessionId};
WorkerMessage msg{MessageType::RESPONSE, responseMsg};
auto& queue = threadQueues_[threadId];
if (queue->Push(msg)) {
if (eventSystems_[threadId] && threadQueueEventIds_[threadId] != -1) {
if (eventSystems_[threadId]->NotifyEventId(threadQueueEventIds_[threadId])) {
MCP_LOG(MCP_LOG_LEVEL_DEBUG, "Dispatched response to thread " +
std::to_string(threadId) + " for session " + context.sessionId);
return true;
} else {
MCP_LOG(MCP_LOG_LEVEL_ERROR, "Failed to notify thread " + std::to_string(threadId) + " for response");
}
}
} else {
MCP_LOG(MCP_LOG_LEVEL_ERROR, "Failed to push response to queue for thread " + std::to_string(threadId));
}
return false;
}
}