calvyn-code 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (1718) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +217 -0
  3. package/README.zh-CN.md +180 -0
  4. package/acp_adapter/__init__.py +1 -0
  5. package/acp_adapter/__main__.py +5 -0
  6. package/acp_adapter/auth.py +68 -0
  7. package/acp_adapter/bootstrap/__init__.py +0 -0
  8. package/acp_adapter/bootstrap/bootstrap_browser_tools.ps1 +288 -0
  9. package/acp_adapter/bootstrap/bootstrap_browser_tools.sh +399 -0
  10. package/acp_adapter/entry.py +292 -0
  11. package/acp_adapter/events.py +265 -0
  12. package/acp_adapter/permissions.py +148 -0
  13. package/acp_adapter/server.py +1713 -0
  14. package/acp_adapter/session.py +629 -0
  15. package/acp_adapter/tools.py +1180 -0
  16. package/agent/__init__.py +6 -0
  17. package/agent/__pycache__/__init__.cpython-312.pyc +0 -0
  18. package/agent/__pycache__/account_usage.cpython-312.pyc +0 -0
  19. package/agent/__pycache__/anthropic_adapter.cpython-312.pyc +0 -0
  20. package/agent/__pycache__/async_utils.cpython-312.pyc +0 -0
  21. package/agent/__pycache__/auxiliary_client.cpython-312.pyc +0 -0
  22. package/agent/__pycache__/codex_responses_adapter.cpython-312.pyc +0 -0
  23. package/agent/__pycache__/context_compressor.cpython-312.pyc +0 -0
  24. package/agent/__pycache__/context_engine.cpython-312.pyc +0 -0
  25. package/agent/__pycache__/context_references.cpython-312.pyc +0 -0
  26. package/agent/__pycache__/credential_pool.cpython-312.pyc +0 -0
  27. package/agent/__pycache__/curator.cpython-312.pyc +0 -0
  28. package/agent/__pycache__/display.cpython-312.pyc +0 -0
  29. package/agent/__pycache__/error_classifier.cpython-312.pyc +0 -0
  30. package/agent/__pycache__/file_safety.cpython-312.pyc +0 -0
  31. package/agent/__pycache__/google_code_assist.cpython-312.pyc +0 -0
  32. package/agent/__pycache__/google_oauth.cpython-312.pyc +0 -0
  33. package/agent/__pycache__/i18n.cpython-312.pyc +0 -0
  34. package/agent/__pycache__/image_gen_provider.cpython-312.pyc +0 -0
  35. package/agent/__pycache__/image_gen_registry.cpython-312.pyc +0 -0
  36. package/agent/__pycache__/insights.cpython-312.pyc +0 -0
  37. package/agent/__pycache__/lmstudio_reasoning.cpython-312.pyc +0 -0
  38. package/agent/__pycache__/manual_compression_feedback.cpython-312.pyc +0 -0
  39. package/agent/__pycache__/markdown_tables.cpython-312.pyc +0 -0
  40. package/agent/__pycache__/memory_manager.cpython-312.pyc +0 -0
  41. package/agent/__pycache__/memory_provider.cpython-312.pyc +0 -0
  42. package/agent/__pycache__/model_metadata.cpython-312.pyc +0 -0
  43. package/agent/__pycache__/models_dev.cpython-312.pyc +0 -0
  44. package/agent/__pycache__/moonshot_schema.cpython-312.pyc +0 -0
  45. package/agent/__pycache__/onboarding.cpython-312.pyc +0 -0
  46. package/agent/__pycache__/portal_tags.cpython-312.pyc +0 -0
  47. package/agent/__pycache__/prompt_builder.cpython-312.pyc +0 -0
  48. package/agent/__pycache__/prompt_caching.cpython-312.pyc +0 -0
  49. package/agent/__pycache__/redact.cpython-312.pyc +0 -0
  50. package/agent/__pycache__/retry_utils.cpython-312.pyc +0 -0
  51. package/agent/__pycache__/shell_hooks.cpython-312.pyc +0 -0
  52. package/agent/__pycache__/skill_commands.cpython-312.pyc +0 -0
  53. package/agent/__pycache__/skill_preprocessing.cpython-312.pyc +0 -0
  54. package/agent/__pycache__/skill_utils.cpython-312.pyc +0 -0
  55. package/agent/__pycache__/subdirectory_hints.cpython-312.pyc +0 -0
  56. package/agent/__pycache__/think_scrubber.cpython-312.pyc +0 -0
  57. package/agent/__pycache__/title_generator.cpython-312.pyc +0 -0
  58. package/agent/__pycache__/tool_guardrails.cpython-312.pyc +0 -0
  59. package/agent/__pycache__/tool_result_classification.cpython-312.pyc +0 -0
  60. package/agent/__pycache__/trajectory.cpython-312.pyc +0 -0
  61. package/agent/__pycache__/usage_pricing.cpython-312.pyc +0 -0
  62. package/agent/__pycache__/video_gen_provider.cpython-312.pyc +0 -0
  63. package/agent/__pycache__/video_gen_registry.cpython-312.pyc +0 -0
  64. package/agent/__pycache__/web_search_provider.cpython-312.pyc +0 -0
  65. package/agent/__pycache__/web_search_registry.cpython-312.pyc +0 -0
  66. package/agent/account_usage.py +326 -0
  67. package/agent/anthropic_adapter.py +2087 -0
  68. package/agent/async_utils.py +68 -0
  69. package/agent/auxiliary_client.py +4893 -0
  70. package/agent/bedrock_adapter.py +1276 -0
  71. package/agent/codex_responses_adapter.py +1084 -0
  72. package/agent/context_compressor.py +1583 -0
  73. package/agent/context_engine.py +211 -0
  74. package/agent/context_references.py +519 -0
  75. package/agent/copilot_acp_client.py +684 -0
  76. package/agent/credential_pool.py +1780 -0
  77. package/agent/credential_sources.py +449 -0
  78. package/agent/curator.py +1782 -0
  79. package/agent/curator_backup.py +694 -0
  80. package/agent/display.py +987 -0
  81. package/agent/error_classifier.py +1058 -0
  82. package/agent/file_safety.py +112 -0
  83. package/agent/gemini_cloudcode_adapter.py +909 -0
  84. package/agent/gemini_native_adapter.py +971 -0
  85. package/agent/gemini_schema.py +99 -0
  86. package/agent/google_code_assist.py +452 -0
  87. package/agent/google_oauth.py +1062 -0
  88. package/agent/i18n.py +258 -0
  89. package/agent/image_gen_provider.py +243 -0
  90. package/agent/image_gen_registry.py +145 -0
  91. package/agent/image_routing.py +301 -0
  92. package/agent/insights.py +931 -0
  93. package/agent/lmstudio_reasoning.py +48 -0
  94. package/agent/lsp/__init__.py +106 -0
  95. package/agent/lsp/__pycache__/__init__.cpython-312.pyc +0 -0
  96. package/agent/lsp/__pycache__/cli.cpython-312.pyc +0 -0
  97. package/agent/lsp/__pycache__/client.cpython-312.pyc +0 -0
  98. package/agent/lsp/__pycache__/eventlog.cpython-312.pyc +0 -0
  99. package/agent/lsp/__pycache__/manager.cpython-312.pyc +0 -0
  100. package/agent/lsp/__pycache__/protocol.cpython-312.pyc +0 -0
  101. package/agent/lsp/__pycache__/servers.cpython-312.pyc +0 -0
  102. package/agent/lsp/__pycache__/workspace.cpython-312.pyc +0 -0
  103. package/agent/lsp/cli.py +308 -0
  104. package/agent/lsp/client.py +930 -0
  105. package/agent/lsp/eventlog.py +213 -0
  106. package/agent/lsp/install.py +376 -0
  107. package/agent/lsp/manager.py +644 -0
  108. package/agent/lsp/protocol.py +196 -0
  109. package/agent/lsp/range_shift.py +149 -0
  110. package/agent/lsp/reporter.py +78 -0
  111. package/agent/lsp/servers.py +1040 -0
  112. package/agent/lsp/workspace.py +223 -0
  113. package/agent/manual_compression_feedback.py +49 -0
  114. package/agent/markdown_tables.py +309 -0
  115. package/agent/memory_manager.py +556 -0
  116. package/agent/memory_provider.py +279 -0
  117. package/agent/model_metadata.py +1827 -0
  118. package/agent/models_dev.py +724 -0
  119. package/agent/moonshot_schema.py +231 -0
  120. package/agent/nous_rate_guard.py +326 -0
  121. package/agent/onboarding.py +193 -0
  122. package/agent/plugin_llm.py +1046 -0
  123. package/agent/portal_tags.py +64 -0
  124. package/agent/prompt_builder.py +1457 -0
  125. package/agent/prompt_caching.py +79 -0
  126. package/agent/rate_limit_tracker.py +246 -0
  127. package/agent/redact.py +403 -0
  128. package/agent/retry_utils.py +57 -0
  129. package/agent/shell_hooks.py +837 -0
  130. package/agent/skill_commands.py +502 -0
  131. package/agent/skill_preprocessing.py +131 -0
  132. package/agent/skill_utils.py +512 -0
  133. package/agent/subdirectory_hints.py +224 -0
  134. package/agent/think_scrubber.py +386 -0
  135. package/agent/title_generator.py +171 -0
  136. package/agent/tool_guardrails.py +458 -0
  137. package/agent/tool_result_classification.py +26 -0
  138. package/agent/trajectory.py +56 -0
  139. package/agent/transports/__init__.py +68 -0
  140. package/agent/transports/__pycache__/__init__.cpython-312.pyc +0 -0
  141. package/agent/transports/__pycache__/anthropic.cpython-312.pyc +0 -0
  142. package/agent/transports/__pycache__/base.cpython-312.pyc +0 -0
  143. package/agent/transports/__pycache__/bedrock.cpython-312.pyc +0 -0
  144. package/agent/transports/__pycache__/chat_completions.cpython-312.pyc +0 -0
  145. package/agent/transports/__pycache__/codex.cpython-312.pyc +0 -0
  146. package/agent/transports/__pycache__/types.cpython-312.pyc +0 -0
  147. package/agent/transports/anthropic.py +179 -0
  148. package/agent/transports/base.py +89 -0
  149. package/agent/transports/bedrock.py +154 -0
  150. package/agent/transports/chat_completions.py +614 -0
  151. package/agent/transports/codex.py +283 -0
  152. package/agent/transports/codex_app_server.py +368 -0
  153. package/agent/transports/codex_app_server_session.py +810 -0
  154. package/agent/transports/codex_event_projector.py +312 -0
  155. package/agent/transports/hermes_tools_mcp_server.py +233 -0
  156. package/agent/transports/types.py +162 -0
  157. package/agent/usage_pricing.py +877 -0
  158. package/agent/video_gen_provider.py +300 -0
  159. package/agent/video_gen_registry.py +117 -0
  160. package/agent/web_search_provider.py +221 -0
  161. package/agent/web_search_registry.py +262 -0
  162. package/assets/banner.png +0 -0
  163. package/batch_runner.py +1303 -0
  164. package/bin/calvyn.js +67 -0
  165. package/calvyn_bootstrap.py +130 -0
  166. package/calvyn_constants.py +346 -0
  167. package/calvyn_logging.py +390 -0
  168. package/calvyn_state.py +2967 -0
  169. package/calvyn_time.py +105 -0
  170. package/cli.py +14160 -0
  171. package/cron/__init__.py +42 -0
  172. package/cron/__pycache__/__init__.cpython-312.pyc +0 -0
  173. package/cron/__pycache__/jobs.cpython-312.pyc +0 -0
  174. package/cron/__pycache__/scheduler.cpython-312.pyc +0 -0
  175. package/cron/jobs.py +1160 -0
  176. package/cron/scheduler.py +1832 -0
  177. package/gateway/__init__.py +35 -0
  178. package/gateway/__pycache__/__init__.cpython-312.pyc +0 -0
  179. package/gateway/__pycache__/channel_directory.cpython-312.pyc +0 -0
  180. package/gateway/__pycache__/config.cpython-312.pyc +0 -0
  181. package/gateway/__pycache__/delivery.cpython-312.pyc +0 -0
  182. package/gateway/__pycache__/display_config.cpython-312.pyc +0 -0
  183. package/gateway/__pycache__/hooks.cpython-312.pyc +0 -0
  184. package/gateway/__pycache__/pairing.cpython-312.pyc +0 -0
  185. package/gateway/__pycache__/platform_registry.cpython-312.pyc +0 -0
  186. package/gateway/__pycache__/restart.cpython-312.pyc +0 -0
  187. package/gateway/__pycache__/run.cpython-312.pyc +0 -0
  188. package/gateway/__pycache__/runtime_footer.cpython-312.pyc +0 -0
  189. package/gateway/__pycache__/session.cpython-312.pyc +0 -0
  190. package/gateway/__pycache__/session_context.cpython-312.pyc +0 -0
  191. package/gateway/__pycache__/shutdown_forensics.cpython-312.pyc +0 -0
  192. package/gateway/__pycache__/slash_access.cpython-312.pyc +0 -0
  193. package/gateway/__pycache__/status.cpython-312.pyc +0 -0
  194. package/gateway/__pycache__/stream_consumer.cpython-312.pyc +0 -0
  195. package/gateway/__pycache__/whatsapp_identity.cpython-312.pyc +0 -0
  196. package/gateway/assets/telegram-botfather-threads-settings.jpg +0 -0
  197. package/gateway/builtin_hooks/__init__.py +1 -0
  198. package/gateway/channel_directory.py +357 -0
  199. package/gateway/config.py +1873 -0
  200. package/gateway/delivery.py +258 -0
  201. package/gateway/display_config.py +206 -0
  202. package/gateway/hooks.py +210 -0
  203. package/gateway/mirror.py +179 -0
  204. package/gateway/pairing.py +322 -0
  205. package/gateway/platform_registry.py +260 -0
  206. package/gateway/platforms/ADDING_A_PLATFORM.md +374 -0
  207. package/gateway/platforms/__init__.py +45 -0
  208. package/gateway/platforms/__pycache__/__init__.cpython-312.pyc +0 -0
  209. package/gateway/platforms/__pycache__/base.cpython-312.pyc +0 -0
  210. package/gateway/platforms/__pycache__/helpers.cpython-312.pyc +0 -0
  211. package/gateway/platforms/__pycache__/telegram.cpython-312.pyc +0 -0
  212. package/gateway/platforms/__pycache__/telegram_network.cpython-312.pyc +0 -0
  213. package/gateway/platforms/__pycache__/yuanbao.cpython-312.pyc +0 -0
  214. package/gateway/platforms/__pycache__/yuanbao_media.cpython-312.pyc +0 -0
  215. package/gateway/platforms/__pycache__/yuanbao_proto.cpython-312.pyc +0 -0
  216. package/gateway/platforms/_http_client_limits.py +84 -0
  217. package/gateway/platforms/api_server.py +3488 -0
  218. package/gateway/platforms/base.py +3747 -0
  219. package/gateway/platforms/bluebubbles.py +937 -0
  220. package/gateway/platforms/dingtalk.py +1473 -0
  221. package/gateway/platforms/discord.py +5584 -0
  222. package/gateway/platforms/email.py +773 -0
  223. package/gateway/platforms/feishu.py +5059 -0
  224. package/gateway/platforms/feishu_comment.py +1382 -0
  225. package/gateway/platforms/feishu_comment_rules.py +430 -0
  226. package/gateway/platforms/helpers.py +279 -0
  227. package/gateway/platforms/homeassistant.py +449 -0
  228. package/gateway/platforms/matrix.py +2777 -0
  229. package/gateway/platforms/mattermost.py +852 -0
  230. package/gateway/platforms/msgraph_webhook.py +397 -0
  231. package/gateway/platforms/qqbot/__init__.py +91 -0
  232. package/gateway/platforms/qqbot/adapter.py +3072 -0
  233. package/gateway/platforms/qqbot/chunked_upload.py +602 -0
  234. package/gateway/platforms/qqbot/constants.py +74 -0
  235. package/gateway/platforms/qqbot/crypto.py +45 -0
  236. package/gateway/platforms/qqbot/keyboards.py +473 -0
  237. package/gateway/platforms/qqbot/onboard.py +220 -0
  238. package/gateway/platforms/qqbot/utils.py +71 -0
  239. package/gateway/platforms/signal.py +1518 -0
  240. package/gateway/platforms/signal_rate_limit.py +369 -0
  241. package/gateway/platforms/slack.py +3028 -0
  242. package/gateway/platforms/sms.py +377 -0
  243. package/gateway/platforms/telegram.py +4836 -0
  244. package/gateway/platforms/telegram_network.py +249 -0
  245. package/gateway/platforms/webhook.py +806 -0
  246. package/gateway/platforms/wecom.py +1610 -0
  247. package/gateway/platforms/wecom_callback.py +403 -0
  248. package/gateway/platforms/wecom_crypto.py +142 -0
  249. package/gateway/platforms/weixin.py +2170 -0
  250. package/gateway/platforms/whatsapp.py +1283 -0
  251. package/gateway/platforms/yuanbao.py +4873 -0
  252. package/gateway/platforms/yuanbao_media.py +645 -0
  253. package/gateway/platforms/yuanbao_proto.py +1209 -0
  254. package/gateway/platforms/yuanbao_sticker.py +558 -0
  255. package/gateway/restart.py +20 -0
  256. package/gateway/run.py +17074 -0
  257. package/gateway/runtime_footer.py +150 -0
  258. package/gateway/session.py +1399 -0
  259. package/gateway/session_context.py +156 -0
  260. package/gateway/shutdown_forensics.py +462 -0
  261. package/gateway/slash_access.py +229 -0
  262. package/gateway/status.py +972 -0
  263. package/gateway/sticker_cache.py +111 -0
  264. package/gateway/stream_consumer.py +1286 -0
  265. package/gateway/whatsapp_identity.py +156 -0
  266. package/hermes_cli/__init__.py +47 -0
  267. package/hermes_cli/__pycache__/__init__.cpython-312.pyc +0 -0
  268. package/hermes_cli/__pycache__/_parser.cpython-312.pyc +0 -0
  269. package/hermes_cli/__pycache__/auth.cpython-312.pyc +0 -0
  270. package/hermes_cli/__pycache__/banner.cpython-312.pyc +0 -0
  271. package/hermes_cli/__pycache__/browser_connect.cpython-312.pyc +0 -0
  272. package/hermes_cli/__pycache__/callbacks.cpython-312.pyc +0 -0
  273. package/hermes_cli/__pycache__/checkpoints.cpython-312.pyc +0 -0
  274. package/hermes_cli/__pycache__/cli_output.cpython-312.pyc +0 -0
  275. package/hermes_cli/__pycache__/codex_models.cpython-312.pyc +0 -0
  276. package/hermes_cli/__pycache__/codex_runtime_switch.cpython-312.pyc +0 -0
  277. package/hermes_cli/__pycache__/colors.cpython-312.pyc +0 -0
  278. package/hermes_cli/__pycache__/commands.cpython-312.pyc +0 -0
  279. package/hermes_cli/__pycache__/config.cpython-312.pyc +0 -0
  280. package/hermes_cli/__pycache__/copilot_auth.cpython-312.pyc +0 -0
  281. package/hermes_cli/__pycache__/curator.cpython-312.pyc +0 -0
  282. package/hermes_cli/__pycache__/curses_ui.cpython-312.pyc +0 -0
  283. package/hermes_cli/__pycache__/debug.cpython-312.pyc +0 -0
  284. package/hermes_cli/__pycache__/default_soul.cpython-312.pyc +0 -0
  285. package/hermes_cli/__pycache__/env_loader.cpython-312.pyc +0 -0
  286. package/hermes_cli/__pycache__/fallback_cmd.cpython-312.pyc +0 -0
  287. package/hermes_cli/__pycache__/gateway.cpython-312.pyc +0 -0
  288. package/hermes_cli/__pycache__/gateway_windows.cpython-312.pyc +0 -0
  289. package/hermes_cli/__pycache__/goals.cpython-312.pyc +0 -0
  290. package/hermes_cli/__pycache__/inventory.cpython-312.pyc +0 -0
  291. package/hermes_cli/__pycache__/kanban.cpython-312.pyc +0 -0
  292. package/hermes_cli/__pycache__/kanban_db.cpython-312.pyc +0 -0
  293. package/hermes_cli/__pycache__/main.cpython-312.pyc +0 -0
  294. package/hermes_cli/__pycache__/model_catalog.cpython-312.pyc +0 -0
  295. package/hermes_cli/__pycache__/model_normalize.cpython-312.pyc +0 -0
  296. package/hermes_cli/__pycache__/model_switch.cpython-312.pyc +0 -0
  297. package/hermes_cli/__pycache__/models.cpython-312.pyc +0 -0
  298. package/hermes_cli/__pycache__/nous_subscription.cpython-312.pyc +0 -0
  299. package/hermes_cli/__pycache__/pairing.cpython-312.pyc +0 -0
  300. package/hermes_cli/__pycache__/platforms.cpython-312.pyc +0 -0
  301. package/hermes_cli/__pycache__/plugins.cpython-312.pyc +0 -0
  302. package/hermes_cli/__pycache__/profiles.cpython-312.pyc +0 -0
  303. package/hermes_cli/__pycache__/providers.cpython-312.pyc +0 -0
  304. package/hermes_cli/__pycache__/pt_input_extras.cpython-312.pyc +0 -0
  305. package/hermes_cli/__pycache__/runtime_provider.cpython-312.pyc +0 -0
  306. package/hermes_cli/__pycache__/security_advisories.cpython-312.pyc +0 -0
  307. package/hermes_cli/__pycache__/setup.cpython-312.pyc +0 -0
  308. package/hermes_cli/__pycache__/skills_hub.cpython-312.pyc +0 -0
  309. package/hermes_cli/__pycache__/skin_engine.cpython-312.pyc +0 -0
  310. package/hermes_cli/__pycache__/stdio.cpython-312.pyc +0 -0
  311. package/hermes_cli/__pycache__/timeouts.cpython-312.pyc +0 -0
  312. package/hermes_cli/__pycache__/tips.cpython-312.pyc +0 -0
  313. package/hermes_cli/__pycache__/tools_config.cpython-312.pyc +0 -0
  314. package/hermes_cli/__pycache__/voice.cpython-312.pyc +0 -0
  315. package/hermes_cli/_parser.py +365 -0
  316. package/hermes_cli/_subprocess_compat.py +175 -0
  317. package/hermes_cli/auth.py +6299 -0
  318. package/hermes_cli/auth_commands.py +749 -0
  319. package/hermes_cli/azure_detect.py +300 -0
  320. package/hermes_cli/backup.py +938 -0
  321. package/hermes_cli/banner.py +703 -0
  322. package/hermes_cli/browser_connect.py +139 -0
  323. package/hermes_cli/callbacks.py +243 -0
  324. package/hermes_cli/checkpoints.py +244 -0
  325. package/hermes_cli/claw.py +810 -0
  326. package/hermes_cli/cli_output.py +78 -0
  327. package/hermes_cli/clipboard.py +495 -0
  328. package/hermes_cli/codex_models.py +198 -0
  329. package/hermes_cli/codex_runtime_plugin_migration.py +757 -0
  330. package/hermes_cli/codex_runtime_switch.py +266 -0
  331. package/hermes_cli/colors.py +38 -0
  332. package/hermes_cli/commands.py +1728 -0
  333. package/hermes_cli/completion.py +315 -0
  334. package/hermes_cli/config.py +5382 -0
  335. package/hermes_cli/copilot_auth.py +392 -0
  336. package/hermes_cli/cron.py +313 -0
  337. package/hermes_cli/curator.py +598 -0
  338. package/hermes_cli/curses_ui.py +472 -0
  339. package/hermes_cli/debug.py +747 -0
  340. package/hermes_cli/default_soul.py +11 -0
  341. package/hermes_cli/dep_ensure.py +107 -0
  342. package/hermes_cli/dingtalk_auth.py +293 -0
  343. package/hermes_cli/doctor.py +1863 -0
  344. package/hermes_cli/dump.py +326 -0
  345. package/hermes_cli/env_loader.py +175 -0
  346. package/hermes_cli/fallback_cmd.py +361 -0
  347. package/hermes_cli/gateway.py +5422 -0
  348. package/hermes_cli/gateway_windows.py +692 -0
  349. package/hermes_cli/goals.py +757 -0
  350. package/hermes_cli/hooks.py +385 -0
  351. package/hermes_cli/inventory.py +240 -0
  352. package/hermes_cli/kanban.py +2252 -0
  353. package/hermes_cli/kanban_db.py +4840 -0
  354. package/hermes_cli/kanban_diagnostics.py +776 -0
  355. package/hermes_cli/kanban_specify.py +266 -0
  356. package/hermes_cli/logs.py +391 -0
  357. package/hermes_cli/main.py +12396 -0
  358. package/hermes_cli/mcp_config.py +781 -0
  359. package/hermes_cli/memory_setup.py +465 -0
  360. package/hermes_cli/model_catalog.py +330 -0
  361. package/hermes_cli/model_normalize.py +473 -0
  362. package/hermes_cli/model_switch.py +1777 -0
  363. package/hermes_cli/models.py +3789 -0
  364. package/hermes_cli/nous_subscription.py +799 -0
  365. package/hermes_cli/oneshot.py +351 -0
  366. package/hermes_cli/pairing.py +115 -0
  367. package/hermes_cli/platforms.py +83 -0
  368. package/hermes_cli/plugins.py +1562 -0
  369. package/hermes_cli/plugins_cmd.py +1587 -0
  370. package/hermes_cli/profile_distribution.py +703 -0
  371. package/hermes_cli/profiles.py +1319 -0
  372. package/hermes_cli/providers.py +720 -0
  373. package/hermes_cli/proxy/__init__.py +20 -0
  374. package/hermes_cli/proxy/adapters/__init__.py +35 -0
  375. package/hermes_cli/proxy/adapters/base.py +94 -0
  376. package/hermes_cli/proxy/adapters/nous_portal.py +137 -0
  377. package/hermes_cli/proxy/cli.py +141 -0
  378. package/hermes_cli/proxy/server.py +265 -0
  379. package/hermes_cli/pt_input_extras.py +83 -0
  380. package/hermes_cli/pty_bridge.py +237 -0
  381. package/hermes_cli/relaunch.py +205 -0
  382. package/hermes_cli/runtime_provider.py +1428 -0
  383. package/hermes_cli/security_advisories.py +452 -0
  384. package/hermes_cli/setup.py +3559 -0
  385. package/hermes_cli/skills_config.py +177 -0
  386. package/hermes_cli/skills_hub.py +1595 -0
  387. package/hermes_cli/skin_engine.py +929 -0
  388. package/hermes_cli/slack_cli.py +160 -0
  389. package/hermes_cli/status.py +550 -0
  390. package/hermes_cli/stdio.py +252 -0
  391. package/hermes_cli/timeouts.py +82 -0
  392. package/hermes_cli/tips.py +487 -0
  393. package/hermes_cli/tools_config.py +3151 -0
  394. package/hermes_cli/uninstall.py +681 -0
  395. package/hermes_cli/vercel_auth.py +70 -0
  396. package/hermes_cli/voice.py +846 -0
  397. package/hermes_cli/web_server.py +4438 -0
  398. package/hermes_cli/webhook.py +275 -0
  399. package/locales/af.yaml +350 -0
  400. package/locales/de.yaml +350 -0
  401. package/locales/en.yaml +365 -0
  402. package/locales/es.yaml +350 -0
  403. package/locales/fr.yaml +350 -0
  404. package/locales/ga.yaml +354 -0
  405. package/locales/hu.yaml +350 -0
  406. package/locales/it.yaml +350 -0
  407. package/locales/ja.yaml +350 -0
  408. package/locales/ko.yaml +350 -0
  409. package/locales/pt.yaml +350 -0
  410. package/locales/ru.yaml +350 -0
  411. package/locales/tr.yaml +350 -0
  412. package/locales/uk.yaml +350 -0
  413. package/locales/zh-hant.yaml +350 -0
  414. package/locales/zh.yaml +350 -0
  415. package/mcp_serve.py +898 -0
  416. package/model_tools.py +899 -0
  417. package/optional-skills/DESCRIPTION.md +24 -0
  418. package/optional-skills/autonomous-ai-agents/DESCRIPTION.md +2 -0
  419. package/optional-skills/autonomous-ai-agents/blackbox/SKILL.md +144 -0
  420. package/optional-skills/autonomous-ai-agents/honcho/SKILL.md +431 -0
  421. package/optional-skills/blockchain/evm/SKILL.md +211 -0
  422. package/optional-skills/blockchain/evm/scripts/evm_client.py +1508 -0
  423. package/optional-skills/blockchain/hyperliquid/SKILL.md +211 -0
  424. package/optional-skills/blockchain/hyperliquid/scripts/hyperliquid_client.py +1660 -0
  425. package/optional-skills/blockchain/solana/SKILL.md +208 -0
  426. package/optional-skills/blockchain/solana/scripts/solana_client.py +698 -0
  427. package/optional-skills/communication/DESCRIPTION.md +1 -0
  428. package/optional-skills/communication/one-three-one-rule/SKILL.md +104 -0
  429. package/optional-skills/creative/blender-mcp/SKILL.md +117 -0
  430. package/optional-skills/creative/concept-diagrams/SKILL.md +362 -0
  431. package/optional-skills/creative/concept-diagrams/examples/apartment-floor-plan-conversion.md +244 -0
  432. package/optional-skills/creative/concept-diagrams/examples/automated-password-reset-flow.md +276 -0
  433. package/optional-skills/creative/concept-diagrams/examples/autonomous-llm-research-agent-flow.md +240 -0
  434. package/optional-skills/creative/concept-diagrams/examples/banana-journey-tree-to-smoothie.md +161 -0
  435. package/optional-skills/creative/concept-diagrams/examples/commercial-aircraft-structure.md +209 -0
  436. package/optional-skills/creative/concept-diagrams/examples/cpu-ooo-microarchitecture.md +236 -0
  437. package/optional-skills/creative/concept-diagrams/examples/electricity-grid-flow.md +182 -0
  438. package/optional-skills/creative/concept-diagrams/examples/feature-film-production-pipeline.md +172 -0
  439. package/optional-skills/creative/concept-diagrams/examples/hospital-emergency-department-flow.md +165 -0
  440. package/optional-skills/creative/concept-diagrams/examples/ml-benchmark-grouped-bar-chart.md +114 -0
  441. package/optional-skills/creative/concept-diagrams/examples/place-order-uml-sequence.md +325 -0
  442. package/optional-skills/creative/concept-diagrams/examples/smart-city-infrastructure.md +173 -0
  443. package/optional-skills/creative/concept-diagrams/examples/smartphone-layer-anatomy.md +154 -0
  444. package/optional-skills/creative/concept-diagrams/examples/sn2-reaction-mechanism.md +247 -0
  445. package/optional-skills/creative/concept-diagrams/examples/wind-turbine-structure.md +338 -0
  446. package/optional-skills/creative/concept-diagrams/references/dashboard-patterns.md +43 -0
  447. package/optional-skills/creative/concept-diagrams/references/infrastructure-patterns.md +144 -0
  448. package/optional-skills/creative/concept-diagrams/references/physical-shape-cookbook.md +42 -0
  449. package/optional-skills/creative/concept-diagrams/templates/template.html +174 -0
  450. package/optional-skills/creative/hyperframes/SKILL.md +191 -0
  451. package/optional-skills/creative/hyperframes/references/cli.md +185 -0
  452. package/optional-skills/creative/hyperframes/references/composition.md +129 -0
  453. package/optional-skills/creative/hyperframes/references/features.md +289 -0
  454. package/optional-skills/creative/hyperframes/references/gsap.md +136 -0
  455. package/optional-skills/creative/hyperframes/references/troubleshooting.md +137 -0
  456. package/optional-skills/creative/hyperframes/references/website-to-video.md +145 -0
  457. package/optional-skills/creative/hyperframes/scripts/setup.sh +135 -0
  458. package/optional-skills/creative/kanban-video-orchestrator/SKILL.md +207 -0
  459. package/optional-skills/creative/kanban-video-orchestrator/assets/brief.md.tmpl +79 -0
  460. package/optional-skills/creative/kanban-video-orchestrator/assets/setup.sh.tmpl +185 -0
  461. package/optional-skills/creative/kanban-video-orchestrator/assets/soul.md.tmpl +38 -0
  462. package/optional-skills/creative/kanban-video-orchestrator/references/examples.md +227 -0
  463. package/optional-skills/creative/kanban-video-orchestrator/references/intake.md +166 -0
  464. package/optional-skills/creative/kanban-video-orchestrator/references/kanban-setup.md +276 -0
  465. package/optional-skills/creative/kanban-video-orchestrator/references/monitoring.md +180 -0
  466. package/optional-skills/creative/kanban-video-orchestrator/references/role-archetypes.md +298 -0
  467. package/optional-skills/creative/kanban-video-orchestrator/references/tool-matrix.md +317 -0
  468. package/optional-skills/creative/kanban-video-orchestrator/scripts/bootstrap_pipeline.py +501 -0
  469. package/optional-skills/creative/kanban-video-orchestrator/scripts/monitor.py +195 -0
  470. package/optional-skills/creative/meme-generation/EXAMPLES.md +46 -0
  471. package/optional-skills/creative/meme-generation/SKILL.md +130 -0
  472. package/optional-skills/creative/meme-generation/scripts/generate_meme.py +471 -0
  473. package/optional-skills/creative/meme-generation/scripts/templates.json +97 -0
  474. package/optional-skills/devops/cli/SKILL.md +156 -0
  475. package/optional-skills/devops/cli/references/app-discovery.md +112 -0
  476. package/optional-skills/devops/cli/references/authentication.md +59 -0
  477. package/optional-skills/devops/cli/references/cli-reference.md +104 -0
  478. package/optional-skills/devops/cli/references/running-apps.md +171 -0
  479. package/optional-skills/devops/docker-management/SKILL.md +281 -0
  480. package/optional-skills/devops/pinggy-tunnel/SKILL.md +309 -0
  481. package/optional-skills/devops/watchers/SKILL.md +112 -0
  482. package/optional-skills/devops/watchers/scripts/_watermark.py +148 -0
  483. package/optional-skills/devops/watchers/scripts/watch_github.py +168 -0
  484. package/optional-skills/devops/watchers/scripts/watch_http_json.py +131 -0
  485. package/optional-skills/devops/watchers/scripts/watch_rss.py +121 -0
  486. package/optional-skills/dogfood/DESCRIPTION.md +3 -0
  487. package/optional-skills/dogfood/adversarial-ux-test/SKILL.md +191 -0
  488. package/optional-skills/email/agentmail/SKILL.md +126 -0
  489. package/optional-skills/finance/3-statement-model/SKILL.md +433 -0
  490. package/optional-skills/finance/3-statement-model/references/formatting.md +118 -0
  491. package/optional-skills/finance/3-statement-model/references/formulas.md +292 -0
  492. package/optional-skills/finance/3-statement-model/references/sec-filings.md +125 -0
  493. package/optional-skills/finance/comps-analysis/SKILL.md +662 -0
  494. package/optional-skills/finance/dcf-model/SKILL.md +1270 -0
  495. package/optional-skills/finance/dcf-model/TROUBLESHOOTING.md +40 -0
  496. package/optional-skills/finance/dcf-model/requirements.txt +7 -0
  497. package/optional-skills/finance/dcf-model/scripts/validate_dcf.py +292 -0
  498. package/optional-skills/finance/excel-author/SKILL.md +244 -0
  499. package/optional-skills/finance/excel-author/scripts/recalc.py +88 -0
  500. package/optional-skills/finance/lbo-model/SKILL.md +291 -0
  501. package/optional-skills/finance/merger-model/SKILL.md +144 -0
  502. package/optional-skills/finance/pptx-author/SKILL.md +173 -0
  503. package/optional-skills/finance/stocks/SKILL.md +95 -0
  504. package/optional-skills/finance/stocks/scripts/stocks_client.py +755 -0
  505. package/optional-skills/health/DESCRIPTION.md +1 -0
  506. package/optional-skills/health/fitness-nutrition/SKILL.md +256 -0
  507. package/optional-skills/health/fitness-nutrition/references/FORMULAS.md +100 -0
  508. package/optional-skills/health/fitness-nutrition/scripts/body_calc.py +210 -0
  509. package/optional-skills/health/fitness-nutrition/scripts/nutrition_search.py +86 -0
  510. package/optional-skills/health/neuroskill-bci/SKILL.md +459 -0
  511. package/optional-skills/health/neuroskill-bci/references/api.md +286 -0
  512. package/optional-skills/health/neuroskill-bci/references/metrics.md +220 -0
  513. package/optional-skills/health/neuroskill-bci/references/protocols.md +452 -0
  514. package/optional-skills/mcp/DESCRIPTION.md +3 -0
  515. package/optional-skills/mcp/fastmcp/SKILL.md +300 -0
  516. package/optional-skills/mcp/fastmcp/references/fastmcp-cli.md +110 -0
  517. package/optional-skills/mcp/fastmcp/scripts/scaffold_fastmcp.py +56 -0
  518. package/optional-skills/mcp/fastmcp/templates/api_wrapper.py +54 -0
  519. package/optional-skills/mcp/fastmcp/templates/database_server.py +77 -0
  520. package/optional-skills/mcp/fastmcp/templates/file_processor.py +55 -0
  521. package/optional-skills/mcp/mcporter/SKILL.md +123 -0
  522. package/optional-skills/migration/DESCRIPTION.md +2 -0
  523. package/optional-skills/migration/openclaw-migration/SKILL.md +298 -0
  524. package/optional-skills/migration/openclaw-migration/scripts/openclaw_to_hermes.py +3136 -0
  525. package/optional-skills/mlops/accelerate/SKILL.md +336 -0
  526. package/optional-skills/mlops/accelerate/references/custom-plugins.md +453 -0
  527. package/optional-skills/mlops/accelerate/references/megatron-integration.md +489 -0
  528. package/optional-skills/mlops/accelerate/references/performance.md +525 -0
  529. package/optional-skills/mlops/chroma/SKILL.md +410 -0
  530. package/optional-skills/mlops/chroma/references/integration.md +38 -0
  531. package/optional-skills/mlops/clip/SKILL.md +257 -0
  532. package/optional-skills/mlops/clip/references/applications.md +207 -0
  533. package/optional-skills/mlops/faiss/SKILL.md +225 -0
  534. package/optional-skills/mlops/faiss/references/index_types.md +280 -0
  535. package/optional-skills/mlops/flash-attention/SKILL.md +367 -0
  536. package/optional-skills/mlops/flash-attention/references/benchmarks.md +215 -0
  537. package/optional-skills/mlops/flash-attention/references/transformers-integration.md +293 -0
  538. package/optional-skills/mlops/guidance/SKILL.md +576 -0
  539. package/optional-skills/mlops/guidance/references/backends.md +554 -0
  540. package/optional-skills/mlops/guidance/references/constraints.md +674 -0
  541. package/optional-skills/mlops/guidance/references/examples.md +767 -0
  542. package/optional-skills/mlops/huggingface-tokenizers/SKILL.md +520 -0
  543. package/optional-skills/mlops/huggingface-tokenizers/references/algorithms.md +653 -0
  544. package/optional-skills/mlops/huggingface-tokenizers/references/integration.md +637 -0
  545. package/optional-skills/mlops/huggingface-tokenizers/references/pipeline.md +723 -0
  546. package/optional-skills/mlops/huggingface-tokenizers/references/training.md +565 -0
  547. package/optional-skills/mlops/inference/outlines/SKILL.md +656 -0
  548. package/optional-skills/mlops/inference/outlines/references/backends.md +615 -0
  549. package/optional-skills/mlops/inference/outlines/references/examples.md +773 -0
  550. package/optional-skills/mlops/inference/outlines/references/json_generation.md +652 -0
  551. package/optional-skills/mlops/instructor/SKILL.md +744 -0
  552. package/optional-skills/mlops/instructor/references/examples.md +107 -0
  553. package/optional-skills/mlops/instructor/references/providers.md +70 -0
  554. package/optional-skills/mlops/instructor/references/validation.md +606 -0
  555. package/optional-skills/mlops/lambda-labs/SKILL.md +549 -0
  556. package/optional-skills/mlops/lambda-labs/references/advanced-usage.md +611 -0
  557. package/optional-skills/mlops/lambda-labs/references/troubleshooting.md +530 -0
  558. package/optional-skills/mlops/llava/SKILL.md +308 -0
  559. package/optional-skills/mlops/llava/references/training.md +197 -0
  560. package/optional-skills/mlops/modal/SKILL.md +345 -0
  561. package/optional-skills/mlops/modal/references/advanced-usage.md +503 -0
  562. package/optional-skills/mlops/modal/references/troubleshooting.md +494 -0
  563. package/optional-skills/mlops/nemo-curator/SKILL.md +387 -0
  564. package/optional-skills/mlops/nemo-curator/references/deduplication.md +87 -0
  565. package/optional-skills/mlops/nemo-curator/references/filtering.md +102 -0
  566. package/optional-skills/mlops/peft/SKILL.md +435 -0
  567. package/optional-skills/mlops/peft/references/advanced-usage.md +514 -0
  568. package/optional-skills/mlops/peft/references/troubleshooting.md +480 -0
  569. package/optional-skills/mlops/pinecone/SKILL.md +362 -0
  570. package/optional-skills/mlops/pinecone/references/deployment.md +181 -0
  571. package/optional-skills/mlops/pytorch-fsdp/SKILL.md +130 -0
  572. package/optional-skills/mlops/pytorch-fsdp/references/index.md +7 -0
  573. package/optional-skills/mlops/pytorch-fsdp/references/other.md +4261 -0
  574. package/optional-skills/mlops/pytorch-lightning/SKILL.md +350 -0
  575. package/optional-skills/mlops/pytorch-lightning/references/callbacks.md +436 -0
  576. package/optional-skills/mlops/pytorch-lightning/references/distributed.md +490 -0
  577. package/optional-skills/mlops/pytorch-lightning/references/hyperparameter-tuning.md +556 -0
  578. package/optional-skills/mlops/qdrant/SKILL.md +497 -0
  579. package/optional-skills/mlops/qdrant/references/advanced-usage.md +648 -0
  580. package/optional-skills/mlops/qdrant/references/troubleshooting.md +631 -0
  581. package/optional-skills/mlops/saelens/SKILL.md +390 -0
  582. package/optional-skills/mlops/saelens/references/README.md +69 -0
  583. package/optional-skills/mlops/saelens/references/api.md +333 -0
  584. package/optional-skills/mlops/saelens/references/tutorials.md +318 -0
  585. package/optional-skills/mlops/simpo/SKILL.md +223 -0
  586. package/optional-skills/mlops/simpo/references/datasets.md +478 -0
  587. package/optional-skills/mlops/simpo/references/hyperparameters.md +452 -0
  588. package/optional-skills/mlops/simpo/references/loss-functions.md +350 -0
  589. package/optional-skills/mlops/slime/SKILL.md +468 -0
  590. package/optional-skills/mlops/slime/references/api-reference.md +392 -0
  591. package/optional-skills/mlops/slime/references/troubleshooting.md +386 -0
  592. package/optional-skills/mlops/stable-diffusion/SKILL.md +523 -0
  593. package/optional-skills/mlops/stable-diffusion/references/advanced-usage.md +716 -0
  594. package/optional-skills/mlops/stable-diffusion/references/troubleshooting.md +555 -0
  595. package/optional-skills/mlops/tensorrt-llm/SKILL.md +191 -0
  596. package/optional-skills/mlops/tensorrt-llm/references/multi-gpu.md +298 -0
  597. package/optional-skills/mlops/tensorrt-llm/references/optimization.md +242 -0
  598. package/optional-skills/mlops/tensorrt-llm/references/serving.md +470 -0
  599. package/optional-skills/mlops/torchtitan/SKILL.md +362 -0
  600. package/optional-skills/mlops/torchtitan/references/checkpoint.md +181 -0
  601. package/optional-skills/mlops/torchtitan/references/custom-models.md +258 -0
  602. package/optional-skills/mlops/torchtitan/references/float8.md +133 -0
  603. package/optional-skills/mlops/torchtitan/references/fsdp.md +126 -0
  604. package/optional-skills/mlops/training/axolotl/SKILL.md +166 -0
  605. package/optional-skills/mlops/training/axolotl/references/api.md +5548 -0
  606. package/optional-skills/mlops/training/axolotl/references/dataset-formats.md +1029 -0
  607. package/optional-skills/mlops/training/axolotl/references/index.md +15 -0
  608. package/optional-skills/mlops/training/axolotl/references/other.md +3563 -0
  609. package/optional-skills/mlops/training/trl-fine-tuning/SKILL.md +463 -0
  610. package/optional-skills/mlops/training/trl-fine-tuning/references/dpo-variants.md +227 -0
  611. package/optional-skills/mlops/training/trl-fine-tuning/references/grpo-training.md +504 -0
  612. package/optional-skills/mlops/training/trl-fine-tuning/references/online-rl.md +82 -0
  613. package/optional-skills/mlops/training/trl-fine-tuning/references/reward-modeling.md +122 -0
  614. package/optional-skills/mlops/training/trl-fine-tuning/references/sft-training.md +168 -0
  615. package/optional-skills/mlops/training/trl-fine-tuning/templates/basic_grpo_training.py +228 -0
  616. package/optional-skills/mlops/training/unsloth/SKILL.md +84 -0
  617. package/optional-skills/mlops/training/unsloth/references/index.md +7 -0
  618. package/optional-skills/mlops/training/unsloth/references/llms-full.md +16799 -0
  619. package/optional-skills/mlops/training/unsloth/references/llms-txt.md +12044 -0
  620. package/optional-skills/mlops/training/unsloth/references/llms.md +82 -0
  621. package/optional-skills/mlops/whisper/SKILL.md +321 -0
  622. package/optional-skills/mlops/whisper/references/languages.md +189 -0
  623. package/optional-skills/productivity/canvas/SKILL.md +98 -0
  624. package/optional-skills/productivity/canvas/scripts/canvas_api.py +157 -0
  625. package/optional-skills/productivity/here-now/SKILL.md +217 -0
  626. package/optional-skills/productivity/here-now/scripts/drive.sh +406 -0
  627. package/optional-skills/productivity/here-now/scripts/publish.sh +445 -0
  628. package/optional-skills/productivity/memento-flashcards/SKILL.md +324 -0
  629. package/optional-skills/productivity/memento-flashcards/scripts/memento_cards.py +353 -0
  630. package/optional-skills/productivity/memento-flashcards/scripts/youtube_quiz.py +88 -0
  631. package/optional-skills/productivity/shop-app/SKILL.md +340 -0
  632. package/optional-skills/productivity/shopify/SKILL.md +373 -0
  633. package/optional-skills/productivity/siyuan/SKILL.md +298 -0
  634. package/optional-skills/productivity/telephony/SKILL.md +418 -0
  635. package/optional-skills/productivity/telephony/scripts/telephony.py +1343 -0
  636. package/optional-skills/research/bioinformatics/SKILL.md +235 -0
  637. package/optional-skills/research/darwinian-evolver/SKILL.md +199 -0
  638. package/optional-skills/research/darwinian-evolver/scripts/parrot_openrouter.py +218 -0
  639. package/optional-skills/research/darwinian-evolver/scripts/show_snapshot.py +69 -0
  640. package/optional-skills/research/darwinian-evolver/templates/custom_problem_template.py +240 -0
  641. package/optional-skills/research/domain-intel/SKILL.md +97 -0
  642. package/optional-skills/research/domain-intel/scripts/domain_intel.py +397 -0
  643. package/optional-skills/research/drug-discovery/SKILL.md +227 -0
  644. package/optional-skills/research/drug-discovery/references/ADMET_REFERENCE.md +66 -0
  645. package/optional-skills/research/drug-discovery/scripts/chembl_target.py +53 -0
  646. package/optional-skills/research/drug-discovery/scripts/ro5_screen.py +44 -0
  647. package/optional-skills/research/duckduckgo-search/SKILL.md +238 -0
  648. package/optional-skills/research/duckduckgo-search/scripts/duckduckgo.sh +28 -0
  649. package/optional-skills/research/gitnexus-explorer/SKILL.md +214 -0
  650. package/optional-skills/research/gitnexus-explorer/scripts/proxy.mjs +92 -0
  651. package/optional-skills/research/osint-investigation/SKILL.md +277 -0
  652. package/optional-skills/research/osint-investigation/references/sources/courtlistener.md +98 -0
  653. package/optional-skills/research/osint-investigation/references/sources/gdelt.md +104 -0
  654. package/optional-skills/research/osint-investigation/references/sources/icij-offshore.md +104 -0
  655. package/optional-skills/research/osint-investigation/references/sources/nyc-acris.md +90 -0
  656. package/optional-skills/research/osint-investigation/references/sources/ofac-sdn.md +92 -0
  657. package/optional-skills/research/osint-investigation/references/sources/opencorporates.md +103 -0
  658. package/optional-skills/research/osint-investigation/references/sources/sec-edgar.md +83 -0
  659. package/optional-skills/research/osint-investigation/references/sources/senate-ld.md +89 -0
  660. package/optional-skills/research/osint-investigation/references/sources/usaspending.md +97 -0
  661. package/optional-skills/research/osint-investigation/references/sources/wayback.md +93 -0
  662. package/optional-skills/research/osint-investigation/references/sources/wikipedia.md +107 -0
  663. package/optional-skills/research/osint-investigation/scripts/_http.py +82 -0
  664. package/optional-skills/research/osint-investigation/scripts/_normalize.py +67 -0
  665. package/optional-skills/research/osint-investigation/scripts/build_findings.py +221 -0
  666. package/optional-skills/research/osint-investigation/scripts/entity_resolution.py +228 -0
  667. package/optional-skills/research/osint-investigation/scripts/fetch_courtlistener.py +149 -0
  668. package/optional-skills/research/osint-investigation/scripts/fetch_gdelt.py +162 -0
  669. package/optional-skills/research/osint-investigation/scripts/fetch_icij_offshore.py +234 -0
  670. package/optional-skills/research/osint-investigation/scripts/fetch_nyc_acris.py +203 -0
  671. package/optional-skills/research/osint-investigation/scripts/fetch_ofac_sdn.py +175 -0
  672. package/optional-skills/research/osint-investigation/scripts/fetch_opencorporates.py +192 -0
  673. package/optional-skills/research/osint-investigation/scripts/fetch_sec_edgar.py +184 -0
  674. package/optional-skills/research/osint-investigation/scripts/fetch_senate_ld.py +146 -0
  675. package/optional-skills/research/osint-investigation/scripts/fetch_usaspending.py +170 -0
  676. package/optional-skills/research/osint-investigation/scripts/fetch_wayback.py +142 -0
  677. package/optional-skills/research/osint-investigation/scripts/fetch_wikipedia.py +267 -0
  678. package/optional-skills/research/osint-investigation/scripts/timing_analysis.py +253 -0
  679. package/optional-skills/research/osint-investigation/templates/source-template.md +59 -0
  680. package/optional-skills/research/parallel-cli/SKILL.md +391 -0
  681. package/optional-skills/research/qmd/SKILL.md +441 -0
  682. package/optional-skills/research/scrapling/SKILL.md +336 -0
  683. package/optional-skills/research/searxng-search/SKILL.md +212 -0
  684. package/optional-skills/research/searxng-search/scripts/searxng.sh +22 -0
  685. package/optional-skills/security/1password/SKILL.md +163 -0
  686. package/optional-skills/security/1password/references/cli-examples.md +31 -0
  687. package/optional-skills/security/1password/references/get-started.md +21 -0
  688. package/optional-skills/security/DESCRIPTION.md +3 -0
  689. package/optional-skills/security/oss-forensics/SKILL.md +423 -0
  690. package/optional-skills/security/oss-forensics/references/evidence-types.md +89 -0
  691. package/optional-skills/security/oss-forensics/references/github-archive-guide.md +184 -0
  692. package/optional-skills/security/oss-forensics/references/investigation-templates.md +131 -0
  693. package/optional-skills/security/oss-forensics/references/recovery-techniques.md +164 -0
  694. package/optional-skills/security/oss-forensics/scripts/evidence-store.py +313 -0
  695. package/optional-skills/security/oss-forensics/templates/forensic-report.md +151 -0
  696. package/optional-skills/security/oss-forensics/templates/malicious-package-report.md +43 -0
  697. package/optional-skills/security/sherlock/SKILL.md +193 -0
  698. package/optional-skills/software-development/rest-graphql-debug/SKILL.md +514 -0
  699. package/optional-skills/web-development/DESCRIPTION.md +5 -0
  700. package/optional-skills/web-development/page-agent/SKILL.md +190 -0
  701. package/package.json +78 -0
  702. package/plugins/__init__.py +1 -0
  703. package/plugins/__pycache__/__init__.cpython-312.pyc +0 -0
  704. package/plugins/context_engine/__init__.py +219 -0
  705. package/plugins/disk-cleanup/README.md +51 -0
  706. package/plugins/disk-cleanup/__init__.py +316 -0
  707. package/plugins/disk-cleanup/disk_cleanup.py +497 -0
  708. package/plugins/disk-cleanup/plugin.yaml +7 -0
  709. package/plugins/example-dashboard/dashboard/manifest.json +14 -0
  710. package/plugins/example-dashboard/dashboard/plugin_api.py +17 -0
  711. package/plugins/google_meet/README.md +131 -0
  712. package/plugins/google_meet/SKILL.md +148 -0
  713. package/plugins/google_meet/__init__.py +103 -0
  714. package/plugins/google_meet/audio_bridge.py +244 -0
  715. package/plugins/google_meet/cli.py +479 -0
  716. package/plugins/google_meet/meet_bot.py +852 -0
  717. package/plugins/google_meet/node/__init__.py +54 -0
  718. package/plugins/google_meet/node/cli.py +125 -0
  719. package/plugins/google_meet/node/client.py +107 -0
  720. package/plugins/google_meet/node/protocol.py +124 -0
  721. package/plugins/google_meet/node/registry.py +113 -0
  722. package/plugins/google_meet/node/server.py +201 -0
  723. package/plugins/google_meet/plugin.yaml +16 -0
  724. package/plugins/google_meet/process_manager.py +324 -0
  725. package/plugins/google_meet/realtime/__init__.py +10 -0
  726. package/plugins/google_meet/realtime/openai_client.py +332 -0
  727. package/plugins/google_meet/tools.py +348 -0
  728. package/plugins/hermes-achievements/LICENSE +21 -0
  729. package/plugins/hermes-achievements/README.md +150 -0
  730. package/plugins/hermes-achievements/dashboard/dist/index.js +732 -0
  731. package/plugins/hermes-achievements/dashboard/dist/style.css +146 -0
  732. package/plugins/hermes-achievements/dashboard/manifest.json +11 -0
  733. package/plugins/hermes-achievements/dashboard/plugin_api.py +1062 -0
  734. package/plugins/hermes-achievements/docs/achievements-performance-implementation-plan.md +157 -0
  735. package/plugins/hermes-achievements/docs/achievements-performance-implementation-spec.md +219 -0
  736. package/plugins/hermes-achievements/docs/achievements-performance-spec.md +174 -0
  737. package/plugins/hermes-achievements/docs/assets/achievements-dashboard-hd.png +0 -0
  738. package/plugins/hermes-achievements/docs/assets/achievements-tier-showcase-hd.png +0 -0
  739. package/plugins/hermes-achievements/tests/test_achievement_engine.py +156 -0
  740. package/plugins/image_gen/openai/__init__.py +303 -0
  741. package/plugins/image_gen/openai/__pycache__/__init__.cpython-312.pyc +0 -0
  742. package/plugins/image_gen/openai/plugin.yaml +7 -0
  743. package/plugins/image_gen/openai-codex/__init__.py +378 -0
  744. package/plugins/image_gen/openai-codex/__pycache__/__init__.cpython-312.pyc +0 -0
  745. package/plugins/image_gen/openai-codex/plugin.yaml +5 -0
  746. package/plugins/image_gen/xai/__init__.py +316 -0
  747. package/plugins/image_gen/xai/__pycache__/__init__.cpython-312.pyc +0 -0
  748. package/plugins/image_gen/xai/plugin.yaml +7 -0
  749. package/plugins/kanban/dashboard/dist/index.js +3143 -0
  750. package/plugins/kanban/dashboard/dist/style.css +1500 -0
  751. package/plugins/kanban/dashboard/manifest.json +14 -0
  752. package/plugins/kanban/dashboard/plugin_api.py +1612 -0
  753. package/plugins/kanban/systemd/hermes-kanban-dispatcher.service +32 -0
  754. package/plugins/memory/__init__.py +408 -0
  755. package/plugins/memory/byterover/README.md +41 -0
  756. package/plugins/memory/byterover/__init__.py +384 -0
  757. package/plugins/memory/byterover/plugin.yaml +9 -0
  758. package/plugins/memory/hindsight/README.md +138 -0
  759. package/plugins/memory/hindsight/__init__.py +1758 -0
  760. package/plugins/memory/hindsight/plugin.yaml +8 -0
  761. package/plugins/memory/holographic/README.md +36 -0
  762. package/plugins/memory/holographic/__init__.py +409 -0
  763. package/plugins/memory/holographic/holographic.py +203 -0
  764. package/plugins/memory/holographic/plugin.yaml +5 -0
  765. package/plugins/memory/holographic/retrieval.py +593 -0
  766. package/plugins/memory/holographic/store.py +579 -0
  767. package/plugins/memory/honcho/README.md +328 -0
  768. package/plugins/memory/honcho/__init__.py +1329 -0
  769. package/plugins/memory/honcho/cli.py +1452 -0
  770. package/plugins/memory/honcho/client.py +784 -0
  771. package/plugins/memory/honcho/plugin.yaml +7 -0
  772. package/plugins/memory/honcho/session.py +1255 -0
  773. package/plugins/memory/mem0/README.md +38 -0
  774. package/plugins/memory/mem0/__init__.py +374 -0
  775. package/plugins/memory/mem0/plugin.yaml +5 -0
  776. package/plugins/memory/openviking/README.md +40 -0
  777. package/plugins/memory/openviking/__init__.py +945 -0
  778. package/plugins/memory/openviking/plugin.yaml +9 -0
  779. package/plugins/memory/retaindb/README.md +40 -0
  780. package/plugins/memory/retaindb/__init__.py +767 -0
  781. package/plugins/memory/retaindb/plugin.yaml +7 -0
  782. package/plugins/memory/supermemory/README.md +99 -0
  783. package/plugins/memory/supermemory/__init__.py +792 -0
  784. package/plugins/memory/supermemory/plugin.yaml +5 -0
  785. package/plugins/model-providers/README.md +70 -0
  786. package/plugins/model-providers/ai-gateway/__init__.py +43 -0
  787. package/plugins/model-providers/ai-gateway/__pycache__/__init__.cpython-312.pyc +0 -0
  788. package/plugins/model-providers/ai-gateway/plugin.yaml +5 -0
  789. package/plugins/model-providers/alibaba/__init__.py +13 -0
  790. package/plugins/model-providers/alibaba/__pycache__/__init__.cpython-312.pyc +0 -0
  791. package/plugins/model-providers/alibaba/plugin.yaml +5 -0
  792. package/plugins/model-providers/alibaba-coding-plan/__init__.py +21 -0
  793. package/plugins/model-providers/alibaba-coding-plan/__pycache__/__init__.cpython-312.pyc +0 -0
  794. package/plugins/model-providers/alibaba-coding-plan/plugin.yaml +5 -0
  795. package/plugins/model-providers/anthropic/__init__.py +52 -0
  796. package/plugins/model-providers/anthropic/__pycache__/__init__.cpython-312.pyc +0 -0
  797. package/plugins/model-providers/anthropic/plugin.yaml +5 -0
  798. package/plugins/model-providers/arcee/__init__.py +13 -0
  799. package/plugins/model-providers/arcee/__pycache__/__init__.cpython-312.pyc +0 -0
  800. package/plugins/model-providers/arcee/plugin.yaml +5 -0
  801. package/plugins/model-providers/azure-foundry/__init__.py +21 -0
  802. package/plugins/model-providers/azure-foundry/__pycache__/__init__.cpython-312.pyc +0 -0
  803. package/plugins/model-providers/azure-foundry/plugin.yaml +5 -0
  804. package/plugins/model-providers/bedrock/__init__.py +29 -0
  805. package/plugins/model-providers/bedrock/__pycache__/__init__.cpython-312.pyc +0 -0
  806. package/plugins/model-providers/bedrock/plugin.yaml +5 -0
  807. package/plugins/model-providers/copilot/__init__.py +58 -0
  808. package/plugins/model-providers/copilot/__pycache__/__init__.cpython-312.pyc +0 -0
  809. package/plugins/model-providers/copilot/plugin.yaml +5 -0
  810. package/plugins/model-providers/copilot-acp/__init__.py +34 -0
  811. package/plugins/model-providers/copilot-acp/__pycache__/__init__.cpython-312.pyc +0 -0
  812. package/plugins/model-providers/copilot-acp/plugin.yaml +5 -0
  813. package/plugins/model-providers/custom/__init__.py +68 -0
  814. package/plugins/model-providers/custom/__pycache__/__init__.cpython-312.pyc +0 -0
  815. package/plugins/model-providers/custom/plugin.yaml +5 -0
  816. package/plugins/model-providers/deepseek/__init__.py +99 -0
  817. package/plugins/model-providers/deepseek/__pycache__/__init__.cpython-312.pyc +0 -0
  818. package/plugins/model-providers/deepseek/plugin.yaml +5 -0
  819. package/plugins/model-providers/gemini/__init__.py +72 -0
  820. package/plugins/model-providers/gemini/__pycache__/__init__.cpython-312.pyc +0 -0
  821. package/plugins/model-providers/gemini/plugin.yaml +5 -0
  822. package/plugins/model-providers/gmi/__init__.py +31 -0
  823. package/plugins/model-providers/gmi/__pycache__/__init__.cpython-312.pyc +0 -0
  824. package/plugins/model-providers/gmi/plugin.yaml +5 -0
  825. package/plugins/model-providers/huggingface/__init__.py +20 -0
  826. package/plugins/model-providers/huggingface/__pycache__/__init__.cpython-312.pyc +0 -0
  827. package/plugins/model-providers/huggingface/plugin.yaml +5 -0
  828. package/plugins/model-providers/kilocode/__init__.py +14 -0
  829. package/plugins/model-providers/kilocode/__pycache__/__init__.cpython-312.pyc +0 -0
  830. package/plugins/model-providers/kilocode/plugin.yaml +5 -0
  831. package/plugins/model-providers/kimi-coding/__init__.py +71 -0
  832. package/plugins/model-providers/kimi-coding/__pycache__/__init__.cpython-312.pyc +0 -0
  833. package/plugins/model-providers/kimi-coding/plugin.yaml +5 -0
  834. package/plugins/model-providers/minimax/__init__.py +45 -0
  835. package/plugins/model-providers/minimax/__pycache__/__init__.cpython-312.pyc +0 -0
  836. package/plugins/model-providers/minimax/plugin.yaml +5 -0
  837. package/plugins/model-providers/nous/__init__.py +54 -0
  838. package/plugins/model-providers/nous/__pycache__/__init__.cpython-312.pyc +0 -0
  839. package/plugins/model-providers/nous/plugin.yaml +5 -0
  840. package/plugins/model-providers/novita/__init__.py +27 -0
  841. package/plugins/model-providers/novita/__pycache__/__init__.cpython-312.pyc +0 -0
  842. package/plugins/model-providers/novita/plugin.yaml +5 -0
  843. package/plugins/model-providers/nvidia/__init__.py +21 -0
  844. package/plugins/model-providers/nvidia/__pycache__/__init__.cpython-312.pyc +0 -0
  845. package/plugins/model-providers/nvidia/plugin.yaml +5 -0
  846. package/plugins/model-providers/ollama-cloud/__init__.py +14 -0
  847. package/plugins/model-providers/ollama-cloud/__pycache__/__init__.cpython-312.pyc +0 -0
  848. package/plugins/model-providers/ollama-cloud/plugin.yaml +5 -0
  849. package/plugins/model-providers/openai-codex/__init__.py +15 -0
  850. package/plugins/model-providers/openai-codex/__pycache__/__init__.cpython-312.pyc +0 -0
  851. package/plugins/model-providers/openai-codex/plugin.yaml +5 -0
  852. package/plugins/model-providers/opencode-zen/__init__.py +30 -0
  853. package/plugins/model-providers/opencode-zen/__pycache__/__init__.cpython-312.pyc +0 -0
  854. package/plugins/model-providers/opencode-zen/plugin.yaml +5 -0
  855. package/plugins/model-providers/openrouter/__init__.py +115 -0
  856. package/plugins/model-providers/openrouter/__pycache__/__init__.cpython-312.pyc +0 -0
  857. package/plugins/model-providers/openrouter/plugin.yaml +5 -0
  858. package/plugins/model-providers/qwen-oauth/__init__.py +82 -0
  859. package/plugins/model-providers/qwen-oauth/__pycache__/__init__.cpython-312.pyc +0 -0
  860. package/plugins/model-providers/qwen-oauth/plugin.yaml +5 -0
  861. package/plugins/model-providers/stepfun/__init__.py +14 -0
  862. package/plugins/model-providers/stepfun/__pycache__/__init__.cpython-312.pyc +0 -0
  863. package/plugins/model-providers/stepfun/plugin.yaml +5 -0
  864. package/plugins/model-providers/xai/__init__.py +15 -0
  865. package/plugins/model-providers/xai/__pycache__/__init__.cpython-312.pyc +0 -0
  866. package/plugins/model-providers/xai/plugin.yaml +5 -0
  867. package/plugins/model-providers/xiaomi/__init__.py +14 -0
  868. package/plugins/model-providers/xiaomi/__pycache__/__init__.cpython-312.pyc +0 -0
  869. package/plugins/model-providers/xiaomi/plugin.yaml +5 -0
  870. package/plugins/model-providers/zai/__init__.py +21 -0
  871. package/plugins/model-providers/zai/__pycache__/__init__.cpython-312.pyc +0 -0
  872. package/plugins/model-providers/zai/plugin.yaml +5 -0
  873. package/plugins/observability/langfuse/README.md +53 -0
  874. package/plugins/observability/langfuse/__init__.py +1004 -0
  875. package/plugins/observability/langfuse/plugin.yaml +14 -0
  876. package/plugins/platforms/google_chat/__init__.py +3 -0
  877. package/plugins/platforms/google_chat/__pycache__/__init__.cpython-312.pyc +0 -0
  878. package/plugins/platforms/google_chat/__pycache__/adapter.cpython-312.pyc +0 -0
  879. package/plugins/platforms/google_chat/adapter.py +3343 -0
  880. package/plugins/platforms/google_chat/oauth.py +639 -0
  881. package/plugins/platforms/google_chat/plugin.yaml +39 -0
  882. package/plugins/platforms/irc/__init__.py +3 -0
  883. package/plugins/platforms/irc/__pycache__/__init__.cpython-312.pyc +0 -0
  884. package/plugins/platforms/irc/__pycache__/adapter.cpython-312.pyc +0 -0
  885. package/plugins/platforms/irc/adapter.py +969 -0
  886. package/plugins/platforms/irc/plugin.yaml +54 -0
  887. package/plugins/platforms/line/__init__.py +3 -0
  888. package/plugins/platforms/line/__pycache__/__init__.cpython-312.pyc +0 -0
  889. package/plugins/platforms/line/__pycache__/adapter.cpython-312.pyc +0 -0
  890. package/plugins/platforms/line/adapter.py +1639 -0
  891. package/plugins/platforms/line/plugin.yaml +65 -0
  892. package/plugins/platforms/simplex/__init__.py +3 -0
  893. package/plugins/platforms/simplex/__pycache__/__init__.cpython-312.pyc +0 -0
  894. package/plugins/platforms/simplex/__pycache__/adapter.cpython-312.pyc +0 -0
  895. package/plugins/platforms/simplex/adapter.py +746 -0
  896. package/plugins/platforms/simplex/plugin.yaml +37 -0
  897. package/plugins/platforms/teams/__init__.py +3 -0
  898. package/plugins/platforms/teams/__pycache__/__init__.cpython-312.pyc +0 -0
  899. package/plugins/platforms/teams/__pycache__/adapter.cpython-312.pyc +0 -0
  900. package/plugins/platforms/teams/adapter.py +1188 -0
  901. package/plugins/platforms/teams/plugin.yaml +48 -0
  902. package/plugins/spotify/__init__.py +66 -0
  903. package/plugins/spotify/__pycache__/__init__.cpython-312.pyc +0 -0
  904. package/plugins/spotify/__pycache__/client.cpython-312.pyc +0 -0
  905. package/plugins/spotify/__pycache__/tools.cpython-312.pyc +0 -0
  906. package/plugins/spotify/client.py +435 -0
  907. package/plugins/spotify/plugin.yaml +13 -0
  908. package/plugins/spotify/tools.py +454 -0
  909. package/plugins/teams_pipeline/__init__.py +23 -0
  910. package/plugins/teams_pipeline/cli.py +463 -0
  911. package/plugins/teams_pipeline/meetings.py +333 -0
  912. package/plugins/teams_pipeline/models.py +350 -0
  913. package/plugins/teams_pipeline/pipeline.py +692 -0
  914. package/plugins/teams_pipeline/plugin.yaml +9 -0
  915. package/plugins/teams_pipeline/runtime.py +135 -0
  916. package/plugins/teams_pipeline/store.py +194 -0
  917. package/plugins/teams_pipeline/subscriptions.py +249 -0
  918. package/plugins/video_gen/fal/__init__.py +523 -0
  919. package/plugins/video_gen/fal/__pycache__/__init__.cpython-312.pyc +0 -0
  920. package/plugins/video_gen/fal/plugin.yaml +7 -0
  921. package/plugins/video_gen/xai/__init__.py +441 -0
  922. package/plugins/video_gen/xai/__pycache__/__init__.cpython-312.pyc +0 -0
  923. package/plugins/video_gen/xai/plugin.yaml +7 -0
  924. package/plugins/web/__init__.py +7 -0
  925. package/plugins/web/__pycache__/__init__.cpython-312.pyc +0 -0
  926. package/plugins/web/brave_free/__init__.py +14 -0
  927. package/plugins/web/brave_free/__pycache__/__init__.cpython-312.pyc +0 -0
  928. package/plugins/web/brave_free/__pycache__/provider.cpython-312.pyc +0 -0
  929. package/plugins/web/brave_free/plugin.yaml +7 -0
  930. package/plugins/web/brave_free/provider.py +137 -0
  931. package/plugins/web/ddgs/__init__.py +15 -0
  932. package/plugins/web/ddgs/__pycache__/__init__.cpython-312.pyc +0 -0
  933. package/plugins/web/ddgs/__pycache__/provider.cpython-312.pyc +0 -0
  934. package/plugins/web/ddgs/plugin.yaml +7 -0
  935. package/plugins/web/ddgs/provider.py +104 -0
  936. package/plugins/web/exa/__init__.py +15 -0
  937. package/plugins/web/exa/__pycache__/__init__.cpython-312.pyc +0 -0
  938. package/plugins/web/exa/__pycache__/provider.cpython-312.pyc +0 -0
  939. package/plugins/web/exa/plugin.yaml +7 -0
  940. package/plugins/web/exa/provider.py +212 -0
  941. package/plugins/web/firecrawl/__init__.py +28 -0
  942. package/plugins/web/firecrawl/__pycache__/__init__.cpython-312.pyc +0 -0
  943. package/plugins/web/firecrawl/__pycache__/provider.cpython-312.pyc +0 -0
  944. package/plugins/web/firecrawl/plugin.yaml +7 -0
  945. package/plugins/web/firecrawl/provider.py +773 -0
  946. package/plugins/web/parallel/__init__.py +16 -0
  947. package/plugins/web/parallel/__pycache__/__init__.cpython-312.pyc +0 -0
  948. package/plugins/web/parallel/__pycache__/provider.cpython-312.pyc +0 -0
  949. package/plugins/web/parallel/plugin.yaml +7 -0
  950. package/plugins/web/parallel/provider.py +291 -0
  951. package/plugins/web/searxng/__init__.py +15 -0
  952. package/plugins/web/searxng/__pycache__/__init__.cpython-312.pyc +0 -0
  953. package/plugins/web/searxng/__pycache__/provider.cpython-312.pyc +0 -0
  954. package/plugins/web/searxng/plugin.yaml +7 -0
  955. package/plugins/web/searxng/provider.py +140 -0
  956. package/plugins/web/tavily/__init__.py +15 -0
  957. package/plugins/web/tavily/__pycache__/__init__.cpython-312.pyc +0 -0
  958. package/plugins/web/tavily/__pycache__/provider.cpython-312.pyc +0 -0
  959. package/plugins/web/tavily/plugin.yaml +7 -0
  960. package/plugins/web/tavily/provider.py +285 -0
  961. package/providers/README.md +78 -0
  962. package/providers/__init__.py +192 -0
  963. package/providers/__pycache__/__init__.cpython-312.pyc +0 -0
  964. package/providers/__pycache__/base.cpython-312.pyc +0 -0
  965. package/providers/base.py +184 -0
  966. package/pyproject.toml +255 -0
  967. package/run_agent.py +16409 -0
  968. package/scripts/benchmark_browser_eval.py +138 -0
  969. package/scripts/build_model_catalog.py +95 -0
  970. package/scripts/build_skills_index.py +325 -0
  971. package/scripts/check-windows-footguns.py +624 -0
  972. package/scripts/contributor_audit.py +473 -0
  973. package/scripts/discord-voice-doctor.py +396 -0
  974. package/scripts/hermes-gateway +416 -0
  975. package/scripts/install.cmd +28 -0
  976. package/scripts/install.ps1 +1611 -0
  977. package/scripts/install.sh +2007 -0
  978. package/scripts/install_psutil_android.py +117 -0
  979. package/scripts/keystroke_diagnostic.py +81 -0
  980. package/scripts/kill_modal.sh +34 -0
  981. package/scripts/lib/node-bootstrap.sh +238 -0
  982. package/scripts/lint_diff.py +207 -0
  983. package/scripts/postinstall.js +150 -0
  984. package/scripts/profile-tui.py +626 -0
  985. package/scripts/release.py +1680 -0
  986. package/scripts/run_tests.sh +129 -0
  987. package/scripts/sample_and_compress.py +409 -0
  988. package/scripts/setup_open_webui.sh +349 -0
  989. package/scripts/whatsapp-bridge/allowlist.js +88 -0
  990. package/scripts/whatsapp-bridge/allowlist.test.mjs +80 -0
  991. package/scripts/whatsapp-bridge/bridge.js +729 -0
  992. package/scripts/whatsapp-bridge/package-lock.json +2141 -0
  993. package/scripts/whatsapp-bridge/package.json +19 -0
  994. package/skills/apple/DESCRIPTION.md +2 -0
  995. package/skills/apple/apple-notes/SKILL.md +90 -0
  996. package/skills/apple/apple-reminders/SKILL.md +98 -0
  997. package/skills/apple/findmy/SKILL.md +131 -0
  998. package/skills/apple/imessage/SKILL.md +102 -0
  999. package/skills/apple/macos-computer-use/SKILL.md +201 -0
  1000. package/skills/autonomous-ai-agents/DESCRIPTION.md +3 -0
  1001. package/skills/autonomous-ai-agents/claude-code/SKILL.md +745 -0
  1002. package/skills/autonomous-ai-agents/codex/SKILL.md +130 -0
  1003. package/skills/autonomous-ai-agents/hermes-agent/SKILL.md +1014 -0
  1004. package/skills/autonomous-ai-agents/opencode/SKILL.md +219 -0
  1005. package/skills/creative/DESCRIPTION.md +3 -0
  1006. package/skills/creative/architecture-diagram/SKILL.md +148 -0
  1007. package/skills/creative/architecture-diagram/templates/template.html +319 -0
  1008. package/skills/creative/ascii-art/SKILL.md +322 -0
  1009. package/skills/creative/ascii-video/README.md +290 -0
  1010. package/skills/creative/ascii-video/SKILL.md +241 -0
  1011. package/skills/creative/ascii-video/references/architecture.md +802 -0
  1012. package/skills/creative/ascii-video/references/composition.md +892 -0
  1013. package/skills/creative/ascii-video/references/effects.md +1865 -0
  1014. package/skills/creative/ascii-video/references/inputs.md +685 -0
  1015. package/skills/creative/ascii-video/references/optimization.md +688 -0
  1016. package/skills/creative/ascii-video/references/scenes.md +1011 -0
  1017. package/skills/creative/ascii-video/references/shaders.md +1385 -0
  1018. package/skills/creative/ascii-video/references/troubleshooting.md +367 -0
  1019. package/skills/creative/baoyu-comic/PORT_NOTES.md +77 -0
  1020. package/skills/creative/baoyu-comic/SKILL.md +247 -0
  1021. package/skills/creative/baoyu-comic/references/analysis-framework.md +176 -0
  1022. package/skills/creative/baoyu-comic/references/art-styles/chalk.md +101 -0
  1023. package/skills/creative/baoyu-comic/references/art-styles/ink-brush.md +97 -0
  1024. package/skills/creative/baoyu-comic/references/art-styles/ligne-claire.md +75 -0
  1025. package/skills/creative/baoyu-comic/references/art-styles/manga.md +93 -0
  1026. package/skills/creative/baoyu-comic/references/art-styles/minimalist.md +84 -0
  1027. package/skills/creative/baoyu-comic/references/art-styles/realistic.md +89 -0
  1028. package/skills/creative/baoyu-comic/references/auto-selection.md +71 -0
  1029. package/skills/creative/baoyu-comic/references/base-prompt.md +98 -0
  1030. package/skills/creative/baoyu-comic/references/character-template.md +180 -0
  1031. package/skills/creative/baoyu-comic/references/layouts/cinematic.md +23 -0
  1032. package/skills/creative/baoyu-comic/references/layouts/dense.md +23 -0
  1033. package/skills/creative/baoyu-comic/references/layouts/four-panel.md +40 -0
  1034. package/skills/creative/baoyu-comic/references/layouts/mixed.md +23 -0
  1035. package/skills/creative/baoyu-comic/references/layouts/splash.md +23 -0
  1036. package/skills/creative/baoyu-comic/references/layouts/standard.md +23 -0
  1037. package/skills/creative/baoyu-comic/references/layouts/webtoon.md +30 -0
  1038. package/skills/creative/baoyu-comic/references/ohmsha-guide.md +85 -0
  1039. package/skills/creative/baoyu-comic/references/partial-workflows.md +106 -0
  1040. package/skills/creative/baoyu-comic/references/presets/concept-story.md +121 -0
  1041. package/skills/creative/baoyu-comic/references/presets/four-panel.md +107 -0
  1042. package/skills/creative/baoyu-comic/references/presets/ohmsha.md +114 -0
  1043. package/skills/creative/baoyu-comic/references/presets/shoujo.md +116 -0
  1044. package/skills/creative/baoyu-comic/references/presets/wuxia.md +110 -0
  1045. package/skills/creative/baoyu-comic/references/storyboard-template.md +143 -0
  1046. package/skills/creative/baoyu-comic/references/tones/action.md +110 -0
  1047. package/skills/creative/baoyu-comic/references/tones/dramatic.md +95 -0
  1048. package/skills/creative/baoyu-comic/references/tones/energetic.md +105 -0
  1049. package/skills/creative/baoyu-comic/references/tones/neutral.md +63 -0
  1050. package/skills/creative/baoyu-comic/references/tones/romantic.md +100 -0
  1051. package/skills/creative/baoyu-comic/references/tones/vintage.md +104 -0
  1052. package/skills/creative/baoyu-comic/references/tones/warm.md +94 -0
  1053. package/skills/creative/baoyu-comic/references/workflow.md +401 -0
  1054. package/skills/creative/baoyu-infographic/PORT_NOTES.md +43 -0
  1055. package/skills/creative/baoyu-infographic/SKILL.md +237 -0
  1056. package/skills/creative/baoyu-infographic/references/analysis-framework.md +182 -0
  1057. package/skills/creative/baoyu-infographic/references/base-prompt.md +43 -0
  1058. package/skills/creative/baoyu-infographic/references/layouts/bento-grid.md +41 -0
  1059. package/skills/creative/baoyu-infographic/references/layouts/binary-comparison.md +48 -0
  1060. package/skills/creative/baoyu-infographic/references/layouts/bridge.md +41 -0
  1061. package/skills/creative/baoyu-infographic/references/layouts/circular-flow.md +41 -0
  1062. package/skills/creative/baoyu-infographic/references/layouts/comic-strip.md +41 -0
  1063. package/skills/creative/baoyu-infographic/references/layouts/comparison-matrix.md +41 -0
  1064. package/skills/creative/baoyu-infographic/references/layouts/dashboard.md +41 -0
  1065. package/skills/creative/baoyu-infographic/references/layouts/dense-modules.md +72 -0
  1066. package/skills/creative/baoyu-infographic/references/layouts/funnel.md +41 -0
  1067. package/skills/creative/baoyu-infographic/references/layouts/hierarchical-layers.md +48 -0
  1068. package/skills/creative/baoyu-infographic/references/layouts/hub-spoke.md +41 -0
  1069. package/skills/creative/baoyu-infographic/references/layouts/iceberg.md +41 -0
  1070. package/skills/creative/baoyu-infographic/references/layouts/isometric-map.md +41 -0
  1071. package/skills/creative/baoyu-infographic/references/layouts/jigsaw.md +41 -0
  1072. package/skills/creative/baoyu-infographic/references/layouts/linear-progression.md +48 -0
  1073. package/skills/creative/baoyu-infographic/references/layouts/periodic-table.md +41 -0
  1074. package/skills/creative/baoyu-infographic/references/layouts/story-mountain.md +41 -0
  1075. package/skills/creative/baoyu-infographic/references/layouts/structural-breakdown.md +48 -0
  1076. package/skills/creative/baoyu-infographic/references/layouts/tree-branching.md +41 -0
  1077. package/skills/creative/baoyu-infographic/references/layouts/venn-diagram.md +41 -0
  1078. package/skills/creative/baoyu-infographic/references/layouts/winding-roadmap.md +41 -0
  1079. package/skills/creative/baoyu-infographic/references/structured-content-template.md +244 -0
  1080. package/skills/creative/baoyu-infographic/references/styles/aged-academia.md +36 -0
  1081. package/skills/creative/baoyu-infographic/references/styles/bold-graphic.md +36 -0
  1082. package/skills/creative/baoyu-infographic/references/styles/chalkboard.md +61 -0
  1083. package/skills/creative/baoyu-infographic/references/styles/claymation.md +29 -0
  1084. package/skills/creative/baoyu-infographic/references/styles/corporate-memphis.md +29 -0
  1085. package/skills/creative/baoyu-infographic/references/styles/craft-handmade.md +44 -0
  1086. package/skills/creative/baoyu-infographic/references/styles/cyberpunk-neon.md +29 -0
  1087. package/skills/creative/baoyu-infographic/references/styles/hand-drawn-edu.md +63 -0
  1088. package/skills/creative/baoyu-infographic/references/styles/ikea-manual.md +29 -0
  1089. package/skills/creative/baoyu-infographic/references/styles/kawaii.md +29 -0
  1090. package/skills/creative/baoyu-infographic/references/styles/knolling.md +29 -0
  1091. package/skills/creative/baoyu-infographic/references/styles/lego-brick.md +29 -0
  1092. package/skills/creative/baoyu-infographic/references/styles/morandi-journal.md +60 -0
  1093. package/skills/creative/baoyu-infographic/references/styles/origami.md +29 -0
  1094. package/skills/creative/baoyu-infographic/references/styles/pixel-art.md +29 -0
  1095. package/skills/creative/baoyu-infographic/references/styles/pop-laboratory.md +48 -0
  1096. package/skills/creative/baoyu-infographic/references/styles/retro-pop-grid.md +47 -0
  1097. package/skills/creative/baoyu-infographic/references/styles/storybook-watercolor.md +29 -0
  1098. package/skills/creative/baoyu-infographic/references/styles/subway-map.md +29 -0
  1099. package/skills/creative/baoyu-infographic/references/styles/technical-schematic.md +36 -0
  1100. package/skills/creative/baoyu-infographic/references/styles/ui-wireframe.md +29 -0
  1101. package/skills/creative/claude-design/SKILL.md +591 -0
  1102. package/skills/creative/comfyui/SKILL.md +612 -0
  1103. package/skills/creative/comfyui/references/official-cli.md +255 -0
  1104. package/skills/creative/comfyui/references/rest-api.md +312 -0
  1105. package/skills/creative/comfyui/references/template-integrity.md +243 -0
  1106. package/skills/creative/comfyui/references/workflow-format.md +226 -0
  1107. package/skills/creative/comfyui/scripts/_common.py +835 -0
  1108. package/skills/creative/comfyui/scripts/auto_fix_deps.py +225 -0
  1109. package/skills/creative/comfyui/scripts/check_deps.py +437 -0
  1110. package/skills/creative/comfyui/scripts/comfyui_setup.sh +286 -0
  1111. package/skills/creative/comfyui/scripts/extract_schema.py +315 -0
  1112. package/skills/creative/comfyui/scripts/fetch_logs.py +158 -0
  1113. package/skills/creative/comfyui/scripts/hardware_check.py +497 -0
  1114. package/skills/creative/comfyui/scripts/health_check.py +223 -0
  1115. package/skills/creative/comfyui/scripts/run_batch.py +243 -0
  1116. package/skills/creative/comfyui/scripts/run_workflow.py +796 -0
  1117. package/skills/creative/comfyui/scripts/ws_monitor.py +267 -0
  1118. package/skills/creative/comfyui/tests/README.md +50 -0
  1119. package/skills/creative/comfyui/tests/conftest.py +64 -0
  1120. package/skills/creative/comfyui/tests/pytest.ini +5 -0
  1121. package/skills/creative/comfyui/tests/test_check_deps.py +68 -0
  1122. package/skills/creative/comfyui/tests/test_cloud_integration.py +95 -0
  1123. package/skills/creative/comfyui/tests/test_common.py +447 -0
  1124. package/skills/creative/comfyui/tests/test_extract_schema.py +185 -0
  1125. package/skills/creative/comfyui/tests/test_run_workflow.py +213 -0
  1126. package/skills/creative/comfyui/workflows/README.md +86 -0
  1127. package/skills/creative/comfyui/workflows/animatediff_video.json +64 -0
  1128. package/skills/creative/comfyui/workflows/flux_dev_txt2img.json +78 -0
  1129. package/skills/creative/comfyui/workflows/sd15_txt2img.json +49 -0
  1130. package/skills/creative/comfyui/workflows/sdxl_img2img.json +54 -0
  1131. package/skills/creative/comfyui/workflows/sdxl_inpaint.json +59 -0
  1132. package/skills/creative/comfyui/workflows/sdxl_txt2img.json +49 -0
  1133. package/skills/creative/comfyui/workflows/upscale_4x.json +27 -0
  1134. package/skills/creative/comfyui/workflows/wan_video_t2v.json +69 -0
  1135. package/skills/creative/creative-ideation/SKILL.md +152 -0
  1136. package/skills/creative/creative-ideation/references/full-prompt-library.md +110 -0
  1137. package/skills/creative/design-md/SKILL.md +199 -0
  1138. package/skills/creative/design-md/templates/starter.md +99 -0
  1139. package/skills/creative/excalidraw/SKILL.md +199 -0
  1140. package/skills/creative/excalidraw/references/colors.md +44 -0
  1141. package/skills/creative/excalidraw/references/dark-mode.md +68 -0
  1142. package/skills/creative/excalidraw/references/examples.md +141 -0
  1143. package/skills/creative/excalidraw/scripts/upload.py +133 -0
  1144. package/skills/creative/humanizer/LICENSE +21 -0
  1145. package/skills/creative/humanizer/SKILL.md +578 -0
  1146. package/skills/creative/manim-video/README.md +23 -0
  1147. package/skills/creative/manim-video/SKILL.md +269 -0
  1148. package/skills/creative/manim-video/references/animation-design-thinking.md +161 -0
  1149. package/skills/creative/manim-video/references/animations.md +282 -0
  1150. package/skills/creative/manim-video/references/camera-and-3d.md +135 -0
  1151. package/skills/creative/manim-video/references/decorations.md +202 -0
  1152. package/skills/creative/manim-video/references/equations.md +216 -0
  1153. package/skills/creative/manim-video/references/graphs-and-data.md +163 -0
  1154. package/skills/creative/manim-video/references/mobjects.md +333 -0
  1155. package/skills/creative/manim-video/references/paper-explainer.md +255 -0
  1156. package/skills/creative/manim-video/references/production-quality.md +190 -0
  1157. package/skills/creative/manim-video/references/rendering.md +185 -0
  1158. package/skills/creative/manim-video/references/scene-planning.md +118 -0
  1159. package/skills/creative/manim-video/references/troubleshooting.md +135 -0
  1160. package/skills/creative/manim-video/references/updaters-and-trackers.md +260 -0
  1161. package/skills/creative/manim-video/references/visual-design.md +124 -0
  1162. package/skills/creative/manim-video/scripts/setup.sh +14 -0
  1163. package/skills/creative/p5js/README.md +64 -0
  1164. package/skills/creative/p5js/SKILL.md +556 -0
  1165. package/skills/creative/p5js/references/animation.md +439 -0
  1166. package/skills/creative/p5js/references/color-systems.md +352 -0
  1167. package/skills/creative/p5js/references/core-api.md +410 -0
  1168. package/skills/creative/p5js/references/export-pipeline.md +566 -0
  1169. package/skills/creative/p5js/references/interaction.md +398 -0
  1170. package/skills/creative/p5js/references/shapes-and-geometry.md +300 -0
  1171. package/skills/creative/p5js/references/troubleshooting.md +532 -0
  1172. package/skills/creative/p5js/references/typography.md +302 -0
  1173. package/skills/creative/p5js/references/visual-effects.md +895 -0
  1174. package/skills/creative/p5js/references/webgl-and-3d.md +423 -0
  1175. package/skills/creative/p5js/scripts/export-frames.js +179 -0
  1176. package/skills/creative/p5js/scripts/render.sh +108 -0
  1177. package/skills/creative/p5js/scripts/serve.sh +28 -0
  1178. package/skills/creative/p5js/scripts/setup.sh +87 -0
  1179. package/skills/creative/p5js/templates/viewer.html +395 -0
  1180. package/skills/creative/pixel-art/ATTRIBUTION.md +54 -0
  1181. package/skills/creative/pixel-art/SKILL.md +218 -0
  1182. package/skills/creative/pixel-art/references/palettes.md +49 -0
  1183. package/skills/creative/pixel-art/scripts/__init__.py +0 -0
  1184. package/skills/creative/pixel-art/scripts/palettes.py +167 -0
  1185. package/skills/creative/pixel-art/scripts/pixel_art.py +162 -0
  1186. package/skills/creative/pixel-art/scripts/pixel_art_video.py +345 -0
  1187. package/skills/creative/popular-web-designs/SKILL.md +214 -0
  1188. package/skills/creative/popular-web-designs/templates/airbnb.md +259 -0
  1189. package/skills/creative/popular-web-designs/templates/airtable.md +102 -0
  1190. package/skills/creative/popular-web-designs/templates/apple.md +326 -0
  1191. package/skills/creative/popular-web-designs/templates/bmw.md +193 -0
  1192. package/skills/creative/popular-web-designs/templates/cal.md +272 -0
  1193. package/skills/creative/popular-web-designs/templates/claude.md +325 -0
  1194. package/skills/creative/popular-web-designs/templates/clay.md +317 -0
  1195. package/skills/creative/popular-web-designs/templates/clickhouse.md +294 -0
  1196. package/skills/creative/popular-web-designs/templates/cohere.md +279 -0
  1197. package/skills/creative/popular-web-designs/templates/coinbase.md +142 -0
  1198. package/skills/creative/popular-web-designs/templates/composio.md +320 -0
  1199. package/skills/creative/popular-web-designs/templates/cursor.md +322 -0
  1200. package/skills/creative/popular-web-designs/templates/elevenlabs.md +278 -0
  1201. package/skills/creative/popular-web-designs/templates/expo.md +294 -0
  1202. package/skills/creative/popular-web-designs/templates/figma.md +233 -0
  1203. package/skills/creative/popular-web-designs/templates/framer.md +259 -0
  1204. package/skills/creative/popular-web-designs/templates/hashicorp.md +291 -0
  1205. package/skills/creative/popular-web-designs/templates/ibm.md +345 -0
  1206. package/skills/creative/popular-web-designs/templates/intercom.md +159 -0
  1207. package/skills/creative/popular-web-designs/templates/kraken.md +138 -0
  1208. package/skills/creative/popular-web-designs/templates/linear.app.md +380 -0
  1209. package/skills/creative/popular-web-designs/templates/lovable.md +311 -0
  1210. package/skills/creative/popular-web-designs/templates/minimax.md +270 -0
  1211. package/skills/creative/popular-web-designs/templates/mintlify.md +339 -0
  1212. package/skills/creative/popular-web-designs/templates/miro.md +121 -0
  1213. package/skills/creative/popular-web-designs/templates/mistral.ai.md +274 -0
  1214. package/skills/creative/popular-web-designs/templates/mongodb.md +279 -0
  1215. package/skills/creative/popular-web-designs/templates/notion.md +322 -0
  1216. package/skills/creative/popular-web-designs/templates/nvidia.md +306 -0
  1217. package/skills/creative/popular-web-designs/templates/ollama.md +280 -0
  1218. package/skills/creative/popular-web-designs/templates/opencode.ai.md +294 -0
  1219. package/skills/creative/popular-web-designs/templates/pinterest.md +243 -0
  1220. package/skills/creative/popular-web-designs/templates/posthog.md +269 -0
  1221. package/skills/creative/popular-web-designs/templates/raycast.md +281 -0
  1222. package/skills/creative/popular-web-designs/templates/replicate.md +274 -0
  1223. package/skills/creative/popular-web-designs/templates/resend.md +316 -0
  1224. package/skills/creative/popular-web-designs/templates/revolut.md +198 -0
  1225. package/skills/creative/popular-web-designs/templates/runwayml.md +257 -0
  1226. package/skills/creative/popular-web-designs/templates/sanity.md +370 -0
  1227. package/skills/creative/popular-web-designs/templates/sentry.md +275 -0
  1228. package/skills/creative/popular-web-designs/templates/spacex.md +207 -0
  1229. package/skills/creative/popular-web-designs/templates/spotify.md +259 -0
  1230. package/skills/creative/popular-web-designs/templates/stripe.md +335 -0
  1231. package/skills/creative/popular-web-designs/templates/supabase.md +268 -0
  1232. package/skills/creative/popular-web-designs/templates/superhuman.md +265 -0
  1233. package/skills/creative/popular-web-designs/templates/together.ai.md +276 -0
  1234. package/skills/creative/popular-web-designs/templates/uber.md +308 -0
  1235. package/skills/creative/popular-web-designs/templates/vercel.md +323 -0
  1236. package/skills/creative/popular-web-designs/templates/voltagent.md +336 -0
  1237. package/skills/creative/popular-web-designs/templates/warp.md +266 -0
  1238. package/skills/creative/popular-web-designs/templates/webflow.md +105 -0
  1239. package/skills/creative/popular-web-designs/templates/wise.md +186 -0
  1240. package/skills/creative/popular-web-designs/templates/x.ai.md +270 -0
  1241. package/skills/creative/popular-web-designs/templates/zapier.md +341 -0
  1242. package/skills/creative/pretext/SKILL.md +220 -0
  1243. package/skills/creative/pretext/references/patterns.md +258 -0
  1244. package/skills/creative/pretext/templates/donut-orbit.html +1468 -0
  1245. package/skills/creative/pretext/templates/hello-orb-flow.html +95 -0
  1246. package/skills/creative/sketch/SKILL.md +218 -0
  1247. package/skills/creative/songwriting-and-ai-music/SKILL.md +287 -0
  1248. package/skills/creative/touchdesigner-mcp/SKILL.md +356 -0
  1249. package/skills/creative/touchdesigner-mcp/references/3d-scene.md +275 -0
  1250. package/skills/creative/touchdesigner-mcp/references/animation.md +221 -0
  1251. package/skills/creative/touchdesigner-mcp/references/audio-reactive.md +175 -0
  1252. package/skills/creative/touchdesigner-mcp/references/dat-scripting.md +352 -0
  1253. package/skills/creative/touchdesigner-mcp/references/external-data.md +322 -0
  1254. package/skills/creative/touchdesigner-mcp/references/geometry-comp.md +121 -0
  1255. package/skills/creative/touchdesigner-mcp/references/glsl.md +151 -0
  1256. package/skills/creative/touchdesigner-mcp/references/layout-compositor.md +131 -0
  1257. package/skills/creative/touchdesigner-mcp/references/mcp-tools.md +382 -0
  1258. package/skills/creative/touchdesigner-mcp/references/midi-osc.md +211 -0
  1259. package/skills/creative/touchdesigner-mcp/references/network-patterns.md +966 -0
  1260. package/skills/creative/touchdesigner-mcp/references/operator-tips.md +106 -0
  1261. package/skills/creative/touchdesigner-mcp/references/operators.md +239 -0
  1262. package/skills/creative/touchdesigner-mcp/references/panel-ui.md +281 -0
  1263. package/skills/creative/touchdesigner-mcp/references/particles.md +245 -0
  1264. package/skills/creative/touchdesigner-mcp/references/pitfalls.md +704 -0
  1265. package/skills/creative/touchdesigner-mcp/references/postfx.md +183 -0
  1266. package/skills/creative/touchdesigner-mcp/references/projection-mapping.md +211 -0
  1267. package/skills/creative/touchdesigner-mcp/references/python-api.md +463 -0
  1268. package/skills/creative/touchdesigner-mcp/references/replicator.md +198 -0
  1269. package/skills/creative/touchdesigner-mcp/references/troubleshooting.md +244 -0
  1270. package/skills/creative/touchdesigner-mcp/scripts/setup.sh +115 -0
  1271. package/skills/data-science/DESCRIPTION.md +3 -0
  1272. package/skills/data-science/jupyter-live-kernel/SKILL.md +167 -0
  1273. package/skills/devops/kanban-orchestrator/SKILL.md +189 -0
  1274. package/skills/devops/kanban-worker/SKILL.md +184 -0
  1275. package/skills/devops/webhook-subscriptions/SKILL.md +204 -0
  1276. package/skills/diagramming/DESCRIPTION.md +3 -0
  1277. package/skills/dogfood/SKILL.md +162 -0
  1278. package/skills/dogfood/references/issue-taxonomy.md +109 -0
  1279. package/skills/dogfood/templates/dogfood-report-template.md +86 -0
  1280. package/skills/domain/DESCRIPTION.md +24 -0
  1281. package/skills/email/DESCRIPTION.md +3 -0
  1282. package/skills/email/himalaya/SKILL.md +299 -0
  1283. package/skills/email/himalaya/references/configuration.md +227 -0
  1284. package/skills/email/himalaya/references/message-composition.md +199 -0
  1285. package/skills/gaming/DESCRIPTION.md +3 -0
  1286. package/skills/gaming/minecraft-modpack-server/SKILL.md +187 -0
  1287. package/skills/gaming/pokemon-player/SKILL.md +216 -0
  1288. package/skills/gifs/DESCRIPTION.md +3 -0
  1289. package/skills/github/DESCRIPTION.md +3 -0
  1290. package/skills/github/codebase-inspection/SKILL.md +116 -0
  1291. package/skills/github/github-auth/SKILL.md +247 -0
  1292. package/skills/github/github-auth/scripts/gh-env.sh +66 -0
  1293. package/skills/github/github-code-review/SKILL.md +481 -0
  1294. package/skills/github/github-code-review/references/review-output-template.md +74 -0
  1295. package/skills/github/github-issues/SKILL.md +370 -0
  1296. package/skills/github/github-issues/templates/bug-report.md +35 -0
  1297. package/skills/github/github-issues/templates/feature-request.md +31 -0
  1298. package/skills/github/github-pr-workflow/SKILL.md +367 -0
  1299. package/skills/github/github-pr-workflow/references/ci-troubleshooting.md +183 -0
  1300. package/skills/github/github-pr-workflow/references/conventional-commits.md +71 -0
  1301. package/skills/github/github-pr-workflow/templates/pr-body-bugfix.md +35 -0
  1302. package/skills/github/github-pr-workflow/templates/pr-body-feature.md +33 -0
  1303. package/skills/github/github-repo-management/SKILL.md +516 -0
  1304. package/skills/github/github-repo-management/references/github-api-cheatsheet.md +161 -0
  1305. package/skills/index-cache/anthropics_skills_skills_.json +1 -0
  1306. package/skills/index-cache/claude_marketplace_anthropics_skills.json +1 -0
  1307. package/skills/index-cache/lobehub_index.json +1 -0
  1308. package/skills/index-cache/openai_skills_skills_.json +1 -0
  1309. package/skills/inference-sh/DESCRIPTION.md +19 -0
  1310. package/skills/mcp/DESCRIPTION.md +3 -0
  1311. package/skills/mcp/native-mcp/SKILL.md +357 -0
  1312. package/skills/media/DESCRIPTION.md +3 -0
  1313. package/skills/media/gif-search/SKILL.md +91 -0
  1314. package/skills/media/heartmula/SKILL.md +171 -0
  1315. package/skills/media/songsee/SKILL.md +83 -0
  1316. package/skills/media/spotify/SKILL.md +135 -0
  1317. package/skills/media/youtube-content/SKILL.md +73 -0
  1318. package/skills/media/youtube-content/references/output-formats.md +56 -0
  1319. package/skills/media/youtube-content/scripts/fetch_transcript.py +124 -0
  1320. package/skills/mlops/DESCRIPTION.md +3 -0
  1321. package/skills/mlops/evaluation/DESCRIPTION.md +3 -0
  1322. package/skills/mlops/evaluation/lm-evaluation-harness/SKILL.md +498 -0
  1323. package/skills/mlops/evaluation/lm-evaluation-harness/references/api-evaluation.md +490 -0
  1324. package/skills/mlops/evaluation/lm-evaluation-harness/references/benchmark-guide.md +488 -0
  1325. package/skills/mlops/evaluation/lm-evaluation-harness/references/custom-tasks.md +602 -0
  1326. package/skills/mlops/evaluation/lm-evaluation-harness/references/distributed-eval.md +519 -0
  1327. package/skills/mlops/evaluation/weights-and-biases/SKILL.md +594 -0
  1328. package/skills/mlops/evaluation/weights-and-biases/references/artifacts.md +584 -0
  1329. package/skills/mlops/evaluation/weights-and-biases/references/integrations.md +700 -0
  1330. package/skills/mlops/evaluation/weights-and-biases/references/sweeps.md +847 -0
  1331. package/skills/mlops/huggingface-hub/SKILL.md +81 -0
  1332. package/skills/mlops/inference/DESCRIPTION.md +3 -0
  1333. package/skills/mlops/inference/llama-cpp/SKILL.md +249 -0
  1334. package/skills/mlops/inference/llama-cpp/references/advanced-usage.md +504 -0
  1335. package/skills/mlops/inference/llama-cpp/references/hub-discovery.md +168 -0
  1336. package/skills/mlops/inference/llama-cpp/references/optimization.md +89 -0
  1337. package/skills/mlops/inference/llama-cpp/references/quantization.md +243 -0
  1338. package/skills/mlops/inference/llama-cpp/references/server.md +150 -0
  1339. package/skills/mlops/inference/llama-cpp/references/troubleshooting.md +442 -0
  1340. package/skills/mlops/inference/obliteratus/SKILL.md +342 -0
  1341. package/skills/mlops/inference/obliteratus/references/analysis-modules.md +166 -0
  1342. package/skills/mlops/inference/obliteratus/references/methods-guide.md +141 -0
  1343. package/skills/mlops/inference/obliteratus/templates/abliteration-config.yaml +33 -0
  1344. package/skills/mlops/inference/obliteratus/templates/analysis-study.yaml +40 -0
  1345. package/skills/mlops/inference/obliteratus/templates/batch-abliteration.yaml +41 -0
  1346. package/skills/mlops/inference/vllm/SKILL.md +372 -0
  1347. package/skills/mlops/inference/vllm/references/optimization.md +226 -0
  1348. package/skills/mlops/inference/vllm/references/quantization.md +284 -0
  1349. package/skills/mlops/inference/vllm/references/server-deployment.md +255 -0
  1350. package/skills/mlops/inference/vllm/references/troubleshooting.md +447 -0
  1351. package/skills/mlops/models/DESCRIPTION.md +3 -0
  1352. package/skills/mlops/models/audiocraft/SKILL.md +568 -0
  1353. package/skills/mlops/models/audiocraft/references/advanced-usage.md +666 -0
  1354. package/skills/mlops/models/audiocraft/references/troubleshooting.md +504 -0
  1355. package/skills/mlops/models/segment-anything/SKILL.md +506 -0
  1356. package/skills/mlops/models/segment-anything/references/advanced-usage.md +589 -0
  1357. package/skills/mlops/models/segment-anything/references/troubleshooting.md +484 -0
  1358. package/skills/mlops/research/DESCRIPTION.md +3 -0
  1359. package/skills/mlops/research/dspy/SKILL.md +594 -0
  1360. package/skills/mlops/research/dspy/references/examples.md +663 -0
  1361. package/skills/mlops/research/dspy/references/modules.md +475 -0
  1362. package/skills/mlops/research/dspy/references/optimizers.md +566 -0
  1363. package/skills/mlops/training/DESCRIPTION.md +3 -0
  1364. package/skills/mlops/vector-databases/DESCRIPTION.md +3 -0
  1365. package/skills/note-taking/DESCRIPTION.md +3 -0
  1366. package/skills/note-taking/obsidian/SKILL.md +61 -0
  1367. package/skills/productivity/DESCRIPTION.md +3 -0
  1368. package/skills/productivity/airtable/SKILL.md +229 -0
  1369. package/skills/productivity/google-workspace/SKILL.md +335 -0
  1370. package/skills/productivity/google-workspace/references/gmail-search-syntax.md +63 -0
  1371. package/skills/productivity/google-workspace/scripts/_hermes_home.py +43 -0
  1372. package/skills/productivity/google-workspace/scripts/google_api.py +1221 -0
  1373. package/skills/productivity/google-workspace/scripts/gws_bridge.py +108 -0
  1374. package/skills/productivity/google-workspace/scripts/setup.py +454 -0
  1375. package/skills/productivity/linear/SKILL.md +380 -0
  1376. package/skills/productivity/linear/scripts/linear_api.py +445 -0
  1377. package/skills/productivity/maps/SKILL.md +195 -0
  1378. package/skills/productivity/maps/scripts/maps_client.py +1298 -0
  1379. package/skills/productivity/nano-pdf/SKILL.md +52 -0
  1380. package/skills/productivity/notion/SKILL.md +448 -0
  1381. package/skills/productivity/notion/references/block-types.md +112 -0
  1382. package/skills/productivity/ocr-and-documents/DESCRIPTION.md +3 -0
  1383. package/skills/productivity/ocr-and-documents/SKILL.md +172 -0
  1384. package/skills/productivity/ocr-and-documents/scripts/extract_marker.py +87 -0
  1385. package/skills/productivity/ocr-and-documents/scripts/extract_pymupdf.py +98 -0
  1386. package/skills/productivity/powerpoint/LICENSE.txt +30 -0
  1387. package/skills/productivity/powerpoint/SKILL.md +237 -0
  1388. package/skills/productivity/powerpoint/editing.md +205 -0
  1389. package/skills/productivity/powerpoint/pptxgenjs.md +420 -0
  1390. package/skills/productivity/powerpoint/scripts/__init__.py +0 -0
  1391. package/skills/productivity/powerpoint/scripts/add_slide.py +195 -0
  1392. package/skills/productivity/powerpoint/scripts/clean.py +286 -0
  1393. package/skills/productivity/powerpoint/scripts/office/helpers/__init__.py +0 -0
  1394. package/skills/productivity/powerpoint/scripts/office/helpers/merge_runs.py +199 -0
  1395. package/skills/productivity/powerpoint/scripts/office/helpers/simplify_redlines.py +197 -0
  1396. package/skills/productivity/powerpoint/scripts/office/pack.py +159 -0
  1397. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
  1398. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
  1399. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
  1400. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
  1401. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
  1402. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
  1403. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
  1404. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
  1405. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
  1406. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
  1407. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
  1408. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
  1409. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
  1410. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
  1411. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
  1412. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
  1413. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
  1414. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
  1415. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
  1416. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
  1417. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
  1418. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
  1419. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
  1420. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
  1421. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
  1422. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
  1423. package/skills/productivity/powerpoint/scripts/office/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
  1424. package/skills/productivity/powerpoint/scripts/office/schemas/ecma/fourth-edition/opc-contentTypes.xsd +42 -0
  1425. package/skills/productivity/powerpoint/scripts/office/schemas/ecma/fourth-edition/opc-coreProperties.xsd +50 -0
  1426. package/skills/productivity/powerpoint/scripts/office/schemas/ecma/fourth-edition/opc-digSig.xsd +49 -0
  1427. package/skills/productivity/powerpoint/scripts/office/schemas/ecma/fourth-edition/opc-relationships.xsd +33 -0
  1428. package/skills/productivity/powerpoint/scripts/office/schemas/mce/mc.xsd +75 -0
  1429. package/skills/productivity/powerpoint/scripts/office/schemas/microsoft/wml-2010.xsd +560 -0
  1430. package/skills/productivity/powerpoint/scripts/office/schemas/microsoft/wml-2012.xsd +67 -0
  1431. package/skills/productivity/powerpoint/scripts/office/schemas/microsoft/wml-2018.xsd +14 -0
  1432. package/skills/productivity/powerpoint/scripts/office/schemas/microsoft/wml-cex-2018.xsd +20 -0
  1433. package/skills/productivity/powerpoint/scripts/office/schemas/microsoft/wml-cid-2016.xsd +13 -0
  1434. package/skills/productivity/powerpoint/scripts/office/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
  1435. package/skills/productivity/powerpoint/scripts/office/schemas/microsoft/wml-symex-2015.xsd +8 -0
  1436. package/skills/productivity/teams-meeting-pipeline/SKILL.md +116 -0
  1437. package/skills/red-teaming/godmode/SKILL.md +404 -0
  1438. package/skills/red-teaming/godmode/references/jailbreak-templates.md +128 -0
  1439. package/skills/red-teaming/godmode/references/refusal-detection.md +142 -0
  1440. package/skills/red-teaming/godmode/scripts/auto_jailbreak.py +769 -0
  1441. package/skills/red-teaming/godmode/scripts/godmode_race.py +530 -0
  1442. package/skills/red-teaming/godmode/scripts/load_godmode.py +45 -0
  1443. package/skills/red-teaming/godmode/scripts/parseltongue.py +550 -0
  1444. package/skills/red-teaming/godmode/templates/prefill-subtle.json +10 -0
  1445. package/skills/red-teaming/godmode/templates/prefill.json +18 -0
  1446. package/skills/research/DESCRIPTION.md +3 -0
  1447. package/skills/research/arxiv/SKILL.md +282 -0
  1448. package/skills/research/arxiv/scripts/search_arxiv.py +114 -0
  1449. package/skills/research/blogwatcher/SKILL.md +137 -0
  1450. package/skills/research/llm-wiki/SKILL.md +507 -0
  1451. package/skills/research/polymarket/SKILL.md +77 -0
  1452. package/skills/research/polymarket/references/api-endpoints.md +220 -0
  1453. package/skills/research/polymarket/scripts/polymarket.py +284 -0
  1454. package/skills/research/research-paper-writing/SKILL.md +2377 -0
  1455. package/skills/research/research-paper-writing/references/autoreason-methodology.md +394 -0
  1456. package/skills/research/research-paper-writing/references/checklists.md +434 -0
  1457. package/skills/research/research-paper-writing/references/citation-workflow.md +564 -0
  1458. package/skills/research/research-paper-writing/references/experiment-patterns.md +728 -0
  1459. package/skills/research/research-paper-writing/references/human-evaluation.md +476 -0
  1460. package/skills/research/research-paper-writing/references/paper-types.md +481 -0
  1461. package/skills/research/research-paper-writing/references/reviewer-guidelines.md +433 -0
  1462. package/skills/research/research-paper-writing/references/sources.md +191 -0
  1463. package/skills/research/research-paper-writing/references/writing-guide.md +474 -0
  1464. package/skills/research/research-paper-writing/templates/README.md +251 -0
  1465. package/skills/research/research-paper-writing/templates/aaai2026/README.md +534 -0
  1466. package/skills/research/research-paper-writing/templates/aaai2026/aaai2026-unified-supp.tex +144 -0
  1467. package/skills/research/research-paper-writing/templates/aaai2026/aaai2026-unified-template.tex +952 -0
  1468. package/skills/research/research-paper-writing/templates/aaai2026/aaai2026.bib +111 -0
  1469. package/skills/research/research-paper-writing/templates/aaai2026/aaai2026.bst +1493 -0
  1470. package/skills/research/research-paper-writing/templates/aaai2026/aaai2026.sty +315 -0
  1471. package/skills/research/research-paper-writing/templates/acl/README.md +50 -0
  1472. package/skills/research/research-paper-writing/templates/acl/acl.sty +312 -0
  1473. package/skills/research/research-paper-writing/templates/acl/acl_latex.tex +377 -0
  1474. package/skills/research/research-paper-writing/templates/acl/acl_lualatex.tex +101 -0
  1475. package/skills/research/research-paper-writing/templates/acl/acl_natbib.bst +1940 -0
  1476. package/skills/research/research-paper-writing/templates/acl/anthology.bib.txt +26 -0
  1477. package/skills/research/research-paper-writing/templates/acl/custom.bib +70 -0
  1478. package/skills/research/research-paper-writing/templates/acl/formatting.md +326 -0
  1479. package/skills/research/research-paper-writing/templates/colm2025/README.md +3 -0
  1480. package/skills/research/research-paper-writing/templates/colm2025/colm2025_conference.bib +11 -0
  1481. package/skills/research/research-paper-writing/templates/colm2025/colm2025_conference.bst +1440 -0
  1482. package/skills/research/research-paper-writing/templates/colm2025/colm2025_conference.pdf +0 -0
  1483. package/skills/research/research-paper-writing/templates/colm2025/colm2025_conference.sty +218 -0
  1484. package/skills/research/research-paper-writing/templates/colm2025/colm2025_conference.tex +305 -0
  1485. package/skills/research/research-paper-writing/templates/colm2025/fancyhdr.sty +485 -0
  1486. package/skills/research/research-paper-writing/templates/colm2025/math_commands.tex +508 -0
  1487. package/skills/research/research-paper-writing/templates/colm2025/natbib.sty +1246 -0
  1488. package/skills/research/research-paper-writing/templates/iclr2026/fancyhdr.sty +485 -0
  1489. package/skills/research/research-paper-writing/templates/iclr2026/iclr2026_conference.bib +24 -0
  1490. package/skills/research/research-paper-writing/templates/iclr2026/iclr2026_conference.bst +1440 -0
  1491. package/skills/research/research-paper-writing/templates/iclr2026/iclr2026_conference.pdf +0 -0
  1492. package/skills/research/research-paper-writing/templates/iclr2026/iclr2026_conference.sty +246 -0
  1493. package/skills/research/research-paper-writing/templates/iclr2026/iclr2026_conference.tex +414 -0
  1494. package/skills/research/research-paper-writing/templates/iclr2026/math_commands.tex +508 -0
  1495. package/skills/research/research-paper-writing/templates/iclr2026/natbib.sty +1246 -0
  1496. package/skills/research/research-paper-writing/templates/icml2026/algorithm.sty +79 -0
  1497. package/skills/research/research-paper-writing/templates/icml2026/algorithmic.sty +201 -0
  1498. package/skills/research/research-paper-writing/templates/icml2026/example_paper.bib +75 -0
  1499. package/skills/research/research-paper-writing/templates/icml2026/example_paper.pdf +0 -0
  1500. package/skills/research/research-paper-writing/templates/icml2026/example_paper.tex +662 -0
  1501. package/skills/research/research-paper-writing/templates/icml2026/fancyhdr.sty +864 -0
  1502. package/skills/research/research-paper-writing/templates/icml2026/icml2026.bst +1443 -0
  1503. package/skills/research/research-paper-writing/templates/icml2026/icml2026.sty +767 -0
  1504. package/skills/research/research-paper-writing/templates/icml2026/icml_numpapers.pdf +0 -0
  1505. package/skills/research/research-paper-writing/templates/neurips2025/Makefile +36 -0
  1506. package/skills/research/research-paper-writing/templates/neurips2025/extra_pkgs.tex +53 -0
  1507. package/skills/research/research-paper-writing/templates/neurips2025/main.tex +38 -0
  1508. package/skills/research/research-paper-writing/templates/neurips2025/neurips.sty +382 -0
  1509. package/skills/smart-home/DESCRIPTION.md +3 -0
  1510. package/skills/smart-home/openhue/SKILL.md +109 -0
  1511. package/skills/social-media/DESCRIPTION.md +3 -0
  1512. package/skills/social-media/xurl/SKILL.md +414 -0
  1513. package/skills/software-development/debugging-hermes-tui-commands/SKILL.md +152 -0
  1514. package/skills/software-development/hermes-agent-skill-authoring/SKILL.md +165 -0
  1515. package/skills/software-development/node-inspect-debugger/SKILL.md +319 -0
  1516. package/skills/software-development/plan/SKILL.md +58 -0
  1517. package/skills/software-development/python-debugpy/SKILL.md +375 -0
  1518. package/skills/software-development/requesting-code-review/SKILL.md +280 -0
  1519. package/skills/software-development/spike/SKILL.md +197 -0
  1520. package/skills/software-development/subagent-driven-development/SKILL.md +352 -0
  1521. package/skills/software-development/subagent-driven-development/references/context-budget-discipline.md +53 -0
  1522. package/skills/software-development/subagent-driven-development/references/gates-taxonomy.md +93 -0
  1523. package/skills/software-development/systematic-debugging/SKILL.md +367 -0
  1524. package/skills/software-development/test-driven-development/SKILL.md +343 -0
  1525. package/skills/software-development/writing-plans/SKILL.md +297 -0
  1526. package/skills/yuanbao/SKILL.md +108 -0
  1527. package/tools/__init__.py +25 -0
  1528. package/tools/__pycache__/__init__.cpython-312.pyc +0 -0
  1529. package/tools/__pycache__/approval.cpython-312.pyc +0 -0
  1530. package/tools/__pycache__/binary_extensions.cpython-312.pyc +0 -0
  1531. package/tools/__pycache__/browser_camofox.cpython-312.pyc +0 -0
  1532. package/tools/__pycache__/browser_camofox_state.cpython-312.pyc +0 -0
  1533. package/tools/__pycache__/browser_cdp_tool.cpython-312.pyc +0 -0
  1534. package/tools/__pycache__/browser_dialog_tool.cpython-312.pyc +0 -0
  1535. package/tools/__pycache__/browser_supervisor.cpython-312.pyc +0 -0
  1536. package/tools/__pycache__/browser_tool.cpython-312.pyc +0 -0
  1537. package/tools/__pycache__/budget_config.cpython-312.pyc +0 -0
  1538. package/tools/__pycache__/checkpoint_manager.cpython-312.pyc +0 -0
  1539. package/tools/__pycache__/clarify_gateway.cpython-312.pyc +0 -0
  1540. package/tools/__pycache__/clarify_tool.cpython-312.pyc +0 -0
  1541. package/tools/__pycache__/code_execution_tool.cpython-312.pyc +0 -0
  1542. package/tools/__pycache__/computer_use_tool.cpython-312.pyc +0 -0
  1543. package/tools/__pycache__/cronjob_tools.cpython-312.pyc +0 -0
  1544. package/tools/__pycache__/debug_helpers.cpython-312.pyc +0 -0
  1545. package/tools/__pycache__/delegate_tool.cpython-312.pyc +0 -0
  1546. package/tools/__pycache__/discord_tool.cpython-312.pyc +0 -0
  1547. package/tools/__pycache__/feishu_doc_tool.cpython-312.pyc +0 -0
  1548. package/tools/__pycache__/feishu_drive_tool.cpython-312.pyc +0 -0
  1549. package/tools/__pycache__/file_operations.cpython-312.pyc +0 -0
  1550. package/tools/__pycache__/file_state.cpython-312.pyc +0 -0
  1551. package/tools/__pycache__/file_tools.cpython-312.pyc +0 -0
  1552. package/tools/__pycache__/homeassistant_tool.cpython-312.pyc +0 -0
  1553. package/tools/__pycache__/image_generation_tool.cpython-312.pyc +0 -0
  1554. package/tools/__pycache__/interrupt.cpython-312.pyc +0 -0
  1555. package/tools/__pycache__/kanban_tools.cpython-312.pyc +0 -0
  1556. package/tools/__pycache__/lazy_deps.cpython-312.pyc +0 -0
  1557. package/tools/__pycache__/managed_tool_gateway.cpython-312.pyc +0 -0
  1558. package/tools/__pycache__/mcp_tool.cpython-312.pyc +0 -0
  1559. package/tools/__pycache__/memory_tool.cpython-312.pyc +0 -0
  1560. package/tools/__pycache__/mixture_of_agents_tool.cpython-312.pyc +0 -0
  1561. package/tools/__pycache__/openrouter_client.cpython-312.pyc +0 -0
  1562. package/tools/__pycache__/process_registry.cpython-312.pyc +0 -0
  1563. package/tools/__pycache__/registry.cpython-312.pyc +0 -0
  1564. package/tools/__pycache__/schema_sanitizer.cpython-312.pyc +0 -0
  1565. package/tools/__pycache__/send_message_tool.cpython-312.pyc +0 -0
  1566. package/tools/__pycache__/session_search_tool.cpython-312.pyc +0 -0
  1567. package/tools/__pycache__/skill_manager_tool.cpython-312.pyc +0 -0
  1568. package/tools/__pycache__/skill_provenance.cpython-312.pyc +0 -0
  1569. package/tools/__pycache__/skill_usage.cpython-312.pyc +0 -0
  1570. package/tools/__pycache__/skills_guard.cpython-312.pyc +0 -0
  1571. package/tools/__pycache__/skills_sync.cpython-312.pyc +0 -0
  1572. package/tools/__pycache__/skills_tool.cpython-312.pyc +0 -0
  1573. package/tools/__pycache__/slash_confirm.cpython-312.pyc +0 -0
  1574. package/tools/__pycache__/terminal_tool.cpython-312.pyc +0 -0
  1575. package/tools/__pycache__/tirith_security.cpython-312.pyc +0 -0
  1576. package/tools/__pycache__/todo_tool.cpython-312.pyc +0 -0
  1577. package/tools/__pycache__/tool_backend_helpers.cpython-312.pyc +0 -0
  1578. package/tools/__pycache__/tool_result_storage.cpython-312.pyc +0 -0
  1579. package/tools/__pycache__/tts_tool.cpython-312.pyc +0 -0
  1580. package/tools/__pycache__/url_safety.cpython-312.pyc +0 -0
  1581. package/tools/__pycache__/video_generation_tool.cpython-312.pyc +0 -0
  1582. package/tools/__pycache__/vision_tools.cpython-312.pyc +0 -0
  1583. package/tools/__pycache__/voice_mode.cpython-312.pyc +0 -0
  1584. package/tools/__pycache__/web_tools.cpython-312.pyc +0 -0
  1585. package/tools/__pycache__/website_policy.cpython-312.pyc +0 -0
  1586. package/tools/__pycache__/x_search_tool.cpython-312.pyc +0 -0
  1587. package/tools/__pycache__/xai_http.cpython-312.pyc +0 -0
  1588. package/tools/__pycache__/yuanbao_tools.cpython-312.pyc +0 -0
  1589. package/tools/ansi_strip.py +44 -0
  1590. package/tools/approval.py +1392 -0
  1591. package/tools/binary_extensions.py +42 -0
  1592. package/tools/browser_camofox.py +700 -0
  1593. package/tools/browser_camofox_state.py +48 -0
  1594. package/tools/browser_cdp_tool.py +569 -0
  1595. package/tools/browser_dialog_tool.py +148 -0
  1596. package/tools/browser_providers/__init__.py +10 -0
  1597. package/tools/browser_providers/__pycache__/__init__.cpython-312.pyc +0 -0
  1598. package/tools/browser_providers/__pycache__/base.cpython-312.pyc +0 -0
  1599. package/tools/browser_providers/__pycache__/browser_use.cpython-312.pyc +0 -0
  1600. package/tools/browser_providers/__pycache__/browserbase.cpython-312.pyc +0 -0
  1601. package/tools/browser_providers/__pycache__/firecrawl.cpython-312.pyc +0 -0
  1602. package/tools/browser_providers/base.py +59 -0
  1603. package/tools/browser_providers/browser_use.py +225 -0
  1604. package/tools/browser_providers/browserbase.py +222 -0
  1605. package/tools/browser_providers/firecrawl.py +112 -0
  1606. package/tools/browser_supervisor.py +1457 -0
  1607. package/tools/browser_tool.py +3676 -0
  1608. package/tools/budget_config.py +51 -0
  1609. package/tools/checkpoint_manager.py +1639 -0
  1610. package/tools/clarify_gateway.py +278 -0
  1611. package/tools/clarify_tool.py +141 -0
  1612. package/tools/code_execution_tool.py +1782 -0
  1613. package/tools/computer_use/__init__.py +43 -0
  1614. package/tools/computer_use/__pycache__/__init__.cpython-312.pyc +0 -0
  1615. package/tools/computer_use/__pycache__/backend.cpython-312.pyc +0 -0
  1616. package/tools/computer_use/__pycache__/schema.cpython-312.pyc +0 -0
  1617. package/tools/computer_use/__pycache__/tool.cpython-312.pyc +0 -0
  1618. package/tools/computer_use/backend.py +150 -0
  1619. package/tools/computer_use/cua_backend.py +682 -0
  1620. package/tools/computer_use/schema.py +191 -0
  1621. package/tools/computer_use/tool.py +521 -0
  1622. package/tools/computer_use_tool.py +39 -0
  1623. package/tools/credential_files.py +437 -0
  1624. package/tools/cronjob_tools.py +719 -0
  1625. package/tools/debug_helpers.py +106 -0
  1626. package/tools/delegate_tool.py +2797 -0
  1627. package/tools/discord_tool.py +959 -0
  1628. package/tools/env_passthrough.py +145 -0
  1629. package/tools/environments/__init__.py +14 -0
  1630. package/tools/environments/__pycache__/__init__.cpython-312.pyc +0 -0
  1631. package/tools/environments/__pycache__/base.cpython-312.pyc +0 -0
  1632. package/tools/environments/__pycache__/docker.cpython-312.pyc +0 -0
  1633. package/tools/environments/__pycache__/file_sync.cpython-312.pyc +0 -0
  1634. package/tools/environments/__pycache__/local.cpython-312.pyc +0 -0
  1635. package/tools/environments/__pycache__/managed_modal.cpython-312.pyc +0 -0
  1636. package/tools/environments/__pycache__/modal.cpython-312.pyc +0 -0
  1637. package/tools/environments/__pycache__/modal_utils.cpython-312.pyc +0 -0
  1638. package/tools/environments/__pycache__/singularity.cpython-312.pyc +0 -0
  1639. package/tools/environments/__pycache__/ssh.cpython-312.pyc +0 -0
  1640. package/tools/environments/base.py +844 -0
  1641. package/tools/environments/daytona.py +270 -0
  1642. package/tools/environments/docker.py +656 -0
  1643. package/tools/environments/file_sync.py +400 -0
  1644. package/tools/environments/local.py +658 -0
  1645. package/tools/environments/managed_modal.py +282 -0
  1646. package/tools/environments/modal.py +479 -0
  1647. package/tools/environments/modal_utils.py +199 -0
  1648. package/tools/environments/singularity.py +263 -0
  1649. package/tools/environments/ssh.py +295 -0
  1650. package/tools/environments/vercel_sandbox.py +655 -0
  1651. package/tools/feishu_doc_tool.py +138 -0
  1652. package/tools/feishu_drive_tool.py +431 -0
  1653. package/tools/file_operations.py +1825 -0
  1654. package/tools/file_state.py +332 -0
  1655. package/tools/file_tools.py +1172 -0
  1656. package/tools/fuzzy_match.py +703 -0
  1657. package/tools/homeassistant_tool.py +513 -0
  1658. package/tools/image_generation_tool.py +1098 -0
  1659. package/tools/interrupt.py +98 -0
  1660. package/tools/kanban_tools.py +1139 -0
  1661. package/tools/lazy_deps.py +608 -0
  1662. package/tools/managed_tool_gateway.py +168 -0
  1663. package/tools/mcp_oauth.py +633 -0
  1664. package/tools/mcp_oauth_manager.py +607 -0
  1665. package/tools/mcp_tool.py +3483 -0
  1666. package/tools/memory_tool.py +584 -0
  1667. package/tools/microsoft_graph_auth.py +245 -0
  1668. package/tools/microsoft_graph_client.py +408 -0
  1669. package/tools/mixture_of_agents_tool.py +542 -0
  1670. package/tools/neutts_samples/jo.txt +1 -0
  1671. package/tools/neutts_samples/jo.wav +0 -0
  1672. package/tools/neutts_synth.py +104 -0
  1673. package/tools/openrouter_client.py +33 -0
  1674. package/tools/osv_check.py +155 -0
  1675. package/tools/patch_parser.py +592 -0
  1676. package/tools/path_security.py +43 -0
  1677. package/tools/process_registry.py +1534 -0
  1678. package/tools/registry.py +589 -0
  1679. package/tools/schema_sanitizer.py +370 -0
  1680. package/tools/send_message_tool.py +1900 -0
  1681. package/tools/session_search_tool.py +613 -0
  1682. package/tools/skill_manager_tool.py +932 -0
  1683. package/tools/skill_provenance.py +78 -0
  1684. package/tools/skill_usage.py +610 -0
  1685. package/tools/skills_guard.py +932 -0
  1686. package/tools/skills_hub.py +3263 -0
  1687. package/tools/skills_sync.py +432 -0
  1688. package/tools/skills_tool.py +1569 -0
  1689. package/tools/slash_confirm.py +167 -0
  1690. package/tools/terminal_tool.py +2376 -0
  1691. package/tools/tirith_security.py +775 -0
  1692. package/tools/todo_tool.py +277 -0
  1693. package/tools/tool_backend_helpers.py +144 -0
  1694. package/tools/tool_output_limits.py +92 -0
  1695. package/tools/tool_result_storage.py +232 -0
  1696. package/tools/transcription_tools.py +936 -0
  1697. package/tools/tts_tool.py +2285 -0
  1698. package/tools/url_safety.py +330 -0
  1699. package/tools/video_generation_tool.py +561 -0
  1700. package/tools/vision_tools.py +1422 -0
  1701. package/tools/voice_mode.py +1019 -0
  1702. package/tools/web_tools.py +1551 -0
  1703. package/tools/website_policy.py +283 -0
  1704. package/tools/x_search_tool.py +424 -0
  1705. package/tools/xai_http.py +83 -0
  1706. package/tools/yuanbao_tools.py +736 -0
  1707. package/toolset_distributions.py +364 -0
  1708. package/toolsets.py +866 -0
  1709. package/trajectory_compressor.py +1509 -0
  1710. package/tui_gateway/__init__.py +0 -0
  1711. package/tui_gateway/entry.py +251 -0
  1712. package/tui_gateway/event_publisher.py +126 -0
  1713. package/tui_gateway/render.py +49 -0
  1714. package/tui_gateway/server.py +6623 -0
  1715. package/tui_gateway/slash_worker.py +76 -0
  1716. package/tui_gateway/transport.py +219 -0
  1717. package/tui_gateway/ws.py +178 -0
  1718. package/utils.py +361 -0
@@ -0,0 +1,3483 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ MCP (Model Context Protocol) Client Support
4
+
5
+ Connects to external MCP servers via stdio, HTTP/StreamableHTTP, or SSE
6
+ transport, discovers their tools, and registers them into the hermes-agent
7
+ tool registry so the agent can call them like any built-in tool.
8
+
9
+ Configuration is read from ~/.hermes/config.yaml under the ``mcp_servers`` key.
10
+ The ``mcp`` Python package is optional -- if not installed, this module is a
11
+ no-op and logs a debug message.
12
+
13
+ Example config::
14
+
15
+ mcp_servers:
16
+ filesystem:
17
+ command: "npx"
18
+ args: ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"]
19
+ env: {}
20
+ timeout: 120 # per-tool-call timeout in seconds (default: 120)
21
+ connect_timeout: 60 # initial connection timeout (default: 60)
22
+ github:
23
+ command: "npx"
24
+ args: ["-y", "@modelcontextprotocol/server-github"]
25
+ env:
26
+ GITHUB_PERSONAL_ACCESS_TOKEN: "ghp_..."
27
+ supports_parallel_tool_calls: true # tools from this server may run concurrently
28
+ remote_api:
29
+ url: "https://my-mcp-server.example.com/mcp"
30
+ headers:
31
+ Authorization: "Bearer sk-..."
32
+ timeout: 180
33
+ searxng:
34
+ url: "http://localhost:8000/sse"
35
+ transport: sse # use SSE transport instead of Streamable HTTP
36
+ timeout: 180
37
+ connect_timeout: 10
38
+ command: "npx"
39
+ args: ["-y", "analysis-server"]
40
+ sampling: # server-initiated LLM requests
41
+ enabled: true # default: true
42
+ model: "gemini-3-flash" # override model (optional)
43
+ max_tokens_cap: 4096 # max tokens per request
44
+ timeout: 30 # LLM call timeout (seconds)
45
+ max_rpm: 10 # max requests per minute
46
+ allowed_models: [] # model whitelist (empty = all)
47
+ max_tool_rounds: 5 # tool loop limit (0 = disable)
48
+ log_level: "info" # audit verbosity
49
+
50
+ Features:
51
+ - Stdio transport (command + args) and HTTP/StreamableHTTP transport (url)
52
+ - SSE transport (transport: sse) for MCP servers using the SSE protocol
53
+ - Automatic reconnection with exponential backoff (up to 5 retries)
54
+ - Environment variable filtering for stdio subprocesses (security)
55
+ - Credential stripping in error messages returned to the LLM
56
+ - Configurable per-server timeouts for tool calls and connections
57
+ - Thread-safe architecture with dedicated background event loop
58
+ - Sampling support: MCP servers can request LLM completions via
59
+ sampling/createMessage (text and tool-use responses)
60
+ - Parallel tool call opt-in: per-server ``supports_parallel_tool_calls``
61
+ flag allows concurrent execution of tools from the same server
62
+
63
+ Architecture:
64
+ A dedicated background event loop (_mcp_loop) runs in a daemon thread.
65
+ Each MCP server runs as a long-lived asyncio Task on this loop, keeping
66
+ its transport context alive. Tool call coroutines are scheduled onto the
67
+ loop via ``run_coroutine_threadsafe()``.
68
+
69
+ On shutdown, each server Task is signalled to exit its ``async with``
70
+ block, ensuring the anyio cancel-scope cleanup happens in the *same*
71
+ Task that opened the connection (required by anyio).
72
+
73
+ Thread safety:
74
+ _servers and _mcp_loop/_mcp_thread are accessed from both the MCP
75
+ background thread and caller threads. All mutations are protected by
76
+ _lock so the code is safe regardless of GIL presence (e.g. Python 3.13+
77
+ free-threading).
78
+ """
79
+
80
+ import asyncio
81
+ import concurrent.futures
82
+ import inspect
83
+ import json
84
+ import logging
85
+ import math
86
+ import os
87
+ import re
88
+ import shutil
89
+ import sys
90
+ import threading
91
+ import time
92
+ from datetime import datetime
93
+ from typing import Any, Dict, List, Optional
94
+
95
+ logger = logging.getLogger(__name__)
96
+
97
+
98
+ # ---------------------------------------------------------------------------
99
+ # Stdio subprocess stderr redirection
100
+ # ---------------------------------------------------------------------------
101
+ #
102
+ # The MCP SDK's ``stdio_client(server, errlog=sys.stderr)`` defaults the
103
+ # subprocess stderr stream to the parent process's real stderr, i.e. the
104
+ # user's TTY. That means any MCP server we spawn at startup (FastMCP
105
+ # banners, slack-mcp-server JSON startup logs, etc.) writes directly onto
106
+ # the terminal while prompt_toolkit / Rich is rendering the TUI — which
107
+ # corrupts the display and can hang the session.
108
+ #
109
+ # Instead we redirect every stdio MCP subprocess's stderr into a shared
110
+ # per-profile log file (~/.hermes/logs/mcp-stderr.log), tagged with the
111
+ # server name so individual servers remain debuggable.
112
+ #
113
+ # Fallback is os.devnull if opening the log file fails for any reason.
114
+
115
+ _mcp_stderr_log_fh: Optional[Any] = None
116
+ _mcp_stderr_log_lock = threading.Lock()
117
+
118
+
119
+ def _get_mcp_stderr_log() -> Any:
120
+ """Return a shared append-mode file handle for MCP subprocess stderr.
121
+
122
+ Opened once per process and reused for every stdio server. Must have a
123
+ real OS-level file descriptor (``fileno()``) because asyncio's subprocess
124
+ machinery wires the child's stderr directly to that fd. Falls back to
125
+ ``/dev/null`` if opening the log file fails.
126
+ """
127
+ global _mcp_stderr_log_fh
128
+ with _mcp_stderr_log_lock:
129
+ if _mcp_stderr_log_fh is not None:
130
+ return _mcp_stderr_log_fh
131
+ try:
132
+ from calvyn_constants import get_hermes_home
133
+ log_dir = get_hermes_home() / "logs"
134
+ log_dir.mkdir(parents=True, exist_ok=True)
135
+ log_path = log_dir / "mcp-stderr.log"
136
+ # Line-buffered so server output lands on disk promptly; errors=
137
+ # "replace" tolerates garbled binary output from misbehaving
138
+ # servers.
139
+ fh = open(log_path, "a", encoding="utf-8", errors="replace", buffering=1)
140
+ # Sanity-check: confirm a real fd is available before we commit.
141
+ fh.fileno()
142
+ _mcp_stderr_log_fh = fh
143
+ except Exception as exc: # pragma: no cover — best-effort fallback
144
+ logger.debug("Failed to open MCP stderr log, using devnull: %s", exc)
145
+ try:
146
+ _mcp_stderr_log_fh = open(os.devnull, "w", encoding="utf-8")
147
+ except Exception:
148
+ # Last resort: the real stderr. Not ideal for TUI users but
149
+ # it matches pre-fix behavior.
150
+ _mcp_stderr_log_fh = sys.stderr
151
+ return _mcp_stderr_log_fh
152
+
153
+
154
+ def _write_stderr_log_header(server_name: str) -> None:
155
+ """Write a human-readable session marker before launching a server.
156
+
157
+ Gives operators a way to find each server's output in the shared
158
+ ``mcp-stderr.log`` file without needing per-line prefixes (which would
159
+ require a pipe + reader thread and complicate shutdown).
160
+ """
161
+ fh = _get_mcp_stderr_log()
162
+ try:
163
+ ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
164
+ fh.write(f"\n===== [{ts}] starting MCP server '{server_name}' =====\n")
165
+ fh.flush()
166
+ except Exception:
167
+ pass
168
+
169
+ # ---------------------------------------------------------------------------
170
+ # Graceful import -- MCP SDK is an optional dependency
171
+ # ---------------------------------------------------------------------------
172
+
173
+ _MCP_AVAILABLE = False
174
+ _MCP_HTTP_AVAILABLE = False
175
+ _MCP_SAMPLING_TYPES = False
176
+ _MCP_NOTIFICATION_TYPES = False
177
+ _MCP_MESSAGE_HANDLER_SUPPORTED = False
178
+ # Conservative fallback for SDK builds that don't export LATEST_PROTOCOL_VERSION.
179
+ # Streamable HTTP was introduced by 2025-03-26, so this remains valid for the
180
+ # HTTP transport path even on older-but-supported SDK versions.
181
+ LATEST_PROTOCOL_VERSION = "2025-03-26"
182
+ try:
183
+ from mcp import ClientSession, StdioServerParameters
184
+ from mcp.client.stdio import stdio_client
185
+ _MCP_AVAILABLE = True
186
+ try:
187
+ from mcp.client.streamable_http import streamablehttp_client
188
+ _MCP_HTTP_AVAILABLE = True
189
+ except ImportError:
190
+ _MCP_HTTP_AVAILABLE = False
191
+ # Prefer the non-deprecated API (mcp >= 1.24.0); fall back to the
192
+ # deprecated wrapper for older SDK versions.
193
+ try:
194
+ from mcp.client.streamable_http import streamable_http_client
195
+ _MCP_NEW_HTTP = True
196
+ except ImportError:
197
+ _MCP_NEW_HTTP = False
198
+ try:
199
+ from mcp.types import LATEST_PROTOCOL_VERSION
200
+ except ImportError:
201
+ logger.debug("mcp.types.LATEST_PROTOCOL_VERSION not available -- using fallback protocol version")
202
+ # SSE transport client (for MCP servers using SSE transport instead of Streamable HTTP)
203
+ try:
204
+ from mcp.client.sse import sse_client
205
+ except ImportError:
206
+ sse_client = None
207
+ logger.debug("mcp.client.sse.sse_client not available -- SSE transport disabled")
208
+ # Sampling types -- separated so older SDK versions don't break MCP support
209
+ try:
210
+ from mcp.types import (
211
+ CreateMessageResult,
212
+ CreateMessageResultWithTools,
213
+ ErrorData,
214
+ SamplingCapability,
215
+ SamplingToolsCapability,
216
+ TextContent,
217
+ ToolUseContent,
218
+ )
219
+ _MCP_SAMPLING_TYPES = True
220
+ except ImportError:
221
+ logger.debug("MCP sampling types not available -- sampling disabled")
222
+ # Notification types for dynamic tool discovery (tools/list_changed)
223
+ try:
224
+ from mcp.types import (
225
+ ServerNotification,
226
+ ToolListChangedNotification,
227
+ PromptListChangedNotification,
228
+ ResourceListChangedNotification,
229
+ )
230
+ _MCP_NOTIFICATION_TYPES = True
231
+ except ImportError:
232
+ logger.debug("MCP notification types not available -- dynamic tool discovery disabled")
233
+ except ImportError:
234
+ logger.debug("mcp package not installed -- MCP tool support disabled")
235
+
236
+
237
+ def _check_message_handler_support() -> bool:
238
+ """Check if ClientSession accepts ``message_handler`` kwarg.
239
+
240
+ Inspects the constructor signature for backward compatibility with older
241
+ MCP SDK versions that don't support notification handlers.
242
+ """
243
+ if not _MCP_AVAILABLE:
244
+ return False
245
+ try:
246
+ return "message_handler" in inspect.signature(ClientSession).parameters
247
+ except (TypeError, ValueError):
248
+ return False
249
+
250
+
251
+ _MCP_MESSAGE_HANDLER_SUPPORTED = _check_message_handler_support()
252
+ if _MCP_AVAILABLE and not _MCP_MESSAGE_HANDLER_SUPPORTED:
253
+ logger.debug("MCP SDK does not support message_handler -- dynamic tool discovery disabled")
254
+
255
+ # ---------------------------------------------------------------------------
256
+ # Constants
257
+ # ---------------------------------------------------------------------------
258
+
259
+ _DEFAULT_TOOL_TIMEOUT = 120 # seconds for tool calls
260
+ _DEFAULT_CONNECT_TIMEOUT = 60 # seconds for initial connection per server
261
+ _MAX_RECONNECT_RETRIES = 5
262
+ _MAX_INITIAL_CONNECT_RETRIES = 3 # retries for the very first connection attempt
263
+ _MAX_BACKOFF_SECONDS = 60
264
+
265
+ # Environment variables that are safe to pass to stdio subprocesses
266
+ _SAFE_ENV_KEYS = frozenset({
267
+ "PATH", "HOME", "USER", "LANG", "LC_ALL", "TERM", "SHELL", "TMPDIR",
268
+ })
269
+
270
+ # Regex for credential patterns to strip from error messages
271
+ _CREDENTIAL_PATTERN = re.compile(
272
+ r"(?:"
273
+ r"ghp_[A-Za-z0-9_]{1,255}" # GitHub PAT
274
+ r"|sk-[A-Za-z0-9_]{1,255}" # OpenAI-style key
275
+ r"|Bearer\s+\S+" # Bearer token
276
+ r"|token=[^\s&,;\"']{1,255}" # token=...
277
+ r"|key=[^\s&,;\"']{1,255}" # key=...
278
+ r"|API_KEY=[^\s&,;\"']{1,255}" # API_KEY=...
279
+ r"|password=[^\s&,;\"']{1,255}" # password=...
280
+ r"|secret=[^\s&,;\"']{1,255}" # secret=...
281
+ r")",
282
+ re.IGNORECASE,
283
+ )
284
+
285
+ # Pre-compiled pattern for ${VAR_NAME} style env-var interpolation.
286
+ # Supports any non-} characters in the variable name (hyphens, dots, etc.)
287
+ # so providers like MY-VAR or my.var work correctly.
288
+ _ENV_VAR_PATTERN = re.compile(r"\$\{([^}]+)\}")
289
+
290
+
291
+ # ---------------------------------------------------------------------------
292
+ # Security helpers
293
+ # ---------------------------------------------------------------------------
294
+
295
+ def _build_safe_env(user_env: Optional[dict]) -> dict:
296
+ """Build a filtered environment dict for stdio subprocesses.
297
+
298
+ Only passes through safe baseline variables (PATH, HOME, etc.) and XDG_*
299
+ variables from the current process environment, plus any variables
300
+ explicitly specified by the user in the server config.
301
+
302
+ This prevents accidentally leaking secrets like API keys, tokens, or
303
+ credentials to MCP server subprocesses.
304
+ """
305
+ env = {}
306
+ for key, value in os.environ.items():
307
+ if key in _SAFE_ENV_KEYS or key.startswith("XDG_"):
308
+ env[key] = value
309
+ if user_env:
310
+ env.update(user_env)
311
+ return env
312
+
313
+
314
+ def _sanitize_error(text: str) -> str:
315
+ """Strip credential-like patterns from error text before returning to LLM.
316
+
317
+ Replaces tokens, keys, and other secrets with [REDACTED] to prevent
318
+ accidental credential exposure in tool error responses.
319
+ """
320
+ return _CREDENTIAL_PATTERN.sub("[REDACTED]", text)
321
+
322
+
323
+ def _exc_str(exc: BaseException) -> str:
324
+ """Return a non-empty human-readable string for *exc*.
325
+
326
+ Some exception classes (e.g. ``anyio.ClosedResourceError``) are raised
327
+ without a message argument, so ``str(exc)`` is ``""``. This helper
328
+ falls back to ``repr(exc)`` so that error messages shown to the user
329
+ and logged to disk always carry *some* diagnostic information.
330
+ """
331
+ text = str(exc).strip()
332
+ return text if text else repr(exc)
333
+
334
+
335
+ # ---------------------------------------------------------------------------
336
+ # MCP tool description content scanning
337
+ # ---------------------------------------------------------------------------
338
+
339
+ # Patterns that indicate potential prompt injection in MCP tool descriptions.
340
+ # These are WARNING-level — we log but don't block, since false positives
341
+ # would break legitimate MCP servers.
342
+ _MCP_INJECTION_PATTERNS = [
343
+ (re.compile(r"ignore\s+(all\s+)?previous\s+instructions", re.I),
344
+ "prompt override attempt ('ignore previous instructions')"),
345
+ (re.compile(r"you\s+are\s+now\s+a", re.I),
346
+ "identity override attempt ('you are now a...')"),
347
+ (re.compile(r"your\s+new\s+(task|role|instructions?)\s+(is|are)", re.I),
348
+ "task override attempt"),
349
+ (re.compile(r"system\s*:\s*", re.I),
350
+ "system prompt injection attempt"),
351
+ (re.compile(r"<\s*(system|human|assistant)\s*>", re.I),
352
+ "role tag injection attempt"),
353
+ (re.compile(r"do\s+not\s+(tell|inform|mention|reveal)", re.I),
354
+ "concealment instruction"),
355
+ (re.compile(r"(curl|wget|fetch)\s+https?://", re.I),
356
+ "network command in description"),
357
+ (re.compile(r"base64\.(b64decode|decodebytes)", re.I),
358
+ "base64 decode reference"),
359
+ (re.compile(r"exec\s*\(|eval\s*\(", re.I),
360
+ "code execution reference"),
361
+ (re.compile(r"import\s+(subprocess|os|shutil|socket)", re.I),
362
+ "dangerous import reference"),
363
+ ]
364
+
365
+
366
+ def _scan_mcp_description(server_name: str, tool_name: str, description: str) -> List[str]:
367
+ """Scan an MCP tool description for prompt injection patterns.
368
+
369
+ Returns a list of finding strings (empty = clean).
370
+ """
371
+ findings = []
372
+ if not description:
373
+ return findings
374
+ for pattern, reason in _MCP_INJECTION_PATTERNS:
375
+ if pattern.search(description):
376
+ findings.append(reason)
377
+ if findings:
378
+ logger.warning(
379
+ "MCP server '%s' tool '%s': suspicious description content — %s. "
380
+ "Description: %.200s",
381
+ server_name, tool_name, "; ".join(findings),
382
+ description,
383
+ )
384
+ return findings
385
+
386
+
387
+ def _prepend_path(env: dict, directory: str) -> dict:
388
+ """Prepend *directory* to env PATH if it is not already present."""
389
+ updated = dict(env or {})
390
+ if not directory:
391
+ return updated
392
+
393
+ existing = updated.get("PATH", "")
394
+ parts = [part for part in existing.split(os.pathsep) if part]
395
+ if directory not in parts:
396
+ parts = [directory, *parts]
397
+ updated["PATH"] = os.pathsep.join(parts) if parts else directory
398
+ return updated
399
+
400
+
401
+ def _resolve_stdio_command(command: str, env: dict) -> tuple[str, dict]:
402
+ """Resolve a stdio MCP command against the exact subprocess environment.
403
+
404
+ This primarily exists to make bare ``npx``/``npm``/``node`` commands work
405
+ reliably even when MCP subprocesses run under a filtered PATH.
406
+ """
407
+ resolved_command = os.path.expanduser(str(command).strip())
408
+ resolved_env = dict(env or {})
409
+
410
+ if os.sep not in resolved_command:
411
+ path_arg = resolved_env["PATH"] if "PATH" in resolved_env else None
412
+ which_hit = shutil.which(resolved_command, path=path_arg)
413
+ if which_hit:
414
+ resolved_command = which_hit
415
+ elif resolved_command in {"npx", "npm", "node"}:
416
+ hermes_home = os.path.expanduser(
417
+ os.getenv(
418
+ "HERMES_HOME", os.path.join(os.path.expanduser("~"), ".hermes")
419
+ )
420
+ )
421
+ candidates = [
422
+ os.path.join(hermes_home, "node", "bin", resolved_command),
423
+ os.path.join(os.path.expanduser("~"), ".local", "bin", resolved_command),
424
+ ]
425
+ for candidate in candidates:
426
+ if os.path.isfile(candidate) and os.access(candidate, os.X_OK):
427
+ resolved_command = candidate
428
+ break
429
+
430
+ command_dir = os.path.dirname(resolved_command)
431
+ if command_dir:
432
+ resolved_env = _prepend_path(resolved_env, command_dir)
433
+
434
+ return resolved_command, resolved_env
435
+
436
+
437
+ # ---------------------------------------------------------------------------
438
+ # MCP ImageContent block → Hermes MEDIA tag
439
+ # ---------------------------------------------------------------------------
440
+
441
+
442
+ def _mcp_image_extension_for_mime_type(mime_type: str) -> str:
443
+ """Return a reasonable file extension for an MCP image MIME type."""
444
+ import mimetypes
445
+ normalized = (mime_type or "").split(";", 1)[0].strip().lower()
446
+ if normalized in {"image/jpeg", "image/jpg"}:
447
+ return ".jpg"
448
+ return mimetypes.guess_extension(normalized) or ".png"
449
+
450
+
451
+ def _cache_mcp_image_block(block) -> str:
452
+ """Cache an MCP ``ImageContent`` block to the shared image cache and
453
+ return a ``MEDIA:<path>`` tag that Hermes gateways know how to render.
454
+
455
+ Returns an empty string when *block* is not an image, when the base64
456
+ payload is malformed, or when the cache helper rejects the bytes (e.g.
457
+ non-image MIME masquerading as an image). Errors are logged, not raised:
458
+ a single bad block shouldn't kill the tool result, and the caller will
459
+ fall through to any text blocks that did parse.
460
+ """
461
+ import base64
462
+
463
+ data = getattr(block, "data", None)
464
+ mime_type = getattr(block, "mimeType", None)
465
+ normalized_mime = str(mime_type or "").split(";", 1)[0].strip().lower()
466
+ if data is None or not normalized_mime.startswith("image/"):
467
+ return ""
468
+
469
+ try:
470
+ raw_bytes = base64.b64decode(data)
471
+ except (TypeError, ValueError) as exc:
472
+ logger.warning("MCP image block decode failed (%s): %s", normalized_mime, exc)
473
+ return ""
474
+
475
+ try:
476
+ from gateway.platforms.base import cache_image_from_bytes
477
+
478
+ image_path = cache_image_from_bytes(
479
+ raw_bytes,
480
+ ext=_mcp_image_extension_for_mime_type(normalized_mime),
481
+ )
482
+ except ImportError:
483
+ # gateway.platforms.base not importable in this process (e.g. cron
484
+ # without gateway deps). Fall back to silently dropping — callers
485
+ # get any text blocks that did parse.
486
+ logger.debug("MCP image caching skipped — gateway.platforms.base unavailable")
487
+ return ""
488
+ except Exception as exc:
489
+ logger.warning("MCP image block cache failed: %s", exc)
490
+ return ""
491
+
492
+ return f"MEDIA:{image_path}"
493
+
494
+
495
+ def _format_connect_error(exc: BaseException) -> str:
496
+ """Render nested MCP connection errors into an actionable short message."""
497
+
498
+ def _find_missing(current: BaseException) -> Optional[str]:
499
+ nested = getattr(current, "exceptions", None)
500
+ if nested:
501
+ for child in nested:
502
+ missing = _find_missing(child)
503
+ if missing:
504
+ return missing
505
+ return None
506
+ if isinstance(current, FileNotFoundError):
507
+ if getattr(current, "filename", None):
508
+ return str(current.filename)
509
+ match = re.search(r"No such file or directory: '([^']+)'", str(current))
510
+ if match:
511
+ return match.group(1)
512
+ for attr in ("__cause__", "__context__"):
513
+ nested_exc = getattr(current, attr, None)
514
+ if isinstance(nested_exc, BaseException):
515
+ missing = _find_missing(nested_exc)
516
+ if missing:
517
+ return missing
518
+ return None
519
+
520
+ def _flatten_messages(current: BaseException) -> List[str]:
521
+ nested = getattr(current, "exceptions", None)
522
+ if nested:
523
+ flattened: List[str] = []
524
+ for child in nested:
525
+ flattened.extend(_flatten_messages(child))
526
+ return flattened
527
+ messages = []
528
+ text = str(current).strip()
529
+ if text:
530
+ messages.append(text)
531
+ for attr in ("__cause__", "__context__"):
532
+ nested_exc = getattr(current, attr, None)
533
+ if isinstance(nested_exc, BaseException):
534
+ messages.extend(_flatten_messages(nested_exc))
535
+ return messages or [current.__class__.__name__]
536
+
537
+ missing = _find_missing(exc)
538
+ if missing:
539
+ message = f"missing executable '{missing}'"
540
+ if os.path.basename(missing) in {"npx", "npm", "node"}:
541
+ message += (
542
+ " (ensure Node.js is installed and PATH includes its bin directory, "
543
+ "or set mcp_servers.<name>.command to an absolute path and include "
544
+ "that directory in mcp_servers.<name>.env.PATH)"
545
+ )
546
+ return _sanitize_error(message)
547
+
548
+ deduped: List[str] = []
549
+ for item in _flatten_messages(exc):
550
+ if item not in deduped:
551
+ deduped.append(item)
552
+ return _sanitize_error("; ".join(deduped[:3]))
553
+
554
+
555
+ # ---------------------------------------------------------------------------
556
+ # Sampling -- server-initiated LLM requests (MCP sampling/createMessage)
557
+ # ---------------------------------------------------------------------------
558
+
559
+ def _safe_numeric(value, default, coerce=int, minimum=1):
560
+ """Coerce a config value to a numeric type, returning *default* on failure.
561
+
562
+ Handles string values from YAML (e.g. ``"10"`` instead of ``10``),
563
+ non-finite floats, and values below *minimum*.
564
+ """
565
+ try:
566
+ result = coerce(value)
567
+ if isinstance(result, float) and not math.isfinite(result):
568
+ return default
569
+ return max(result, minimum)
570
+ except (TypeError, ValueError, OverflowError):
571
+ return default
572
+
573
+
574
+ class SamplingHandler:
575
+ """Handles sampling/createMessage requests for a single MCP server.
576
+
577
+ Each MCPServerTask that has sampling enabled creates one SamplingHandler.
578
+ The handler is callable and passed directly to ``ClientSession`` as
579
+ the ``sampling_callback``. All state (rate-limit timestamps, metrics,
580
+ tool-loop counters) lives on the instance -- no module-level globals.
581
+
582
+ The callback is async and runs on the MCP background event loop. The
583
+ sync LLM call is offloaded to a thread via ``asyncio.to_thread()`` so
584
+ it doesn't block the event loop.
585
+ """
586
+
587
+ _STOP_REASON_MAP = {"stop": "endTurn", "length": "maxTokens", "tool_calls": "toolUse"}
588
+
589
+ def __init__(self, server_name: str, config: dict):
590
+ self.server_name = server_name
591
+ self.max_rpm = _safe_numeric(config.get("max_rpm", 10), 10, int)
592
+ self.timeout = _safe_numeric(config.get("timeout", 30), 30, float)
593
+ self.max_tokens_cap = _safe_numeric(config.get("max_tokens_cap", 4096), 4096, int)
594
+ self.max_tool_rounds = _safe_numeric(
595
+ config.get("max_tool_rounds", 5), 5, int, minimum=0,
596
+ )
597
+ self.model_override = config.get("model")
598
+ self.allowed_models = config.get("allowed_models", [])
599
+
600
+ _log_levels = {"debug": logging.DEBUG, "info": logging.INFO, "warning": logging.WARNING}
601
+ self.audit_level = _log_levels.get(
602
+ str(config.get("log_level", "info")).lower(), logging.INFO,
603
+ )
604
+
605
+ # Per-instance state
606
+ self._rate_timestamps: List[float] = []
607
+ self._tool_loop_count = 0
608
+ self.metrics = {"requests": 0, "errors": 0, "tokens_used": 0, "tool_use_count": 0}
609
+
610
+ # -- Rate limiting -------------------------------------------------------
611
+
612
+ def _check_rate_limit(self) -> bool:
613
+ """Sliding-window rate limiter. Returns True if request is allowed."""
614
+ now = time.time()
615
+ window = now - 60
616
+ self._rate_timestamps[:] = [t for t in self._rate_timestamps if t > window]
617
+ if len(self._rate_timestamps) >= self.max_rpm:
618
+ return False
619
+ self._rate_timestamps.append(now)
620
+ return True
621
+
622
+ # -- Model resolution ----------------------------------------------------
623
+
624
+ def _resolve_model(self, preferences) -> Optional[str]:
625
+ """Config override > server hint > None (use default)."""
626
+ if self.model_override:
627
+ return self.model_override
628
+ if preferences and hasattr(preferences, "hints") and preferences.hints:
629
+ for hint in preferences.hints:
630
+ if hasattr(hint, "name") and hint.name:
631
+ return hint.name
632
+ return None
633
+
634
+ # -- Message conversion --------------------------------------------------
635
+
636
+ @staticmethod
637
+ def _extract_tool_result_text(block) -> str:
638
+ """Extract text from a ToolResultContent block."""
639
+ if not hasattr(block, "content") or block.content is None:
640
+ return ""
641
+ items = block.content if isinstance(block.content, list) else [block.content]
642
+ return "\n".join(item.text for item in items if hasattr(item, "text"))
643
+
644
+ def _convert_messages(self, params) -> List[dict]:
645
+ """Convert MCP SamplingMessages to OpenAI format.
646
+
647
+ Uses ``msg.content_as_list`` (SDK helper) so single-block and
648
+ list-of-blocks are handled uniformly. Dispatches per block type
649
+ with ``isinstance`` on real SDK types when available, falling back
650
+ to duck-typing via ``hasattr`` for compatibility.
651
+ """
652
+ messages: List[dict] = []
653
+ for msg in params.messages:
654
+ blocks = msg.content_as_list if hasattr(msg, "content_as_list") else (
655
+ msg.content if isinstance(msg.content, list) else [msg.content]
656
+ )
657
+
658
+ # Separate blocks by kind
659
+ tool_results = [b for b in blocks if hasattr(b, "toolUseId")]
660
+ tool_uses = [b for b in blocks if hasattr(b, "name") and hasattr(b, "input") and not hasattr(b, "toolUseId")]
661
+ content_blocks = [b for b in blocks if not hasattr(b, "toolUseId") and not (hasattr(b, "name") and hasattr(b, "input"))]
662
+
663
+ # Emit tool result messages (role: tool)
664
+ for tr in tool_results:
665
+ messages.append({
666
+ "role": "tool",
667
+ "tool_call_id": tr.toolUseId,
668
+ "content": self._extract_tool_result_text(tr),
669
+ })
670
+
671
+ # Emit assistant tool_calls message
672
+ if tool_uses:
673
+ tc_list = []
674
+ for tu in tool_uses:
675
+ tc_list.append({
676
+ "id": getattr(tu, "id", f"call_{len(tc_list)}"),
677
+ "type": "function",
678
+ "function": {
679
+ "name": tu.name,
680
+ "arguments": json.dumps(tu.input, ensure_ascii=False) if isinstance(tu.input, dict) else str(tu.input),
681
+ },
682
+ })
683
+ msg_dict: dict = {"role": msg.role, "tool_calls": tc_list}
684
+ # Include any accompanying text
685
+ text_parts = [b.text for b in content_blocks if hasattr(b, "text")]
686
+ if text_parts:
687
+ msg_dict["content"] = "\n".join(text_parts)
688
+ messages.append(msg_dict)
689
+ elif content_blocks:
690
+ # Pure text/image content
691
+ if len(content_blocks) == 1 and hasattr(content_blocks[0], "text"):
692
+ messages.append({"role": msg.role, "content": content_blocks[0].text})
693
+ else:
694
+ parts = []
695
+ for block in content_blocks:
696
+ if hasattr(block, "text"):
697
+ parts.append({"type": "text", "text": block.text})
698
+ elif hasattr(block, "data") and hasattr(block, "mimeType"):
699
+ parts.append({
700
+ "type": "image_url",
701
+ "image_url": {"url": f"data:{block.mimeType};base64,{block.data}"},
702
+ })
703
+ else:
704
+ logger.warning(
705
+ "Unsupported sampling content block type: %s (skipped)",
706
+ type(block).__name__,
707
+ )
708
+ if parts:
709
+ messages.append({"role": msg.role, "content": parts})
710
+
711
+ return messages
712
+
713
+ # -- Error helper --------------------------------------------------------
714
+
715
+ @staticmethod
716
+ def _error(message: str, code: int = -1):
717
+ """Return ErrorData (MCP spec) or raise as fallback."""
718
+ if _MCP_SAMPLING_TYPES:
719
+ return ErrorData(code=code, message=message)
720
+ raise Exception(message)
721
+
722
+ # -- Response building ---------------------------------------------------
723
+
724
+ def _build_tool_use_result(self, choice, response):
725
+ """Build a CreateMessageResultWithTools from an LLM tool_calls response."""
726
+ self.metrics["tool_use_count"] += 1
727
+
728
+ # Tool loop governance
729
+ if self.max_tool_rounds == 0:
730
+ self._tool_loop_count = 0
731
+ return self._error(
732
+ f"Tool loops disabled for server '{self.server_name}' (max_tool_rounds=0)"
733
+ )
734
+
735
+ self._tool_loop_count += 1
736
+ if self._tool_loop_count > self.max_tool_rounds:
737
+ self._tool_loop_count = 0
738
+ return self._error(
739
+ f"Tool loop limit exceeded for server '{self.server_name}' "
740
+ f"(max {self.max_tool_rounds} rounds)"
741
+ )
742
+
743
+ content_blocks = []
744
+ for tc in choice.message.tool_calls:
745
+ args = tc.function.arguments
746
+ if isinstance(args, str):
747
+ try:
748
+ parsed = json.loads(args)
749
+ except (json.JSONDecodeError, ValueError):
750
+ logger.warning(
751
+ "MCP server '%s': malformed tool_calls arguments "
752
+ "from LLM (wrapping as raw): %.100s",
753
+ self.server_name, args,
754
+ )
755
+ parsed = {"_raw": args}
756
+ else:
757
+ parsed = args if isinstance(args, dict) else {"_raw": str(args)}
758
+
759
+ content_blocks.append(ToolUseContent(
760
+ type="tool_use",
761
+ id=tc.id,
762
+ name=tc.function.name,
763
+ input=parsed,
764
+ ))
765
+
766
+ logger.log(
767
+ self.audit_level,
768
+ "MCP server '%s' sampling response: model=%s, tokens=%s, tool_calls=%d",
769
+ self.server_name, response.model,
770
+ getattr(getattr(response, "usage", None), "total_tokens", "?"),
771
+ len(content_blocks),
772
+ )
773
+
774
+ return CreateMessageResultWithTools(
775
+ role="assistant",
776
+ content=content_blocks,
777
+ model=response.model,
778
+ stopReason="toolUse",
779
+ )
780
+
781
+ def _build_text_result(self, choice, response):
782
+ """Build a CreateMessageResult from a normal text response."""
783
+ self._tool_loop_count = 0 # reset on text response
784
+ response_text = choice.message.content or ""
785
+
786
+ logger.log(
787
+ self.audit_level,
788
+ "MCP server '%s' sampling response: model=%s, tokens=%s",
789
+ self.server_name, response.model,
790
+ getattr(getattr(response, "usage", None), "total_tokens", "?"),
791
+ )
792
+
793
+ return CreateMessageResult(
794
+ role="assistant",
795
+ content=TextContent(type="text", text=_sanitize_error(response_text)),
796
+ model=response.model,
797
+ stopReason=self._STOP_REASON_MAP.get(choice.finish_reason, "endTurn"),
798
+ )
799
+
800
+ # -- Session kwargs helper -----------------------------------------------
801
+
802
+ def session_kwargs(self) -> dict:
803
+ """Return kwargs to pass to ClientSession for sampling support."""
804
+ return {
805
+ "sampling_callback": self,
806
+ "sampling_capabilities": SamplingCapability(
807
+ tools=SamplingToolsCapability(),
808
+ ),
809
+ }
810
+
811
+ # -- Main callback -------------------------------------------------------
812
+
813
+ async def __call__(self, context, params):
814
+ """Sampling callback invoked by the MCP SDK.
815
+
816
+ Conforms to ``SamplingFnT`` protocol. Returns
817
+ ``CreateMessageResult``, ``CreateMessageResultWithTools``, or
818
+ ``ErrorData``.
819
+ """
820
+ # Rate limit
821
+ if not self._check_rate_limit():
822
+ logger.warning(
823
+ "MCP server '%s' sampling rate limit exceeded (%d/min)",
824
+ self.server_name, self.max_rpm,
825
+ )
826
+ self.metrics["errors"] += 1
827
+ return self._error(
828
+ f"Sampling rate limit exceeded for server '{self.server_name}' "
829
+ f"({self.max_rpm} requests/minute)"
830
+ )
831
+
832
+ # Resolve model
833
+ model = self._resolve_model(getattr(params, "modelPreferences", None))
834
+
835
+ # Get auxiliary LLM client via centralized router
836
+ from agent.auxiliary_client import call_llm
837
+
838
+ # Model whitelist check (we need to resolve model before calling)
839
+ resolved_model = model or self.model_override or ""
840
+
841
+ if self.allowed_models and resolved_model and resolved_model not in self.allowed_models:
842
+ logger.warning(
843
+ "MCP server '%s' requested model '%s' not in allowed_models",
844
+ self.server_name, resolved_model,
845
+ )
846
+ self.metrics["errors"] += 1
847
+ return self._error(
848
+ f"Model '{resolved_model}' not allowed for server "
849
+ f"'{self.server_name}'. Allowed: {', '.join(self.allowed_models)}"
850
+ )
851
+
852
+ # Convert messages
853
+ messages = self._convert_messages(params)
854
+ if hasattr(params, "systemPrompt") and params.systemPrompt:
855
+ messages.insert(0, {"role": "system", "content": params.systemPrompt})
856
+
857
+ # Build LLM call kwargs
858
+ max_tokens = min(params.maxTokens, self.max_tokens_cap)
859
+ call_temperature = None
860
+ if hasattr(params, "temperature") and params.temperature is not None:
861
+ call_temperature = params.temperature
862
+
863
+ # Forward server-provided tools
864
+ call_tools = None
865
+ server_tools = getattr(params, "tools", None)
866
+ if server_tools:
867
+ call_tools = [
868
+ {
869
+ "type": "function",
870
+ "function": {
871
+ "name": getattr(t, "name", ""),
872
+ "description": getattr(t, "description", "") or "",
873
+ "parameters": _normalize_mcp_input_schema(
874
+ getattr(t, "inputSchema", None)
875
+ ),
876
+ },
877
+ }
878
+ for t in server_tools
879
+ ]
880
+
881
+ logger.log(
882
+ self.audit_level,
883
+ "MCP server '%s' sampling request: model=%s, max_tokens=%d, messages=%d",
884
+ self.server_name, resolved_model, max_tokens, len(messages),
885
+ )
886
+
887
+ # Offload sync LLM call to thread (non-blocking)
888
+ def _sync_call():
889
+ return call_llm(
890
+ task="mcp",
891
+ model=resolved_model or None,
892
+ messages=messages,
893
+ temperature=call_temperature,
894
+ max_tokens=max_tokens,
895
+ tools=call_tools,
896
+ timeout=self.timeout,
897
+ )
898
+
899
+ try:
900
+ response = await asyncio.wait_for(
901
+ asyncio.to_thread(_sync_call), timeout=self.timeout,
902
+ )
903
+ except asyncio.TimeoutError:
904
+ self.metrics["errors"] += 1
905
+ return self._error(
906
+ f"Sampling LLM call timed out after {self.timeout}s "
907
+ f"for server '{self.server_name}'"
908
+ )
909
+ except Exception as exc:
910
+ self.metrics["errors"] += 1
911
+ return self._error(
912
+ f"Sampling LLM call failed: {_sanitize_error(_exc_str(exc))}"
913
+ )
914
+
915
+ # Guard against empty choices (content filtering, provider errors)
916
+ if not getattr(response, "choices", None):
917
+ self.metrics["errors"] += 1
918
+ return self._error(
919
+ f"LLM returned empty response (no choices) for server "
920
+ f"'{self.server_name}'"
921
+ )
922
+
923
+ # Track metrics
924
+ choice = response.choices[0]
925
+ self.metrics["requests"] += 1
926
+ total_tokens = getattr(getattr(response, "usage", None), "total_tokens", 0)
927
+ if isinstance(total_tokens, int):
928
+ self.metrics["tokens_used"] += total_tokens
929
+
930
+ # Dispatch based on response type
931
+ if (
932
+ choice.finish_reason == "tool_calls"
933
+ and hasattr(choice.message, "tool_calls")
934
+ and choice.message.tool_calls
935
+ ):
936
+ return self._build_tool_use_result(choice, response)
937
+
938
+ return self._build_text_result(choice, response)
939
+
940
+
941
+ # ---------------------------------------------------------------------------
942
+ # Server task -- each MCP server lives in one long-lived asyncio Task
943
+ # ---------------------------------------------------------------------------
944
+
945
+ class MCPServerTask:
946
+ """Manages a single MCP server connection in a dedicated asyncio Task.
947
+
948
+ The entire connection lifecycle (connect, discover, serve, disconnect)
949
+ runs inside one asyncio Task so that anyio cancel-scopes created by
950
+ the transport client are entered and exited in the same Task context.
951
+
952
+ Supports both stdio and HTTP/StreamableHTTP transports.
953
+ """
954
+
955
+ __slots__ = (
956
+ "name", "session", "tool_timeout",
957
+ "_task", "_ready", "_shutdown_event", "_reconnect_event",
958
+ "_tools", "_error", "_config",
959
+ "_sampling", "_registered_tool_names", "_auth_type", "_refresh_lock",
960
+ "_rpc_lock", "_pending_refresh_tasks",
961
+ "initialize_result",
962
+ )
963
+
964
+ def __init__(self, name: str):
965
+ self.name = name
966
+ self.session: Optional[Any] = None
967
+ self.tool_timeout: float = _DEFAULT_TOOL_TIMEOUT
968
+ self._task: Optional[asyncio.Task] = None
969
+ self._ready = asyncio.Event()
970
+ self._shutdown_event = asyncio.Event()
971
+ # Set by tool handlers on auth failure after manager.handle_401()
972
+ # confirms recovery is viable. When set, _run_http / _run_stdio
973
+ # exit their async-with blocks cleanly (no exception), and the
974
+ # outer run() loop re-enters the transport so the MCP session is
975
+ # rebuilt with fresh credentials.
976
+ self._reconnect_event = asyncio.Event()
977
+ self._tools: list = []
978
+ self._error: Optional[Exception] = None
979
+ self._config: dict = {}
980
+ self._sampling: Optional[SamplingHandler] = None
981
+ self._registered_tool_names: list[str] = []
982
+ self._auth_type: str = ""
983
+ self._refresh_lock = asyncio.Lock()
984
+ # MCP stdio sessions are a single JSON-RPC stream. Some servers emit
985
+ # list_changed notifications during startup; if the notification
986
+ # handler calls list_tools while a normal tool call is in flight, the
987
+ # stream can wedge and the user-visible tool call times out. Serialize
988
+ # client-initiated RPCs per server. The lock is also applied to HTTP
989
+ # transports for conservative per-server ordering.
990
+ self._rpc_lock = asyncio.Lock()
991
+ self._pending_refresh_tasks: set[asyncio.Task] = set()
992
+ # Captures the ``InitializeResult`` returned by
993
+ # ``await session.initialize()`` so downstream code can inspect the
994
+ # server's real advertised capabilities (``.capabilities.resources``,
995
+ # ``.capabilities.prompts``) instead of assuming every ``ClientSession``
996
+ # method attribute corresponds to a supported server method. See #18051.
997
+ self.initialize_result: Optional[Any] = None
998
+
999
+ def _is_http(self) -> bool:
1000
+ """Check if this server uses HTTP transport."""
1001
+ return "url" in self._config
1002
+
1003
+ # ----- Dynamic tool discovery (notifications/tools/list_changed) -----
1004
+
1005
+ async def _refresh_tools_task(self):
1006
+ """Run a dynamic tool refresh and log failures from background tasks."""
1007
+ try:
1008
+ await self._refresh_tools()
1009
+ except asyncio.CancelledError:
1010
+ raise
1011
+ except Exception:
1012
+ logger.exception("MCP server '%s': dynamic tool refresh failed", self.name)
1013
+
1014
+ def _schedule_tools_refresh(self) -> asyncio.Task:
1015
+ """Schedule a background tool refresh and keep it strongly referenced."""
1016
+ task = asyncio.create_task(self._refresh_tools_task())
1017
+ self._pending_refresh_tasks.add(task)
1018
+ task.add_done_callback(self._pending_refresh_tasks.discard)
1019
+ return task
1020
+
1021
+ def _make_message_handler(self):
1022
+ """Build a ``message_handler`` callback for ``ClientSession``.
1023
+
1024
+ Dispatches on notification type. Only ``ToolListChangedNotification``
1025
+ triggers a refresh; prompt and resource change notifications are
1026
+ logged as stubs for future work.
1027
+ """
1028
+ async def _handler(message):
1029
+ try:
1030
+ if isinstance(message, Exception):
1031
+ logger.debug("MCP message handler (%s): exception: %s", self.name, message)
1032
+ return
1033
+ if _MCP_NOTIFICATION_TYPES and isinstance(message, ServerNotification):
1034
+ match message.root:
1035
+ case ToolListChangedNotification():
1036
+ logger.info(
1037
+ "MCP server '%s': received tools/list_changed notification",
1038
+ self.name,
1039
+ )
1040
+ # Some servers (notably mongodb-mcp-server) emit
1041
+ # tools/list_changed immediately after initialize,
1042
+ # while the client may already be executing another
1043
+ # request. Refreshing synchronously inside the SDK
1044
+ # notification handler can race with that request
1045
+ # and wedge the stdio JSON-RPC stream, making all
1046
+ # subsequent tool calls time out. Do the refresh in
1047
+ # a separate task and let the handler return
1048
+ # promptly.
1049
+ self._schedule_tools_refresh()
1050
+ # Yield one loop tick so tests and short-lived
1051
+ # notification contexts can observe the scheduled
1052
+ # refresh without awaiting the full server RPC.
1053
+ await asyncio.sleep(0)
1054
+ case PromptListChangedNotification():
1055
+ logger.debug("MCP server '%s': prompts/list_changed (ignored)", self.name)
1056
+ case ResourceListChangedNotification():
1057
+ logger.debug("MCP server '%s': resources/list_changed (ignored)", self.name)
1058
+ case _:
1059
+ pass
1060
+ except Exception:
1061
+ logger.exception("Error in MCP message handler for '%s'", self.name)
1062
+ return _handler
1063
+
1064
+ async def _refresh_tools(self):
1065
+ """Re-fetch tools from the server and update the registry.
1066
+
1067
+ Called when the server sends ``notifications/tools/list_changed``.
1068
+ The lock prevents overlapping refreshes from rapid-fire notifications.
1069
+ After the initial ``await`` (list_tools), all mutations are synchronous
1070
+ — atomic from the event loop's perspective.
1071
+ """
1072
+ from tools.registry import registry
1073
+
1074
+ async with self._refresh_lock:
1075
+ # Capture old tool names for change diff
1076
+ old_tool_names = set(self._registered_tool_names)
1077
+
1078
+ # 1. Fetch current tool list from server
1079
+ async with self._rpc_lock:
1080
+ tools_result = await self.session.list_tools()
1081
+ new_mcp_tools = tools_result.tools if hasattr(tools_result, "tools") else []
1082
+
1083
+ # 2. Re-register with fresh tool list. Avoid nuke-and-repave for
1084
+ # all names: live agent turns may already have tool-call IDs
1085
+ # pointing at existing handler functions. Replacing entries
1086
+ # in-place is enough for unchanged names and avoids transient
1087
+ # "tool not connected" / stale-handler races during startup
1088
+ # notifications. Tools absent from the fresh list are no longer
1089
+ # callable, so remove only those stale registry entries first.
1090
+ stale_tool_names = old_tool_names - {
1091
+ f"mcp_{sanitize_mcp_name_component(self.name)}_"
1092
+ f"{sanitize_mcp_name_component(tool.name)}"
1093
+ for tool in new_mcp_tools
1094
+ }
1095
+ for tool_name in stale_tool_names:
1096
+ registry.deregister(tool_name)
1097
+
1098
+ # 3. Re-register with fresh tool list
1099
+ self._tools = new_mcp_tools
1100
+ self._registered_tool_names = _register_server_tools(
1101
+ self.name, self, self._config
1102
+ )
1103
+
1104
+ # 5. Log what changed (user-visible notification)
1105
+ new_tool_names = set(self._registered_tool_names)
1106
+ added = new_tool_names - old_tool_names
1107
+ removed = old_tool_names - new_tool_names
1108
+ changes = []
1109
+ if added:
1110
+ changes.append(f"added: {', '.join(sorted(added))}")
1111
+ if removed:
1112
+ changes.append(f"removed: {', '.join(sorted(removed))}")
1113
+ if changes:
1114
+ logger.warning(
1115
+ "MCP server '%s': tools changed dynamically — %s. "
1116
+ "Verify these changes are expected.",
1117
+ self.name, "; ".join(changes),
1118
+ )
1119
+ else:
1120
+ logger.info(
1121
+ "MCP server '%s': dynamically refreshed %d tool(s) (no changes)",
1122
+ self.name, len(self._registered_tool_names),
1123
+ )
1124
+
1125
+ async def _wait_for_lifecycle_event(self) -> str:
1126
+ """Block until either _shutdown_event or _reconnect_event fires.
1127
+
1128
+ Returns:
1129
+ "shutdown" if the server should exit the run loop entirely.
1130
+ "reconnect" if the server should tear down the current MCP
1131
+ session and re-enter the transport (fresh OAuth
1132
+ tokens, new session ID, etc.). The reconnect event
1133
+ is cleared before return so the next cycle starts
1134
+ with a fresh signal.
1135
+
1136
+ Shutdown takes precedence if both events are set simultaneously.
1137
+
1138
+ Periodically sends a lightweight keepalive (``list_tools``) to
1139
+ prevent TCP connections from going stale during long idle
1140
+ periods (#17003). If the keepalive fails, triggers a reconnect.
1141
+ """
1142
+ # Keepalive interval in seconds. Must be shorter than typical
1143
+ # LB / NAT idle-timeout (commonly 300-600s).
1144
+ _KEEPALIVE_INTERVAL = 180 # 3 minutes
1145
+
1146
+ shutdown_task = asyncio.create_task(self._shutdown_event.wait())
1147
+ reconnect_task = asyncio.create_task(self._reconnect_event.wait())
1148
+ try:
1149
+ while True:
1150
+ done, _pending = await asyncio.wait(
1151
+ {shutdown_task, reconnect_task},
1152
+ timeout=_KEEPALIVE_INTERVAL,
1153
+ return_when=asyncio.FIRST_COMPLETED,
1154
+ )
1155
+ if done:
1156
+ break
1157
+
1158
+ # Timeout — no lifecycle event fired. Send a keepalive
1159
+ # to exercise the connection and detect stale sockets.
1160
+ if self.session:
1161
+ try:
1162
+ await asyncio.wait_for(
1163
+ self.session.list_tools(),
1164
+ timeout=30.0,
1165
+ )
1166
+ except Exception as exc:
1167
+ logger.warning(
1168
+ "MCP server '%s' keepalive failed, "
1169
+ "triggering reconnect: %s",
1170
+ self.name, exc,
1171
+ )
1172
+ self._reconnect_event.set()
1173
+ break
1174
+ finally:
1175
+ for t in (shutdown_task, reconnect_task):
1176
+ if not t.done():
1177
+ t.cancel()
1178
+ try:
1179
+ await t
1180
+ except (asyncio.CancelledError, Exception):
1181
+ pass
1182
+
1183
+ if self._shutdown_event.is_set():
1184
+ return "shutdown"
1185
+ self._reconnect_event.clear()
1186
+ return "reconnect"
1187
+
1188
+ async def _run_stdio(self, config: dict):
1189
+ """Run the server using stdio transport."""
1190
+ command = config.get("command")
1191
+ args = config.get("args", [])
1192
+ user_env = config.get("env")
1193
+
1194
+ if not command:
1195
+ raise ValueError(
1196
+ f"MCP server '{self.name}' has no 'command' in config"
1197
+ )
1198
+
1199
+ safe_env = _build_safe_env(user_env)
1200
+ command, safe_env = _resolve_stdio_command(command, safe_env)
1201
+
1202
+ # Check package against OSV malware database before spawning
1203
+ from tools.osv_check import check_package_for_malware
1204
+ malware_error = check_package_for_malware(command, args)
1205
+ if malware_error:
1206
+ raise ValueError(
1207
+ f"MCP server '{self.name}': {malware_error}"
1208
+ )
1209
+
1210
+ server_params = StdioServerParameters(
1211
+ command=command,
1212
+ args=args,
1213
+ env=safe_env if safe_env else None,
1214
+ )
1215
+
1216
+ sampling_kwargs = self._sampling.session_kwargs() if self._sampling else {}
1217
+ if _MCP_NOTIFICATION_TYPES and _MCP_MESSAGE_HANDLER_SUPPORTED:
1218
+ sampling_kwargs["message_handler"] = self._make_message_handler()
1219
+
1220
+ # Snapshot child PIDs before spawning so we can track the new one.
1221
+ pids_before = _snapshot_child_pids()
1222
+ new_pids: set = set()
1223
+ # Redirect subprocess stderr into a shared log file so MCP servers
1224
+ # (FastMCP banners, slack-mcp startup JSON, etc.) don't dump onto
1225
+ # the user's TTY and corrupt the TUI. Preserves debuggability via
1226
+ # ~/.hermes/logs/mcp-stderr.log.
1227
+ _write_stderr_log_header(self.name)
1228
+ _errlog = _get_mcp_stderr_log()
1229
+ try:
1230
+ async with stdio_client(server_params, errlog=_errlog) as (
1231
+ read_stream,
1232
+ write_stream,
1233
+ ):
1234
+ # Capture the newly spawned subprocess PID for force-kill cleanup.
1235
+ new_pids = _snapshot_child_pids() - pids_before
1236
+ if new_pids:
1237
+ with _lock:
1238
+ for _pid in new_pids:
1239
+ _stdio_pids[_pid] = self.name
1240
+ async with ClientSession(
1241
+ read_stream, write_stream, **sampling_kwargs
1242
+ ) as session:
1243
+ self.initialize_result = await session.initialize()
1244
+ self.session = session
1245
+ await self._discover_tools()
1246
+ self._ready.set()
1247
+ # stdio transport does not use OAuth, but we still honor
1248
+ # _reconnect_event (e.g. future manual /mcp refresh) for
1249
+ # consistency with _run_http.
1250
+ await self._wait_for_lifecycle_event()
1251
+ finally:
1252
+ # Runs on clean exit, exceptions, AND asyncio cancellation.
1253
+ # If any of the spawned PIDs are still alive, the SDK's
1254
+ # teardown failed (common when the task is cancelled mid-way
1255
+ # on Linux, where setsid() children escape the parent cgroup).
1256
+ # Mark them as orphans so the next cleanup sweep can reap them.
1257
+ if new_pids:
1258
+ with _lock:
1259
+ for _pid in new_pids:
1260
+ _stdio_pids.pop(_pid, None)
1261
+ for pid in new_pids:
1262
+ # ``os.kill(pid, 0)`` is NOT a no-op on Windows
1263
+ # (bpo-14484). Use the cross-platform check.
1264
+ from gateway.status import _pid_exists
1265
+ if not _pid_exists(pid):
1266
+ continue # process already exited — nothing to do
1267
+ _orphan_stdio_pids.add(pid)
1268
+
1269
+ async def _run_http(self, config: dict):
1270
+ """Run the server using HTTP/StreamableHTTP transport."""
1271
+ if not _MCP_HTTP_AVAILABLE:
1272
+ raise ImportError(
1273
+ f"MCP server '{self.name}' requires HTTP transport but "
1274
+ "mcp.client.streamable_http is not available. "
1275
+ "Upgrade the mcp package to get HTTP support."
1276
+ )
1277
+
1278
+ url = config["url"]
1279
+ headers = dict(config.get("headers") or {})
1280
+ # Some MCP servers require MCP-Protocol-Version on the initial
1281
+ # initialize request and reject session-less POSTs otherwise.
1282
+ # Seed it as a client-level default, but treat user overrides as
1283
+ # case-insensitive so conventional casing is preserved.
1284
+ if not any(key.lower() == "mcp-protocol-version" for key in headers):
1285
+ headers["mcp-protocol-version"] = LATEST_PROTOCOL_VERSION
1286
+ connect_timeout = config.get("connect_timeout", _DEFAULT_CONNECT_TIMEOUT)
1287
+ ssl_verify = config.get("ssl_verify", True)
1288
+
1289
+ # OAuth 2.1 PKCE: route through the central MCPOAuthManager so the
1290
+ # same provider instance is reused across reconnects, pre-flow
1291
+ # disk-watch is active, and config-time CLI code paths share state.
1292
+ # If OAuth setup fails (e.g. non-interactive env without cached
1293
+ # tokens), re-raise so this server is reported as failed without
1294
+ # blocking other MCP servers from connecting.
1295
+ _oauth_auth = None
1296
+ if self._auth_type == "oauth":
1297
+ try:
1298
+ from tools.mcp_oauth_manager import get_manager
1299
+ _oauth_auth = get_manager().get_or_build_provider(
1300
+ self.name, url, config.get("oauth"),
1301
+ )
1302
+ except Exception as exc:
1303
+ logger.warning("MCP OAuth setup failed for '%s': %s", self.name, exc)
1304
+ raise
1305
+
1306
+ sampling_kwargs = self._sampling.session_kwargs() if self._sampling else {}
1307
+ if _MCP_NOTIFICATION_TYPES and _MCP_MESSAGE_HANDLER_SUPPORTED:
1308
+ sampling_kwargs["message_handler"] = self._make_message_handler()
1309
+
1310
+ # SSE transport (for MCP servers that implement the SSE transport protocol
1311
+ # rather than Streamable HTTP). Configure with ``transport: sse`` in the
1312
+ # mcp_servers entry in config.yaml.
1313
+ if config.get("transport") == "sse":
1314
+ if sse_client is None:
1315
+ raise ImportError(
1316
+ f"MCP server '{self.name}' requires SSE transport but "
1317
+ "mcp.client.sse.sse_client is not available. "
1318
+ "Upgrade the mcp package to get SSE support."
1319
+ )
1320
+ # sse_read_timeout governs how long sse_client will wait between
1321
+ # events on the SSE stream. Using the tool_timeout (default 60s)
1322
+ # here is wrong: SSE servers commonly hold the stream idle for
1323
+ # minutes between events, so a 60s read timeout drops the
1324
+ # connection after the first slow stretch. 300s matches the
1325
+ # Streamable HTTP code path's httpx read timeout below. Original
1326
+ # observation from @amiller in PR #5981 (Router Teamwork,
1327
+ # Supermemory on Cloudflare Workers idle-disconnect at ~60s).
1328
+ _sse_kwargs: dict = {
1329
+ "url": url,
1330
+ "headers": headers or None,
1331
+ "timeout": float(connect_timeout),
1332
+ "sse_read_timeout": 300.0,
1333
+ }
1334
+ if _oauth_auth is not None:
1335
+ # Pass OAuth auth through to sse_client so SSE MCP servers
1336
+ # behind OAuth 2.1 PKCE work. Previously built but never
1337
+ # forwarded — SSE OAuth would silently fail with 401s.
1338
+ _sse_kwargs["auth"] = _oauth_auth
1339
+ async with sse_client(**_sse_kwargs) as (read_stream, write_stream):
1340
+ async with ClientSession(
1341
+ read_stream, write_stream, **sampling_kwargs
1342
+ ) as session:
1343
+ self.initialize_result = await session.initialize()
1344
+ self.session = session
1345
+ await self._discover_tools()
1346
+ self._ready.set()
1347
+ reason = await self._wait_for_lifecycle_event()
1348
+ if reason == "reconnect":
1349
+ logger.info(
1350
+ "MCP server '%s': reconnect requested — "
1351
+ "tearing down SSE session", self.name,
1352
+ )
1353
+ return
1354
+
1355
+ if _MCP_NEW_HTTP:
1356
+ # New API (mcp >= 1.24.0): build an explicit httpx.AsyncClient
1357
+ # matching the SDK's own create_mcp_http_client defaults.
1358
+ import httpx
1359
+
1360
+ _original_url = httpx.URL(url)
1361
+
1362
+ async def _strip_auth_on_cross_origin_redirect(response):
1363
+ """Strip Authorization headers when redirected to a different origin."""
1364
+ if response.is_redirect and response.next_request:
1365
+ target = response.next_request.url
1366
+ if (target.scheme, target.host, target.port) != (
1367
+ _original_url.scheme, _original_url.host, _original_url.port,
1368
+ ):
1369
+ response.next_request.headers.pop("authorization", None)
1370
+ response.next_request.headers.pop("Authorization", None)
1371
+
1372
+ client_kwargs: dict = {
1373
+ "follow_redirects": True,
1374
+ "timeout": httpx.Timeout(float(connect_timeout), read=300.0),
1375
+ "verify": ssl_verify,
1376
+ "event_hooks": {"response": [_strip_auth_on_cross_origin_redirect]},
1377
+ }
1378
+ if headers:
1379
+ client_kwargs["headers"] = headers
1380
+ if _oauth_auth is not None:
1381
+ client_kwargs["auth"] = _oauth_auth
1382
+
1383
+ # Caller owns the client lifecycle — the SDK skips cleanup when
1384
+ # http_client is provided, so we wrap in async-with.
1385
+ async with httpx.AsyncClient(**client_kwargs) as http_client:
1386
+ async with streamable_http_client(url, http_client=http_client) as (
1387
+ read_stream, write_stream, _get_session_id,
1388
+ ):
1389
+ async with ClientSession(read_stream, write_stream, **sampling_kwargs) as session:
1390
+ self.initialize_result = await session.initialize()
1391
+ self.session = session
1392
+ await self._discover_tools()
1393
+ self._ready.set()
1394
+ reason = await self._wait_for_lifecycle_event()
1395
+ if reason == "reconnect":
1396
+ logger.info(
1397
+ "MCP server '%s': reconnect requested — "
1398
+ "tearing down HTTP session", self.name,
1399
+ )
1400
+ else:
1401
+ # Deprecated API (mcp < 1.24.0): manages httpx client internally.
1402
+ _http_kwargs: dict = {
1403
+ "headers": headers,
1404
+ "timeout": float(connect_timeout),
1405
+ "verify": ssl_verify,
1406
+ }
1407
+ if _oauth_auth is not None:
1408
+ _http_kwargs["auth"] = _oauth_auth
1409
+ async with streamablehttp_client(url, **_http_kwargs) as (
1410
+ read_stream, write_stream, _get_session_id,
1411
+ ):
1412
+ async with ClientSession(read_stream, write_stream, **sampling_kwargs) as session:
1413
+ self.initialize_result = await session.initialize()
1414
+ self.session = session
1415
+ await self._discover_tools()
1416
+ self._ready.set()
1417
+ reason = await self._wait_for_lifecycle_event()
1418
+ if reason == "reconnect":
1419
+ logger.info(
1420
+ "MCP server '%s': reconnect requested — "
1421
+ "tearing down legacy HTTP session", self.name,
1422
+ )
1423
+
1424
+ async def _discover_tools(self):
1425
+ """Discover tools from the connected session."""
1426
+ if self.session is None:
1427
+ return
1428
+ async with self._rpc_lock:
1429
+ tools_result = await self.session.list_tools()
1430
+ self._tools = (
1431
+ tools_result.tools
1432
+ if hasattr(tools_result, "tools")
1433
+ else []
1434
+ )
1435
+
1436
+ async def run(self, config: dict):
1437
+ """Long-lived coroutine: connect, discover tools, wait, disconnect.
1438
+
1439
+ Includes automatic reconnection with exponential backoff if the
1440
+ connection drops unexpectedly (unless shutdown was requested).
1441
+ """
1442
+ self._config = config
1443
+ self.tool_timeout = config.get("timeout", _DEFAULT_TOOL_TIMEOUT)
1444
+ self._auth_type = (config.get("auth") or "").lower().strip()
1445
+
1446
+ # Set up sampling handler if enabled and SDK types are available
1447
+ sampling_config = config.get("sampling", {})
1448
+ if sampling_config.get("enabled", True) and _MCP_SAMPLING_TYPES:
1449
+ self._sampling = SamplingHandler(self.name, sampling_config)
1450
+ else:
1451
+ self._sampling = None
1452
+
1453
+ # Validate: warn if both url and command are present
1454
+ if "url" in config and "command" in config:
1455
+ logger.warning(
1456
+ "MCP server '%s' has both 'url' and 'command' in config. "
1457
+ "Using HTTP transport ('url'). Remove 'command' to silence "
1458
+ "this warning.",
1459
+ self.name,
1460
+ )
1461
+ retries = 0
1462
+ initial_retries = 0
1463
+ backoff = 1.0
1464
+
1465
+ while True:
1466
+ try:
1467
+ if self._is_http():
1468
+ await self._run_http(config)
1469
+ else:
1470
+ await self._run_stdio(config)
1471
+ # Transport returned cleanly. Two cases:
1472
+ # - _shutdown_event was set: exit the run loop entirely.
1473
+ # - _reconnect_event was set (auth recovery): loop back and
1474
+ # rebuild the MCP session with fresh credentials. Do NOT
1475
+ # touch the retry counters — this is not a failure.
1476
+ if self._shutdown_event.is_set():
1477
+ break
1478
+ logger.info(
1479
+ "MCP server '%s': reconnecting (OAuth recovery or "
1480
+ "manual refresh)",
1481
+ self.name,
1482
+ )
1483
+ # Reset the session reference; _run_http/_run_stdio will
1484
+ # repopulate it on successful re-entry.
1485
+ self.session = None
1486
+ # Keep _ready set across reconnects so tool handlers can
1487
+ # still detect a transient in-flight state — it'll be
1488
+ # re-set after the fresh session initializes.
1489
+ continue
1490
+ except asyncio.CancelledError:
1491
+ # Task was cancelled (shutdown, gateway restart, explicit
1492
+ # task.cancel()). Don't treat this as a connection failure —
1493
+ # CancelledError inherits from BaseException (not Exception)
1494
+ # in Python 3.11+, so the broad ``except Exception`` below
1495
+ # would NOT catch it; we'd silently exit the reconnect loop
1496
+ # and the MCP server would stay dead until Hermes is fully
1497
+ # restarted. Re-raise so the task's cancellation propagates
1498
+ # correctly to asyncio's task machinery and ``shutdown()``'s
1499
+ # ``await self._task`` completes. See #9930.
1500
+ self.session = None
1501
+ raise
1502
+ except Exception as exc:
1503
+ self.session = None
1504
+
1505
+ # If this is the first connection attempt, retry with backoff
1506
+ # before giving up. A transient DNS/network blip at startup
1507
+ # should not permanently kill the server.
1508
+ # (Ported from Kilo Code's MCP resilience fix.)
1509
+ if not self._ready.is_set():
1510
+ if _is_auth_error(exc):
1511
+ logger.warning(
1512
+ "MCP server '%s' failed initial OAuth authentication, "
1513
+ "not retrying automatically: %s",
1514
+ self.name, exc,
1515
+ )
1516
+ self._error = exc
1517
+ self._ready.set()
1518
+ return
1519
+
1520
+ initial_retries += 1
1521
+ if initial_retries > _MAX_INITIAL_CONNECT_RETRIES:
1522
+ logger.warning(
1523
+ "MCP server '%s' failed initial connection after "
1524
+ "%d attempts, giving up: %s",
1525
+ self.name, _MAX_INITIAL_CONNECT_RETRIES, exc,
1526
+ )
1527
+ self._error = exc
1528
+ self._ready.set()
1529
+ return
1530
+
1531
+ logger.warning(
1532
+ "MCP server '%s' initial connection failed "
1533
+ "(attempt %d/%d), retrying in %.0fs: %s",
1534
+ self.name, initial_retries,
1535
+ _MAX_INITIAL_CONNECT_RETRIES, backoff, exc,
1536
+ )
1537
+ await asyncio.sleep(backoff)
1538
+ backoff = min(backoff * 2, _MAX_BACKOFF_SECONDS)
1539
+
1540
+ # Check if shutdown was requested during the sleep
1541
+ if self._shutdown_event.is_set():
1542
+ self._error = exc
1543
+ self._ready.set()
1544
+ return
1545
+ continue
1546
+
1547
+ # If shutdown was requested, don't reconnect
1548
+ if self._shutdown_event.is_set():
1549
+ logger.debug(
1550
+ "MCP server '%s' disconnected during shutdown: %s",
1551
+ self.name, exc,
1552
+ )
1553
+ return
1554
+
1555
+ retries += 1
1556
+ if retries > _MAX_RECONNECT_RETRIES:
1557
+ logger.warning(
1558
+ "MCP server '%s' failed after %d reconnection attempts, "
1559
+ "giving up: %s",
1560
+ self.name, _MAX_RECONNECT_RETRIES, exc,
1561
+ )
1562
+ return
1563
+
1564
+ logger.warning(
1565
+ "MCP server '%s' connection lost (attempt %d/%d), "
1566
+ "reconnecting in %.0fs: %s",
1567
+ self.name, retries, _MAX_RECONNECT_RETRIES,
1568
+ backoff, exc,
1569
+ )
1570
+ await asyncio.sleep(backoff)
1571
+ backoff = min(backoff * 2, _MAX_BACKOFF_SECONDS)
1572
+
1573
+ # Check again after sleeping
1574
+ if self._shutdown_event.is_set():
1575
+ return
1576
+ finally:
1577
+ self.session = None
1578
+
1579
+ async def start(self, config: dict):
1580
+ """Create the background Task and wait until ready (or failed)."""
1581
+ self._task = asyncio.ensure_future(self.run(config))
1582
+ await self._ready.wait()
1583
+ if self._error:
1584
+ raise self._error
1585
+
1586
+ async def shutdown(self):
1587
+ """Signal the Task to exit and wait for clean resource teardown."""
1588
+ from tools.registry import registry
1589
+
1590
+ self._shutdown_event.set()
1591
+ # Defensive: if _wait_for_lifecycle_event is blocking, we need ANY
1592
+ # event to unblock it. _shutdown_event alone is sufficient (the
1593
+ # helper checks shutdown first), but setting reconnect too ensures
1594
+ # there's no race where the helper misses the shutdown flag after
1595
+ # returning "reconnect".
1596
+ self._reconnect_event.set()
1597
+ if self._task and not self._task.done():
1598
+ try:
1599
+ await asyncio.wait_for(self._task, timeout=10)
1600
+ except asyncio.TimeoutError:
1601
+ logger.warning(
1602
+ "MCP server '%s' shutdown timed out, cancelling task",
1603
+ self.name,
1604
+ )
1605
+ self._task.cancel()
1606
+ try:
1607
+ await self._task
1608
+ except asyncio.CancelledError:
1609
+ pass
1610
+ if self._pending_refresh_tasks:
1611
+ for task in list(self._pending_refresh_tasks):
1612
+ task.cancel()
1613
+ await asyncio.gather(*self._pending_refresh_tasks, return_exceptions=True)
1614
+ self._pending_refresh_tasks.clear()
1615
+ for tool_name in list(getattr(self, "_registered_tool_names", [])):
1616
+ registry.deregister(tool_name)
1617
+ self._registered_tool_names = []
1618
+ self.session = None
1619
+
1620
+
1621
+ # ---------------------------------------------------------------------------
1622
+ # Module-level state
1623
+ # ---------------------------------------------------------------------------
1624
+
1625
+ _servers: Dict[str, MCPServerTask] = {}
1626
+
1627
+ # Circuit breaker: consecutive error counts per server. After
1628
+ # _CIRCUIT_BREAKER_THRESHOLD consecutive failures, the handler returns
1629
+ # a "server unreachable" message that tells the model to stop retrying,
1630
+ # preventing the 90-iteration burn loop described in #10447.
1631
+ #
1632
+ # State machine:
1633
+ # closed — error count below threshold; all calls go through.
1634
+ # open — threshold reached; calls short-circuit until the
1635
+ # cooldown elapses.
1636
+ # half-open — cooldown elapsed; the next call is a probe that
1637
+ # actually hits the session. Probe success → closed.
1638
+ # Probe failure → reopens (cooldown re-armed).
1639
+ #
1640
+ # ``_server_breaker_opened_at`` records the monotonic timestamp when
1641
+ # the breaker most recently transitioned into the open state. Use the
1642
+ # ``_bump_server_error`` / ``_reset_server_error`` helpers to mutate
1643
+ # this state — they keep the count and timestamp in sync.
1644
+ _server_error_counts: Dict[str, int] = {}
1645
+ _server_breaker_opened_at: Dict[str, float] = {}
1646
+ _CIRCUIT_BREAKER_THRESHOLD = 3
1647
+ _CIRCUIT_BREAKER_COOLDOWN_SEC = 60.0
1648
+
1649
+
1650
+ def _bump_server_error(server_name: str) -> None:
1651
+ """Increment the consecutive-failure count for ``server_name``.
1652
+
1653
+ When the count crosses :data:`_CIRCUIT_BREAKER_THRESHOLD`, stamp the
1654
+ breaker-open timestamp so the cooldown clock starts (or re-starts,
1655
+ for probe failures in the half-open state).
1656
+ """
1657
+ n = _server_error_counts.get(server_name, 0) + 1
1658
+ _server_error_counts[server_name] = n
1659
+ if n >= _CIRCUIT_BREAKER_THRESHOLD:
1660
+ _server_breaker_opened_at[server_name] = time.monotonic()
1661
+
1662
+
1663
+ def _reset_server_error(server_name: str) -> None:
1664
+ """Fully close the breaker for ``server_name``.
1665
+
1666
+ Clears both the failure count and the breaker-open timestamp. Call
1667
+ this on any unambiguous success signal (successful tool call,
1668
+ successful reconnect, manual /mcp refresh).
1669
+ """
1670
+ _server_error_counts[server_name] = 0
1671
+ _server_breaker_opened_at.pop(server_name, None)
1672
+
1673
+ # ---------------------------------------------------------------------------
1674
+ # Auth-failure detection helpers (Task 6 of MCP OAuth consolidation)
1675
+ # ---------------------------------------------------------------------------
1676
+
1677
+ # Cached tuple of auth-related exception types. Lazy so this module
1678
+ # imports cleanly when the MCP SDK OAuth module is missing.
1679
+ _AUTH_ERROR_TYPES: tuple = ()
1680
+
1681
+
1682
+ def _get_auth_error_types() -> tuple:
1683
+ """Return a tuple of exception types that indicate MCP OAuth failure.
1684
+
1685
+ Cached after first call. Includes:
1686
+ - ``mcp.client.auth.OAuthFlowError`` / ``OAuthTokenError`` — raised by
1687
+ the SDK's auth flow when discovery, refresh, or full re-auth fails.
1688
+ - ``mcp.client.auth.UnauthorizedError`` (older MCP SDKs) — kept as an
1689
+ optional import for forward/backward compatibility.
1690
+ - ``tools.mcp_oauth.OAuthNonInteractiveError`` — raised by our callback
1691
+ handler when no user is present to complete a browser flow.
1692
+ - ``httpx.HTTPStatusError`` — caller must additionally check
1693
+ ``status_code == 401`` via :func:`_is_auth_error`.
1694
+ """
1695
+ global _AUTH_ERROR_TYPES
1696
+ if _AUTH_ERROR_TYPES:
1697
+ return _AUTH_ERROR_TYPES
1698
+ types: list = []
1699
+ try:
1700
+ from mcp.client.auth import OAuthFlowError, OAuthTokenError
1701
+ types.extend([OAuthFlowError, OAuthTokenError])
1702
+ except ImportError:
1703
+ pass
1704
+ try:
1705
+ # Older MCP SDK variants exported this
1706
+ from mcp.client.auth import UnauthorizedError # type: ignore
1707
+ types.append(UnauthorizedError)
1708
+ except ImportError:
1709
+ pass
1710
+ try:
1711
+ from tools.mcp_oauth import OAuthNonInteractiveError
1712
+ types.append(OAuthNonInteractiveError)
1713
+ except ImportError:
1714
+ pass
1715
+ try:
1716
+ import httpx
1717
+ types.append(httpx.HTTPStatusError)
1718
+ except ImportError:
1719
+ pass
1720
+ _AUTH_ERROR_TYPES = tuple(types)
1721
+ return _AUTH_ERROR_TYPES
1722
+
1723
+
1724
+ def _is_auth_error(exc: BaseException) -> bool:
1725
+ """Return True if ``exc`` indicates an MCP OAuth failure.
1726
+
1727
+ ``httpx.HTTPStatusError`` is only treated as auth-related when the
1728
+ response status code is 401. Other HTTP errors fall through to the
1729
+ generic error path in the tool handlers.
1730
+ """
1731
+ types = _get_auth_error_types()
1732
+ if not types or not isinstance(exc, types):
1733
+ return False
1734
+ try:
1735
+ import httpx
1736
+ if isinstance(exc, httpx.HTTPStatusError):
1737
+ return getattr(exc.response, "status_code", None) == 401
1738
+ except ImportError:
1739
+ pass
1740
+ return True
1741
+
1742
+
1743
+ def _handle_auth_error_and_retry(
1744
+ server_name: str,
1745
+ exc: BaseException,
1746
+ retry_call,
1747
+ op_description: str,
1748
+ ):
1749
+ """Attempt auth recovery and one retry; return None to fall through.
1750
+
1751
+ Called by the 5 MCP tool handlers when ``session.<op>()`` raises an
1752
+ auth-related exception. Workflow:
1753
+
1754
+ 1. Ask :class:`tools.mcp_oauth_manager.MCPOAuthManager.handle_401` if
1755
+ recovery is viable (i.e., disk has fresh tokens, or the SDK can
1756
+ refresh in-place).
1757
+ 2. If yes, set the server's ``_reconnect_event`` so the server task
1758
+ tears down the current MCP session and rebuilds it with fresh
1759
+ credentials. Wait briefly for ``_ready`` to re-fire.
1760
+ 3. Retry the operation once. Return the retry result if it produced
1761
+ a non-error JSON payload. Otherwise return the ``needs_reauth``
1762
+ error dict so the model stops hallucinating manual refresh.
1763
+ 4. Return None if ``exc`` is not an auth error, signalling the
1764
+ caller to use the generic error path.
1765
+
1766
+ Args:
1767
+ server_name: Name of the MCP server that raised.
1768
+ exc: The exception from the failed tool call.
1769
+ retry_call: Zero-arg callable that re-runs the tool call, returning
1770
+ the same JSON string format as the handler.
1771
+ op_description: Human-readable name of the operation (for logs).
1772
+
1773
+ Returns:
1774
+ A JSON string if auth recovery was attempted, or None to fall
1775
+ through to the caller's generic error path.
1776
+ """
1777
+ if not _is_auth_error(exc):
1778
+ return None
1779
+
1780
+ from tools.mcp_oauth_manager import get_manager
1781
+ manager = get_manager()
1782
+
1783
+ async def _recover():
1784
+ return await manager.handle_401(server_name, None)
1785
+
1786
+ try:
1787
+ recovered = _run_on_mcp_loop(_recover, timeout=10)
1788
+ except Exception as rec_exc:
1789
+ logger.warning(
1790
+ "MCP OAuth '%s': recovery attempt failed: %s",
1791
+ server_name, rec_exc,
1792
+ )
1793
+ recovered = False
1794
+
1795
+ if recovered:
1796
+ with _lock:
1797
+ srv = _servers.get(server_name)
1798
+ if srv is not None and hasattr(srv, "_reconnect_event"):
1799
+ loop = _mcp_loop
1800
+ if loop is not None and loop.is_running():
1801
+ loop.call_soon_threadsafe(srv._reconnect_event.set)
1802
+ # Wait briefly for the session to come back ready. Bounded
1803
+ # so that a stuck reconnect falls through to the error
1804
+ # path rather than hanging the caller.
1805
+ deadline = time.monotonic() + 15
1806
+ while time.monotonic() < deadline:
1807
+ if srv.session is not None and srv._ready.is_set():
1808
+ break
1809
+ time.sleep(0.25)
1810
+
1811
+ # A successful OAuth recovery is independent evidence that the
1812
+ # server is viable again, so close the circuit breaker here —
1813
+ # not only on retry success. Without this, a reconnect
1814
+ # followed by a failing retry would leave the breaker pinned
1815
+ # above threshold forever (the retry-exception branch below
1816
+ # bumps the count again). The post-reset retry still goes
1817
+ # through _bump_server_error on failure, so a genuinely broken
1818
+ # server will re-trip the breaker as normal.
1819
+ _reset_server_error(server_name)
1820
+
1821
+ try:
1822
+ result = retry_call()
1823
+ try:
1824
+ parsed = json.loads(result)
1825
+ if "error" not in parsed:
1826
+ _reset_server_error(server_name)
1827
+ return result
1828
+ except (json.JSONDecodeError, TypeError):
1829
+ _reset_server_error(server_name)
1830
+ return result
1831
+ except Exception as retry_exc:
1832
+ logger.warning(
1833
+ "MCP %s/%s retry after auth recovery failed: %s",
1834
+ server_name, op_description, retry_exc,
1835
+ )
1836
+
1837
+ # No recovery available, or retry also failed: surface a structured
1838
+ # needs_reauth error. Bumps the circuit breaker so the model stops
1839
+ # retrying the tool.
1840
+ _bump_server_error(server_name)
1841
+ return json.dumps({
1842
+ "error": (
1843
+ f"MCP server '{server_name}' requires re-authentication. "
1844
+ f"Run `hermes mcp login {server_name}` (or delete the tokens "
1845
+ f"file under ~/.hermes/mcp-tokens/ and restart). Do NOT retry "
1846
+ f"this tool — ask the user to re-authenticate."
1847
+ ),
1848
+ "needs_reauth": True,
1849
+ "server": server_name,
1850
+ }, ensure_ascii=False)
1851
+
1852
+
1853
+ # Substrings (lower-cased match) that indicate the MCP server rejected
1854
+ # the request because its server-side transport session expired /
1855
+ # was garbage-collected. The caller's OAuth token is still valid —
1856
+ # only the transport-layer session state needs rebuilding. See #13383.
1857
+ _SESSION_EXPIRED_MARKERS: tuple = (
1858
+ "invalid or expired session",
1859
+ "expired session",
1860
+ "session expired",
1861
+ "session not found",
1862
+ "unknown session",
1863
+ "session terminated",
1864
+ "closedresourceerror",
1865
+ "closed resource",
1866
+ "transport is closed",
1867
+ "connection closed",
1868
+ "broken pipe",
1869
+ "end of file",
1870
+ )
1871
+
1872
+
1873
+ def _is_session_expired_error(exc: BaseException) -> bool:
1874
+ """Return True if ``exc`` looks like an MCP transport session expiry.
1875
+
1876
+ Streamable HTTP MCP servers may garbage-collect server-side session
1877
+ state while the OAuth token remains valid — idle TTL, server
1878
+ restart, horizontal-scaling pod rotation, etc. The SDK surfaces
1879
+ this as a JSON-RPC error whose message contains phrases like
1880
+ ``"Invalid or expired session"``. This class of failure is
1881
+ distinct from :func:`_is_auth_error`: re-running the OAuth refresh
1882
+ flow would be pointless because the access token is fine. What's
1883
+ needed is a transport reconnect — tear down and rebuild the
1884
+ ``streamablehttp_client`` + ``ClientSession`` pair, which is
1885
+ exactly what ``MCPServerTask._reconnect_event`` triggers.
1886
+ """
1887
+ if isinstance(exc, InterruptedError):
1888
+ return False
1889
+ # Exception messages vary across SDK versions + server
1890
+ # implementations, so match on a small allow-list of stable
1891
+ # substrings rather than exception type. Kept narrow to avoid
1892
+ # false positives on unrelated server errors.
1893
+ msg = str(exc).lower()
1894
+ if not msg:
1895
+ return False
1896
+ return any(marker in msg for marker in _SESSION_EXPIRED_MARKERS)
1897
+
1898
+
1899
+ def _handle_session_expired_and_retry(
1900
+ server_name: str,
1901
+ exc: BaseException,
1902
+ retry_call,
1903
+ op_description: str,
1904
+ ):
1905
+ """Trigger a transport reconnect and retry once on session expiry.
1906
+
1907
+ Unlike :func:`_handle_auth_error_and_retry`, this does **not** call
1908
+ the OAuth manager's ``handle_401`` — the access token is still
1909
+ valid, only the server-side session state is stale. Setting
1910
+ ``_reconnect_event`` causes the server task's lifecycle loop to
1911
+ tear down the current ``streamablehttp_client`` + ``ClientSession``
1912
+ and rebuild them, reusing the existing OAuth provider instance.
1913
+ See #13383.
1914
+
1915
+ Args:
1916
+ server_name: Name of the MCP server that raised.
1917
+ exc: The exception from the failed call.
1918
+ retry_call: Zero-arg callable that re-runs the operation,
1919
+ returning the same JSON string format as the handler.
1920
+ op_description: Human-readable name of the operation (logs).
1921
+
1922
+ Returns:
1923
+ A JSON string if reconnect + retry was attempted and produced
1924
+ a response, or ``None`` to fall through to the caller's
1925
+ generic error path (not a session-expired error, no server
1926
+ record, reconnect didn't ready in time, or retry also failed).
1927
+ """
1928
+ if not _is_session_expired_error(exc):
1929
+ return None
1930
+
1931
+ with _lock:
1932
+ srv = _servers.get(server_name)
1933
+ if srv is None or not hasattr(srv, "_reconnect_event"):
1934
+ return None
1935
+
1936
+ loop = _mcp_loop
1937
+ if loop is None or not loop.is_running():
1938
+ return None
1939
+
1940
+ logger.info(
1941
+ "MCP server '%s': %s failed with session-expired error (%s); "
1942
+ "signalling transport reconnect and retrying once.",
1943
+ server_name, op_description, exc,
1944
+ )
1945
+
1946
+ # Trigger the same reconnect mechanism the OAuth recovery path
1947
+ # uses, then wait briefly for the new session to come back ready.
1948
+ loop.call_soon_threadsafe(srv._reconnect_event.set)
1949
+ deadline = time.monotonic() + 15
1950
+ ready = False
1951
+ while time.monotonic() < deadline:
1952
+ if srv.session is not None and srv._ready.is_set():
1953
+ ready = True
1954
+ break
1955
+ time.sleep(0.25)
1956
+ if not ready:
1957
+ logger.warning(
1958
+ "MCP server '%s': reconnect did not ready within 15s after "
1959
+ "session-expired error; falling through to error response.",
1960
+ server_name,
1961
+ )
1962
+ return None
1963
+
1964
+ try:
1965
+ result = retry_call()
1966
+ try:
1967
+ parsed = json.loads(result)
1968
+ if "error" not in parsed:
1969
+ _server_error_counts[server_name] = 0
1970
+ return result
1971
+ except (json.JSONDecodeError, TypeError):
1972
+ _server_error_counts[server_name] = 0
1973
+ return result
1974
+ except Exception as retry_exc:
1975
+ logger.warning(
1976
+ "MCP %s/%s retry after session reconnect failed: %s",
1977
+ server_name, op_description, retry_exc,
1978
+ )
1979
+ return None
1980
+
1981
+
1982
+ # Sanitized server names whose ``supports_parallel_tool_calls`` config is True.
1983
+ # Populated during ``register_mcp_servers()`` and queried by
1984
+ # ``is_mcp_tool_parallel_safe()`` for the parallel-execution check in run_agent.
1985
+ _parallel_safe_servers: set = set()
1986
+
1987
+ # Dedicated event loop running in a background daemon thread.
1988
+ _mcp_loop: Optional[asyncio.AbstractEventLoop] = None
1989
+ _mcp_thread: Optional[threading.Thread] = None
1990
+
1991
+ # Protects _mcp_loop, _mcp_thread, _servers, _parallel_safe_servers, and _stdio_pids.
1992
+ _lock = threading.Lock()
1993
+
1994
+ # PIDs of stdio MCP server subprocesses. Tracked so we can force-kill
1995
+ # them on shutdown if the graceful cleanup (SDK context-manager teardown)
1996
+ # fails or times out. PIDs are added after connection and removed on
1997
+ # normal server shutdown.
1998
+ _stdio_pids: Dict[int, str] = {} # pid -> server_name
1999
+
2000
+ # PIDs that survived their session context exit (SDK teardown failed to
2001
+ # terminate them). These are detected in _run_stdio's finally block and
2002
+ # can be cleaned up asynchronously by _kill_orphaned_mcp_children().
2003
+ # Separate from _stdio_pids so cleanup sweeps never race with active
2004
+ # sessions (e.g. concurrent cron jobs or live user chats).
2005
+ _orphan_stdio_pids: set = set()
2006
+
2007
+
2008
+ def _snapshot_child_pids() -> set:
2009
+ """Return a set of current child process PIDs.
2010
+
2011
+ Uses /proc on Linux, falls back to psutil, then empty set.
2012
+ Used by _run_stdio to identify the subprocess spawned by stdio_client.
2013
+ """
2014
+ my_pid = os.getpid()
2015
+
2016
+ # Linux: read from /proc
2017
+ try:
2018
+ children_path = f"/proc/{my_pid}/task/{my_pid}/children"
2019
+ with open(children_path, encoding="utf-8") as f:
2020
+ return {int(p) for p in f.read().split() if p.strip()}
2021
+ except (FileNotFoundError, OSError, ValueError):
2022
+ pass
2023
+
2024
+ # Fallback: psutil
2025
+ try:
2026
+ import psutil
2027
+ return {c.pid for c in psutil.Process(my_pid).children()}
2028
+ except Exception:
2029
+ pass
2030
+
2031
+ return set()
2032
+
2033
+
2034
+ def _mcp_loop_exception_handler(loop, context):
2035
+ """Suppress benign 'Event loop is closed' noise during shutdown.
2036
+
2037
+ When the MCP event loop is stopped and closed, httpx/httpcore async
2038
+ transports may fire __del__ finalizers that call call_soon() on the
2039
+ dead loop. asyncio catches that RuntimeError and routes it here.
2040
+ We silence it because the connection is being torn down anyway; all
2041
+ other exceptions are forwarded to the default handler.
2042
+ """
2043
+ exc = context.get("exception")
2044
+ if isinstance(exc, RuntimeError) and "Event loop is closed" in str(exc):
2045
+ return # benign shutdown race — suppress
2046
+ loop.default_exception_handler(context)
2047
+
2048
+
2049
+ def _ensure_mcp_loop():
2050
+ """Start the background event loop thread if not already running."""
2051
+ global _mcp_loop, _mcp_thread
2052
+ with _lock:
2053
+ if _mcp_loop is not None and _mcp_loop.is_running():
2054
+ return
2055
+ _mcp_loop = asyncio.new_event_loop()
2056
+ _mcp_loop.set_exception_handler(_mcp_loop_exception_handler)
2057
+ _mcp_thread = threading.Thread(
2058
+ target=_mcp_loop.run_forever,
2059
+ name="mcp-event-loop",
2060
+ daemon=True,
2061
+ )
2062
+ _mcp_thread.start()
2063
+
2064
+
2065
+ def _run_on_mcp_loop(coro_or_factory, timeout: float = 30):
2066
+ """Schedule a coroutine on the MCP event loop and block until done.
2067
+
2068
+ Accepts either a coroutine object or a zero-arg callable that returns one.
2069
+ Callers can pass a factory to avoid constructing coroutine objects when
2070
+ the MCP loop is unavailable (which would otherwise leak the coroutine
2071
+ frame and emit ``"coroutine was never awaited"`` warnings).
2072
+
2073
+ Poll in short intervals so the calling agent thread can honor user
2074
+ interrupts while the MCP work is still running on the background loop.
2075
+ """
2076
+ from tools.interrupt import is_interrupted
2077
+ from agent.async_utils import safe_schedule_threadsafe
2078
+
2079
+ with _lock:
2080
+ loop = _mcp_loop
2081
+ if loop is None or not loop.is_running():
2082
+ if asyncio.iscoroutine(coro_or_factory):
2083
+ coro_or_factory.close()
2084
+ raise RuntimeError("MCP event loop is not running")
2085
+
2086
+ coro = coro_or_factory() if callable(coro_or_factory) else coro_or_factory
2087
+ future = safe_schedule_threadsafe(
2088
+ coro, loop,
2089
+ logger=logger,
2090
+ log_message="MCP scheduling failed",
2091
+ )
2092
+ if future is None:
2093
+ raise RuntimeError("MCP event loop unavailable (failed to schedule)")
2094
+ start_time = time.monotonic()
2095
+ deadline = None if timeout is None else start_time + timeout
2096
+
2097
+ while True:
2098
+ if is_interrupted():
2099
+ future.cancel()
2100
+ raise InterruptedError("User sent a new message")
2101
+
2102
+ wait_timeout = 0.1
2103
+ if deadline is not None:
2104
+ remaining = deadline - time.monotonic()
2105
+ if remaining <= 0:
2106
+ future.cancel()
2107
+ elapsed = time.monotonic() - start_time
2108
+ raise TimeoutError(
2109
+ f"MCP call timed out after {elapsed:.1f}s "
2110
+ f"(configured timeout: {float(timeout):.1f}s)"
2111
+ )
2112
+ wait_timeout = min(wait_timeout, remaining)
2113
+
2114
+ try:
2115
+ return future.result(timeout=wait_timeout)
2116
+ except concurrent.futures.TimeoutError:
2117
+ continue
2118
+
2119
+
2120
+ def _interrupted_call_result() -> str:
2121
+ """Standardized JSON error for a user-interrupted MCP tool call."""
2122
+ return json.dumps({
2123
+ "error": "MCP call interrupted: user sent a new message"
2124
+ }, ensure_ascii=False)
2125
+
2126
+
2127
+ # ---------------------------------------------------------------------------
2128
+ # Config loading
2129
+ # ---------------------------------------------------------------------------
2130
+
2131
+ def _interpolate_env_vars(value):
2132
+ """Recursively resolve ``${VAR}`` placeholders from ``os.environ``."""
2133
+ if isinstance(value, str):
2134
+ def _replace(m):
2135
+ return os.environ.get(m.group(1), m.group(0))
2136
+ return _ENV_VAR_PATTERN.sub(_replace, value)
2137
+ if isinstance(value, dict):
2138
+ return {k: _interpolate_env_vars(v) for k, v in value.items()}
2139
+ if isinstance(value, list):
2140
+ return [_interpolate_env_vars(v) for v in value]
2141
+ return value
2142
+
2143
+
2144
+ def _load_mcp_config() -> Dict[str, dict]:
2145
+ """Read ``mcp_servers`` from the Hermes config file.
2146
+
2147
+ Returns a dict of ``{server_name: server_config}`` or empty dict.
2148
+ Server config can contain either ``command``/``args``/``env`` for stdio
2149
+ transport or ``url``/``headers`` for HTTP transport, plus optional
2150
+ ``timeout``, ``connect_timeout``, and ``auth`` overrides.
2151
+
2152
+ ``${ENV_VAR}`` placeholders in string values are resolved from
2153
+ ``os.environ`` (which includes ``~/.hermes/.env`` loaded at startup).
2154
+ """
2155
+ try:
2156
+ from hermes_cli.config import load_config
2157
+ config = load_config()
2158
+ servers = config.get("mcp_servers")
2159
+ if not servers or not isinstance(servers, dict):
2160
+ return {}
2161
+ # Ensure .env vars are available for interpolation
2162
+ try:
2163
+ from hermes_cli.env_loader import load_hermes_dotenv
2164
+ load_hermes_dotenv()
2165
+ except Exception:
2166
+ pass
2167
+ return {name: _interpolate_env_vars(cfg) for name, cfg in servers.items()}
2168
+ except Exception as exc:
2169
+ logger.debug("Failed to load MCP config: %s", exc)
2170
+ return {}
2171
+
2172
+
2173
+ # ---------------------------------------------------------------------------
2174
+ # Server connection helper
2175
+ # ---------------------------------------------------------------------------
2176
+
2177
+ async def _connect_server(name: str, config: dict) -> MCPServerTask:
2178
+ """Create an MCPServerTask, start it, and return when ready.
2179
+
2180
+ The server Task keeps the connection alive in the background.
2181
+ Call ``server.shutdown()`` (on the same event loop) to tear it down.
2182
+
2183
+ Raises:
2184
+ ValueError: if required config keys are missing.
2185
+ ImportError: if HTTP transport is needed but not available.
2186
+ Exception: on connection or initialization failure.
2187
+ """
2188
+ server = MCPServerTask(name)
2189
+ await server.start(config)
2190
+ return server
2191
+
2192
+
2193
+ # ---------------------------------------------------------------------------
2194
+ # Handler / check-fn factories
2195
+ # ---------------------------------------------------------------------------
2196
+
2197
+ def _make_tool_handler(server_name: str, tool_name: str, tool_timeout: float):
2198
+ """Return a sync handler that calls an MCP tool via the background loop.
2199
+
2200
+ The handler conforms to the registry's dispatch interface:
2201
+ ``handler(args_dict, **kwargs) -> str``
2202
+ """
2203
+
2204
+ def _handler(args: dict, **kwargs) -> str:
2205
+ # Circuit breaker: if this server has failed too many times
2206
+ # consecutively, short-circuit with a clear message so the model
2207
+ # stops retrying and uses alternative approaches (#10447).
2208
+ #
2209
+ # Once the cooldown elapses, the breaker transitions to
2210
+ # half-open: we let the *next* call through as a probe. On
2211
+ # success the success-path below resets the breaker; on
2212
+ # failure the error paths below bump the count again, which
2213
+ # re-stamps the open-time via _bump_server_error (re-arming
2214
+ # the cooldown).
2215
+ if _server_error_counts.get(server_name, 0) >= _CIRCUIT_BREAKER_THRESHOLD:
2216
+ opened_at = _server_breaker_opened_at.get(server_name, 0.0)
2217
+ age = time.monotonic() - opened_at
2218
+ if age < _CIRCUIT_BREAKER_COOLDOWN_SEC:
2219
+ remaining = max(1, int(_CIRCUIT_BREAKER_COOLDOWN_SEC - age))
2220
+ return json.dumps({
2221
+ "error": (
2222
+ f"MCP server '{server_name}' is unreachable after "
2223
+ f"{_server_error_counts[server_name]} consecutive "
2224
+ f"failures. Auto-retry available in ~{remaining}s. "
2225
+ f"Do NOT retry this tool yet — use alternative "
2226
+ f"approaches or ask the user to check the MCP server."
2227
+ )
2228
+ }, ensure_ascii=False)
2229
+ # Cooldown elapsed → fall through as a half-open probe.
2230
+
2231
+ with _lock:
2232
+ server = _servers.get(server_name)
2233
+ if not server or not server.session:
2234
+ _bump_server_error(server_name)
2235
+ return json.dumps({
2236
+ "error": f"MCP server '{server_name}' is not connected"
2237
+ }, ensure_ascii=False)
2238
+
2239
+ async def _call():
2240
+ async with server._rpc_lock:
2241
+ result = await server.session.call_tool(tool_name, arguments=args)
2242
+ # MCP CallToolResult has .content (list of content blocks) and .isError
2243
+ if result.isError:
2244
+ error_text = ""
2245
+ for block in (result.content or []):
2246
+ if hasattr(block, "text"):
2247
+ error_text += block.text
2248
+ return json.dumps({
2249
+ "error": _sanitize_error(
2250
+ error_text or "MCP tool returned an error"
2251
+ )
2252
+ }, ensure_ascii=False)
2253
+
2254
+ # Collect text from content blocks. MCP tool results can also
2255
+ # include ImageContent blocks (screenshot / Blockbench / Playwright
2256
+ # etc.); cache those via the gateway's image-cache helper so they
2257
+ # flow through Hermes' MEDIA: tag convention and out to messaging
2258
+ # adapters that render images natively. Without this, image blocks
2259
+ # were silently dropped and the agent got an empty response.
2260
+ #
2261
+ # Distilled from #17915 (c3115644151) and #10848 (gnanirahulnutakki),
2262
+ # both too stale to cherry-pick. #10848's approach (integrate with
2263
+ # Hermes' MEDIA tag + cache_image_from_bytes) was the cleaner of
2264
+ # the two — plugs into existing infrastructure.
2265
+ parts: List[str] = []
2266
+ for block in (result.content or []):
2267
+ if hasattr(block, "text") and block.text:
2268
+ parts.append(block.text)
2269
+ continue
2270
+ image_tag = _cache_mcp_image_block(block)
2271
+ if image_tag:
2272
+ parts.append(image_tag)
2273
+ text_result = "\n".join(parts) if parts else ""
2274
+
2275
+ # Combine content + structuredContent when both are present.
2276
+ # MCP spec: content is model-oriented (text), structuredContent
2277
+ # is machine-oriented (JSON metadata). For an AI agent, content
2278
+ # is the primary payload; structuredContent supplements it.
2279
+ structured = getattr(result, "structuredContent", None)
2280
+ if structured is not None:
2281
+ if text_result:
2282
+ return json.dumps({
2283
+ "result": text_result,
2284
+ "structuredContent": structured,
2285
+ }, ensure_ascii=False)
2286
+ return json.dumps({"result": structured}, ensure_ascii=False)
2287
+ return json.dumps({"result": text_result}, ensure_ascii=False)
2288
+
2289
+ def _call_once():
2290
+ return _run_on_mcp_loop(_call, timeout=tool_timeout)
2291
+
2292
+ try:
2293
+ result = _call_once()
2294
+ # Check if the MCP tool itself returned an error
2295
+ try:
2296
+ parsed = json.loads(result)
2297
+ if "error" in parsed:
2298
+ _bump_server_error(server_name)
2299
+ else:
2300
+ _reset_server_error(server_name) # success — reset
2301
+ except (json.JSONDecodeError, TypeError):
2302
+ _reset_server_error(server_name) # non-JSON = success
2303
+ return result
2304
+ except InterruptedError:
2305
+ return _interrupted_call_result()
2306
+ except Exception as exc:
2307
+ # Auth-specific recovery path: consult the manager, signal
2308
+ # reconnect if viable, retry once. Returns None to fall
2309
+ # through for non-auth exceptions.
2310
+ recovered = _handle_auth_error_and_retry(
2311
+ server_name, exc, _call_once,
2312
+ f"tools/call {tool_name}",
2313
+ )
2314
+ if recovered is not None:
2315
+ return recovered
2316
+
2317
+ # Transport session expiry (#13383): same reconnect flow
2318
+ # but skips OAuth recovery because the access token is
2319
+ # still valid — only the server-side session is stale.
2320
+ recovered = _handle_session_expired_and_retry(
2321
+ server_name, exc, _call_once,
2322
+ f"tools/call {tool_name}",
2323
+ )
2324
+ if recovered is not None:
2325
+ return recovered
2326
+
2327
+ _bump_server_error(server_name)
2328
+ logger.error(
2329
+ "MCP tool %s/%s call failed: %s",
2330
+ server_name, tool_name, exc,
2331
+ )
2332
+ return json.dumps({
2333
+ "error": _sanitize_error(
2334
+ f"MCP call failed: {type(exc).__name__}: {_exc_str(exc)}"
2335
+ )
2336
+ }, ensure_ascii=False)
2337
+
2338
+ return _handler
2339
+
2340
+
2341
+ def _make_list_resources_handler(server_name: str, tool_timeout: float):
2342
+ """Return a sync handler that lists resources from an MCP server."""
2343
+
2344
+ def _handler(args: dict, **kwargs) -> str:
2345
+ with _lock:
2346
+ server = _servers.get(server_name)
2347
+ if not server or not server.session:
2348
+ return json.dumps({
2349
+ "error": f"MCP server '{server_name}' is not connected"
2350
+ }, ensure_ascii=False)
2351
+
2352
+ async def _call():
2353
+ async with server._rpc_lock:
2354
+ result = await server.session.list_resources()
2355
+ resources = []
2356
+ for r in (result.resources if hasattr(result, "resources") else []):
2357
+ entry = {}
2358
+ if hasattr(r, "uri"):
2359
+ entry["uri"] = str(r.uri)
2360
+ if hasattr(r, "name"):
2361
+ entry["name"] = r.name
2362
+ if hasattr(r, "description") and r.description:
2363
+ entry["description"] = r.description
2364
+ if hasattr(r, "mimeType") and r.mimeType:
2365
+ entry["mimeType"] = r.mimeType
2366
+ resources.append(entry)
2367
+ return json.dumps({"resources": resources}, ensure_ascii=False)
2368
+
2369
+ def _call_once():
2370
+ return _run_on_mcp_loop(_call, timeout=tool_timeout)
2371
+
2372
+ try:
2373
+ return _call_once()
2374
+ except InterruptedError:
2375
+ return _interrupted_call_result()
2376
+ except Exception as exc:
2377
+ recovered = _handle_auth_error_and_retry(
2378
+ server_name, exc, _call_once, "resources/list",
2379
+ )
2380
+ if recovered is not None:
2381
+ return recovered
2382
+ recovered = _handle_session_expired_and_retry(
2383
+ server_name, exc, _call_once, "resources/list",
2384
+ )
2385
+ if recovered is not None:
2386
+ return recovered
2387
+ logger.error(
2388
+ "MCP %s/list_resources failed: %s", server_name, exc,
2389
+ )
2390
+ return json.dumps({
2391
+ "error": _sanitize_error(
2392
+ f"MCP call failed: {type(exc).__name__}: {_exc_str(exc)}"
2393
+ )
2394
+ }, ensure_ascii=False)
2395
+
2396
+ return _handler
2397
+
2398
+
2399
+ def _make_read_resource_handler(server_name: str, tool_timeout: float):
2400
+ """Return a sync handler that reads a resource by URI from an MCP server."""
2401
+
2402
+ def _handler(args: dict, **kwargs) -> str:
2403
+ from tools.registry import tool_error
2404
+
2405
+ with _lock:
2406
+ server = _servers.get(server_name)
2407
+ if not server or not server.session:
2408
+ return json.dumps({
2409
+ "error": f"MCP server '{server_name}' is not connected"
2410
+ }, ensure_ascii=False)
2411
+
2412
+ uri = args.get("uri")
2413
+ if not uri:
2414
+ return tool_error("Missing required parameter 'uri'")
2415
+
2416
+ async def _call():
2417
+ async with server._rpc_lock:
2418
+ result = await server.session.read_resource(uri)
2419
+ # read_resource returns ReadResourceResult with .contents list
2420
+ parts: List[str] = []
2421
+ contents = result.contents if hasattr(result, "contents") else []
2422
+ for block in contents:
2423
+ if hasattr(block, "text"):
2424
+ parts.append(block.text)
2425
+ elif hasattr(block, "blob"):
2426
+ parts.append(f"[binary data, {len(block.blob)} bytes]")
2427
+ return json.dumps({"result": "\n".join(parts) if parts else ""}, ensure_ascii=False)
2428
+
2429
+ def _call_once():
2430
+ return _run_on_mcp_loop(_call, timeout=tool_timeout)
2431
+
2432
+ try:
2433
+ return _call_once()
2434
+ except InterruptedError:
2435
+ return _interrupted_call_result()
2436
+ except Exception as exc:
2437
+ recovered = _handle_auth_error_and_retry(
2438
+ server_name, exc, _call_once, "resources/read",
2439
+ )
2440
+ if recovered is not None:
2441
+ return recovered
2442
+ recovered = _handle_session_expired_and_retry(
2443
+ server_name, exc, _call_once, "resources/read",
2444
+ )
2445
+ if recovered is not None:
2446
+ return recovered
2447
+ logger.error(
2448
+ "MCP %s/read_resource failed: %s", server_name, exc,
2449
+ )
2450
+ return json.dumps({
2451
+ "error": _sanitize_error(
2452
+ f"MCP call failed: {type(exc).__name__}: {_exc_str(exc)}"
2453
+ )
2454
+ }, ensure_ascii=False)
2455
+
2456
+ return _handler
2457
+
2458
+
2459
+ def _make_list_prompts_handler(server_name: str, tool_timeout: float):
2460
+ """Return a sync handler that lists prompts from an MCP server."""
2461
+
2462
+ def _handler(args: dict, **kwargs) -> str:
2463
+ with _lock:
2464
+ server = _servers.get(server_name)
2465
+ if not server or not server.session:
2466
+ return json.dumps({
2467
+ "error": f"MCP server '{server_name}' is not connected"
2468
+ }, ensure_ascii=False)
2469
+
2470
+ async def _call():
2471
+ async with server._rpc_lock:
2472
+ result = await server.session.list_prompts()
2473
+ prompts = []
2474
+ for p in (result.prompts if hasattr(result, "prompts") else []):
2475
+ entry = {}
2476
+ if hasattr(p, "name"):
2477
+ entry["name"] = p.name
2478
+ if hasattr(p, "description") and p.description:
2479
+ entry["description"] = p.description
2480
+ if hasattr(p, "arguments") and p.arguments:
2481
+ entry["arguments"] = [
2482
+ {
2483
+ "name": a.name,
2484
+ **({"description": a.description} if hasattr(a, "description") and a.description else {}),
2485
+ **({"required": a.required} if hasattr(a, "required") else {}),
2486
+ }
2487
+ for a in p.arguments
2488
+ ]
2489
+ prompts.append(entry)
2490
+ return json.dumps({"prompts": prompts}, ensure_ascii=False)
2491
+
2492
+ def _call_once():
2493
+ return _run_on_mcp_loop(_call, timeout=tool_timeout)
2494
+
2495
+ try:
2496
+ return _call_once()
2497
+ except InterruptedError:
2498
+ return _interrupted_call_result()
2499
+ except Exception as exc:
2500
+ recovered = _handle_auth_error_and_retry(
2501
+ server_name, exc, _call_once, "prompts/list",
2502
+ )
2503
+ if recovered is not None:
2504
+ return recovered
2505
+ recovered = _handle_session_expired_and_retry(
2506
+ server_name, exc, _call_once, "prompts/list",
2507
+ )
2508
+ if recovered is not None:
2509
+ return recovered
2510
+ logger.error(
2511
+ "MCP %s/list_prompts failed: %s", server_name, exc,
2512
+ )
2513
+ return json.dumps({
2514
+ "error": _sanitize_error(
2515
+ f"MCP call failed: {type(exc).__name__}: {_exc_str(exc)}"
2516
+ )
2517
+ }, ensure_ascii=False)
2518
+
2519
+ return _handler
2520
+
2521
+
2522
+ def _make_get_prompt_handler(server_name: str, tool_timeout: float):
2523
+ """Return a sync handler that gets a prompt by name from an MCP server."""
2524
+
2525
+ def _handler(args: dict, **kwargs) -> str:
2526
+ from tools.registry import tool_error
2527
+
2528
+ with _lock:
2529
+ server = _servers.get(server_name)
2530
+ if not server or not server.session:
2531
+ return json.dumps({
2532
+ "error": f"MCP server '{server_name}' is not connected"
2533
+ }, ensure_ascii=False)
2534
+
2535
+ name = args.get("name")
2536
+ if not name:
2537
+ return tool_error("Missing required parameter 'name'")
2538
+ arguments = args.get("arguments", {})
2539
+
2540
+ async def _call():
2541
+ async with server._rpc_lock:
2542
+ result = await server.session.get_prompt(name, arguments=arguments)
2543
+ # GetPromptResult has .messages list
2544
+ messages = []
2545
+ for msg in (result.messages if hasattr(result, "messages") else []):
2546
+ entry = {}
2547
+ if hasattr(msg, "role"):
2548
+ entry["role"] = msg.role
2549
+ if hasattr(msg, "content"):
2550
+ content = msg.content
2551
+ if hasattr(content, "text"):
2552
+ entry["content"] = content.text
2553
+ elif isinstance(content, str):
2554
+ entry["content"] = content
2555
+ else:
2556
+ entry["content"] = str(content)
2557
+ messages.append(entry)
2558
+ resp = {"messages": messages}
2559
+ if hasattr(result, "description") and result.description:
2560
+ resp["description"] = result.description
2561
+ return json.dumps(resp, ensure_ascii=False)
2562
+
2563
+ def _call_once():
2564
+ return _run_on_mcp_loop(_call, timeout=tool_timeout)
2565
+
2566
+ try:
2567
+ return _call_once()
2568
+ except InterruptedError:
2569
+ return _interrupted_call_result()
2570
+ except Exception as exc:
2571
+ recovered = _handle_auth_error_and_retry(
2572
+ server_name, exc, _call_once, "prompts/get",
2573
+ )
2574
+ if recovered is not None:
2575
+ return recovered
2576
+ recovered = _handle_session_expired_and_retry(
2577
+ server_name, exc, _call_once, "prompts/get",
2578
+ )
2579
+ if recovered is not None:
2580
+ return recovered
2581
+ logger.error(
2582
+ "MCP %s/get_prompt failed: %s", server_name, exc,
2583
+ )
2584
+ return json.dumps({
2585
+ "error": _sanitize_error(
2586
+ f"MCP call failed: {type(exc).__name__}: {_exc_str(exc)}"
2587
+ )
2588
+ }, ensure_ascii=False)
2589
+
2590
+ return _handler
2591
+
2592
+
2593
+ def _make_check_fn(server_name: str):
2594
+ """Return a check function that verifies the MCP connection is alive."""
2595
+
2596
+ def _check() -> bool:
2597
+ with _lock:
2598
+ server = _servers.get(server_name)
2599
+ return server is not None and server.session is not None
2600
+
2601
+ return _check
2602
+
2603
+
2604
+ # ---------------------------------------------------------------------------
2605
+ # Discovery & registration
2606
+ # ---------------------------------------------------------------------------
2607
+
2608
+ def _normalize_mcp_input_schema(schema: dict | None) -> dict:
2609
+ """Normalize MCP input schemas for LLM tool-calling compatibility.
2610
+
2611
+ MCP servers can emit plain JSON Schema with ``definitions`` /
2612
+ ``#/definitions/...`` references. Kimi / Moonshot rejects that form and
2613
+ requires local refs to point into ``#/$defs/...`` instead. Normalize the
2614
+ common draft-07 shape here so MCP tool schemas remain portable across
2615
+ OpenAI-compatible providers.
2616
+
2617
+ Additional MCP-server robustness repairs applied recursively:
2618
+
2619
+ * Missing or ``null`` ``type`` on an object-shaped node is coerced to
2620
+ ``"object"`` (some servers omit it). See PR #4897.
2621
+ * When an ``object`` node lacks ``properties``, an empty ``properties``
2622
+ dict is added so ``required`` entries don't dangle.
2623
+ * ``required`` arrays are pruned to only names that exist in
2624
+ ``properties``; otherwise Google AI Studio / Gemini 400s with
2625
+ ``property is not defined``. See PR #4651.
2626
+ * MCP/Pydantic optional fields commonly arrive as
2627
+ ``anyOf: [{...}, {"type": "null"}], default: null``. Anthropic rejects
2628
+ nullable branches in tool input schemas, so nullable unions are collapsed
2629
+ to the non-null branch and optionality remains represented solely by the
2630
+ parent object's ``required`` list.
2631
+
2632
+ All repairs are provider-agnostic and ideally produce a schema valid on
2633
+ OpenAI, Anthropic, Gemini, and Moonshot in one pass.
2634
+ """
2635
+ if not schema:
2636
+ return {"type": "object", "properties": {}}
2637
+
2638
+ def _rewrite_local_refs(node):
2639
+ if isinstance(node, dict):
2640
+ normalized = {}
2641
+ for key, value in node.items():
2642
+ out_key = "$defs" if key == "definitions" else key
2643
+ normalized[out_key] = _rewrite_local_refs(value)
2644
+ ref = normalized.get("$ref")
2645
+ if isinstance(ref, str) and ref.startswith("#/definitions/"):
2646
+ normalized["$ref"] = "#/$defs/" + ref[len("#/definitions/"):]
2647
+ return normalized
2648
+ if isinstance(node, list):
2649
+ return [_rewrite_local_refs(item) for item in node]
2650
+ return node
2651
+
2652
+ def _strip_nullable_union(node):
2653
+ """Collapse JSON Schema nullable unions to provider-safe non-null schemas.
2654
+
2655
+ Delegates to ``tools.schema_sanitizer.strip_nullable_unions`` so MCP
2656
+ ingestion, the Anthropic guard, and the global sanitizer all share one
2657
+ implementation. Keeps the ``nullable: true`` hint so runtime argument
2658
+ coercion can still map a model-emitted ``"null"`` string to Python
2659
+ ``None`` for this optional field.
2660
+ """
2661
+ from tools.schema_sanitizer import strip_nullable_unions
2662
+
2663
+ return strip_nullable_unions(node, keep_nullable_hint=True)
2664
+
2665
+ def _repair_object_shape(node):
2666
+ """Recursively repair object-shaped nodes: fill type, prune required."""
2667
+ if isinstance(node, list):
2668
+ return [_repair_object_shape(item) for item in node]
2669
+ if not isinstance(node, dict):
2670
+ return node
2671
+
2672
+ repaired = {k: _repair_object_shape(v) for k, v in node.items()}
2673
+
2674
+ # Coerce missing / null type when the shape is clearly an object
2675
+ # (has properties or required but no type).
2676
+ if not repaired.get("type") and (
2677
+ "properties" in repaired or "required" in repaired
2678
+ ):
2679
+ repaired["type"] = "object"
2680
+
2681
+ if repaired.get("type") == "object":
2682
+ # Ensure properties exists so required can reference it safely
2683
+ if "properties" not in repaired or not isinstance(
2684
+ repaired.get("properties"), dict
2685
+ ):
2686
+ repaired["properties"] = {} if "properties" not in repaired else repaired["properties"]
2687
+ if not isinstance(repaired.get("properties"), dict):
2688
+ repaired["properties"] = {}
2689
+
2690
+ # Prune required to only include names that exist in properties
2691
+ required = repaired.get("required")
2692
+ if isinstance(required, list):
2693
+ props = repaired.get("properties") or {}
2694
+ valid = [r for r in required if isinstance(r, str) and r in props]
2695
+ if len(valid) != len(required):
2696
+ if valid:
2697
+ repaired["required"] = valid
2698
+ else:
2699
+ repaired.pop("required", None)
2700
+
2701
+ return repaired
2702
+
2703
+ normalized = _rewrite_local_refs(schema)
2704
+ normalized = _strip_nullable_union(normalized)
2705
+ normalized = _repair_object_shape(normalized)
2706
+
2707
+ # Ensure top-level is a well-formed object schema
2708
+ if not isinstance(normalized, dict):
2709
+ return {"type": "object", "properties": {}}
2710
+ if normalized.get("type") == "object" and "properties" not in normalized:
2711
+ normalized = {**normalized, "properties": {}}
2712
+
2713
+ return normalized
2714
+
2715
+
2716
+ def sanitize_mcp_name_component(value: str) -> str:
2717
+ """Return an MCP name component safe for tool and prefix generation.
2718
+
2719
+ Preserves Hermes's historical behavior of converting hyphens to
2720
+ underscores, and also replaces any other character outside
2721
+ ``[A-Za-z0-9_]`` with ``_`` so generated tool names are compatible with
2722
+ provider validation rules.
2723
+ """
2724
+ return re.sub(r"[^A-Za-z0-9_]", "_", str(value or ""))
2725
+
2726
+
2727
+ def _convert_mcp_schema(server_name: str, mcp_tool) -> dict:
2728
+ """Convert an MCP tool listing to the Hermes registry schema format.
2729
+
2730
+ Args:
2731
+ server_name: The logical server name for prefixing.
2732
+ mcp_tool: An MCP ``Tool`` object with ``.name``, ``.description``,
2733
+ and ``.inputSchema``.
2734
+
2735
+ Returns:
2736
+ A dict suitable for ``registry.register(schema=...)``.
2737
+ """
2738
+ safe_tool_name = sanitize_mcp_name_component(mcp_tool.name)
2739
+ safe_server_name = sanitize_mcp_name_component(server_name)
2740
+ prefixed_name = f"mcp_{safe_server_name}_{safe_tool_name}"
2741
+ return {
2742
+ "name": prefixed_name,
2743
+ "description": mcp_tool.description or f"MCP tool {mcp_tool.name} from {server_name}",
2744
+ "parameters": _normalize_mcp_input_schema(getattr(mcp_tool, "inputSchema", None)),
2745
+ }
2746
+
2747
+
2748
+ def _build_utility_schemas(server_name: str) -> List[dict]:
2749
+ """Build schemas for the MCP utility tools (resources & prompts).
2750
+
2751
+ Returns a list of (schema, handler_factory_name) tuples encoded as dicts
2752
+ with keys: schema, handler_key.
2753
+ """
2754
+ safe_name = sanitize_mcp_name_component(server_name)
2755
+ return [
2756
+ {
2757
+ "schema": {
2758
+ "name": f"mcp_{safe_name}_list_resources",
2759
+ "description": f"List available resources from MCP server '{server_name}'",
2760
+ "parameters": {
2761
+ "type": "object",
2762
+ "properties": {},
2763
+ },
2764
+ },
2765
+ "handler_key": "list_resources",
2766
+ },
2767
+ {
2768
+ "schema": {
2769
+ "name": f"mcp_{safe_name}_read_resource",
2770
+ "description": f"Read a resource by URI from MCP server '{server_name}'",
2771
+ "parameters": {
2772
+ "type": "object",
2773
+ "properties": {
2774
+ "uri": {
2775
+ "type": "string",
2776
+ "description": "URI of the resource to read",
2777
+ },
2778
+ },
2779
+ "required": ["uri"],
2780
+ },
2781
+ },
2782
+ "handler_key": "read_resource",
2783
+ },
2784
+ {
2785
+ "schema": {
2786
+ "name": f"mcp_{safe_name}_list_prompts",
2787
+ "description": f"List available prompts from MCP server '{server_name}'",
2788
+ "parameters": {
2789
+ "type": "object",
2790
+ "properties": {},
2791
+ },
2792
+ },
2793
+ "handler_key": "list_prompts",
2794
+ },
2795
+ {
2796
+ "schema": {
2797
+ "name": f"mcp_{safe_name}_get_prompt",
2798
+ "description": f"Get a prompt by name from MCP server '{server_name}'",
2799
+ "parameters": {
2800
+ "type": "object",
2801
+ "properties": {
2802
+ "name": {
2803
+ "type": "string",
2804
+ "description": "Name of the prompt to retrieve",
2805
+ },
2806
+ "arguments": {
2807
+ "type": "object",
2808
+ "description": "Optional arguments to pass to the prompt",
2809
+ "properties": {},
2810
+ "additionalProperties": True,
2811
+ },
2812
+ },
2813
+ "required": ["name"],
2814
+ },
2815
+ },
2816
+ "handler_key": "get_prompt",
2817
+ },
2818
+ ]
2819
+
2820
+
2821
+ def _normalize_name_filter(value: Any, label: str) -> set[str]:
2822
+ """Normalize include/exclude config to a set of tool names."""
2823
+ if value is None:
2824
+ return set()
2825
+ if isinstance(value, str):
2826
+ return {value}
2827
+ if isinstance(value, (list, tuple, set)):
2828
+ return {str(item) for item in value}
2829
+ logger.warning("MCP config %s must be a string or list of strings; ignoring %r", label, value)
2830
+ return set()
2831
+
2832
+
2833
+ def _parse_boolish(value: Any, default: bool = True) -> bool:
2834
+ """Parse a bool-like config value with safe fallback."""
2835
+ if value is None:
2836
+ return default
2837
+ if isinstance(value, bool):
2838
+ return value
2839
+ if isinstance(value, str):
2840
+ lowered = value.strip().lower()
2841
+ if lowered in {"true", "1", "yes", "on"}:
2842
+ return True
2843
+ if lowered in {"false", "0", "no", "off"}:
2844
+ return False
2845
+ logger.warning("MCP config expected a boolean-ish value, got %r; using default=%s", value, default)
2846
+ return default
2847
+
2848
+
2849
+ _UTILITY_CAPABILITY_METHODS = {
2850
+ "list_resources": "list_resources",
2851
+ "read_resource": "read_resource",
2852
+ "list_prompts": "list_prompts",
2853
+ "get_prompt": "get_prompt",
2854
+ }
2855
+
2856
+ # Maps each utility handler to the MCP capability key that must be non-None
2857
+ # on the server's ``initialize`` response for the handler to be registered.
2858
+ # Source of truth: MCP spec — capabilities.resources / capabilities.prompts
2859
+ # are present on the response only when the server actually implements
2860
+ # those request families. Without this gate, tools-only servers (e.g.
2861
+ # Context7 @upstash/context7-mcp, which advertises only ``tools``) had
2862
+ # all four utility stubs registered and every model call to them came
2863
+ # back with JSON-RPC ``-32601 Method not found``, which made the model
2864
+ # conclude the server was broken even when the real tools worked. See
2865
+ # #18051.
2866
+ _UTILITY_CAPABILITY_ATTRS = {
2867
+ "list_resources": "resources",
2868
+ "read_resource": "resources",
2869
+ "list_prompts": "prompts",
2870
+ "get_prompt": "prompts",
2871
+ }
2872
+
2873
+
2874
+ def _select_utility_schemas(server_name: str, server: MCPServerTask, config: dict) -> List[dict]:
2875
+ """Select utility schemas based on config and server capabilities."""
2876
+ tools_filter = config.get("tools") or {}
2877
+ resources_enabled = _parse_boolish(tools_filter.get("resources"), default=True)
2878
+ prompts_enabled = _parse_boolish(tools_filter.get("prompts"), default=True)
2879
+
2880
+ # ``initialize_result.capabilities`` is the source of truth: its sub-objects
2881
+ # (``resources``, ``prompts``) are non-None iff the server advertises that
2882
+ # request family. ``hasattr(server.session, ...)`` was the old gate but
2883
+ # ClientSession always has the four method attributes defined on the class,
2884
+ # so it never filtered anything.
2885
+ advertised_caps = None
2886
+ init_result = getattr(server, "initialize_result", None)
2887
+ if init_result is not None:
2888
+ advertised_caps = getattr(init_result, "capabilities", None)
2889
+
2890
+ selected: List[dict] = []
2891
+ for entry in _build_utility_schemas(server_name):
2892
+ handler_key = entry["handler_key"]
2893
+ if handler_key in {"list_resources", "read_resource"} and not resources_enabled:
2894
+ logger.debug("MCP server '%s': skipping utility '%s' (resources disabled)", server_name, handler_key)
2895
+ continue
2896
+ if handler_key in {"list_prompts", "get_prompt"} and not prompts_enabled:
2897
+ logger.debug("MCP server '%s': skipping utility '%s' (prompts disabled)", server_name, handler_key)
2898
+ continue
2899
+
2900
+ # Preferred gate: check the server's advertised capabilities. Skip
2901
+ # if the capability is explicitly not advertised.
2902
+ if advertised_caps is not None:
2903
+ cap_attr = _UTILITY_CAPABILITY_ATTRS[handler_key]
2904
+ if getattr(advertised_caps, cap_attr, None) is None:
2905
+ logger.debug(
2906
+ "MCP server '%s': skipping utility '%s' "
2907
+ "(server does not advertise '%s' capability)",
2908
+ server_name,
2909
+ handler_key,
2910
+ cap_attr,
2911
+ )
2912
+ continue
2913
+ else:
2914
+ # Legacy fallback for test fixtures or older code paths where
2915
+ # initialize_result wasn't captured. Preserves the old behavior
2916
+ # of registering every stub in that case rather than regressing
2917
+ # any server that was working before this fix.
2918
+ required_method = _UTILITY_CAPABILITY_METHODS[handler_key]
2919
+ if not hasattr(server.session, required_method):
2920
+ logger.debug(
2921
+ "MCP server '%s': skipping utility '%s' (session lacks %s)",
2922
+ server_name,
2923
+ handler_key,
2924
+ required_method,
2925
+ )
2926
+ continue
2927
+ selected.append(entry)
2928
+ return selected
2929
+
2930
+
2931
+ def _existing_tool_names() -> List[str]:
2932
+ """Return tool names for all currently connected servers."""
2933
+ names: List[str] = []
2934
+ for _sname, server in _servers.items():
2935
+ if hasattr(server, "_registered_tool_names"):
2936
+ names.extend(server._registered_tool_names)
2937
+ continue
2938
+ for mcp_tool in server._tools:
2939
+ schema = _convert_mcp_schema(server.name, mcp_tool)
2940
+ names.append(schema["name"])
2941
+ return names
2942
+
2943
+
2944
+ def _register_server_tools(name: str, server: MCPServerTask, config: dict) -> List[str]:
2945
+ """Register tools from an already-connected server into the registry.
2946
+
2947
+ Handles include/exclude filtering and utility tools. Toolset resolution
2948
+ for ``mcp-{server}`` and raw server-name aliases is derived from the live
2949
+ registry, rather than mutating ``toolsets.TOOLSETS`` at runtime.
2950
+
2951
+ Used by both initial discovery and dynamic refresh (list_changed).
2952
+
2953
+ Returns:
2954
+ List of registered prefixed tool names.
2955
+ """
2956
+ from tools.registry import registry
2957
+
2958
+ registered_names: List[str] = []
2959
+ toolset_name = f"mcp-{name}"
2960
+
2961
+ # Selective tool loading: honour include/exclude lists from config.
2962
+ # Rules (matching issue #690 spec):
2963
+ # tools.include — whitelist: only these tool names are registered
2964
+ # tools.exclude — blacklist: all tools EXCEPT these are registered
2965
+ # include takes precedence over exclude
2966
+ # Neither set → register all tools (backward-compatible default)
2967
+ tools_filter = config.get("tools") or {}
2968
+ include_set = _normalize_name_filter(tools_filter.get("include"), f"mcp_servers.{name}.tools.include")
2969
+ exclude_set = _normalize_name_filter(tools_filter.get("exclude"), f"mcp_servers.{name}.tools.exclude")
2970
+
2971
+ def _should_register(tool_name: str) -> bool:
2972
+ if include_set:
2973
+ return tool_name in include_set
2974
+ if exclude_set:
2975
+ return tool_name not in exclude_set
2976
+ return True
2977
+
2978
+ for mcp_tool in server._tools:
2979
+ if not _should_register(mcp_tool.name):
2980
+ logger.debug("MCP server '%s': skipping tool '%s' (filtered by config)", name, mcp_tool.name)
2981
+ continue
2982
+
2983
+ # Scan tool description for prompt injection patterns
2984
+ _scan_mcp_description(name, mcp_tool.name, mcp_tool.description or "")
2985
+
2986
+ schema = _convert_mcp_schema(name, mcp_tool)
2987
+ tool_name_prefixed = schema["name"]
2988
+
2989
+ # Guard against collisions with built-in (non-MCP) tools.
2990
+ existing_toolset = registry.get_toolset_for_tool(tool_name_prefixed)
2991
+ if existing_toolset and not existing_toolset.startswith("mcp-"):
2992
+ logger.warning(
2993
+ "MCP server '%s': tool '%s' (→ '%s') collides with built-in "
2994
+ "tool in toolset '%s' — skipping to preserve built-in",
2995
+ name, mcp_tool.name, tool_name_prefixed, existing_toolset,
2996
+ )
2997
+ continue
2998
+
2999
+ registry.register(
3000
+ name=tool_name_prefixed,
3001
+ toolset=toolset_name,
3002
+ schema=schema,
3003
+ handler=_make_tool_handler(name, mcp_tool.name, server.tool_timeout),
3004
+ check_fn=_make_check_fn(name),
3005
+ is_async=False,
3006
+ description=schema["description"],
3007
+ )
3008
+ registered_names.append(tool_name_prefixed)
3009
+
3010
+ # Register MCP Resources & Prompts utility tools, filtered by config and
3011
+ # only when the server actually supports the corresponding capability.
3012
+ _handler_factories = {
3013
+ "list_resources": _make_list_resources_handler,
3014
+ "read_resource": _make_read_resource_handler,
3015
+ "list_prompts": _make_list_prompts_handler,
3016
+ "get_prompt": _make_get_prompt_handler,
3017
+ }
3018
+ check_fn = _make_check_fn(name)
3019
+ for entry in _select_utility_schemas(name, server, config):
3020
+ schema = entry["schema"]
3021
+ handler_key = entry["handler_key"]
3022
+ handler = _handler_factories[handler_key](name, server.tool_timeout)
3023
+ util_name = schema["name"]
3024
+
3025
+ # Same collision guard for utility tools.
3026
+ existing_toolset = registry.get_toolset_for_tool(util_name)
3027
+ if existing_toolset and not existing_toolset.startswith("mcp-"):
3028
+ logger.warning(
3029
+ "MCP server '%s': utility tool '%s' collides with built-in "
3030
+ "tool in toolset '%s' — skipping to preserve built-in",
3031
+ name, util_name, existing_toolset,
3032
+ )
3033
+ continue
3034
+
3035
+ registry.register(
3036
+ name=util_name,
3037
+ toolset=toolset_name,
3038
+ schema=schema,
3039
+ handler=handler,
3040
+ check_fn=check_fn,
3041
+ is_async=False,
3042
+ description=schema["description"],
3043
+ )
3044
+ registered_names.append(util_name)
3045
+
3046
+ if registered_names:
3047
+ registry.register_toolset_alias(name, toolset_name)
3048
+
3049
+ return registered_names
3050
+
3051
+
3052
+ async def _discover_and_register_server(name: str, config: dict) -> List[str]:
3053
+ """Connect to a single MCP server, discover tools, and register them.
3054
+
3055
+ Returns list of registered tool names.
3056
+ """
3057
+ connect_timeout = config.get("connect_timeout", _DEFAULT_CONNECT_TIMEOUT)
3058
+ server = await asyncio.wait_for(
3059
+ _connect_server(name, config),
3060
+ timeout=connect_timeout,
3061
+ )
3062
+ with _lock:
3063
+ _servers[name] = server
3064
+
3065
+ registered_names = _register_server_tools(name, server, config)
3066
+ server._registered_tool_names = list(registered_names)
3067
+
3068
+ transport_type = "HTTP" if "url" in config else "stdio"
3069
+ logger.info(
3070
+ "MCP server '%s' (%s): registered %d tool(s): %s",
3071
+ name, transport_type, len(registered_names),
3072
+ ", ".join(registered_names),
3073
+ )
3074
+ return registered_names
3075
+
3076
+
3077
+ # ---------------------------------------------------------------------------
3078
+ # Public API
3079
+ # ---------------------------------------------------------------------------
3080
+
3081
+ def register_mcp_servers(servers: Dict[str, dict]) -> List[str]:
3082
+ """Connect to explicit MCP servers and register their tools.
3083
+
3084
+ Idempotent for already-connected server names. Servers with
3085
+ ``enabled: false`` are skipped without disconnecting existing sessions.
3086
+
3087
+ Args:
3088
+ servers: Mapping of ``{server_name: server_config}``.
3089
+
3090
+ Returns:
3091
+ List of all currently registered MCP tool names.
3092
+ """
3093
+ if not _MCP_AVAILABLE:
3094
+ logger.debug("MCP SDK not available -- skipping explicit MCP registration")
3095
+ return []
3096
+
3097
+ if not servers:
3098
+ logger.debug("No explicit MCP servers provided")
3099
+ return []
3100
+
3101
+ # Only attempt servers that aren't already connected and are enabled
3102
+ # (enabled: false skips the server entirely without removing its config)
3103
+ with _lock:
3104
+ new_servers = {
3105
+ k: v
3106
+ for k, v in servers.items()
3107
+ if k not in _servers and _parse_boolish(v.get("enabled", True), default=True)
3108
+ }
3109
+ # Track which servers opt-in to parallel tool calls (idempotent).
3110
+ for srv_name, srv_cfg in servers.items():
3111
+ if _parse_boolish(srv_cfg.get("supports_parallel_tool_calls", False), default=False):
3112
+ _parallel_safe_servers.add(sanitize_mcp_name_component(srv_name))
3113
+ else:
3114
+ _parallel_safe_servers.discard(sanitize_mcp_name_component(srv_name))
3115
+
3116
+ if not new_servers:
3117
+ return _existing_tool_names()
3118
+
3119
+ # Start the background event loop for MCP connections
3120
+ _ensure_mcp_loop()
3121
+
3122
+ async def _discover_one(name: str, cfg: dict) -> List[str]:
3123
+ """Connect to a single server and return its registered tool names."""
3124
+ return await _discover_and_register_server(name, cfg)
3125
+
3126
+ async def _discover_all():
3127
+ server_names = list(new_servers.keys())
3128
+ # Connect to all servers in PARALLEL
3129
+ results = await asyncio.gather(
3130
+ *(_discover_one(name, cfg) for name, cfg in new_servers.items()),
3131
+ return_exceptions=True,
3132
+ )
3133
+ for name, result in zip(server_names, results):
3134
+ if isinstance(result, Exception):
3135
+ command = new_servers.get(name, {}).get("command")
3136
+ logger.warning(
3137
+ "Failed to connect to MCP server '%s'%s: %s",
3138
+ name,
3139
+ f" (command={command})" if command else "",
3140
+ _format_connect_error(result),
3141
+ )
3142
+
3143
+ # Per-server timeouts are handled inside _discover_and_register_server.
3144
+ # The outer timeout is generous: 120s total for parallel discovery.
3145
+ #
3146
+ # Temporarily clear the interrupt flag on the current thread so that MCP
3147
+ # discovery is never cancelled by a stale interrupt from a prior agent
3148
+ # session (executor threads get reused and may carry old interrupt state).
3149
+ from tools.interrupt import is_interrupted as _is_interrupted, set_interrupt as _set_interrupt
3150
+ _was_interrupted = _is_interrupted()
3151
+ if _was_interrupted:
3152
+ _set_interrupt(False)
3153
+ try:
3154
+ _run_on_mcp_loop(_discover_all, timeout=120)
3155
+ finally:
3156
+ if _was_interrupted:
3157
+ _set_interrupt(True)
3158
+
3159
+ # Log a summary so ACP callers get visibility into what was registered.
3160
+ with _lock:
3161
+ connected = [n for n in new_servers if n in _servers]
3162
+ new_tool_count = sum(
3163
+ len(getattr(_servers[n], "_registered_tool_names", []))
3164
+ for n in connected
3165
+ )
3166
+ failed = len(new_servers) - len(connected)
3167
+ if new_tool_count or failed:
3168
+ summary = f"MCP: registered {new_tool_count} tool(s) from {len(connected)} server(s)"
3169
+ if failed:
3170
+ summary += f" ({failed} failed)"
3171
+ logger.info(summary)
3172
+
3173
+ return _existing_tool_names()
3174
+
3175
+
3176
+ def discover_mcp_tools() -> List[str]:
3177
+ """Entry point: load config, connect to MCP servers, register tools.
3178
+
3179
+ Called from ``model_tools`` after ``discover_builtin_tools()``. Safe to call even when
3180
+ the ``mcp`` package is not installed (returns empty list).
3181
+
3182
+ Idempotent for already-connected servers. If some servers failed on a
3183
+ previous call, only the missing ones are retried.
3184
+
3185
+ Returns:
3186
+ List of all registered MCP tool names.
3187
+ """
3188
+ if not _MCP_AVAILABLE:
3189
+ logger.debug("MCP SDK not available -- skipping MCP tool discovery")
3190
+ return []
3191
+
3192
+ servers = _load_mcp_config()
3193
+ if not servers:
3194
+ logger.debug("No MCP servers configured")
3195
+ return []
3196
+
3197
+ with _lock:
3198
+ new_server_names = [
3199
+ name
3200
+ for name, cfg in servers.items()
3201
+ if name not in _servers and _parse_boolish(cfg.get("enabled", True), default=True)
3202
+ ]
3203
+
3204
+ tool_names = register_mcp_servers(servers)
3205
+ if not new_server_names:
3206
+ return tool_names
3207
+
3208
+ with _lock:
3209
+ connected_server_names = [name for name in new_server_names if name in _servers]
3210
+ new_tool_count = sum(
3211
+ len(getattr(_servers[name], "_registered_tool_names", []))
3212
+ for name in connected_server_names
3213
+ )
3214
+
3215
+ failed_count = len(new_server_names) - len(connected_server_names)
3216
+ if new_tool_count or failed_count:
3217
+ summary = f" MCP: {new_tool_count} tool(s) from {len(connected_server_names)} server(s)"
3218
+ if failed_count:
3219
+ summary += f" ({failed_count} failed)"
3220
+ logger.info(summary)
3221
+
3222
+ return tool_names
3223
+
3224
+
3225
+ def is_mcp_tool_parallel_safe(tool_name: str) -> bool:
3226
+ """Check if an MCP tool belongs to a server that supports parallel tool calls.
3227
+
3228
+ MCP tool names follow the pattern ``mcp_{server}_{tool}``. This extracts
3229
+ the server component and checks it against the set of servers whose config
3230
+ includes ``supports_parallel_tool_calls: true``.
3231
+
3232
+ Returns False for non-MCP tools or tools from servers without the flag.
3233
+ """
3234
+ if not tool_name.startswith("mcp_"):
3235
+ return False
3236
+ # Strip the "mcp_" prefix and extract the server name.
3237
+ # Tool names are: mcp_{sanitized_server}_{sanitized_tool}
3238
+ # We need to check all possible server prefixes because the server name
3239
+ # itself may contain underscores after sanitization.
3240
+ rest = tool_name[4:] # strip "mcp_"
3241
+ with _lock:
3242
+ for server_name in _parallel_safe_servers:
3243
+ if rest.startswith(server_name + "_") and len(rest) > len(server_name) + 1:
3244
+ return True
3245
+ return False
3246
+
3247
+
3248
+ def get_mcp_status() -> List[dict]:
3249
+ """Return status of all configured MCP servers for banner display.
3250
+
3251
+ Returns a list of dicts with keys: name, transport, tools, connected.
3252
+ Includes both successfully connected servers and configured-but-failed ones.
3253
+ """
3254
+ result: List[dict] = []
3255
+
3256
+ # Get configured servers from config
3257
+ configured = _load_mcp_config()
3258
+ if not configured:
3259
+ return result
3260
+
3261
+ with _lock:
3262
+ active_servers = dict(_servers)
3263
+
3264
+ for name, cfg in configured.items():
3265
+ transport = cfg.get("transport", "http") if "url" in cfg else "stdio"
3266
+ server = active_servers.get(name)
3267
+ if server and server.session is not None:
3268
+ entry = {
3269
+ "name": name,
3270
+ "transport": transport,
3271
+ "tools": len(server._registered_tool_names) if hasattr(server, "_registered_tool_names") else len(server._tools),
3272
+ "connected": True,
3273
+ }
3274
+ if server._sampling:
3275
+ entry["sampling"] = dict(server._sampling.metrics)
3276
+ result.append(entry)
3277
+ else:
3278
+ result.append({
3279
+ "name": name,
3280
+ "transport": transport,
3281
+ "tools": 0,
3282
+ "connected": False,
3283
+ })
3284
+
3285
+ return result
3286
+
3287
+
3288
+ def probe_mcp_server_tools() -> Dict[str, List[tuple]]:
3289
+ """Temporarily connect to configured MCP servers and list their tools.
3290
+
3291
+ Designed for ``hermes tools`` interactive configuration — connects to each
3292
+ enabled server, grabs tool names and descriptions, then disconnects.
3293
+ Does NOT register tools in the Hermes registry.
3294
+
3295
+ Returns:
3296
+ Dict mapping server name to list of (tool_name, description) tuples.
3297
+ Servers that fail to connect are omitted from the result.
3298
+ """
3299
+ if not _MCP_AVAILABLE:
3300
+ return {}
3301
+
3302
+ servers_config = _load_mcp_config()
3303
+ if not servers_config:
3304
+ return {}
3305
+
3306
+ enabled = {
3307
+ k: v for k, v in servers_config.items()
3308
+ if _parse_boolish(v.get("enabled", True), default=True)
3309
+ }
3310
+ if not enabled:
3311
+ return {}
3312
+
3313
+ _ensure_mcp_loop()
3314
+
3315
+ result: Dict[str, List[tuple]] = {}
3316
+ probed_servers: List[MCPServerTask] = []
3317
+
3318
+ async def _probe_all():
3319
+ names = list(enabled.keys())
3320
+ coros = []
3321
+ for name, cfg in enabled.items():
3322
+ ct = cfg.get("connect_timeout", _DEFAULT_CONNECT_TIMEOUT)
3323
+ coros.append(asyncio.wait_for(_connect_server(name, cfg), timeout=ct))
3324
+
3325
+ outcomes = await asyncio.gather(*coros, return_exceptions=True)
3326
+
3327
+ for name, outcome in zip(names, outcomes):
3328
+ if isinstance(outcome, Exception):
3329
+ logger.debug("Probe: failed to connect to '%s': %s", name, outcome)
3330
+ continue
3331
+ probed_servers.append(outcome)
3332
+ tools = []
3333
+ for t in outcome._tools:
3334
+ desc = getattr(t, "description", "") or ""
3335
+ tools.append((t.name, desc))
3336
+ result[name] = tools
3337
+
3338
+ # Shut down all probed connections
3339
+ await asyncio.gather(
3340
+ *(s.shutdown() for s in probed_servers),
3341
+ return_exceptions=True,
3342
+ )
3343
+
3344
+ try:
3345
+ _run_on_mcp_loop(_probe_all, timeout=120)
3346
+ except Exception as exc:
3347
+ logger.debug("MCP probe failed: %s", exc)
3348
+ finally:
3349
+ _stop_mcp_loop()
3350
+
3351
+ return result
3352
+
3353
+
3354
+ def shutdown_mcp_servers():
3355
+ """Close all MCP server connections and stop the background loop.
3356
+
3357
+ Each server Task is signalled to exit its ``async with`` block so that
3358
+ the anyio cancel-scope cleanup happens in the same Task that opened it.
3359
+ All servers are shut down in parallel via ``asyncio.gather``.
3360
+ """
3361
+ with _lock:
3362
+ servers_snapshot = list(_servers.values())
3363
+
3364
+ # Fast path: nothing to shut down.
3365
+ if not servers_snapshot:
3366
+ _stop_mcp_loop()
3367
+ return
3368
+
3369
+ async def _shutdown():
3370
+ results = await asyncio.gather(
3371
+ *(server.shutdown() for server in servers_snapshot),
3372
+ return_exceptions=True,
3373
+ )
3374
+ for server, result in zip(servers_snapshot, results):
3375
+ if isinstance(result, Exception):
3376
+ logger.debug(
3377
+ "Error closing MCP server '%s': %s", server.name, result,
3378
+ )
3379
+ with _lock:
3380
+ _servers.clear()
3381
+
3382
+ with _lock:
3383
+ loop = _mcp_loop
3384
+ if loop is not None and loop.is_running():
3385
+ from agent.async_utils import safe_schedule_threadsafe
3386
+ future = safe_schedule_threadsafe(
3387
+ _shutdown(), loop,
3388
+ logger=logger,
3389
+ log_message="MCP shutdown: failed to schedule",
3390
+ )
3391
+ if future is not None:
3392
+ try:
3393
+ future.result(timeout=15)
3394
+ except Exception as exc:
3395
+ logger.debug("Error during MCP shutdown: %s", exc)
3396
+
3397
+ _stop_mcp_loop()
3398
+
3399
+
3400
+ def _kill_orphaned_mcp_children(include_active: bool = False) -> None:
3401
+ """Best-effort graceful shutdown of stdio MCP subprocesses to reap orphans.
3402
+
3403
+ Orphans are PIDs that survived their session context exit (SDK teardown
3404
+ did not terminate the process — common on Linux when stdio children escape
3405
+ the parent cgroup on cancellation). By default only entries in
3406
+ ``_orphan_stdio_pids`` are reaped so concurrent cron jobs and live user
3407
+ sessions are not disrupted.
3408
+
3409
+ Sends SIGTERM, waits 2 seconds, then escalates to SIGKILL for any
3410
+ survivors, avoiding shared-resource collisions when multiple hermes
3411
+ processes run on the same host (each has its own ``_stdio_pids`` dict).
3412
+
3413
+ With ``include_active=True`` also kills every PID in ``_stdio_pids`` —
3414
+ used only at final shutdown, after the MCP event loop has stopped and no
3415
+ sessions can still be in flight.
3416
+ """
3417
+ import signal as _signal
3418
+ import time as _time
3419
+
3420
+ with _lock:
3421
+ pids: Dict[int, str] = {}
3422
+ for opid in _orphan_stdio_pids:
3423
+ pids[opid] = "orphan"
3424
+ _orphan_stdio_pids.clear()
3425
+ if include_active:
3426
+ pids.update(dict(_stdio_pids))
3427
+ _stdio_pids.clear()
3428
+
3429
+ # Fast path: no tracked stdio PIDs to reap. Skip the SIGTERM/sleep/SIGKILL
3430
+ # dance entirely — otherwise every MCP-free shutdown pays a 2s sleep tax.
3431
+ if not pids:
3432
+ return
3433
+
3434
+ # Phase 1: SIGTERM (graceful)
3435
+ for pid, server_name in pids.items():
3436
+ try:
3437
+ os.kill(pid, _signal.SIGTERM)
3438
+ logger.debug("Sent SIGTERM to orphaned MCP process %d (%s)", pid, server_name)
3439
+ except (ProcessLookupError, PermissionError, OSError):
3440
+ pass
3441
+
3442
+ # Phase 2: Wait for graceful exit
3443
+ _time.sleep(2)
3444
+
3445
+ # Phase 3: SIGKILL any survivors
3446
+ _sigkill = getattr(_signal, "SIGKILL", _signal.SIGTERM)
3447
+ # ``os.kill(pid, 0)`` is NOT a no-op on Windows. Use the cross-platform
3448
+ # existence check before escalating to SIGKILL.
3449
+ from gateway.status import _pid_exists
3450
+ for pid, server_name in pids.items():
3451
+ if not _pid_exists(pid):
3452
+ continue # Good — exited after SIGTERM
3453
+ try:
3454
+ os.kill(pid, _sigkill)
3455
+ logger.warning(
3456
+ "Force-killed MCP process %d (%s) after SIGTERM timeout",
3457
+ pid, server_name,
3458
+ )
3459
+ except (ProcessLookupError, PermissionError, OSError):
3460
+ pass
3461
+
3462
+
3463
+ def _stop_mcp_loop():
3464
+ """Stop the background event loop and join its thread."""
3465
+ global _mcp_loop, _mcp_thread
3466
+ with _lock:
3467
+ loop = _mcp_loop
3468
+ thread = _mcp_thread
3469
+ _mcp_loop = None
3470
+ _mcp_thread = None
3471
+ if loop is not None:
3472
+ loop.call_soon_threadsafe(loop.stop)
3473
+ if thread is not None:
3474
+ thread.join(timeout=5)
3475
+ try:
3476
+ loop.close()
3477
+ except Exception:
3478
+ pass
3479
+ # After closing the loop, any stdio subprocesses that survived the
3480
+ # graceful shutdown are now orphaned — include active PIDs too
3481
+ # since the loop is gone and no session can still be in flight.
3482
+ _kill_orphaned_mcp_children(include_active=True)
3483
+