nexo_ai 0.11.0 → 0.13.0
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/.rdoc_options +5 -0
- data/CHANGELOG.md +95 -0
- data/README.md +13 -14
- data/Rakefile +5 -0
- data/docs/concurrency.md +1 -2
- data/docs/durable-workflows.md +5 -3
- data/docs/getting-started.md +1 -1
- data/docs/loops.md +62 -89
- data/docs/mcp.md +57 -51
- data/docs/rails.md +1 -1
- data/docs/sessions.md +7 -3
- data/docs/skills.md +20 -11
- data/docs/workflows.md +2 -2
- data/examples/mcp_filesystem.rb +1 -1
- data/lib/generators/nexo/artifacts/artifacts_generator.rb +9 -7
- data/lib/generators/nexo/artifacts/templates/add_artifacts_to_nexo_workflow_runs.rb +1 -1
- data/lib/generators/nexo/install/install_generator.rb +7 -5
- data/lib/generators/nexo/skill/skill_generator.rb +10 -8
- data/lib/generators/nexo/state/state_generator.rb +9 -7
- data/lib/generators/nexo/state/templates/add_state_to_nexo_workflow_runs.rb +1 -1
- data/lib/generators/nexo/workflows/templates/create_nexo_workflow_runs.rb +3 -3
- data/lib/generators/nexo/workflows/workflows_generator.rb +7 -5
- data/lib/nexo/agent.rb +159 -169
- data/lib/nexo/concurrent.rb +18 -16
- data/lib/nexo/configuration.rb +21 -21
- data/lib/nexo/engine.rb +1 -1
- data/lib/nexo/loop.rb +12 -14
- data/lib/nexo/loops/ruby_llm.rb +43 -34
- data/lib/nexo/mcp/gated_tool.rb +31 -27
- data/lib/nexo/mcp.rb +73 -68
- data/lib/nexo/output_truncator.rb +9 -9
- data/lib/nexo/permissions.rb +44 -46
- data/lib/nexo/read_tracker.rb +4 -4
- data/lib/nexo/run_store.rb +30 -28
- data/lib/nexo/sandbox.rb +29 -27
- data/lib/nexo/sandboxes/container.rb +75 -75
- data/lib/nexo/sandboxes/local.rb +15 -15
- data/lib/nexo/sandboxes/remote.rb +25 -23
- data/lib/nexo/sandboxes/virtual.rb +2 -2
- data/lib/nexo/sandboxes.rb +9 -9
- data/lib/nexo/session.rb +31 -29
- data/lib/nexo/skills/skill.rb +89 -0
- data/lib/nexo/skills.rb +33 -58
- data/lib/nexo/tools/fetch.rb +21 -21
- data/lib/nexo/tools/glob.rb +6 -6
- data/lib/nexo/tools/read_file.rb +8 -8
- data/lib/nexo/tools/shell.rb +7 -7
- data/lib/nexo/tools/web_search.rb +16 -16
- data/lib/nexo/tools/write_file.rb +11 -11
- data/lib/nexo/turbo_broadcaster.rb +1 -1
- data/lib/nexo/version.rb +2 -2
- data/lib/nexo/workflow.rb +207 -193
- data/lib/nexo/workflow_job.rb +6 -6
- data/lib/nexo/workflow_run.rb +10 -10
- data/lib/nexo.rb +28 -26
- data/sig/nexo/agent.rbs +1 -1
- metadata +7 -34
- data/lib/nexo/loops/agent_sdk.rb +0 -68
data/lib/nexo/concurrent.rb
CHANGED
|
@@ -3,41 +3,43 @@
|
|
|
3
3
|
module Nexo
|
|
4
4
|
# Bounded fan-out driver for running many agent/workflow calls concurrently on
|
|
5
5
|
# Ruby's fiber scheduler (Spec 5). It is the value Nexo adds over a raw
|
|
6
|
-
#
|
|
6
|
+
# `Async {}` block: *rate-bounded* concurrency (so you stay under provider rate
|
|
7
7
|
# limits) with proper error propagation (the first task failure is re-raised,
|
|
8
8
|
# never swallowed) and results returned in **submission order**.
|
|
9
9
|
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
10
|
+
# ```ruby
|
|
11
|
+
# results = Nexo.concurrent(max_in_flight: 8) do |c|
|
|
12
|
+
# docs.each { |d| c.add { SummarizeDocument.run(text: d.body).result } }
|
|
13
|
+
# end
|
|
14
|
+
# # => one result per doc, in doc order, never more than 8 calls in flight
|
|
15
|
+
# ```
|
|
14
16
|
#
|
|
15
|
-
#
|
|
16
|
-
# fan-out actually runs. With the gem absent,
|
|
17
|
+
# `async` is a SOFT (optional) dependency: it is required lazily the moment a
|
|
18
|
+
# fan-out actually runs. With the gem absent, `require "nexo"` still loads
|
|
17
19
|
# cleanly; only calling Nexo.concurrent raises MissingDependencyError.
|
|
18
20
|
class Concurrent
|
|
19
21
|
# A submitted unit of work. Wrapping the block in a Struct keeps submission
|
|
20
|
-
# order explicit (the index into
|
|
22
|
+
# order explicit (the index into `@tasks` is the index into the results).
|
|
21
23
|
Task = Struct.new(:block)
|
|
22
24
|
|
|
23
|
-
#
|
|
24
|
-
# collector is run inside an
|
|
25
|
+
# `max_in_flight` bounds how many submitted blocks run concurrently once the
|
|
26
|
+
# collector is run inside an `async` reactor.
|
|
25
27
|
def initialize(max_in_flight:)
|
|
26
28
|
@max_in_flight = max_in_flight
|
|
27
29
|
@tasks = []
|
|
28
30
|
end
|
|
29
31
|
|
|
30
32
|
# Registers a block to run. Called via the yielded collector inside
|
|
31
|
-
# Nexo.concurrent. Order of
|
|
33
|
+
# Nexo.concurrent. Order of `add` calls is the order of the results array.
|
|
32
34
|
def add(&block)
|
|
33
35
|
@tasks << Task.new(block)
|
|
34
36
|
end
|
|
35
37
|
|
|
36
38
|
# Runs every added block inside ONE reactor, bounding in-flight work with an
|
|
37
|
-
#
|
|
39
|
+
# `Async::Semaphore` and coordinating with an `Async::Barrier`. Results are
|
|
38
40
|
# index-assigned, so the returned Array is in submission order regardless of
|
|
39
41
|
# completion order. On the first task that raises, the error propagates out
|
|
40
|
-
# of
|
|
42
|
+
# of `barrier.wait` and the `ensure` stops the remaining in-flight tasks —
|
|
41
43
|
# errors are never swallowed.
|
|
42
44
|
def run
|
|
43
45
|
require_async!
|
|
@@ -62,9 +64,9 @@ module Nexo
|
|
|
62
64
|
|
|
63
65
|
private
|
|
64
66
|
|
|
65
|
-
# Lazily loads
|
|
66
|
-
# soft-dependency
|
|
67
|
-
#
|
|
67
|
+
# Lazily loads `async` and its coordination primitives, mirroring the
|
|
68
|
+
# soft-dependency pattern: a bare
|
|
69
|
+
# `require` (so tests can stub it on the instance) rescuing stdlib `LoadError`
|
|
68
70
|
# into a MissingDependencyError that names the gem and the exact remedy.
|
|
69
71
|
def require_async!
|
|
70
72
|
require "async"
|
data/lib/nexo/configuration.rb
CHANGED
|
@@ -5,73 +5,73 @@ module Nexo
|
|
|
5
5
|
# mutated through Nexo.configure. Every default is safe-by-default and
|
|
6
6
|
# provider-neutral — there is intentionally no hardcoded model.
|
|
7
7
|
class Configuration
|
|
8
|
-
# The default ruby_llm model id.
|
|
8
|
+
# The default ruby_llm model id. `nil` means none chosen — provider-neutral.
|
|
9
9
|
attr_accessor :default_model
|
|
10
10
|
|
|
11
|
-
# The default sandbox:
|
|
11
|
+
# The default sandbox: `:virtual` (in-memory, zero host access).
|
|
12
12
|
attr_accessor :default_sandbox
|
|
13
13
|
|
|
14
|
-
# The default permission mode:
|
|
14
|
+
# The default permission mode: `:read_only`.
|
|
15
15
|
attr_accessor :default_permissions
|
|
16
16
|
|
|
17
|
-
# Where SKILL.md packages live. Rails-aware:
|
|
17
|
+
# Where SKILL.md packages live. Rails-aware: `app/skills` under the app root.
|
|
18
18
|
attr_accessor :skills_path
|
|
19
19
|
|
|
20
|
-
# Concurrency mode (Spec 5):
|
|
20
|
+
# Concurrency mode (Spec 5): `:threaded` (default) | `:async`. This is the
|
|
21
21
|
# single switch that gates the Sandboxes::Local offload path — under
|
|
22
|
-
#
|
|
23
|
-
# stall a fiber reactor; under
|
|
22
|
+
# `:async` its blocking file/shell I/O runs on a worker thread so it does not
|
|
23
|
+
# stall a fiber reactor; under `:threaded` it runs inline (zero overhead,
|
|
24
24
|
# byte-for-byte the Spec 1 behavior). It does NOT gate Nexo.concurrent,
|
|
25
25
|
# which always runs its own reactor when called.
|
|
26
26
|
attr_accessor :concurrency
|
|
27
27
|
|
|
28
28
|
# How RubyLLM executes MULTIPLE tool calls returned in a single assistant turn:
|
|
29
|
-
#
|
|
30
|
-
#
|
|
29
|
+
# `:fibers` (via `async`, the same driver Nexo.concurrent uses), `:threads`, or
|
|
30
|
+
# `false` for one at a time. `nil` (the default) leaves RubyLLM's own setting
|
|
31
31
|
# alone, so existing behaviour is unchanged until you opt in.
|
|
32
32
|
#
|
|
33
33
|
# Distinct from #concurrency, which governs how Nexo itself runs blocking work.
|
|
34
34
|
attr_accessor :tool_concurrency
|
|
35
35
|
|
|
36
|
-
# Max in-flight tasks for Nexo.concurrent fan-out (Spec 5), default
|
|
36
|
+
# Max in-flight tasks for Nexo.concurrent fan-out (Spec 5), default `8`.
|
|
37
37
|
# The single most important knob for staying under provider rate limits.
|
|
38
38
|
attr_accessor :max_in_flight
|
|
39
39
|
|
|
40
40
|
# Whether Workflow buffers events in memory and flushes once at the end of
|
|
41
|
-
# a run instead of persisting per
|
|
41
|
+
# a run instead of persisting per `emit` (Spec 5), default `false`. Buffering
|
|
42
42
|
# avoids a blocking per-event DB write under a fiber reactor.
|
|
43
43
|
attr_accessor :buffer_workflow_events
|
|
44
44
|
|
|
45
|
-
# The host
|
|
46
|
-
# given as a **String** class name (default
|
|
45
|
+
# The host `acts_as_chat` model that stores Nexo::Session threads (Spec 10),
|
|
46
|
+
# given as a **String** class name (default `"Chat"` — ruby_llm's own default).
|
|
47
47
|
# It is constantized lazily at resume time so plain Ruby / no-ActiveRecord
|
|
48
48
|
# paths never reference an AR constant, mirroring how RunStore only touches
|
|
49
49
|
# Nexo::WorkflowRun when it is defined. The host app owns this model (columns
|
|
50
|
-
#
|
|
50
|
+
# plus a unique `(agent_name, instance_id)` index); Nexo ships no migration for it.
|
|
51
51
|
attr_accessor :session_chat_model
|
|
52
52
|
|
|
53
53
|
# Whether to mirror run event notifications over Turbo Streams (Spec 11 R2),
|
|
54
|
-
# default
|
|
54
|
+
# default `false` (safe-by-default). When true and turbo-rails is present, the
|
|
55
55
|
# engine subscribes Nexo::TurboBroadcaster at boot. The plain
|
|
56
|
-
#
|
|
56
|
+
# `nexo.workflow.event`/`nexo.workflow.status` notifications fire regardless —
|
|
57
57
|
# this only gates the opt-in Turbo mirror.
|
|
58
58
|
attr_accessor :broadcast_events
|
|
59
59
|
|
|
60
|
-
# The run store Workflow persists runs through, default
|
|
60
|
+
# The run store Workflow persists runs through, default `nil` → the automatic
|
|
61
61
|
# choice (ActiveRecord under Rails, in-memory otherwise). Set an instance to
|
|
62
62
|
# override, which is what a host outside Rails needs for anything durable:
|
|
63
63
|
# checkpoint/suspend/resume all read state back from the store, and the
|
|
64
|
-
# in-memory one dies with the process.
|
|
64
|
+
# in-memory one dies with the process. `Nexo::RunStore::Disk.new(dir:)` is the
|
|
65
65
|
# shipped answer for a CLI or a script.
|
|
66
66
|
attr_accessor :run_store
|
|
67
67
|
|
|
68
68
|
# The ActiveJob queue Workflow.run_later enqueues onto (Spec 11 R1), default
|
|
69
|
-
#
|
|
69
|
+
# `nil` → ActiveJob's default queue. Set to a symbol (e.g. `:nexo`) to route
|
|
70
70
|
# workflow jobs to a dedicated queue.
|
|
71
71
|
attr_accessor :job_queue
|
|
72
72
|
|
|
73
|
-
# Seeds the safe, provider-neutral defaults (no model,
|
|
74
|
-
#
|
|
73
|
+
# Seeds the safe, provider-neutral defaults (no model, `:virtual` sandbox,
|
|
74
|
+
# `:read_only` permissions, `:threaded` concurrency).
|
|
75
75
|
def initialize
|
|
76
76
|
@default_model = nil # provider-neutral: NO hardcoded default
|
|
77
77
|
@default_sandbox = :virtual # safe default
|
data/lib/nexo/engine.rb
CHANGED
|
@@ -9,7 +9,7 @@ if defined?(::Rails::Engine)
|
|
|
9
9
|
# The Rails engine that wires Nexo into a host app: it loads the WorkflowRun
|
|
10
10
|
# model and WorkflowJob during boot, opt-in-subscribes the Turbo broadcaster,
|
|
11
11
|
# and (via Rails::Engine) exposes Nexo's generators and rake tasks. Defined
|
|
12
|
-
# only when
|
|
12
|
+
# only when `::Rails::Engine` is present, so the plain-Ruby core stays
|
|
13
13
|
# Rails-free.
|
|
14
14
|
class Engine < ::Rails::Engine
|
|
15
15
|
isolate_namespace Nexo
|
data/lib/nexo/loop.rb
CHANGED
|
@@ -3,26 +3,24 @@
|
|
|
3
3
|
module Nexo
|
|
4
4
|
# The loop seam: the engine that drives one prompt to completion. Swapping the
|
|
5
5
|
# loop swaps *how* the agent's turns are run — the plain provider-neutral
|
|
6
|
-
#
|
|
7
|
-
#
|
|
6
|
+
# `ruby_llm` tool loop, or a backend of your own — by constructor injection,
|
|
7
|
+
# with no change to the agent class.
|
|
8
8
|
#
|
|
9
9
|
# A loop implements the contract below. The base class raises so an incomplete
|
|
10
|
-
# subclass fails loudly. The optional
|
|
11
|
-
# with
|
|
12
|
-
#
|
|
10
|
+
# subclass fails loudly. The optional `&on_event` block, when given, is called
|
|
11
|
+
# with `(type, payload)` as the run progresses (e.g. `:tool_call`,
|
|
12
|
+
# `:tool_result`, `:done`) — observability only; it never steers the run.
|
|
13
13
|
#
|
|
14
|
-
# See Loops::RubyLLM
|
|
15
|
-
# (opt-in, Anthropic-oriented).
|
|
14
|
+
# See Loops::RubyLLM, the default and only backend Nexo ships.
|
|
16
15
|
class Loop
|
|
17
|
-
# Runs
|
|
18
|
-
#
|
|
19
|
-
# observability (RubyLLM — see the turn-cap caveat in the README).
|
|
16
|
+
# Runs `prompt` through `agent` and returns the final response. `max_turns`
|
|
17
|
+
# caps how many tool rounds one prompt may run; Loops::RubyLLM enforces it.
|
|
20
18
|
#
|
|
21
|
-
#
|
|
19
|
+
# `chat:` (default `nil`) lets a Nexo::Session inject a hydrated, continuing
|
|
22
20
|
# chat so the loop runs over the persisted thread. It is part of the base
|
|
23
|
-
# contract so every backend accepts it
|
|
24
|
-
#
|
|
25
|
-
#
|
|
21
|
+
# contract so every backend accepts it; a backend that cannot continue a
|
|
22
|
+
# chat should reject a non-nil one rather than silently drop the session's
|
|
23
|
+
# memory.
|
|
26
24
|
def run(agent:, prompt:, max_turns: 25, chat: nil, &on_event)
|
|
27
25
|
raise NotImplementedError
|
|
28
26
|
end
|
data/lib/nexo/loops/ruby_llm.rb
CHANGED
|
@@ -1,58 +1,60 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Nexo
|
|
4
|
-
# Namespace for the
|
|
5
|
-
#
|
|
6
|
-
#
|
|
7
|
-
#
|
|
4
|
+
# Namespace for the loop backends that drive one prompt to completion.
|
|
5
|
+
# Loops::RubyLLM is the default and the only one Nexo ships; a custom backend
|
|
6
|
+
# subclasses Nexo::Loop and is selected per agent via the `loop:` constructor
|
|
7
|
+
# injection.
|
|
8
8
|
module Loops
|
|
9
|
-
# The default, provider-neutral loop
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
14
|
-
#
|
|
9
|
+
# The default, provider-neutral loop: build the agent's chat (with its
|
|
10
|
+
# sandbox-backed tools) and let `ruby_llm` run the whole tool loop inside
|
|
11
|
+
# `#ask`. It works identically on any `ruby_llm`-supported model — Anthropic,
|
|
12
|
+
# OpenAI, Gemini, Ollama/Gemma — because file/shell capability comes entirely
|
|
13
|
+
# from the agent's own sandbox-backed tools, not from anything vendor-specific
|
|
14
|
+
# here.
|
|
15
15
|
#
|
|
16
|
-
# Tool
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
# otherwise so an older/newer +ruby_llm+ degrades to no observability rather
|
|
20
|
-
# than crashing.
|
|
16
|
+
# Tool activity is reported through `ruby_llm`'s `before_tool_call` /
|
|
17
|
+
# `after_tool_result` callbacks, and the turn cap is `ruby_llm`'s own
|
|
18
|
+
# `with_tool_options(rounds:)`.
|
|
21
19
|
class RubyLLM < Nexo::Loop
|
|
22
|
-
#
|
|
20
|
+
# `chat:` lets a Nexo::Session inject the hydrated, continuing chat so the
|
|
23
21
|
# loop runs over the persisted thread; left nil it builds the agent's own
|
|
24
|
-
# fresh chat
|
|
25
|
-
# +max_turns+ is a BUDGET, not a hard stop. ruby_llm runs the whole tool loop
|
|
26
|
-
# inside #ask and its callbacks cannot halt it, so this loop cannot cut a run
|
|
27
|
-
# short. What it can do is COUNT the tool calls and say so: exceeding the budget
|
|
28
|
-
# emits a +:turn_limit_exceeded+ event (once) carrying the count and the limit.
|
|
22
|
+
# fresh chat.
|
|
29
23
|
#
|
|
30
|
-
#
|
|
31
|
-
#
|
|
32
|
-
#
|
|
33
|
-
#
|
|
24
|
+
# `max_turns` is a HARD CAP on tool rounds: the number of model responses
|
|
25
|
+
# that ask for tools within this one prompt, however many calls each holds.
|
|
26
|
+
# When the cap is reached `ruby_llm` raises `RubyLLM::ToolRoundLimitError`
|
|
27
|
+
# instead of asking the model again. Every tool call that ran has its
|
|
28
|
+
# result, so the conversation stays valid. This loop emits a
|
|
29
|
+
# `:turn_limit_exceeded` event and lets the error propagate — a run that
|
|
30
|
+
# blows its budget fails loudly rather than continuing unbounded. Pass
|
|
31
|
+
# `max_turns: nil` to run uncapped.
|
|
34
32
|
def run(agent:, prompt:, max_turns: 25, chat: nil, &on_event)
|
|
35
33
|
chat ||= agent.chat
|
|
36
34
|
|
|
37
35
|
wire_observability(chat, max_turns: max_turns, &on_event)
|
|
36
|
+
chat.with_tool_options(rounds: max_turns) if chat.respond_to?(:with_tool_options)
|
|
38
37
|
|
|
39
38
|
response = chat.ask(prompt)
|
|
40
39
|
on_event&.call(:done, response)
|
|
41
40
|
response
|
|
41
|
+
rescue ::RubyLLM::ToolRoundLimitError
|
|
42
|
+
report_turn_limit(chat, max_turns)
|
|
43
|
+
raise
|
|
42
44
|
end
|
|
43
45
|
|
|
44
46
|
private
|
|
45
47
|
|
|
46
|
-
# Tool-call OBSERVABILITY
|
|
47
|
-
#
|
|
48
|
-
#
|
|
49
|
-
#
|
|
50
|
-
#
|
|
48
|
+
# Tool-call OBSERVABILITY: these callbacks report tool activity; the cap
|
|
49
|
+
# itself is `with_tool_options(rounds:)` in #run. BOTH callbacks must be
|
|
50
|
+
# present to wire (verified on ruby_llm 2.2.3, where they are additive); a
|
|
51
|
+
# chat double missing either degrades to no observability rather than
|
|
52
|
+
# crashing on the one it has.
|
|
51
53
|
#
|
|
52
54
|
# A continuing Nexo::Session runs the loop repeatedly over the SAME chat, so
|
|
53
55
|
# the wiring is done once per chat (flagged with an ivar) — otherwise ruby_llm
|
|
54
56
|
# (which *appends* callbacks) would stack a fresh pair every prompt and fire
|
|
55
|
-
# each event N times. The current
|
|
57
|
+
# each event N times. The current `on_event` is stashed on the chat so a later
|
|
56
58
|
# prompt observes with its own block rather than the first one. A fresh chat
|
|
57
59
|
# (the default per-prompt path) simply wires once — byte-for-byte the prior
|
|
58
60
|
# behavior.
|
|
@@ -77,16 +79,23 @@ module Nexo
|
|
|
77
79
|
end
|
|
78
80
|
end
|
|
79
81
|
|
|
80
|
-
# Counts one tool call
|
|
81
|
-
#
|
|
82
|
+
# Counts one tool call. A round may hold several calls, so the count can pass
|
|
83
|
+
# `max_turns` before the round cap trips; the first time it does, that is
|
|
84
|
+
# reported once per prompt so a long run does not drown its own event log.
|
|
82
85
|
def count_turn(chat)
|
|
83
86
|
limit = chat.instance_variable_get(:@nexo_max_turns)
|
|
84
87
|
turns = chat.instance_variable_get(:@nexo_turns).to_i + 1
|
|
85
88
|
chat.instance_variable_set(:@nexo_turns, turns)
|
|
86
|
-
|
|
89
|
+
report_turn_limit(chat, limit) if limit && turns > limit
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Emits `:turn_limit_exceeded` at most once per prompt, carrying the tool-call
|
|
93
|
+
# count so far and the limit.
|
|
94
|
+
def report_turn_limit(chat, limit)
|
|
87
95
|
return if chat.instance_variable_get(:@nexo_turns_reported)
|
|
88
96
|
|
|
89
97
|
chat.instance_variable_set(:@nexo_turns_reported, true)
|
|
98
|
+
turns = chat.instance_variable_get(:@nexo_turns).to_i
|
|
90
99
|
chat.instance_variable_get(:@nexo_on_event)&.call(:turn_limit_exceeded, {turns: turns, max_turns: limit})
|
|
91
100
|
end
|
|
92
101
|
end
|
data/lib/nexo/mcp/gated_tool.rb
CHANGED
|
@@ -2,53 +2,57 @@
|
|
|
2
2
|
|
|
3
3
|
module Nexo
|
|
4
4
|
module MCP
|
|
5
|
-
# Wraps
|
|
6
|
-
#
|
|
7
|
-
#
|
|
8
|
-
#
|
|
9
|
-
#
|
|
5
|
+
# Wraps an MCP tool so the chat sees an identical tool but every invocation is
|
|
6
|
+
# authorized through Nexo::Permissions#authorize_mcp! first. A denied call
|
|
7
|
+
# returns `{ error: ... }` (recoverable) and never raises into the loop —
|
|
8
|
+
# matching the sandbox-backed tools' authorize→act→rescue-`Denied`→`{error:}`
|
|
9
|
+
# shape (see `lib/nexo/tools/write_file.rb`).
|
|
10
10
|
#
|
|
11
11
|
# Everything except the execution method is delegated to the wrapped tool, so
|
|
12
12
|
# the chat cannot tell the wrapper apart from the raw MCP tool: it reports the
|
|
13
|
-
# same
|
|
13
|
+
# same `name`/`description`/`parameters_schema` and serializes identically.
|
|
14
14
|
#
|
|
15
|
-
#
|
|
16
|
-
# does not type-check
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
20
|
-
#
|
|
21
|
-
# etc. to the wrapped tool.
|
|
15
|
+
# VERIFIED (ruby_llm 2.2.3): a server's tools are `RubyLLM::MCP::Tool`
|
|
16
|
+
# instances (a `RubyLLM::Tool` subclass). `Chat#with_tools` does not type-check
|
|
17
|
+
# — it registers any object answering `qualified_name`/`namespace`/`name`/
|
|
18
|
+
# `deferred?` — and the tool loop invokes `tool.call(**arguments, tool_call:)`
|
|
19
|
+
# with KEYWORDS. So a delegating wrapper is accepted; `method_missing` forwards
|
|
20
|
+
# every reader the chat and the provider renderers ask for.
|
|
22
21
|
class GatedTool
|
|
23
|
-
# Wraps
|
|
24
|
-
#
|
|
22
|
+
# Wraps `tool` (an MCP tool) so every call is authorized through
|
|
23
|
+
# `permissions` (the MCP axis) before delegating.
|
|
25
24
|
def initialize(tool:, permissions:)
|
|
26
25
|
@tool = tool
|
|
27
26
|
@permissions = permissions
|
|
28
27
|
end
|
|
29
28
|
|
|
30
29
|
# Authorizes the call by tool name, then delegates to the wrapped tool. A
|
|
31
|
-
# denial is returned as
|
|
30
|
+
# denial is returned as `{ error: ... }` so the model can recover — never
|
|
32
31
|
# raised into the loop.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
#
|
|
33
|
+
# Arguments arrive as keywords from the chat loop. A single positional Hash
|
|
34
|
+
# is accepted too, for direct callers. `tool_call:` is the chat's own
|
|
35
|
+
# bookkeeping, not a tool argument, so the gate never sees it.
|
|
36
|
+
def call(args = nil, **kwargs)
|
|
37
|
+
arguments = (args || {}).to_h.merge(kwargs).transform_keys(&:to_sym)
|
|
38
|
+
@permissions.authorize_mcp!(name, arguments.except(:tool_call))
|
|
39
|
+
@tool.call(**arguments)
|
|
36
40
|
rescue Nexo::Permissions::Denied => e
|
|
37
41
|
{error: e.message}
|
|
38
42
|
end
|
|
39
43
|
|
|
40
|
-
# The wrapped tool's name —
|
|
44
|
+
# The wrapped tool's name — the key the gate authorizes on.
|
|
41
45
|
def name = @tool.name
|
|
42
46
|
|
|
43
|
-
#
|
|
44
|
-
#
|
|
45
|
-
# skip authorization entirely. Route it back to the gated #call.
|
|
47
|
+
# Routes `#execute` through the same gate, so the wrapper is safe whichever
|
|
48
|
+
# entry point a caller uses.
|
|
46
49
|
def execute(*args, **kwargs)
|
|
47
|
-
call(
|
|
50
|
+
call(args.first, **kwargs)
|
|
48
51
|
end
|
|
49
52
|
|
|
50
|
-
# Forward description/
|
|
51
|
-
# serializes it exactly like the raw MCP tool — but never
|
|
53
|
+
# Forward description/parameters_schema/to_h/etc. to the wrapped tool so the
|
|
54
|
+
# chat serializes it exactly like the raw MCP tool — but never `execute` (see
|
|
55
|
+
# above).
|
|
52
56
|
def respond_to_missing?(method_name, include_private = false)
|
|
53
57
|
return true if method_name == :execute
|
|
54
58
|
|
|
@@ -56,7 +60,7 @@ module Nexo
|
|
|
56
60
|
end
|
|
57
61
|
|
|
58
62
|
# Delegates any unknown method to the wrapped tool (description,
|
|
59
|
-
#
|
|
63
|
+
# parameters_schema, to_h, ...), so the wrapper serializes identically. `execute`
|
|
60
64
|
# is handled explicitly above and never falls through here.
|
|
61
65
|
def method_missing(method_name, *args, **kwargs, &block)
|
|
62
66
|
if @tool.respond_to?(method_name)
|
data/lib/nexo/mcp.rb
CHANGED
|
@@ -1,89 +1,67 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Nexo
|
|
4
|
-
# Composes
|
|
5
|
-
# with a single
|
|
6
|
-
# contributes tools that are gated by
|
|
7
|
-
# the model can invoke them (see
|
|
4
|
+
# Composes `ruby_llm`'s built-in MCP client (`RubyLLM::MCP`, core since 2.1) so an
|
|
5
|
+
# agent can attach one or more MCP servers with a single `mcp` class macro — no
|
|
6
|
+
# MCP client wiring by hand. A server contributes tools that are gated by
|
|
7
|
+
# Nexo::Permissions#authorize_mcp! before the model can invoke them (see
|
|
8
|
+
# MCP::GatedTool).
|
|
8
9
|
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
# with install guidance.
|
|
10
|
+
# No extra gem is needed: the client ships with `ruby_llm`. The community
|
|
11
|
+
# `ruby_llm-mcp` gem defines the same `RubyLLM::MCP` constant and must NOT be in
|
|
12
|
+
# the bundle alongside it.
|
|
13
13
|
#
|
|
14
14
|
# Provider-neutral by construction: MCP servers are reached through a server (not
|
|
15
15
|
# a vendor SDK), so behavior is identical on Anthropic, a local model, or anything
|
|
16
|
-
# else
|
|
16
|
+
# else `ruby_llm` supports.
|
|
17
17
|
module MCP
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
# gem and the exact remedy — when the gem is not installed. Mirrors
|
|
22
|
-
# Nexo::Skills.load!.
|
|
23
|
-
def load!
|
|
24
|
-
require "ruby_llm/mcp"
|
|
25
|
-
rescue LoadError
|
|
26
|
-
raise MissingDependencyError,
|
|
27
|
-
'MCP requires the `ruby_llm-mcp` gem. Add `gem "ruby_llm-mcp"` to your Gemfile.'
|
|
28
|
-
end
|
|
18
|
+
# Transports that reach a server over HTTP. `ruby_llm` speaks Streamable HTTP
|
|
19
|
+
# and falls back for older servers itself, so every one of these is just "a URL".
|
|
20
|
+
HTTP_TRANSPORTS = %i[http sse streamable streamable_http].freeze
|
|
29
21
|
|
|
30
|
-
|
|
31
|
-
#
|
|
32
|
-
#
|
|
33
|
-
#
|
|
22
|
+
class << self
|
|
23
|
+
# Builds a `RubyLLM::MCP` server from Nexo's friendly, transport-shaped macro
|
|
24
|
+
# opts and returns it. Nothing connects yet: `ruby_llm` starts a stdio process
|
|
25
|
+
# or opens the HTTP session on the first request (listing the tools), and the
|
|
26
|
+
# server is reusable across prompts until torn down with `#close`.
|
|
34
27
|
#
|
|
35
|
-
#
|
|
36
|
-
#
|
|
37
|
-
#
|
|
38
|
-
#
|
|
39
|
-
#
|
|
40
|
-
# handoff config carries no +token+ key by construction). When absent,
|
|
41
|
-
# +config:+ passes through byte-for-byte (no +headers+ key added) — +:stdio+ is
|
|
42
|
-
# untouched. Nexo runs no OAuth flow, refresh, or token store: it injects a
|
|
43
|
-
# header, the host owns the credential (see docs/mcp.md).
|
|
28
|
+
# `transport: :stdio` takes `command:` (a String, or an argv Array) plus
|
|
29
|
+
# optional `args:`, `env:` and `directory:`. An HTTP transport (`:http`,
|
|
30
|
+
# `:sse`, `:streamable`) takes `url:` plus optional `headers:` and
|
|
31
|
+
# `allow_http:`. `timeout:` applies to both. Any other key is handed to
|
|
32
|
+
# `ruby_llm` unchanged, which raises `ArgumentError` on one it does not know.
|
|
44
33
|
#
|
|
45
|
-
#
|
|
46
|
-
#
|
|
47
|
-
#
|
|
48
|
-
#
|
|
49
|
-
#
|
|
50
|
-
#
|
|
51
|
-
#
|
|
52
|
-
#
|
|
53
|
-
#
|
|
54
|
-
def build(name:, transport:, token: nil, **config)
|
|
55
|
-
load!
|
|
56
|
-
config = inject_auth(config, token) unless token.nil?
|
|
57
|
-
client_factory.client(name: name.to_s, transport_type: transport, config: config)
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
# Resolves a static-or-callable token and returns +config+ with an
|
|
61
|
-
# +Authorization: Bearer+ header merged under +:headers+ (the Group-0-verified
|
|
62
|
-
# key). A caller's other headers are preserved; Nexo's +Authorization+ wins.
|
|
34
|
+
# A stdio server's process inherits only a short list of safe variables from
|
|
35
|
+
# the application (`ruby_llm` 2.2). Pass what the server needs with `env:`.
|
|
36
|
+
#
|
|
37
|
+
# `token:` is Nexo's own option — a static bearer String or a callable
|
|
38
|
+
# (`#call`) that yields one — for an authenticated HTTP server. It is handed
|
|
39
|
+
# to `ruby_llm` as its `bearer_token`, which sends `Authorization: Bearer …`
|
|
40
|
+
# and reads a callable again for each request, so a rotated token is picked
|
|
41
|
+
# up without reconnecting. Nexo runs no OAuth flow, refresh, or token store:
|
|
42
|
+
# the host owns the credential (see docs/mcp.md).
|
|
63
43
|
#
|
|
64
44
|
# SECURITY: the token is a live credential. It must stay out of logs, events,
|
|
65
|
-
# persisted
|
|
66
|
-
#
|
|
67
|
-
#
|
|
68
|
-
#
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
config.merge(headers: headers)
|
|
45
|
+
# persisted `WorkflowRun` records, and URLs/query strings. It lives only in
|
|
46
|
+
# the server's in-memory settings; the event path (Workflow#serializable)
|
|
47
|
+
# carries only tool `name`/`args` and return values, never connection config,
|
|
48
|
+
# so the token never reaches `emit` (see the no-leak test).
|
|
49
|
+
def build(name:, transport:, token: nil, **config)
|
|
50
|
+
client_factory.mcp(**settings(name: name, transport: transport, token: token, config: config))
|
|
51
|
+
rescue ArgumentError => e
|
|
52
|
+
raise ConfigurationError, "mcp :#{name} — #{e.message}"
|
|
74
53
|
end
|
|
75
54
|
|
|
76
|
-
# The object whose
|
|
77
|
-
#
|
|
78
|
-
#
|
|
79
|
-
# a constant.
|
|
55
|
+
# The object whose `#mcp` builds the server — `::RubyLLM` in production.
|
|
56
|
+
# Overridable via #stub_client_factory so an offline test can inspect the
|
|
57
|
+
# exact settings Nexo hands off without a live server or stubbing a constant.
|
|
80
58
|
def client_factory
|
|
81
|
-
@client_factory || ::RubyLLM
|
|
59
|
+
@client_factory || ::RubyLLM
|
|
82
60
|
end
|
|
83
61
|
|
|
84
|
-
# Swaps #client_factory to
|
|
85
|
-
# restores it. Test-only seam
|
|
86
|
-
#
|
|
62
|
+
# Swaps #client_factory to `factory` for the duration of the block, then
|
|
63
|
+
# restores it. Test-only seam — keeps the settings mapping unit-testable at
|
|
64
|
+
# the `ruby_llm` boundary.
|
|
87
65
|
def stub_client_factory(factory)
|
|
88
66
|
previous = @client_factory
|
|
89
67
|
@client_factory = factory
|
|
@@ -91,6 +69,33 @@ module Nexo
|
|
|
91
69
|
ensure
|
|
92
70
|
@client_factory = previous
|
|
93
71
|
end
|
|
72
|
+
|
|
73
|
+
private
|
|
74
|
+
|
|
75
|
+
# Maps the macro opts onto the keywords `RubyLLM.mcp` takes.
|
|
76
|
+
def settings(name:, transport:, token:, config:)
|
|
77
|
+
config = config.dup
|
|
78
|
+
mapped = {name: name.to_s}
|
|
79
|
+
|
|
80
|
+
if transport.to_sym == :stdio
|
|
81
|
+
command = Array(config.delete(:command)) + Array(config.delete(:args))
|
|
82
|
+
raise ConfigurationError, "mcp :#{name} — a :stdio server needs a `command:`" if command.empty?
|
|
83
|
+
mapped[:command] = command.map(&:to_s)
|
|
84
|
+
elsif HTTP_TRANSPORTS.include?(transport.to_sym)
|
|
85
|
+
mapped[:bearer_token] = bearer(token) unless token.nil?
|
|
86
|
+
else
|
|
87
|
+
raise ConfigurationError,
|
|
88
|
+
"mcp :#{name} — unknown transport #{transport.inspect} (use :stdio, :http, :sse or :streamable)"
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
mapped.merge(config)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# `ruby_llm` reads a Proc again for each request; any other callable is
|
|
95
|
+
# wrapped in one so it gets the same treatment.
|
|
96
|
+
def bearer(token)
|
|
97
|
+
token.respond_to?(:call) ? -> { token.call } : token
|
|
98
|
+
end
|
|
94
99
|
end
|
|
95
100
|
end
|
|
96
101
|
end
|