klee 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b40d9febee207cdff655e91d94f0f719188454fa98592a5cb9fa7b54521ed0c7
4
- data.tar.gz: 701294ca8f65152b881f6492caa4e51979e4de81dec22735989829ae87b8a015
3
+ metadata.gz: 6ad4060f69da403186a89fb198ddfdb917606eac5e3344dcb5b0cc58f3d1bdd8
4
+ data.tar.gz: 90b540e449684b48e71714194b3c622f9fd35ecae931cb4b94fb53545f338958
5
5
  SHA512:
6
- metadata.gz: 57dc53e9ff93189272b0a5ad5d203e9921f21376c1142db3f37d5b1074808dd92f57b4b2bc2181e43f4ca0df64a9c75e467f40efaf61b0b0bc380e4aa1830a7a
7
- data.tar.gz: 07a3b7c9588044077452430f1ebb9dba9a5f23443d141fcb7bb6b6b2ec4f32182cd1cd024178f9b87850aacdabe1d778ec9dc9f18d8ddd972ccaafa40993365f
6
+ metadata.gz: 46ed4f0a4469aee13b165138f3ff04da7b30819e6e37b01524e750df591a52cf6da53dc2f082a8c353a1bfa6a47e37111814baf8bbcdd377f246797ea12ec283
7
+ data.tar.gz: 77af3a015eb3e3fd8267b78525e46f53bb51694b2160d12a04e05bf6c04734549ed628a9cb5a8955cf4c9d6d4ec3ac1a544b011556125fdd5d07d2e1b6b71037
data/CHANGELOG.md CHANGED
@@ -5,6 +5,22 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/)
6
6
  and this project adheres to [Semantic Versioning](http://semver.org/).
7
7
 
8
+ ## [0.1.2] - 2026-02-01
9
+
10
+ ### Added
11
+
12
+ - MCP server support via klee-mcp executable (requires gem install mcp) (08c2294)
13
+ - discover_vocabulary tool for extracting domain language (08c2294)
14
+ - find_concept_clusters tool for identifying related concepts (08c2294)
15
+ - explore_concept tool for deep-diving into specific concepts (08c2294)
16
+ - find_collaborators tool for finding co-occurring objects (08c2294)
17
+ - check_naming_consistency tool for detecting naming deviations (08c2294)
18
+ - codebase_summary tool for high-level vocabulary overview (08c2294)
19
+
20
+ ### Changed
21
+
22
+ - README now includes MCP server setup and available tools (f84bdc5)
23
+
8
24
  ## [0.1.1] - 2026-01-29
9
25
 
10
26
  ### Changed
@@ -23,9 +39,3 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
23
39
  - FileAnalyzer using Prism AST to extract classes, methods, and collaborators (3e509a1)
24
40
  - Configurable threshold and ignore list for filtering noise (3e509a1)
25
41
  - Method-level scope option for tighter coupling analysis (3e509a1)
26
-
27
- ## [0.1.0] - 2022-05-01
28
-
29
- ### Added
30
-
31
- - Initial release
data/README.md CHANGED
@@ -101,6 +101,38 @@ codebase.collaborators.clusters
101
101
  # => [Set["user", "account", "session"], Set["order", "payment", "invoice"]]
102
102
  ```
103
103
 
104
+ ### MCP Server for Claude Code
105
+
106
+ Expose klee's vocabulary analysis to Claude Code or other MCP clients.
107
+
108
+ ```bash
109
+ # Install the mcp gem (optional dependency)
110
+ gem install mcp
111
+
112
+ # Run the server
113
+ klee-mcp
114
+ ```
115
+
116
+ Configure Claude Code in `~/.claude.json`:
117
+
118
+ ```json
119
+ {
120
+ "mcpServers": {
121
+ "klee": {
122
+ "command": "klee-mcp"
123
+ }
124
+ }
125
+ }
126
+ ```
127
+
128
+ Available tools:
129
+ - `discover_vocabulary` - Extract domain language from a codebase
130
+ - `find_concept_clusters` - Identify groups of related concepts
131
+ - `explore_concept` - Deep-dive into a specific concept
132
+ - `find_collaborators` - Find objects that work together
133
+ - `check_naming_consistency` - Detect naming pattern deviations
134
+ - `codebase_summary` - High-level vocabulary overview
135
+
104
136
  ## Development
105
137
 
106
138
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/exe/klee-mcp ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ begin
5
+ require "mcp"
6
+ rescue LoadError
7
+ warn <<~ERROR
8
+ klee-mcp requires the 'mcp' gem for MCP server support.
9
+
10
+ Install it with:
11
+ gem install mcp
12
+
13
+ Then run klee-mcp again.
14
+ ERROR
15
+ exit 1
16
+ end
17
+
18
+ require "klee"
19
+ require "klee/mcp"
20
+
21
+ Klee::MCP::Server.new.run
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klee
4
+ class Codebase
5
+ def initialize(*patterns, ignore: [], threshold: 2)
6
+ @patterns = patterns
7
+ @ignore = ignore
8
+ @threshold = threshold
9
+ end
10
+
11
+ def concepts
12
+ @concepts ||= build_concept_index
13
+ end
14
+
15
+ def collaborators
16
+ @collaborators ||= build_collaborator_index
17
+ end
18
+
19
+ private
20
+
21
+ def files
22
+ @patterns.flat_map { |p| Dir.glob(p) }
23
+ .uniq
24
+ .select { |f| File.file?(f) && f.end_with?(".rb") }
25
+ end
26
+
27
+ def analyzers
28
+ @analyzers ||= files.map { |path| FileAnalyzer.new(path) }
29
+ end
30
+
31
+ def build_concept_index
32
+ index = ConceptIndex.new(ignore: @ignore, threshold: @threshold)
33
+ analyzers.each { |a| index.add(a) }
34
+ index
35
+ end
36
+
37
+ def build_collaborator_index
38
+ index = CollaboratorIndex.new(ignore: @ignore, threshold: @threshold)
39
+ analyzers.each { |a| index.add(a) }
40
+ index
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klee
4
+ class CollaboratorIndex
5
+ def initialize(ignore: [], threshold: 2)
6
+ @ignore = Set.new(ignore.map(&:to_s))
7
+ @threshold = threshold
8
+ @file_pairs = Hash.new(0)
9
+ @method_pairs = Hash.new(0)
10
+ end
11
+
12
+ def add(file_analyzer)
13
+ # File-level pairs
14
+ collabs = filter(file_analyzer.collaborators.uniq)
15
+ each_pair(collabs) { |pair| @file_pairs[pair] += 1 }
16
+
17
+ # Method-level pairs
18
+ file_analyzer.method_collaborators.each_value do |method_collabs|
19
+ filtered = filter(method_collabs.uniq)
20
+ each_pair(filtered) { |pair| @method_pairs[pair] += 1 }
21
+ end
22
+ end
23
+
24
+ def pairs(scope: :file)
25
+ source = (scope == :method) ? @method_pairs : @file_pairs
26
+ source.select { |_, count| count >= @threshold }
27
+ .sort_by { |_, count| -count }.to_h
28
+ end
29
+
30
+ def for(collaborator)
31
+ name = collaborator.to_s
32
+ pairs.select { |pair, _| pair.include?(name) }
33
+ .transform_keys { |pair| (pair - [name]).first }
34
+ end
35
+
36
+ def clusters
37
+ build_clusters(pairs)
38
+ end
39
+
40
+ private
41
+
42
+ def filter(names)
43
+ names.map(&:to_s).reject { |n| @ignore.include?(n) }
44
+ end
45
+
46
+ def each_pair(items)
47
+ items.combination(2).each { |pair| yield pair.sort }
48
+ end
49
+
50
+ def build_clusters(pair_data)
51
+ graph = Hash.new { |h, k| h[k] = Set.new }
52
+ pair_data.each_key do |(a, b)|
53
+ graph[a] << b
54
+ graph[b] << a
55
+ end
56
+
57
+ visited = Set.new
58
+ clusters = []
59
+
60
+ graph.each_key do |node|
61
+ next if visited.include?(node)
62
+
63
+ cluster = Set.new
64
+ stack = [node]
65
+
66
+ while stack.any?
67
+ current = stack.pop
68
+ next if visited.include?(current)
69
+
70
+ visited << current
71
+ cluster << current
72
+ stack.concat(graph[current].to_a)
73
+ end
74
+
75
+ clusters << cluster if cluster.size > 1
76
+ end
77
+
78
+ clusters.sort_by { |c| -c.size }
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ module Klee
6
+ class Collaborators
7
+ def initialize(const)
8
+ @const = const
9
+ end
10
+ attr_reader :const
11
+
12
+ def lexed
13
+ Prism.lex_file(Object.const_source_location(const.name).first)
14
+ end
15
+
16
+ def additional_processing_types = [:DOT, :BRACKET_LEFT]
17
+
18
+ def identifier_with_additional_processing
19
+ lexed.value.each_cons(2).filter_map do |(token, next_token)|
20
+ if token.first.type == :IDENTIFIER &&
21
+ additional_processing_types.include?(next_token.first.type)
22
+ token.first.value
23
+ end
24
+ end
25
+ end
26
+
27
+ def tally
28
+ identifier_with_additional_processing.tally
29
+ end
30
+
31
+ def rank
32
+ tally.group_by do |_, count|
33
+ count
34
+ end.sort.reverse.to_h.transform_values do |value|
35
+ value.map(&:first)
36
+ end.reject do |key, value|
37
+ value.empty? || key < 3
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klee
4
+ class ConceptIndex
5
+ include Enumerable
6
+
7
+ def initialize(ignore: [], threshold: 2)
8
+ @ignore = Set.new(ignore.map(&:to_s))
9
+ @threshold = threshold
10
+ @data = Hash.new { |h, k| h[k] = {classes: Set.new, methods: Set.new} }
11
+ end
12
+
13
+ def add(file_analyzer)
14
+ file_analyzer.class_names.each do |name|
15
+ words_from(name).each { |word| @data[word][:classes] << name }
16
+ end
17
+
18
+ file_analyzer.method_names.each do |name|
19
+ words_from(name).each { |word| @data[word][:methods] << name }
20
+ end
21
+ end
22
+
23
+ def [](concept)
24
+ @data[concept.to_s]
25
+ end
26
+
27
+ def each(&block)
28
+ filtered.each(&block)
29
+ end
30
+
31
+ def rank
32
+ filtered.sort_by { |word, locs| -(locs[:classes].size + locs[:methods].size) }.to_h
33
+ end
34
+
35
+ private
36
+
37
+ def words_from(name)
38
+ name.to_s.gsub(/([a-z])([A-Z])/, '\1_\2')
39
+ .downcase.split("_")
40
+ .reject { |w| w.empty? || @ignore.include?(w) }
41
+ end
42
+
43
+ def filtered
44
+ @data.select { |_, locs| (locs[:classes].size + locs[:methods].size) >= @threshold }
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ module Klee
6
+ class FileAnalyzer
7
+ attr_reader :path, :class_names, :method_names, :collaborators, :method_collaborators
8
+
9
+ def initialize(path)
10
+ @path = path
11
+ @class_names = []
12
+ @method_names = []
13
+ @collaborators = []
14
+ @method_collaborators = Hash.new { |h, k| h[k] = [] }
15
+ parse
16
+ end
17
+
18
+ private
19
+
20
+ def parse
21
+ result = Prism.parse_file(@path)
22
+ visit(result.value, current_method: nil)
23
+ end
24
+
25
+ def visit(node, current_method:)
26
+ case node
27
+ when Prism::ClassNode, Prism::ModuleNode
28
+ @class_names << node.name.to_s
29
+ when Prism::DefNode
30
+ @method_names << node.name.to_s
31
+ current_method = node.name.to_s
32
+ when Prism::CallNode
33
+ name = extract_collaborator(node)
34
+ if name
35
+ @collaborators << name
36
+ @method_collaborators[current_method] << name if current_method
37
+ end
38
+ end
39
+
40
+ node.child_nodes.compact.each { |child| visit(child, current_method: current_method) }
41
+ end
42
+
43
+ def extract_collaborator(call_node)
44
+ receiver = call_node.receiver
45
+ case receiver
46
+ when Prism::LocalVariableReadNode
47
+ receiver.name.to_s
48
+ when Prism::InstanceVariableReadNode
49
+ receiver.name.to_s.delete_prefix("@")
50
+ when Prism::CallNode
51
+ # Chain like user.account.name - extract the root
52
+ extract_collaborator(receiver)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klee
4
+ module MCP
5
+ class PathValidator
6
+ def initialize(allowed_roots: [Dir.pwd])
7
+ @allowed_roots = allowed_roots.map { |r| File.expand_path(r) }
8
+ end
9
+
10
+ def validate!(patterns)
11
+ patterns.each do |pattern|
12
+ expanded = expand_pattern_root(pattern)
13
+ unless allowed?(expanded)
14
+ raise SecurityError, "Pattern '#{pattern}' resolves to '#{expanded}' which is outside allowed roots: #{@allowed_roots.join(", ")}"
15
+ end
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def expand_pattern_root(pattern)
22
+ base = pattern.split(/[*?\[{]/).first || "."
23
+ base = "." if base.empty?
24
+ File.expand_path(base)
25
+ end
26
+
27
+ def allowed?(path)
28
+ @allowed_roots.any? { |root| path.start_with?(root) }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mcp"
4
+ require_relative "tools/discover_vocabulary"
5
+ require_relative "tools/find_concept_clusters"
6
+ require_relative "tools/explore_concept"
7
+ require_relative "tools/find_collaborators"
8
+ require_relative "tools/check_naming_consistency"
9
+ require_relative "tools/codebase_summary"
10
+
11
+ module Klee
12
+ module MCP
13
+ class Server
14
+ TOOLS = [
15
+ Tools::DiscoverVocabulary,
16
+ Tools::FindConceptClusters,
17
+ Tools::ExploreConcept,
18
+ Tools::FindCollaborators,
19
+ Tools::CheckNamingConsistency,
20
+ Tools::CodebaseSummary
21
+ ].freeze
22
+
23
+ def initialize
24
+ @server = ::MCP::Server.new(
25
+ name: "klee",
26
+ version: Klee::MCP::VERSION,
27
+ tools: TOOLS
28
+ )
29
+ end
30
+
31
+ def run
32
+ transport = ::MCP::Server::Transports::StdioTransport.new(@server)
33
+ transport.open
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,141 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klee
4
+ module MCP
5
+ module Tools
6
+ class CheckNamingConsistency < ::MCP::Tool
7
+ description "Find methods that deviate from established naming patterns. Identifies unusual names and suggests alternatives based on common conventions in the codebase."
8
+
9
+ input_schema(
10
+ properties: {
11
+ patterns: {
12
+ type: "array",
13
+ items: {type: "string"},
14
+ description: "Glob patterns to match Ruby files, e.g. ['app/**/*.rb']"
15
+ },
16
+ conventions: {
17
+ type: "object",
18
+ properties: {
19
+ prefixes: {
20
+ type: "array",
21
+ items: {type: "string"},
22
+ description: "Expected method prefixes, e.g. ['find_', 'create_', 'update_', 'delete_']"
23
+ },
24
+ suffixes: {
25
+ type: "array",
26
+ items: {type: "string"},
27
+ description: "Expected method suffixes, e.g. ['?', '!', '_at', '_by']"
28
+ }
29
+ },
30
+ description: "Naming conventions to check against"
31
+ },
32
+ threshold: {
33
+ type: "integer",
34
+ description: "Levenshtein distance threshold for similarity suggestions (default: 6)"
35
+ },
36
+ ignore: {
37
+ type: "array",
38
+ items: {type: "string"},
39
+ description: "Method names to ignore"
40
+ }
41
+ },
42
+ required: ["patterns"]
43
+ )
44
+
45
+ class << self
46
+ def call(patterns:, conventions: {}, threshold: 6, ignore: [], server_context: nil)
47
+ validator = Klee::MCP::PathValidator.new
48
+ validator.validate!(patterns)
49
+
50
+ codebase = Klee.scan(*patterns, ignore: ignore, threshold: 1)
51
+ all_methods = codebase.concepts.flat_map { |_, locs| locs[:methods].to_a }.uniq
52
+
53
+ prefixes = conventions["prefixes"] || conventions[:prefixes] || []
54
+ suffixes = conventions["suffixes"] || conventions[:suffixes] || []
55
+
56
+ conforming = {}
57
+ unusual = []
58
+
59
+ prefixes.each do |prefix|
60
+ matched = all_methods.select { |m| m.start_with?(prefix) }
61
+ conforming["#{prefix}*"] = matched.sort if matched.any?
62
+ end
63
+
64
+ suffixes.each do |suffix|
65
+ matched = all_methods.select { |m| m.end_with?(suffix) }
66
+ conforming["*#{suffix}"] = matched.sort if matched.any?
67
+ end
68
+
69
+ conforming_methods = conforming.values.flatten.uniq
70
+ non_conforming = all_methods - conforming_methods
71
+
72
+ non_conforming.each do |method|
73
+ suggestion = find_similar(method, conforming_methods, threshold)
74
+ unusual << if suggestion
75
+ {
76
+ method: method,
77
+ suggestion: suggestion[:name],
78
+ similarity: suggestion[:distance]
79
+ }
80
+ else
81
+ {method: method, suggestion: nil, similarity: nil}
82
+ end
83
+ end
84
+
85
+ unusual.sort_by! { |u| u[:similarity] || Float::INFINITY }
86
+
87
+ result = {
88
+ conforming: conforming,
89
+ unusual: unusual.first(50)
90
+ }
91
+
92
+ ::MCP::Tool::Response.new([{
93
+ type: "text",
94
+ text: JSON.pretty_generate(result)
95
+ }])
96
+ end
97
+
98
+ private
99
+
100
+ def find_similar(method, candidates, max_distance)
101
+ best = nil
102
+ best_distance = max_distance + 1
103
+
104
+ candidates.each do |candidate|
105
+ distance = levenshtein_distance(method, candidate)
106
+ if distance < best_distance
107
+ best = candidate
108
+ best_distance = distance
109
+ end
110
+ end
111
+
112
+ best ? {name: best, distance: best_distance} : nil
113
+ end
114
+
115
+ def levenshtein_distance(a, b)
116
+ return b.length if a.empty?
117
+ return a.length if b.empty?
118
+
119
+ matrix = Array.new(a.length + 1) { Array.new(b.length + 1) }
120
+
121
+ (0..a.length).each { |i| matrix[i][0] = i }
122
+ (0..b.length).each { |j| matrix[0][j] = j }
123
+
124
+ (1..a.length).each do |i|
125
+ (1..b.length).each do |j|
126
+ cost = (a[i - 1] == b[j - 1]) ? 0 : 1
127
+ matrix[i][j] = [
128
+ matrix[i - 1][j] + 1,
129
+ matrix[i][j - 1] + 1,
130
+ matrix[i - 1][j - 1] + cost
131
+ ].min
132
+ end
133
+ end
134
+
135
+ matrix[a.length][b.length]
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klee
4
+ module MCP
5
+ module Tools
6
+ class CodebaseSummary < ::MCP::Tool
7
+ description "Get a high-level vocabulary overview of a Ruby codebase for quick onboarding. Shows top domain concepts, concept clusters, and vocabulary metrics."
8
+
9
+ input_schema(
10
+ properties: {
11
+ patterns: {
12
+ type: "array",
13
+ items: {type: "string"},
14
+ description: "Glob patterns to match Ruby files, e.g. ['app/**/*.rb', 'lib/**/*.rb']"
15
+ },
16
+ threshold: {
17
+ type: "integer",
18
+ description: "Minimum occurrences for concepts (default: 2)"
19
+ },
20
+ ignore: {
21
+ type: "array",
22
+ items: {type: "string"},
23
+ description: "Words to ignore"
24
+ }
25
+ },
26
+ required: ["patterns"]
27
+ )
28
+
29
+ class << self
30
+ def call(patterns:, threshold: 2, ignore: [], server_context: nil)
31
+ validator = Klee::MCP::PathValidator.new
32
+ validator.validate!(patterns)
33
+
34
+ codebase = Klee.scan(*patterns, ignore: ignore, threshold: threshold)
35
+
36
+ ranked = codebase.concepts.rank
37
+ top_concepts = ranked.first(15).map(&:first)
38
+
39
+ clusters = codebase.collaborators.clusters
40
+
41
+ total_classes = Set.new
42
+ total_methods = Set.new
43
+ total_identifiers = 0
44
+
45
+ codebase.concepts.each do |_, locs|
46
+ total_classes.merge(locs[:classes])
47
+ total_methods.merge(locs[:methods])
48
+ total_identifiers += locs[:classes].size + locs[:methods].size
49
+ end
50
+
51
+ unique_concepts = ranked.keys.size
52
+ vocabulary_richness = total_identifiers.zero? ? 0 : (unique_concepts.to_f / total_identifiers).round(2)
53
+
54
+ result = {
55
+ top_concepts: top_concepts,
56
+ concept_clusters: clusters.size,
57
+ total_classes: total_classes.size,
58
+ total_methods: total_methods.size,
59
+ vocabulary_richness: vocabulary_richness,
60
+ cluster_preview: clusters.first(3).map { |c| c.to_a.sort }
61
+ }
62
+
63
+ ::MCP::Tool::Response.new([{
64
+ type: "text",
65
+ text: JSON.pretty_generate(result)
66
+ }])
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klee
4
+ module MCP
5
+ module Tools
6
+ class DiscoverVocabulary < ::MCP::Tool
7
+ description "Extract the domain language vocabulary from a Ruby codebase. Returns words that appear frequently in class and method names, revealing the key domain concepts."
8
+
9
+ input_schema(
10
+ properties: {
11
+ patterns: {
12
+ type: "array",
13
+ items: {type: "string"},
14
+ description: "Glob patterns to match Ruby files, e.g. ['app/**/*.rb', 'lib/**/*.rb']"
15
+ },
16
+ threshold: {
17
+ type: "integer",
18
+ description: "Minimum occurrences for a word to be included (default: 3)"
19
+ },
20
+ limit: {
21
+ type: "integer",
22
+ description: "Maximum number of vocabulary terms to return (default: 30)"
23
+ },
24
+ ignore: {
25
+ type: "array",
26
+ items: {type: "string"},
27
+ description: "Words to ignore, e.g. common terms like 'get', 'set', 'new'"
28
+ }
29
+ },
30
+ required: ["patterns"]
31
+ )
32
+
33
+ class << self
34
+ def call(patterns:, threshold: 3, limit: 30, ignore: [], server_context: nil)
35
+ validator = Klee::MCP::PathValidator.new
36
+ validator.validate!(patterns)
37
+
38
+ codebase = Klee.scan(*patterns, ignore: ignore, threshold: threshold)
39
+ ranked = codebase.concepts.rank
40
+
41
+ vocabulary = ranked.first(limit).map do |word, locations|
42
+ {
43
+ word: word,
44
+ frequency: locations[:classes].size + locations[:methods].size,
45
+ in_classes: locations[:classes].to_a,
46
+ in_methods: locations[:methods].to_a
47
+ }
48
+ end
49
+
50
+ ::MCP::Tool::Response.new([{
51
+ type: "text",
52
+ text: JSON.pretty_generate({vocabulary: vocabulary})
53
+ }])
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klee
4
+ module MCP
5
+ module Tools
6
+ class ExploreConcept < ::MCP::Tool
7
+ description "Deep-dive into a specific domain concept to see everywhere it appears in the codebase, what it co-occurs with, and naming patterns used."
8
+
9
+ input_schema(
10
+ properties: {
11
+ patterns: {
12
+ type: "array",
13
+ items: {type: "string"},
14
+ description: "Glob patterns to match Ruby files, e.g. ['app/**/*.rb']"
15
+ },
16
+ concept: {
17
+ type: "string",
18
+ description: "The domain concept word to explore, e.g. 'subscription', 'user', 'order'"
19
+ },
20
+ threshold: {
21
+ type: "integer",
22
+ description: "Minimum occurrences threshold (default: 1)"
23
+ },
24
+ ignore: {
25
+ type: "array",
26
+ items: {type: "string"},
27
+ description: "Words to ignore"
28
+ }
29
+ },
30
+ required: ["patterns", "concept"]
31
+ )
32
+
33
+ class << self
34
+ def call(patterns:, concept:, threshold: 1, ignore: [], server_context: nil)
35
+ validator = Klee::MCP::PathValidator.new
36
+ validator.validate!(patterns)
37
+
38
+ codebase = Klee.scan(*patterns, ignore: ignore, threshold: threshold)
39
+ concept_data = codebase.concepts[concept]
40
+
41
+ collaborator_pairs = codebase.collaborators.for(concept)
42
+ co_occurs_with = collaborator_pairs.keys.sort_by { |k| -collaborator_pairs[k] }
43
+
44
+ methods = concept_data[:methods].to_a
45
+ naming_patterns = methods.group_by { |m| extract_pattern(m, concept) }
46
+ .transform_values(&:sort)
47
+
48
+ result = {
49
+ concept: concept,
50
+ appears_in: {
51
+ classes: concept_data[:classes].to_a.sort,
52
+ methods: methods.sort
53
+ },
54
+ co_occurs_with: co_occurs_with,
55
+ naming_patterns: naming_patterns
56
+ }
57
+
58
+ ::MCP::Tool::Response.new([{
59
+ type: "text",
60
+ text: JSON.pretty_generate(result)
61
+ }])
62
+ end
63
+
64
+ private
65
+
66
+ def extract_pattern(method_name, concept)
67
+ case method_name
68
+ when /^#{concept}_/ then "#{concept}_*"
69
+ when /_#{concept}$/ then "*_#{concept}"
70
+ when /_#{concept}_/ then "*_#{concept}_*"
71
+ when /#{concept}\?$/ then "#{concept}?"
72
+ when /#{concept}!$/ then "#{concept}!"
73
+ else "other"
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klee
4
+ module MCP
5
+ module Tools
6
+ class FindCollaborators < ::MCP::Tool
7
+ description "Discover which objects conceptually belong together by analyzing co-occurrence patterns. Shows what a given object typically works with."
8
+
9
+ input_schema(
10
+ properties: {
11
+ patterns: {
12
+ type: "array",
13
+ items: {type: "string"},
14
+ description: "Glob patterns to match Ruby files, e.g. ['app/**/*.rb']"
15
+ },
16
+ object: {
17
+ type: "string",
18
+ description: "The object/variable name to find collaborators for, e.g. 'User', 'order', 'cart'"
19
+ },
20
+ threshold: {
21
+ type: "integer",
22
+ description: "Minimum co-occurrences to be considered a collaborator (default: 2)"
23
+ },
24
+ scope: {
25
+ type: "string",
26
+ enum: ["file", "method"],
27
+ description: "Scope for finding collaborators: 'file' (default) or 'method' level"
28
+ },
29
+ ignore: {
30
+ type: "array",
31
+ items: {type: "string"},
32
+ description: "Object names to ignore"
33
+ }
34
+ },
35
+ required: ["patterns", "object"]
36
+ )
37
+
38
+ class << self
39
+ def call(patterns:, object:, threshold: 2, scope: "file", ignore: [], server_context: nil)
40
+ validator = Klee::MCP::PathValidator.new
41
+ validator.validate!(patterns)
42
+
43
+ codebase = Klee.scan(*patterns, ignore: ignore, threshold: threshold)
44
+ pairs = codebase.collaborators.pairs(scope: scope.to_sym)
45
+
46
+ relevant_pairs = pairs.select { |pair, _| pair.include?(object) }
47
+
48
+ collaborators = relevant_pairs.map do |pair, count|
49
+ other = (pair - [object]).first
50
+ {
51
+ name: other,
52
+ co_occurrences: count,
53
+ scope: scope
54
+ }
55
+ end.sort_by { |c| -c[:co_occurrences] }
56
+
57
+ result = {
58
+ object: object,
59
+ collaborators: collaborators
60
+ }
61
+
62
+ ::MCP::Tool::Response.new([{
63
+ type: "text",
64
+ text: JSON.pretty_generate(result)
65
+ }])
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klee
4
+ module MCP
5
+ module Tools
6
+ class FindConceptClusters < ::MCP::Tool
7
+ description "Identify groups of domain concepts that frequently appear together in code, suggesting logical module boundaries or related functionality."
8
+
9
+ input_schema(
10
+ properties: {
11
+ patterns: {
12
+ type: "array",
13
+ items: {type: "string"},
14
+ description: "Glob patterns to match Ruby files, e.g. ['app/**/*.rb']"
15
+ },
16
+ threshold: {
17
+ type: "integer",
18
+ description: "Minimum co-occurrences for concepts to be considered related (default: 2)"
19
+ },
20
+ ignore: {
21
+ type: "array",
22
+ items: {type: "string"},
23
+ description: "Collaborator names to ignore"
24
+ }
25
+ },
26
+ required: ["patterns"]
27
+ )
28
+
29
+ class << self
30
+ def call(patterns:, threshold: 2, ignore: [], server_context: nil)
31
+ validator = Klee::MCP::PathValidator.new
32
+ validator.validate!(patterns)
33
+
34
+ codebase = Klee.scan(*patterns, ignore: ignore, threshold: threshold)
35
+ raw_clusters = codebase.collaborators.clusters
36
+
37
+ clusters = raw_clusters.map do |cluster_set|
38
+ {
39
+ concepts: cluster_set.to_a.sort,
40
+ size: cluster_set.size
41
+ }
42
+ end
43
+
44
+ ::MCP::Tool::Response.new([{
45
+ type: "text",
46
+ text: JSON.pretty_generate({clusters: clusters})
47
+ }])
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klee
4
+ module MCP
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
data/lib/klee/mcp.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "mcp/version"
4
+ require_relative "mcp/path_validator"
5
+ require_relative "mcp/server"
6
+
7
+ module Klee
8
+ module MCP
9
+ end
10
+ end
data/lib/klee/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Klee
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
data/sig/klee.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Klee
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klee
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Gay
@@ -29,6 +29,7 @@ email:
29
29
  - jim@saturnflyer.com
30
30
  executables:
31
31
  - klee
32
+ - klee-mcp
32
33
  extensions: []
33
34
  extra_rdoc_files: []
34
35
  files:
@@ -39,11 +40,28 @@ files:
39
40
  - bin/console
40
41
  - bin/setup
41
42
  - exe/klee
43
+ - exe/klee-mcp
42
44
  - lib/klee.rb
45
+ - lib/klee/codebase.rb
46
+ - lib/klee/collaborator_index.rb
47
+ - lib/klee/collaborators.rb
48
+ - lib/klee/concept_index.rb
43
49
  - lib/klee/concepts.rb
50
+ - lib/klee/file_analyzer.rb
44
51
  - lib/klee/gestalt.rb
52
+ - lib/klee/mcp.rb
53
+ - lib/klee/mcp/path_validator.rb
54
+ - lib/klee/mcp/server.rb
55
+ - lib/klee/mcp/tools/check_naming_consistency.rb
56
+ - lib/klee/mcp/tools/codebase_summary.rb
57
+ - lib/klee/mcp/tools/discover_vocabulary.rb
58
+ - lib/klee/mcp/tools/explore_concept.rb
59
+ - lib/klee/mcp/tools/find_collaborators.rb
60
+ - lib/klee/mcp/tools/find_concept_clusters.rb
61
+ - lib/klee/mcp/version.rb
45
62
  - lib/klee/patterns.rb
46
63
  - lib/klee/version.rb
64
+ - sig/klee.rbs
47
65
  homepage: https://github.com/saturnflyer/klee
48
66
  licenses:
49
67
  - MIT
@@ -51,6 +69,13 @@ metadata:
51
69
  homepage_uri: https://github.com/saturnflyer/klee
52
70
  source_code_uri: https://github.com/saturnflyer/klee
53
71
  changelog_uri: https://github.com/saturnflyer/klee/blob/main/CHANGELOG.md
72
+ post_install_message: |
73
+ klee installed successfully!
74
+
75
+ For MCP server support (Claude Code integration), also install:
76
+ gem install mcp
77
+
78
+ Then run: klee-mcp
54
79
  rdoc_options: []
55
80
  require_paths:
56
81
  - lib