238 lines
5.8 KiB
C++
238 lines
5.8 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.
|
|
*/
|
|
|
|
#ifndef MCP_RESOURCE_H
|
|
#define MCP_RESOURCE_H
|
|
|
|
#include "mcp_protocol.h"
|
|
|
|
namespace mcp {
|
|
|
|
/**
|
|
* @class file_resource
|
|
* @brief Resource for file system operations
|
|
*
|
|
* The file_resource class provides access to files and directories.
|
|
*/
|
|
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 type
|
|
* @return Resource type
|
|
*/
|
|
resource_type type() const override {
|
|
return resource_type::file;
|
|
}
|
|
|
|
/**
|
|
* @brief Get resource metadata
|
|
* @return Metadata as JSON
|
|
*/
|
|
json metadata() const override;
|
|
|
|
/**
|
|
* @brief Handle request
|
|
* @param req The request
|
|
* @return The response
|
|
*/
|
|
response handle_request(const request& req) override;
|
|
|
|
/**
|
|
* @brief Get resource schema
|
|
* @return The schema as JSON
|
|
*/
|
|
json schema() const override;
|
|
|
|
private:
|
|
std::string base_path_;
|
|
|
|
// Helper methods
|
|
response read_file(const std::string& path);
|
|
response write_file(const std::string& path, const std::string& content);
|
|
response delete_file(const std::string& path);
|
|
response list_directory(const std::string& path);
|
|
};
|
|
|
|
/**
|
|
* @class directory_resource
|
|
* @brief Resource for directory operations
|
|
*
|
|
* The directory_resource class provides access to directories.
|
|
*/
|
|
class directory_resource : public resource {
|
|
public:
|
|
/**
|
|
* @brief Constructor
|
|
* @param base_path The base path for directory operations (for security)
|
|
*/
|
|
explicit directory_resource(const std::string& base_path);
|
|
|
|
/**
|
|
* @brief Get resource type
|
|
* @return Resource type
|
|
*/
|
|
resource_type type() const override {
|
|
return resource_type::directory;
|
|
}
|
|
|
|
/**
|
|
* @brief Get resource metadata
|
|
* @return Metadata as JSON
|
|
*/
|
|
json metadata() const override;
|
|
|
|
/**
|
|
* @brief Handle request
|
|
* @param req The request
|
|
* @return The response
|
|
*/
|
|
response handle_request(const request& req) override;
|
|
|
|
/**
|
|
* @brief Get resource schema
|
|
* @return The schema as JSON
|
|
*/
|
|
json schema() const override;
|
|
|
|
private:
|
|
std::string base_path_;
|
|
|
|
// Helper methods
|
|
response list_directory(const std::string& path);
|
|
response create_directory(const std::string& path);
|
|
response delete_directory(const std::string& path);
|
|
};
|
|
|
|
/**
|
|
* @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<response(const request&)>;
|
|
|
|
/**
|
|
* @brief Constructor
|
|
* @param name Resource name
|
|
* @param description Resource description
|
|
*/
|
|
api_resource(const std::string& name, const std::string& description);
|
|
|
|
/**
|
|
* @brief Get resource type
|
|
* @return Resource type
|
|
*/
|
|
resource_type type() const override {
|
|
return resource_type::api;
|
|
}
|
|
|
|
/**
|
|
* @brief Get resource metadata
|
|
* @return Metadata as JSON
|
|
*/
|
|
json metadata() const override;
|
|
|
|
/**
|
|
* @brief Handle request
|
|
* @param req The request
|
|
* @return The response
|
|
*/
|
|
response handle_request(const request& req) override;
|
|
|
|
/**
|
|
* @brief Get resource schema
|
|
* @return The schema as JSON
|
|
*/
|
|
json schema() const override;
|
|
|
|
/**
|
|
* @brief Register a handler for a specific path and HTTP method
|
|
* @param method The HTTP method
|
|
* @param path The path (relative to the resource path)
|
|
* @param handler The handler function
|
|
* @param description Description of the endpoint
|
|
* @param schema Schema of the endpoint parameters/response
|
|
*/
|
|
void register_handler(http_method method,
|
|
const std::string& path,
|
|
handler_func handler,
|
|
const std::string& description = "",
|
|
const json& schema = json::object());
|
|
|
|
private:
|
|
struct endpoint {
|
|
handler_func handler;
|
|
std::string description;
|
|
json schema;
|
|
};
|
|
|
|
std::string name_;
|
|
std::string description_;
|
|
std::map<std::string, std::map<http_method, endpoint>> endpoints_;
|
|
};
|
|
|
|
/**
|
|
* @class image_resource
|
|
* @brief Resource for image operations
|
|
*
|
|
* The image_resource class provides access to images.
|
|
*/
|
|
class image_resource : public resource {
|
|
public:
|
|
/**
|
|
* @brief Constructor
|
|
* @param base_path The base path for image files (for security)
|
|
*/
|
|
explicit image_resource(const std::string& base_path);
|
|
|
|
/**
|
|
* @brief Get resource type
|
|
* @return Resource type
|
|
*/
|
|
resource_type type() const override {
|
|
return resource_type::image;
|
|
}
|
|
|
|
/**
|
|
* @brief Get resource metadata
|
|
* @return Metadata as JSON
|
|
*/
|
|
json metadata() const override;
|
|
|
|
/**
|
|
* @brief Handle request
|
|
* @param req The request
|
|
* @return The response
|
|
*/
|
|
response handle_request(const request& req) override;
|
|
|
|
/**
|
|
* @brief Get resource schema
|
|
* @return The schema as JSON
|
|
*/
|
|
json schema() const override;
|
|
|
|
private:
|
|
std::string base_path_;
|
|
|
|
// Helper methods
|
|
response get_image(const std::string& path);
|
|
response upload_image(const std::string& path, const std::string& content, bool is_base64 = false);
|
|
response resize_image(const std::string& path, int width, int height);
|
|
};
|
|
|
|
} // namespace mcp
|
|
|
|
#endif // MCP_RESOURCE_H
|