Model Context Protocol

MCP servers that expose C++ procedures as tools. More...

Topics

 Parameter Schemas
 JSON Schema descriptors for MCP tool parameters.
 Tools and Server Declaration
 Declare an MCP server and its tools.
 Result Content
 Format a tool result as an MCP content block.
 HTTP Transport
 Serve MCP tools over HTTP.
 Standard I/O Transport
 Serve MCP tools over standard streams.

Detailed Description

This module is an MCP server, so a caller registers ordinary C++ procedures and serves them as tools that a client can list and call. It is not an MCP client. The unit of work is a tool: a named procedure, a JSON Schema for its parameters, and a content type for its result. JSON-RPC is the envelope. Pt::Remoting supplies the procedure table. This module adds the MCP methods, the tool schemas, and the transports.

Work proceeds in four steps. A Pt::Remoting::ServiceDefinition holds the C++ functions or methods. A ToolDeclaration holds the matching tool names, descriptions, and parameter schemas. Each tool may name a ContentType for its result; the default is text. A transport then serves that pair: HttpService on an HTTP server, or Service / StdioService on standard streams.

The tool name in the declaration must be the procedure name in the definition. Arguments and results must be serializable, as in the other remoting modules: a type that can be written to SerializationInfo can be a tool parameter or a return value. Custom types are welcome when they have that support.

The declaration and the definition are not owned by the transport. Both must outlive the HttpService, Service, or StdioService that uses them. A Type named by a tool parameter must outlive that tool. Primitive schema types from integerType() and the other factories are process-wide singletons. Composed types (ObjectType, ArrayType, EnumType, NullableType) are owned by the caller. ToolDeclaration owns the Tool objects it creates. A ContentType used with Tool::setContent() must outlive the tool; textContent() and imageContent() are process-wide singletons.

The protocol methods are initialize, tools/list, and tools/call. Initialize negotiates a protocol version and returns the server name and version from the declaration. Tools/list returns the declared tools and their input schemas. Tools/call looks up the tool, maps named arguments onto the procedure, and writes the result as MCP content. A request without an id is a notification and produces no JSON-RPC body.

Supported protocol versions are 2025-11-25 and 2025-03-26. ToolDeclaration::preferredVersion() returns the requested version when it is supported, otherwise the latest. A tool that throws Pt::JsonRpc::Fault or Pt::Remoting::Fault becomes an MCP error response. Other failures are reported as JSON-RPC errors by the transport.

The example registers two procedures, declares the matching tools with schemas, and serves them over HTTP. The stdio path is the same declaration and definition with StdioService.

class DemoDefinition : public Pt::Remoting::ServiceDefinition
{
public:
DemoDefinition()
{
registerProcedure("add", *this, &DemoDefinition::add);
registerProcedure("echo", *this, &DemoDefinition::echo);
}
private:
int add(int a, int b)
{ return a + b; }
std::string echo(const std::string& msg)
{ return msg; }
};
DemoDefinition defn;
Pt::Mcp::ToolDeclaration decl("pt-mcp-demo", "1.0.0");
decl.addTool("add", "Add two integers")
.addParam("a", Pt::Mcp::integerType(), "First operand")
.addParam("b", Pt::Mcp::integerType(), "Second operand");
decl.addTool("echo", "Echo a string")
.addParam("message", Pt::Mcp::stringType(), "The message");
Pt::Mcp::HttpService mcpService(defn, decl);
Pt::Http::MapUrl servlet("/mcp", mcpService);
server.addServlet(servlet);
loop.run();
Maps requests to a service by URL.
Definition Servlet.h:147
Listening HTTP server.
Definition Server.h:83
HTTP service that serves declared MCP tools.
Definition HttpService.h:40
MCP server name, version and tool catalog.
Definition ToolDeclaration.h:126
static Endpoint ip4Any(unsigned short port)
Creates the IPv4 any-address for port.
Remote service definition.
Definition ServiceDefinition.h:59
void run()
Starts the loop.
Platform event loop.
Definition MainLoop.h:51
PT_MCP_API const Type & stringType()
Returns the process-wide JSON string schema type.
PT_MCP_API const Type & integerType()
Returns the process-wide JSON integer schema type.

The rest of this chapter is parameter schemas, then tool declarations, then result content, then the HTTP transport, then standard I/O.