131 lines
3.2 KiB
C++
131 lines
3.2 KiB
C++
/**
|
|
* @file mcp_resource.h
|
|
* @brief Resource implementation for MCP
|
|
*
|
|
* This file defines the base resource class and common resource types for the MCP protocol.
|
|
* Follows the 2024-11-05 basic protocol specification.
|
|
*/
|
|
|
|
#ifndef MCP_RESOURCE_H
|
|
#define MCP_RESOURCE_H
|
|
|
|
#include "mcp_message.h"
|
|
|
|
namespace mcp {
|
|
|
|
/**
|
|
* @class resource
|
|
* @brief Base class for MCP resources
|
|
*
|
|
* The resource class defines the interface for resources that can be
|
|
* accessed through the MCP protocol.
|
|
*/
|
|
class resource {
|
|
public:
|
|
virtual ~resource() = default;
|
|
|
|
/**
|
|
* @brief Get resource metadata
|
|
* @return Metadata as JSON
|
|
*/
|
|
virtual json get_metadata() const = 0;
|
|
|
|
/**
|
|
* @brief Access the resource
|
|
* @param params Parameters for accessing the resource
|
|
* @return The resource data
|
|
*/
|
|
virtual json access(const json& params) const = 0;
|
|
};
|
|
|
|
/**
|
|
* @class file_resource
|
|
* @brief Resource for file system operations
|
|
*
|
|
* The file_resource class provides access to files.
|
|
*/
|
|
class file_resource : public resource {
|
|
public:
|
|
/**
|
|
* @brief Constructor
|
|
* @param base_path The base path for file operations (for security)
|
|
*/
|
|
explicit file_resource(const std::string& base_path);
|
|
|
|
/**
|
|
* @brief Get resource metadata
|
|
* @return Metadata as JSON
|
|
*/
|
|
json get_metadata() const override;
|
|
|
|
/**
|
|
* @brief Access the resource
|
|
* @param params Parameters for accessing the resource
|
|
* @return The resource data
|
|
*/
|
|
json access(const json& params) const override;
|
|
|
|
private:
|
|
std::string base_path_;
|
|
|
|
// Helper methods
|
|
json read_file(const std::string& path) const;
|
|
json write_file(const std::string& path, const std::string& content) const;
|
|
json delete_file(const std::string& path) const;
|
|
json list_directory(const std::string& path) const;
|
|
};
|
|
|
|
/**
|
|
* @class api_resource
|
|
* @brief Resource for custom API endpoints
|
|
*
|
|
* The api_resource class provides a way to define custom API endpoints.
|
|
*/
|
|
class api_resource : public resource {
|
|
public:
|
|
using handler_func = std::function<json(const json&)>;
|
|
|
|
/**
|
|
* @brief Constructor
|
|
* @param name Resource name
|
|
* @param description Resource description
|
|
*/
|
|
api_resource(const std::string& name, const std::string& description);
|
|
|
|
/**
|
|
* @brief Get resource metadata
|
|
* @return Metadata as JSON
|
|
*/
|
|
json get_metadata() const override;
|
|
|
|
/**
|
|
* @brief Access the resource
|
|
* @param params Parameters for accessing the resource
|
|
* @return The resource data
|
|
*/
|
|
json access(const json& params) const override;
|
|
|
|
/**
|
|
* @brief Register a handler for a specific endpoint
|
|
* @param endpoint The endpoint name
|
|
* @param handler The handler function
|
|
* @param description Description of the endpoint
|
|
*/
|
|
void register_handler(const std::string& endpoint,
|
|
handler_func handler,
|
|
const std::string& description = "");
|
|
|
|
private:
|
|
struct endpoint_info {
|
|
handler_func handler;
|
|
std::string description;
|
|
};
|
|
|
|
std::string name_;
|
|
std::string description_;
|
|
std::map<std::string, endpoint_info> endpoints_;
|
|
};
|
|
|
|
} // namespace mcp
|
|
|
|
#endif // MCP_RESOURCE_H
|