mistri 0.6.1 → 0.8.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 (113) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +355 -2
  3. data/CONTRIBUTING.md +17 -3
  4. data/NOTICE +11 -0
  5. data/README.md +145 -226
  6. data/UPGRADING.md +810 -0
  7. data/assets/vendors/README.md +12 -0
  8. data/assets/vendors/claude-mark.svg +3 -0
  9. data/assets/vendors/gemini-mark.svg +10 -0
  10. data/assets/vendors/openai-mark-dark.svg +1 -0
  11. data/assets/vendors/openai-mark.svg +1 -0
  12. data/docs/README.md +2 -1
  13. data/docs/context-and-workspaces.md +84 -13
  14. data/docs/logging.md +90 -0
  15. data/docs/mcp.md +762 -21
  16. data/docs/reliability.md +210 -24
  17. data/docs/sessions.md +415 -36
  18. data/docs/sub-agents.md +85 -7
  19. data/docs/tool-contracts.md +528 -13
  20. data/examples/approval.rb +7 -7
  21. data/examples/browser.rb +1 -1
  22. data/examples/page_editor.rb +2 -2
  23. data/examples/quickstart.rb +1 -1
  24. data/lib/generators/mistri/install/templates/migration.rb.tt +4 -2
  25. data/lib/generators/mistri/mcp/mcp_generator.rb +2 -2
  26. data/lib/generators/mistri/mcp/templates/model.rb.tt +21 -8
  27. data/lib/mistri/agent.rb +839 -141
  28. data/lib/mistri/call_order.rb +137 -0
  29. data/lib/mistri/child.rb +69 -13
  30. data/lib/mistri/compaction.rb +200 -35
  31. data/lib/mistri/compactor/text.rb +91 -0
  32. data/lib/mistri/compactor.rb +474 -117
  33. data/lib/mistri/console.rb +44 -11
  34. data/lib/mistri/content.rb +70 -3
  35. data/lib/mistri/definition.rb +2 -2
  36. data/lib/mistri/edit.rb +13 -7
  37. data/lib/mistri/errors.rb +37 -0
  38. data/lib/mistri/event.rb +20 -9
  39. data/lib/mistri/locks/rails_cache.rb +16 -2
  40. data/lib/mistri/locks.rb +12 -3
  41. data/lib/mistri/mcp/client.rb +247 -61
  42. data/lib/mistri/mcp/conversation.rb +161 -0
  43. data/lib/mistri/mcp/egress.rb +5 -4
  44. data/lib/mistri/mcp/mirror.rb +90 -0
  45. data/lib/mistri/mcp/negotiation.rb +222 -0
  46. data/lib/mistri/mcp/oauth/client_identity.rb +53 -0
  47. data/lib/mistri/mcp/oauth/tolerance.rb +61 -0
  48. data/lib/mistri/mcp/oauth.rb +303 -82
  49. data/lib/mistri/mcp/results.rb +97 -0
  50. data/lib/mistri/mcp/sdk_client.rb +47 -0
  51. data/lib/mistri/mcp/wires.rb +325 -52
  52. data/lib/mistri/mcp.rb +304 -55
  53. data/lib/mistri/memory.rb +1 -1
  54. data/lib/mistri/message.rb +45 -8
  55. data/lib/mistri/models.rb +146 -24
  56. data/lib/mistri/partial_json.rb +5 -5
  57. data/lib/mistri/providers/anthropic/assembler.rb +40 -3
  58. data/lib/mistri/providers/anthropic/serializer.rb +154 -22
  59. data/lib/mistri/providers/anthropic.rb +77 -12
  60. data/lib/mistri/providers/fake.rb +4 -1
  61. data/lib/mistri/providers/gemini/assembler.rb +63 -3
  62. data/lib/mistri/providers/gemini/serializer.rb +27 -16
  63. data/lib/mistri/providers/gemini.rb +56 -11
  64. data/lib/mistri/providers/openai/assembler.rb +36 -5
  65. data/lib/mistri/providers/openai/serializer.rb +76 -22
  66. data/lib/mistri/providers/openai.rb +91 -14
  67. data/lib/mistri/reminder.rb +35 -3
  68. data/lib/mistri/result.rb +6 -4
  69. data/lib/mistri/result_limit.rb +164 -0
  70. data/lib/mistri/sandbox/quickjs.rb +345 -0
  71. data/lib/mistri/sandbox.rb +18 -0
  72. data/lib/mistri/schema.rb +26 -2
  73. data/lib/mistri/scratchpad.rb +107 -0
  74. data/lib/mistri/session/log.rb +137 -0
  75. data/lib/mistri/session.rb +405 -75
  76. data/lib/mistri/sinks/logger.rb +407 -0
  77. data/lib/mistri/skills.rb +10 -4
  78. data/lib/mistri/spawner.rb +43 -12
  79. data/lib/mistri/sse.rb +3 -2
  80. data/lib/mistri/stores/active_record.rb +30 -6
  81. data/lib/mistri/stores/jsonl.rb +26 -7
  82. data/lib/mistri/stores/memory.rb +6 -2
  83. data/lib/mistri/sub_agent/execution.rb +11 -3
  84. data/lib/mistri/sub_agent/runtime.rb +11 -1
  85. data/lib/mistri/sub_agent.rb +13 -4
  86. data/lib/mistri/tool.rb +71 -7
  87. data/lib/mistri/tool_arguments.rb +10 -11
  88. data/lib/mistri/tool_context.rb +15 -3
  89. data/lib/mistri/tool_executor.rb +49 -26
  90. data/lib/mistri/tool_result.rb +8 -2
  91. data/lib/mistri/tool_search/index.rb +193 -0
  92. data/lib/mistri/tool_search.rb +200 -0
  93. data/lib/mistri/tools/delete_file.rb +22 -0
  94. data/lib/mistri/tools/edit_file.rb +24 -14
  95. data/lib/mistri/tools/find_in_file.rb +1 -0
  96. data/lib/mistri/tools/grep.rb +212 -0
  97. data/lib/mistri/tools/list_files.rb +38 -5
  98. data/lib/mistri/tools/read_file.rb +83 -21
  99. data/lib/mistri/tools/read_memory.rb +2 -1
  100. data/lib/mistri/tools/run_code.rb +167 -0
  101. data/lib/mistri/tools/write_file.rb +66 -9
  102. data/lib/mistri/tools.rb +72 -1
  103. data/lib/mistri/transport/streamable_http.rb +140 -0
  104. data/lib/mistri/transport.rb +53 -63
  105. data/lib/mistri/usage.rb +33 -4
  106. data/lib/mistri/version.rb +1 -1
  107. data/lib/mistri/workspace/active_record.rb +22 -10
  108. data/lib/mistri/workspace/directory.rb +5 -1
  109. data/lib/mistri/workspace/memory.rb +4 -0
  110. data/lib/mistri/workspace/path_space.rb +56 -0
  111. data/lib/mistri/workspace/single.rb +6 -1
  112. data/lib/mistri.rb +28 -4
  113. metadata +30 -2
@@ -0,0 +1,222 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mistri
4
+ module MCP
5
+ # Which revision of the protocol a client speaks with its server. Since
6
+ # 2026-07-28 there is no handshake: a request carries its own version, and
7
+ # a client that also speaks an older revision first asks the server which
8
+ # it has. Part of Client, which gives it the wire and rpc.
9
+ module Negotiation
10
+ PROTOCOL_VERSION = "2026-07-28"
11
+ HANDSHAKE_VERSIONS = %w[2025-11-25 2025-06-18 2025-03-26 2024-11-05].freeze
12
+ SUPPORTED_VERSIONS = [PROTOCOL_VERSION, *HANDSHAKE_VERSIONS].freeze
13
+ # The errors that only a server of 2026-07-28 sends: a header that does
14
+ # not match, a capability the client lacks, a version it does not speak.
15
+ NEWER_ERRORS = [-32_020, -32_021, -32_022].freeze
16
+ UNSUPPORTED_VERSION = -32_022
17
+ # A refusal with one of these says nothing about the revision.
18
+ UNRELATED_STATUSES = [401, 403, 408, 429].freeze
19
+ # Times a call is sent again for a server that asks to be called again.
20
+ STATE_ROUNDS = 3
21
+ private_constant :HANDSHAKE_VERSIONS, :NEWER_ERRORS, :UNSUPPORTED_VERSION,
22
+ :UNRELATED_STATUSES, :STATE_ROUNDS
23
+
24
+ private
25
+
26
+ # A server is asked once which revisions it speaks, and the answer is
27
+ # kept. A server on stdio that was slow to start answers too late and
28
+ # then refuses the handshake, so it is asked once more with the full
29
+ # wait. So is a server that refuses the handshake of a revision this
30
+ # client remembered: it may speak only 2026-07-28 by now.
31
+ def ensure_connected
32
+ return @wire.protocol_version = @version if stateless?
33
+ return if @connected && @wire.open?
34
+
35
+ remembered = !@version.nil?
36
+ silent = !remembered && discover(@wire.probe_wait) == :silent
37
+ return if stateless?
38
+
39
+ begin
40
+ open_session
41
+ rescue Error, ProviderError => e
42
+ raise unless newest_after_all?(silent, remembered && refused?(e))
43
+ end
44
+ rescue Mistri::Error
45
+ reset_wire
46
+ raise
47
+ end
48
+
49
+ # A server that was silent gets the full wait this time. One that
50
+ # refused a remembered revision gets the wait of a first question.
51
+ def newest_after_all?(silent, refused)
52
+ return false unless silent || refused
53
+
54
+ discover(silent ? nil : @wire.probe_wait) == :newest
55
+ end
56
+
57
+ # The server's own answer to a request: a JSON-RPC error, or an HTTP
58
+ # status that says something about the request.
59
+ def refused?(error)
60
+ error.is_a?(ProviderError) ? !unrelated?(error.status) : !error.code.nil?
61
+ end
62
+
63
+ def stateless? = @version == PROTOCOL_VERSION
64
+
65
+ # Asks in the form of 2026-07-28. An older server does not know the
66
+ # request: whatever it answers that is not an error of that revision
67
+ # says so, and so does silence on stdio. Never one error code alone. A
68
+ # refusal to authorize, a server error and a lost connection say
69
+ # nothing about the revision.
70
+ def discover(wait, again: false)
71
+ @wire.protocol_version = PROTOCOL_VERSION
72
+ answer = question(wait, again)
73
+ answer.is_a?(Symbol) ? answer : adopt(answer)
74
+ end
75
+
76
+ # The server's answer, or what its failure to give one says.
77
+ def question(wait, again)
78
+ rpc("server/discover", {}, newest: true, wait: wait)
79
+ rescue WireTimeout
80
+ raise unless wait
81
+
82
+ older(:silent)
83
+ rescue Error, ProviderError => e
84
+ raise if e.is_a?(ProviderError) && unrelated?(e.status)
85
+
86
+ code, data = refusal_of(e)
87
+ return older(:older) unless NEWER_ERRORS.include?(code)
88
+ raise if again || code != UNSUPPORTED_VERSION
89
+
90
+ offered(data.is_a?(Hash) ? data["supported"] : nil) { discover(wait, again: true) }
91
+ end
92
+
93
+ def adopt(result)
94
+ return older(:older) unless result.is_a?(Hash) && result["supportedVersions"].is_a?(Array)
95
+
96
+ offered(result["supportedVersions"]) do
97
+ meta = result["_meta"]
98
+ @server_info = meta["io.modelcontextprotocol/serverInfo"] if meta.is_a?(Hash)
99
+ text = result["instructions"]
100
+ @instructions = (text if text.is_a?(String) && !text.empty?)
101
+ @resources = offers_resources?(result)
102
+ @described = true
103
+ @version = PROTOCOL_VERSION
104
+ @tools = usable(@tools) if @tools
105
+ :newest
106
+ end
107
+ end
108
+
109
+ # The block runs when the server offers 2026-07-28. A server that
110
+ # offers only older revisions gets the handshake.
111
+ def offered(versions)
112
+ versions = Array(versions)
113
+ return yield if versions.include?(PROTOCOL_VERSION)
114
+ return older(:older) if versions.intersect?(HANDSHAKE_VERSIONS)
115
+
116
+ raise Error, "the server speaks no protocol version this client does: " \
117
+ "#{versions.first(8).join(", ")[0, 120]}"
118
+ end
119
+
120
+ def older(answer)
121
+ @wire.protocol_version = nil
122
+ answer
123
+ end
124
+
125
+ def unrelated?(status) = status.nil? || status >= 500 || UNRELATED_STATUSES.include?(status)
126
+
127
+ # The JSON-RPC error a failure carries: its own, or the one in the body
128
+ # of an HTTP refusal.
129
+ def refusal_of(error)
130
+ return [error.code, error.data] if error.is_a?(Error)
131
+
132
+ inner = JSON.parse(error.body.to_s)
133
+ inner = inner["error"] if inner.is_a?(Hash)
134
+ [inner["code"], inner["data"]] if inner.is_a?(Hash)
135
+ rescue JSON::ParserError
136
+ nil
137
+ end
138
+
139
+ def open_session
140
+ result = rpc("initialize", {
141
+ protocolVersion: HANDSHAKE_VERSIONS.first,
142
+ capabilities: {},
143
+ clientInfo: { name: @client_name, version: Mistri::VERSION }
144
+ })
145
+ version = result["protocolVersion"].to_s
146
+ unless HANDSHAKE_VERSIONS.include?(version)
147
+ raise Error, "server negotiated unsupported protocol version #{version.inspect}"
148
+ end
149
+
150
+ @wire.protocol_version = version
151
+ @version = version
152
+ @server_info = result["serverInfo"]
153
+ text = result["instructions"]
154
+ @instructions = (text if text.is_a?(String) && !text.empty?)
155
+ @resources = offers_resources?(result)
156
+ @described = true
157
+ @wire.notify({ jsonrpc: "2.0", method: "notifications/initialized" })
158
+ @connected = true
159
+ end
160
+
161
+ # A server that has resources declares the capability, in either form.
162
+ def offers_resources?(result)
163
+ declared = result["capabilities"]
164
+ declared.is_a?(Hash) && declared.key?("resources")
165
+ end
166
+
167
+ def form_refused?(error)
168
+ code, = refusal_of(error)
169
+ return true if code == UNSUPPORTED_VERSION
170
+
171
+ error.is_a?(ProviderError) && !unrelated?(error.status) && !NEWER_ERRORS.include?(code)
172
+ end
173
+
174
+ # What every request says of itself since 2026-07-28, where no handshake
175
+ # says it once.
176
+ def described
177
+ { "io.modelcontextprotocol/protocolVersion" => PROTOCOL_VERSION,
178
+ "io.modelcontextprotocol/clientInfo" => { name: @client_name, version: Mistri::VERSION },
179
+ "io.modelcontextprotocol/clientCapabilities" => {} }
180
+ end
181
+
182
+ # In the form of 2026-07-28 a server may answer a call by asking for
183
+ # input. This client declares none it can give, so a call that waits
184
+ # for some ends as the tool's own error, in words a model can pass on. A
185
+ # result that asks for nothing and carries state is sent again with it.
186
+ def finished(name, arguments, result, rounds = STATE_ROUNDS)
187
+ return result unless result.is_a?(Hash) && result["resultType"] == "input_required"
188
+
189
+ asked = result["inputRequests"]
190
+ return declined(asked) if rounds.zero? || (asked.is_a?(Hash) && !asked.empty?)
191
+
192
+ params = { name: name, arguments: arguments }
193
+ params[:requestState] = result["requestState"] if result.key?("requestState")
194
+ @taken = true
195
+ finished(name, arguments, request("tools/call", params), rounds - 1)
196
+ end
197
+
198
+ # What a model reads when a server asked for input to finish a call.
199
+ def declined(asked)
200
+ wanted = (asked.is_a?(Hash) ? asked.values : []).first(3).filter_map do |input|
201
+ next unless input.is_a?(Hash)
202
+
203
+ params = input["params"]
204
+ [input["method"], (params["message"] if params.is_a?(Hash))].compact.join(": ")
205
+ end
206
+ detail = " (#{wanted.join("; ")[0, 300]})" unless wanted.empty?
207
+ text = "The server asked for input before it would finish this call#{detail}. " \
208
+ "This client gives a server no input, so the call was not finished."
209
+ { "isError" => true, "content" => [{ "type" => "text", "text" => text }] }
210
+ end
211
+
212
+ # A result names its type since 2026-07-28. None means complete, and one
213
+ # this client does not know is not a result it can use.
214
+ def typed(result)
215
+ type = result["resultType"] if result.is_a?(Hash)
216
+ return result if type.nil? || %w[complete input_required].include?(type)
217
+
218
+ raise Error, "the server returned a result of the unknown type #{type.to_s[0, 60].inspect}"
219
+ end
220
+ end
221
+ end
222
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The rules for application_type and for the address of a client ID metadata
4
+ # document are adapted from the Model Context Protocol Ruby SDK
5
+ # (lib/mcp/client/oauth/discovery.rb), Apache License 2.0. See NOTICE.
6
+
7
+ module Mistri
8
+ module MCP
9
+ # How the application names itself to an authorization server.
10
+ module OAuth
11
+ module_function
12
+
13
+ # OpenID Connect servers read a registration with no application_type as
14
+ # a web application, and refuse a loopback redirect URI for one.
15
+ def application_type(redirect_uri)
16
+ Egress.loopback?(URI(redirect_uri).hostname) ? "native" : "web"
17
+ end
18
+
19
+ # The document a host serves at the address it passes to start as
20
+ # client_metadata_url:. redirect_uris holds every redirect URI the
21
+ # application signs in with.
22
+ def client_metadata(url:, client_name:, redirect_uris:)
23
+ validate_client_metadata_url(url)
24
+ { "client_id" => url, "client_name" => client_name,
25
+ "redirect_uris" => Array(redirect_uris).map { |uri| Egress.redirect_uri(uri) },
26
+ "grant_types" => %w[authorization_code refresh_token],
27
+ "response_types" => ["code"], "token_endpoint_auth_method" => "none" }
28
+ end
29
+
30
+ def validate_client_metadata_url(url)
31
+ return if client_metadata_url?(url)
32
+
33
+ raise ConfigurationError,
34
+ "client_metadata_url: must be an https URL with a path and no query or fragment"
35
+ end
36
+
37
+ # The address is the client ID, so two spellings of one document must
38
+ # not exist: no query, no fragment, no credentials and no dot segments.
39
+ def client_metadata_url?(url)
40
+ uri = URI.parse(url.to_s)
41
+ return false unless uri.scheme&.downcase == "https" && !uri.host.to_s.empty?
42
+ return false if uri.fragment || uri.query || uri.user || uri.password
43
+
44
+ path = uri.path.to_s
45
+ return false if path.empty? || path == "/"
46
+
47
+ !path.gsub(/%2e/i, ".").split("/", -1).intersect?(%w[. ..])
48
+ rescue URI::InvalidURIError
49
+ false
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The rule for a resource that covers a server's address is adapted from the
4
+ # Model Context Protocol Ruby SDK (lib/mcp/client/oauth/discovery.rb), Apache
5
+ # License 2.0. See NOTICE.
6
+
7
+ module Mistri
8
+ module MCP
9
+ # What tolerant: true accepts where exact discovery refuses. Each rule is
10
+ # one the official SDKs apply to servers that predate exact metadata.
11
+ module OAuth
12
+ module_function
13
+
14
+ # A resource document may stand for the origin, or for a path above the
15
+ # server's. One that names a query covers that query alone.
16
+ def covers?(resource, url)
17
+ named = URI(resource)
18
+ server = URI(url)
19
+ return false unless Egress.origin(named) == Egress.origin(server)
20
+
21
+ above = named.path.to_s.chomp("/")
22
+ return false unless server.path.to_s == above || server.path.to_s.start_with?("#{above}/")
23
+
24
+ named.query.nil? || named.query == server.query
25
+ end
26
+
27
+ # An issuer taken from a URL is often written with the slash its
28
+ # metadata leaves out, or without the one it has.
29
+ def same_issuer?(first, second)
30
+ return false unless first.is_a?(String) && second.is_a?(String)
31
+
32
+ first == second || first == "#{second}/" || "#{first}/" == second
33
+ end
34
+
35
+ # A document that was looked for is not published when every answer
36
+ # says so: a redirect, a refusal other than 429, or a body that is no
37
+ # JSON object. No answer, a 429 and a 5xx say nothing of what is there.
38
+ def unpublished?(missed)
39
+ missed.all? do |answer|
40
+ answer == :unreadable || ((300..499).cover?(answer) && answer != 429)
41
+ end
42
+ end
43
+
44
+ # What an exact refusal adds when tolerant discovery takes the same
45
+ # answer, so the host can choose it. It names what is accepted: a later
46
+ # step of the sign-in can still refuse the server.
47
+ def tolerable(what, accepted = "a difference")
48
+ "; #{what}, #{accepted} that tolerant: true accepts"
49
+ end
50
+
51
+ # The endpoints of the 2025-03-26 revision, for an authorization server
52
+ # that publishes no metadata. That revision required PKCE.
53
+ def default_metadata(authority)
54
+ base = authority.chomp("/")
55
+ { "issuer" => authority, "authorization_endpoint" => "#{base}/authorize",
56
+ "token_endpoint" => "#{base}/token", "registration_endpoint" => "#{base}/register",
57
+ "code_challenge_methods_supported" => ["S256"] }
58
+ end
59
+ end
60
+ end
61
+ end