cpp-mcp/include/mcp_resource.h

131 lines
3.2 KiB
C
Raw Normal View History

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