mirror of
https://github.com/lighttransport/tinyusdz.git
synced 2026-01-18 01:11:17 +01:00
MCP server w.i.p.
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
#include <iostream>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include "prim-types.hh"
|
||||
#include "tydra/mcp-server.hh"
|
||||
#include "tydra/command-and-history.hh"
|
||||
@@ -20,24 +26,28 @@ int main(int argc, char **argv) {
|
||||
|
||||
}
|
||||
|
||||
std::cout << "is_setr " << parser.is_set("--port") << "\n";
|
||||
int port = 8085;
|
||||
|
||||
double portval;
|
||||
if (!parser.get("--port", portval)) {
|
||||
std::cerr << "--port is missing or invalid\n";
|
||||
return -1;
|
||||
if (parser.is_set("--port")) {
|
||||
if (!parser.get("--port", portval)) {
|
||||
std::cerr << "--port is missing or invalid\n";
|
||||
return -1;
|
||||
}
|
||||
port = int(portval);
|
||||
}
|
||||
|
||||
int port = int(portval);
|
||||
|
||||
std::string hostname;
|
||||
if (!parser.get("--host", hostname)) {
|
||||
std::cerr << "--host is missing or invalid\n";
|
||||
return -1;
|
||||
std::string hostname = "localhost";
|
||||
if (parser.is_set("--host")) {
|
||||
if (!parser.get("--host", hostname)) {
|
||||
std::cerr << "--host is missing or invalid\n";
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "port " << port << "\n";
|
||||
std::cout << "hostname " << hostname << "\n";
|
||||
|
||||
std::cout << "http://" + hostname << ":" << port << "/mcp" << "\n";
|
||||
|
||||
tydra::MCPServer server;
|
||||
if (!server.init(port, hostname)) {
|
||||
@@ -45,16 +55,27 @@ int main(int argc, char **argv) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool done =false;
|
||||
while (!done) {
|
||||
|
||||
Layer empty;
|
||||
|
||||
tydra::EditHistory hist;
|
||||
hist.layer = std::move(empty);
|
||||
#ifdef _WIN32
|
||||
Sleep(1000);
|
||||
#else
|
||||
sleep(1);
|
||||
#endif
|
||||
|
||||
tydra::HistoryQueue queue;
|
||||
if (!queue.push(std::move(hist))) {
|
||||
return -1;
|
||||
//Layer empty;
|
||||
//
|
||||
//tydra::EditHistory hist;
|
||||
//hist.layer = std::move(empty);
|
||||
|
||||
//tydra::HistoryQueue queue;
|
||||
//if (!queue.push(std::move(hist))) {
|
||||
// return -1;
|
||||
//}
|
||||
}
|
||||
|
||||
server.stop();
|
||||
|
||||
return 0;
|
||||
|
||||
|
||||
18
examples/mcp_server/test-initialize.sh
Normal file
18
examples/mcp_server/test-initialize.sh
Normal file
@@ -0,0 +1,18 @@
|
||||
curl -X POST \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"jsonrpc": "2.0",
|
||||
"method": "initialize",
|
||||
"params": {
|
||||
"protocolVersion": "2025-03-26",
|
||||
"capabilities": {
|
||||
"tools": {}
|
||||
},
|
||||
"clientInfo": {
|
||||
"name": "curl-client",
|
||||
"version": "1.0.0"
|
||||
}
|
||||
},
|
||||
"id": 1
|
||||
}' \
|
||||
http://localhost:8085/mcp
|
||||
14
examples/mcp_server/test-tools-call.sh
Normal file
14
examples/mcp_server/test-tools-call.sh
Normal file
@@ -0,0 +1,14 @@
|
||||
curl -X POST \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"jsonrpc": "2.0",
|
||||
"method": "tools/call",
|
||||
"params": {
|
||||
"name": "tusdcat",
|
||||
"arguments": {
|
||||
"text": "#usda 1.0\n def \"bora\" { }"
|
||||
},
|
||||
},
|
||||
"id": 2
|
||||
}' \
|
||||
http://localhost:8085/mcp
|
||||
11
examples/mcp_server/test-tools-list.sh
Normal file
11
examples/mcp_server/test-tools-list.sh
Normal file
@@ -0,0 +1,11 @@
|
||||
curl -X POST \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"jsonrpc": "2.0",
|
||||
"method": "tools/list",
|
||||
"params": {
|
||||
},
|
||||
},
|
||||
"id": 2
|
||||
}' \
|
||||
http://localhost:8085/mcp
|
||||
@@ -82,7 +82,11 @@ class MCPServer::Impl {
|
||||
// Run the server
|
||||
bool run();
|
||||
|
||||
bool stop();
|
||||
bool stop() {
|
||||
// Nothing to do here.
|
||||
// mg_stop() is called in dtor.
|
||||
return true;
|
||||
}
|
||||
|
||||
// Register a JSON-RPC method handler
|
||||
void register_method(const std::string& method, MethodHandler handler);
|
||||
@@ -91,8 +95,8 @@ class MCPServer::Impl {
|
||||
struct mg_context *ctx_ = nullptr; // Pointer to the CivetWeb context
|
||||
std::map<std::string, MethodHandler> method_handlers_;
|
||||
|
||||
// Static callback for HTTP requests
|
||||
static int http_handler(struct mg_connection *conn, void *user_data);
|
||||
// Static callback for MCP requests(POST + jsonrpc)
|
||||
static int mcp_handler(struct mg_connection *conn, void *user_data);
|
||||
|
||||
// Process JSON-RPC request
|
||||
JsonRpcResponse process_request(const JsonRpcRequest& request);
|
||||
@@ -104,8 +108,7 @@ class MCPServer::Impl {
|
||||
JsonRpcResponse create_error_response(int code, const std::string& message, const nlohmann::json& id = nullptr);
|
||||
};
|
||||
|
||||
// Static HTTP handler implementation
|
||||
int MCPServer::Impl::http_handler(struct mg_connection *conn, void *user_data) {
|
||||
int MCPServer::Impl::mcp_handler(struct mg_connection *conn, void *user_data) {
|
||||
MCPServer::Impl* server = static_cast<MCPServer::Impl*>(user_data);
|
||||
|
||||
const struct mg_request_info *request_info = mg_get_request_info(conn);
|
||||
@@ -157,20 +160,28 @@ int MCPServer::Impl::http_handler(struct mg_connection *conn, void *user_data) {
|
||||
JsonRpcRequest MCPServer::Impl::parse_request(const std::string& json_str) {
|
||||
JsonRpcRequest request;
|
||||
|
||||
nlohmann::json json_obj = nlohmann::json::parse(json_str);
|
||||
|
||||
if (json_obj.contains("jsonrpc")) {
|
||||
request.jsonrpc = json_obj["jsonrpc"];
|
||||
}
|
||||
if (json_obj.contains("method")) {
|
||||
request.method = json_obj["method"];
|
||||
}
|
||||
if (json_obj.contains("params")) {
|
||||
request.params = json_obj["params"];
|
||||
}
|
||||
if (json_obj.contains("id")) {
|
||||
request.id = json_obj["id"];
|
||||
}
|
||||
nlohmann::json json_obj;
|
||||
if (!nlohmann::json::accept(json_str)) {
|
||||
// Return invalid request on parse error
|
||||
request.method = "";
|
||||
request.jsonrpc = "";
|
||||
return request;
|
||||
}
|
||||
|
||||
json_obj = nlohmann::json::parse(json_str);
|
||||
|
||||
if (json_obj.contains("jsonrpc")) {
|
||||
request.jsonrpc = json_obj["jsonrpc"];
|
||||
}
|
||||
if (json_obj.contains("method")) {
|
||||
request.method = json_obj["method"];
|
||||
}
|
||||
if (json_obj.contains("params")) {
|
||||
request.params = json_obj["params"];
|
||||
}
|
||||
if (json_obj.contains("id")) {
|
||||
request.id = json_obj["id"];
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
@@ -188,13 +199,13 @@ JsonRpcResponse MCPServer::Impl::process_request(const JsonRpcRequest& request)
|
||||
}
|
||||
|
||||
// Execute method handler
|
||||
nlohmann::json result = handler_it->second(request.params);
|
||||
|
||||
JsonRpcResponse response;
|
||||
response.id = request.id;
|
||||
response.result = result;
|
||||
|
||||
return response;
|
||||
nlohmann::json result = handler_it->second(request.params);
|
||||
|
||||
JsonRpcResponse response;
|
||||
response.id = request.id;
|
||||
response.result = result;
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
JsonRpcResponse MCPServer::Impl::create_error_response(int code, const std::string& message, const nlohmann::json& id) {
|
||||
@@ -216,6 +227,36 @@ bool MCPServer::Impl::init(int port, const std::string &host) {
|
||||
// TODO
|
||||
(void)host;
|
||||
|
||||
// Register MCP initialize method
|
||||
register_method("initialize", [](const nlohmann::json& params) -> nlohmann::json {
|
||||
// Extract client info if provided
|
||||
std::string client_name = "unknown";
|
||||
std::string client_version = "unknown";
|
||||
|
||||
if (params.contains("clientInfo")) {
|
||||
auto client_info = params["clientInfo"];
|
||||
if (client_info.contains("name")) {
|
||||
client_name = client_info["name"];
|
||||
}
|
||||
if (client_info.contains("version")) {
|
||||
client_version = client_info["version"];
|
||||
}
|
||||
}
|
||||
|
||||
// Return server capabilities
|
||||
return nlohmann::json{
|
||||
{"protocolVersion", "2025-03-26"},
|
||||
{"serverInfo", {
|
||||
{"name", "tinyusdz-mcp-server"},
|
||||
{"version", "1.0.0"}
|
||||
}},
|
||||
{"capabilities", {
|
||||
{"tools", nlohmann::json::object()},
|
||||
{"resources", nlohmann::json::object()}
|
||||
}}
|
||||
};
|
||||
});
|
||||
|
||||
// CivetWeb options
|
||||
std::string port_str = std::to_string(port);
|
||||
std::vector<const char *> options;
|
||||
@@ -232,8 +273,8 @@ bool MCPServer::Impl::init(int port, const std::string &host) {
|
||||
return false; // Failed to start server
|
||||
}
|
||||
|
||||
// Register HTTP handler for JSON-RPC endpoint
|
||||
mg_set_request_handler(ctx_, "/jsonrpc", http_handler, this);
|
||||
// Register HTTP handler for MCP endpoint
|
||||
mg_set_request_handler(ctx_, "/mcp", mcp_handler, this);
|
||||
|
||||
return true; // Server initialized successfully
|
||||
}
|
||||
@@ -253,6 +294,14 @@ bool MCPServer::init(int port, const std::string &host) {
|
||||
return impl_->init(port, host);
|
||||
}
|
||||
|
||||
bool MCPServer::run() {
|
||||
return impl_->run();
|
||||
}
|
||||
|
||||
bool MCPServer::stop() {
|
||||
return impl_->stop();
|
||||
}
|
||||
|
||||
} // namespace tydra
|
||||
} // namespace tinyusdz
|
||||
|
||||
|
||||
Reference in New Issue
Block a user