|
Sonde v1.1.0
Embeddable Model Context Protocol server for Qt applications
|
An embeddable Model Context Protocol server. More...
#include <server.h>
Public Member Functions | |
| Server (QObject *parent=nullptr) | |
| Creates a server that is not listening yet, with the threading the environment asks for: WorkerThread if SONDE_THREADED is 1, else ServerThread. | |
| Server (ServerThreading threading, QObject *parent=nullptr) | |
Creates a server that is not listening yet, using threading whatever SONDE_THREADED says. | |
| ~Server () override | |
| Closes all listeners and ends all sessions (without emitting signals). | |
| void | setServerInfo (const QString &name, const QString &version) |
| Sets the serverInfo reported to clients (defaults to the application name/version). | |
| QString | serverName () const |
| The serverInfo name. | |
| QString | serverVersion () const |
| The serverInfo version. | |
| void | setInstructions (const QString &instructions) |
| Sets the instructions text returned by initialize, which tells the model how to use this server. | |
| void | appendInstructions (const QString ¶graph) |
| Appends a paragraph to the instructions unless it is already present. | |
| QString | instructions () const |
| The current instructions text. | |
| bool | listenHttp (const QHostAddress &address=QHostAddress::LocalHost, quint16 port=0, const QString &path=QStringLiteral("/mcp")) |
| Starts (or restarts) the Streamable HTTP transport. | |
| bool | isListeningHttp () const |
| Whether the HTTP transport is listening. | |
| quint16 | httpPort () const |
| The HTTP port, or 0 if not listening. | |
| QUrl | httpUrl () const |
| The endpoint URL clients should use, or an empty URL. | |
| void | setHttpAuthToken (const QString &token) |
| Requires "Authorization: Bearer <token>" on HTTP requests (an empty token disables the check). | |
| bool | listenLocal (const QString &name) |
| Starts (or restarts) the local socket transport. | |
| bool | isListeningLocal () const |
| Whether the local socket transport is listening. | |
| QString | localServerName () const |
| The full socket path or pipe name, or an empty string. | |
| bool | listenFromEnvironment () |
| Starts the listeners configured by environment variables. | |
| void | close () |
| Stops all listeners and ends all sessions. | |
| void | addTool (const ToolDefinition &definition, ToolHandler handler) |
| Registers (or replaces) a synchronous tool. | |
| void | addAsyncTool (const ToolDefinition &definition, AsyncToolHandler handler) |
| Registers (or replaces) an asynchronous tool. | |
| bool | removeTool (const QString &name) |
| Unregisters a tool. | |
| bool | hasTool (const QString &name) const |
| Whether a tool with this name is registered. | |
| QStringList | toolNames () const |
| Names of all registered tools, sorted. | |
| void | addResource (const ResourceDefinition &definition, ResourceReader reader) |
| Registers (or replaces) a resource. | |
| bool | removeResource (const QString &uri) |
| Unregisters a resource. | |
| void | notifyResourceUpdated (const QString &uri) |
Sends notifications/resources/updated to clients subscribed to uri. | |
| void | addEventSource (EventSource *source) |
| Adds a source of events. | |
| QList< EventSource * > | eventSources () const |
| The live event sources. | |
| void | setEventBufferCapacity (int capacity) |
| Sets how many events are kept (default 5000, minimum 1). | |
| int | eventBufferCapacity () const |
| How many events are kept. | |
| void | sendLogMessage (LogLevel level, const QString &logger, const QJsonValue &data) |
| Sends a log notification to every client whose level permits it, without recording it as an event. | |
| int | sessionCount () const |
| Number of connected sessions (initialized or not). | |
| void | setSessionApproval (SessionApproval approval) |
Asks approval, on the server's thread, before each new client is let in (design 0033). | |
| QList< SessionInfo > | sessions () const |
| The connected clients that were let in, oldest first. | |
| bool | endSession (const QString &id) |
Ends session id as if its client had disconnected: its calls are cancelled, and its connection closed. | |
| ServerThreading | threading () const |
| The threads the server uses (chosen when it was created). | |
| void | setStackSampling (StackSampling sampling) |
| Sets whether stall events carry samples of the blocked thread's stack (design 0016). | |
| StackSampling | stackSampling () const |
| Whether stall events carry stack samples (see setStackSampling()). | |
| void | setLimits (const ServerLimits &limits) |
| Sets the resource limits. Applies to new connections, sessions and calls. | |
| ServerLimits | limits () const |
| The current resource limits. | |
| void | sessionStarted (const QString &sessionId, const QJsonObject &clientInfo) |
| A client completed initialize. | |
| void | sessionEnded (const QString &sessionId) |
| An initialized session ended (disconnect, DELETE, expiry or close()). | |
Private Attributes | |
| PrivatePtr< ServerPrivate > | d |
| Implementation. | |
Friends | |
| class | ServerPrivate |
An embeddable Model Context Protocol server.
Transports:
All tool and resource handlers run on the thread that owns the Server (normally the GUI thread). The Server's API is not thread-safe, except for EventSource::emitEvent().
|
explicit |
Creates a server that is not listening yet, using threading whatever SONDE_THREADED says.
In WorkerThread mode tool handlers, resource readers and cancel callbacks still run on the server's thread, so tools need no changes; see notes/design/0015-threaded-transport.md.
| void Sonde::Server::addEventSource | ( | EventSource * | source | ) |
Adds a source of events.
The server takes ownership if the source has no parent. The event_read/event_wait tools and an events://<name> resource are registered for it.
| void Sonde::Server::appendInstructions | ( | const QString & | paragraph | ) |
Appends a paragraph to the instructions unless it is already present.
Tool sets use this to describe themselves.
| bool Sonde::Server::endSession | ( | const QString & | id | ) |
Ends session id as if its client had disconnected: its calls are cancelled, and its connection closed.
sessionEnded follows.
| bool Sonde::Server::listenFromEnvironment | ( | ) |
Starts the listeners configured by environment variables.
For development setups with several checkouts, {instance} in SONDE_LOCAL is replaced by instanceKey(), e.g. myapp-mcp-{instance}, and in SONDE_HTTP, {instance} or {instance:<low>-<high>} by a port from that range (default 28600-29599) chosen by the key, or the next free one after it if that one is taken. A release build shouldn't call this function at all (see release.md).
| bool Sonde::Server::listenHttp | ( | const QHostAddress & | address = QHostAddress::LocalHost, |
| quint16 | port = 0, | ||
| const QString & | path = QStringLiteral("/mcp") ) |
Starts (or restarts) the Streamable HTTP transport.
If the server is already listening, the current listener is only replaced once the new one is up, so a failed call changes nothing. (Restarting on the same port releases it first.)
| address | Address to bind; localhost by default. |
| port | Port to bind; 0 picks a free port (see httpPort()). |
| path | Endpoint path. |
| bool Sonde::Server::listenLocal | ( | const QString & | name | ) |
Starts (or restarts) the local socket transport.
Socket access is restricted to the current user. A stale socket file left by a crashed process is replaced. As with listenHttp(), a failed call leaves the current listener running (unless it used the same name).
| bool Sonde::Server::removeResource | ( | const QString & | uri | ) |
Unregisters a resource.
| bool Sonde::Server::removeTool | ( | const QString & | name | ) |
Unregisters a tool.
| void Sonde::Server::setHttpAuthToken | ( | const QString & | token | ) |
Requires "Authorization: Bearer <token>" on HTTP requests (an empty token disables the check).
May be called before or after listenHttp().
| void Sonde::Server::setSessionApproval | ( | SessionApproval | approval | ) |
Asks approval, on the server's thread, before each new client is let in (design 0033).
It is called when a client sends its first initialize. Until it calls SessionRequest::approve() or deny(), the client gets only ping answered; with neither within limits().approvalTimeout, the client is refused. Without an approval (the default, or an empty one), every client is let in at once. Applies to clients that start afterwards: set it before listening.
| void Sonde::Server::setStackSampling | ( | StackSampling | sampling | ) |
Sets whether stall events carry samples of the blocked thread's stack (design 0016).
The default, StackSampling::DuringStalls, samples only a thread that a StallEventSource of this server watches, only while it is blocked, and only while the server listens. Choose StackSampling::Off if the application can't have that: on Linux a sample is taken by a signal, which makes a blocking call such as poll() return early with EINTR, and needs a signal the application doesn't use (SIGRTMIN + 3, or SONDE_STACK_SAMPLES_SIGNAL). The user turns it off with SONDE_STACK_SAMPLES=off. Off also refuses app_stall_arm.