cpp-mcp/include/mcp_server.h

156 lines
4.0 KiB
C
Raw Normal View History

2025-03-08 01:50:39 +08:00
/**
* @file mcp_server.h
* @brief MCP Server implementation using cpp-httplib
*
* This file implements the server-side functionality for the Model Context Protocol.
*/
#ifndef MCP_SERVER_H
#define MCP_SERVER_H
#include "mcp_protocol.h"
#include "mcp_resource.h"
#include "mcp_tools.h"
// Include the HTTP library
#include "httplib.h"
#include <string>
#include <map>
#include <vector>
#include <memory>
#include <mutex>
#include <thread>
#include <functional>
#include <iostream>
namespace mcp {
/**
* @class server
* @brief Main MCP server class
*
* The server class implements an HTTP server that exposes MCP resources
* and tools to clients (like AI models or agents).
*/
class server {
public:
/**
* @brief Constructor
* @param host The host to bind to (e.g., "localhost", "0.0.0.0")
* @param port The port to listen on
*/
server(const std::string& host = "localhost", int port = 8080);
/**
* @brief Destructor
*/
~server();
/**
* @brief Start the server
* @param blocking If true, this call blocks until the server stops
* @return True if the server started successfully
*/
bool start(bool blocking = true);
/**
* @brief Stop the server
*/
void stop();
/**
* @brief Check if the server is running
* @return True if the server is running
*/
bool is_running() const;
/**
* @brief Register a resource
* @param path The path to mount the resource at
* @param resource The resource to register
*/
void register_resource(const std::string& path, std::shared_ptr<resource> resource);
/**
* @brief Register a tool
* @param tool The tool to register
* @param handler The function to call when the tool is invoked
*/
void register_tool(const tool& tool, tool_handler handler);
/**
* @brief Get the list of available tools
* @return JSON array of available tools
*/
json get_tools() const;
/**
* @brief Get server information
* @return JSON object with server information
*/
json get_server_info() const;
// /**
// * @brief Set authentication callback
// * @param callback Function that takes username and password and returns true if valid
// */
// void set_auth_callback(std::function<bool(const std::string&, const std::string&)> callback);
/**
* @brief Enable or disable CORS
* @param enable True to enable CORS, false to disable
* @param allowed_origins Comma-separated list of allowed origins (default "*")
*/
void set_cors(bool enable, const std::string& allowed_origins = "*");
/**
* @brief Set the server name
* @param name The name of the server
*/
void set_name(const std::string& name);
private:
std::string host_;
int port_;
std::string name_;
bool cors_enabled_;
std::string allowed_origins_;
// The HTTP server
std::unique_ptr<httplib::Server> http_server_;
// Server thread (for non-blocking mode)
std::unique_ptr<std::thread> server_thread_;
// Resources map (path -> resource)
std::map<std::string, std::shared_ptr<resource>> resources_;
// Tools map (name -> handler)
std::map<std::string, std::pair<tool, tool_handler>> tools_;
// Authentication callback
std::function<bool(const std::string&, const std::string&)> auth_callback_;
// Mutex for thread safety
mutable std::mutex mutex_;
// Running flag
bool running_ = false;
// Handle incoming requests
void handle_request(const httplib::Request& req, httplib::Response& res);
// Convert httplib Request to mcp Request
request convert_request(const httplib::Request& req);
// Apply mcp Response to httplib Response
void apply_response(const response& mcp_res, httplib::Response& http_res);
// Set CORS headers if enabled
void set_cors_headers(httplib::Response& res);
};
} // namespace mcp
#endif // MCP_SERVER_H