NeoGraph 0.16.0
A C++20 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
tool_service.h
1// neograph::grpc — single-tool-call gRPC service + remote-tool client.
2//
3// Two mirror halves of the same `ToolService.CallTool` RPC:
4//
5// * run_tool_server — SERVE local C++ functions over gRPC (the fair
6// gRPC counterpart of MCP's JSON-RPC 2.0 `tools/call`; example 55).
7// * GrpcRemoteTool — CONSUME a remote ToolService method as an
8// ordinary neograph::Tool, so an Agent/ReAct loop can call a tool
9// that lives in another process/language with no code change on
10// the agent side (example 57).
11//
12// Opt-in (NEOGRAPH_BUILD_GRPC). Whole header behind NEOGRAPH_HAVE_GRPC
13// so the umbrella include never pulls grpc++.
14//
15// make_tool_service() returns a std::unique_ptr<::grpc::Service>, which the
16// caller destroys: the header therefore includes the gRPC service base
17// (<grpcpp/impl/service_type.h>), and including it alone is enough to build
18// and destroy the service. The generated neograph.grpc.pb.h is not needed.
19
20#pragma once
21
22#ifdef NEOGRAPH_HAVE_GRPC
23
24#include <chrono>
25#include <functional>
26#include <memory>
27#include <string>
28#include <unordered_map>
29
30// ::grpc::Service, complete for the destructor of the returned unique_ptr.
31#if __has_include(<grpcpp/impl/service_type.h>)
32#include <grpcpp/impl/service_type.h>
33#else
34#include <grpcpp/impl/codegen/service_type.h>
35#endif
36
37#include <neograph/api.h> // NEOGRAPH_API
38#include <neograph/grpc/serve_options.h>
39#include <neograph/tool.h> // neograph::Tool, ChatTool, json
40
41#include <asio/awaitable.hpp>
42
43namespace neograph::grpc {
44
53using ToolFn = std::function<std::string(const std::string&)>;
54
57NEOGRAPH_API std::unique_ptr<::grpc::Service> make_tool_service(
58 std::unordered_map<std::string, ToolFn> tools);
59
65NEOGRAPH_API void run_tool_server(
66 const std::string& address,
67 std::unordered_map<std::string, ToolFn> tools);
70NEOGRAPH_API void run_tool_server(
71 const std::string& address,
72 std::unordered_map<std::string, ToolFn> tools,
73 const ServeOptions& options);
74
90class NEOGRAPH_API GrpcRemoteTool : public neograph::Tool,
92public:
94 GrpcRemoteTool(const std::string& target,
95 std::string name,
96 std::string description,
97 neograph::json parameters);
98 ~GrpcRemoteTool() override;
99
100 ChatTool get_definition() const override;
101 std::string execute(const neograph::json& arguments) override;
105 asio::awaitable<std::string> execute_async(
106 const neograph::json& arguments, neograph::ToolExecutionContext context) override;
107 std::string get_name() const override;
108
112 void set_call_timeout(std::chrono::milliseconds timeout);
113
114private:
115 struct Impl;
116 std::unique_ptr<Impl> impl_;
117};
118
119} // namespace neograph::grpc
120
121#endif // NEOGRAPH_HAVE_GRPC
NEOGRAPH_API export/import macro for shared-library builds.
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
Per-invocation context for a cooperative asynchronous tool.
Definition tool.h:39
Abstract tool interface for callable functions.