clamo 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +8 -0
- data/CHANGELOG.md +26 -0
- data/PRD.md +345 -0
- data/README.md +4 -2
- data/lib/clamo/jsonrpc.rb +6 -6
- data/lib/clamo/server.rb +101 -41
- data/lib/clamo/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 04f79d47039bcdc103787dd8b7e8317b7bbe0d2b0577062bb48e1adae7027df3
|
|
4
|
+
data.tar.gz: ca152903c69a49d623f78ccdcf39c9b210de3a5556f7fa5b2179d63cb3e8974a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ad6fbabb4e03a8ba5c01c7b79a8ab61321a07c893811bfd467bfabf0967119b685e28507098e2665f5728bc96cc5f3fe378439c02fb5e533599d09e6aa5431c2
|
|
7
|
+
data.tar.gz: b20dc6db24cf80b6554db5a6e29d99046e8f7b1ce712d011f44c4ae3f1230ac92cd8169fece8643f484292811c973a283de60f7de78e7fa22401bdeb9faaeb74
|
data/.rubocop.yml
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
AllCops:
|
|
2
2
|
NewCops: enable
|
|
3
3
|
TargetRubyVersion: 3.3
|
|
4
|
+
SuggestExtensions: false
|
|
5
|
+
|
|
6
|
+
plugins:
|
|
7
|
+
- rubocop-minitest
|
|
4
8
|
|
|
5
9
|
Metrics/MethodLength:
|
|
6
10
|
Enabled: false
|
|
@@ -23,5 +27,9 @@ Style/MultilineBlockChain:
|
|
|
23
27
|
Style/StringLiterals:
|
|
24
28
|
EnforcedStyle: double_quotes
|
|
25
29
|
|
|
30
|
+
Naming/PredicateMethod:
|
|
31
|
+
Exclude:
|
|
32
|
+
- "test/**/*"
|
|
33
|
+
|
|
26
34
|
Style/StringLiteralsInInterpolation:
|
|
27
35
|
EnforcedStyle: double_quotes
|
data/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,32 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|
6
6
|
|
|
7
|
+
## [1.0.1] - 2026-11-30
|
|
8
|
+
|
|
9
|
+
### Changed
|
|
10
|
+
|
|
11
|
+
- Batch parallelism is now opt-in. A batch runs sequentially, in order, in the calling process and thread unless `Parallel.map` options (e.g. `in_threads: 4`) are passed to `dispatch`, `dispatch_json` or `handle_json`. Previously any batch of two or more requests went through `Parallel.map` whenever the `parallel` gem was installed, and its default is to fork one process per core: in-memory state changes made by service methods and `on_error` calls were lost to the parent process, and results had to be Marshal-able. To keep the old behavior, pass `in_processes: Parallel.processor_count`.
|
|
12
|
+
|
|
13
|
+
### Fixed
|
|
14
|
+
|
|
15
|
+
- `handle_json`: a result that cannot be encoded as JSON (`NaN`, `Infinity`, circular structures) now returns `-32603 Internal error` for that request alone and is reported through `on_error`, instead of raising or failing the whole batch.
|
|
16
|
+
- Fractional request ids (e.g. `1.5`) are accepted, as the JSON-RPC 2.0 specification allows any Number.
|
|
17
|
+
- An invalid request echoes its `id` only when the id is itself a valid type; otherwise the response carries `"id": null`.
|
|
18
|
+
- A notification whose `params` is not an Array or Object (e.g. `"params": null`) no longer executes the method.
|
|
19
|
+
- Arity validation: a splat parameter no longer skips the check for required positional arguments.
|
|
20
|
+
- A private or protected singleton method that shadows a public method of the object's class now returns `-32601 Method not found` instead of `-32000 Server error`.
|
|
21
|
+
- A `LoadError` raised by a service method during a batch now propagates instead of replaying the whole batch sequentially.
|
|
22
|
+
|
|
23
|
+
## [1.0.0] - 2026-03-15
|
|
24
|
+
|
|
25
|
+
### Added
|
|
26
|
+
|
|
27
|
+
- RDoc comments on all public methods and modules.
|
|
28
|
+
|
|
29
|
+
### Changed
|
|
30
|
+
|
|
31
|
+
- README restructured (table of contents, installation, roadmap). No functional changes since 0.12.0.
|
|
32
|
+
|
|
7
33
|
## [0.12.0] - 2026-03-14
|
|
8
34
|
|
|
9
35
|
### Changed
|
data/PRD.md
ADDED
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
# Clamo — Product Requirements Document
|
|
2
|
+
|
|
3
|
+
> Future requirements for Clamo, the minimal JSON-RPC 2.0 server library for Ruby.
|
|
4
|
+
>
|
|
5
|
+
> Current state: v1.0.0 — spec-compliant dispatch, arity validation, batch processing,
|
|
6
|
+
> optional parallelism, per-call configuration, zero runtime dependencies (~350 LOC).
|
|
7
|
+
>
|
|
8
|
+
> **16 requirements** across 5 tiers — 0 completed, 16 pending.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Table of Contents
|
|
13
|
+
|
|
14
|
+
- [Tier 1 — Foundation](#tier-1--foundation)
|
|
15
|
+
- [Tier 2 — Observability & Lifecycle](#tier-2--observability--lifecycle)
|
|
16
|
+
- [Tier 3 — Multi-Object & Namespace](#tier-3--multi-object--namespace)
|
|
17
|
+
- [Tier 4 — Integration & Ecosystem](#tier-4--integration--ecosystem)
|
|
18
|
+
- [Tier 5 — Documentation & Examples](#tier-5--documentation--examples)
|
|
19
|
+
- [Dependency Graph](#dependency-graph)
|
|
20
|
+
- [Suggested Implementation Order](#suggested-implementation-order)
|
|
21
|
+
- [Design Principles](#design-principles)
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Tier 1 — Foundation
|
|
26
|
+
|
|
27
|
+
High-value, no-dependency features that unlock later tiers.
|
|
28
|
+
|
|
29
|
+
### 1.1 Method Metadata Caching
|
|
30
|
+
|
|
31
|
+
- [ ] Cache resolved `Method` objects and parameter signatures per service object
|
|
32
|
+
- [ ] Avoid repeated `public_method_defined?` / `.method()` reflection on every request
|
|
33
|
+
- [ ] Provide a registry API: `Clamo::Server.method_metadata(object, method_name)`
|
|
34
|
+
- [ ] Support cache invalidation (explicit `reset!` and optional TTL)
|
|
35
|
+
|
|
36
|
+
**Why:** Every dispatch currently reflects on the service object from scratch. Caching
|
|
37
|
+
method metadata improves throughput for high-frequency services and provides the
|
|
38
|
+
introspection foundation that Service Discovery, Autodoc, Schemas, and Profiling require.
|
|
39
|
+
|
|
40
|
+
**Depends on:** None. Unlocks: Service Discovery (1.5), Profiling (2.3), Auto-Prefixing (3.1), Schemas (4.3), Autodoc (4.4).
|
|
41
|
+
|
|
42
|
+
### 1.2 Method Allowlists / Denylists
|
|
43
|
+
|
|
44
|
+
- [ ] Allow restricting which methods are callable via `allow:` / `deny:` keyword options
|
|
45
|
+
- [ ] Support both per-call and per-object-registration configuration
|
|
46
|
+
- [ ] Deny takes precedence over allow when both are specified
|
|
47
|
+
- [ ] Return `-32601 Method not found` for denied methods (indistinguishable from non-existent)
|
|
48
|
+
|
|
49
|
+
**Why:** `public_method_defined?` prevents private-method access, but real services often
|
|
50
|
+
need finer control — exposing a subset of public methods, or hiding specific methods
|
|
51
|
+
without making them private. This is the most-requested security feature for production
|
|
52
|
+
JSON-RPC services.
|
|
53
|
+
|
|
54
|
+
**Depends on:** None. Unlocks: Auto-Prefixing (3.1).
|
|
55
|
+
|
|
56
|
+
### 1.3 Error Data Builder
|
|
57
|
+
|
|
58
|
+
- [ ] Provide a `Clamo::Error` exception class carrying `code`, `message`, and `data` fields
|
|
59
|
+
- [ ] Allow service methods to `raise Clamo::Error.new(code: -32001, message: "Not found", data: {id: 42})`
|
|
60
|
+
- [ ] Serialize the error fields into the JSON-RPC error response automatically
|
|
61
|
+
- [ ] Support the optional `data` field for structured error details (validation errors, field-level messages)
|
|
62
|
+
- [ ] Preserve the existing behavior of never leaking raw exception details for non-Clamo exceptions
|
|
63
|
+
|
|
64
|
+
**Why:** The JSON-RPC 2.0 spec defines an optional `data` field on error objects for
|
|
65
|
+
"additional information about the error." Currently Clamo strips all exception data for
|
|
66
|
+
security. Services need a safe, intentional way to attach structured error details
|
|
67
|
+
to responses. A dedicated exception class is the simplest path: raise it to send
|
|
68
|
+
structured errors, raise anything else and Clamo strips it as before.
|
|
69
|
+
|
|
70
|
+
**Depends on:** None. Unlocks: Schemas (4.3).
|
|
71
|
+
|
|
72
|
+
### 1.4 Context Injection
|
|
73
|
+
|
|
74
|
+
- [ ] Allow passing a `context:` keyword through dispatch that is forwarded to the service method
|
|
75
|
+
- [ ] Support both explicit argument injection and a thread-local accessor pattern
|
|
76
|
+
- [ ] Do not change the method signature contract for methods that don't need context
|
|
77
|
+
|
|
78
|
+
**Why:** Real-world services need request-scoped data (authenticated user, request ID,
|
|
79
|
+
tenant, locale) available inside dispatched methods. Without context injection, users must
|
|
80
|
+
resort to thread-locals or global state, both of which are error-prone and hard to test.
|
|
81
|
+
This is a prerequisite for meaningful Rack/Rails integration.
|
|
82
|
+
|
|
83
|
+
**Depends on:** None. Unlocks: Hooks (2.1), Rack Middleware (4.1), Rails Helpers (4.2).
|
|
84
|
+
|
|
85
|
+
### 1.5 Service Discovery
|
|
86
|
+
|
|
87
|
+
- [ ] Provide a built-in `rpc.discover` JSON-RPC method that returns available methods and their signatures
|
|
88
|
+
- [ ] For each method, report: name, parameter names, parameter kinds (required/optional/keyword/splat)
|
|
89
|
+
- [ ] Respect allowlists/denylists (1.2) — only discover callable methods
|
|
90
|
+
- [ ] Enrich output with schema information when Schemas (4.3) are available
|
|
91
|
+
- [ ] Reserve the `rpc.*` namespace for system methods (per JSON-RPC 2.0 spec recommendation)
|
|
92
|
+
|
|
93
|
+
**Why:** JSON-RPC has no built-in discoverability. MCP and LSP both rely on method
|
|
94
|
+
enumeration for tool/capability discovery. A runtime `rpc.discover` method makes Clamo
|
|
95
|
+
services self-describing without external documentation. This is distinct from Autodoc
|
|
96
|
+
(4.4), which generates static Markdown — Service Discovery is a live, queryable API.
|
|
97
|
+
|
|
98
|
+
**Depends on:** Method Metadata Caching (1.1). Unlocks: MCP Adapter (4.5), Autodoc (4.4).
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## Tier 2 — Observability & Lifecycle
|
|
103
|
+
|
|
104
|
+
Lifecycle hooks that enable logging, profiling, and instrumentation.
|
|
105
|
+
|
|
106
|
+
### 2.1 Hooks (before/after dispatch)
|
|
107
|
+
|
|
108
|
+
- [ ] Re-introduce `before_dispatch` and `after_dispatch` hooks with improved design
|
|
109
|
+
- [ ] Support both global and per-call hook registration
|
|
110
|
+
- [ ] `before_dispatch` receives `(method, params, context)` — raise to halt
|
|
111
|
+
- [ ] `after_dispatch` receives `(method, params, result, duration)` on success
|
|
112
|
+
- [ ] `on_error` continues to handle the failure path
|
|
113
|
+
- [ ] Hooks must be composable (multiple hooks run in registration order)
|
|
114
|
+
|
|
115
|
+
**Why:** Hooks were added in v0.8.0 and removed in v0.10.0 because callers could wrap
|
|
116
|
+
`dispatch` directly. However, wrapping doesn't compose well for cross-cutting concerns
|
|
117
|
+
(auth, logging, metrics) especially in batch requests where individual method calls need
|
|
118
|
+
instrumentation. A hook-based approach is cleaner for multi-concern lifecycles.
|
|
119
|
+
|
|
120
|
+
**Depends on:** Context Injection (1.4) — hooks should receive context. Unlocks: Observability (2.2), Profiling (2.3).
|
|
121
|
+
|
|
122
|
+
### 2.2 Observability & Logging
|
|
123
|
+
|
|
124
|
+
- [ ] Provide a structured logging interface (not tied to any specific logger)
|
|
125
|
+
- [ ] Log at minimum: method name, params (redactable), duration, outcome (success/error)
|
|
126
|
+
- [ ] Support log-level filtering (debug/info/warn/error)
|
|
127
|
+
- [ ] Integrate with Hooks (2.1) so custom instrumentation is possible
|
|
128
|
+
|
|
129
|
+
**Why:** Production services need visibility into what Clamo is dispatching. A
|
|
130
|
+
logger-agnostic interface lets users plug in Rails.logger, $stdout, or structured logging
|
|
131
|
+
(e.g., Semantic Logger) without Clamo taking an opinion on the logging library.
|
|
132
|
+
|
|
133
|
+
**Depends on:** Hooks (2.1). Unlocks: Rails Helpers (4.2).
|
|
134
|
+
|
|
135
|
+
### 2.3 Profiling
|
|
136
|
+
|
|
137
|
+
- [ ] Measure and expose per-method dispatch duration
|
|
138
|
+
- [ ] Provide a simple profiling hook or middleware that collects timing data
|
|
139
|
+
- [ ] Support aggregation (min/max/avg/p99) for long-running services
|
|
140
|
+
- [ ] Keep profiling opt-in and zero-overhead when disabled
|
|
141
|
+
|
|
142
|
+
**Why:** Before optimizing, you need to measure. Profiling helps users identify slow
|
|
143
|
+
methods, arity-check overhead, and serialization costs in their JSON-RPC services.
|
|
144
|
+
|
|
145
|
+
**Depends on:** Hooks (2.1), Method Metadata Caching (1.1). Unlocks: None (leaf feature).
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## Tier 3 — Multi-Object & Namespace
|
|
150
|
+
|
|
151
|
+
Support for composing multiple service objects into a single JSON-RPC endpoint.
|
|
152
|
+
|
|
153
|
+
### 3.1 Method Auto-Prefixing
|
|
154
|
+
|
|
155
|
+
- [ ] Map a service object to a method prefix: `object: MyAuth, prefix: "auth"` makes `auth.login` call `MyAuth.login`
|
|
156
|
+
- [ ] Support configurable separator (default: `"."`)
|
|
157
|
+
- [ ] Prefix is stripped before dispatch, so service methods keep clean names
|
|
158
|
+
- [ ] Prefixed methods appear in allowlists/denylists and metadata cache with their full name
|
|
159
|
+
|
|
160
|
+
**Why:** JSON-RPC services commonly use dotted method names to namespace operations
|
|
161
|
+
(e.g., `user.create`, `auth.login`). Without auto-prefixing, users must manually route
|
|
162
|
+
or name their Ruby methods with dots/underscores, which is unidiomatic.
|
|
163
|
+
|
|
164
|
+
**Depends on:** Method Metadata Caching (1.1), Method Allowlists/Denylists (1.2). Unlocks: Namespace Fusion (3.2).
|
|
165
|
+
|
|
166
|
+
### 3.2 Namespace Fusion (Multiple Objects)
|
|
167
|
+
|
|
168
|
+
- [ ] Accept an array or hash of `{ prefix: object }` pairs instead of a single `object:`
|
|
169
|
+
- [ ] Route each request to the correct service object by method prefix
|
|
170
|
+
- [ ] Batch requests may span multiple service objects
|
|
171
|
+
- [ ] Detect and reject duplicate method names across objects at registration time
|
|
172
|
+
|
|
173
|
+
**Why:** Non-trivial services split logic across multiple modules. Namespace fusion lets
|
|
174
|
+
users compose independently developed service objects into a single JSON-RPC endpoint
|
|
175
|
+
without a manual dispatch layer.
|
|
176
|
+
|
|
177
|
+
**Depends on:** Method Auto-Prefixing (3.1). Unlocks: Examples (5.1).
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
## Tier 4 — Integration & Ecosystem
|
|
182
|
+
|
|
183
|
+
Adapters and helpers for common Ruby frameworks and protocols.
|
|
184
|
+
|
|
185
|
+
### 4.1 Rack Middleware / Helpers
|
|
186
|
+
|
|
187
|
+
- [ ] Provide `Clamo::Rack::Middleware` that reads the request body, dispatches, and writes the response
|
|
188
|
+
- [ ] Set correct `Content-Type: application/json` headers
|
|
189
|
+
- [ ] Handle HTTP method filtering (POST only by convention)
|
|
190
|
+
- [ ] Support mounting at a path prefix (e.g., `map "/rpc"`)
|
|
191
|
+
- [ ] Integrate with Context Injection (1.4) to pass Rack env as context
|
|
192
|
+
|
|
193
|
+
**Why:** Most Ruby JSON-RPC services run behind Rack (Puma, Falcon, etc.). A thin Rack
|
|
194
|
+
adapter eliminates the boilerplate of reading the body, calling `handle_json`, and writing
|
|
195
|
+
the response — which every user currently writes by hand. This is ~30 LOC and immediately
|
|
196
|
+
doubles the addressable user base.
|
|
197
|
+
|
|
198
|
+
**Depends on:** Context Injection (1.4). Unlocks: Rails Helpers (4.2), MCP Adapter (4.5), Examples (5.1).
|
|
199
|
+
|
|
200
|
+
### 4.2 Rails Helpers
|
|
201
|
+
|
|
202
|
+
- [ ] Provide a Rails controller concern or base controller for JSON-RPC endpoints
|
|
203
|
+
- [ ] Support Rails routing conventions (`post "/rpc", to: "jsonrpc#dispatch"`)
|
|
204
|
+
- [ ] Integrate with Rails.logger via Observability (2.2)
|
|
205
|
+
- [ ] Forward `current_user` / request context via Context Injection (1.4)
|
|
206
|
+
|
|
207
|
+
**Why:** Rails is the most common Ruby web framework. A first-class integration lowers
|
|
208
|
+
the barrier to adoption and ensures Clamo works well with Rails conventions (strong
|
|
209
|
+
parameters, CSRF, logging, etc.).
|
|
210
|
+
|
|
211
|
+
**Depends on:** Rack Middleware (4.1), Context Injection (1.4), Observability (2.2). Unlocks: Examples (5.1).
|
|
212
|
+
|
|
213
|
+
### 4.3 Schemas (Request/Response Validation)
|
|
214
|
+
|
|
215
|
+
- [ ] Allow declaring parameter schemas per method (types, required fields, constraints)
|
|
216
|
+
- [ ] Validate incoming params against schema before dispatch
|
|
217
|
+
- [ ] Return `-32602 Invalid params` with structured error data on schema violations
|
|
218
|
+
- [ ] Support JSON Schema or a lightweight DSL — evaluate both before choosing
|
|
219
|
+
|
|
220
|
+
**Why:** Arity validation catches wrong argument counts, but not wrong types or values.
|
|
221
|
+
Schema validation adds a declarative safety layer that catches malformed requests before
|
|
222
|
+
they reach service logic, improving reliability and API documentation.
|
|
223
|
+
|
|
224
|
+
**Depends on:** Method Metadata Caching (1.1), Error Data Builder (1.3). Unlocks: Autodoc (4.4).
|
|
225
|
+
|
|
226
|
+
### 4.4 Autodoc (Markdown Generation)
|
|
227
|
+
|
|
228
|
+
- [ ] Introspect service objects and generate method documentation automatically
|
|
229
|
+
- [ ] Include method name, parameters (with types from schemas if available), and description
|
|
230
|
+
- [ ] Output Markdown by default; support pluggable formatters
|
|
231
|
+
- [ ] Complement Service Discovery (1.5) — Autodoc generates static docs, `rpc.discover` is runtime
|
|
232
|
+
|
|
233
|
+
**Why:** JSON-RPC services lack the built-in discoverability of REST APIs (no URL
|
|
234
|
+
structure, no OpenAPI by convention). Autodoc makes services self-documenting, which is
|
|
235
|
+
especially valuable for developer onboarding and API reference generation.
|
|
236
|
+
|
|
237
|
+
**Depends on:** Method Metadata Caching (1.1), Service Discovery (1.5), Schemas (4.3) — optional but enriches output. Unlocks: None (leaf feature).
|
|
238
|
+
|
|
239
|
+
### 4.5 MCP Server Adapter
|
|
240
|
+
|
|
241
|
+
- [ ] Provide `Clamo::MCP` adapter for the Model Context Protocol (JSON-RPC 2.0 over stdio/SSE)
|
|
242
|
+
- [ ] Handle MCP lifecycle methods: `initialize`, `tools/list`, `tools/call`
|
|
243
|
+
- [ ] Map Ruby service methods to MCP tool definitions using Service Discovery (1.5)
|
|
244
|
+
- [ ] Support stdio transport (for CLI tools and AI IDE integrations)
|
|
245
|
+
- [ ] Support SSE transport (for web-based MCP clients)
|
|
246
|
+
- [ ] Consider shipping as a separate gem (`clamo-mcp`) to keep the core dependency-free
|
|
247
|
+
|
|
248
|
+
**Why:** MCP (Model Context Protocol) is the emerging standard for AI tool integration,
|
|
249
|
+
used by Claude, Cursor, Windsurf, and other AI-powered development tools. MCP uses
|
|
250
|
+
JSON-RPC 2.0 as its transport layer — Clamo already speaks the protocol. An MCP adapter
|
|
251
|
+
makes Clamo the fastest path to building Ruby MCP servers, positioning the library at the
|
|
252
|
+
center of the AI tooling ecosystem.
|
|
253
|
+
|
|
254
|
+
**Depends on:** Service Discovery (1.5), Rack Middleware (4.1) — for SSE transport. Unlocks: Examples (5.1).
|
|
255
|
+
|
|
256
|
+
---
|
|
257
|
+
|
|
258
|
+
## Tier 5 — Documentation & Examples
|
|
259
|
+
|
|
260
|
+
### 5.1 Usage Examples
|
|
261
|
+
|
|
262
|
+
- [ ] Basic Rack/Puma example (standalone JSON-RPC server)
|
|
263
|
+
- [ ] Rails integration example (controller + routes)
|
|
264
|
+
- [ ] MCP server example (JSON-RPC over stdio for AI tool integration)
|
|
265
|
+
- [ ] Multi-object namespace fusion example
|
|
266
|
+
- [ ] Error handling patterns (custom error data, logging)
|
|
267
|
+
- [ ] Testing patterns (using `JSONRPCTestHelpers`)
|
|
268
|
+
|
|
269
|
+
**Why:** The current README covers the API surface but lacks runnable examples for common
|
|
270
|
+
integration patterns. Examples are the fastest path to adoption.
|
|
271
|
+
|
|
272
|
+
**Depends on:** Rack Helpers (4.1), Rails Helpers (4.2), MCP Adapter (4.5), Namespace
|
|
273
|
+
Fusion (3.2) — examples should demonstrate the features they cover. Unlocks: None (leaf feature).
|
|
274
|
+
|
|
275
|
+
---
|
|
276
|
+
|
|
277
|
+
## Dependency Graph
|
|
278
|
+
|
|
279
|
+
```
|
|
280
|
+
Depth 0 ─ no prerequisites (build in parallel)
|
|
281
|
+
1.1 Method Metadata Caching
|
|
282
|
+
1.2 Allowlists / Denylists
|
|
283
|
+
1.3 Error Data Builder
|
|
284
|
+
1.4 Context Injection
|
|
285
|
+
|
|
286
|
+
Depth 1 ─ needs one or more Depth-0 items (build in parallel)
|
|
287
|
+
1.5 Service Discovery ◄── 1.1
|
|
288
|
+
2.1 Hooks ◄── 1.4
|
|
289
|
+
3.1 Auto-Prefixing ◄── 1.1 + 1.2
|
|
290
|
+
4.1 Rack Middleware ◄── 1.4
|
|
291
|
+
4.3 Schemas ◄── 1.1 + 1.3
|
|
292
|
+
|
|
293
|
+
Depth 2 ─ needs one or more Depth-1 items
|
|
294
|
+
2.2 Observability ◄── 2.1
|
|
295
|
+
2.3 Profiling ◄── 2.1 + 1.1
|
|
296
|
+
3.2 Namespace Fusion ◄── 3.1
|
|
297
|
+
4.4 Autodoc ◄── 1.1 + 1.5 + (4.3)
|
|
298
|
+
4.5 MCP Adapter ◄── 1.5 + 4.1
|
|
299
|
+
|
|
300
|
+
Depth 3
|
|
301
|
+
4.2 Rails Helpers ◄── 4.1 + 2.2
|
|
302
|
+
|
|
303
|
+
Depth 4
|
|
304
|
+
5.1 Examples ◄── 4.1 + 4.2 + 4.5 + 3.2
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
---
|
|
308
|
+
|
|
309
|
+
## Suggested Implementation Order
|
|
310
|
+
|
|
311
|
+
Phases map directly to the dependency graph depths. Items within each phase can
|
|
312
|
+
be built in parallel — they share no mutual dependencies. The **Value** column
|
|
313
|
+
indicates adoption impact: how much each feature expands real-world usability.
|
|
314
|
+
|
|
315
|
+
| Phase | Items | Prerequisites | Value |
|
|
316
|
+
|-------|-------|---------------|-------|
|
|
317
|
+
| 1 | 1.3 Error Data Builder | None | Critical — unblocks structured error responses |
|
|
318
|
+
| 1 | 1.4 Context Injection | None | Critical — unblocks Rack/Rails/hooks |
|
|
319
|
+
| 1 | 1.2 Allowlists / Denylists | None | High — production security requirement |
|
|
320
|
+
| 1 | 1.1 Method Metadata Caching | None | High — foundation for introspection features |
|
|
321
|
+
| 2 | 4.1 Rack Middleware | 1.4 | Critical — trivial effort, doubles user base |
|
|
322
|
+
| 2 | 1.5 Service Discovery | 1.1 | High — MCP readiness, runtime introspection |
|
|
323
|
+
| 2 | 2.1 Hooks | 1.4 | High — enables observability stack |
|
|
324
|
+
| 2 | 3.1 Auto-Prefixing | 1.1, 1.2 | Medium — namespace support |
|
|
325
|
+
| 2 | 4.3 Schemas | 1.1, 1.3 | Medium — type validation |
|
|
326
|
+
| 3 | 4.5 MCP Adapter | 1.5, 4.1 | High — strategic AI ecosystem positioning |
|
|
327
|
+
| 3 | 2.2 Observability | 2.1 | Medium — production logging |
|
|
328
|
+
| 3 | 2.3 Profiling | 2.1, 1.1 | Low — optimization tool |
|
|
329
|
+
| 3 | 3.2 Namespace Fusion | 3.1 | Medium — multi-service composition |
|
|
330
|
+
| 3 | 4.4 Autodoc | 1.1, 1.5, (4.3) | Low — static docs generation |
|
|
331
|
+
| 4 | 4.2 Rails Helpers | 4.1, 2.2 | Medium — Rails adoption |
|
|
332
|
+
| 5 | 5.1 Examples | 4.1, 4.2, 4.5, 3.2 | High — adoption accelerator |
|
|
333
|
+
|
|
334
|
+
---
|
|
335
|
+
|
|
336
|
+
## Design Principles
|
|
337
|
+
|
|
338
|
+
These principles carry forward from v1.0.0 and apply to all future work:
|
|
339
|
+
|
|
340
|
+
1. **Zero required dependencies** — new features must not add mandatory gems
|
|
341
|
+
2. **Opt-in complexity** — features are off by default; users enable what they need
|
|
342
|
+
3. **Spec compliance** — never deviate from JSON-RPC 2.0 behavior
|
|
343
|
+
4. **Security by default** — never leak internal details unless explicitly opted in
|
|
344
|
+
5. **Thread safety** — all mutable state must be snapshot or synchronized
|
|
345
|
+
6. **Minimal LOC** — resist abstraction until the third use case demands it
|
data/README.md
CHANGED
|
@@ -177,16 +177,18 @@ Per-call config is snapshotted at the start of each dispatch, so concurrent muta
|
|
|
177
177
|
|
|
178
178
|
### Parallel Processing
|
|
179
179
|
|
|
180
|
-
Batch requests are processed in
|
|
180
|
+
Batch requests are processed sequentially, in order, in the calling process and thread. Parallel processing is opt-in: add the [parallel](https://github.com/grosser/parallel) gem to your bundle and pass any `Parallel.map` option to switch it on:
|
|
181
181
|
|
|
182
182
|
```ruby
|
|
183
183
|
Clamo::Server.dispatch(
|
|
184
184
|
request: batch_request,
|
|
185
185
|
object: MyService,
|
|
186
|
-
|
|
186
|
+
in_threads: 4 # Any Parallel.map option enables parallel processing
|
|
187
187
|
)
|
|
188
188
|
```
|
|
189
189
|
|
|
190
|
+
Prefer `in_threads:`. With `in_processes:` each request runs in a forked child, so changes a method makes to in-memory state and calls to `on_error` do not reach the parent process, and results must be Marshal-able. If options are passed but `parallel` is not installed, the batch still runs sequentially.
|
|
191
|
+
|
|
190
192
|
### Building JSON-RPC Requests
|
|
191
193
|
|
|
192
194
|
Clamo provides utilities for building JSON-RPC requests:
|
data/lib/clamo/jsonrpc.rb
CHANGED
|
@@ -86,6 +86,11 @@ module Clamo
|
|
|
86
86
|
end
|
|
87
87
|
end
|
|
88
88
|
|
|
89
|
+
# Used internally by Clamo::Server — not part of the public API.
|
|
90
|
+
def valid_id?(id)
|
|
91
|
+
id.is_a?(String) || id.is_a?(Numeric) || id.is_a?(NilClass)
|
|
92
|
+
end
|
|
93
|
+
|
|
89
94
|
# Used internally by Clamo::Server — not part of the public API.
|
|
90
95
|
def valid_request?(request)
|
|
91
96
|
request.is_a?(Hash) &&
|
|
@@ -105,12 +110,7 @@ module Clamo
|
|
|
105
110
|
end
|
|
106
111
|
|
|
107
112
|
def proper_id_if_any?(request)
|
|
108
|
-
|
|
109
|
-
id = fetch_indifferent(request, "id")
|
|
110
|
-
id.is_a?(String) || id.is_a?(Integer) || id.is_a?(NilClass)
|
|
111
|
-
else
|
|
112
|
-
true
|
|
113
|
-
end
|
|
113
|
+
!key_indifferent?(request, "id") || valid_id?(fetch_indifferent(request, "id"))
|
|
114
114
|
end
|
|
115
115
|
|
|
116
116
|
def validate_params_type!(params)
|
data/lib/clamo/server.rb
CHANGED
|
@@ -19,7 +19,10 @@ module Clamo
|
|
|
19
19
|
# )
|
|
20
20
|
# # => '{"jsonrpc":"2.0","result":3,"id":1}'
|
|
21
21
|
module Server
|
|
22
|
-
|
|
22
|
+
# +serialize+ is set by #handle_json: successful results are encoded to JSON
|
|
23
|
+
# as soon as they are produced, so an unencodable result fails only its own
|
|
24
|
+
# request (see #serialize_result).
|
|
25
|
+
Config = Data.define(:on_error, :serialize)
|
|
23
26
|
|
|
24
27
|
class << self
|
|
25
28
|
# Global error callback. Called with +(exception, method, params)+ whenever
|
|
@@ -31,10 +34,14 @@ module Clamo
|
|
|
31
34
|
#
|
|
32
35
|
# Clamo::Server.handle_json(request: body, object: MyService)
|
|
33
36
|
#
|
|
34
|
-
#
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
# A result that cannot be encoded as JSON (NaN, circular structures, ...)
|
|
38
|
+
# becomes an Internal error (-32603) for that request alone, and is
|
|
39
|
+
# reported through +on_error+. Other responses in a batch are unaffected.
|
|
40
|
+
#
|
|
41
|
+
# Accepts the same options as #dispatch.
|
|
42
|
+
def handle_json(request:, object:, on_error: self.on_error, **)
|
|
43
|
+
config = Config.new(on_error: on_error, serialize: true)
|
|
44
|
+
render_json(dispatch_json_with(request: request, object: object, config: config, **))
|
|
38
45
|
end
|
|
39
46
|
|
|
40
47
|
alias handle handle_json
|
|
@@ -45,17 +52,10 @@ module Clamo
|
|
|
45
52
|
#
|
|
46
53
|
# Clamo::Server.dispatch_json(request: json_string, object: MyService)
|
|
47
54
|
#
|
|
48
|
-
#
|
|
49
|
-
def dispatch_json(request:, object:, **)
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
begin
|
|
53
|
-
parsed = JSON.parse(request)
|
|
54
|
-
rescue JSON::JSONError
|
|
55
|
-
return JSONRPC.build_error_response_parse_error
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
dispatch(request: parsed, object: object, **)
|
|
55
|
+
# Accepts the same options as #dispatch.
|
|
56
|
+
def dispatch_json(request:, object:, on_error: self.on_error, **)
|
|
57
|
+
config = Config.new(on_error: on_error, serialize: false)
|
|
58
|
+
dispatch_json_with(request: request, object: object, config: config, **)
|
|
59
59
|
end
|
|
60
60
|
|
|
61
61
|
alias unparsed_dispatch_to_object dispatch_json
|
|
@@ -68,23 +68,53 @@ module Clamo
|
|
|
68
68
|
#
|
|
69
69
|
# ==== Options
|
|
70
70
|
# +on_error+:: Error callback for this call, overrides the global on_error.
|
|
71
|
-
# Extra keyword arguments
|
|
71
|
+
# Extra keyword arguments (e.g. +in_threads: 4+) are forwarded to
|
|
72
|
+
# +Parallel.map+ for batch requests and switch batch processing from
|
|
73
|
+
# sequential to parallel. They require the +parallel+ gem; without it the
|
|
74
|
+
# batch still runs sequentially.
|
|
72
75
|
def dispatch(request:, object:,
|
|
73
76
|
on_error: self.on_error,
|
|
74
|
-
**
|
|
77
|
+
**)
|
|
75
78
|
raise ArgumentError, "object is required" unless object
|
|
76
79
|
|
|
80
|
+
config = Config.new(on_error: on_error, serialize: false)
|
|
81
|
+
dispatch_with(request: request, object: object, config: config, **)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
alias parsed_dispatch_to_object dispatch
|
|
85
|
+
|
|
86
|
+
private
|
|
87
|
+
|
|
88
|
+
def dispatch_json_with(request:, object:, config:, **)
|
|
89
|
+
raise ArgumentError, "object is required" unless object
|
|
90
|
+
|
|
91
|
+
begin
|
|
92
|
+
parsed = JSON.parse(request)
|
|
93
|
+
rescue JSON::JSONError
|
|
94
|
+
return JSONRPC.build_error_response_parse_error
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
dispatch_with(request: parsed, object: object, config: config, **)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def dispatch_with(request:, object:, config:, **)
|
|
77
101
|
request = normalize_request_keys(request)
|
|
78
|
-
config = Config.new(on_error: on_error)
|
|
79
102
|
|
|
80
|
-
response_for(request: request, object: object, config: config, **
|
|
103
|
+
response_for(request: request, object: object, config: config, **) do |method, params|
|
|
81
104
|
dispatch_to_ruby(object: object, method: method, params: params)
|
|
82
105
|
end
|
|
83
106
|
end
|
|
84
107
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
108
|
+
# Joins the responses produced under +serialize: true+. Successful results
|
|
109
|
+
# arrive already encoded as Strings; everything else is still a Hash.
|
|
110
|
+
def render_json(response)
|
|
111
|
+
case response
|
|
112
|
+
when nil then nil
|
|
113
|
+
when String then response
|
|
114
|
+
when Array then "[#{response.map { |item| item.is_a?(String) ? item : item.to_json }.join(",")}]"
|
|
115
|
+
else response.to_json
|
|
116
|
+
end
|
|
117
|
+
end
|
|
88
118
|
|
|
89
119
|
def normalize_request_keys(request)
|
|
90
120
|
case request
|
|
@@ -99,8 +129,14 @@ module Clamo
|
|
|
99
129
|
if object.is_a?(Module)
|
|
100
130
|
object.singleton_class.public_method_defined?(name, false)
|
|
101
131
|
else
|
|
102
|
-
object.
|
|
132
|
+
singleton_has = object.singleton_class.public_method_defined?(name, false) ||
|
|
133
|
+
object.singleton_class.protected_method_defined?(name, false) ||
|
|
134
|
+
object.singleton_class.private_method_defined?(name, false)
|
|
135
|
+
if singleton_has
|
|
103
136
|
object.singleton_class.public_method_defined?(name, false)
|
|
137
|
+
else
|
|
138
|
+
object.class.public_method_defined?(name, false)
|
|
139
|
+
end
|
|
104
140
|
end
|
|
105
141
|
end
|
|
106
142
|
|
|
@@ -113,7 +149,8 @@ module Clamo
|
|
|
113
149
|
end
|
|
114
150
|
|
|
115
151
|
# Extra keyword arguments (**) are forwarded to response_for_batch only,
|
|
116
|
-
# where they become options for Parallel.map (e.g.,
|
|
152
|
+
# where they become options for Parallel.map (e.g., in_threads: 4) and
|
|
153
|
+
# opt the batch into parallel processing (see #map_batch).
|
|
117
154
|
# For single requests they are silently ignored.
|
|
118
155
|
def response_for(request:, object:, config:, **, &block)
|
|
119
156
|
case request
|
|
@@ -130,8 +167,9 @@ module Clamo
|
|
|
130
167
|
error = validate_request_structure(request)
|
|
131
168
|
return error if error
|
|
132
169
|
|
|
133
|
-
|
|
134
|
-
|
|
170
|
+
# Not via a validate_* helper: error_for is nil for notifications, which
|
|
171
|
+
# a caller would read as "valid" and go on to execute the method.
|
|
172
|
+
return error_for(request, JSONRPC::ProtocolErrors::INVALID_PARAMS) unless JSONRPC.proper_params_if_any?(request)
|
|
135
173
|
|
|
136
174
|
unless method_known?(object: object, method: request["method"])
|
|
137
175
|
return error_for(request, JSONRPC::ProtocolErrors::METHOD_NOT_FOUND)
|
|
@@ -162,11 +200,23 @@ module Clamo
|
|
|
162
200
|
result.empty? ? nil : result
|
|
163
201
|
end
|
|
164
202
|
|
|
165
|
-
|
|
203
|
+
# Parallelism is opt-in: without options the batch runs in order in the
|
|
204
|
+
# calling process and thread. Parallel.map's own default is to fork one
|
|
205
|
+
# process per core, where a service method's state changes and on_error
|
|
206
|
+
# calls would be lost to the parent.
|
|
207
|
+
def map_batch(items, **opts, &)
|
|
208
|
+
return items.map(&) if opts.empty? || !parallel_available?
|
|
209
|
+
|
|
210
|
+
Parallel.map(items, **opts, &)
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
# Only the require is guarded: a LoadError raised by a service method while
|
|
214
|
+
# the batch runs must propagate, not trigger a serial replay of the batch.
|
|
215
|
+
def parallel_available?
|
|
166
216
|
require "parallel"
|
|
167
|
-
|
|
217
|
+
true
|
|
168
218
|
rescue LoadError
|
|
169
|
-
|
|
219
|
+
false
|
|
170
220
|
end
|
|
171
221
|
|
|
172
222
|
def error_for(request, descriptor)
|
|
@@ -178,16 +228,12 @@ module Clamo
|
|
|
178
228
|
def validate_request_structure(request)
|
|
179
229
|
return if JSONRPC.valid_request?(request)
|
|
180
230
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
)
|
|
185
|
-
end
|
|
186
|
-
|
|
187
|
-
def validate_params_type(request)
|
|
188
|
-
return if JSONRPC.proper_params_if_any?(request)
|
|
231
|
+
# The id is echoed only when it is itself valid; otherwise the spec
|
|
232
|
+
# requires null.
|
|
233
|
+
id = request["id"] if request.is_a?(Hash)
|
|
234
|
+
id = nil unless JSONRPC.valid_id?(id)
|
|
189
235
|
|
|
190
|
-
|
|
236
|
+
JSONRPC.build_error_response_from(id: id, descriptor: JSONRPC::ProtocolErrors::INVALID_REQUEST)
|
|
191
237
|
end
|
|
192
238
|
|
|
193
239
|
def dispatch_notification(request, block, config)
|
|
@@ -204,12 +250,25 @@ module Clamo
|
|
|
204
250
|
method = request["method"]
|
|
205
251
|
params = request["params"]
|
|
206
252
|
result = block.yield(method, params)
|
|
207
|
-
JSONRPC.build_result_response(id: request["id"], result: result)
|
|
253
|
+
serialize_result(JSONRPC.build_result_response(id: request["id"], result: result), method, params, config)
|
|
208
254
|
rescue StandardError => e
|
|
209
255
|
config.on_error&.call(e, method, params)
|
|
210
256
|
JSONRPC.build_error_response_from(id: request["id"], descriptor: JSONRPC::ProtocolErrors::SERVER_ERROR)
|
|
211
257
|
end
|
|
212
258
|
|
|
259
|
+
# Encodes the response here rather than in #handle_json, where the method
|
|
260
|
+
# and params needed for +on_error+ are no longer known. Kept outside the
|
|
261
|
+
# dispatch rescue so a service method raising a JSON error is still a
|
|
262
|
+
# Server error, not an Internal error.
|
|
263
|
+
def serialize_result(response, method, params, config)
|
|
264
|
+
return response unless config.serialize
|
|
265
|
+
|
|
266
|
+
response.to_json
|
|
267
|
+
rescue JSON::JSONError => e
|
|
268
|
+
config.on_error&.call(e, method, params)
|
|
269
|
+
JSONRPC.build_error_response_from(id: response["id"], descriptor: JSONRPC::ProtocolErrors::INTERNAL_ERROR)
|
|
270
|
+
end
|
|
271
|
+
|
|
213
272
|
def params_match_arity?(object:, method:, params:)
|
|
214
273
|
ruby_method = resolve_method(object, method)
|
|
215
274
|
return true unless ruby_method
|
|
@@ -234,9 +293,10 @@ module Clamo
|
|
|
234
293
|
by_type = parameters.group_by(&:first)
|
|
235
294
|
|
|
236
295
|
return false if by_type.key?(:keyreq)
|
|
237
|
-
return true if by_type.key?(:rest)
|
|
238
296
|
|
|
239
297
|
required = by_type.fetch(:req, []).size
|
|
298
|
+
return count >= required if by_type.key?(:rest)
|
|
299
|
+
|
|
240
300
|
count.between?(required, required + by_type.fetch(:opt, []).size)
|
|
241
301
|
end
|
|
242
302
|
|
data/lib/clamo/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: clamo
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andriy Tyurnikov
|
|
8
|
-
bindir:
|
|
8
|
+
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies: []
|
|
@@ -21,6 +21,7 @@ files:
|
|
|
21
21
|
- ".rubocop.yml"
|
|
22
22
|
- CHANGELOG.md
|
|
23
23
|
- LICENSE
|
|
24
|
+
- PRD.md
|
|
24
25
|
- README.md
|
|
25
26
|
- Rakefile
|
|
26
27
|
- lib/clamo.rb
|
|
@@ -49,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
49
50
|
- !ruby/object:Gem::Version
|
|
50
51
|
version: '0'
|
|
51
52
|
requirements: []
|
|
52
|
-
rubygems_version: 4.0.
|
|
53
|
+
rubygems_version: 4.0.20
|
|
53
54
|
specification_version: 4
|
|
54
55
|
summary: JSON-RPC 2.0 server toolkit for Ruby
|
|
55
56
|
test_files: []
|