Qore json Module 1.11.0
Loading...
Searching...
No Matches
McpClient::McpClient Class Reference

MCP Client class for connecting to MCP servers. More...

#include <McpClient.qc.dox.h>

Public Member Methods

void destructor ()
 Destructor - closes the connection.
 
string getUrl ()
 Returns the server URL.
 
bool isInitialized ()
 Checks if the client is initialized.
 
soft< string > getProtocolVersion ()
 Gets the negotiated protocol version.
 
soft< hash< auto > > getServerInfo ()
 Gets server information.
 
soft< hash< auto > > getServerCapabilities ()
 Gets server capabilities.
 
bool ping ()
 Sends a ping request to check server health.
 
list< auto > listTools ()
 Lists available tools from the server.
 
list< auto > listResources ()
 Lists available resources from the server.
 
hash< auto > readResource (string uri)
 Reads a resource from the server.
 
list< auto > listPrompts ()
 Lists available prompts from the server.
 
hash< auto > getPrompt (string name, soft< hash< auto > > arguments)
 Gets a prompt from the server.
 
hash< auto > complete (hash< auto > ref, hash< auto > argument)
 Requests completion suggestions.
 
void setLogLevel (string level)
 Sets the server log level.
 
void setCallback (string event, code callback)
 Registers an event callback.
 
void removeCallback (string event)
 Removes an event callback.
 
void startSseListener ()
 Starts the SSE listener for server notifications.
 
void stopSseListener ()
 Stops the SSE listener.
 
bool isSseListenerRunning ()
 Checks if SSE listener is running.
 
void startLegacySseListener ()
 Starts the legacy SSE listener.
 
void stopLegacySseListener ()
 Stops the legacy SSE listener.
 
bool startStreamableListener ()
 Starts the streamable HTTP GET listener.
 
void stopStreamableListener ()
 Stops the streamable HTTP GET listener.
 
soft< HttpStreamClient::HTTPResponseStream > openStreamableGet ()
 Opens a streamable HTTP GET event stream.
 
void streamableEventLoop ()
 Streamable GET event loop.
 
void close ()
 Closes the MCP connection.
 
soft< string > getSessionId ()
 Gets the session ID.
 
void setSessionId (string sid)
 Sets the session ID (used internally)
 
void ensureInitialized ()
 Internal: Ensures the client is initialized.
 
int getNextRequestId ()
 Internal: Gets the next request ID.
 
hash< auto > sendRequest (string method, soft< hash< auto > > params)
 Internal: Sends a JSON-RPC request with streamable HTTP, falling back to legacy.
 
hash< auto > sendRequestLegacy (string method, soft< hash< auto > > params)
 Internal: Sends a JSON-RPC request using legacy HTTP transport.
 
hash< auto > sendRequestStreamable (string method, soft< hash< auto > > params)
 Internal: Sends a JSON-RPC request using streamable HTTP.
 
hash< auto > readStreamableJsonResponse (HttpStreamClient::HTTPResponseStream stream)
 Internal: Reads a streamable JSON response.
 
hash< auto > readStreamableSseResponse (HttpStreamClient::HTTPResponseStream stream, int request_id)
 Internal: Reads a streamable SSE response and returns the matching response.
 
soft< hash< auto > > handleStreamableMessage (auto msg, int request_id)
 Internal: Handles parsed streamable message payloads.
 
string readStreamableBody (HttpStreamClient::HTTPResponseStream stream)
 Internal: Reads remaining stream body into a string.
 
void sendNotification (string method, soft< hash< auto > > params)
 Internal: Sends a JSON-RPC notification (no response expected)
 
void invokeCallback (string event, soft< list< auto > > args)
 Internal: Invokes an event callback.
 
void handleSseConnected ()
 Internal: Handles SSE connection events.
 
void handleSseClosed ()
 Internal: Handles SSE disconnection events.
 
void update (string event_id, hash< auto > data_)
 Observer callback for SSE client events.
 
void handleNotification (hash< auto > message)
 Internal: Handles a server notification.
 
void logDebug (string fmt)
 Internal: Logs a debug message.
 

Public Attributes

const auto DefaultConnectTimeout = ...
 Default connection timeout (30 seconds)
 
const auto DefaultTimeout = ...
 Default request timeout (60 seconds)
 
const auto JsonRpcVersion = ...
 JSON-RPC version.
 

Private Attributes

string url
 The server URL.
 
HTTPClient http
 HTTP client for requests.
 
HTTPClient stream_http
 HTTP client for streamable GET listener.
 
soft< string > session_id
 MCP session ID.
 
soft< string > protocol_version
 Negotiated protocol version.
 
soft< hash< auto > > server_info
 Server info after initialization.
 
soft< hash< auto > > server_capabilities
 Server capabilities.
 
soft< LoggerInterface > logger
 Logger interface.
 
hash< string, code > callbacks
 Event callbacks.
 
soft< ServerSentEventClient::ServerSentEventClient > sse_client
 SSE client for receiving server messages.
 
ThreadPool stream_tp
 Streamable GET listener thread pool.
 
soft< HttpStreamClient::HTTPResponseStream > streamable_stream
 Streamable GET response stream.
 
hash< string, hash< auto > > pending_requests
 Pending requests waiting for responses.
 

Detailed Description

MCP Client class for connecting to MCP servers.

This class provides a client implementation for the Model Context Protocol (MCP). It allows connecting to MCP servers over HTTP with JSON-RPC 2.0 protocol.

Example:
McpClient client("http://localhost:8080/mcp");
# Initialize connection
hash<auto> info = client.initialize({
"name": "MyApp",
"version": "1.0",
});
# Call a tool
hash<auto> result = client.callTool("echo", {"message": "Hello"});
client.close();
the McpClient namespace holds all public definitions in the McpClient module
Definition McpClient.qc.dox.h:2

Member Function Documentation

◆ complete()

hash< auto > McpClient::McpClient::complete ( hash< auto >  ref,
hash< auto >  argument 
)

Requests completion suggestions.

Parameters
refthe reference (prompt or resource) to complete for
argumentthe argument being completed
Returns
completion suggestions
Exceptions
MCP-ERRORif the request fails

◆ getPrompt()

hash< auto > McpClient::McpClient::getPrompt ( string  name,
soft< hash< auto > >  arguments 
)

Gets a prompt from the server.

Parameters
namethe name of the prompt
argumentsoptional arguments for the prompt
Returns
the prompt with messages
Exceptions
MCP-ERRORif the request fails

◆ getProtocolVersion()

soft< string > McpClient::McpClient::getProtocolVersion ( )

Gets the negotiated protocol version.

Returns
the protocol version string or NOTHING if not initialized

◆ getServerCapabilities()

soft< hash< auto > > McpClient::McpClient::getServerCapabilities ( )

Gets server capabilities.

Returns
server capabilities hash or NOTHING if not initialized

◆ getServerInfo()

soft< hash< auto > > McpClient::McpClient::getServerInfo ( )

Gets server information.

Returns
server info hash or NOTHING if not initialized

◆ getSessionId()

soft< string > McpClient::McpClient::getSessionId ( )

Gets the session ID.

Returns
the MCP session ID or NOTHING if not set

◆ isInitialized()

bool McpClient::McpClient::isInitialized ( )

Checks if the client is initialized.

Returns
True if initialized, False otherwise

◆ isSseListenerRunning()

bool McpClient::McpClient::isSseListenerRunning ( )

Checks if SSE listener is running.

Returns
True if SSE listener is active

◆ listPrompts()

list< auto > McpClient::McpClient::listPrompts ( )

Lists available prompts from the server.

Returns
list of prompt information
Exceptions
MCP-ERRORif the request fails

◆ listResources()

list< auto > McpClient::McpClient::listResources ( )

Lists available resources from the server.

Returns
list of resource information
Exceptions
MCP-ERRORif the request fails

◆ listTools()

list< auto > McpClient::McpClient::listTools ( )

Lists available tools from the server.

Returns
list of tool information
Exceptions
MCP-ERRORif the request fails

◆ ping()

bool McpClient::McpClient::ping ( )

Sends a ping request to check server health.

Returns
True if server responds successfully
Exceptions
MCP-PING-ERRORif ping fails

◆ readResource()

hash< auto > McpClient::McpClient::readResource ( string  uri)

Reads a resource from the server.

Parameters
urithe URI of the resource to read
Returns
the resource content
Exceptions
MCP-ERRORif the request fails

◆ removeCallback()

void McpClient::McpClient::removeCallback ( string  event)

Removes an event callback.

Parameters
eventthe event name to remove

◆ setCallback()

void McpClient::McpClient::setCallback ( string  event,
code  callback 
)

Registers an event callback.

Parameters
eventthe event name (onToolsChanged, onResourcesChanged, onPromptsChanged, onLogMessage, onError)
callbackthe callback code to invoke

Supported events:

  • onToolsChanged: called when the server's tool list changes
  • onResourcesChanged: called when the server's resource list changes
  • onPromptsChanged: called when the server's prompt list changes
  • onLogMessage: called when the server sends a log message (receives: string level, *auto data, *string logger)
  • onError: called when an error occurs (receives: string error_message)
  • onConnected: called when SSE connection is established
  • onDisconnected: called when SSE connection is closed

◆ setLogLevel()

void McpClient::McpClient::setLogLevel ( string  level)

Sets the server log level.

Parameters
levelthe log level to set
Exceptions
MCP-ERRORif the request fails
MCP-INVALID-LOG-LEVELif the level is not valid

◆ startSseListener()

void McpClient::McpClient::startSseListener ( )

Starts the SSE listener for server notifications.

This starts a background thread that listens for Server-Sent Events from the MCP server. Call this after initialize() to receive notifications.

Exceptions
MCP-NOT-INITIALIZEDif client is not initialized

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