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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +355 -2
- data/CONTRIBUTING.md +17 -3
- data/NOTICE +11 -0
- data/README.md +145 -226
- data/UPGRADING.md +810 -0
- data/assets/vendors/README.md +12 -0
- data/assets/vendors/claude-mark.svg +3 -0
- data/assets/vendors/gemini-mark.svg +10 -0
- data/assets/vendors/openai-mark-dark.svg +1 -0
- data/assets/vendors/openai-mark.svg +1 -0
- data/docs/README.md +2 -1
- data/docs/context-and-workspaces.md +84 -13
- data/docs/logging.md +90 -0
- data/docs/mcp.md +762 -21
- data/docs/reliability.md +210 -24
- data/docs/sessions.md +415 -36
- data/docs/sub-agents.md +85 -7
- data/docs/tool-contracts.md +528 -13
- data/examples/approval.rb +7 -7
- data/examples/browser.rb +1 -1
- data/examples/page_editor.rb +2 -2
- data/examples/quickstart.rb +1 -1
- data/lib/generators/mistri/install/templates/migration.rb.tt +4 -2
- data/lib/generators/mistri/mcp/mcp_generator.rb +2 -2
- data/lib/generators/mistri/mcp/templates/model.rb.tt +21 -8
- data/lib/mistri/agent.rb +839 -141
- data/lib/mistri/call_order.rb +137 -0
- data/lib/mistri/child.rb +69 -13
- data/lib/mistri/compaction.rb +200 -35
- data/lib/mistri/compactor/text.rb +91 -0
- data/lib/mistri/compactor.rb +474 -117
- data/lib/mistri/console.rb +44 -11
- data/lib/mistri/content.rb +70 -3
- data/lib/mistri/definition.rb +2 -2
- data/lib/mistri/edit.rb +13 -7
- data/lib/mistri/errors.rb +37 -0
- data/lib/mistri/event.rb +20 -9
- data/lib/mistri/locks/rails_cache.rb +16 -2
- data/lib/mistri/locks.rb +12 -3
- data/lib/mistri/mcp/client.rb +247 -61
- data/lib/mistri/mcp/conversation.rb +161 -0
- data/lib/mistri/mcp/egress.rb +5 -4
- data/lib/mistri/mcp/mirror.rb +90 -0
- data/lib/mistri/mcp/negotiation.rb +222 -0
- data/lib/mistri/mcp/oauth/client_identity.rb +53 -0
- data/lib/mistri/mcp/oauth/tolerance.rb +61 -0
- data/lib/mistri/mcp/oauth.rb +303 -82
- data/lib/mistri/mcp/results.rb +97 -0
- data/lib/mistri/mcp/sdk_client.rb +47 -0
- data/lib/mistri/mcp/wires.rb +325 -52
- data/lib/mistri/mcp.rb +304 -55
- data/lib/mistri/memory.rb +1 -1
- data/lib/mistri/message.rb +45 -8
- data/lib/mistri/models.rb +146 -24
- data/lib/mistri/partial_json.rb +5 -5
- data/lib/mistri/providers/anthropic/assembler.rb +40 -3
- data/lib/mistri/providers/anthropic/serializer.rb +154 -22
- data/lib/mistri/providers/anthropic.rb +77 -12
- data/lib/mistri/providers/fake.rb +4 -1
- data/lib/mistri/providers/gemini/assembler.rb +63 -3
- data/lib/mistri/providers/gemini/serializer.rb +27 -16
- data/lib/mistri/providers/gemini.rb +56 -11
- data/lib/mistri/providers/openai/assembler.rb +36 -5
- data/lib/mistri/providers/openai/serializer.rb +76 -22
- data/lib/mistri/providers/openai.rb +91 -14
- data/lib/mistri/reminder.rb +35 -3
- data/lib/mistri/result.rb +6 -4
- data/lib/mistri/result_limit.rb +164 -0
- data/lib/mistri/sandbox/quickjs.rb +345 -0
- data/lib/mistri/sandbox.rb +18 -0
- data/lib/mistri/schema.rb +26 -2
- data/lib/mistri/scratchpad.rb +107 -0
- data/lib/mistri/session/log.rb +137 -0
- data/lib/mistri/session.rb +405 -75
- data/lib/mistri/sinks/logger.rb +407 -0
- data/lib/mistri/skills.rb +10 -4
- data/lib/mistri/spawner.rb +43 -12
- data/lib/mistri/sse.rb +3 -2
- data/lib/mistri/stores/active_record.rb +30 -6
- data/lib/mistri/stores/jsonl.rb +26 -7
- data/lib/mistri/stores/memory.rb +6 -2
- data/lib/mistri/sub_agent/execution.rb +11 -3
- data/lib/mistri/sub_agent/runtime.rb +11 -1
- data/lib/mistri/sub_agent.rb +13 -4
- data/lib/mistri/tool.rb +71 -7
- data/lib/mistri/tool_arguments.rb +10 -11
- data/lib/mistri/tool_context.rb +15 -3
- data/lib/mistri/tool_executor.rb +49 -26
- data/lib/mistri/tool_result.rb +8 -2
- data/lib/mistri/tool_search/index.rb +193 -0
- data/lib/mistri/tool_search.rb +200 -0
- data/lib/mistri/tools/delete_file.rb +22 -0
- data/lib/mistri/tools/edit_file.rb +24 -14
- data/lib/mistri/tools/find_in_file.rb +1 -0
- data/lib/mistri/tools/grep.rb +212 -0
- data/lib/mistri/tools/list_files.rb +38 -5
- data/lib/mistri/tools/read_file.rb +83 -21
- data/lib/mistri/tools/read_memory.rb +2 -1
- data/lib/mistri/tools/run_code.rb +167 -0
- data/lib/mistri/tools/write_file.rb +66 -9
- data/lib/mistri/tools.rb +72 -1
- data/lib/mistri/transport/streamable_http.rb +140 -0
- data/lib/mistri/transport.rb +53 -63
- data/lib/mistri/usage.rb +33 -4
- data/lib/mistri/version.rb +1 -1
- data/lib/mistri/workspace/active_record.rb +22 -10
- data/lib/mistri/workspace/directory.rb +5 -1
- data/lib/mistri/workspace/memory.rb +4 -0
- data/lib/mistri/workspace/path_space.rb +56 -0
- data/lib/mistri/workspace/single.rb +6 -1
- data/lib/mistri.rb +28 -4
- metadata +30 -2
data/docs/mcp.md
CHANGED
|
@@ -26,13 +26,26 @@ tools = Mistri::MCP.tools(
|
|
|
26
26
|
gates: { "create_issue" => true },
|
|
27
27
|
)
|
|
28
28
|
|
|
29
|
-
agent = Mistri.agent("claude-opus-
|
|
29
|
+
agent = Mistri.agent("claude-opus-5-5", tools: tools)
|
|
30
30
|
```
|
|
31
31
|
|
|
32
32
|
`token:` accepts a String or callable. The callable resolves for every request.
|
|
33
33
|
After a 401, Mistri resolves it once more and retries once, so the host can put
|
|
34
34
|
refresh behavior behind the callable.
|
|
35
35
|
|
|
36
|
+
A callable that takes `refused:` is given the token the server just refused,
|
|
37
|
+
when it is asked again after a 401. It can then replace that token only when
|
|
38
|
+
it is still the stored one, which matters when several processes share a
|
|
39
|
+
connection:
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
token: ->(refused: nil) { connection.bearer_token(refused: refused) }
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
After a 403 whose challenge says `insufficient_scope`, Mistri does not ask
|
|
46
|
+
again: a new token of the same sign-in has the same scope. See
|
|
47
|
+
[More scope](#more-scope).
|
|
48
|
+
|
|
36
49
|
Use `headers:` for fixed application headers. Mistri copies them at client
|
|
37
50
|
construction and rejects framing plus MCP session and protocol headers. A fixed
|
|
38
51
|
Authorization header is permitted; when `token:` is supplied, Mistri owns
|
|
@@ -42,6 +55,23 @@ One Client serializes its requests. Parallel Agent tool calls against the same
|
|
|
42
55
|
client queue rather than interleave. Use distinct clients only when the server
|
|
43
56
|
and host policy allow true parallel connections.
|
|
44
57
|
|
|
58
|
+
A server answers a request with a JSON body or with an SSE stream. A call
|
|
59
|
+
returns when its answer arrives. Most servers end the stream with the answer,
|
|
60
|
+
and the connection is then kept for the next request. A stream that is still
|
|
61
|
+
open a tenth of a second after the answer is left, and its connection is
|
|
62
|
+
closed.
|
|
63
|
+
|
|
64
|
+
A server may end a stream before the answer and expect the client back. When
|
|
65
|
+
an event of that stream carried an id, the client waits the `retry` the server
|
|
66
|
+
named, or one second, and asks for the rest with a GET and `Last-Event-ID`. It
|
|
67
|
+
does so once, and not when the wait is longer than `read_timeout`. The call is
|
|
68
|
+
never sent again.
|
|
69
|
+
|
|
70
|
+
An HTTP status that refuses a request is raised as the transport's error for
|
|
71
|
+
that status, such as `Mistri::ServerError`, with its `status` and `body`. The
|
|
72
|
+
message names the MCP server: `the MCP server answered HTTP 400`, followed by
|
|
73
|
+
the message of a JSON-RPC error when the body holds one.
|
|
74
|
+
|
|
45
75
|
### Stdio
|
|
46
76
|
|
|
47
77
|
```ruby
|
|
@@ -60,21 +90,157 @@ tools = Mistri::MCP.tools(
|
|
|
60
90
|
)
|
|
61
91
|
```
|
|
62
92
|
|
|
63
|
-
`command:` is an argument array, not a shell string.
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
93
|
+
`command:` is an argument array, not a shell string. Mistri writes one JSON-RPC
|
|
94
|
+
object per line and bounds the complete record read, including the time after
|
|
95
|
+
its first byte.
|
|
96
|
+
|
|
97
|
+
A line on the server's stdout that is not JSON is skipped. It is written to the
|
|
98
|
+
host's standard error, where the server's own standard error already goes, and
|
|
99
|
+
the wait for the message goes on.
|
|
100
|
+
|
|
101
|
+
The server does not inherit the host's environment, which holds provider keys
|
|
102
|
+
and other secrets. It starts with `env:` and these variables from the host
|
|
103
|
+
process:
|
|
104
|
+
|
|
105
|
+
| Purpose | Variables |
|
|
106
|
+
| --- | --- |
|
|
107
|
+
| Identity and programs | `HOME`, `USER`, `LOGNAME`, `SHELL`, `PATH`, `TERM` |
|
|
108
|
+
| RubyGems paths | `GEM_HOME`, `GEM_PATH` |
|
|
109
|
+
| Locale, time, and temp files | `LANG`, `LC_ALL`, `LC_CTYPE`, `TZ`, `TMPDIR` |
|
|
110
|
+
| CA bundles | `SSL_CERT_FILE`, `SSL_CERT_DIR`, `NODE_EXTRA_CA_CERTS`, `REQUESTS_CA_BUNDLE`, `CURL_CA_BUNDLE` |
|
|
111
|
+
|
|
112
|
+
Pass anything else through `env:`. It also overrides the variables above. For
|
|
113
|
+
example, `env: { "API_KEY" => key, **ENV.slice("HTTPS_PROXY", "NO_PROXY") }`
|
|
114
|
+
gives the server a credential and the host's proxy settings.
|
|
115
|
+
|
|
116
|
+
This is not a sandbox. The server runs as the host's user and can read the
|
|
117
|
+
host's files.
|
|
67
118
|
|
|
68
119
|
`@latest` is convenient for local exploration. Pin a reviewed package version
|
|
69
120
|
in an application or deployment so the executable does not change without a
|
|
70
121
|
code review.
|
|
71
122
|
|
|
72
|
-
Call `client.close` when a long-lived host is finished. For stdio, it
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
123
|
+
Call `client.close` when a long-lived host is finished. For stdio, it closes the
|
|
124
|
+
server's input and waits half a second for the server to end. A server that is
|
|
125
|
+
still running is sent SIGTERM, and SIGKILL one second after that. For HTTP, it
|
|
126
|
+
sends a DELETE with the session id, as the spec asks, on a connection that
|
|
127
|
+
waits five seconds at most, and forgets the session whatever the server
|
|
128
|
+
answers. It sends the token it last sent and does not ask the host for a new
|
|
129
|
+
one. The discovered tool cache remains available.
|
|
76
130
|
`call_tool` or `tools(refresh: true)` performs a fresh handshake when needed.
|
|
77
131
|
|
|
132
|
+
### Requests from a server
|
|
133
|
+
|
|
134
|
+
In the older form of the protocol a server may send its own requests while a
|
|
135
|
+
call waits for its answer. The client declares no capability, so it answers
|
|
136
|
+
`ping` with an empty result and refuses any other request with JSON-RPC error
|
|
137
|
+
-32601. Over Streamable HTTP the reply is a POST of its own, on a second
|
|
138
|
+
connection that waits five seconds at most, because the first is taken by the
|
|
139
|
+
stream of the call. A reply and a cancel carry the token last sent. The host's
|
|
140
|
+
token callable is asked only for a request.
|
|
141
|
+
|
|
142
|
+
A request is never taken for the answer to a call, whatever its id. An answer
|
|
143
|
+
is matched by the id of its call, also when the server sends that id back as a
|
|
144
|
+
string.
|
|
145
|
+
|
|
146
|
+
### What the host is told
|
|
147
|
+
|
|
148
|
+
```ruby
|
|
149
|
+
client = Mistri::MCP::Client.new(
|
|
150
|
+
url: server_url,
|
|
151
|
+
on_notification: ->(method, params) { Rails.logger.info("#{method} #{params}") },
|
|
152
|
+
progress: true,
|
|
153
|
+
)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
`on_notification:` is called with the method and the params of each
|
|
157
|
+
notification the server sends while the client reads: a changed tool list, a
|
|
158
|
+
progress notice, a log line. It runs inside the call, so it must not use the
|
|
159
|
+
client. What it raises is written to standard error with the prefix
|
|
160
|
+
`mistri:`, and the call goes on. A bridged tool's `timeout:` still ends the
|
|
161
|
+
call while it runs. The kept tool list is not changed by a notice. The host
|
|
162
|
+
lists again with `refresh: true` when it decides to.
|
|
163
|
+
|
|
164
|
+
A notice reaches the host when it arrives on a stream the client reads. Over
|
|
165
|
+
stdio that is the next call at the latest. Over HTTP it is the stream of a
|
|
166
|
+
call, up to a tenth of a second after its answer. A notice that a server
|
|
167
|
+
sends on a stream of its own, outside any call, is not read.
|
|
168
|
+
|
|
169
|
+
`progress: true` asks for progress with each tool call, by a token that is the
|
|
170
|
+
call's own request id. Each progress notice is a message, so a call that
|
|
171
|
+
reports progress is not ended by `read_timeout`. The limit of the bridged
|
|
172
|
+
tool still holds.
|
|
173
|
+
|
|
174
|
+
`client.state` is `:idle` before anything was asked, `:connected` once the
|
|
175
|
+
server answered, `:needs_sign_in` after it refused to authorize or the host's
|
|
176
|
+
token callable raised `OAuth::InvalidGrantError`, and `:failed`
|
|
177
|
+
when it could not be reached, its connection broke, a local server could not
|
|
178
|
+
be started, or it answered with an HTTP error. `client.failure` holds the
|
|
179
|
+
reason. A call that timed out, an answer over a limit and an error the server
|
|
180
|
+
answered with leave both as they were. The next answer makes the state
|
|
181
|
+
`:connected` again.
|
|
182
|
+
|
|
183
|
+
A bridged tool whose server cannot be used tells the model so, and names the
|
|
184
|
+
server by the `prefix:` the host gave it: `No connection to the MCP server
|
|
185
|
+
"tracker" could be established, so tracker__search did not run`, or `the MCP
|
|
186
|
+
server "tracker" needs a sign-in before its tools can be used`. A call to a
|
|
187
|
+
server whose connection could not be opened did not run, and is not reported
|
|
188
|
+
as one that may have completed. A tool whose `timeout:` passes before its
|
|
189
|
+
call is sent says that it did not run, too. That includes a call that waited
|
|
190
|
+
for another call of the same client, which runs one at a time.
|
|
191
|
+
|
|
192
|
+
### Protocol revisions
|
|
193
|
+
|
|
194
|
+
The client speaks two forms of the protocol. In the form of 2026-07-28 there
|
|
195
|
+
is no handshake and no session: each request carries the revision, the
|
|
196
|
+
client's name and its capabilities. In the older form, 2025-11-25 and three
|
|
197
|
+
revisions before it, a session opens with `initialize`.
|
|
198
|
+
|
|
199
|
+
Before its first request the client asks the server which revisions it speaks,
|
|
200
|
+
with `server/discover`. A server that offers 2026-07-28 is used in that form.
|
|
201
|
+
An older server does not know the request, and whatever it answers that is not
|
|
202
|
+
an error of the 2026-07-28 revision leads to the handshake. On stdio so does no
|
|
203
|
+
answer within five seconds, or within `read_timeout` when that is shorter. A
|
|
204
|
+
refusal to authorize, a server error and a lost connection say nothing about
|
|
205
|
+
the revision and are raised as they are.
|
|
206
|
+
|
|
207
|
+
The answer is kept for the life of the client. `client.listing` holds it as
|
|
208
|
+
`"protocolVersion"`, so a client built from a stored listing asks nothing: in
|
|
209
|
+
the form of 2026-07-28 its first request is the call itself.
|
|
210
|
+
|
|
211
|
+
A server can move from one form to the other after the answer was kept. When
|
|
212
|
+
a server refuses the handshake of a remembered older revision, or refuses a
|
|
213
|
+
request in the form of 2026-07-28 in a way that may be about its form, the
|
|
214
|
+
client asks it again. A refused sign-in, a timeout, a limit and an HTTP
|
|
215
|
+
status of 500 or above are not about its form. A request is sent a second time only to a
|
|
216
|
+
server that turned out to speak the other form, which refused the first for
|
|
217
|
+
its form. A server that still speaks the same form gave its answer, and that
|
|
218
|
+
is raised.
|
|
219
|
+
|
|
220
|
+
In the form of 2026-07-28:
|
|
221
|
+
|
|
222
|
+
- A request from the server gets no reply. The revision allows neither.
|
|
223
|
+
- Over HTTP a call that is given up is cancelled by its closed stream, with no
|
|
224
|
+
notification, and a stream that ended early is not asked for again.
|
|
225
|
+
- Over HTTP each request repeats its method in `Mcp-Method`, a call repeats
|
|
226
|
+
its tool in `Mcp-Name`, and each parameter that a tool marks with
|
|
227
|
+
`x-mcp-header` is repeated in `Mcp-Param-*`. A direct `call_tool` on a
|
|
228
|
+
client that holds no listing lists the tools first, to learn the marks. A
|
|
229
|
+
tool whose marks break the rules of the revision is left out of the list and
|
|
230
|
+
named in a warning on standard error. So is one in a stored listing, once
|
|
231
|
+
the client knows the server speaks 2026-07-28, and no parameter of such a
|
|
232
|
+
tool is ever repeated. `client.left_out` holds the names of the tools that
|
|
233
|
+
were left out. A tool that was bridged before that stays offered
|
|
234
|
+
until the host builds its tools again.
|
|
235
|
+
- `close` sends no DELETE, because there is no session.
|
|
236
|
+
- A server may answer a call by asking for input. The client declares no
|
|
237
|
+
capability to give any, so such a call ends as a tool error that says so,
|
|
238
|
+
with the server's own message. A result that asks for nothing and carries
|
|
239
|
+
`requestState` is sent again with it, three times at most.
|
|
240
|
+
- A header that does not match the request is raised as the server's refusal.
|
|
241
|
+
The spec suggests listing the tools again. The kept listing is the host's
|
|
242
|
+
pin, so the host decides.
|
|
243
|
+
|
|
78
244
|
## Tool discovery
|
|
79
245
|
|
|
80
246
|
`client.tools` performs the handshake and follows `tools/list` pagination once,
|
|
@@ -94,13 +260,301 @@ client = Mistri::MCP::Client.new(
|
|
|
94
260
|
Repeated cursors, malformed collections, and exceeded limits raise instead of
|
|
95
261
|
looping or allocating without bound.
|
|
96
262
|
|
|
263
|
+
### Storing a server's listing
|
|
264
|
+
|
|
265
|
+
Listing a server's tools takes a connection, and a stdio server must be
|
|
266
|
+
started first. With many servers that is too slow to repeat each time an
|
|
267
|
+
agent is built. `client.listing` returns the server's tools, its
|
|
268
|
+
`instructions` when it sent any, the revision it speaks, whether it has
|
|
269
|
+
resources, and the names of the tools the client left out, as plain JSON.
|
|
270
|
+
Store it, and pass it to the clients built later:
|
|
271
|
+
|
|
272
|
+
```ruby
|
|
273
|
+
# Once, in a job.
|
|
274
|
+
kept = Mistri::MCP::Client.new(url: server_url, token: token).listing
|
|
275
|
+
store.write("tracker", kept)
|
|
276
|
+
|
|
277
|
+
# Each time an agent is built.
|
|
278
|
+
client = Mistri::MCP::Client.new(url: server_url, token: token,
|
|
279
|
+
listing: store.read("tracker"))
|
|
280
|
+
tools = Mistri::MCP.tools(client, prefix: "tracker", deferred: true)
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
A client built with `listing:` returns `tools` and `instructions` from it and
|
|
284
|
+
sends no request. It connects when a tool is first called, and a stdio server
|
|
285
|
+
is not started before that. Building an agent over many servers then opens no
|
|
286
|
+
connection.
|
|
287
|
+
|
|
288
|
+
`client.listing(refresh: true)` lists the server again. When to do that is
|
|
289
|
+
the host's decision. Until then a tool the server has removed fails when it
|
|
290
|
+
is called, and a tool it has added is not offered.
|
|
291
|
+
|
|
292
|
+
### Server instructions
|
|
293
|
+
|
|
294
|
+
A server may send `instructions` in its initialize result: text on how to use
|
|
295
|
+
its tools. `client.instructions` returns it, and `Mistri::MCP.tools` sets it
|
|
296
|
+
on every tool it bridges, as `Tool#instructions`.
|
|
297
|
+
|
|
298
|
+
The text is the server's, and the host decides whether the model reads it,
|
|
299
|
+
and with what weight:
|
|
300
|
+
|
|
301
|
+
```ruby
|
|
302
|
+
Mistri::MCP.tools(client) # the server's text
|
|
303
|
+
Mistri::MCP.tools(client, instructions: false) # none
|
|
304
|
+
Mistri::MCP.tools(client, instructions: "Use for orders.") # the host's text
|
|
305
|
+
Mistri::MCP.tools(client, instructions_as: :user) # as text of a user turn
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
An agent appends the instructions of every server to the conversation with
|
|
309
|
+
its first request, before the model chooses a tool, so they can guide the
|
|
310
|
+
choice. They are appended as one reminder, a system message of the
|
|
311
|
+
conversation whose `reminder` is `"tool_instructions"`. It holds one text
|
|
312
|
+
block for each server, which names that server's tools. A server's
|
|
313
|
+
instructions are cut at 2,000 bytes.
|
|
314
|
+
|
|
315
|
+
When the text blocks of all servers are more than 20,000 bytes together, the
|
|
316
|
+
instructions of a server whose tools are all deferred are appended with the
|
|
317
|
+
request after one of its tools is loaded. A server with a tool that is not
|
|
318
|
+
deferred is not delayed.
|
|
319
|
+
|
|
320
|
+
After a compaction that summarized them, the instructions are appended again
|
|
321
|
+
by the same rule.
|
|
322
|
+
|
|
323
|
+
The instructions are text from the server, and they are sent the way every
|
|
324
|
+
[reminder](tool-contracts.md#tools-on-demand) is: as a system message to an
|
|
325
|
+
Anthropic model that takes one in a conversation, as a developer message to
|
|
326
|
+
OpenAI, and otherwise as text of a user turn in a system-reminder block. Each
|
|
327
|
+
block names the tools it is about. A model gives such a message the weight of
|
|
328
|
+
your own instructions, so give a server's instructions the trust you give
|
|
329
|
+
that server.
|
|
330
|
+
|
|
331
|
+
`instructions_as: :user` gives a server's instructions less weight. They are
|
|
332
|
+
then sent as text of a user turn in a system-reminder block on every provider,
|
|
333
|
+
and never as a system or developer message. The reminder they are appended as
|
|
334
|
+
is `"tool_instructions_as_user"`. When tools of one server are bridged under
|
|
335
|
+
one prefix with both weights, the server's instructions are sent once, as
|
|
336
|
+
text of a user turn.
|
|
337
|
+
|
|
338
|
+
### What else a server's text can do
|
|
339
|
+
|
|
340
|
+
A tool's description is text from the server that the model reads as guidance.
|
|
341
|
+
`Mistri::MCP.tools` cuts one at 4,096 characters and ends it with a line that
|
|
342
|
+
says so. `description_limit:` sets another number, which is an integer that
|
|
343
|
+
is not negative, and `nil` sets none. Of
|
|
344
|
+
350 definitions from 32 public servers the longest description has 3,674
|
|
345
|
+
characters.
|
|
346
|
+
|
|
347
|
+
A server marks a tool that only reads with `readOnlyHint`, and such a tool may
|
|
348
|
+
run beside others in a turn. Nothing checks the hint. `trust_hints: false`
|
|
349
|
+
means no tool is marked by it, only by the host's own `parallel:`.
|
|
350
|
+
|
|
351
|
+
A server can change a tool's definition after a person approved it.
|
|
352
|
+
`Mistri::MCP.digest(definition)` is the SHA-256 of a definition, as JSON with
|
|
353
|
+
the keys of every object in order, to keep with what was approved.
|
|
354
|
+
`Mistri::MCP.changes(kept, fresh)` takes two listings and returns the names of
|
|
355
|
+
the tools that were added, removed and changed:
|
|
356
|
+
|
|
357
|
+
```ruby
|
|
358
|
+
kept = store.read("tracker")
|
|
359
|
+
fresh = client.listing(refresh: true)
|
|
360
|
+
Mistri::MCP.changes(kept, fresh)
|
|
361
|
+
# => { "added" => [], "removed" => ["archive"], "changed" => ["search"] }
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
The client itself never replaces a kept listing. What to do with a changed
|
|
365
|
+
definition is the host's decision. When a listing names one tool twice, the
|
|
366
|
+
first definition is the one compared, as it is the one bridged.
|
|
367
|
+
|
|
97
368
|
`Mistri::MCP.tools` filters remote names with `allow:` and `deny:`. `prefix:`
|
|
98
369
|
creates local names such as `linear__create_issue` while the remote call still
|
|
99
|
-
uses `create_issue`. Duplicate local tool names make Agent
|
|
100
|
-
one server can never silently shadow another.
|
|
370
|
+
uses `create_issue`. Duplicate local tool names from two servers make Agent
|
|
371
|
+
construction fail; one server can never silently shadow another.
|
|
372
|
+
|
|
373
|
+
A local name is one every provider takes: letters, digits, underscores and
|
|
374
|
+
hyphens, 64 characters at most, not starting with a digit or a hyphen. A name
|
|
375
|
+
inside that set is not changed. MCP allows more, and so does a single provider
|
|
376
|
+
here and there, such as a dot on Gemini or a leading digit on Anthropic. Any
|
|
377
|
+
such name has its unsafe characters replaced and ends in eight characters of a
|
|
378
|
+
hash of the whole name, so `issues.list/open` becomes `issues_list_open_` and
|
|
379
|
+
the hash. The local name depends only on the prefix and the remote name: it is
|
|
380
|
+
the same in every run, whatever else the server lists.
|
|
381
|
+
`Mistri::MCP.local_name(remote, prefix)` gives it. A host that keeps names of
|
|
382
|
+
its own passes one with `bridge(client, definition, name:)`, and that name is
|
|
383
|
+
used as given.
|
|
384
|
+
|
|
385
|
+
A tool that cannot be bridged is left out, and the server's other tools stay
|
|
386
|
+
usable. That is a tool whose schema is not one Mistri can validate, one with
|
|
387
|
+
no name, one whose name or description is not UTF-8 text, a definition that
|
|
388
|
+
is not an object, and a tool whose local name is already taken by one listed
|
|
389
|
+
before it. Without a block the tool and the reason are written as a warning.
|
|
390
|
+
With one, the block is given what the server listed and the error, whose
|
|
391
|
+
message names the tool:
|
|
392
|
+
|
|
393
|
+
```ruby
|
|
394
|
+
tools = Mistri::MCP.tools(client, prefix: "tracker") do |_definition, error|
|
|
395
|
+
connection.note_unusable(error.message)
|
|
396
|
+
end
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
What the server listed may not be an object, so check before reading a key
|
|
400
|
+
from it. A host that would sooner fail raises the error from the block.
|
|
401
|
+
`Mistri::MCP.bridge`, which takes one definition, still raises.
|
|
402
|
+
|
|
403
|
+
Two listed tools can get the same local name: the server lists one name twice,
|
|
404
|
+
lists a tool under the name Mistri made for another, or lists two long names
|
|
405
|
+
that share their first 55 characters and the short hash. The first one listed
|
|
406
|
+
keeps the name, so the order the server lists in decides which remote tool the
|
|
407
|
+
name calls. A host that stores local names or holds approvals across runs for
|
|
408
|
+
such a server should raise from the block.
|
|
101
409
|
|
|
102
410
|
`needs_approval:` gates every bridged tool. `gates:` sets policy by remote name
|
|
103
|
-
and overrides the shared default.
|
|
411
|
+
and overrides the shared default. A key of `gates:` that names no tool of the
|
|
412
|
+
server raises `Mistri::ConfigurationError`: a name spelled another way, or a
|
|
413
|
+
symbol, would leave the tool it was meant for to run with no approval. A key
|
|
414
|
+
may name a tool that is not offered: one that `allow:` or `deny:` leaves out,
|
|
415
|
+
or one that the client left out itself.
|
|
416
|
+
|
|
417
|
+
A bridged tool is marked `parallel` when its server lists it with
|
|
418
|
+
`readOnlyHint: true`, so it runs beside its marked neighbours in a turn. Any
|
|
419
|
+
other tool runs alone, in the order the model wrote, see
|
|
420
|
+
[The order of a turn's calls](tool-contracts.md#the-order-of-a-turns-calls).
|
|
421
|
+
The hint is the server's own word and nothing checks it. `parallel: false`
|
|
422
|
+
runs every tool of the server alone, `parallel: true` marks every tool, and a
|
|
423
|
+
hash by remote name sets single tools and leaves the rest to their hint. A
|
|
424
|
+
key that names no tool of the server raises `Mistri::ConfigurationError`. The
|
|
425
|
+
check is made against the tools the client has listed, so a server that drops
|
|
426
|
+
or renames that tool makes `MCP.tools` raise until the hash is changed. A
|
|
427
|
+
tool that only waits is read-only and still has to come before the call after
|
|
428
|
+
it, so set it to false:
|
|
429
|
+
|
|
430
|
+
```ruby
|
|
431
|
+
Mistri::MCP.tools(browser, parallel: { "browser_wait_for" => false })
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
One client sends its requests one at a time, so marked calls to one server
|
|
435
|
+
gain no time beside each other. They gain it beside other servers and other
|
|
436
|
+
marked tools.
|
|
437
|
+
|
|
438
|
+
`deferred: true` keeps a server's tools out of the model's sight until a search
|
|
439
|
+
loads them, so the definitions of a server with a long tool list are left out
|
|
440
|
+
of every request until one of its tools is needed. See
|
|
441
|
+
[Tools on demand](tool-contracts.md#tools-on-demand).
|
|
442
|
+
|
|
443
|
+
## Many servers
|
|
444
|
+
|
|
445
|
+
An agent can be given the tools of tens or hundreds of servers. Four things
|
|
446
|
+
keep that cheap, and each is a host decision that Mistri supports:
|
|
447
|
+
|
|
448
|
+
1. List each server once, in a job, and store the listing. Listing takes a
|
|
449
|
+
connection for each server, so run the servers in parallel, wait a bounded
|
|
450
|
+
time for all of them together, and let one failure leave the others:
|
|
451
|
+
|
|
452
|
+
```ruby
|
|
453
|
+
clients = servers.transform_values { |url| Mistri::MCP::Client.new(url: url) }
|
|
454
|
+
threads = clients.transform_values { |client| Thread.new { client.listing } }
|
|
455
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + 30
|
|
456
|
+
threads.each do |name, thread|
|
|
457
|
+
left = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
458
|
+
store.write(name, thread.value) if thread.join([left, 0].max)
|
|
459
|
+
rescue StandardError => e
|
|
460
|
+
report(name, e)
|
|
461
|
+
end
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
A server that has not answered by then is left for the next run of the
|
|
465
|
+
job. Its client is still `:idle`, and its thread ends by the client's own
|
|
466
|
+
timeouts. `client.state` and `client.failure` say how each of the others
|
|
467
|
+
ended. Mistri has no helper for this: it would start threads inside the
|
|
468
|
+
host's process, and how many a process may hold, and for how long, is the
|
|
469
|
+
host's decision.
|
|
470
|
+
|
|
471
|
+
2. Build the clients from the stored listings and bridge them once for the
|
|
472
|
+
process. Bridging validates each schema, so it is the part worth doing
|
|
473
|
+
once. Give every agent the same tool objects:
|
|
474
|
+
|
|
475
|
+
```ruby
|
|
476
|
+
TOOLS = servers.flat_map do |name, url|
|
|
477
|
+
listing = store.read(name)
|
|
478
|
+
next [] unless listing
|
|
479
|
+
|
|
480
|
+
client = Mistri::MCP::Client.new(url: url, listing: listing)
|
|
481
|
+
Mistri::MCP.tools(client, prefix: name, deferred: true)
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
Mistri.agent("claude-haiku-5-5", tools: [*own_tools, *TOOLS])
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
No connection is opened here. A server without a stored listing is left
|
|
488
|
+
out, because a client built with `listing: nil` lists the server itself
|
|
489
|
+
and connects to do so. A client connects when one of its tools is first
|
|
490
|
+
called, and one client runs one call at a time. A host whose servers take
|
|
491
|
+
a token for each user builds the clients of that user instead and keeps
|
|
492
|
+
them for the user's session. After a listing is stored again, bridge that
|
|
493
|
+
server again and replace its tools: a bridged tool keeps the definition
|
|
494
|
+
it was built from.
|
|
495
|
+
|
|
496
|
+
3. Defer the tools, as above. The conversation then holds the names of the
|
|
497
|
+
tools and the instructions of the servers, and a request carries the
|
|
498
|
+
definition of a tool once a search has loaded it. When the instructions
|
|
499
|
+
are more than 20,000 bytes together, a server's instructions are appended
|
|
500
|
+
once one of its tools is loaded. See
|
|
501
|
+
[Tools on demand](tool-contracts.md#tools-on-demand) and
|
|
502
|
+
[Server instructions](#server-instructions).
|
|
503
|
+
|
|
504
|
+
4. Expect a server to be unreachable when it is first used. Whether a server
|
|
505
|
+
can be reached is not known until one of its tools is called. If no
|
|
506
|
+
connection can be established then, the tool's result states that it did
|
|
507
|
+
not run, and the run continues.
|
|
508
|
+
|
|
509
|
+
Measured in an application with 22 servers and 303 tools: listing every
|
|
510
|
+
server took 21.6 seconds, once. After that, building the 22 clients took
|
|
511
|
+
34 ms, bridging 88 ms once for the process, and building an agent 0.5 ms. The
|
|
512
|
+
first search of the process took 33.5 ms and each later agent's first search
|
|
513
|
+
0.5 ms. Every request carried two tool definitions until a search loaded
|
|
514
|
+
more.
|
|
515
|
+
|
|
516
|
+
## A client of your own
|
|
517
|
+
|
|
518
|
+
`Mistri::MCP.tools` and `Mistri::MCP.bridge` take any object that answers as a
|
|
519
|
+
client does. They need two methods and use three more when they are there:
|
|
520
|
+
|
|
521
|
+
| Method | Needed | What it returns |
|
|
522
|
+
| --- | --- | --- |
|
|
523
|
+
| `tools` | Yes | An array of hashes with `"name"`, and `"description"`, `"inputSchema"` and `"annotations"` when the server sent them |
|
|
524
|
+
| `call_tool(name, arguments)` | Yes | The result of the call: a hash with `"content"`, and `"isError"` and `"structuredContent"` when present |
|
|
525
|
+
| `instructions` | No | The server's instructions, or nil |
|
|
526
|
+
| `resources?` and `read_resource(uri)` | No | Whether the server has resources, and the contents of one |
|
|
527
|
+
|
|
528
|
+
An error the client raises becomes the tool's error. One that is tagged
|
|
529
|
+
`Mistri::MCP::NotSent`, or is a `Mistri::AuthenticationError`, is told to the
|
|
530
|
+
model as a server that could not be used.
|
|
531
|
+
|
|
532
|
+
A host uses this to put something in front of a client. One that lists a
|
|
533
|
+
stored set of tools and starts its server on the first call is a few lines:
|
|
534
|
+
|
|
535
|
+
```ruby
|
|
536
|
+
class LazyServer
|
|
537
|
+
def initialize(listed) = @listed = listed
|
|
538
|
+
def tools = @listed
|
|
539
|
+
def call_tool(name, arguments = {}) = client.call_tool(name, arguments)
|
|
540
|
+
|
|
541
|
+
private
|
|
542
|
+
|
|
543
|
+
def client = @client ||= Mistri::MCP::Client.new(command: ["a-server"])
|
|
544
|
+
end
|
|
545
|
+
```
|
|
546
|
+
|
|
547
|
+
The official MCP Ruby SDK's client, the `mcp` gem, answers in another shape:
|
|
548
|
+
its tools are objects, and a call returns the whole response. Wrap it:
|
|
549
|
+
|
|
550
|
+
```ruby
|
|
551
|
+
sdk = MCP::Client.new(transport: transport)
|
|
552
|
+
sdk.connect(client_info: { name: "YourApp", version: "1.0" })
|
|
553
|
+
tools = Mistri::MCP.tools(Mistri::MCP::SDKClient.new(sdk), prefix: "files")
|
|
554
|
+
```
|
|
555
|
+
|
|
556
|
+
Mistri does not depend on that gem. The host builds the SDK's client,
|
|
557
|
+
connects it and closes it.
|
|
104
558
|
|
|
105
559
|
## Schema authority
|
|
106
560
|
|
|
@@ -134,8 +588,16 @@ external references are rejected even with a complete validator so validation
|
|
|
134
588
|
never performs hidden file or network resolution.
|
|
135
589
|
|
|
136
590
|
An omitted `inputSchema` means a closed no-argument object. Explicit null,
|
|
137
|
-
non-object roots
|
|
138
|
-
|
|
591
|
+
non-object roots and malformed keyword shapes are configuration errors.
|
|
592
|
+
|
|
593
|
+
A schema may declare JSON Schema 2020-12 or draft-07, which is what common
|
|
594
|
+
schema generators emit. The two read the same for everything Mistri validates
|
|
595
|
+
locally, apart from three shapes that are refused for that one tool: `items`
|
|
596
|
+
as an array, a constraint beside a `$ref` (draft-07 ignores it and 2020-12
|
|
597
|
+
applies it), and `prefixItems` in a draft-07 schema. Every other rule on this
|
|
598
|
+
page holds for a draft-07 schema as well, so a shape draft-07 allows and
|
|
599
|
+
Mistri does not, such as an `$id` that is only a fragment, is still refused.
|
|
600
|
+
Any other declared dialect is a configuration error.
|
|
139
601
|
|
|
140
602
|
## Results and large data
|
|
141
603
|
|
|
@@ -155,6 +617,56 @@ Large results should live behind a server or host resource. Return an MCP
|
|
|
155
617
|
payload. Mistri renders the URI for the model and never fetches it automatically;
|
|
156
618
|
fetching credentials, retention, and access policy belong to the host or server.
|
|
157
619
|
|
|
620
|
+
### Resources
|
|
621
|
+
|
|
622
|
+
A server that has resources declares so, and `client.resources?` says whether
|
|
623
|
+
it did. `client.read_resource(uri)` returns the contents of one: hashes with
|
|
624
|
+
`"uri"`, `"mimeType"`, and `"text"` or `"blob"`.
|
|
625
|
+
|
|
626
|
+
For such a server `Mistri::MCP.tools` returns one more tool, `read_resource`
|
|
627
|
+
under the server's prefix. It takes a URI and returns what the server holds
|
|
628
|
+
there. A link in a result of the server's tools then tells the model how to
|
|
629
|
+
follow it:
|
|
630
|
+
|
|
631
|
+
```text
|
|
632
|
+
[Resource notes://today "Today's notes" (text/plain, 10 bytes). What to buy. Read it with notes__read_resource.]
|
|
633
|
+
```
|
|
634
|
+
|
|
635
|
+
- `resources: false` leaves the tool out, and a link is then the line
|
|
636
|
+
`[resource: notes://today]`, as before.
|
|
637
|
+
- `resources: true` offers the tool for a server that returns links and
|
|
638
|
+
declares no resources.
|
|
639
|
+
- `allow:` and `deny:` decide it under the name `read_resource`, like any
|
|
640
|
+
tool the server lists. With `allow: ["search"]` it is left out, and a link
|
|
641
|
+
is the line `[resource: notes://today]`.
|
|
642
|
+
- `gates: { "read_resource" => true }` makes a read wait for approval. The
|
|
643
|
+
tool follows `deferred:` and `timeout:` like the server's own tools. It is
|
|
644
|
+
marked to run beside other calls, unless `parallel: false` or
|
|
645
|
+
`parallel: { "read_resource" => false }` says it runs alone.
|
|
646
|
+
- A server that lists a tool named `read_resource` keeps that name, and
|
|
647
|
+
Mistri's tool is left out.
|
|
648
|
+
|
|
649
|
+
The tool reads whatever URI the model gives it, from that one server. A host
|
|
650
|
+
that reviews a server's tools one by one reviews this one too, or leaves it
|
|
651
|
+
out.
|
|
652
|
+
|
|
653
|
+
A result may also hold a resource itself. Its text is shown. Bytes are shown
|
|
654
|
+
when they are an image, read as text when their type is a text type, and
|
|
655
|
+
otherwise kept in the agent's [scratchpad](context-and-workspaces.md), where
|
|
656
|
+
the result says they are:
|
|
657
|
+
|
|
658
|
+
```text
|
|
659
|
+
[Binary resource notes://report.pdf (application/pdf, 48211 bytes) saved at scratchpad/tool-results/20261126T030609-notes__report-d53873f1.pdf]
|
|
660
|
+
```
|
|
661
|
+
|
|
662
|
+
An agent with no scratchpad is told the type and the size and that the bytes
|
|
663
|
+
are not shown. A scratchpad whose workspace holds only text, such as one in a
|
|
664
|
+
text column, cannot take them, and the result says so.
|
|
665
|
+
|
|
666
|
+
`Mistri::MCP.answer(result)`, for a host that bridges tools itself, shows the
|
|
667
|
+
image and reads the text. It has no tool to name and no scratchpad, so a link
|
|
668
|
+
and other bytes are the line `[resource: uri]`.
|
|
669
|
+
|
|
158
670
|
## Response boundaries
|
|
159
671
|
|
|
160
672
|
Provider and MCP JSON bodies, individual SSE lines, and stdio records default
|
|
@@ -172,15 +684,79 @@ Successful HTTP bodies are counted incrementally, and a declared oversized body
|
|
|
172
684
|
fails before it is read. Requests use identity encoding so compressed expansion
|
|
173
685
|
cannot bypass the boundary.
|
|
174
686
|
|
|
175
|
-
Each completed SSE JSON record also
|
|
176
|
-
|
|
177
|
-
|
|
687
|
+
Each completed MCP SSE JSON record is also checked before JSON parsing for 64
|
|
688
|
+
levels and 64 KiB per numeric token. The number of values in a record is not
|
|
689
|
+
counted, on a stream or on any other transport, so a server with a long tool
|
|
690
|
+
list answers the same however it replies.
|
|
178
691
|
|
|
179
692
|
Byte overflow raises `Mistri::ResponseTooLargeError`. Structural overflow raises
|
|
180
693
|
`Mistri::ResponseTooComplexError`. Both expose `kind` and `limit`.
|
|
181
694
|
|
|
182
|
-
Either failure
|
|
183
|
-
handshake
|
|
695
|
+
Either failure closes the connection. Over HTTP the session is kept. Over stdio
|
|
696
|
+
the server is ended, and the next operation starts one and does the handshake
|
|
697
|
+
again.
|
|
698
|
+
|
|
699
|
+
### The time one call may take
|
|
700
|
+
|
|
701
|
+
`read_timeout:` is the wait for one message from the server, over stdio and
|
|
702
|
+
over HTTP. A comment that a server sends to keep a stream open is not a
|
|
703
|
+
message and does not start the wait again. When the wait passes, the client
|
|
704
|
+
sends `notifications/cancelled` for that request and stops waiting. It keeps
|
|
705
|
+
the stdio server, or the HTTP session. An answer that arrives later is
|
|
706
|
+
ignored. A list raises `Mistri::MCP::Error`. A tool call raises
|
|
707
|
+
`Mistri::AmbiguousDeliveryError`, because the tool may have run. The handshake
|
|
708
|
+
is never cancelled: a server that does not answer `initialize` is ended.
|
|
709
|
+
|
|
710
|
+
A timeout does not end a stdio server. A request with no answer leaves a
|
|
711
|
+
doubt about it. Before the next request, the client asks the server one
|
|
712
|
+
question that changes nothing: `ping`, or `server/discover` for a server of
|
|
713
|
+
2026-07-28, which has no `ping`. The server has `read_timeout` to give that
|
|
714
|
+
sign of life, counted from the request that got no answer. So a server that
|
|
715
|
+
handles one request at a time can first finish that request. A question that
|
|
716
|
+
is asked later than that still gets five seconds, or `read_timeout` when that
|
|
717
|
+
is shorter. The time is for the answer, and other messages do not extend it.
|
|
718
|
+
|
|
719
|
+
- A server that answers was slow. It is kept, with everything it holds.
|
|
720
|
+
- A server that does not answer is ended. A line on standard error with the
|
|
721
|
+
prefix `mistri:` says so. The request starts a new server with a new
|
|
722
|
+
handshake.
|
|
723
|
+
|
|
724
|
+
The question is part of the next call, so the limit of that call applies to it
|
|
725
|
+
too. A call whose limit ends the question did not run, and the model reads
|
|
726
|
+
that. The call after it asks again, with the time that is left. Two settings
|
|
727
|
+
leave a server that is gone to the host, which ends it with `client.close`:
|
|
728
|
+
`read_timeout: nil`, where the question has no limit of its own, and calls
|
|
729
|
+
with a limit shorter than the time one question gets.
|
|
730
|
+
|
|
731
|
+
A server that answers the question and still never answers a call is kept, and
|
|
732
|
+
each call to it waits for the timeout. Whether such a server is worth keeping
|
|
733
|
+
is the host's decision: `client.close` ends it, and the next call starts a new
|
|
734
|
+
one. A server over HTTP is never asked, because it is not the client's to end.
|
|
735
|
+
|
|
736
|
+
A call is also written under `read_timeout`. When a stdio server has stopped
|
|
737
|
+
reading its input, a call with large arguments ends there. Half a message is
|
|
738
|
+
then left in that server's input, so the server is ended, and the next call
|
|
739
|
+
starts a new one with a new handshake.
|
|
740
|
+
|
|
741
|
+
A server that keeps sending messages and never answers the call starts the
|
|
742
|
+
wait again each time, so a bridged tool also has a limit for the whole call:
|
|
743
|
+
|
|
744
|
+
```ruby
|
|
745
|
+
Mistri::MCP.tools(client) # 300 seconds, Mistri::MCP::CALL_TIMEOUT
|
|
746
|
+
Mistri::MCP.tools(client, timeout: 900) # the host's number
|
|
747
|
+
Mistri::MCP.tools(client, timeout: nil) # no limit
|
|
748
|
+
```
|
|
749
|
+
|
|
750
|
+
`Mistri::MCP.bridge` takes the same option. The limit is the tool's own
|
|
751
|
+
`timeout`, so the tool executor ends the call. The model reads that the tool
|
|
752
|
+
timed out and may have completed, and the agent goes on. The client sends
|
|
753
|
+
`notifications/cancelled` for the call, over stdio and over Streamable HTTP.
|
|
754
|
+
The server and its session are kept. Before the next call a stdio server is
|
|
755
|
+
asked for a sign of life, as after a timeout. A call that waits behind another
|
|
756
|
+
call to the same server spends its limit waiting.
|
|
757
|
+
|
|
758
|
+
A direct `Client#call_tool` has no such limit. A host that puts its own
|
|
759
|
+
`Timeout` around one gets the same cancel and keeps the server.
|
|
184
760
|
|
|
185
761
|
### Ambiguous tool delivery
|
|
186
762
|
|
|
@@ -199,12 +775,25 @@ replay the call. A model may choose to issue a new call after reading the error,
|
|
|
199
775
|
so approval, idempotency, and reconciliation remain host policy.
|
|
200
776
|
|
|
201
777
|
If a matching result arrived before an invalid trailing SSE record, that result
|
|
202
|
-
remains authoritative. The
|
|
778
|
+
remains authoritative. The connection is closed and the session is kept.
|
|
203
779
|
|
|
204
780
|
Session-expired and authentication retries occur only through explicit server
|
|
205
781
|
responses that establish the request was not accepted under the old session or
|
|
206
782
|
credential. An ambiguous outcome never takes those replay paths.
|
|
207
783
|
|
|
784
|
+
The opposite case is a call that was never sent. A client connects on its
|
|
785
|
+
first request, so a server that cannot be reached fails in the handshake,
|
|
786
|
+
before `tools/call` leaves. `Client#call_tool` raises a copy of that error
|
|
787
|
+
tagged with `Mistri::MCP::NotSent`, of the same class. A bridged tool turns it
|
|
788
|
+
into an error result that states no connection could be established and the
|
|
789
|
+
tool did not run, so the model does not report a partial effect.
|
|
790
|
+
|
|
791
|
+
A server of 2026-07-28 may ask for a call to be sent again with state. Once it
|
|
792
|
+
has answered one round, the call has reached it. A later round that is not
|
|
793
|
+
delivered, because the token callable raised, no connection could be opened or
|
|
794
|
+
the server did not authorize it, raises `Mistri::AmbiguousDeliveryError`, and
|
|
795
|
+
is never reported as a call that did not run.
|
|
796
|
+
|
|
208
797
|
## Remote network policy
|
|
209
798
|
|
|
210
799
|
Remote MCP and server-side OAuth URLs are untrusted input. By default Mistri:
|
|
@@ -285,6 +874,7 @@ end
|
|
|
285
874
|
|
|
286
875
|
tokens = Mistri::MCP::OAuth.complete(
|
|
287
876
|
code: params.fetch(:code),
|
|
877
|
+
iss: params[:iss],
|
|
288
878
|
code_verifier: flow.fetch("code_verifier"),
|
|
289
879
|
client_id: flow.fetch("client_id"),
|
|
290
880
|
client_secret: flow["client_secret"],
|
|
@@ -296,6 +886,23 @@ tokens = Mistri::MCP::OAuth.complete(
|
|
|
296
886
|
)
|
|
297
887
|
```
|
|
298
888
|
|
|
889
|
+
`iss:` is the `iss` parameter of the callback, or nil when the callback had
|
|
890
|
+
none. Mistri refuses a callback that names another issuer than the one the
|
|
891
|
+
sign-in was started with, and one that names none when the authorization
|
|
892
|
+
server's metadata says it sends one. It compares the two as they are written,
|
|
893
|
+
and it does so before the code is sent anywhere. This is the defence against
|
|
894
|
+
an authorization server that tries to collect a code meant for another
|
|
895
|
+
(RFC 9207). A host that leaves `iss:` out gets no such check, because Mistri
|
|
896
|
+
cannot see the callback.
|
|
897
|
+
|
|
898
|
+
A callback may carry `error` in place of a code. Check its issuer the same
|
|
899
|
+
way before the error is shown to anyone:
|
|
900
|
+
|
|
901
|
+
```ruby
|
|
902
|
+
oauth = Mistri::MCP::OAuth
|
|
903
|
+
oauth.verify_response_issuer(params[:iss], oauth.bound_metadata(flow.fetch("issuer")))
|
|
904
|
+
```
|
|
905
|
+
|
|
299
906
|
Refresh tokens can rotate, so persist a returned replacement:
|
|
300
907
|
|
|
301
908
|
```ruby
|
|
@@ -317,7 +924,10 @@ does this.
|
|
|
317
924
|
|
|
318
925
|
Mistri requires S256 PKCE. It follows protected-resource and authorization-server
|
|
319
926
|
discovery, binds metadata to exact resource and issuer identifiers, and honors
|
|
320
|
-
challenge scope. It does not add `offline_access` as gem policy.
|
|
927
|
+
challenge scope. It does not add `offline_access` as gem policy. A host that
|
|
928
|
+
wants a refresh token passes `offline_access: true` to `start`: the scope is
|
|
929
|
+
then added when the authorization server lists it, and only to a scope that
|
|
930
|
+
is being asked for.
|
|
321
931
|
|
|
322
932
|
When discovery advertises multiple authorization servers, the host must pass
|
|
323
933
|
`issuer:` explicitly. A pre-registered `client_id` always requires the exact
|
|
@@ -327,6 +937,129 @@ also accept `client_secret:` and an explicit supported `token_auth_method:`.
|
|
|
327
937
|
OAuth response bodies are uncompressed and bounded to 256 KiB. Provider error
|
|
328
938
|
descriptions are not copied into credential-bearing exceptions.
|
|
329
939
|
|
|
940
|
+
### How the application names itself
|
|
941
|
+
|
|
942
|
+
There are three ways, and `start` takes them in this order:
|
|
943
|
+
|
|
944
|
+
1. **A client set up by hand.** Pass `client_id:`, with `client_secret:` and
|
|
945
|
+
`token_auth_method:` when the server gave them, and the `issuer:` it was
|
|
946
|
+
set up at.
|
|
947
|
+
2. **A client ID metadata document.** Pass `client_metadata_url:`, the https
|
|
948
|
+
address where the application serves the document. A server whose metadata
|
|
949
|
+
says `client_id_metadata_document_supported` gets that address as the
|
|
950
|
+
client ID, and nothing is registered. The 2026-07-28 revision prefers this
|
|
951
|
+
way.
|
|
952
|
+
3. **Dynamic registration**, when neither applies and the server offers it.
|
|
953
|
+
Mistri says what kind of application it registers: `native` for a loopback
|
|
954
|
+
redirect URI and `web` for any other.
|
|
955
|
+
|
|
956
|
+
The document is the same for every server, so one route serves it:
|
|
957
|
+
|
|
958
|
+
```ruby
|
|
959
|
+
render json: Mistri::MCP::OAuth.client_metadata(
|
|
960
|
+
url: client_metadata_url,
|
|
961
|
+
client_name: "YourApp",
|
|
962
|
+
redirect_uris: [callback_url],
|
|
963
|
+
)
|
|
964
|
+
```
|
|
965
|
+
|
|
966
|
+
A client is bound to the issuer that gave it. Store the `issuer` beside the
|
|
967
|
+
client ID, and pass both again when the same client signs in later. When the
|
|
968
|
+
server no longer names that issuer, `start` raises
|
|
969
|
+
`Mistri::MCP::OAuth::IssuerChanged` and registers nothing. A host then drops a
|
|
970
|
+
client that was registered dynamically and calls `start` without it, and shows
|
|
971
|
+
the error for one that was set up by hand. Mistri does not register with a
|
|
972
|
+
new authorization server by itself, because whether to trust it is the host's
|
|
973
|
+
decision. A document's address is the same client at every issuer.
|
|
974
|
+
|
|
975
|
+
### A refused token request
|
|
976
|
+
|
|
977
|
+
`complete` and `refresh` raise `Mistri::MCP::OAuth::TokenError` when the token
|
|
978
|
+
endpoint refuses. `error.reason` is the code it gave, when that is one of the
|
|
979
|
+
standard ones, and `error.status` its HTTP status.
|
|
980
|
+
`Mistri::MCP::OAuth::InvalidGrantError` is the one a host acts on: the grant
|
|
981
|
+
is spent or revoked, so the person signs in again.
|
|
982
|
+
|
|
983
|
+
```ruby
|
|
984
|
+
begin
|
|
985
|
+
tokens = Mistri::MCP::OAuth.refresh(**stored)
|
|
986
|
+
rescue Mistri::MCP::OAuth::InvalidGrantError
|
|
987
|
+
connection.update!(status: "needs_sign_in")
|
|
988
|
+
end
|
|
989
|
+
```
|
|
990
|
+
|
|
991
|
+
`Mistri::MCP::OAuth.revoke(token:, **stored)` ends a token at the
|
|
992
|
+
authorization server. It returns false for a server that names no revocation
|
|
993
|
+
endpoint.
|
|
994
|
+
|
|
995
|
+
### More scope
|
|
996
|
+
|
|
997
|
+
A server may answer a call with a 403 and a challenge that asks for more
|
|
998
|
+
scope. The `Mistri::AuthenticationError` carries the challenge:
|
|
999
|
+
|
|
1000
|
+
```ruby
|
|
1001
|
+
rescue Mistri::AuthenticationError => e
|
|
1002
|
+
asked = Mistri::MCP::OAuth.challenge(e)
|
|
1003
|
+
if asked["error"] == "insufficient_scope"
|
|
1004
|
+
scope = Mistri::MCP::OAuth.step_up_scope(connection.scope, asked["scope"])
|
|
1005
|
+
flow = Mistri::MCP::OAuth.start(url: server_url, scope: scope, **client)
|
|
1006
|
+
end
|
|
1007
|
+
end
|
|
1008
|
+
```
|
|
1009
|
+
|
|
1010
|
+
`step_up_scope` joins what was granted with what is asked, so the new token
|
|
1011
|
+
keeps what the old one could do. What was granted is `tokens["scope"]`, or
|
|
1012
|
+
`flow["scope"]` when the token response names none. Stop after a few rounds:
|
|
1013
|
+
a server that keeps asking is not going to be satisfied.
|
|
1014
|
+
|
|
1015
|
+
### Reading how a server signs in
|
|
1016
|
+
|
|
1017
|
+
```ruby
|
|
1018
|
+
found = Mistri::MCP::OAuth.discover(url: server_url)
|
|
1019
|
+
found["issuer"] # the authorization server
|
|
1020
|
+
found["scope"] # what a sign-in would ask for
|
|
1021
|
+
found["server_metadata"]["registration_endpoint"] # absent: set a client up by hand
|
|
1022
|
+
```
|
|
1023
|
+
|
|
1024
|
+
`discover` takes the steps `start` takes and stops before it registers
|
|
1025
|
+
anything. It returns the resource, the issuer, the scope, and the two
|
|
1026
|
+
metadata documents as the servers publish them.
|
|
1027
|
+
|
|
1028
|
+
### Tolerant discovery
|
|
1029
|
+
|
|
1030
|
+
Exact discovery refuses some servers that the official SDKs connect to.
|
|
1031
|
+
`tolerant: true`, on `discover`, `start`, `complete`, `refresh` and `revoke`,
|
|
1032
|
+
accepts three things:
|
|
1033
|
+
|
|
1034
|
+
- A resource document for the server's origin, or for a path above the
|
|
1035
|
+
server's. That resource is then the one tokens are asked for.
|
|
1036
|
+
- An issuer that differs from the one named by one trailing slash.
|
|
1037
|
+
- No resource document at all, which is the 2025-03-26 revision: the server's
|
|
1038
|
+
origin is the authorization server, and when it publishes no metadata
|
|
1039
|
+
either, the endpoints are `/authorize`, `/token` and `/register`.
|
|
1040
|
+
|
|
1041
|
+
The third applies only when the documents are not published. A server that
|
|
1042
|
+
could not be reached, or that answered 429 or a 5xx, ends the sign-in with its
|
|
1043
|
+
error. None of the three lets a token be asked for another origin, and a
|
|
1044
|
+
client that was registered with another issuer is refused with
|
|
1045
|
+
`IssuerChanged`, as it is exactly. A host that starts a sign-in tolerantly
|
|
1046
|
+
passes `tolerant: true` to every later step of it.
|
|
1047
|
+
|
|
1048
|
+
Exact discovery is the default. When it refuses a server for the first or the
|
|
1049
|
+
second reason, the error names what tolerant discovery accepts:
|
|
1050
|
+
|
|
1051
|
+
```text
|
|
1052
|
+
protected resource metadata resource does not match https://mcp.example.com/mcp; it names https://mcp.example.com, a resource that tolerant: true accepts
|
|
1053
|
+
authorization server metadata issuer does not match https://auth.example.com/; it differs by one trailing slash, a difference that tolerant: true accepts
|
|
1054
|
+
```
|
|
1055
|
+
|
|
1056
|
+
That is one step of the sign-in. A later step can still refuse the server, for
|
|
1057
|
+
example when it names more than one authorization server.
|
|
1058
|
+
|
|
1059
|
+
A resource document for an origin makes a token good for every server on that
|
|
1060
|
+
origin. Keep discovery exact for an origin that holds servers of different
|
|
1061
|
+
owners.
|
|
1062
|
+
|
|
330
1063
|
## Optional Rails connection generator
|
|
331
1064
|
|
|
332
1065
|
Rails applications can generate an encrypted connection model and migration:
|
|
@@ -340,6 +1073,14 @@ and scope. The generated model exposes `connect`, `complete`, token refresh, a
|
|
|
340
1073
|
Client, and bridged tools. Set up Active Record encryption before storing
|
|
341
1074
|
credentials.
|
|
342
1075
|
|
|
1076
|
+
The callback passes its `iss` on: `McpConnection.complete(state: params[:state],
|
|
1077
|
+
code: params[:code], iss: params[:iss])`.
|
|
1078
|
+
|
|
1079
|
+
A server retires a refresh token when it is used, so two processes that
|
|
1080
|
+
refresh one row at the same moment would lose the sign-in. The generated
|
|
1081
|
+
`bearer_token` locks the row for the refresh and looks again once it holds the
|
|
1082
|
+
lock. The lock is held while the token endpoint answers.
|
|
1083
|
+
|
|
343
1084
|
Override the generated class policy when the server is internal:
|
|
344
1085
|
|
|
345
1086
|
```ruby
|