NeoGraph 0.16.0
A C++20 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
client.h
Go to the documentation of this file.
1
15#pragma once
16
17#include <neograph/api.h>
18#include <neograph/mcp/types.h>
19#include <neograph/tool.h>
20
21#include <asio/awaitable.hpp>
22
23#include <chrono>
24#include <functional>
25#include <memory>
26#include <optional>
27#include <string>
28#include <vector>
29
30namespace neograph::graph { class CancelToken; }
31
32namespace neograph::mcp {
33
34namespace detail {
35class ProtocolSession;
36}
37
44class NEOGRAPH_API MCPTool : public AsyncTool, public ContextualAsyncTool {
45 public:
52 MCPTool(const std::string& server_url,
53 const std::string& name,
54 const std::string& description,
55 const json& input_schema);
56
58 const ToolDefinition& get_mcp_definition() const noexcept { return definition_; }
59
61 CallToolResult execute_result(const json& arguments);
62 asio::awaitable<CallToolResult> execute_result_async(const json& arguments);
67 asio::awaitable<CallToolResult> execute_result_async(const json& arguments,
68 ToolExecutionContext context);
69
70 ChatTool get_definition() const override;
71
80 asio::awaitable<std::string> execute_async(const json& arguments) override;
81
85 asio::awaitable<std::string> execute_async(const json& arguments,
86 ToolExecutionContext context) override;
87
88 std::string get_name() const override { return definition_.name; }
89
90 private:
91 friend class MCPClient;
92 MCPTool(std::shared_ptr<detail::ProtocolSession> session,
93 ToolDefinition definition);
95 std::shared_ptr<detail::ProtocolSession> protocol_session();
96
97 std::string server_url_;
98 std::shared_ptr<detail::ProtocolSession> session_;
99 ToolDefinition definition_;
100};
101
121class NEOGRAPH_API MCPClient {
122 public:
141 explicit MCPClient(const std::string& server_url);
142 MCPClient(const std::string& server_url, MCPClientConfig config);
143
154 explicit MCPClient(std::vector<std::string> argv);
157 explicit MCPClient(StdioClientConfig config);
158
159 MCPClient(const MCPClient&) = delete;
160 MCPClient& operator=(const MCPClient&) = delete;
161 MCPClient(MCPClient&&) = delete;
162 MCPClient& operator=(MCPClient&&) = delete;
163
164
171 void shutdown() noexcept;
177 bool initialize(const std::string& client_name = "neograph");
178
180 asio::awaitable<bool> initialize_async(const std::string& client_name = "neograph");
181
187 bool initialize(const std::string& client_name,
188 std::chrono::steady_clock::time_point deadline,
189 std::shared_ptr<graph::CancelToken> cancel_token);
190 asio::awaitable<bool> initialize_async(const std::string& client_name,
191 std::chrono::steady_clock::time_point deadline,
192 std::shared_ptr<graph::CancelToken> cancel_token);
193
202 void set_tool_list_changed_handler(std::function<void()> handler);
203
204 bool is_initialized() const noexcept;
205 InitializeResult get_initialize_result() const;
206
211 std::vector<std::unique_ptr<Tool>> get_tools();
212
214 ListToolsPage list_tools(
215 const std::optional<std::string>& cursor = std::nullopt);
216 asio::awaitable<ListToolsPage> list_tools_async(
217 const std::optional<std::string>& cursor = std::nullopt);
218
220 std::vector<ToolDefinition> get_tool_definitions();
221
228 json call_tool(const std::string& name, const json& arguments);
229
230 CallToolResult call_tool_result(const std::string& name,
231 const json& arguments);
232
235 asio::awaitable<json> call_tool_async(const std::string& name,
236 const json& arguments);
237 asio::awaitable<CallToolResult> call_tool_result_async(
238 const std::string& name,
239 const json& arguments);
242 asio::awaitable<CallToolResult> call_tool_result_async(
243 const std::string& name,
244 const json& arguments,
245 std::chrono::steady_clock::time_point deadline,
246 std::shared_ptr<graph::CancelToken> cancel_token);
247
259 asio::awaitable<json> rpc_call_async(
260 const std::string& method,
261 const json& params = json::object(),
262 std::chrono::steady_clock::time_point deadline =
263 std::chrono::steady_clock::time_point::max(),
264 std::shared_ptr<graph::CancelToken> cancel_token = {});
265
266 private:
267 std::shared_ptr<detail::ProtocolSession> session_;
268};
269
270} // namespace neograph::mcp
NEOGRAPH_API export/import macro for shared-library builds.
Adapter base class for tools whose work is naturally coroutine-shaped (HTTP fetch,...
Definition tool.h:185
Optional async Tool extension that receives per-invocation context.
Definition tool.h:67
Abstract base class for tools that agents can call.
Definition tool.h:114
Client for connecting to MCP (Model Context Protocol) servers.
Definition client.h:121
MCPClient(std::vector< std::string > argv)
Construct a stdio-mode MCP client by spawning a subprocess.
MCPClient(const std::string &server_url)
Construct an HTTP-mode (Streamable HTTP) MCP client.
MCPClient(StdioClientConfig config)
Hardened local launch with validated replacement environment, cwd, bounded stderr capture,...
void shutdown() noexcept
Permanently revoke this client and all tools retaining its session.
Wraps a remote MCP server tool as a local Tool.
Definition client.h:44
MCPTool(const std::string &server_url, const std::string &name, const std::string &description, const json &input_schema)
Direct HTTP constructor for one known tool.
CallToolResult execute_result(const json &arguments)
Typed execution preserving all content block kinds and MCP result fields.
asio::awaitable< std::string > execute_async(const json &arguments) override
Execute the tool on the MCP server.
asio::awaitable< CallToolResult > execute_result_async(const json &arguments, ToolExecutionContext context)
Typed execution bounded by the dispatcher's context: its cancel token and deadline stop waiting for t...
std::string get_name() const override
Get the tool name.
Definition client.h:88
const ToolDefinition & get_mcp_definition() const noexcept
Full MCP definition, including outputSchema and annotations.
Definition client.h:58
ChatTool get_definition() const override
Get the tool definition metadata.
asio::awaitable< std::string > execute_async(const json &arguments, ToolExecutionContext context) override
What every dispatcher calls (ToolExecutionController): the call is bounded by the run's cancel token ...
Complete typed values for the supported MCP tools client surface.
Tool definition metadata sent to the LLM.
Definition types.h:62
Per-invocation context for a cooperative asynchronous tool.
Definition tool.h:39
Explicit subprocess boundary for local credentialless MCP adoption.
Definition types.h:55
Abstract tool interface for callable functions.