@yahyahammad/venastine 0.3.8 → 0.3.10
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.
- package/.env.example +19 -19
- package/CONFIG_ARCHITECTURE.md +2139 -2002
- package/LICENSE +201 -201
- package/README.md +1043 -1034
- package/agents/builtin/compactor.md +75 -75
- package/agents/builtin/explore.md +77 -77
- package/agents/builtin/pipeline-reviewer.md +100 -100
- package/agents/builtin/plan.md +106 -106
- package/agents/builtin/review.md +74 -74
- package/agents/manager.py +261 -261
- package/agents/subagent_tool.py +472 -472
- package/agents/tui_commands.py +238 -146
- package/bin/venastine.mjs +584 -528
- package/bin/venastine.py +73 -71
- package/cli_commands.py +848 -835
- package/config.yaml +64 -0
- package/config_edit.py +1301 -1301
- package/config_schema.py +987 -966
- package/config_update.py +749 -749
- package/core/agent_activity.py +256 -256
- package/core/ansi.py +452 -452
- package/core/approval.py +137 -137
- package/core/call_view.py +364 -364
- package/core/cancel.py +121 -121
- package/core/chatgpt_auth.py +747 -747
- package/core/claims_view.py +107 -107
- package/core/client.py +2756 -2715
- package/core/commands.py +464 -454
- package/core/compaction.py +1735 -1735
- package/core/config_loader.py +1982 -1982
- package/core/connect.py +686 -686
- package/core/events.py +123 -123
- package/core/file_mentions.py +280 -280
- package/core/interaction.py +411 -411
- package/core/loop.py +2566 -2566
- package/core/memory.py +482 -482
- package/core/pipeline_models.py +190 -190
- package/core/provider_errors.py +274 -234
- package/core/reasoning/authorization.py +130 -130
- package/core/reasoning/base.py +219 -219
- package/core/reasoning/confidence_scoring.py +484 -484
- package/core/reasoning/events.py +149 -149
- package/core/reasoning/orchestrator.py +1561 -1561
- package/core/reasoning/output_writer.py +345 -345
- package/core/reasoning/payload_validation.py +524 -524
- package/core/reasoning/pipeline_storage.py +427 -427
- package/core/reasoning/scholar.py +467 -458
- package/core/reasoning/source_corpus.py +433 -433
- package/core/reasoning/source_scoring.py +855 -855
- package/core/replay.py +315 -315
- package/core/shell_sessions.py +1553 -1553
- package/credentials.py +307 -307
- package/database.py +333 -333
- package/docs/PRIVACY.md +78 -78
- package/docs/SECURITY.md +114 -108
- package/docs/THIRD_PARTY_NOTICES.md +148 -148
- package/env_secrets.py +118 -118
- package/json_store.py +343 -343
- package/logging_setup.py +295 -295
- package/main.py +3433 -3433
- package/mcp_client/client.py +780 -733
- package/mcp_client/config.py +484 -484
- package/mcp_client/registration.py +343 -343
- package/memories/manager.py +147 -147
- package/memories/tui_commands.py +319 -239
- package/package.json +303 -303
- package/project_init/config_files.py +282 -282
- package/project_init/generator.py +728 -728
- package/project_init/manifest.py +418 -418
- package/project_init/tui_commands.py +147 -147
- package/prompts/critic_pass.md +24 -24
- package/prompts/revalidate.md +25 -25
- package/prompts/revise.md +17 -17
- package/prompts/source_grounding.md +66 -66
- package/prompts/system_prompts.py +316 -316
- package/providers.json.example +94 -94
- package/pyproject.toml +149 -149
- package/requirements.txt +314 -295
- package/safety/policy_enforcement.py +1082 -1082
- package/security/askpass.py +419 -419
- package/security/capability.py +207 -207
- package/security/confine.py +484 -484
- package/security/hosted.py +182 -182
- package/security/permissions.py +237 -237
- package/security/protected_paths.py +501 -501
- package/security/sandbox.py +4108 -4108
- package/skills/builtin/math/formal-specification.md +67 -67
- package/skills/builtin/math/numerical-methods.md +66 -66
- package/skills/builtin/research/experiment-design.md +62 -62
- package/skills/builtin/research/reproducibility.md +55 -55
- package/skills/builtin/research/source-evaluation.md +70 -70
- package/skills/builtin/research/statistical-inference.md +63 -63
- package/skills/builtin/research/technical-writing.md +59 -59
- package/skills/builtin/software/code-review.md +61 -61
- package/skills/builtin/software/debugging.md +61 -61
- package/skills/builtin/software/test-design.md +67 -67
- package/skills/tui_commands.py +110 -110
- package/storage.py +1338 -1338
- package/tools/base.py +465 -465
- package/tools/builtin/_math_common.py +667 -667
- package/tools/builtin/_net_common.py +1145 -267
- package/tools/builtin/_page_text.py +490 -0
- package/tools/builtin/_pdf_text.py +113 -0
- package/tools/builtin/arxiv.py +685 -681
- package/tools/builtin/fetch_url.py +639 -371
- package/tools/builtin/file_ops.py +720 -720
- package/tools/builtin/geometry.py +184 -184
- package/tools/builtin/load_skill.py +76 -76
- package/tools/builtin/pin.py +227 -227
- package/tools/builtin/probability_stats.py +224 -224
- package/tools/builtin/project_docs.py +464 -464
- package/tools/builtin/remember.py +175 -175
- package/tools/builtin/shell.py +980 -980
- package/tools/builtin/todo.py +149 -149
- package/tools/builtin/web_search.py +402 -328
- package/tools/context.py +159 -159
- package/tools/isolation.py +387 -387
- package/tools/registry.py +1252 -1242
- package/tui/app.py +8372 -8307
- package/tui/app.tcss +754 -739
- package/tui/commands.py +26 -26
- package/tui/diffs.py +286 -286
- package/tui/history.py +128 -128
- package/tui/hosted_driver.py +102 -102
- package/tui/markdown.py +1329 -1329
- package/tui/meters.py +364 -364
- package/tui/ravens.py +146 -146
- package/tui/screens.py +1255 -1197
- package/tui/themes.py +599 -599
- package/tui/widgets.py +3861 -3796
package/mcp_client/client.py
CHANGED
|
@@ -1,733 +1,780 @@
|
|
|
1
|
-
"""
|
|
2
|
-
mcp_client/client.py
|
|
3
|
-
|
|
4
|
-
The async bridge and the MCP client. ROADMAP_v2 §17, D2a/D23.
|
|
5
|
-
|
|
6
|
-
The MCP SDK is async end to end with no synchronous surface. Making tool
|
|
7
|
-
dispatch async to accommodate it would touch every file in this project
|
|
8
|
-
and invalidate a wholly synchronous test suite, for a concurrency benefit
|
|
9
|
-
nothing here needs. So ONE dedicated background thread owns ONE event
|
|
10
|
-
loop for all MCP traffic, and every call crosses into it via
|
|
11
|
-
run_coroutine_threadsafe(...).result(timeout=...). `_run()`,
|
|
12
|
-
`registry.dispatch()` and every tool handler stay plain sync functions.
|
|
13
|
-
|
|
14
|
-
--------------------------------------------------------------------
|
|
15
|
-
THE MANAGER TASK -- read this before touching connect/disconnect
|
|
16
|
-
--------------------------------------------------------------------
|
|
17
|
-
§17's spec has connect_all() do `stack.enter_async_context(...)` and
|
|
18
|
-
disconnect_all() call `stack.aclose()`. That does not work, and the
|
|
19
|
-
failure is not theoretical -- it was reproduced against mcp 2.0.0 before
|
|
20
|
-
this file was written:
|
|
21
|
-
|
|
22
|
-
RuntimeError: Attempted to exit cancel scope in a different task
|
|
23
|
-
than it was entered in
|
|
24
|
-
|
|
25
|
-
Rev. 3 reasoned that the exit stack must live on the MCP *loop*, because
|
|
26
|
-
entering on one loop and exiting on another is undefined. True, but not
|
|
27
|
-
sufficient. anyio binds cancel scopes to the TASK, and
|
|
28
|
-
run_coroutine_threadsafe() creates a NEW TASK for every call. Connecting
|
|
29
|
-
in one task and closing from another is illegal even on the same loop.
|
|
30
|
-
|
|
31
|
-
So a single long-lived _manager() task enters every context, signals
|
|
32
|
-
ready, then parks on an event until shutdown -- the stack unwinds in the
|
|
33
|
-
same task that entered it. Per-call tasks are fine: calling a method on a
|
|
34
|
-
connected client doesn't exit anyone's cancel scope.
|
|
35
|
-
|
|
36
|
-
If you ever see that RuntimeError again, something moved an
|
|
37
|
-
enter/exit across the task boundary. The fix is never "use a different
|
|
38
|
-
loop"; it is "do it inside _manager".
|
|
39
|
-
"""
|
|
40
|
-
|
|
41
|
-
import asyncio
|
|
42
|
-
import contextlib
|
|
43
|
-
import json
|
|
44
|
-
import logging
|
|
45
|
-
import threading
|
|
46
|
-
import time
|
|
47
|
-
from typing import Optional
|
|
48
|
-
|
|
49
|
-
from mcp import Client
|
|
50
|
-
from mcp.client.sse import sse_client
|
|
51
|
-
from mcp.client.stdio import StdioServerParameters, stdio_client
|
|
52
|
-
from mcp.client.streamable_http import (
|
|
53
|
-
create_mcp_http_client,
|
|
54
|
-
streamable_http_client,
|
|
55
|
-
)
|
|
56
|
-
from mcp.shared.exceptions import MCPError
|
|
57
|
-
|
|
58
|
-
import config as _harness_config
|
|
59
|
-
from safety.policy_enforcement import TOKEN_CHARS, fragment_end
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
#
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
#
|
|
71
|
-
#
|
|
72
|
-
#
|
|
73
|
-
#
|
|
74
|
-
#
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
#
|
|
79
|
-
#
|
|
80
|
-
#
|
|
81
|
-
#
|
|
82
|
-
#
|
|
83
|
-
#
|
|
84
|
-
#
|
|
85
|
-
#
|
|
86
|
-
#
|
|
87
|
-
#
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
#
|
|
92
|
-
#
|
|
93
|
-
#
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
#
|
|
98
|
-
#
|
|
99
|
-
#
|
|
100
|
-
#
|
|
101
|
-
#
|
|
102
|
-
#
|
|
103
|
-
#
|
|
104
|
-
#
|
|
105
|
-
#
|
|
106
|
-
#
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
that
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
self.
|
|
124
|
-
self.
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
if
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
def
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
returns
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
redaction
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
#
|
|
321
|
-
#
|
|
322
|
-
#
|
|
323
|
-
#
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
note =
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
if
|
|
358
|
-
|
|
359
|
-
if
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
#
|
|
437
|
-
#
|
|
438
|
-
#
|
|
439
|
-
#
|
|
440
|
-
#
|
|
441
|
-
#
|
|
442
|
-
#
|
|
443
|
-
#
|
|
444
|
-
#
|
|
445
|
-
#
|
|
446
|
-
#
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
#
|
|
577
|
-
#
|
|
578
|
-
#
|
|
579
|
-
#
|
|
580
|
-
#
|
|
581
|
-
#
|
|
582
|
-
#
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
(
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
1
|
+
"""
|
|
2
|
+
mcp_client/client.py
|
|
3
|
+
|
|
4
|
+
The async bridge and the MCP client. ROADMAP_v2 §17, D2a/D23.
|
|
5
|
+
|
|
6
|
+
The MCP SDK is async end to end with no synchronous surface. Making tool
|
|
7
|
+
dispatch async to accommodate it would touch every file in this project
|
|
8
|
+
and invalidate a wholly synchronous test suite, for a concurrency benefit
|
|
9
|
+
nothing here needs. So ONE dedicated background thread owns ONE event
|
|
10
|
+
loop for all MCP traffic, and every call crosses into it via
|
|
11
|
+
run_coroutine_threadsafe(...).result(timeout=...). `_run()`,
|
|
12
|
+
`registry.dispatch()` and every tool handler stay plain sync functions.
|
|
13
|
+
|
|
14
|
+
--------------------------------------------------------------------
|
|
15
|
+
THE MANAGER TASK -- read this before touching connect/disconnect
|
|
16
|
+
--------------------------------------------------------------------
|
|
17
|
+
§17's spec has connect_all() do `stack.enter_async_context(...)` and
|
|
18
|
+
disconnect_all() call `stack.aclose()`. That does not work, and the
|
|
19
|
+
failure is not theoretical -- it was reproduced against mcp 2.0.0 before
|
|
20
|
+
this file was written:
|
|
21
|
+
|
|
22
|
+
RuntimeError: Attempted to exit cancel scope in a different task
|
|
23
|
+
than it was entered in
|
|
24
|
+
|
|
25
|
+
Rev. 3 reasoned that the exit stack must live on the MCP *loop*, because
|
|
26
|
+
entering on one loop and exiting on another is undefined. True, but not
|
|
27
|
+
sufficient. anyio binds cancel scopes to the TASK, and
|
|
28
|
+
run_coroutine_threadsafe() creates a NEW TASK for every call. Connecting
|
|
29
|
+
in one task and closing from another is illegal even on the same loop.
|
|
30
|
+
|
|
31
|
+
So a single long-lived _manager() task enters every context, signals
|
|
32
|
+
ready, then parks on an event until shutdown -- the stack unwinds in the
|
|
33
|
+
same task that entered it. Per-call tasks are fine: calling a method on a
|
|
34
|
+
connected client doesn't exit anyone's cancel scope.
|
|
35
|
+
|
|
36
|
+
If you ever see that RuntimeError again, something moved an
|
|
37
|
+
enter/exit across the task boundary. The fix is never "use a different
|
|
38
|
+
loop"; it is "do it inside _manager".
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
import asyncio
|
|
42
|
+
import contextlib
|
|
43
|
+
import json
|
|
44
|
+
import logging
|
|
45
|
+
import threading
|
|
46
|
+
import time
|
|
47
|
+
from typing import Optional
|
|
48
|
+
|
|
49
|
+
from mcp import Client
|
|
50
|
+
from mcp.client.sse import sse_client
|
|
51
|
+
from mcp.client.stdio import StdioServerParameters, stdio_client
|
|
52
|
+
from mcp.client.streamable_http import (
|
|
53
|
+
create_mcp_http_client,
|
|
54
|
+
streamable_http_client,
|
|
55
|
+
)
|
|
56
|
+
from mcp.shared.exceptions import MCPError
|
|
57
|
+
|
|
58
|
+
import config as _harness_config
|
|
59
|
+
from safety.policy_enforcement import TOKEN_CHARS, fragment_end
|
|
60
|
+
from tools.builtin import _net_common
|
|
61
|
+
|
|
62
|
+
logger = logging.getLogger(__name__)
|
|
63
|
+
|
|
64
|
+
# Rev. 1 S12: MCP servers are third-party code and can hang. A generous default
|
|
65
|
+
# beats an indefinite block; individual calls may override.
|
|
66
|
+
DEFAULT_CALL_TIMEOUT_S = 180.0
|
|
67
|
+
CONNECT_TIMEOUT_S = 60.0
|
|
68
|
+
SHUTDOWN_TIMEOUT_S = 15.0
|
|
69
|
+
|
|
70
|
+
# F6 (#64): the WHOLE teardown -- polite close, force-cancel, loop-thread
|
|
71
|
+
# join -- shares this one wall-clock budget, instead of three sequential
|
|
72
|
+
# SHUTDOWN_TIMEOUT_S waits whose worst case hung a quitting harness ~45s.
|
|
73
|
+
# Master value lives in config.py beside the other harness bounds; the
|
|
74
|
+
# re-export here keeps it beside its sibling timeouts and gives tests a
|
|
75
|
+
# single name to shrink.
|
|
76
|
+
TEARDOWN_BUDGET_S = _harness_config.TEARDOWN_BUDGET_S
|
|
77
|
+
|
|
78
|
+
# How much of a structured error payload rides in the error STRING.
|
|
79
|
+
# D23 promises call_tool() returns one error shape, so a server's
|
|
80
|
+
# structured_content on a failure belongs inside that string rather than
|
|
81
|
+
# in a second key every consumer of a dispatch error would have to learn
|
|
82
|
+
# (A32). Bounded because the payload is third-party and the string reaches
|
|
83
|
+
# a model: 500 is room for the {code, retry_after, message} an error
|
|
84
|
+
# payload actually carries, and the cut is visible for the reason
|
|
85
|
+
# _catalog_text's is.
|
|
86
|
+
#
|
|
87
|
+
# The result AS A WHOLE has its own bound, `max_read_chars` (A79); see
|
|
88
|
+
# _normalize.
|
|
89
|
+
MAX_ERROR_DETAIL_CHARS = 500
|
|
90
|
+
|
|
91
|
+
# Per-server, inside the manager. CONNECT_TIMEOUT_S is the caller-side
|
|
92
|
+
# budget for ALL servers together; without a per-server bound one hung
|
|
93
|
+
# handshake spends it and every server after it in the loop is starved of
|
|
94
|
+
# time it never got to use.
|
|
95
|
+
SERVER_CONNECT_TIMEOUT_S = 20.0
|
|
96
|
+
|
|
97
|
+
# How far below the caller-side timeout the protocol-level timeout sits.
|
|
98
|
+
# Both are set, and they do different jobs: read_timeout_seconds cancels
|
|
99
|
+
# the REQUEST at the protocol level, while future.result(timeout=) only
|
|
100
|
+
# unblocks the calling thread and abandons the call locally. The margin
|
|
101
|
+
# makes the protocol one win the race, so a hung server is told to stop
|
|
102
|
+
# rather than merely stopped being waited on, and the caller gets the
|
|
103
|
+
# SDK's specific MCPError instead of a bare TimeoutError.
|
|
104
|
+
#
|
|
105
|
+
# The future timeout is still needed as a backstop for the case the
|
|
106
|
+
# protocol one can't cover: a wedged loop, where no MCP-level response
|
|
107
|
+
# (including the timeout) is coming at all.
|
|
108
|
+
_TIMEOUT_MARGIN_S = 5.0
|
|
109
|
+
|
|
110
|
+
class _TrackClose:
|
|
111
|
+
"""Records that one server's session closed cleanly, so a teardown
|
|
112
|
+
that runs out of budget can NAME which server ignored the shutdown
|
|
113
|
+
request instead of reporting an anonymous hang (F6).
|
|
114
|
+
|
|
115
|
+
Recorded only on an unexceptional exit: a session whose __aexit__
|
|
116
|
+
raised, or was interrupted by the force-cancel, did not demonstrably
|
|
117
|
+
close -- and "we stopped waiting for it" is exactly what the
|
|
118
|
+
straggler line means. Attribute access forwards to the real client,
|
|
119
|
+
so list_tools()/call_tool() never notice the wrapper.
|
|
120
|
+
"""
|
|
121
|
+
|
|
122
|
+
def __init__(self, inner, name: str, closed: set):
|
|
123
|
+
self._inner = inner
|
|
124
|
+
self._name = name
|
|
125
|
+
self._closed = closed
|
|
126
|
+
|
|
127
|
+
async def __aenter__(self):
|
|
128
|
+
return await self._inner.__aenter__()
|
|
129
|
+
|
|
130
|
+
async def __aexit__(self, *exc):
|
|
131
|
+
try:
|
|
132
|
+
result = await self._inner.__aexit__(*exc)
|
|
133
|
+
except BaseException:
|
|
134
|
+
raise
|
|
135
|
+
self._closed.add(self._name)
|
|
136
|
+
return result
|
|
137
|
+
|
|
138
|
+
def __getattr__(self, item):
|
|
139
|
+
return getattr(self._inner, item)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
_loop: Optional[asyncio.AbstractEventLoop] = None
|
|
143
|
+
_thread: Optional[threading.Thread] = None
|
|
144
|
+
_loop_lock = threading.Lock()
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _metered_mcp_client(headers=None, timeout=None, auth=None):
|
|
148
|
+
"""The SDK's own MCP client, with the MCP meter's hooks on it (§53).
|
|
149
|
+
|
|
150
|
+
`sse_client`'s `httpx_client_factory` signature, so the one function
|
|
151
|
+
serves both HTTP transports. The SDK builds the client -- its timeouts,
|
|
152
|
+
its redirect rule -- and `event_hooks` is public on an httpx2 client,
|
|
153
|
+
so nothing here re-states what the SDK already decides.
|
|
154
|
+
"""
|
|
155
|
+
client = create_mcp_http_client(headers=headers, timeout=timeout,
|
|
156
|
+
auth=auth)
|
|
157
|
+
client.event_hooks = _net_common.mcp_event_hooks()
|
|
158
|
+
return client
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _ensure_loop() -> asyncio.AbstractEventLoop:
|
|
162
|
+
"""The one MCP event loop, created on first use."""
|
|
163
|
+
global _loop, _thread
|
|
164
|
+
with _loop_lock:
|
|
165
|
+
if _loop is None:
|
|
166
|
+
_loop = asyncio.new_event_loop()
|
|
167
|
+
_thread = threading.Thread(
|
|
168
|
+
target=_loop.run_forever, name="mcp-loop", daemon=True)
|
|
169
|
+
_thread.start()
|
|
170
|
+
logger.debug("Started MCP event loop thread.")
|
|
171
|
+
return _loop
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _shutdown_loop(join_timeout: float = SHUTDOWN_TIMEOUT_S) -> None:
|
|
175
|
+
global _loop, _thread
|
|
176
|
+
with _loop_lock:
|
|
177
|
+
if _loop is None:
|
|
178
|
+
return
|
|
179
|
+
_loop.call_soon_threadsafe(_loop.stop)
|
|
180
|
+
if _thread is not None:
|
|
181
|
+
_thread.join(timeout=join_timeout)
|
|
182
|
+
if _thread.is_alive():
|
|
183
|
+
logger.warning("MCP loop thread did not stop within %.1fs.",
|
|
184
|
+
join_timeout)
|
|
185
|
+
_loop = None
|
|
186
|
+
_thread = None
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def _detail(structured) -> str:
|
|
190
|
+
"""A structured payload as one bounded line, for the error string.
|
|
191
|
+
|
|
192
|
+
default=str so a payload holding something JSON cannot carry is still
|
|
193
|
+
reported rather than raising inside the error path -- D23's promise
|
|
194
|
+
that this module returns one error shape has to mean always, which is
|
|
195
|
+
the same argument the broad except in call_tool() makes.
|
|
196
|
+
"""
|
|
197
|
+
try:
|
|
198
|
+
rendered = json.dumps(structured, default=str, sort_keys=True)
|
|
199
|
+
except Exception:
|
|
200
|
+
rendered = repr(structured)
|
|
201
|
+
rendered = " ".join(rendered.split())
|
|
202
|
+
if len(rendered) <= MAX_ERROR_DETAIL_CHARS:
|
|
203
|
+
return rendered
|
|
204
|
+
return rendered[:MAX_ERROR_DETAIL_CHARS - 1].rstrip() + "\u2026"
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def _cut(text: str, limit: int) -> str:
|
|
208
|
+
"""The first *limit* characters of *text*, less a word the cut split.
|
|
209
|
+
|
|
210
|
+
The redactor runs after this, on the dispatch path, and cannot match
|
|
211
|
+
half a credential. So when the character past the cut is a token
|
|
212
|
+
character, the token-character run the cut ends inside goes too: the
|
|
213
|
+
rule A72 made for a shell session's dropped middle. A cut that fell
|
|
214
|
+
between two words drops nothing more.
|
|
215
|
+
"""
|
|
216
|
+
if len(text) <= limit:
|
|
217
|
+
return text
|
|
218
|
+
kept = text[:limit]
|
|
219
|
+
if text[limit] in TOKEN_CHARS:
|
|
220
|
+
kept = kept[:fragment_end(kept)]
|
|
221
|
+
return kept
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def _json_chars(structured) -> Optional[int]:
|
|
225
|
+
"""How many characters *structured* takes as JSON, or None when it
|
|
226
|
+
cannot be rendered -- which _normalize reads as too large to keep,
|
|
227
|
+
since nothing can say how large it is."""
|
|
228
|
+
try:
|
|
229
|
+
return len(json.dumps(structured, default=str, ensure_ascii=False))
|
|
230
|
+
except Exception:
|
|
231
|
+
return None
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def _repeats(structured, text: str) -> bool:
|
|
235
|
+
"""Whether *structured* is the server's text over again.
|
|
236
|
+
|
|
237
|
+
The SDK's own server sends a tool that returns a string as that text
|
|
238
|
+
AND as `{"result": <the text>}`, and a typed return as the JSON in
|
|
239
|
+
both places. A dropped copy of the text is no loss, and the note says
|
|
240
|
+
so, so the model does not call again for something it already has.
|
|
241
|
+
"""
|
|
242
|
+
if structured == {"result": text}:
|
|
243
|
+
return True
|
|
244
|
+
try:
|
|
245
|
+
return json.loads(text) == structured
|
|
246
|
+
except Exception:
|
|
247
|
+
return False
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def _normalize(result, tool_name: str = "") -> dict:
|
|
251
|
+
"""CallToolResult -> the plain dict shape every other tool handler
|
|
252
|
+
returns (D23).
|
|
253
|
+
|
|
254
|
+
Fixing this at the producer rather than the consumer is the whole
|
|
255
|
+
point. Downstream, check_output_policy() calls result.get(key) and
|
|
256
|
+
would raise AttributeError on a pydantic model -- meaning MCP output,
|
|
257
|
+
the output most likely to come from code the user didn't write, is
|
|
258
|
+
exactly the output that would bypass secret redaction. And
|
|
259
|
+
memory.add_tool_result() would str() a pydantic repr into the thread.
|
|
260
|
+
|
|
261
|
+
MCP signals tool failure IN BAND via is_error rather than by raising,
|
|
262
|
+
so it maps onto the same {"error": ...} convention _run() already
|
|
263
|
+
understands for ToolCallDenied and disallowed tools. No new error
|
|
264
|
+
channel, no special-casing in the loop.
|
|
265
|
+
|
|
266
|
+
Keys are 'content' and 'result' deliberately, though NOT for the
|
|
267
|
+
reason this comment used to give. It said both were in
|
|
268
|
+
safety/policy_enforcement.py's `_SCANNED_KEYS`, so MCP output was
|
|
269
|
+
secret-redacted by the existing layer with no change to it. That set
|
|
270
|
+
no longer exists (#47): check_output_policy scans every value, so any
|
|
271
|
+
key would now be covered. Choosing these two is about matching the
|
|
272
|
+
shape every other handler returns (D23), not about clearing a
|
|
273
|
+
redaction bar.
|
|
274
|
+
|
|
275
|
+
Worth keeping as a caution rather than deleting: this docstring is
|
|
276
|
+
the evidence that authors DID read the allowlist as a contract and
|
|
277
|
+
write to it. Two older built-ins returned `results` and never
|
|
278
|
+
satisfied it, which is the defect #47 records.
|
|
279
|
+
|
|
280
|
+
v2 renamed these fields to snake_case (.content/.structured_content/
|
|
281
|
+
.is_error); §17's sketch reads the v1 camelCase spellings and would
|
|
282
|
+
silently see is_error as always-False against v2.
|
|
283
|
+
|
|
284
|
+
THE RESULT IS BOUNDED HERE, AS A WHOLE, AT `max_read_chars` (A79).
|
|
285
|
+
Every other producer of third-party text bounds what it hands on, and
|
|
286
|
+
this one handed on anything: 10 MB of text, of structured content or
|
|
287
|
+
of an error message went through whole, into the thread and the
|
|
288
|
+
model's context, behind a redaction pass that took 1.2-2.5 s over it.
|
|
289
|
+
The server's text is cut at the bound with a note giving the size it
|
|
290
|
+
had. A structured payload is kept whole if it fits in what the text
|
|
291
|
+
left of the bound, and otherwise dropped with a note -- never cut,
|
|
292
|
+
since a JSON document cut short can read as complete. A server that
|
|
293
|
+
follows the MCP spec sends structured output as text as well, so the
|
|
294
|
+
text usually carries what was dropped, and the note says when it was
|
|
295
|
+
a repeat (see `_repeats`). The bound is `max_read_chars`
|
|
296
|
+
because it is already how much one read is, and the compaction
|
|
297
|
+
headroom check measures a read against it.
|
|
298
|
+
"""
|
|
299
|
+
bound = int(_harness_config.MAX_READ_CHARS)
|
|
300
|
+
blocks = getattr(result, "content", None) or []
|
|
301
|
+
kept, dropped = [], []
|
|
302
|
+
for block in blocks:
|
|
303
|
+
kind = getattr(block, "type", None) or "unknown"
|
|
304
|
+
if kind == "text":
|
|
305
|
+
value = getattr(block, "text", None)
|
|
306
|
+
if value:
|
|
307
|
+
kept.append(value)
|
|
308
|
+
# AN EMPTY TEXT BLOCK HAS NOTHING TO DROP. It is filtered out
|
|
309
|
+
# of the join either way, but counting it as dropped content
|
|
310
|
+
# made the placeholder contradict itself -- `text("")` came
|
|
311
|
+
# back as "[non-text MCP content: text]", reporting the kind
|
|
312
|
+
# as non-text when the kind is text.
|
|
313
|
+
continue
|
|
314
|
+
dropped.append(kind)
|
|
315
|
+
|
|
316
|
+
full = "\n".join(kept)
|
|
317
|
+
text = _cut(full, bound)
|
|
318
|
+
sent, shown = len(full), len(text)
|
|
319
|
+
if dropped:
|
|
320
|
+
# APPENDED, NOT SUBSTITUTED (A32). Non-text blocks (images,
|
|
321
|
+
# embedded resources) are still dropped -- the harness has no
|
|
322
|
+
# multimodal tool-result path, and inventing one here is scope the
|
|
323
|
+
# rest of the design can't carry. What changed is WHEN the
|
|
324
|
+
# disclosure happens: the placeholder used to fire only when NO
|
|
325
|
+
# text survived, so a result carrying text AND non-text dropped
|
|
326
|
+
# the non-text silently, and "Chart generated:" with no chart and
|
|
327
|
+
# no note is a result the model will answer from. CallToolResult.
|
|
328
|
+
# content is a REQUIRED list of mixed block types on the installed
|
|
329
|
+
# SDK, so that shape is first-class in the protocol rather than
|
|
330
|
+
# hypothetical.
|
|
331
|
+
kinds = ", ".join(sorted(set(dropped)))
|
|
332
|
+
note = f"[non-text MCP content: {kinds}]"
|
|
333
|
+
text = f"{text}\n{note}" if text else note
|
|
334
|
+
# ONE WARNING NAMING THE TOOL AND WHAT CHANGED, which is the rule
|
|
335
|
+
# #49 was filed to establish and which this function broke on all
|
|
336
|
+
# three of its dropping paths. The tool name is a PARAMETER
|
|
337
|
+
# because this function could not name the tool -- it took the
|
|
338
|
+
# result alone, so the rule was unsatisfiable here as written.
|
|
339
|
+
logger.warning(
|
|
340
|
+
"MCP tool %s returned %d non-text block(s) (%s); this harness "
|
|
341
|
+
"has no multimodal tool-result path, so they were dropped and "
|
|
342
|
+
"the result says so in band.",
|
|
343
|
+
tool_name or "<unknown>", len(dropped), kinds)
|
|
344
|
+
|
|
345
|
+
if shown < sent:
|
|
346
|
+
# After the non-text note, so neither note is what the cut takes.
|
|
347
|
+
note = (f"[MCP result cut: the server sent {sent:,} characters of "
|
|
348
|
+
f"text, and the {shown:,} shown end at the last word break "
|
|
349
|
+
f"inside the bound, max_read_chars ({bound:,})]")
|
|
350
|
+
text = f"{text}\n{note}" if text else note
|
|
351
|
+
logger.warning(
|
|
352
|
+
"MCP tool %s returned %d characters of text; it was cut to %d "
|
|
353
|
+
"(max_read_chars is %d) and the result says so in band.",
|
|
354
|
+
tool_name or "<unknown>", sent, shown, bound)
|
|
355
|
+
|
|
356
|
+
structured = getattr(result, "structured_content", None)
|
|
357
|
+
if getattr(result, "is_error", False):
|
|
358
|
+
message = text or "MCP tool call failed"
|
|
359
|
+
if structured is not None:
|
|
360
|
+
# D23's ONE error shape, not a second key (A32): a server
|
|
361
|
+
# reporting {code: 429, retry_after: 30} used to lose it
|
|
362
|
+
# entirely, so the one thing that would have explained the
|
|
363
|
+
# failure was the one thing dropped.
|
|
364
|
+
detail = _detail(structured)
|
|
365
|
+
message = f"{message} (server detail: {detail})"
|
|
366
|
+
logger.warning(
|
|
367
|
+
"MCP tool %s failed with a structured payload; it is "
|
|
368
|
+
"folded into the error string: %s",
|
|
369
|
+
tool_name or "<unknown>", detail)
|
|
370
|
+
return {"error": message}
|
|
371
|
+
|
|
372
|
+
if structured is not None:
|
|
373
|
+
size, room = _json_chars(structured), bound - shown
|
|
374
|
+
if size is None or size > room:
|
|
375
|
+
if size is None:
|
|
376
|
+
why = "it could not be rendered as JSON to be measured"
|
|
377
|
+
else:
|
|
378
|
+
why = (f"it is {size:,} characters as JSON, and the text "
|
|
379
|
+
f"left {room:,} of the bound, max_read_chars "
|
|
380
|
+
f"({bound:,})")
|
|
381
|
+
if _repeats(structured, full):
|
|
382
|
+
why += "; it repeats the server's text"
|
|
383
|
+
note = f"[MCP structured result dropped: {why}]"
|
|
384
|
+
text = f"{text}\n{note}" if text else note
|
|
385
|
+
logger.warning(
|
|
386
|
+
"MCP tool %s returned structured content of %s characters "
|
|
387
|
+
"with %d left of max_read_chars; it was dropped and the "
|
|
388
|
+
"result says so in band.",
|
|
389
|
+
tool_name or "<unknown>",
|
|
390
|
+
"unmeasurable" if size is None else size, room)
|
|
391
|
+
structured = None
|
|
392
|
+
|
|
393
|
+
out = {"content": text}
|
|
394
|
+
if structured is not None:
|
|
395
|
+
out["result"] = structured
|
|
396
|
+
return out
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
class MCPClient:
|
|
400
|
+
"""Connects to configured MCP servers and exposes a synchronous
|
|
401
|
+
call_tool() that looks completely ordinary to registry.dispatch()."""
|
|
402
|
+
|
|
403
|
+
def __init__(self, server_configs: dict):
|
|
404
|
+
# Set, and inspectable (§17 AC1). Rev. 1 omitted this assignment
|
|
405
|
+
# entirely -- a real bug (C10) that made every later method fail
|
|
406
|
+
# on a missing attribute.
|
|
407
|
+
self.server_configs = server_configs
|
|
408
|
+
self.clients: dict = {}
|
|
409
|
+
self.failures: dict = {} # server name -> why it isn't usable
|
|
410
|
+
self._closed: set = set() # servers whose session closed cleanly
|
|
411
|
+
# F8 (#63): the catalogue gathered during connect, inside that
|
|
412
|
+
# call's per-server timeout. Registration serves from here --
|
|
413
|
+
# which removes MCP's one bridge call with NO protocol timeout,
|
|
414
|
+
# and the window where a slow post-connect listing burned its
|
|
415
|
+
# own 30s. A miss falls back to fetching.
|
|
416
|
+
self._catalogs: dict = {}
|
|
417
|
+
self._ready: Optional[asyncio.Event] = None
|
|
418
|
+
self._shutdown: Optional[asyncio.Event] = None
|
|
419
|
+
self._done: Optional[asyncio.Event] = None
|
|
420
|
+
self._task = None
|
|
421
|
+
|
|
422
|
+
# -- connection ------------------------------------------------------
|
|
423
|
+
|
|
424
|
+
def _transport_for(self, cfg):
|
|
425
|
+
"""SDK object for one server. v2 collapsed v1's stdio-vs-HTTP
|
|
426
|
+
branch: Client() takes a Transport, a URL string, or an in-process
|
|
427
|
+
server object, so this returns whichever and Client sorts it out."""
|
|
428
|
+
if cfg.transport == "stdio":
|
|
429
|
+
return stdio_client(StdioServerParameters(
|
|
430
|
+
command=cfg.command,
|
|
431
|
+
args=[str(a) for a in cfg.args],
|
|
432
|
+
env=cfg.env or None,
|
|
433
|
+
cwd=cfg.cwd,
|
|
434
|
+
))
|
|
435
|
+
if cfg.transport == "sse":
|
|
436
|
+
# SSE must ALWAYS build its own transport: a bare URL string
|
|
437
|
+
# means STREAMABLE HTTP to Client(), which is exactly the
|
|
438
|
+
# silent misconnection #62 records. Headers go straight to
|
|
439
|
+
# the SDK (its signature takes `headers=`), through the same
|
|
440
|
+
# create_mcp_http_client defaults -- so an Authorization
|
|
441
|
+
# header authenticates here too, never discarded (M19's
|
|
442
|
+
# property holds on both HTTP transports).
|
|
443
|
+
#
|
|
444
|
+
# ROADMAP_v3 §53: with the MCP meter on, the SDK's own factory
|
|
445
|
+
# builds the client and the meter's hooks go on it, so its
|
|
446
|
+
# timeouts are the SDK's and only the counting is ours.
|
|
447
|
+
if _net_common.MCP.enabled():
|
|
448
|
+
return sse_client(
|
|
449
|
+
cfg.url,
|
|
450
|
+
headers=dict(cfg.headers) or None,
|
|
451
|
+
httpx_client_factory=_metered_mcp_client,
|
|
452
|
+
)
|
|
453
|
+
return sse_client(
|
|
454
|
+
cfg.url,
|
|
455
|
+
headers=dict(cfg.headers) or None,
|
|
456
|
+
)
|
|
457
|
+
if cfg.transport == "http":
|
|
458
|
+
# `headers` was parsed into ServerConfig and then never used:
|
|
459
|
+
# a bare URL carries none of them, so a server configured with
|
|
460
|
+
# an Authorization header connected UNAUTHENTICATED, with
|
|
461
|
+
# nothing indicating they had been discarded. In v2 headers
|
|
462
|
+
# live on the httpx2 client, and the SDK ships the helper that
|
|
463
|
+
# applies its own MCP defaults alongside them -- so this uses
|
|
464
|
+
# that rather than hand-building a client and losing the
|
|
465
|
+
# SSE-friendly timeouts. Without headers the SDK builds its own
|
|
466
|
+
# client, exactly as Client() does for a bare URL.
|
|
467
|
+
#
|
|
468
|
+
# NEVER a bare URL now, even without headers (batch 208): mcp
|
|
469
|
+
# 2.3.0 fails a call whose one server-sent event exceeds
|
|
470
|
+
# `max_sse_event_size`, and a bare URL takes the SDK's 1 MiB,
|
|
471
|
+
# which a streamed screenshot passes. config.yaml's
|
|
472
|
+
# `mcp_max_event_bytes` is the ceiling, read per connection. It
|
|
473
|
+
# bounds STREAMED answers only: one sent as plain JSON is read
|
|
474
|
+
# whole (CONFIG_ARCHITECTURE.md has the measurement). The legacy
|
|
475
|
+
# "sse" branch above keeps the SDK's fixed 1 MiB: `sse_client`
|
|
476
|
+
# takes no size.
|
|
477
|
+
#
|
|
478
|
+
# ROADMAP_v3 §53: with the MCP meter on there is ALWAYS a client
|
|
479
|
+
# of ours, headers or not, because the hooks have to be on it.
|
|
480
|
+
# Built by the SDK's helper either way, so its timeouts hold.
|
|
481
|
+
if _net_common.MCP.enabled():
|
|
482
|
+
http_client = _metered_mcp_client(
|
|
483
|
+
headers=dict(cfg.headers) or None)
|
|
484
|
+
elif cfg.headers:
|
|
485
|
+
http_client = create_mcp_http_client(headers=dict(cfg.headers))
|
|
486
|
+
else:
|
|
487
|
+
http_client = None
|
|
488
|
+
return streamable_http_client(
|
|
489
|
+
cfg.url,
|
|
490
|
+
http_client=http_client,
|
|
491
|
+
max_sse_event_size=_harness_config.MCP_MAX_EVENT_BYTES,
|
|
492
|
+
)
|
|
493
|
+
raise ValueError(cfg.error or "unusable server configuration")
|
|
494
|
+
|
|
495
|
+
@staticmethod
|
|
496
|
+
def _reason(e: BaseException, budget: Optional[float] = None) -> str:
|
|
497
|
+
"""Why a server is unusable, in one string for BOTH consumers.
|
|
498
|
+
|
|
499
|
+
Batch 121, from the owner's log: `MCP server 'joplin' failed to
|
|
500
|
+
connect: ` and nothing after the colon. The per-server
|
|
501
|
+
asyncio.timeout raises TimeoutError, whose str() is "" -- so the
|
|
502
|
+
%s below rendered nothing, and the one message that explains a
|
|
503
|
+
silent MCP named no cause at all.
|
|
504
|
+
|
|
505
|
+
It was also TWO strings: `failures` carried the exception type and
|
|
506
|
+
the log line did not, so the better of the two never reached the
|
|
507
|
+
place a user looks first. One copy now, and the timeout names its
|
|
508
|
+
budget because "it timed out" without a duration leaves the
|
|
509
|
+
reader unable to tell a dead server from a slow one.
|
|
510
|
+
"""
|
|
511
|
+
text = str(e)
|
|
512
|
+
if text:
|
|
513
|
+
return f"{type(e).__name__}: {text}"
|
|
514
|
+
if isinstance(e, TimeoutError) and budget is not None:
|
|
515
|
+
return f"{type(e).__name__} (no handshake within {budget}s)"
|
|
516
|
+
return type(e).__name__
|
|
517
|
+
|
|
518
|
+
async def _manager(self):
|
|
519
|
+
"""Owns the AsyncExitStack for the whole process lifetime, in ONE
|
|
520
|
+
task. See the module docstring -- this shape is required, not
|
|
521
|
+
stylistic."""
|
|
522
|
+
try:
|
|
523
|
+
async with contextlib.AsyncExitStack() as stack:
|
|
524
|
+
for name, cfg in self.server_configs.items():
|
|
525
|
+
if cfg.disabled:
|
|
526
|
+
logger.info("MCP server %r is disabled; skipping.", name)
|
|
527
|
+
continue
|
|
528
|
+
try:
|
|
529
|
+
# PER-SERVER bound, not just the caller's blanket
|
|
530
|
+
# one. v2's list_tools() takes no
|
|
531
|
+
# read_timeout_seconds, so a server that spawns and
|
|
532
|
+
# then never answers the handshake would otherwise
|
|
533
|
+
# consume the whole shared connect budget and starve
|
|
534
|
+
# every server after it in this loop.
|
|
535
|
+
async with asyncio.timeout(SERVER_CONNECT_TIMEOUT_S):
|
|
536
|
+
client = await stack.enter_async_context(
|
|
537
|
+
_TrackClose(
|
|
538
|
+
Client(self._transport_for(cfg)), name,
|
|
539
|
+
self._closed)
|
|
540
|
+
)
|
|
541
|
+
# Validation is by connection (§17 decision G):
|
|
542
|
+
# a server that connects AND lists its tools is
|
|
543
|
+
# usable. The listing IS registration's
|
|
544
|
+
# catalogue (F8): cached below, served from,
|
|
545
|
+
# never fetched a second time.
|
|
546
|
+
tools = await client.list_tools()
|
|
547
|
+
self.clients[name] = client
|
|
548
|
+
self._catalogs[name] = list(tools.tools)
|
|
549
|
+
logger.info("MCP server %r connected (%d tools).",
|
|
550
|
+
name, len(tools.tools))
|
|
551
|
+
except Exception as e:
|
|
552
|
+
# One unreachable server must never take down the
|
|
553
|
+
# others, or a single stale entry makes the whole
|
|
554
|
+
# harness unusable. §17's sketch had no try/except
|
|
555
|
+
# here at all.
|
|
556
|
+
why = self._reason(e, SERVER_CONNECT_TIMEOUT_S)
|
|
557
|
+
self.failures[name] = why
|
|
558
|
+
logger.warning("MCP server %r failed to connect: %s",
|
|
559
|
+
name, why)
|
|
560
|
+
self._ready.set()
|
|
561
|
+
await self._shutdown.wait()
|
|
562
|
+
except Exception:
|
|
563
|
+
logger.exception("MCP manager task failed")
|
|
564
|
+
if self._ready is not None and not self._ready.is_set():
|
|
565
|
+
self._ready.set()
|
|
566
|
+
finally:
|
|
567
|
+
if self._done is not None:
|
|
568
|
+
self._done.set()
|
|
569
|
+
|
|
570
|
+
def connect_all(self, timeout: float = CONNECT_TIMEOUT_S) -> None:
|
|
571
|
+
"""Connect every configured server. Synchronous, callable from
|
|
572
|
+
ordinary startup code; the bridge does the rest."""
|
|
573
|
+
if not self.server_configs:
|
|
574
|
+
return
|
|
575
|
+
if self._task is not None:
|
|
576
|
+
# #219, batch 213, the owner's choice of raising over tearing
|
|
577
|
+
# down first. Every attribute below is rebound, so a second call
|
|
578
|
+
# used to abandon the first generation's manager parked on a
|
|
579
|
+
# `_shutdown` nothing could set any more -- its child processes
|
|
580
|
+
# with it, and no straggler warning. Nothing reconnects today
|
|
581
|
+
# (one call site, main.py); F7's runtime /mcp would, and it has
|
|
582
|
+
# to disconnect on purpose.
|
|
583
|
+
raise RuntimeError(
|
|
584
|
+
"connect_all() on an MCP client that is already connected; "
|
|
585
|
+
"call disconnect_all() first")
|
|
586
|
+
loop = _ensure_loop()
|
|
587
|
+
|
|
588
|
+
async def _start():
|
|
589
|
+
self._ready = asyncio.Event()
|
|
590
|
+
self._shutdown = asyncio.Event()
|
|
591
|
+
self._done = asyncio.Event()
|
|
592
|
+
self._task = loop.create_task(self._manager())
|
|
593
|
+
await self._ready.wait()
|
|
594
|
+
|
|
595
|
+
started = asyncio.run_coroutine_threadsafe(_start(), loop)
|
|
596
|
+
try:
|
|
597
|
+
started.result(timeout=timeout)
|
|
598
|
+
except Exception:
|
|
599
|
+
# A timeout here used to abandon the manager permanently. It
|
|
600
|
+
# would still be parked mid-connect holding every child process
|
|
601
|
+
# already spawned, and NOTHING could close it: disconnect_all()
|
|
602
|
+
# waits on _done, which a stuck manager never sets. So one hung
|
|
603
|
+
# server orphaned every other server's subprocess -- the exact
|
|
604
|
+
# inversion of this file's "one unreachable server must never
|
|
605
|
+
# take down the others" invariant.
|
|
606
|
+
#
|
|
607
|
+
# `started` IS RELEASED TOO (A33), and it is a different object
|
|
608
|
+
# from the manager: _start() is still awaiting _ready when the
|
|
609
|
+
# caller's budget runs out, and the two lines below stop the
|
|
610
|
+
# loop out from under it. Left pending, asyncio reports "Task
|
|
611
|
+
# was destroyed but it is pending!" through its own logger at
|
|
612
|
+
# whatever moment the GC gets to it -- and the TUI's
|
|
613
|
+
# TranscriptLogHandler is on the ROOT logger, so that line can
|
|
614
|
+
# surface in a user's transcript naming nothing and
|
|
615
|
+
# attributable to nothing. Cancelled FIRST, while the loop is
|
|
616
|
+
# still running to deliver it; _cancel_manager() needs the
|
|
617
|
+
# same loop alive and _shutdown_loop() is what ends it.
|
|
618
|
+
started.cancel()
|
|
619
|
+
self._cancel_manager()
|
|
620
|
+
self.clients.clear()
|
|
621
|
+
self._catalogs.clear()
|
|
622
|
+
self._task = None
|
|
623
|
+
_shutdown_loop()
|
|
624
|
+
raise
|
|
625
|
+
|
|
626
|
+
# -- calling ---------------------------------------------------------
|
|
627
|
+
|
|
628
|
+
def list_tools(self, server_name: str, timeout: float = 30.0):
|
|
629
|
+
"""The server's tool catalogue. Serves the connect-time cache
|
|
630
|
+
(F8) -- the one MCP call that had no protocol timeout -- and
|
|
631
|
+
crosses the bridge only on a miss. A copy comes back, so a
|
|
632
|
+
caller cannot mutate the cache."""
|
|
633
|
+
cached = self._catalogs.get(server_name)
|
|
634
|
+
if cached is not None:
|
|
635
|
+
return list(cached)
|
|
636
|
+
client = self.clients[server_name]
|
|
637
|
+
fut = asyncio.run_coroutine_threadsafe(client.list_tools(), _ensure_loop())
|
|
638
|
+
return fut.result(timeout=timeout).tools
|
|
639
|
+
|
|
640
|
+
def call_tool(self, server_name: str, tool_name: str, params: dict,
|
|
641
|
+
timeout: float = DEFAULT_CALL_TIMEOUT_S) -> dict:
|
|
642
|
+
"""The synchronous entry point every ToolSpec.handler calls.
|
|
643
|
+
|
|
644
|
+
Looks ordinary to registry.dispatch() regardless of whether the
|
|
645
|
+
caller sits on Textual's main loop, a worker thread, or a plain
|
|
646
|
+
CLI script -- the bridge is what makes caller context irrelevant.
|
|
647
|
+
|
|
648
|
+
Returns a plain dict, never a CallToolResult (D23, §17 AC6).
|
|
649
|
+
"""
|
|
650
|
+
client = self.clients.get(server_name)
|
|
651
|
+
if client is None:
|
|
652
|
+
reason = self.failures.get(server_name, "not connected")
|
|
653
|
+
return {"error": f"MCP server {server_name!r} is unavailable: {reason}"}
|
|
654
|
+
|
|
655
|
+
# ROADMAP_v3 §53 (IO11). Admitted HERE, before anything is sent, and
|
|
656
|
+
# never in the HTTP hooks: a refusal raised inside the SDK would
|
|
657
|
+
# surface in its background writer and read as a timeout. Only the
|
|
658
|
+
# servers the meter can see are asked -- a "stdio" server's traffic
|
|
659
|
+
# is its own process's, and IO5 says so rather than pretending.
|
|
660
|
+
cfg = self.server_configs.get(server_name)
|
|
661
|
+
if getattr(cfg, "transport", None) in ("http", "sse"):
|
|
662
|
+
try:
|
|
663
|
+
_net_common.MCP.admit(_net_common.mcp_call_bytes(params))
|
|
664
|
+
except _net_common.NetworkLimitReached as e:
|
|
665
|
+
return {"error": e.reason}
|
|
666
|
+
|
|
667
|
+
loop = _ensure_loop()
|
|
668
|
+
protocol_timeout = max(1.0, timeout - _TIMEOUT_MARGIN_S)
|
|
669
|
+
coro = client.call_tool(tool_name, params,
|
|
670
|
+
read_timeout_seconds=protocol_timeout)
|
|
671
|
+
future = asyncio.run_coroutine_threadsafe(coro, loop)
|
|
672
|
+
try:
|
|
673
|
+
return _normalize(future.result(timeout=timeout), tool_name)
|
|
674
|
+
except MCPError as e:
|
|
675
|
+
# v2 raises for protocol-level failures -- including timeout,
|
|
676
|
+
# which is code -32001 -- where v1 reported everything in
|
|
677
|
+
# band. Map onto the same {"error": ...} convention so the
|
|
678
|
+
# loop keeps exactly one error shape to understand.
|
|
679
|
+
return {"error": f"MCP call failed: {e}"}
|
|
680
|
+
except TimeoutError:
|
|
681
|
+
# The protocol timeout should have fired first. Reaching here
|
|
682
|
+
# means the loop itself is wedged; cancel so the coroutine
|
|
683
|
+
# can't outlive the call.
|
|
684
|
+
future.cancel()
|
|
685
|
+
return {"error": f"MCP tool {tool_name!r} timed out after {timeout}s"}
|
|
686
|
+
except Exception as e:
|
|
687
|
+
# LAST, so the two specific branches above keep their handling
|
|
688
|
+
# (notably future.cancel()). MCPError is not the only thing v2
|
|
689
|
+
# raises: an outputSchema violation surfaces as a bare
|
|
690
|
+
# RuntimeError and a malformed payload as a pydantic
|
|
691
|
+
# ValidationError. dispatch() has no handler-exception catch
|
|
692
|
+
# and _run() catches only ToolCallDenied, so anything escaping
|
|
693
|
+
# here takes down the whole conversation -- or all ten research
|
|
694
|
+
# passes -- on one buggy third-party server. D23 promises this
|
|
695
|
+
# function returns ONE error shape; that has to mean always.
|
|
696
|
+
logger.debug("MCP call raised", exc_info=True)
|
|
697
|
+
return {"error": f"MCP call failed: {type(e).__name__}: {e}"}
|
|
698
|
+
|
|
699
|
+
# -- shutdown --------------------------------------------------------
|
|
700
|
+
|
|
701
|
+
def _cancel_manager(self, timeout: float = SHUTDOWN_TIMEOUT_S) -> None:
|
|
702
|
+
"""Force the manager task to unwind its exit stack.
|
|
703
|
+
|
|
704
|
+
Cancellation is the ONLY way to close the stack from outside the
|
|
705
|
+
graceful path, and it works for the same reason the graceful path
|
|
706
|
+
does: CancelledError is raised inside the manager's `async with`,
|
|
707
|
+
so __aexit__ runs in the task that entered it. Calling aclose()
|
|
708
|
+
from any other task is the cancel-scope error this module's
|
|
709
|
+
docstring is about.
|
|
710
|
+
"""
|
|
711
|
+
task, loop = self._task, _loop
|
|
712
|
+
if task is None or loop is None:
|
|
713
|
+
return
|
|
714
|
+
try:
|
|
715
|
+
async def _cancel():
|
|
716
|
+
task.cancel()
|
|
717
|
+
if self._done is not None:
|
|
718
|
+
await self._done.wait()
|
|
719
|
+
asyncio.run_coroutine_threadsafe(_cancel(), loop).result(timeout=timeout)
|
|
720
|
+
except Exception as e:
|
|
721
|
+
logger.warning("MCP manager task did not cancel cleanly: %s", e)
|
|
722
|
+
|
|
723
|
+
def disconnect_all(self, timeout: float = None) -> None:
|
|
724
|
+
"""Close every session and stop the loop thread.
|
|
725
|
+
|
|
726
|
+
Not tidiness: stdio servers are child processes THIS harness
|
|
727
|
+
spawned. The daemon thread dies at interpreter exit without
|
|
728
|
+
unwinding the stack, and the failure mode is orphaned
|
|
729
|
+
subprocesses accumulating across sessions.
|
|
730
|
+
|
|
731
|
+
F6 (#64): `timeout` is ONE budget for everything -- the polite
|
|
732
|
+
close gets its first slice, force-cancel takes what remains
|
|
733
|
+
(minus a join reserve), the loop-thread join takes the rest. It
|
|
734
|
+
used to be three sequential SHUTDOWN_TIMEOUT_S waits, so one
|
|
735
|
+
wedged server hung a quitting harness ~45s and Ctrl+C paid all
|
|
736
|
+
of it. Servers that never confirmed a clean close by expiry are
|
|
737
|
+
named in a WARNING.
|
|
738
|
+
"""
|
|
739
|
+
budget = TEARDOWN_BUDGET_S if timeout is None else timeout
|
|
740
|
+
if self._task is None or _loop is None:
|
|
741
|
+
_shutdown_loop()
|
|
742
|
+
return
|
|
743
|
+
|
|
744
|
+
started = time.monotonic()
|
|
745
|
+
|
|
746
|
+
def remaining() -> float:
|
|
747
|
+
return max(0.0, budget - (time.monotonic() - started))
|
|
748
|
+
|
|
749
|
+
graceful = False
|
|
750
|
+
try:
|
|
751
|
+
async def _stop():
|
|
752
|
+
self._shutdown.set()
|
|
753
|
+
await self._done.wait()
|
|
754
|
+
asyncio.run_coroutine_threadsafe(_stop(), _loop).result(
|
|
755
|
+
timeout=max(1.0, budget * 0.6))
|
|
756
|
+
graceful = True
|
|
757
|
+
except Exception as e:
|
|
758
|
+
logger.warning("MCP shutdown did not complete cleanly within "
|
|
759
|
+
"%.1fs of the %.1fs teardown budget: %s",
|
|
760
|
+
max(1.0, budget * 0.6), budget, e)
|
|
761
|
+
if not graceful:
|
|
762
|
+
# The stack is still open and the children are still ours.
|
|
763
|
+
# Asking politely didn't work, so cancel -- otherwise this
|
|
764
|
+
# returns having reaped nothing while reporting only a warning.
|
|
765
|
+
self._cancel_manager(timeout=max(1.0, remaining() - 1.0))
|
|
766
|
+
|
|
767
|
+
stragglers = sorted(set(self.clients) - self._closed)
|
|
768
|
+
if stragglers:
|
|
769
|
+
logger.warning(
|
|
770
|
+
"MCP server(s) %s did not confirm a clean close within the "
|
|
771
|
+
"%.1fs teardown budget; their child processes may outlive "
|
|
772
|
+
"this session.", ", ".join(stragglers), budget)
|
|
773
|
+
|
|
774
|
+
self.clients.clear()
|
|
775
|
+
self._catalogs.clear()
|
|
776
|
+
# #219. Keyed by server name, so a clean close kept from this
|
|
777
|
+
# generation would answer for the next one's straggler check.
|
|
778
|
+
self._closed.clear()
|
|
779
|
+
self._task = None
|
|
780
|
+
_shutdown_loop(join_timeout=max(0.5, remaining()))
|