Client for connecting to MCP (Model Context Protocol) servers.
More...
#include <client.h>
|
| 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 ¶ms=json::object()) |
| | Async variant of rpc_call for the HTTP transport.
|
| |
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.
MCPClient http_client(
"http://localhost:8000");
http_client.initialize("my-agent");
auto tools = http_client.get_tools();
MCPClient stdio_client({
"python",
"server.py"});
auto tools2 = stdio_client.get_tools();
Client for connecting to MCP (Model Context Protocol) servers.
bool initialize(const std::string &client_name="neograph")
Initialize the connection and perform the MCP handshake.
Definition at line 97 of file client.h.
◆ MCPClient() [1/2]
| neograph::mcp::MCPClient::MCPClient |
( |
const std::string & |
server_url | ) |
|
|
explicit |
Construct an HTTP-mode MCP client.
- Parameters
-
| server_url | URL 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
-
| argv | Command + 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().
◆ call_tool()
| json neograph::mcp::MCPClient::call_tool |
( |
const std::string & |
name, |
|
|
const json & |
arguments |
|
) |
| |
Call a tool directly by name.
- Parameters
-
| name | Tool name on the server. |
| arguments | JSON 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_name | Client 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
-
| method | JSON-RPC method name. |
| params | Method 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: