NeoGraph 0.16.0
A C++20 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
server.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <neograph/api.h>
12#include <neograph/mcp/types.h>
13
14#include <cstddef>
15#include <functional>
16#include <istream>
17#include <memory>
18#include <optional>
19#include <ostream>
20#include <string>
21
22namespace neograph::mcp {
23
27inline constexpr const char* MCP_PROTOCOL_VERSION = "2025-11-25";
28
29struct NEOGRAPH_API MCPServerConfig {
30 json server_info = json::object();
31 std::string instructions;
32 std::size_t max_concurrent_calls = 4;
33 std::size_t max_pending_calls = 64;
34};
35
53class NEOGRAPH_API MCPServer {
54 public:
55 using ToolHandler = std::function<CallToolResult(
56 const json&, const std::shared_ptr<graph::CancelToken>&)>;
57
60 std::function<json(const json&, const std::shared_ptr<graph::CancelToken>&, const json&)>;
61
63 using MethodHandler = std::function<json(const json&, const json&)>;
64
66 using ResponseSink = std::function<void(const json&)>;
67
72 using NoResponseSink = std::function<void(const json& id)>;
73
74 explicit MCPServer(MCPServerConfig config);
75 ~MCPServer();
76
77 MCPServer(const MCPServer&) = delete;
78 MCPServer& operator=(const MCPServer&) = delete;
79
81 void register_tool(ToolDefinition definition, ToolHandler handler);
82
84 void register_raw_tool(ToolDefinition definition, RawToolHandler handler);
85
87 void register_method(std::string method, MethodHandler handler);
88
90 void register_extension(std::string identifier, json settings = json::object());
91
96 using Reply = std::function<void(std::optional<json>)>;
97
102 json handle_message(const json& envelope);
103
110 void handle_message(const json& message, Reply reply);
111
113 void run(std::istream& in, std::ostream& out);
114 void run();
115
119
123
125 void stop();
126
127 bool initialized() const;
128 bool is_running() const;
129
133 std::string protocol_version() const;
134
135 private:
136 struct Impl;
137 std::unique_ptr<Impl> impl_;
138};
139
140} // namespace neograph::mcp
NEOGRAPH_API export/import macro for shared-library builds.
Cooperative cancellation primitive for graph runs.
Local, stdio-first MCP server with typed tools and bounded execution.
Definition server.h:53
void register_raw_tool(ToolDefinition definition, RawToolHandler handler)
Add a tool whose result shape is selected using request metadata.
std::function< void(const json &id)> NoResponseSink
Receives the id of a request that ended without a response: the client cancelled it,...
Definition server.h:72
void set_response_sink(ResponseSink sink)
Set the destination for asynchronously completed tool-call responses.
void register_tool(ToolDefinition definition, ToolHandler handler)
Add a tool. Names must be unique and registration closes at initialize.
void handle_message(const json &message, Reply reply)
Process one parsed message and pass its outcome to reply exactly once: before this returns,...
void register_method(std::string method, MethodHandler handler)
Register an extension JSON-RPC method before initialization.
std::function< json(const json &, const json &)> MethodHandler
Synchronous extension-method handler; the server wraps the JSON result.
Definition server.h:63
void run(std::istream &in, std::ostream &out)
Drive newline-delimited MCP stdio until EOF or a message-boundary stop.
std::function< json(const json &, const std::shared_ptr< graph::CancelToken > &, const json &)> RawToolHandler
Extension hook for polymorphic tool results such as CreateTaskResult.
Definition server.h:60
void set_response_sink(ResponseSink sink, NoResponseSink no_response)
As above; no_response receives the id of each call that ends without a response.
std::function< void(std::optional< json >)> Reply
The outcome of one message given to handle_message(message, reply): its response (an object,...
Definition server.h:96
std::string protocol_version() const
The revision initialize negotiated: the client's when it is one of MCP_SUPPORTED_PROTOCOL_VERSIONS,...
void stop()
Cooperatively cancel outstanding calls and drain the worker pool.
std::function< void(const json &)> ResponseSink
Receives complete JSON-RPC response envelopes produced asynchronously.
Definition server.h:66
void register_extension(std::string identifier, json settings=json::object())
Advertise an explicitly enabled MCP extension capability.
json handle_message(const json &envelope)
Process one parsed JSON-RPC message.
constexpr const char * MCP_PROTOCOL_VERSION
The newest revision the server negotiates.
Definition server.h:27
Complete typed values for the supported MCP tools client surface.