Entropic 2.3.8
Local-first agentic inference engine
Loading...
Searching...
No Matches
mcp_c_api.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
14
15#include <cstdlib>
16#include <cstring>
17
26 return reinterpret_cast<entropic::MCPServerBase*>(server);
27}
28
41static char* alloc_string(const std::string& s) {
42 auto* p = static_cast<char*>(std::malloc(s.size() + 1));
43 std::memcpy(p, s.c_str(), s.size() + 1);
44 return p;
45}
46
47extern "C" {
48
57 if (server == nullptr) {
58 return "";
59 }
60 return cast(server)->name().c_str();
61}
62
71 if (server == nullptr) {
72 return alloc_string("[]");
73 }
74 return alloc_string(cast(server)->list_tools());
75}
76
88 const char* tool_name,
89 const char* args_json) {
90 if (server == nullptr || tool_name == nullptr) {
91 return alloc_string(
92 R"({"result":"Error: null server or tool_name","directives":[]})");
93 }
94 auto result = cast(server)->execute(
95 tool_name, args_json ? args_json : "{}");
96 return alloc_string(result);
97}
98
109 const char* config_json) {
110 if (server == nullptr) {
112 }
113 bool ok = cast(server)->configure(
114 config_json ? config_json : "{}");
116}
117
128 const char* path) {
129 if (server == nullptr || path == nullptr) {
131 }
132 bool ok = cast(server)->set_working_dir(path);
133 return ok ? ENTROPIC_OK : ENTROPIC_ERROR_IO;
134}
135
143 delete cast(server);
144}
145
146} // extern "C"
Concrete base class for MCP servers (80% logic).
Definition server_base.h:66
std::string execute(const std::string &tool_name, const std::string &args_json)
Execute a tool and wrap result in ServerResponse JSON.
const std::string & name() const
Get the server name.
virtual bool set_working_dir(const std::string &path)
Set the working directory.
virtual bool configure(const std::string &config_json)
Configure the server after creation.
entropic_error_t
Error codes returned by all C API functions.
Definition error.h:35
@ ENTROPIC_OK
Success.
Definition error.h:36
@ ENTROPIC_ERROR_INVALID_ARGUMENT
NULL pointer, empty string, out-of-range value.
Definition error.h:37
@ ENTROPIC_ERROR_IO
File/network I/O error.
Definition error.h:50
@ ENTROPIC_ERROR_INVALID_CONFIG
Config validation failed (missing fields, bad values)
Definition error.h:38
Pure C interface contract for MCP server plugins.
struct entropic_mcp_server * entropic_mcp_server_t
Opaque handle to an MCP server instance.
char * entropic_mcp_server_execute(entropic_mcp_server_t server, const char *tool_name, const char *args_json)
Execute a tool.
Definition mcp_c_api.cpp:86
entropic_error_t entropic_mcp_server_configure(entropic_mcp_server_t server, const char *config_json)
Configure a server.
static char * alloc_string(const std::string &s)
Allocate a C string copy for caller-owned return.
Definition mcp_c_api.cpp:41
entropic_error_t entropic_mcp_server_set_working_dir(entropic_mcp_server_t server, const char *path)
Set server working directory.
void entropic_mcp_server_destroy(entropic_mcp_server_t server)
Destroy a server instance.
const char * entropic_mcp_server_name(entropic_mcp_server_t server)
Get server name.
Definition mcp_c_api.cpp:56
char * entropic_mcp_server_list_tools(entropic_mcp_server_t server)
List tools as JSON array.
Definition mcp_c_api.cpp:70
static entropic::MCPServerBase * cast(entropic_mcp_server_t server)
Cast opaque handle to MCPServerBase pointer.
Definition mcp_c_api.cpp:25
MCPServerBase concrete base class + ServerResponse.