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

McpServer class definition; to be registered as a handler in the HttpServer class. More...

#include <McpServerHandler.qc.dox.h>

Inherits JsonRpcHandler, and ServerSentEventHandler.

Public Member Methods

void constructor (Logger::LoggerInterface logger, HttpServer::AbstractAuthenticator auth)
 Creates the handler.
 
void constructor (HttpServer::AbstractAuthenticator auth)
 Creates the handler.
 
void constructor ()
 Creates the handler.
 
void registerTool (soft< AbstractDataProvider > prov, soft< hash< auto > > extra, soft< bool > input_initialized, code tool, soft< AbstractDataProviderType > input, string name, string desc)
 Registers a tool.
 
bool unregisterTool (string name)
 Unregisters a tool.
 
list< string > getToolNames ()
 Returns a list of registered tool names.
 
bool hasTools (string name)
 Checks if a tool is registered.
 
void registerResource (soft< code > reader, string mimeType, string desc, string uri, string name)
 Registers a resource.
 
bool unregisterResource (string uri)
 Unregisters a resource.
 
void registerPrompt (soft< code > handler, soft< list< hash< auto > > > arguments, string name, string desc)
 Registers a prompt.
 
bool unregisterPrompt (string name)
 Unregisters a prompt.
 
string getNextRequestId ()
 Returns the next request ID and increments the internal counter.
 
void setSessionTimeout (int timeout_secs)
 Sets the session timeout in seconds.
 
int getSessionTimeout ()
 Returns the session timeout in seconds.
 
int getSessionCount ()
 Returns the number of active sessions.
 
list< hash< auto > > getSessionInfo ()
 Returns information about active sessions (for debugging/monitoring)
 
string getHeartbeatMessage (McpServerConnection conn)
 Use MCP ping as heartbeat message.
 
void registerNewConnectionIntern (string cid, ServerSentEventConnection conn)
 Called while holding the write lock to register the new connection.
 
hash< auto > completionComplete (hash< auto > cx, soft< hash< auto > > params)
 Handle completion/complete requests for autocompletion.
 
hash< auto > completionCompleteIntern (hash< auto > cx, soft< hash< auto > > params)
 Internal completion handler - can be overridden by subclasses.
 
soft< hash< auto > > loggingSetLevel (hash< auto > cx, soft< hash< auto > > params)
 Handle logging/setLevel requests.
 
string generateTaskId ()
 Generates a unique task ID.
 
hash< McpTaskInfocreateTask (soft< auto > requestId, soft< int > ttl, soft< hash< auto > > params, string sessionId, string method)
 Creates a new task for background execution.
 
void updateTaskStatus (soft< string > message, string status, string sessionId, string taskId)
 Updates a task's status.
 
void setTaskResult (soft< hash< auto > > error, soft< hash< auto > > result, string sessionId, string taskId)
 Sets the task result (on completion or error)
 
void sendTaskStatusNotification (string sessionId, string taskId)
 Sends a task status notification to the client.
 
soft< hash< McpTaskInfo > > getTaskInfo (string sessionId, string taskId)
 Gets a task's current information.
 
int cleanupExpiredTasks (string sessionId)
 Cleans up expired tasks for a session.
 
soft< hash< auto > > tasksGet (hash< auto > cx, soft< hash< auto > > params)
 Handle tasks/get request - get task status by ID.
 
soft< hash< auto > > tasksResult (hash< auto > cx, soft< hash< auto > > params)
 Handle tasks/result request - block until task completes and return result.
 
soft< hash< auto > > tasksCancel (hash< auto > cx, soft< hash< auto > > params)
 Handle tasks/cancel request - cancel a running task.
 
hash< auto > tasksList (hash< auto > cx, soft< hash< auto > > params)
 Handle tasks/list request - list all tasks for session.
 
hash< auto > getTaskResponse (hash< McpTaskInfo > task)
 Builds a task response hash.
 
soft< hash< auto > > resourcesSubscribe (hash< auto > cx, soft< hash< auto > > params)
 Handle resources/subscribe request.
 
soft< hash< auto > > resourcesUnsubscribe (hash< auto > cx, soft< hash< auto > > params)
 Handle resources/unsubscribe request.
 
soft< hash< McpResourceTemplateInfo > > matchResourceTemplate (string uri)
 Matches a URI against resource templates.
 
bool matchUriTemplate (string template, string uri)
 Simple URI template matching (RFC 6570 basic)
 
void registerResourceTemplate (soft< hash< auto > > annotations, soft< code > reader, soft< string > mimeType, soft< string > desc, string uriTemplate, string name)
 Registers a resource template.
 
bool unregisterResourceTemplate (string uriTemplate)
 Unregisters a resource template.
 
hash< auto > resourcesTemplatesList (hash< auto > cx, soft< hash< auto > > params)
 Handle resources/templates/list request.
 
void sendProgress (soft< string > message, soft< int > total, int progress, string sessionId, string progressToken)
 Sends a progress notification to the client.
 
soft< string > getProgressToken (soft< hash< auto > > params)
 Extracts the progress token from request params.
 
void cancelRequest (hash< auto > cx, hash< auto > params)
 Handles cancellation notification.
 
bool isRequestCancelled (string sessionId, auto requestId)
 Checks if a request has been cancelled.
 
void markCancellationProcessed (string sessionId, auto requestId)
 Marks a cancellation as processed.
 
hash< auto > sendClientRequestAndWait (int timeout_ms, hash< auto > params, string method, string sessionId, McpServerConnection conn)
 Sends a request to the client and waits for a response.
 
soft< hash< auto > > toolsCallIntern (hash< auto > cx, soft< hash< auto > > params)
 Modified toolsCallIntern to support task-based execution.
 
hash< auto > toolsCallAsTask (soft< auto > requestId, hash< auto > cx, soft< hash< auto > > params)
 Executes a tool call as a background task.
 
void executeToolInBackground (hash< auto > cx, hash< auto > params, hash< McpToolInfo > info, string sessionId, string taskId)
 Executes a tool in the background and sets the task result.
 

Public Attributes

const auto NoInputDescription = ...
 The input description for tools that do not require any input.
 
const auto SseHandlerOpts = ...
 Default SSE handler options.
 
const auto MaxMcpToolNameLength = ...
 Maximum name length for an MCP tool.
 
const auto DefaultSessionTimeoutSecs = ...
 Default session timeout in seconds (1 hour)
 

Private Attributes

hash< string, hash< McpToolInfo > > tmap
 Registered tools: name -> tool.
 
hash< string, hash< McpResourceInfo > > rmap
 Registered resources: URI -> resource info.
 
hash< string, hash< McpPromptInfo > > pmap
 Registered prompts: name -> prompt info.
 
hash< string, hash< McpResourceTemplateInfo > > rtmap
 Registered resource templates: uriTemplate -> template info.
 
hash< string, hash< McpSessionInfo > > smap
 Session map; session ID -> session info.
 
hash< string, int > sidmap
 Session ID map.
 

Detailed Description

McpServer class definition; to be registered as a handler in the HttpServer class.

Member Function Documentation

◆ cleanupExpiredTasks()

int McpServerHandler::McpServerHandler::cleanupExpiredTasks ( string  sessionId)

Cleans up expired tasks for a session.

Parameters
sessionIdthe session ID
Returns
the number of tasks cleaned up

◆ completionComplete()

hash< auto > McpServerHandler::McpServerHandler::completionComplete ( hash< auto >  cx,
soft< hash< auto > >  params 
)

Handle completion/complete requests for autocompletion.

Parameters
cxthe context hash
paramsthe completion parameters including:
  • ref: hash with type (ref/prompt or ref/resource) and name/uri
  • argument: hash with name and value for the argument being completed
Returns
hash with completion results including values array

◆ createTask()

hash< McpTaskInfo > McpServerHandler::McpServerHandler::createTask ( soft< auto >  requestId,
soft< int >  ttl,
soft< hash< auto > >  params,
string  sessionId,
string  method 
)

Creates a new task for background execution.

Parameters
sessionIdthe session ID
methodthe method that created this task
paramsthe original request parameters
ttloptional TTL in milliseconds (defaults to defaultTaskTtl)
Returns
the new task info

◆ getTaskInfo()

soft< hash< McpTaskInfo > > McpServerHandler::McpServerHandler::getTaskInfo ( string  sessionId,
string  taskId 
)

Gets a task's current information.

Parameters
sessionIdthe session ID
taskIdthe task ID
Returns
the task info or NOTHING if not found

◆ hasTools()

bool McpServerHandler::McpServerHandler::hasTools ( string  name)

Checks if a tool is registered.

Parameters
namethe name of the tool
Returns
True if the tool is registered

◆ isRequestCancelled()

bool McpServerHandler::McpServerHandler::isRequestCancelled ( string  sessionId,
auto  requestId 
)

Checks if a request has been cancelled.

Parameters
sessionIdthe session ID
requestIdthe request ID
Returns
True if the request has been cancelled

◆ markCancellationProcessed()

void McpServerHandler::McpServerHandler::markCancellationProcessed ( string  sessionId,
auto  requestId 
)

Marks a cancellation as processed.

Parameters
sessionIdthe session ID
requestIdthe request ID

◆ registerPrompt()

void McpServerHandler::McpServerHandler::registerPrompt ( soft< code >  handler,
soft< list< hash< auto > > >  arguments,
string  name,
string  desc 
)

Registers a prompt.

Parameters
namethe name of the prompt
descthe description of the prompt
argumentsoptional list of argument definitions for the prompt
handleroptional code to generate prompt messages; receives (context, arguments) and should return a list of message hashes with "role" and "content" keys

◆ registerResource()

void McpServerHandler::McpServerHandler::registerResource ( soft< code >  reader,
string  mimeType,
string  desc,
string  uri,
string  name 
)

Registers a resource.

Parameters
urithe URI of the resource
namethe name of the resource
descthe description of the resource
mimeTypethe MIME type of the resource
readeroptional code to read the resource content; if not provided, the resource is not readable

◆ registerResourceTemplate()

void McpServerHandler::McpServerHandler::registerResourceTemplate ( soft< hash< auto > >  annotations,
soft< code >  reader,
soft< string >  mimeType,
soft< string >  desc,
string  uriTemplate,
string  name 
)

Registers a resource template.

Parameters
uriTemplatethe URI template (RFC 6570)
namethe name of the template
descthe description of the template
mimeTypethe MIME type of resources generated by this template
readeroptional code to read the resource content
annotationsoptional annotations

◆ sendClientRequestAndWait()

hash< auto > McpServerHandler::McpServerHandler::sendClientRequestAndWait ( int  timeout_ms,
hash< auto >  params,
string  method,
string  sessionId,
McpServerConnection  conn 
)

Sends a request to the client and waits for a response.

Parameters
sessionIdthe session ID
connthe SSE connection to use
methodthe method name
paramsthe request parameters
timeout_msthe timeout in milliseconds
Returns
the response from the client
Exceptions
MCP-TIMEOUTif the request times out
MCP-CLIENT-ERRORif the client returns an error

◆ sendProgress()

void McpServerHandler::McpServerHandler::sendProgress ( soft< string >  message,
soft< int >  total,
int  progress,
string  sessionId,
string  progressToken 
)

Sends a progress notification to the client.

Parameters
sessionIdthe session ID
progressTokenthe progress token
progressthe current progress value
totaloptional total progress value
messageoptional progress message

◆ sendTaskStatusNotification()

void McpServerHandler::McpServerHandler::sendTaskStatusNotification ( string  sessionId,
string  taskId 
)

Sends a task status notification to the client.

Parameters
sessionIdthe session ID
taskIdthe task ID

◆ setSessionTimeout()

void McpServerHandler::McpServerHandler::setSessionTimeout ( int  timeout_secs)

Sets the session timeout in seconds.

Parameters
timeout_secsthe timeout in seconds; 0 disables session timeout

◆ setTaskResult()

void McpServerHandler::McpServerHandler::setTaskResult ( soft< hash< auto > >  error,
soft< hash< auto > >  result,
string  sessionId,
string  taskId 
)

Sets the task result (on completion or error)

Parameters
sessionIdthe session ID
taskIdthe task ID
resultoptional result (for successful completion)
erroroptional error (for failure)

◆ unregisterPrompt()

bool McpServerHandler::McpServerHandler::unregisterPrompt ( string  name)

Unregisters a prompt.

Parameters
namethe name of the prompt to unregister
Returns
True if the prompt was unregistered, False if it was not found

◆ unregisterResource()

bool McpServerHandler::McpServerHandler::unregisterResource ( string  uri)

Unregisters a resource.

Parameters
urithe URI of the resource to unregister
Returns
True if the resource was unregistered, False if it was not found

◆ unregisterResourceTemplate()

bool McpServerHandler::McpServerHandler::unregisterResourceTemplate ( string  uriTemplate)

Unregisters a resource template.

Parameters
uriTemplatethe URI template to unregister
Returns
True if the template was unregistered, False if it was not found

◆ unregisterTool()

bool McpServerHandler::McpServerHandler::unregisterTool ( string  name)

Unregisters a tool.

Parameters
namethe name of the tool to unregister
Returns
True if the tool was unregistered, False if it was not found

◆ updateTaskStatus()

void McpServerHandler::McpServerHandler::updateTaskStatus ( soft< string >  message,
string  status,
string  sessionId,
string  taskId 
)

Updates a task's status.

Parameters
sessionIdthe session ID
taskIdthe task ID
statusthe new status
messageoptional status message

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