StdioService Class Reference

#include <Pt/Mcp/StdioService.h>

MCP service over standard streams with an event loop. More...

Public Member Functions

 StdioService (Remoting::ServiceDefinition &serviceDef, const ToolDeclaration &decl, System::EventLoop &loop)
 Creates a service for serviceDef, decl and loop.
 ~StdioService ()
 Destroys the service.
std::string readMessage (std::istream &is)
 Reads one Content-Length framed message from is.
void writeMessage (std::ostream &os, const std::string &json)
 Writes a Content-Length framed message json to os.
std::string dispatch (const std::string &json)
 Dispatches an MCP request and returns the response JSON.

Detailed Description

StdioService is the stdio transport that can run asynchronous tool procedures. Framing and dispatch are the same as Service: readMessage(), writeMessage(), and dispatch() on Content-Length framed JSON-RPC. The extra argument is an EventLoop. dispatch() starts the procedure and, when the procedure is asynchronous, runs that loop until the result is ready.

Use this type when a tool is an asynchronous remoting procedure. Use Service when every tool completes on the calling thread. The loop is not owned; it must outlive this service. The Pt::Remoting::ServiceDefinition and the ToolDeclaration are not owned either.

A typical server reads from stdin and writes to stdout until readMessage() returns empty. dispatch() returns an empty string for a notification, and that string is not written.

Constructor & Destructor Documentation

◆ StdioService()

StdioService ( Remoting::ServiceDefinition & serviceDef,
const ToolDeclaration & decl,
System::EventLoop & loop )

loop drives asynchronous service procedures.

Member Function Documentation

◆ readMessage()

std::string readMessage ( std::istream & is)

Returns the JSON body, or an empty string on EOF.

◆ dispatch()

std::string dispatch ( const std::string & json)

Returns an empty string for notifications (no id).