NeoGraph 0.10.0
A C++17 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
neograph::mcp::MCPClient Class Reference

Client for connecting to MCP (Model Context Protocol) servers. More...

#include <client.h>

Public Member Functions

json call_tool (const std::string &name, const json &arguments)
 Call a tool directly by name.
 
std::vector< std::unique_ptr< Tool > > get_tools ()
 Discover tools from the MCP server.
 
bool initialize (const std::string &client_name="neograph")
 Initialize the connection and perform the MCP handshake.
 
 MCPClient (const std::string &server_url)
 Construct an HTTP-mode MCP client.
 
 MCPClient (std::vector< std::string > argv)
 Construct a stdio-mode MCP client by spawning a subprocess.
 
asio::awaitable< json > rpc_call_async (const std::string &method, const json &params=json::object())
 Async variant of rpc_call for the HTTP transport.
 

Detailed Description

Client for connecting to MCP (Model Context Protocol) servers.

Handles the initialization handshake, tool discovery, and tool invocation. Both HTTP and stdio transports are available; pick via the appropriate constructor.

// HTTP
MCPClient http_client("http://localhost:8000");
http_client.initialize("my-agent");
auto tools = http_client.get_tools();
// stdio — spawn a subprocess
MCPClient stdio_client({"python", "server.py"});
stdio_client.initialize("my-agent");
auto tools2 = stdio_client.get_tools();
Client for connecting to MCP (Model Context Protocol) servers.
Definition client.h:97
bool initialize(const std::string &client_name="neograph")
Initialize the connection and perform the MCP handshake.

Definition at line 97 of file client.h.

Constructor & Destructor Documentation

◆ MCPClient() [1/2]

neograph::mcp::MCPClient::MCPClient ( const std::string &  server_url)
explicit

Construct an HTTP-mode MCP client.

Parameters
server_urlURL of the MCP server (e.g., "http://localhost:8000").

◆ MCPClient() [2/2]

neograph::mcp::MCPClient::MCPClient ( std::vector< std::string >  argv)
explicit

Construct a stdio-mode MCP client by spawning a subprocess.

Parameters
argvCommand + arguments (e.g., {"python", "server.py"}). argv[0] is resolved via PATH (execvp). Throws on fork/exec failure.

The subprocess is terminated (SIGTERM + waitpid) when the last reference to the underlying session is dropped — this is either the MCPClient itself or any MCPTool produced by get_tools().

Member Function Documentation

◆ call_tool()

json neograph::mcp::MCPClient::call_tool ( const std::string &  name,
const json &  arguments 
)

Call a tool directly by name.

Parameters
nameTool name on the server.
argumentsJSON object of tool arguments.
Returns
JSON response from the server.

◆ get_tools()

std::vector< std::unique_ptr< Tool > > neograph::mcp::MCPClient::get_tools ( )

Discover tools from the MCP server.

Returns
Vector of Tool unique_ptrs (MCPTool instances).

◆ initialize()

bool neograph::mcp::MCPClient::initialize ( const std::string &  client_name = "neograph")

Initialize the connection and perform the MCP handshake.

Parameters
client_nameClient identifier sent during handshake (default: "neograph").
Returns
True if initialization succeeded, false otherwise.

◆ rpc_call_async()

asio::awaitable< json > neograph::mcp::MCPClient::rpc_call_async ( const std::string &  method,
const json &  params = json::object() 
)

Async variant of rpc_call for the HTTP transport.

stdio sessions still drive their own blocking exchange (see 2.7 for the asio::posix migration); this awaitable resolves to the stdio result on the calling thread when the client is in stdio mode, so callers can use one async path uniformly.

Parameters
methodJSON-RPC method name.
paramsMethod parameters (defaults to empty object).
Returns
Awaitable resolving to the result field of the JSON-RPC response.

The documentation for this class was generated from the following file: