* Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
*/
#ifndef MCP_CLIENT_SESSION_INCLUDE_H_
#define MCP_CLIENT_SESSION_INCLUDE_H_
#include <atomic>
#include <functional>
#include <future>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>
#include "mcp_error.h"
#include "mcp_client.h"
#include "mcp_type.h"
#include "shared/base_session.h"
#include "shared/jsonrpc.h"
namespace Mcp {
class ClientSession : public BaseSession {
public:
using BaseSession::SendNotification;
using BaseSession::SendRequest;
explicit ClientSession(std::shared_ptr<ClientTransport> transport,
const ClientConfig& clientConfig = ClientConfig(), std::string sessionId = "");
std::future<std::shared_ptr<InitializeResult>> Initialize();
std::future<std::shared_ptr<EmptyResult>> SendPing();
void SendRootsListChanged();
void SetListRootsCallback(ListRootsCallback cb);
void SetElicitCallback(ElicitCallback cb);
void SetElicitUrlCallback(ElicitUrlCallback cb);
void SetLoggingCallback(LoggingCallback cb);
void SetSamplingCreateMessageCallback(McpClient::SamplingCreateMessageCallback cb,
SamplingCapability capability = SamplingCapability{});
std::future<std::shared_ptr<EmptyResult>> SetLoggingLevel(LoggingLevel level);
std::future<std::shared_ptr<CallToolResult>> CallTool(const std::string& name,
const std::optional<std::string>& arguments = std::nullopt,
int timeout = 0,
std::optional<ProgressCallback> progressCallback
= std::nullopt);
std::future<std::shared_ptr<ListToolsResult>> ListTools();
std::future<std::shared_ptr<ListToolsResult>> ListTools(const std::optional<std::string>& cursor);
std::future<std::shared_ptr<ListPromptsResult>> ListPrompts();
std::future<std::shared_ptr<ListResourcesResult>> ListResources();
std::future<std::shared_ptr<ListResourcesResult>> ListResources(const std::optional<std::string>& cursor);
std::future<std::shared_ptr<ListResourceTemplatesResult>> ListResourcesTemplates();
std::future<std::shared_ptr<ReadResourceResult>> ReadResource(const std::string& uri);
std::future<std::shared_ptr<EmptyResult>> SubscribeResource(const std::string& uri);
std::future<std::shared_ptr<EmptyResult>> UnsubscribeResource(const std::string& uri);
std::future<std::shared_ptr<GetPromptResult>> GetPrompt(const std::string& name,
const std::optional<JsonValue>& arguments = std::nullopt);
std::future<std::shared_ptr<CompleteResult>> Complete(const CompleteReference& ref,
const CompletionArgument& argument,
const std::optional<CompletionContext>& context
= std::nullopt);
void SendNotification(std::unique_ptr<Notification> notification,
std::optional<RequestId> relatedRequestId = std::nullopt) override;
bool IsInitialized() const
{
return initialized_;
}
ServerCapabilities GetServerCapabilities() const;
void SendInitializedNotification();
void SendProgressNotification(ProgressToken progressToken, double progress,
std::optional<double> total = std::nullopt,
const std::optional<std::string>& message = std::nullopt) override;
protected:
* @brief Handle an incoming JSON-RPC notification from the server.
*
* This is called by `BaseSession` when a notification message is parsed.
*/
void ReceivedNotification(const Notification& notification) override;
void ReceivedRequest(const RequestId& requestId, const Request& request, RequestContext& ctx) override;
private:
std::atomic<int64_t> _request_id{1};
std::mutex pending_mutex_;
std::unordered_map<int64_t, std::function<void(const std::string&)>> pending_callbacks_;
std::function<bool(const std::string&)> send_raw_;
ClientCapabilities BuildClientCapabilities() const;
void HandleRootsListRequest(const RequestId& requestId, const Request& request, RequestContext& ctx);
void HandleElicitRequest(const RequestId& requestId, const Request& request, RequestContext& ctx);
void HandleSamplingCreateMessageRequest(const RequestId& requestId, const Request& request, RequestContext& ctx);
void SendJsonRpcError(const RequestId& requestId, JsonRpcErrorCode code, const std::string& message,
RequestContext& ctx);
void SendJsonRpcErrorRaw(const RequestId& requestId, int code, const std::string& message, RequestContext& ctx);
void SendMethodNotFound(const RequestId& requestId, const std::string& method, RequestContext& ctx);
void SendInternalError(const RequestId& requestId, const std::string& message, RequestContext& ctx);
void HandleProgressNotification(const ProgressToken& progressToken, double progress,
std::optional<double> total, const std::optional<std::string>& message);
bool initialized_{false};
std::optional<ServerCapabilities> serverCapabilities_{std::nullopt};
ListRootsCallback listRootsCallback_{nullptr};
LoggingCallback loggingCallback_{nullptr};
ElicitCallback elicitCallback_{nullptr};
ElicitUrlCallback elicitUrlCallback_{nullptr};
McpClient::SamplingCreateMessageCallback samplingCreateMessageCallback_{nullptr};
SamplingCapability samplingCapability_{};
ClientConfig clientConfig_;
std::unordered_map<std::string, JsonValue> toolOutputSchemas_;
void CacheToolSchemas(const ListToolsResult& r);
void ValidateToolResult(const std::string& name, const CallToolResult& result);
};
}
#endif