Module: McpToolkit::Protocol

Defined in:
lib/mcp_toolkit/protocol.rb

Overview

MCP JSON-RPC protocol constants + helpers for the hand-rolled AUTHORITY dispatcher (McpToolkit::Dispatcher). Based on the Model Context Protocol specification.

This is the wire vocabulary of a first-party MCP endpoint that serves its own tools (and, as a gateway, aggregates upstream ones) WITHOUT the official mcp SDK in the request path. The SDK-backed satellite path (McpToolkit::Server.build) does not use this module; the two dispatch front-ends coexist by design.

The error codes, the success/error response envelopes, and the version-negotiation constants here are the BYTE contract a monetized authority endpoint depends on, so they are kept fixed. SUPPORTED_VERSIONS is the module default; a host negotiates against config.supported_protocol_versions (defaulting to the same list) so the set is overridable without editing this file.

Defined Under Namespace

Modules: ErrorCodes Classes: Error, InternalError, InvalidParams, InvalidRequest, MethodNotFound, ParseError

Constant Summary collapse

SUPPORTED_VERSIONS =

Protocol versions the server supports, newest first. The version returned in the initialize response is the requested version (if supported) or the latest the server supports, per the MCP spec's version-negotiation rules.

%w[2025-06-18 2025-03-26 2024-11-05].freeze
LATEST_VERSION =
SUPPORTED_VERSIONS.first
VERSION =

Kept for backwards compatibility; prefer LATEST_VERSION going forward.

LATEST_VERSION
JSONRPC_VERSION =
"2.0"

Class Method Summary collapse

Class Method Details

.error_response(id:, error:) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/mcp_toolkit/protocol.rb', line 99

def error_response(id:, error:)
  {
    jsonrpc: JSONRPC_VERSION,
    id:,
    error: error.is_a?(Error) ? error.to_h : error
  }
end

.success_response(id:, result:) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/mcp_toolkit/protocol.rb', line 91

def success_response(id:, result:)
  {
    jsonrpc: JSONRPC_VERSION,
    id:,
    result:
  }
end