legion-mcp 0.1.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.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/ci.yml +16 -0
  3. data/.gitignore +6 -0
  4. data/.rspec +3 -0
  5. data/.rubocop.yml +40 -0
  6. data/CHANGELOG.md +20 -0
  7. data/CLAUDE.md +101 -0
  8. data/Gemfile +10 -0
  9. data/LICENSE +167 -0
  10. data/README.md +182 -0
  11. data/Rakefile +5 -0
  12. data/legion-mcp.gemspec +35 -0
  13. data/lib/legion/mcp/auth.rb +50 -0
  14. data/lib/legion/mcp/context_compiler.rb +173 -0
  15. data/lib/legion/mcp/context_guard.rb +105 -0
  16. data/lib/legion/mcp/embedding_index.rb +113 -0
  17. data/lib/legion/mcp/observer.rb +171 -0
  18. data/lib/legion/mcp/pattern_store.rb +303 -0
  19. data/lib/legion/mcp/resources/extension_info.rb +67 -0
  20. data/lib/legion/mcp/resources/runner_catalog.rb +63 -0
  21. data/lib/legion/mcp/server.rb +178 -0
  22. data/lib/legion/mcp/tier_router.rb +122 -0
  23. data/lib/legion/mcp/tool_governance.rb +77 -0
  24. data/lib/legion/mcp/tools/create_chain.rb +50 -0
  25. data/lib/legion/mcp/tools/create_relationship.rb +51 -0
  26. data/lib/legion/mcp/tools/create_schedule.rb +64 -0
  27. data/lib/legion/mcp/tools/delete_chain.rb +52 -0
  28. data/lib/legion/mcp/tools/delete_relationship.rb +52 -0
  29. data/lib/legion/mcp/tools/delete_schedule.rb +52 -0
  30. data/lib/legion/mcp/tools/delete_task.rb +49 -0
  31. data/lib/legion/mcp/tools/describe_runner.rb +92 -0
  32. data/lib/legion/mcp/tools/disable_extension.rb +50 -0
  33. data/lib/legion/mcp/tools/discover_tools.rb +53 -0
  34. data/lib/legion/mcp/tools/do_action.rb +85 -0
  35. data/lib/legion/mcp/tools/enable_extension.rb +50 -0
  36. data/lib/legion/mcp/tools/get_config.rb +63 -0
  37. data/lib/legion/mcp/tools/get_extension.rb +56 -0
  38. data/lib/legion/mcp/tools/get_status.rb +50 -0
  39. data/lib/legion/mcp/tools/get_task.rb +48 -0
  40. data/lib/legion/mcp/tools/get_task_logs.rb +56 -0
  41. data/lib/legion/mcp/tools/list_chains.rb +48 -0
  42. data/lib/legion/mcp/tools/list_extensions.rb +46 -0
  43. data/lib/legion/mcp/tools/list_relationships.rb +45 -0
  44. data/lib/legion/mcp/tools/list_schedules.rb +51 -0
  45. data/lib/legion/mcp/tools/list_tasks.rb +50 -0
  46. data/lib/legion/mcp/tools/list_workers.rb +54 -0
  47. data/lib/legion/mcp/tools/rbac_assignments.rb +45 -0
  48. data/lib/legion/mcp/tools/rbac_check.rb +46 -0
  49. data/lib/legion/mcp/tools/rbac_grants.rb +41 -0
  50. data/lib/legion/mcp/tools/routing_stats.rb +51 -0
  51. data/lib/legion/mcp/tools/run_task.rb +68 -0
  52. data/lib/legion/mcp/tools/show_worker.rb +48 -0
  53. data/lib/legion/mcp/tools/team_summary.rb +55 -0
  54. data/lib/legion/mcp/tools/update_chain.rb +54 -0
  55. data/lib/legion/mcp/tools/update_relationship.rb +55 -0
  56. data/lib/legion/mcp/tools/update_schedule.rb +65 -0
  57. data/lib/legion/mcp/tools/worker_costs.rb +55 -0
  58. data/lib/legion/mcp/tools/worker_lifecycle.rb +54 -0
  59. data/lib/legion/mcp/usage_filter.rb +86 -0
  60. data/lib/legion/mcp/version.rb +7 -0
  61. data/lib/legion/mcp.rb +30 -0
  62. metadata +195 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9bef62b03828f3bda4594cb1eb2fa020da240b3df72db55412a65f854a515f40
4
+ data.tar.gz: fb5272985978c3d78b03c20b4dafc724cd869f20e7cf25daabdbacae715a889a
5
+ SHA512:
6
+ metadata.gz: 46793c8c4aa53e27f5831cbfec97984209fda9233325001b2fd2f87bf34502e1ee5a7196243ac90fb79cd5dd2351f2fc3078f952680ee25c735534150bc5b984
7
+ data.tar.gz: 4cd1ff55314687a3d5aa7028b53393d3997c53e455bd170cf96b1a93a1486dd1b56dab51c4fba9481d194639068e44622ba991caa5b8497099f411b8b051870b
@@ -0,0 +1,16 @@
1
+ name: CI
2
+ on:
3
+ push:
4
+ branches: [main]
5
+ pull_request:
6
+
7
+ jobs:
8
+ ci:
9
+ uses: LegionIO/.github/.github/workflows/ci.yml@main
10
+
11
+ release:
12
+ needs: ci
13
+ if: github.event_name == 'push' && github.ref == 'refs/heads/main'
14
+ uses: LegionIO/.github/.github/workflows/release.yml@main
15
+ secrets:
16
+ rubygems-api-key: ${{ secrets.RUBYGEMS_API_KEY }}
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ Gemfile.lock
2
+ coverage/
3
+ pkg/
4
+ *.gem
5
+ .bundle/
6
+ vendor/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,40 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.4
3
+ NewCops: enable
4
+ SuggestExtensions: false
5
+ Exclude:
6
+ - 'spec/**/*'
7
+ - 'legion-mcp.gemspec'
8
+ - 'vendor/**/*'
9
+
10
+ Metrics/BlockLength:
11
+ Exclude:
12
+ - 'spec/**/*'
13
+
14
+ Naming/PredicateMethod:
15
+ Enabled: false
16
+
17
+ Layout/HashAlignment:
18
+ EnforcedColonStyle: table
19
+ EnforcedHashRocketStyle: table
20
+
21
+ Style/Documentation:
22
+ Enabled: false
23
+
24
+ Metrics/MethodLength:
25
+ Max: 30
26
+
27
+ Metrics/AbcSize:
28
+ Max: 35
29
+
30
+ Metrics/CyclomaticComplexity:
31
+ Max: 12
32
+
33
+ Metrics/PerceivedComplexity:
34
+ Max: 12
35
+
36
+ Metrics/ModuleLength:
37
+ Max: 250
38
+
39
+ Layout/LineLength:
40
+ Max: 140
data/CHANGELOG.md ADDED
@@ -0,0 +1,20 @@
1
+ # legion-mcp Changelog
2
+
3
+ ## v0.1.0
4
+
5
+ ### Added
6
+ - Initial extraction from LegionIO
7
+ - MCP server builder with 35 tool classes
8
+ - Observer instrumentation pipeline (TBI Phase 0)
9
+ - ContextCompiler with semantic + keyword blended scoring (TBI Phase 2)
10
+ - UsageFilter with frequency/recency/keyword scoring (TBI Phase 2)
11
+ - EmbeddingIndex for semantic tool matching (TBI Phase 3)
12
+ - Auth (JWT + API key)
13
+ - ToolGovernance (risk-tier filtering)
14
+ - Resources: RunnerCatalog, ExtensionInfo
15
+ - PatternStore: 4-layer degrading storage (memory -> cache -> local SQLite -> shared DB)
16
+ - TierRouter: confidence-gated tier selection (Tier 0/1/2)
17
+ - ContextGuard: staleness, rapid-fire, anomaly detection guards
18
+ - Enhanced DoAction (legion.do) with Tier 0 routing before ContextCompiler fallback
19
+ - Observer feedback loop: automatic pattern candidate promotion after N successes
20
+ - Semantic intent matching via cosine similarity on stored pattern vectors
data/CLAUDE.md ADDED
@@ -0,0 +1,101 @@
1
+ # legion-mcp: MCP Server for LegionIO
2
+
3
+ **Parent**: `/Users/miverso2/rubymine/legion/CLAUDE.md`
4
+
5
+ ## Purpose
6
+
7
+ Standalone gem providing the Model Context Protocol (MCP) server for LegionIO. Extracted from LegionIO to enable independent versioning and reuse. Includes semantic tool matching, observation pipeline, context compilation, tiered inference (Tier 0/1/2), and tool governance.
8
+
9
+ **GitHub**: https://github.com/LegionIO/legion-mcp
10
+ **Version**: 0.1.0
11
+ **License**: Apache-2.0
12
+ **Ruby**: >= 3.4
13
+
14
+ ## Architecture
15
+
16
+ ```
17
+ Legion::MCP
18
+ ├── Server # MCP::Server builder, TOOL_CLASSES registration, governance-aware build
19
+ ├── Auth # JWT + API key authentication
20
+ ├── ToolGovernance # Risk-tier tool filtering + invocation audit
21
+ ├── ContextCompiler # Keyword + semantic tool matching, blended scoring (60% semantic + 40% keyword)
22
+ ├── EmbeddingIndex # In-memory vector cache for semantic tool matching
23
+ ├── Observer # Instrumentation pipeline: counters, ring buffer, pattern promotion
24
+ ├── UsageFilter # Frequency/recency/keyword scoring for dynamic tool filtering
25
+ ├── PatternStore # 4-layer degrading storage (L0 memory, L1 cache, L2 local SQLite)
26
+ ├── TierRouter # Confidence-gated tier selection (Tier 0/1/2)
27
+ ├── ContextGuard # Staleness, rapid-fire, anomaly detection guards
28
+ ├── Tools/ # 35 MCP::Tool subclasses (legion.* namespace)
29
+ └── Resources/ # RunnerCatalog, ExtensionInfo
30
+ ```
31
+
32
+ ## Dependencies
33
+
34
+ | Gem | Required | Purpose |
35
+ |-----|----------|---------|
36
+ | `mcp` (~> 0.8) | Yes | MCP server SDK |
37
+ | `legion-data` (>= 1.4) | Yes | Sequel models, migrations |
38
+ | `legion-json` (>= 1.2) | Yes | JSON serialization |
39
+ | `legion-logging` (>= 0.3) | Yes | Logging |
40
+ | `legion-settings` (>= 0.3) | Yes | Configuration |
41
+ | `legion-cache` | Optional | L1 pattern cache (memcached/redis) |
42
+ | `legion-llm` | Optional | Embeddings for semantic matching |
43
+ | `legionio` | Dev only | Full framework for integration testing |
44
+
45
+ ## Guard Strategy
46
+
47
+ All optional dependencies use `defined?()` guards:
48
+ - `defined?(Legion::Cache)` for L1 cache operations
49
+ - `defined?(Legion::Data::Local)` for L2 SQLite persistence
50
+ - `defined?(Legion::LLM)` for embedding generation
51
+ - `defined?(Legion::MCP::EmbeddingIndex)` for semantic matching
52
+ - `defined?(Legion::MCP::TierRouter)` for Tier 0 routing
53
+ - Every storage write wraps in `begin/rescue => nil` -- failed persistence never blocks Tier 0
54
+
55
+ ## Key Patterns
56
+
57
+ - **Graceful degradation**: PatternStore works with any combination of L0/L1/L2 available
58
+ - **Tier routing**: Tier 0 (>= 0.8 confidence, cached), Tier 1 (0.6-0.8, local/fleet), Tier 2 (< 0.6, cloud)
59
+ - **Pattern promotion**: Observer records intent+tool pairs; after 3 successful observations, promotes to PatternStore with seeded confidence 0.5
60
+ - **Context guards**: Staleness (1hr), rapid-fire (5 in 10min), anomaly (2 consecutive misses) prevent stale Tier 0
61
+
62
+ ## File Map
63
+
64
+ | Path | Purpose |
65
+ |------|---------|
66
+ | `lib/legion/mcp.rb` | Entry point: `Legion::MCP.server` singleton factory |
67
+ | `lib/legion/mcp/version.rb` | `Legion::MCP::VERSION` constant |
68
+ | `lib/legion/mcp/server.rb` | MCP::Server builder, TOOL_CLASSES array, governance-aware build |
69
+ | `lib/legion/mcp/auth.rb` | JWT + API key authentication |
70
+ | `lib/legion/mcp/tool_governance.rb` | Risk-tier tool filtering + invocation audit |
71
+ | `lib/legion/mcp/context_compiler.rb` | Keyword + semantic tool matching (60/40 blend) |
72
+ | `lib/legion/mcp/embedding_index.rb` | In-memory vector cache for semantic matching |
73
+ | `lib/legion/mcp/observer.rb` | Instrumentation: counters, ring buffer, pattern promotion |
74
+ | `lib/legion/mcp/usage_filter.rb` | Frequency/recency/keyword scoring for dynamic tool filtering |
75
+ | `lib/legion/mcp/pattern_store.rb` | 4-layer degrading storage (L0/L1/L2) with thread-safe access |
76
+ | `lib/legion/mcp/tier_router.rb` | Confidence-gated tier selection, tool chain execution |
77
+ | `lib/legion/mcp/context_guard.rb` | Staleness, rapid-fire, anomaly detection |
78
+ | `lib/legion/mcp/tools/` | 35 MCP::Tool subclasses (legion.* namespace) |
79
+ | `lib/legion/mcp/tools/do_action.rb` | Natural language intent routing with Tier 0 fast path |
80
+ | `lib/legion/mcp/tools/discover_tools.rb` | Dynamic tool discovery with context |
81
+ | `lib/legion/mcp/tools/run_task.rb` | Execute runner function via dot notation |
82
+ | `lib/legion/mcp/resources/runner_catalog.rb` | `legion://runners` resource |
83
+ | `lib/legion/mcp/resources/extension_info.rb` | `legion://extensions/{name}` resource template |
84
+
85
+ ## Development
86
+
87
+ ```bash
88
+ bundle install
89
+ bundle exec rspec # 278 examples, 0 failures
90
+ bundle exec rubocop -A # auto-fix
91
+ bundle exec rubocop # lint check
92
+ ```
93
+
94
+ ## Pre-Push Pipeline
95
+
96
+ See parent CLAUDE.md for the required pipeline: rspec -> rubocop -A -> rubocop -> version bump -> CHANGELOG -> push
97
+
98
+ ---
99
+
100
+ **Last Updated**: 2026-03-19
101
+ **Maintained By**: Matthew Iverson (@Esity)
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+ gemspec
5
+
6
+ gem 'rake'
7
+ gem 'rspec'
8
+ gem 'rubocop'
9
+ gem 'rubocop-rspec'
10
+ gem 'simplecov'
data/LICENSE ADDED
@@ -0,0 +1,167 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship made available under
36
+ the License, as indicated by a copyright notice that is included in
37
+ or attached to the work (an example is provided in the Appendix below).
38
+
39
+ "Derivative Works" shall mean any work, whether in Source or Object
40
+ form, that is based on (or derived from) the Work and for which the
41
+ editorial revisions, annotations, elaborations, or other transformations
42
+ represent, as a whole, an original work of authorship. For the purposes
43
+ of this License, Derivative Works shall not include works that remain
44
+ separable from, or merely link (or bind by name) to the interfaces of,
45
+ the Work and Derivative Works thereof.
46
+
47
+ "Contribution" shall mean, as submitted to the Licensor for inclusion
48
+ in the Work by the copyright owner or by an individual or Legal Entity
49
+ authorized to submit on behalf of the copyright owner. For the purposes
50
+ of this definition, "submitted" means any form of electronic, verbal,
51
+ or written communication sent to the Licensor or its representatives,
52
+ including but not limited to communication on electronic mailing lists,
53
+ source code control systems, and issue tracking systems that are managed
54
+ by, or on behalf of, the Licensor for the purpose of tracking and
55
+ discussing the Work, but excluding communication that is conspicuously
56
+ marked or designated in writing by the copyright owner as "Not a
57
+ Contribution."
58
+
59
+ "Contributor" shall mean Licensor and any Legal Entity on behalf of
60
+ whom a Contribution has been received by the Licensor and included
61
+ within the Work.
62
+
63
+ 2. Grant of Copyright License. Subject to the terms and conditions of
64
+ this License, each Contributor hereby grants to You a perpetual,
65
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
66
+ copyright license to reproduce, prepare Derivative Works of,
67
+ publicly display, publicly perform, sublicense, and distribute the
68
+ Work and such Derivative Works in Source or Object form.
69
+
70
+ 3. Grant of Patent License. Subject to the terms and conditions of
71
+ this License, each Contributor hereby grants to You a perpetual,
72
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
73
+ (except as stated in this section) patent license to make, have made,
74
+ use, offer to sell, sell, import, and otherwise transfer the Work,
75
+ where such license applies only to those patent claims licensable
76
+ by such Contributor that are necessarily infringed by their
77
+ Contribution(s) alone or by the combination of their Contribution(s)
78
+ with the Work to which such Contribution(s) was submitted. If You
79
+ institute patent litigation against any entity (including a cross-claim
80
+ or counterclaim in a lawsuit) alleging that the Work or any patent
81
+ claims in the Work constituting direct or contributory patent
82
+ infringement, then any patent licenses granted to You under this
83
+ License for the Work shall terminate as of the date such litigation
84
+ is filed.
85
+
86
+ 4. Redistribution. You may reproduce and distribute copies of the
87
+ Work or Derivative Works thereof in any medium, with or without
88
+ modifications, and in Source or Object form, provided that You
89
+ meet the following conditions:
90
+
91
+ (a) You must give any other recipients of the Work or Derivative
92
+ Works a copy of this License; and
93
+
94
+ (b) You must cause any modified files to carry prominent notices
95
+ stating that You changed the files; and
96
+
97
+ (c) You must retain, in the Source form of any Derivative Works
98
+ that You distribute, all copyright, patent, trademark, and
99
+ attribution notices from the Source form of the Work,
100
+ excluding those notices that do not pertain to any part of
101
+ the Derivative Works; and
102
+
103
+ (d) If the Work includes a "NOTICE" text file as part of its
104
+ distribution, You must include a readable copy of the
105
+ attribution notices contained within such NOTICE file, in
106
+ at least one of the following places: within a NOTICE text
107
+ file distributed as part of the Derivative Works; within
108
+ the Source form or documentation, if provided along with the
109
+ Derivative Works; or, within a display generated by the
110
+ Derivative Works, if and wherever such third-party notices
111
+ normally appear. The contents of the NOTICE file are for
112
+ informational purposes only and do not modify the License.
113
+ You may add Your own attribution notices within Derivative
114
+ Works that You distribute, alongside or in addition to the
115
+ NOTICE text from the Work, provided that such additional
116
+ attribution notices cannot be construed as modifying the
117
+ License.
118
+
119
+ You may add Your own license statement for Your modifications and
120
+ may provide additional grant of rights to use, copy, modify, merge,
121
+ publish, distribute, sublicense, and/or sell copies of the
122
+ Derivative Works, and to permit persons to whom the Derivative Works
123
+ is furnished to do so, subject to the following conditions.
124
+
125
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
126
+ any Contribution intentionally submitted for inclusion in the Work
127
+ by You to the Licensor shall be under the terms and conditions of
128
+ this License, without any additional terms or conditions.
129
+ Notwithstanding the above, nothing herein shall supersede or modify
130
+ the terms of any separate license agreement you may have executed
131
+ with Licensor regarding such Contributions.
132
+
133
+ 6. Trademarks. This License does not grant permission to use the trade
134
+ names, trademarks, service marks, or product names of the Licensor,
135
+ except as required for reasonable and customary use in describing the
136
+ origin of the Work and reproducing the content of the NOTICE file.
137
+
138
+ 7. Disclaimer of Warranty. Unless required by applicable law or
139
+ agreed to in writing, Licensor provides the Work (and each
140
+ Contributor provides its Contributions) on an "AS IS" BASIS,
141
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
142
+ implied, including, without limitation, any warranties or conditions
143
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
144
+ PARTICULAR PURPOSE. You are solely responsible for determining the
145
+ appropriateness of using or reproducing the Work and assume any
146
+ risks associated with Your exercise of permissions under this License.
147
+
148
+ 8. Limitation of Liability. In no event and under no legal theory,
149
+ whether in tort (including negligence), contract, or otherwise,
150
+ unless required by applicable law (such as deliberate and grossly
151
+ negligent acts) or agreed to in writing, shall any Contributor be
152
+ liable to You for damages, including any direct, indirect, special,
153
+ incidental, or exemplary damages of any character arising as a
154
+ result of this License or out of the use or inability to use the
155
+ Work (including but not limited to damages for loss of goodwill,
156
+ work stoppage, computer failure or malfunction, or all other
157
+ commercial damages or losses), even if such Contributor has been
158
+ advised of the possibility of such damages.
159
+
160
+ 9. Accepting Warranty or Additional Liability. While redistributing
161
+ the Work or Derivative Works thereof, You may choose to offer,
162
+ and charge a fee for, acceptance of support, warranty, indemnity,
163
+ or other liability obligations and/or rights consistent with this
164
+ License. However, in accepting such obligations, You may offer only
165
+ conditions consistent in kind with this terms of this License.
166
+
167
+ END OF TERMS AND CONDITIONS
data/README.md ADDED
@@ -0,0 +1,182 @@
1
+ # legion-mcp
2
+
3
+ MCP (Model Context Protocol) server for the LegionIO framework. Provides semantic tool matching, observation pipeline, context compilation, and tiered behavioral intelligence (Tier 0/1/2 routing).
4
+
5
+ Extracted from [LegionIO](https://github.com/LegionIO/LegionIO) for independent versioning and reuse.
6
+
7
+ ## Installation
8
+
9
+ ```ruby
10
+ gem 'legion-mcp'
11
+ ```
12
+
13
+ Or in a Gemfile:
14
+
15
+ ```ruby
16
+ gem 'legion-mcp', '~> 0.1'
17
+ ```
18
+
19
+ ## Architecture
20
+
21
+ ```
22
+ Legion::MCP
23
+ ├── Server # MCP::Server builder, TOOL_CLASSES registration
24
+ ├── Auth # JWT + API key authentication
25
+ ├── ToolGovernance # Risk-tier tool filtering + invocation audit
26
+ ├── ContextCompiler # Keyword + semantic tool matching (60/40 blend)
27
+ ├── EmbeddingIndex # In-memory vector cache for semantic matching
28
+ ├── Observer # Instrumentation: counters, ring buffer, pattern promotion
29
+ ├── UsageFilter # Frequency/recency/keyword scoring for dynamic tool filtering
30
+ ├── PatternStore # 4-layer degrading storage (L0 memory → L1 cache → L2 SQLite)
31
+ ├── TierRouter # Confidence-gated tier selection (0/1/2)
32
+ ├── ContextGuard # Staleness, rapid-fire, anomaly detection
33
+ ├── Tools/ # 35 MCP::Tool subclasses (legion.* namespace)
34
+ └── Resources/ # RunnerCatalog, ExtensionInfo
35
+ ```
36
+
37
+ ## Tiered Behavioral Intelligence
38
+
39
+ Requests flow through three tiers, each with increasing latency and capability:
40
+
41
+ | Tier | Confidence | What happens | Latency |
42
+ |------|-----------|--------------|---------|
43
+ | 0 | >= 0.8 | Cached pattern match, no LLM | < 5ms |
44
+ | 1 | 0.6 - 0.8 | Local/fleet model hint | ~100ms |
45
+ | 2 | < 0.6 | Full cloud LLM | ~1-3s |
46
+
47
+ ### Pattern Lifecycle
48
+
49
+ 1. **Observer** records intent+tool pairs from MCP tool invocations
50
+ 2. After 3 successful observations, promotes candidate to **PatternStore** (seeded confidence 0.5)
51
+ 3. Successful executions increase confidence (+0.02), failures decrease it (-0.05)
52
+ 4. Once confidence reaches 0.8, **TierRouter** serves the pattern at Tier 0
53
+
54
+ ### Context Guards
55
+
56
+ Before serving a Tier 0 response, **ContextGuard** checks:
57
+
58
+ - **Staleness**: Pattern not hit in > 1 hour
59
+ - **Rapid-fire**: > 5 requests in 10 minutes (possible loop)
60
+ - **Anomaly**: 2+ consecutive misses (pattern may be stale)
61
+
62
+ If any guard triggers, the request escalates to Tier 1.
63
+
64
+ ### Storage Layers
65
+
66
+ PatternStore degrades gracefully across 4 layers:
67
+
68
+ | Layer | Backend | Requirement |
69
+ |-------|---------|-------------|
70
+ | L0 | In-memory hash | Always available |
71
+ | L1 | Legion::Cache (memcached/redis) | `defined?(Legion::Cache)` |
72
+ | L2 | Legion::Data::Local (SQLite) | `defined?(Legion::Data::Local)` |
73
+
74
+ All persistence wraps in `begin/rescue => nil` — failed writes never block Tier 0.
75
+
76
+ ## Tools
77
+
78
+ 35 MCP tools in the `legion.*` namespace:
79
+
80
+ | Tool | Purpose |
81
+ |------|---------|
82
+ | `legion.do` | Natural language intent routing (Tier 0 fast path) |
83
+ | `legion.discover_tools` | Dynamic tool discovery with context |
84
+ | `legion.run_task` | Execute a runner function via dot notation |
85
+ | `legion.describe_runner` | Runner/function discovery |
86
+ | `legion.list_tasks` / `get_task` / `delete_task` | Task CRUD |
87
+ | `legion.get_task_logs` | Task execution logs |
88
+ | `legion.list_chains` / `create_chain` / `update_chain` / `delete_chain` | Chain management |
89
+ | `legion.list_relationships` / `create_relationship` / `update_relationship` / `delete_relationship` | Task relationships |
90
+ | `legion.list_extensions` / `get_extension` / `enable_extension` / `disable_extension` | Extension management |
91
+ | `legion.list_schedules` / `create_schedule` / `update_schedule` / `delete_schedule` | Schedule CRUD |
92
+ | `legion.get_status` / `get_config` | System introspection |
93
+ | `legion.list_workers` / `show_worker` / `worker_lifecycle` / `worker_costs` | Worker management |
94
+ | `legion.team_summary` / `routing_stats` | Team and routing metrics |
95
+ | `legion.rbac_assignments` / `rbac_check` / `rbac_grants` | Access control |
96
+
97
+ ## Resources
98
+
99
+ | URI | Description |
100
+ |-----|-------------|
101
+ | `legion://runners` | All registered extension.runner.function paths |
102
+ | `legion://extensions/{name}` | Extension detail template |
103
+
104
+ ## Usage
105
+
106
+ ### Standalone MCP server
107
+
108
+ ```ruby
109
+ require 'legion/mcp'
110
+
111
+ server = Legion::MCP.server
112
+ # Start via stdio transport
113
+ server.start
114
+ ```
115
+
116
+ ### Within LegionIO
117
+
118
+ ```bash
119
+ # stdio transport (default)
120
+ legionio mcp stdio
121
+
122
+ # HTTP transport
123
+ legionio mcp http --port 9393
124
+ ```
125
+
126
+ ### Tier 0 direct usage
127
+
128
+ ```ruby
129
+ result = Legion::MCP::TierRouter.route(
130
+ intent: "list all running tasks",
131
+ params: { status: "running" },
132
+ context: {}
133
+ )
134
+
135
+ case result[:tier]
136
+ when 0 then puts "Cached: #{result[:response]}"
137
+ when 1 then puts "Escalate to local model"
138
+ when 2 then puts "Escalate to cloud LLM"
139
+ end
140
+ ```
141
+
142
+ ## Configuration
143
+
144
+ All configuration is optional and read via `Legion::Settings` when available:
145
+
146
+ ```json
147
+ {
148
+ "mcp": {
149
+ "context_guard": {
150
+ "max_stale_seconds": 3600,
151
+ "rapid_fire_threshold": 5,
152
+ "rapid_fire_window_secs": 600,
153
+ "anomaly_miss_threshold": 2
154
+ }
155
+ }
156
+ }
157
+ ```
158
+
159
+ ## Dependencies
160
+
161
+ | Gem | Required | Purpose |
162
+ |-----|----------|---------|
163
+ | `mcp` (~> 0.8) | Yes | MCP server SDK |
164
+ | `legion-data` (>= 1.4) | Yes | Sequel models, migrations |
165
+ | `legion-json` (>= 1.2) | Yes | JSON serialization |
166
+ | `legion-logging` (>= 0.3) | Yes | Logging |
167
+ | `legion-settings` (>= 0.3) | Yes | Configuration |
168
+ | `legion-cache` | Optional | L1 pattern cache |
169
+ | `legion-llm` | Optional | Embeddings for semantic matching |
170
+
171
+ ## Development
172
+
173
+ ```bash
174
+ bundle install
175
+ bundle exec rspec # 278 examples, 0 failures
176
+ bundle exec rubocop -A # auto-fix
177
+ bundle exec rubocop # lint check
178
+ ```
179
+
180
+ ## License
181
+
182
+ Apache-2.0
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/legion/mcp/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'legion-mcp'
7
+ spec.version = Legion::MCP::VERSION
8
+ spec.authors = ['Esity']
9
+ spec.email = ['matthewdiverson@gmail.com']
10
+
11
+ spec.summary = 'MCP server for the LegionIO framework'
12
+ spec.description = 'Model Context Protocol server with semantic tool matching, observation pipeline, and tiered inference for LegionIO'
13
+ spec.homepage = 'https://github.com/LegionIO/legion-mcp'
14
+ spec.license = 'Apache-2.0'
15
+ spec.required_ruby_version = '>= 3.4'
16
+ spec.require_paths = ['lib']
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.extra_rdoc_files = %w[README.md LICENSE CHANGELOG.md]
19
+ spec.metadata = {
20
+ 'bug_tracker_uri' => 'https://github.com/LegionIO/legion-mcp/issues',
21
+ 'changelog_uri' => 'https://github.com/LegionIO/legion-mcp/blob/main/CHANGELOG.md',
22
+ 'documentation_uri' => 'https://github.com/LegionIO/legion-mcp',
23
+ 'homepage_uri' => 'https://github.com/LegionIO/LegionIO',
24
+ 'source_code_uri' => 'https://github.com/LegionIO/legion-mcp',
25
+ 'wiki_uri' => 'https://github.com/LegionIO/legion-mcp/wiki',
26
+ 'rubygems_mfa_required' => 'true'
27
+ }
28
+
29
+ spec.add_dependency 'concurrent-ruby', '>= 1.2'
30
+ spec.add_dependency 'mcp', '~> 0.8'
31
+ spec.add_dependency 'legion-data', '>= 1.4'
32
+ spec.add_dependency 'legion-json', '>= 1.2'
33
+ spec.add_dependency 'legion-logging', '>= 0.3'
34
+ spec.add_dependency 'legion-settings', '>= 0.3'
35
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module MCP
5
+ module Auth
6
+ module_function
7
+
8
+ def authenticate(token)
9
+ return { authenticated: false, error: 'missing_token' } unless token
10
+
11
+ if jwt_token?(token)
12
+ verify_jwt(token)
13
+ else
14
+ verify_api_key(token)
15
+ end
16
+ end
17
+
18
+ def auth_enabled?
19
+ Legion::Settings.dig(:mcp, :auth, :enabled) == true
20
+ end
21
+
22
+ def require_auth?
23
+ Legion::Settings.dig(:mcp, :auth, :require_auth) == true
24
+ end
25
+
26
+ def jwt_token?(token)
27
+ token.count('.') == 2
28
+ end
29
+
30
+ def verify_jwt(token)
31
+ return { authenticated: false, error: 'crypt_unavailable' } unless defined?(Legion::Crypt::JWT)
32
+
33
+ claims = Legion::Crypt::JWT.decode(token)
34
+ { authenticated: true, identity: { user_id: claims[:sub], risk_tier: claims[:risk_tier]&.to_sym,
35
+ tenant_id: claims[:tenant_id], worker_id: claims[:worker_id] } }
36
+ rescue StandardError => e
37
+ { authenticated: false, error: e.message }
38
+ end
39
+
40
+ def verify_api_key(token)
41
+ allowed = Legion::Settings.dig(:mcp, :auth, :allowed_api_keys) || []
42
+ if allowed.include?(token)
43
+ { authenticated: true, identity: { user_id: 'api_key', risk_tier: :low } }
44
+ else
45
+ { authenticated: false, error: 'invalid_api_key' }
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end