Service.h
1 /*
2  * Copyright (C) 2020-2026 by Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27  * MA 02110-1301 USA
28  */
29 
30 #ifndef PT_MCP_SERVICE_H
31 #define PT_MCP_SERVICE_H
32 
33 #include <Pt/Mcp/Api.h>
34 #include <Pt/Mcp/ToolDeclaration.h>
35 #include <Pt/Remoting/ServiceDefinition.h>
36 #include <string>
37 #include <iosfwd>
38 
39 namespace Pt {
40 
41 namespace Mcp {
42 
48 class PT_MCP_API Service
49 {
50  public:
54  const ToolDeclaration& decl);
55 
59 
64  std::string readMessage(std::istream& is);
65 
68  void writeMessage(std::ostream& os, const std::string& json);
69 
75  std::string dispatch(const std::string& json);
76 
77  private:
78  std::string dispatchToolsCall(const std::string& json);
79  std::string dispatchInitialize(const std::string& json);
80  std::string dispatchToolsList(const std::string& json);
81 
82  static std::string extractMethod(const std::string& json);
83  static Pt::int64_t extractId(const std::string& json);
84 
85  Remoting::ServiceDefinition* _serviceDef;
86  const ToolDeclaration* _decl;
87 };
88 
89 } // namespace Mcp
90 
91 } // namespace Pt
92 
93 #endif // PT_MCP_SERVICE_H
Core module.
Definition: Allocator.h:33
~Service()
Destructor.
MCP server and tool registry.
Definition: ToolDeclaration.h:129
Service(Remoting::ServiceDefinition &serviceDef, const ToolDeclaration &decl)
Construct with a service definition and tool declaration.
Remote service definition.
Definition: Api-ServiceDefinition.h:22
int_type int64_t
Signed 64-bit integer type.
Definition: Api-Types.h:55
MCP protocol service over stdio.
Definition: Service.h:49
std::string dispatch(const std::string &json)
Dispatch an MCP request and return the response JSON.
void writeMessage(std::ostream &os, const std::string &json)
Write a Content-Length framed message to the stream.
std::string readMessage(std::istream &is)
Read a Content-Length framed message from the stream.