legion-mcp 0.9.2 → 0.9.3
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/AGENTS.md +192 -26
- data/CHANGELOG.md +13 -0
- data/CLAUDE.md +196 -108
- data/README.md +253 -226
- data/lib/legion/mcp/deferred_registry.rb +4 -8
- data/lib/legion/mcp/server.rb +41 -29
- data/lib/legion/mcp/settings.rb +32 -15
- data/lib/legion/mcp/tool_governance.rb +1 -1
- data/lib/legion/mcp/tools/discover_tools.rb +4 -4
- data/lib/legion/mcp/version.rb +1 -1
- metadata +1 -1
data/lib/legion/mcp/server.rb
CHANGED
|
@@ -83,7 +83,13 @@ module Legion
|
|
|
83
83
|
end
|
|
84
84
|
|
|
85
85
|
def build(identity: nil)
|
|
86
|
-
|
|
86
|
+
identity = identity&.dup&.freeze
|
|
87
|
+
context = {
|
|
88
|
+
identity: identity,
|
|
89
|
+
conversation_id: TracingContext.generate_conversation_id,
|
|
90
|
+
trace_id: TracingContext.generate_trace_id
|
|
91
|
+
}.freeze
|
|
92
|
+
prepare_build(context: context)
|
|
87
93
|
tools = ToolGovernance.filter_tools(tool_registry.dup, identity)
|
|
88
94
|
|
|
89
95
|
server = ::MCP::Server.new(
|
|
@@ -91,11 +97,12 @@ module Legion
|
|
|
91
97
|
version: defined?(Legion::VERSION) ? Legion::VERSION : Legion::MCP::VERSION,
|
|
92
98
|
instructions: instructions,
|
|
93
99
|
tools: tools,
|
|
100
|
+
server_context: context,
|
|
94
101
|
resources: Resources::ExtensionInfo.static_resources,
|
|
95
102
|
resource_templates: Resources::ExtensionInfo.resource_templates
|
|
96
103
|
)
|
|
97
104
|
|
|
98
|
-
configure_server(server)
|
|
105
|
+
configure_server(server, context: context)
|
|
99
106
|
|
|
100
107
|
mcp_log :info, 'server.build.complete',
|
|
101
108
|
identity: Utils.summarize_identity(identity),
|
|
@@ -149,8 +156,8 @@ module Legion
|
|
|
149
156
|
Observer.record_intent_with_result(**observer_args)
|
|
150
157
|
end
|
|
151
158
|
|
|
152
|
-
def build_filtered_tool_list(keywords: [])
|
|
153
|
-
governed = ToolGovernance.filter_tools(tool_registry,
|
|
159
|
+
def build_filtered_tool_list(keywords: [], identity: @current_identity)
|
|
160
|
+
governed = ToolGovernance.filter_tools(tool_registry, identity)
|
|
154
161
|
tool_names = governed.map { |tc| tc.respond_to?(:tool_name) ? tc.tool_name : tc.name }
|
|
155
162
|
ranked = UsageFilter.ranked_tools(tool_names, keywords: keywords)
|
|
156
163
|
result = ranked.filter_map do |name|
|
|
@@ -160,35 +167,35 @@ module Legion
|
|
|
160
167
|
end
|
|
161
168
|
log.debug("[mcp][server] action=build_filtered_tool_list tools_before=#{tool_registry.size} " \
|
|
162
169
|
"governed=#{governed.size} tools_after=#{result.size} " \
|
|
163
|
-
"identity=#{Utils.summarize_identity(
|
|
170
|
+
"identity=#{Utils.summarize_identity(identity)} keywords=#{keywords.size}")
|
|
164
171
|
result
|
|
165
172
|
end
|
|
166
173
|
|
|
167
174
|
private
|
|
168
175
|
|
|
169
|
-
def prepare_build(
|
|
176
|
+
def prepare_build(context:)
|
|
170
177
|
run_function_discovery
|
|
171
178
|
rebuild_tool_registry
|
|
172
|
-
@current_identity = identity
|
|
173
|
-
@conversation_id =
|
|
174
|
-
@trace_id =
|
|
179
|
+
@current_identity = context[:identity]
|
|
180
|
+
@conversation_id = context[:conversation_id]
|
|
181
|
+
@trace_id = context[:trace_id]
|
|
175
182
|
|
|
176
183
|
mcp_log :info, 'server.build.start',
|
|
177
|
-
identity: Utils.summarize_identity(identity),
|
|
184
|
+
identity: Utils.summarize_identity(context[:identity]),
|
|
178
185
|
registry_size: tool_registry.size,
|
|
179
186
|
governance: ToolGovernance.governance_enabled?,
|
|
180
|
-
conversation_id:
|
|
187
|
+
conversation_id: context[:conversation_id]
|
|
181
188
|
end
|
|
182
189
|
|
|
183
|
-
def configure_server(server)
|
|
190
|
+
def configure_server(server, context:)
|
|
184
191
|
if defined?(Observer)
|
|
185
192
|
::MCP.configure do |c|
|
|
186
193
|
c.instrumentation_callback = ->(idata) { Server.wire_observer(idata) }
|
|
187
194
|
end
|
|
188
195
|
end
|
|
189
196
|
|
|
190
|
-
install_deferred_tools_list_handler(server)
|
|
191
|
-
install_tracing_tool_call_handler(server)
|
|
197
|
+
install_deferred_tools_list_handler(server, identity: context[:identity])
|
|
198
|
+
install_tracing_tool_call_handler(server, context: context)
|
|
192
199
|
register_mcp_tools_in_settings_extensions
|
|
193
200
|
|
|
194
201
|
Patterns::Store.hydrate_from_l2 if defined?(Patterns::Store)
|
|
@@ -265,24 +272,23 @@ module Legion
|
|
|
265
272
|
Legion::LLM::OverrideConfidence.hydrate_from_apollo if Legion::LLM::OverrideConfidence.respond_to?(:hydrate_from_apollo)
|
|
266
273
|
end
|
|
267
274
|
|
|
268
|
-
def install_deferred_tools_list_handler(server)
|
|
275
|
+
def install_deferred_tools_list_handler(server, identity:)
|
|
269
276
|
handlers = server.instance_variable_get(:@handlers)
|
|
270
277
|
return unless handlers
|
|
271
278
|
|
|
272
279
|
handlers[::MCP::Methods::TOOLS_LIST] = lambda { |_request|
|
|
273
|
-
tool_list = DeferredRegistry.build_tools_list(build_filtered_tool_list)
|
|
274
|
-
|
|
275
|
-
mcp_log :info, 'server.tools.list', total: tool_list.size, deferred_only: deferred
|
|
280
|
+
tool_list = DeferredRegistry.build_tools_list(build_filtered_tool_list(identity: identity))
|
|
281
|
+
mcp_log :info, 'server.tools.list', total: tool_list.size
|
|
276
282
|
{ tools: tool_list }
|
|
277
283
|
}
|
|
278
284
|
end
|
|
279
285
|
|
|
280
|
-
def install_tracing_tool_call_handler(server)
|
|
281
|
-
tracing_mod = build_tracing_module
|
|
286
|
+
def install_tracing_tool_call_handler(server, context:)
|
|
287
|
+
tracing_mod = build_tracing_module(context: context)
|
|
282
288
|
server.singleton_class.prepend(tracing_mod)
|
|
283
289
|
end
|
|
284
290
|
|
|
285
|
-
def build_tracing_module
|
|
291
|
+
def build_tracing_module(context:)
|
|
286
292
|
server_mod = self
|
|
287
293
|
Module.new do
|
|
288
294
|
define_method(:call_tool) do |request, **options|
|
|
@@ -291,19 +297,25 @@ module Legion
|
|
|
291
297
|
tool_call_id = TracingContext.generate_tool_call_id
|
|
292
298
|
|
|
293
299
|
TracingContext.set(
|
|
294
|
-
conversation_id:
|
|
300
|
+
conversation_id: context[:conversation_id],
|
|
295
301
|
request_id: request_id,
|
|
296
302
|
exchange_id: exchange_id,
|
|
297
303
|
tool_call_id: tool_call_id,
|
|
298
|
-
trace_id:
|
|
304
|
+
trace_id: context[:trace_id]
|
|
299
305
|
)
|
|
300
306
|
|
|
301
307
|
start_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
|
302
|
-
|
|
308
|
+
tool = tools[request&.dig(:name)]
|
|
309
|
+
result = if tool && ToolGovernance.filter_tools([tool], context[:identity]).empty?
|
|
310
|
+
add_instrumentation_data(tool_name: request[:name], tool_arguments: request[:arguments], error: :permission_denied)
|
|
311
|
+
error_tool_response("Permission denied for tool: #{request[:name]}")
|
|
312
|
+
else
|
|
313
|
+
super(request, **options)
|
|
314
|
+
end
|
|
303
315
|
elapsed = ((::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - start_time) * 1000).round(1)
|
|
304
316
|
|
|
305
317
|
server_mod.send(:emit_server_audit,
|
|
306
|
-
params: request, result: result, request_id: request_id,
|
|
318
|
+
context: context, params: request, result: result, request_id: request_id,
|
|
307
319
|
exchange_id: exchange_id, tool_call_id: tool_call_id, duration_ms: elapsed)
|
|
308
320
|
|
|
309
321
|
result
|
|
@@ -313,14 +325,14 @@ module Legion
|
|
|
313
325
|
end
|
|
314
326
|
end
|
|
315
327
|
|
|
316
|
-
def emit_server_audit(params:, result:, request_id:, exchange_id:, tool_call_id:, duration_ms:) # rubocop:disable Metrics/ParameterLists
|
|
328
|
+
def emit_server_audit(context:, params:, result:, request_id:, exchange_id:, tool_call_id:, duration_ms:) # rubocop:disable Metrics/ParameterLists
|
|
317
329
|
return unless defined?(Legion::MCP::Audit)
|
|
318
330
|
|
|
319
331
|
tool_name = params&.dig(:name)
|
|
320
332
|
status = result.is_a?(Hash) && result[:isError] ? :error : :success
|
|
321
333
|
|
|
322
334
|
Legion::MCP::Audit.emit_tool_call(
|
|
323
|
-
conversation_id:
|
|
335
|
+
conversation_id: context[:conversation_id],
|
|
324
336
|
request_id: request_id,
|
|
325
337
|
exchange_id: exchange_id,
|
|
326
338
|
tool_call_id: tool_call_id,
|
|
@@ -329,9 +341,9 @@ module Legion
|
|
|
329
341
|
result: Utils.summarize_result(result),
|
|
330
342
|
status: status,
|
|
331
343
|
duration_ms: duration_ms,
|
|
332
|
-
caller:
|
|
344
|
+
caller: context[:identity],
|
|
333
345
|
source: { type: :mcp_server },
|
|
334
|
-
trace_id:
|
|
346
|
+
trace_id: context[:trace_id],
|
|
335
347
|
timestamp: Time.now.utc.iso8601
|
|
336
348
|
)
|
|
337
349
|
rescue StandardError => e
|
data/lib/legion/mcp/settings.rb
CHANGED
|
@@ -7,19 +7,36 @@ module Legion
|
|
|
7
7
|
module Settings
|
|
8
8
|
module_function
|
|
9
9
|
|
|
10
|
-
def
|
|
10
|
+
def default
|
|
11
11
|
{
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
12
|
+
servers: {},
|
|
13
|
+
overrides: {},
|
|
14
|
+
tool_cache_ttl: 300,
|
|
15
|
+
connect_timeout: 10,
|
|
16
|
+
call_timeout: 30,
|
|
17
|
+
auto_expose_runners: false,
|
|
18
|
+
auth: auth_defaults,
|
|
19
|
+
governance: { enabled: false, audit_invocations: true, tool_risk_tiers: {} },
|
|
20
|
+
roles: {},
|
|
21
|
+
deferred_loading: deferred_loading_defaults,
|
|
22
|
+
dynamic_tools: dynamic_tools_defaults,
|
|
23
|
+
tier0: { guards: { max_stale_seconds: 3600, rapid_fire_threshold: 5, rapid_fire_window_seconds: 600 } },
|
|
24
|
+
cold_start: { patterns_path: nil }
|
|
25
|
+
}
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def codegen
|
|
29
|
+
{ self_generate: self_generate_defaults }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def auth_defaults
|
|
33
|
+
{
|
|
34
|
+
enabled: false,
|
|
35
|
+
require_auth: false,
|
|
36
|
+
jwt_secret: nil,
|
|
37
|
+
jwt_algorithm: 'HS256',
|
|
38
|
+
jwt_issuer: 'legion',
|
|
39
|
+
allowed_api_keys: []
|
|
23
40
|
}
|
|
24
41
|
end
|
|
25
42
|
|
|
@@ -34,6 +51,7 @@ module Legion
|
|
|
34
51
|
def self_generate_defaults
|
|
35
52
|
{
|
|
36
53
|
enabled: false,
|
|
54
|
+
cycle_interval: 300,
|
|
37
55
|
cooldown_seconds: 300,
|
|
38
56
|
max_gaps_per_cycle: 5,
|
|
39
57
|
tier: self_generate_tier_defaults,
|
|
@@ -90,6 +108,5 @@ module Legion
|
|
|
90
108
|
end
|
|
91
109
|
end
|
|
92
110
|
|
|
93
|
-
Legion::MCP::Settings.
|
|
94
|
-
|
|
95
|
-
end
|
|
111
|
+
Legion::Settings.merge_settings(:mcp, Legion::MCP::Settings.default)
|
|
112
|
+
Legion::Settings.merge_settings(:codegen, Legion::MCP::Settings.codegen)
|
|
@@ -33,10 +33,10 @@ module Legion
|
|
|
33
33
|
class << self
|
|
34
34
|
include Legion::Logging::Helper
|
|
35
35
|
|
|
36
|
-
def call(category: nil, intent: nil, tool_names: nil, schema: nil)
|
|
36
|
+
def call(category: nil, intent: nil, tool_names: nil, schema: nil, server_context: nil)
|
|
37
37
|
log.info('Starting legion.mcp.tools.discover_tools.call')
|
|
38
38
|
if tool_names && schema
|
|
39
|
-
resolve_schemas(tool_names)
|
|
39
|
+
resolve_schemas(tool_names, identity: server_context&.[](:identity))
|
|
40
40
|
elsif category
|
|
41
41
|
lookup_category(category)
|
|
42
42
|
elsif intent
|
|
@@ -52,8 +52,8 @@ module Legion
|
|
|
52
52
|
|
|
53
53
|
private
|
|
54
54
|
|
|
55
|
-
def resolve_schemas(tool_names)
|
|
56
|
-
governed = ToolGovernance.filter_tools(Server.tool_registry,
|
|
55
|
+
def resolve_schemas(tool_names, identity:)
|
|
56
|
+
governed = ToolGovernance.filter_tools(Server.tool_registry, identity)
|
|
57
57
|
schemas = DeferredRegistry.resolve_schemas(tool_names, governed)
|
|
58
58
|
if schemas.empty?
|
|
59
59
|
error_response("No tools found matching: #{tool_names.join(', ')}")
|
data/lib/legion/mcp/version.rb
CHANGED