archsight 0.5.2 → 0.5.4

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 (76) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -0
  3. data/docs/archimate.md +1 -1
  4. data/docs/configuration.md +1 -1
  5. data/docs/diagram.md +12 -2
  6. data/docs/import.md +27 -2
  7. data/docs/modeling.md +17 -6
  8. data/docs/pages.md +1 -1
  9. data/docs/search.md +19 -0
  10. data/lib/archsight/annotations/capacity_annotations.rb +15 -0
  11. data/lib/archsight/database.rb +1 -1
  12. data/lib/archsight/diagram/graph/attributes.rb +11 -3
  13. data/lib/archsight/diagram/graph/builder.rb +1 -0
  14. data/lib/archsight/diagram/graph/node.rb +26 -2
  15. data/lib/archsight/diagram/layout/expander.rb +39 -0
  16. data/lib/archsight/diagram/layout.rb +11 -0
  17. data/lib/archsight/diagram/legend/inventory.rb +6 -2
  18. data/lib/archsight/diagram/parser.rb +1 -1
  19. data/lib/archsight/diagram/renderer/node_renderer.rb +6 -1
  20. data/lib/archsight/diagram/renderer/stylesheet.rb +2 -0
  21. data/lib/archsight/diagram/style/representers.rb +14 -0
  22. data/lib/archsight/diagram/support/text_metrics.rb +38 -0
  23. data/lib/archsight/git_source.rb +5 -3
  24. data/lib/archsight/helpers.rb +3 -1
  25. data/lib/archsight/import/handlers/gitea.rb +195 -0
  26. data/lib/archsight/import/handlers/github.rb +1 -0
  27. data/lib/archsight/import/handlers/gitlab.rb +1 -0
  28. data/lib/archsight/import/handlers/repository.rb +32 -4
  29. data/lib/archsight/linter.rb +13 -2
  30. data/lib/archsight/mcp/analyze_resource_tool.rb +145 -43
  31. data/lib/archsight/mcp/base.rb +112 -5
  32. data/lib/archsight/mcp/execute_analysis_tool.rb +11 -1
  33. data/lib/archsight/mcp/query_hints.rb +119 -0
  34. data/lib/archsight/mcp/query_tool.rb +90 -5
  35. data/lib/archsight/mcp/relations_tool.rb +89 -0
  36. data/lib/archsight/mcp/resource_doc_tool.rb +39 -20
  37. data/lib/archsight/mcp/summary_tool.rb +106 -0
  38. data/lib/archsight/mcp.rb +2 -0
  39. data/lib/archsight/query/ast.rb +28 -0
  40. data/lib/archsight/query/evaluator.rb +2 -0
  41. data/lib/archsight/query/lexer.rb +2 -2
  42. data/lib/archsight/query/parser.rb +45 -0
  43. data/lib/archsight/query/text_search.rb +38 -0
  44. data/lib/archsight/query.rb +1 -0
  45. data/lib/archsight/reading_time.rb +66 -0
  46. data/lib/archsight/resources/base.rb +12 -0
  47. data/lib/archsight/resources/business_contract.rb +8 -1
  48. data/lib/archsight/resources/config.rb +28 -0
  49. data/lib/archsight/resources/implementation_work_package.rb +2 -0
  50. data/lib/archsight/resources/motivation_requirement.rb +10 -0
  51. data/lib/archsight/resources/technology_device.rb +14 -0
  52. data/lib/archsight/resources/technology_equipment.rb +3 -1
  53. data/lib/archsight/resources/technology_node.rb +8 -1
  54. data/lib/archsight/summary/builder.rb +204 -0
  55. data/lib/archsight/summary/lens.rb +224 -0
  56. data/lib/archsight/summary.rb +11 -0
  57. data/lib/archsight/version.rb +1 -1
  58. data/lib/archsight/web/api/openapi/spec.yaml +20 -1
  59. data/lib/archsight/web/api/page_helpers.rb +5 -1
  60. data/lib/archsight/web/application.rb +2 -0
  61. data/lib/archsight/web/public/vue/{DocPage-B2AVspsD.js → DocPage-BHffBQ3u.js} +1 -1
  62. data/lib/archsight/web/public/vue/{EditorPage-BYSZG_O0.js → EditorPage-BHZtvtnN.js} +1 -1
  63. data/lib/archsight/web/public/vue/{ErrorPage-eL_Woec4.js → ErrorPage-BQARAwF8.js} +1 -1
  64. data/lib/archsight/web/public/vue/{GraphView-YEiPBxPd.js → GraphView-D84CH00D.js} +1 -1
  65. data/lib/archsight/web/public/vue/{HomePage-BeLTwv2y.js → HomePage-BJpTPWFz.js} +2 -2
  66. data/lib/archsight/web/public/vue/{InstanceRouter-DgvdUouW.js → InstanceRouter-C1Da3nWo.js} +1 -1
  67. data/lib/archsight/web/public/vue/{KindList-BkYvYpnh.js → KindList-DNRUVuEJ.js} +1 -1
  68. data/lib/archsight/web/public/vue/PageView-DzKj212I.js +1 -0
  69. data/lib/archsight/web/public/vue/{QueryError-DzGMgvyb.js → QueryError-CTVAL1Tm.js} +1 -1
  70. data/lib/archsight/web/public/vue/{ResourceList-Cr9ExUDn.js → ResourceList-CRt_FpfS.js} +1 -1
  71. data/lib/archsight/web/public/vue/{SearchResults-D6tx3Atc.js → SearchResults-BMwBz4BB.js} +1 -1
  72. data/lib/archsight/web/public/vue/{WikiPage-BhaaveRq.js → WikiPage-DSEL2gmd.js} +1 -1
  73. data/lib/archsight/web/public/vue/{index-le1guGp1.js → index-JZkWwfYg.js} +2 -2
  74. data/lib/archsight/web/public/vue.html +1 -1
  75. metadata +24 -14
  76. data/lib/archsight/web/public/vue/PageView-B3SnDw9S.js +0 -1
@@ -6,10 +6,45 @@ require_relative "../resources"
6
6
 
7
7
  # Shared database and helper methods for MCP tools
8
8
  module Archsight::MCP
9
+ # Size budget of a tool response (characters of JSON). Larger results are cut down, with a pointer on how to
10
+ # narrow the request, because an agent cannot read a payload this size anyway.
11
+ MAX_RESPONSE_CHARS = 100_000
12
+
9
13
  class << self
10
14
  attr_accessor :db
11
15
 
12
- def complete_summary(resource, omit_kind: false)
16
+ # Pretty JSON of `result`; when it exceeds the budget, `shrink` (called with the attempt number, 1, 2, ...) must
17
+ # return a smaller result. Gives up after a few attempts with the last, smallest one.
18
+ def fit(result, max_chars: MAX_RESPONSE_CHARS, attempts: 8)
19
+ json = JSON.pretty_generate(result)
20
+ attempt = 0
21
+ while json.length > max_chars && attempt < attempts
22
+ attempt += 1
23
+ result = yield(attempt)
24
+ json = JSON.pretty_generate(result)
25
+ end
26
+ json
27
+ end
28
+
29
+ # The first sentence or line of a (markdown) description, at most `max` characters
30
+ def one_liner(text, max: 140)
31
+ line = text.to_s.strip.split(/\n\s*\n|\n/).first.to_s.strip
32
+ line = line[/\A.+?[.!?](?=\s|\z)/m] || line
33
+ line.length > max ? "#{line[0, max - 1].rstrip}…" : line
34
+ end
35
+
36
+ # `complete_summary` reduced to the given annotation keys and relation verbs (nil = everything)
37
+ def project(summary, fields)
38
+ return summary if fields.nil? || fields.empty?
39
+
40
+ wanted = fields.map(&:to_s)
41
+ summary = summary.dup
42
+ summary[:metadata] = { annotations: summary[:metadata][:annotations].slice(*wanted) } if summary[:metadata]
43
+ summary[:spec] = summary[:spec].select { |verb, _| wanted.include?(verb.to_s) } if summary[:spec]
44
+ summary
45
+ end
46
+
47
+ def complete_summary(resource, omit_kind: false, with_source: false)
13
48
  result = {
14
49
  name: resource.name,
15
50
  metadata: {
@@ -18,13 +53,85 @@ module Archsight::MCP
18
53
  spec: resource.spec
19
54
  }
20
55
  result[:kind] = resource.class.to_s.split("::").last unless omit_kind
21
- result
56
+ with_source ? result.merge(source_info(resource)) : result
22
57
  end
23
58
 
24
- def brief_summary(resource, omit_kind: false)
59
+ def brief_summary(resource, omit_kind: false, with_source: false)
25
60
  result = { name: resource.name }
26
- result[:kind] = resource.class.to_s.split("::").last unless omit_kind
27
- result
61
+ result[:kind] = kind_of(resource) unless omit_kind
62
+ with_source ? result.merge(source_info(resource)) : result
63
+ end
64
+
65
+ # Where the resource is defined: `source` ("path:line", relative to the resources root), `generated`
66
+ # (true = written by an importer, do not edit by hand) and, when several documents were merged into the
67
+ # resource, `sources` (every contributing file, hand-written ones included).
68
+ def source_info(resource)
69
+ refs = resource.source_refs
70
+ info = { source: format_ref(refs.first), generated: resource.generated? }
71
+ info[:sources] = refs.map { |ref| format_ref(ref) } if refs.length > 1
72
+ info
73
+ end
74
+
75
+ # The direct relations of a resource, both ways, as [{verb:, direction: "out"|"in", kind:, name:}]
76
+ # ("out": the resource points at the target, "in": the other resource points at it)
77
+ def direct_relations(instance)
78
+ outgoing = instance.relations_grouped.flat_map do |verb, kinds|
79
+ kinds.flat_map do |kind, targets|
80
+ targets.map { |target| { verb: verb.to_s, direction: "out", kind: kind, name: target.name } }
81
+ end
82
+ end
83
+ incoming = instance.references.map do |ref|
84
+ { verb: ref[:verb].to_s, direction: "in", kind: kind_of(ref[:instance]), name: ref[:instance].name }
85
+ end
86
+ (outgoing + incoming).uniq
87
+ end
88
+
89
+ # Up to `limit` resources whose name is close to `name`, as [{kind:, name:}]: same name in other letter case
90
+ # first, then names containing it (or contained in it), then small edit distances. Resources of `kind` win ties.
91
+ def suggest(name, kind: nil, limit: 5)
92
+ wanted = name.to_s.downcase
93
+ return [] if wanted.empty?
94
+
95
+ threshold = [[wanted.length / 3, 2].max, 5].min
96
+ scored = db.instances.flat_map do |klass, by_name|
97
+ by_name.each_key.filter_map do |candidate|
98
+ rank = suggestion_rank(wanted, candidate.to_s.downcase, threshold)
99
+ [rank, klass.to_s.split("::").last == kind ? 0 : 1, candidate, klass.to_s.split("::").last] if rank
100
+ end
101
+ end
102
+ scored.sort.first(limit).map { |(_, _, candidate, candidate_kind)| { kind: candidate_kind, name: candidate } }
103
+ end
104
+
105
+ def suggestion_rank(wanted, candidate, threshold)
106
+ return [0, 0] if candidate == wanted
107
+ return [1, (candidate.length - wanted.length).abs] if candidate.include?(wanted) || wanted.include?(candidate)
108
+
109
+ distance = DidYouMean::Levenshtein.distance(wanted, candidate)
110
+ [2, distance] if distance <= threshold
111
+ end
112
+
113
+ # "Did you mean ..." text for suggestions, or nil
114
+ def did_you_mean(suggestions)
115
+ return nil if suggestions.empty?
116
+
117
+ "Did you mean: #{suggestions.map { |s| "#{s[:kind]}/#{s[:name]}" }.join(", ")}?"
118
+ end
119
+
120
+ # `kind`, `name`, `source` and `generated` of each direct relation target, as [{kind:, name:, source:, generated:}]
121
+ def related_sources(instance)
122
+ targets = instance.class.relations.flat_map do |verb, kind_name, _|
123
+ instance.relations(verb, kind_name)
124
+ end
125
+ targets.uniq.map { |t| { kind: kind_of(t), name: t.name }.merge(source_info(t)) }
126
+ end
127
+
128
+ def format_ref(ref)
129
+ return nil unless ref
130
+
131
+ root = db.respond_to?(:path) ? db.path.to_s : ""
132
+ path = ref.path.to_s
133
+ path = path.delete_prefix(File.join(File.expand_path(root), "")) unless root.empty?
134
+ "#{path}:#{ref.line_no}"
28
135
  end
29
136
 
30
137
  def annotations_summary(resource, omit_kind: false)
@@ -16,6 +16,10 @@ class Archsight::MCP::ExecuteAnalysisTool < FastMcp::Tool
16
16
  2. EXECUTE MODE (name provided): Runs the specified Analysis script in a sandboxed environment
17
17
  and returns the results as markdown. The output includes structured findings like tables,
18
18
  lists, warnings, and errors generated by the script.
19
+
20
+ TYPICAL FLOW: always list first (no name) to get the exact analysis names, then run one by name.
21
+ PITFALLS: analyses take no parameters (they always run over the whole model); tables and lists in the output
22
+ are truncated unless verbose=true; names are exact and case-sensitive (a wrong one returns suggestions).
19
23
  DESC
20
24
 
21
25
  arguments do
@@ -64,7 +68,7 @@ class Archsight::MCP::ExecuteAnalysisTool < FastMcp::Tool
64
68
 
65
69
  db = Archsight::MCP.db
66
70
  analysis = db.instance_by_kind("Analysis", name)
67
- return error_response("Analysis not found: #{name}") unless analysis
71
+ return not_found(name) unless analysis
68
72
 
69
73
  executor = Archsight::Analysis::Executor.new(db)
70
74
  result = executor.execute(analysis)
@@ -91,6 +95,12 @@ class Archsight::MCP::ExecuteAnalysisTool < FastMcp::Tool
91
95
  end
92
96
  end
93
97
 
98
+ def not_found(name)
99
+ suggestions = Archsight::MCP.suggest(name, kind: "Analysis").select { |s| s[:kind] == "Analysis" }
100
+ message = ["Analysis not found: #{name}", Archsight::MCP.did_you_mean(suggestions)].compact.join(". ")
101
+ JSON.pretty_generate(error: "Error", message: message, suggestions: suggestions)
102
+ end
103
+
94
104
  def error_response(message)
95
105
  JSON.pretty_generate(
96
106
  error: "Error",
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+
5
+ # What a query result can tell about the query itself: unknown annotation keys in its filters and the direct
6
+ # relations that made a resource match a relation condition.
7
+ module Archsight::MCP::QueryHints
8
+ class << self
9
+ # Warnings for annotation keys in the filters of a query that no resource of the queried kind (any kind without
10
+ # a kind prefix) has and no kind declares: such a filter silently matches nothing (or, negated, everything).
11
+ def unknown_key_warnings(parsed_query)
12
+ keys = annotation_keys(parsed_query.ast).uniq
13
+ return [] if keys.empty?
14
+
15
+ kind = parsed_query.kind_filter
16
+ klass = kind && Archsight::Resources[kind]
17
+ classes = klass ? [klass] : Archsight::MCP.db.instances.keys
18
+ keys.reject { |key| known_annotation_key?(key, classes) }.map do |key|
19
+ known = known_annotation_keys(classes)
20
+ close = known.sort_by { |candidate| [Archsight::MCP.suggestion_rank(key.downcase, candidate.downcase, 99)&.first || 9, candidate] }.first(8)
21
+ scope = klass ? "known keys for #{kind}" : "closest known keys over all kinds (prefix the query with a Kind: to narrow)"
22
+ "unknown annotation key \"#{key}\"; #{scope}: #{close.join(", ")}"
23
+ end
24
+ end
25
+
26
+ def annotation_keys(node, found = [])
27
+ case node
28
+ when Archsight::Query::AST::QueryNode then annotation_keys(node.expression, found)
29
+ when Archsight::Query::AST::BinaryOp
30
+ annotation_keys(node.left, found)
31
+ annotation_keys(node.right, found)
32
+ when Archsight::Query::AST::NotOp then annotation_keys(node.operand, found)
33
+ when Archsight::Query::AST::TextCondition
34
+ found.concat(node.keys.to_a.reject { |key| key.include?("*") })
35
+ when Archsight::Query::AST::AnnotationCondition, Archsight::Query::AST::AnnotationExistsCondition,
36
+ Archsight::Query::AST::AnnotationInCondition
37
+ found << node.path
38
+ end
39
+ found
40
+ end
41
+
42
+ def known_annotation_key?(key, classes)
43
+ classes.any? do |klass|
44
+ klass.annotation_matching(key) || Archsight::MCP.db.instances[klass]&.each_value&.any? { |inst| inst.annotations.key?(key) }
45
+ end
46
+ end
47
+
48
+ # Declared keys (patterns like scc/language/*/loc included) and the keys resources actually carry
49
+ def known_annotation_keys(classes)
50
+ classes.flat_map do |klass|
51
+ declared = klass.annotations.map(&:key)
52
+ used = (Archsight::MCP.db.instances[klass] || {}).each_value.flat_map { |inst| inst.annotations.keys }
53
+ declared + used
54
+ end.uniq
55
+ end
56
+
57
+ # The free-text conditions (`text =~ "x"`) of a query that are not negated, as AST nodes
58
+ def text_conditions(node, found = [])
59
+ case node
60
+ when Archsight::Query::AST::QueryNode then text_conditions(node.expression, found)
61
+ when Archsight::Query::AST::BinaryOp
62
+ text_conditions(node.left, found)
63
+ text_conditions(node.right, found)
64
+ when Archsight::Query::AST::TextCondition then found << node
65
+ end
66
+ found
67
+ end
68
+
69
+ # Where the text of the conditions was found in `instance`: [{key:, snippet:}], at most `limit` entries
70
+ def matched_text(instance, conditions, limit: 3)
71
+ hits = []
72
+ conditions.each do |condition|
73
+ Archsight::Query::TextSearch.each_match(instance, condition) do |key, value, match|
74
+ hits << { key: key, snippet: Archsight::Query::TextSearch.snippet(value, match) }
75
+ end
76
+ end
77
+ hits.uniq.first(limit)
78
+ end
79
+
80
+ # The direct relation conditions of a query (`-> Kind`, `<- "Name"`, with verb filters) that are not negated,
81
+ # as AST nodes. Transitive, `none` and sub-query targets are left out: they have no single relation to show.
82
+ def relation_conditions(node, found = [])
83
+ case node
84
+ when Archsight::Query::AST::QueryNode then relation_conditions(node.expression, found)
85
+ when Archsight::Query::AST::BinaryOp
86
+ relation_conditions(node.left, found)
87
+ relation_conditions(node.right, found)
88
+ when Archsight::Query::AST::OutgoingDirectRelation, Archsight::Query::AST::IncomingDirectRelation
89
+ found << node if [Archsight::Query::AST::KindTarget, Archsight::Query::AST::InstanceTarget].any? { |t| node.target.is_a?(t) }
90
+ end
91
+ found
92
+ end
93
+
94
+ # The direct relations of `instance` that satisfy the given relation conditions: why a relation query matched it
95
+ def matched_via(instance, conditions)
96
+ relations = Archsight::MCP.direct_relations(instance)
97
+ conditions.flat_map do |cond|
98
+ direction = cond.is_a?(Archsight::Query::AST::OutgoingDirectRelation) ? "out" : "in"
99
+ relations.select do |rel|
100
+ rel[:direction] == direction && relation_target_match?(cond.target, rel) && relation_verb_match?(cond, rel)
101
+ end
102
+ end.uniq
103
+ end
104
+
105
+ def relation_target_match?(target, rel)
106
+ if target.is_a?(Archsight::Query::AST::KindTarget)
107
+ rel[:kind] == target.kind_name
108
+ else
109
+ rel[:name] == target.instance_name
110
+ end
111
+ end
112
+
113
+ def relation_verb_match?(cond, rel)
114
+ return true if cond.verbs.nil?
115
+
116
+ cond.verbs.include?(rel[:verb]) != cond.exclude_verbs
117
+ end
118
+ end
119
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "base"
4
+ require_relative "query_hints"
4
5
 
5
6
  class Archsight::MCP::QueryTool < FastMcp::Tool
6
7
  tool_name "query"
@@ -17,6 +18,11 @@ class Archsight::MCP::QueryTool < FastMcp::Tool
17
18
  • Annotation filters: 'activity/status == "active"', 'scc/language/Go/loc > 5000'
18
19
  Operators: == (equals), != (not equals), =~ (regex match), >, <, >=, <=
19
20
 
21
+ • Free-text search over the string annotations (descriptions, links, titles, ...):
22
+ text =~ "failover" (also text: "failover"; case-insensitive regex, like name =~)
23
+ text(architecture/description) =~ "x", text(link/*, page/content) =~ "x" restrict the keys (* = any run)
24
+ Each hit shows matched_text: the annotation key and a snippet around the match.
25
+
20
26
  • Relation queries:
21
27
  -> Kind (has outgoing relation to kind), -> "Name" (to specific instance)
22
28
  <- Kind (has incoming relation from kind), <- "Name" (from specific instance)
@@ -38,6 +44,24 @@ class Archsight::MCP::QueryTool < FastMcp::Tool
38
44
  '-> none & <- none' - true orphans with no relations at all
39
45
  '~> $(web-checkout)' - transitively reaches instance matching "web-checkout"
40
46
  '<- $(TechnologyArtifact: activity/status == "active")' - referenced by active artifacts
47
+ 'ApplicationComponent: <- none & -> none' - components nothing refers to and that refer to nothing (orphans of one kind)
48
+ 'ApplicationComponent: ~> $(name == "web-checkout")' - components that transitively reach web-checkout
49
+ 'ApplicationService: -{realizedThrough}> ApplicationComponent' - only along one relation verb (<{verb}- for incoming)
50
+ 'ApplicationService: text =~ "failover" & -> ApplicationComponent' - services whose text mentions failover and that have components
51
+ 'text(link/*) =~ "PROJ-1234"' - every resource that links ticket PROJ-1234
52
+ For the impact of changing a resource use analyze_resource(impact=true) with kinds/exclude_kinds/depth/limit.
53
+
54
+ TYPICAL FLOW: find the exact name with a name search (output='brief' shows kind, name and source file),
55
+ then call analyze_resource or relations with that kind and name. Use output='count' first to see how big
56
+ a result is, 'brief' to list it, and 'complete' (with fields=[...] and a small limit) only for details.
57
+
58
+ PITFALLS:
59
+ • A filter on an annotation key that does not exist matches nothing; the result then carries a warning
60
+ 'unknown annotation key ...' listing the known keys. Keys are exact and case-sensitive.
61
+ • Annotation values are strings: compare with == "x", numbers with > < >=, regex with =~.
62
+ • output='complete' returns every annotation and relation of each hit: combine it with fields and limit.
63
+ • Add a Kind: prefix when you can; without it every kind is searched, which is slower and noisier.
64
+ • Pages and menus are resources too (Page, PageMenu): exclude them with a Kind: prefix when you look for architecture.
41
65
  DESC
42
66
  arguments do
43
67
  required(:query).filled(:string).description(
@@ -51,6 +75,10 @@ class Archsight::MCP::QueryTool < FastMcp::Tool
51
75
  "'brief' = minimal output with just kind and name for each resource, " \
52
76
  "'count' = only totals grouped by kind, no individual resources returned"
53
77
  )
78
+ optional(:fields).array(:string).description(
79
+ "Only these annotation keys and relation verbs in output='complete' " \
80
+ "(e.g. ['activity/status', 'maintainedBy']). Default: all. Keeps results small."
81
+ )
54
82
  optional(:limit).filled(:integer).description(
55
83
  "Maximum results to return (1-500). Use with offset for pagination. Ignored when output='count'."
56
84
  )
@@ -60,7 +88,7 @@ class Archsight::MCP::QueryTool < FastMcp::Tool
60
88
  )
61
89
  end
62
90
 
63
- def call(query:, output: "complete", limit: 50, offset: 0)
91
+ def call(query:, output: "complete", limit: 50, offset: 0, fields: nil)
64
92
  db = Archsight::MCP.db
65
93
 
66
94
  begin
@@ -68,9 +96,13 @@ class Archsight::MCP::QueryTool < FastMcp::Tool
68
96
  results = parsed_query.filter(db)
69
97
  total = results.length
70
98
 
71
- # Omit kind from output when filtering by a specific kind (it's redundant)
99
+ # Complete output omits the kind when filtering by a specific kind (it's redundant); brief always names
100
+ # it, so a hit can feed analyze_resource(kind, name) without guessing
72
101
  omit_kind = !parsed_query.kind_filter.nil?
73
102
 
103
+ conditions = Archsight::MCP::QueryHints.relation_conditions(parsed_query.ast)
104
+ text_conditions = Archsight::MCP::QueryHints.text_conditions(parsed_query.ast)
105
+
74
106
  # Count-only mode: return totals grouped by kind
75
107
  if output == "count"
76
108
  by_kind = results.group_by { |r| r.class.to_s.split("::").last }
@@ -86,11 +118,17 @@ class Archsight::MCP::QueryTool < FastMcp::Tool
86
118
  paginated = results.sort_by(&:name).drop(offset).take(limit)
87
119
  resources = case output
88
120
  when "brief"
89
- paginated.map { |r| Archsight::MCP.brief_summary(r, omit_kind: omit_kind) }
121
+ paginated.map { |r| Archsight::MCP.brief_summary(r, with_source: true) }
90
122
  else # "complete"
91
- paginated.map { |r| Archsight::MCP.complete_summary(r, omit_kind: omit_kind) }
123
+ paginated.map do |r|
124
+ summary = Archsight::MCP.complete_summary(r, omit_kind: omit_kind, with_source: true)
125
+ Archsight::MCP.project(summary, fields)
126
+ end
92
127
  end
93
128
 
129
+ add_matched_via(resources, paginated, conditions)
130
+ add_matched_text(resources, paginated, text_conditions)
131
+
94
132
  result = {
95
133
  query: query,
96
134
  total: total,
@@ -101,7 +139,10 @@ class Archsight::MCP::QueryTool < FastMcp::Tool
101
139
  }
102
140
  end
103
141
 
104
- JSON.pretty_generate(result)
142
+ warnings = Archsight::MCP::QueryHints.unknown_key_warnings(parsed_query)
143
+ result = result.merge(warnings: warnings) unless warnings.empty?
144
+
145
+ fit_response(result, output, fields)
105
146
  rescue Archsight::Query::QueryError => e
106
147
  JSON.pretty_generate({
107
148
  error: "Query error",
@@ -110,4 +151,48 @@ class Archsight::MCP::QueryTool < FastMcp::Tool
110
151
  })
111
152
  end
112
153
  end
154
+
155
+ private
156
+
157
+ # For text queries (`text =~ "x"`): the annotation and the snippet around the match, per hit
158
+ def add_matched_text(summaries, resources, conditions)
159
+ return if conditions.empty?
160
+
161
+ summaries.zip(resources).each do |summary, resource|
162
+ found = Archsight::MCP::QueryHints.matched_text(resource, conditions)
163
+ summary[:matched_text] = found unless found.empty?
164
+ end
165
+ end
166
+
167
+ # For relation queries (`-> Kind`, `<- "Name"`): the direct relations that made each hit match
168
+ def add_matched_via(summaries, resources, conditions)
169
+ return if conditions.empty?
170
+
171
+ summaries.zip(resources).each do |summary, resource|
172
+ via = Archsight::MCP::QueryHints.matched_via(resource, conditions)
173
+ summary[:matched_via] = via unless via.empty?
174
+ end
175
+ end
176
+
177
+ # Cuts the resources of an oversized result down (halving until it fits) and says how to get the rest
178
+ def fit_response(result, output, fields)
179
+ return JSON.pretty_generate(result) unless result[:resources]
180
+
181
+ full = result[:resources]
182
+ Archsight::MCP.fit(result) do |attempt|
183
+ keep = [full.length / (2**attempt), 1].max
184
+ result.merge(
185
+ resources: full.take(keep),
186
+ count: keep,
187
+ truncated: "Response cut to #{keep} of #{full.length} resources (size budget). " \
188
+ "#{hint_for(output, fields)}"
189
+ )
190
+ end
191
+ end
192
+
193
+ def hint_for(output, fields)
194
+ return "Use a smaller limit with offset, or output='brief'/'count'." if output == "complete" && fields.nil?
195
+
196
+ "Use a smaller limit with offset."
197
+ end
113
198
  end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+
5
+ class Archsight::MCP::RelationsTool < FastMcp::Tool
6
+ tool_name "relations"
7
+
8
+ description <<~DESC.gsub("\n", " ").strip
9
+ List the DIRECT relations of one resource, filtered and grouped by verb. No transitive expansion, no other
10
+ resource details (use analyze_resource for those, or depth/impact for transitive views).
11
+
12
+ Each relation is {verb, direction, kind, name}: direction 'out' = this resource points at the target
13
+ (e.g. realizedThrough), 'in' = the other resource points at this one (e.g. what it is servedBy).
14
+
15
+ EXAMPLES:
16
+ kind="ApplicationService", name="X", verb="realizedThrough" - only the direct realizedThrough targets
17
+ kind="ApplicationComponent", name="Y", direction="in", target_kind="ApplicationService" - which services use Y
18
+
19
+ A name that does not exist returns the closest matches across all kinds.
20
+
21
+ TYPICAL FLOW: find the exact name with query (output='brief'), then ask for one verb or one target kind here.
22
+ PITFALLS: kind and name are exact and case-sensitive; verbs are exact (a verb the resource does not have
23
+ gives a warning listing the verbs it has); 'in' relations are the ones other resources declare towards it.
24
+ DESC
25
+ arguments do
26
+ required(:kind).filled(:string).description("Resource kind, e.g. ApplicationService")
27
+ required(:name).filled(:string).description(
28
+ "Exact name of the resource (case-sensitive). A wrong name returns suggestions."
29
+ )
30
+ optional(:verb).filled(:string).description("Only this relation verb (e.g. 'realizedThrough', 'servedBy')")
31
+ optional(:direction).filled(:string).description(
32
+ "'out' (this resource points at the target), 'in' (the other points at this one), 'both' (default)"
33
+ )
34
+ optional(:target_kind).filled(:string).description("Only relations whose other end is of this kind")
35
+ optional(:limit).filled(:integer).description("Maximum relations returned (default 100, max 500)")
36
+ end
37
+
38
+ DIRECTIONS = %w[out in both].freeze
39
+
40
+ def call(kind:, name:, verb: nil, direction: "both", target_kind: nil, limit: 100)
41
+ return error("Unknown resource kind: #{kind}") unless Archsight::Resources[kind]
42
+ return error("Unknown direction: #{direction}. Use out, in or both") unless DIRECTIONS.include?(direction)
43
+
44
+ instance = Archsight::MCP.db.instance_by_kind(kind, name)
45
+ return not_found(kind, name) unless instance
46
+
47
+ build(instance, kind, name, verb, direction, target_kind, [[limit.to_i, 1].max, 500].min)
48
+ rescue StandardError => e
49
+ error("#{e.class}: #{e.message}")
50
+ end
51
+
52
+ private
53
+
54
+ def build(instance, kind, name, verb, direction, target_kind, limit)
55
+ all = Archsight::MCP.direct_relations(instance)
56
+ relations = all.select do |rel|
57
+ (verb.nil? || rel[:verb] == verb) &&
58
+ (direction == "both" || rel[:direction] == direction) &&
59
+ (target_kind.nil? || rel[:kind] == target_kind)
60
+ end
61
+ relations = relations.sort_by { |rel| [rel[:verb], rel[:direction], rel[:kind], rel[:name]] }
62
+ shown = relations.first(limit)
63
+
64
+ result = { resource: { kind: kind, name: name }, total: relations.length, shown: shown.length }
65
+ result[:by_verb] = relations.group_by { |rel| rel[:verb] }.transform_values(&:length)
66
+ result[:relations] = shown.group_by { |rel| rel[:verb] }
67
+ result[:truncated] = "#{relations.length - shown.length} more not listed; narrow with verb, direction or target_kind" if shown.length < relations.length
68
+ warning = unknown_verb_warning(all, verb)
69
+ result[:warning] = warning if warning && relations.empty?
70
+ JSON.pretty_generate(result)
71
+ end
72
+
73
+ def unknown_verb_warning(all, verb)
74
+ return nil if verb.nil? || all.any? { |rel| rel[:verb] == verb }
75
+
76
+ known = all.map { |rel| rel[:verb] }.uniq.sort
77
+ "This resource has no relations with verb \"#{verb}\". Verbs present: #{known.empty? ? "none" : known.join(", ")}"
78
+ end
79
+
80
+ def not_found(kind, name)
81
+ suggestions = Archsight::MCP.suggest(name, kind: kind)
82
+ message = ["Resource not found: #{kind}/#{name}", Archsight::MCP.did_you_mean(suggestions)].compact.join(". ")
83
+ JSON.pretty_generate({ error: "Error", message: message, suggestions: suggestions })
84
+ end
85
+
86
+ def error(message)
87
+ JSON.pretty_generate({ error: "Error", message: message })
88
+ end
89
+ end
@@ -18,6 +18,11 @@ class Archsight::MCP::ResourceDocTool < FastMcp::Tool
18
18
  Includes description, available annotations, relations, and a YAML example template.
19
19
  Example: kind="TechnologyArtifact"
20
20
 
21
+ TYPICAL FLOW: list the kinds, then ask for one kind, with section=annotations to learn which annotation keys a
22
+ query filter can use, or section=relations to learn which verbs connect it to other kinds.
23
+ PITFALLS: kind names are exact and case-sensitive; without a kind only a one-line list comes back (not the
24
+ full documentation of every kind), so always name the kind you need.
25
+
21
26
  COMMON RESOURCE KINDS:
22
27
  • TechnologyArtifact - Code repositories, artifacts, and technology components
23
28
  • ApplicationComponent - Services and application building blocks
@@ -29,15 +34,21 @@ class Archsight::MCP::ResourceDocTool < FastMcp::Tool
29
34
  arguments do
30
35
  optional(:kind).filled(:string).description(
31
36
  'Resource kind to get documentation for (e.g., "TechnologyArtifact", "ApplicationComponent"). ' \
32
- "If not specified, returns a list of all available resource kinds."
37
+ "If not specified, returns a compact list of all available resource kinds (one line each)."
38
+ )
39
+ optional(:section).filled(:string).description(
40
+ "Only this part of a kind's documentation: 'description' (everything before the annotations), " \
41
+ "'annotations', 'relations' or 'example'. Default: the whole document. Needs a kind."
33
42
  )
34
43
  end
35
44
 
36
- def call(kind: nil)
45
+ SECTIONS = %w[description annotations relations example].freeze
46
+
47
+ def call(kind: nil, section: nil)
37
48
  if kind.nil? || kind.empty?
38
49
  list_resource_kinds
39
50
  else
40
- get_resource_documentation(kind)
51
+ get_resource_documentation(kind, section)
41
52
  end
42
53
  rescue StandardError => e
43
54
  error_response(e.message, e.class.name)
@@ -52,36 +63,44 @@ class Archsight::MCP::ResourceDocTool < FastMcp::Tool
52
63
  })
53
64
  end
54
65
 
66
+ # One line per kind ("Kind (layer): first sentence"), so the whole list stays a few KB
55
67
  def list_resource_kinds
56
68
  kinds = []
57
69
 
58
70
  Archsight::Resources.each do |kind_symbol|
59
71
  klass = Archsight::Resources.const_get(kind_symbol)
60
- kinds << {
61
- kind: kind_symbol.to_s,
62
- description: klass.description || "No description available",
63
- annotation_count: klass.annotations.count,
64
- relation_count: klass.declared_relations.count
65
- }
72
+ text = Archsight::MCP.one_liner(klass.description, max: 80)
73
+ text = "No description available" if text.empty?
74
+ kinds << "#{kind_symbol} (#{klass.layer}): #{text}"
66
75
  end
67
76
 
68
- result = {
69
- total: kinds.length,
70
- resource_kinds: kinds
71
- }
72
-
73
- JSON.pretty_generate(result)
77
+ JSON.pretty_generate({
78
+ total: kinds.length,
79
+ hint: 'Call resource_doc(kind: "<Kind>") for the full documentation, add section: ' \
80
+ "description|annotations|relations|example for one part.",
81
+ resource_kinds: kinds
82
+ })
74
83
  end
75
84
 
76
- def get_resource_documentation(kind)
85
+ def get_resource_documentation(kind, section)
77
86
  klass = Archsight::Resources[kind]
78
87
  return error_response("Unknown resource kind: #{kind}") unless klass
79
88
 
89
+ return error_response("Unknown section: #{section}. Use one of #{SECTIONS.join(", ")}") if section && !SECTIONS.include?(section)
90
+
80
91
  content = Archsight::Documentation.generate(kind)
92
+ content = extract_section(content, section) if section
93
+ result = { kind: kind }
94
+ result[:section] = section if section
95
+ result[:documentation] = content
81
96
 
82
- JSON.pretty_generate({
83
- kind: kind,
84
- documentation: content
85
- })
97
+ JSON.pretty_generate(result)
98
+ end
99
+
100
+ # "description" is everything before `## Annotations`; the others are the `## Name` block of that heading
101
+ def extract_section(content, section)
102
+ return content.split(/^## Annotations$/, 2).first.to_s.strip if section == "description"
103
+
104
+ content[/^## #{section.capitalize}$.*?(?=^## |\z)/m].to_s.strip
86
105
  end
87
106
  end