StdioService.h
1/* Copyright (C) 2020-2026 by Marc Boris Duerner
2 SPDX-License-Identifier: LGPL-2.1-or-later WITH mif-exception
3*/
4
5#ifndef PT_MCP_STDIOSERVICE_H
6#define PT_MCP_STDIOSERVICE_H
7
8#include <Pt/Mcp/Api.h>
9#include <Pt/Mcp/ToolDeclaration.h>
10#include <Pt/Remoting/ServiceDefinition.h>
11#include <Pt/System/Api.h>
12#include <string>
13#include <iosfwd>
14
15namespace Pt {
16
17namespace Mcp {
18
40class PT_MCP_API StdioService
41{
42 public:
48 const ToolDeclaration& decl,
49 System::EventLoop& loop);
50
54
59 std::string readMessage(std::istream& is);
60
63 void writeMessage(std::ostream& os, const std::string& json);
64
69 std::string dispatch(const std::string& json);
70
71 private:
72 std::string dispatchToolsCall(const std::string& json);
73 std::string dispatchInitialize(const std::string& json);
74 std::string dispatchToolsList(const std::string& json);
75
76 static std::string extractMethod(const std::string& json);
77 static Pt::int64_t extractId(const std::string& json);
78
79 Remoting::ServiceDefinition* _serviceDef;
80 const ToolDeclaration* _decl;
81 System::EventLoop& _loop;
82};
83
84} // namespace Mcp
85
86} // namespace Pt
87
88#endif // PT_MCP_STDIOSERVICE_H
~StdioService()
Destroys the service.
void writeMessage(std::ostream &os, const std::string &json)
Writes a Content-Length framed message json to os.
std::string readMessage(std::istream &is)
Reads one Content-Length framed message from is.
std::string dispatch(const std::string &json)
Dispatches an MCP request and returns the response JSON.
StdioService(Remoting::ServiceDefinition &serviceDef, const ToolDeclaration &decl, System::EventLoop &loop)
Creates a service for serviceDef, decl and loop.
MCP server name, version and tool catalog.
Definition ToolDeclaration.h:126
Remote service definition.
Definition ServiceDefinition.h:59
Event loop of a thread or process.
Definition EventLoop.h:83
int_type int64_t
Signed 64-bit integer type.
Definition Api-Types.h:59
Model Context Protocol (MCP) services.
Core module.
Definition Allocator.h:33