To implement an MCP server over HTTP, configure a mcp::server::configuration object, instantiate a mcp::server, and register tools, resources, and prompts.
Key Steps:
- Configure: Set host and port in
mcp::server::configuration. - Initialize: Call
server.set_server_info(name, version). - Register Tools: Use
mcp::tool_builder to define parameters and register a handler function. Handlers receive mcp::json params and must return a JSON object containing a type: "text" array. - Register Resources: Use
server.register_resource(uri, resource_ptr). - Register Prompts: Use
mcp::prompt_builder and a handler that returns a JSON array of messages. - Start: Call
server.start(true) for blocking mode.
mcp::server::configuration srv_conf;
srv_conf.host = "localhost";
srv_conf.port = 8888;
mcp::server server(srv_conf);
server.set_server_info("MCP Example Server", "0.1.0");
// Register a tool
mcp::json hello_handler(const mcp::json& params, const std::string /* session_id */) {
std::string name = params.contains("name") ? params["name"].get<std::string>() : "World";
return {{
{"type", "text"},
{"text", "Hello, " + name + "!"}
}};
}
mcp::tool hello_tool = mcp::tool_builder("hello")
.with_description("Say hello")
.with_string_param("name", "Name to say hello to", "World")
.build();
server.register_tool(hello_tool, hello_handler);
server.start(true);