massgen 0.0.3__py3-none-any.whl → 0.1.0__py3-none-any.whl

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.

Potentially problematic release.


This version of massgen might be problematic. Click here for more details.

Files changed (268) hide show
  1. massgen/__init__.py +142 -8
  2. massgen/adapters/__init__.py +29 -0
  3. massgen/adapters/ag2_adapter.py +483 -0
  4. massgen/adapters/base.py +183 -0
  5. massgen/adapters/tests/__init__.py +0 -0
  6. massgen/adapters/tests/test_ag2_adapter.py +439 -0
  7. massgen/adapters/tests/test_agent_adapter.py +128 -0
  8. massgen/adapters/utils/__init__.py +2 -0
  9. massgen/adapters/utils/ag2_utils.py +236 -0
  10. massgen/adapters/utils/tests/__init__.py +0 -0
  11. massgen/adapters/utils/tests/test_ag2_utils.py +138 -0
  12. massgen/agent_config.py +329 -55
  13. massgen/api_params_handler/__init__.py +10 -0
  14. massgen/api_params_handler/_api_params_handler_base.py +99 -0
  15. massgen/api_params_handler/_chat_completions_api_params_handler.py +176 -0
  16. massgen/api_params_handler/_claude_api_params_handler.py +113 -0
  17. massgen/api_params_handler/_response_api_params_handler.py +130 -0
  18. massgen/backend/__init__.py +39 -4
  19. massgen/backend/azure_openai.py +385 -0
  20. massgen/backend/base.py +341 -69
  21. massgen/backend/base_with_mcp.py +1102 -0
  22. massgen/backend/capabilities.py +386 -0
  23. massgen/backend/chat_completions.py +577 -130
  24. massgen/backend/claude.py +1033 -537
  25. massgen/backend/claude_code.py +1203 -0
  26. massgen/backend/cli_base.py +209 -0
  27. massgen/backend/docs/BACKEND_ARCHITECTURE.md +126 -0
  28. massgen/backend/{CLAUDE_API_RESEARCH.md → docs/CLAUDE_API_RESEARCH.md} +18 -18
  29. massgen/backend/{GEMINI_API_DOCUMENTATION.md → docs/GEMINI_API_DOCUMENTATION.md} +9 -9
  30. massgen/backend/docs/Gemini MCP Integration Analysis.md +1050 -0
  31. massgen/backend/docs/MCP_IMPLEMENTATION_CLAUDE_BACKEND.md +177 -0
  32. massgen/backend/docs/MCP_INTEGRATION_RESPONSE_BACKEND.md +352 -0
  33. massgen/backend/docs/OPENAI_GPT5_MODELS.md +211 -0
  34. massgen/backend/{OPENAI_RESPONSES_API_FORMAT.md → docs/OPENAI_RESPONSE_API_TOOL_CALLS.md} +3 -3
  35. massgen/backend/docs/OPENAI_response_streaming.md +20654 -0
  36. massgen/backend/docs/inference_backend.md +257 -0
  37. massgen/backend/docs/permissions_and_context_files.md +1085 -0
  38. massgen/backend/external.py +126 -0
  39. massgen/backend/gemini.py +1850 -241
  40. massgen/backend/grok.py +40 -156
  41. massgen/backend/inference.py +156 -0
  42. massgen/backend/lmstudio.py +171 -0
  43. massgen/backend/response.py +1095 -322
  44. massgen/chat_agent.py +131 -113
  45. massgen/cli.py +1560 -275
  46. massgen/config_builder.py +2396 -0
  47. massgen/configs/BACKEND_CONFIGURATION.md +458 -0
  48. massgen/configs/README.md +559 -216
  49. massgen/configs/ag2/ag2_case_study.yaml +27 -0
  50. massgen/configs/ag2/ag2_coder.yaml +34 -0
  51. massgen/configs/ag2/ag2_coder_case_study.yaml +36 -0
  52. massgen/configs/ag2/ag2_gemini.yaml +27 -0
  53. massgen/configs/ag2/ag2_groupchat.yaml +108 -0
  54. massgen/configs/ag2/ag2_groupchat_gpt.yaml +118 -0
  55. massgen/configs/ag2/ag2_single_agent.yaml +21 -0
  56. massgen/configs/basic/multi/fast_timeout_example.yaml +37 -0
  57. massgen/configs/basic/multi/gemini_4o_claude.yaml +31 -0
  58. massgen/configs/basic/multi/gemini_gpt5nano_claude.yaml +36 -0
  59. massgen/configs/{gemini_4o_claude.yaml → basic/multi/geminicode_4o_claude.yaml} +3 -3
  60. massgen/configs/basic/multi/geminicode_gpt5nano_claude.yaml +36 -0
  61. massgen/configs/basic/multi/glm_gemini_claude.yaml +25 -0
  62. massgen/configs/basic/multi/gpt4o_audio_generation.yaml +30 -0
  63. massgen/configs/basic/multi/gpt4o_image_generation.yaml +31 -0
  64. massgen/configs/basic/multi/gpt5nano_glm_qwen.yaml +26 -0
  65. massgen/configs/basic/multi/gpt5nano_image_understanding.yaml +26 -0
  66. massgen/configs/{three_agents_default.yaml → basic/multi/three_agents_default.yaml} +8 -4
  67. massgen/configs/basic/multi/three_agents_opensource.yaml +27 -0
  68. massgen/configs/basic/multi/three_agents_vllm.yaml +20 -0
  69. massgen/configs/basic/multi/two_agents_gemini.yaml +19 -0
  70. massgen/configs/{two_agents.yaml → basic/multi/two_agents_gpt5.yaml} +14 -6
  71. massgen/configs/basic/multi/two_agents_opensource_lmstudio.yaml +31 -0
  72. massgen/configs/basic/multi/two_qwen_vllm_sglang.yaml +28 -0
  73. massgen/configs/{single_agent.yaml → basic/single/single_agent.yaml} +1 -1
  74. massgen/configs/{single_flash2.5.yaml → basic/single/single_flash2.5.yaml} +1 -2
  75. massgen/configs/basic/single/single_gemini2.5pro.yaml +16 -0
  76. massgen/configs/basic/single/single_gpt4o_audio_generation.yaml +22 -0
  77. massgen/configs/basic/single/single_gpt4o_image_generation.yaml +22 -0
  78. massgen/configs/basic/single/single_gpt4o_video_generation.yaml +24 -0
  79. massgen/configs/basic/single/single_gpt5nano.yaml +20 -0
  80. massgen/configs/basic/single/single_gpt5nano_file_search.yaml +18 -0
  81. massgen/configs/basic/single/single_gpt5nano_image_understanding.yaml +17 -0
  82. massgen/configs/basic/single/single_gptoss120b.yaml +15 -0
  83. massgen/configs/basic/single/single_openrouter_audio_understanding.yaml +15 -0
  84. massgen/configs/basic/single/single_qwen_video_understanding.yaml +15 -0
  85. massgen/configs/debug/code_execution/command_filtering_blacklist.yaml +29 -0
  86. massgen/configs/debug/code_execution/command_filtering_whitelist.yaml +28 -0
  87. massgen/configs/debug/code_execution/docker_verification.yaml +29 -0
  88. massgen/configs/debug/skip_coordination_test.yaml +27 -0
  89. massgen/configs/debug/test_sdk_migration.yaml +17 -0
  90. massgen/configs/docs/DISCORD_MCP_SETUP.md +208 -0
  91. massgen/configs/docs/TWITTER_MCP_ENESCINAR_SETUP.md +82 -0
  92. massgen/configs/providers/azure/azure_openai_multi.yaml +21 -0
  93. massgen/configs/providers/azure/azure_openai_single.yaml +19 -0
  94. massgen/configs/providers/claude/claude.yaml +14 -0
  95. massgen/configs/providers/gemini/gemini_gpt5nano.yaml +28 -0
  96. massgen/configs/providers/local/lmstudio.yaml +11 -0
  97. massgen/configs/providers/openai/gpt5.yaml +46 -0
  98. massgen/configs/providers/openai/gpt5_nano.yaml +46 -0
  99. massgen/configs/providers/others/grok_single_agent.yaml +19 -0
  100. massgen/configs/providers/others/zai_coding_team.yaml +108 -0
  101. massgen/configs/providers/others/zai_glm45.yaml +12 -0
  102. massgen/configs/{creative_team.yaml → teams/creative/creative_team.yaml} +16 -6
  103. massgen/configs/{travel_planning.yaml → teams/creative/travel_planning.yaml} +16 -6
  104. massgen/configs/{news_analysis.yaml → teams/research/news_analysis.yaml} +16 -6
  105. massgen/configs/{research_team.yaml → teams/research/research_team.yaml} +15 -7
  106. massgen/configs/{technical_analysis.yaml → teams/research/technical_analysis.yaml} +16 -6
  107. massgen/configs/tools/code-execution/basic_command_execution.yaml +25 -0
  108. massgen/configs/tools/code-execution/code_execution_use_case_simple.yaml +41 -0
  109. massgen/configs/tools/code-execution/docker_claude_code.yaml +32 -0
  110. massgen/configs/tools/code-execution/docker_multi_agent.yaml +32 -0
  111. massgen/configs/tools/code-execution/docker_simple.yaml +29 -0
  112. massgen/configs/tools/code-execution/docker_with_resource_limits.yaml +32 -0
  113. massgen/configs/tools/code-execution/multi_agent_playwright_automation.yaml +57 -0
  114. massgen/configs/tools/filesystem/cc_gpt5_gemini_filesystem.yaml +34 -0
  115. massgen/configs/tools/filesystem/claude_code_context_sharing.yaml +68 -0
  116. massgen/configs/tools/filesystem/claude_code_flash2.5.yaml +43 -0
  117. massgen/configs/tools/filesystem/claude_code_flash2.5_gptoss.yaml +49 -0
  118. massgen/configs/tools/filesystem/claude_code_gpt5nano.yaml +31 -0
  119. massgen/configs/tools/filesystem/claude_code_single.yaml +40 -0
  120. massgen/configs/tools/filesystem/fs_permissions_test.yaml +87 -0
  121. massgen/configs/tools/filesystem/gemini_gemini_workspace_cleanup.yaml +54 -0
  122. massgen/configs/tools/filesystem/gemini_gpt5_filesystem_casestudy.yaml +30 -0
  123. massgen/configs/tools/filesystem/gemini_gpt5nano_file_context_path.yaml +43 -0
  124. massgen/configs/tools/filesystem/gemini_gpt5nano_protected_paths.yaml +45 -0
  125. massgen/configs/tools/filesystem/gpt5mini_cc_fs_context_path.yaml +31 -0
  126. massgen/configs/tools/filesystem/grok4_gpt5_gemini_filesystem.yaml +32 -0
  127. massgen/configs/tools/filesystem/multiturn/grok4_gpt5_claude_code_filesystem_multiturn.yaml +58 -0
  128. massgen/configs/tools/filesystem/multiturn/grok4_gpt5_gemini_filesystem_multiturn.yaml +58 -0
  129. massgen/configs/tools/filesystem/multiturn/two_claude_code_filesystem_multiturn.yaml +47 -0
  130. massgen/configs/tools/filesystem/multiturn/two_gemini_flash_filesystem_multiturn.yaml +48 -0
  131. massgen/configs/tools/mcp/claude_code_discord_mcp_example.yaml +27 -0
  132. massgen/configs/tools/mcp/claude_code_simple_mcp.yaml +35 -0
  133. massgen/configs/tools/mcp/claude_code_twitter_mcp_example.yaml +32 -0
  134. massgen/configs/tools/mcp/claude_mcp_example.yaml +24 -0
  135. massgen/configs/tools/mcp/claude_mcp_test.yaml +27 -0
  136. massgen/configs/tools/mcp/five_agents_travel_mcp_test.yaml +157 -0
  137. massgen/configs/tools/mcp/five_agents_weather_mcp_test.yaml +103 -0
  138. massgen/configs/tools/mcp/gemini_mcp_example.yaml +24 -0
  139. massgen/configs/tools/mcp/gemini_mcp_filesystem_test.yaml +23 -0
  140. massgen/configs/tools/mcp/gemini_mcp_filesystem_test_sharing.yaml +23 -0
  141. massgen/configs/tools/mcp/gemini_mcp_filesystem_test_single_agent.yaml +17 -0
  142. massgen/configs/tools/mcp/gemini_mcp_filesystem_test_with_claude_code.yaml +24 -0
  143. massgen/configs/tools/mcp/gemini_mcp_test.yaml +27 -0
  144. massgen/configs/tools/mcp/gemini_notion_mcp.yaml +52 -0
  145. massgen/configs/tools/mcp/gpt5_nano_mcp_example.yaml +24 -0
  146. massgen/configs/tools/mcp/gpt5_nano_mcp_test.yaml +27 -0
  147. massgen/configs/tools/mcp/gpt5mini_claude_code_discord_mcp_example.yaml +38 -0
  148. massgen/configs/tools/mcp/gpt_oss_mcp_example.yaml +25 -0
  149. massgen/configs/tools/mcp/gpt_oss_mcp_test.yaml +28 -0
  150. massgen/configs/tools/mcp/grok3_mini_mcp_example.yaml +24 -0
  151. massgen/configs/tools/mcp/grok3_mini_mcp_test.yaml +27 -0
  152. massgen/configs/tools/mcp/multimcp_gemini.yaml +111 -0
  153. massgen/configs/tools/mcp/qwen_api_mcp_example.yaml +25 -0
  154. massgen/configs/tools/mcp/qwen_api_mcp_test.yaml +28 -0
  155. massgen/configs/tools/mcp/qwen_local_mcp_example.yaml +24 -0
  156. massgen/configs/tools/mcp/qwen_local_mcp_test.yaml +27 -0
  157. massgen/configs/tools/planning/five_agents_discord_mcp_planning_mode.yaml +140 -0
  158. massgen/configs/tools/planning/five_agents_filesystem_mcp_planning_mode.yaml +151 -0
  159. massgen/configs/tools/planning/five_agents_notion_mcp_planning_mode.yaml +151 -0
  160. massgen/configs/tools/planning/five_agents_twitter_mcp_planning_mode.yaml +155 -0
  161. massgen/configs/tools/planning/gpt5_mini_case_study_mcp_planning_mode.yaml +73 -0
  162. massgen/configs/tools/web-search/claude_streamable_http_test.yaml +43 -0
  163. massgen/configs/tools/web-search/gemini_streamable_http_test.yaml +43 -0
  164. massgen/configs/tools/web-search/gpt5_mini_streamable_http_test.yaml +43 -0
  165. massgen/configs/tools/web-search/gpt_oss_streamable_http_test.yaml +44 -0
  166. massgen/configs/tools/web-search/grok3_mini_streamable_http_test.yaml +43 -0
  167. massgen/configs/tools/web-search/qwen_api_streamable_http_test.yaml +44 -0
  168. massgen/configs/tools/web-search/qwen_local_streamable_http_test.yaml +43 -0
  169. massgen/coordination_tracker.py +708 -0
  170. massgen/docker/README.md +462 -0
  171. massgen/filesystem_manager/__init__.py +21 -0
  172. massgen/filesystem_manager/_base.py +9 -0
  173. massgen/filesystem_manager/_code_execution_server.py +545 -0
  174. massgen/filesystem_manager/_docker_manager.py +477 -0
  175. massgen/filesystem_manager/_file_operation_tracker.py +248 -0
  176. massgen/filesystem_manager/_filesystem_manager.py +813 -0
  177. massgen/filesystem_manager/_path_permission_manager.py +1261 -0
  178. massgen/filesystem_manager/_workspace_tools_server.py +1815 -0
  179. massgen/formatter/__init__.py +10 -0
  180. massgen/formatter/_chat_completions_formatter.py +284 -0
  181. massgen/formatter/_claude_formatter.py +235 -0
  182. massgen/formatter/_formatter_base.py +156 -0
  183. massgen/formatter/_response_formatter.py +263 -0
  184. massgen/frontend/__init__.py +1 -2
  185. massgen/frontend/coordination_ui.py +471 -286
  186. massgen/frontend/displays/base_display.py +56 -11
  187. massgen/frontend/displays/create_coordination_table.py +1956 -0
  188. massgen/frontend/displays/rich_terminal_display.py +1259 -619
  189. massgen/frontend/displays/simple_display.py +9 -4
  190. massgen/frontend/displays/terminal_display.py +27 -68
  191. massgen/logger_config.py +681 -0
  192. massgen/mcp_tools/README.md +232 -0
  193. massgen/mcp_tools/__init__.py +105 -0
  194. massgen/mcp_tools/backend_utils.py +1035 -0
  195. massgen/mcp_tools/circuit_breaker.py +195 -0
  196. massgen/mcp_tools/client.py +894 -0
  197. massgen/mcp_tools/config_validator.py +138 -0
  198. massgen/mcp_tools/docs/circuit_breaker.md +646 -0
  199. massgen/mcp_tools/docs/client.md +950 -0
  200. massgen/mcp_tools/docs/config_validator.md +478 -0
  201. massgen/mcp_tools/docs/exceptions.md +1165 -0
  202. massgen/mcp_tools/docs/security.md +854 -0
  203. massgen/mcp_tools/exceptions.py +338 -0
  204. massgen/mcp_tools/hooks.py +212 -0
  205. massgen/mcp_tools/security.py +780 -0
  206. massgen/message_templates.py +342 -64
  207. massgen/orchestrator.py +1515 -241
  208. massgen/stream_chunk/__init__.py +35 -0
  209. massgen/stream_chunk/base.py +92 -0
  210. massgen/stream_chunk/multimodal.py +237 -0
  211. massgen/stream_chunk/text.py +162 -0
  212. massgen/tests/mcp_test_server.py +150 -0
  213. massgen/tests/multi_turn_conversation_design.md +0 -8
  214. massgen/tests/test_azure_openai_backend.py +156 -0
  215. massgen/tests/test_backend_capabilities.py +262 -0
  216. massgen/tests/test_backend_event_loop_all.py +179 -0
  217. massgen/tests/test_chat_completions_refactor.py +142 -0
  218. massgen/tests/test_claude_backend.py +15 -28
  219. massgen/tests/test_claude_code.py +268 -0
  220. massgen/tests/test_claude_code_context_sharing.py +233 -0
  221. massgen/tests/test_claude_code_orchestrator.py +175 -0
  222. massgen/tests/test_cli_backends.py +180 -0
  223. massgen/tests/test_code_execution.py +679 -0
  224. massgen/tests/test_external_agent_backend.py +134 -0
  225. massgen/tests/test_final_presentation_fallback.py +237 -0
  226. massgen/tests/test_gemini_planning_mode.py +351 -0
  227. massgen/tests/test_grok_backend.py +7 -10
  228. massgen/tests/test_http_mcp_server.py +42 -0
  229. massgen/tests/test_integration_simple.py +198 -0
  230. massgen/tests/test_mcp_blocking.py +125 -0
  231. massgen/tests/test_message_context_building.py +29 -47
  232. massgen/tests/test_orchestrator_final_presentation.py +48 -0
  233. massgen/tests/test_path_permission_manager.py +2087 -0
  234. massgen/tests/test_rich_terminal_display.py +14 -13
  235. massgen/tests/test_timeout.py +133 -0
  236. massgen/tests/test_v3_3agents.py +11 -12
  237. massgen/tests/test_v3_simple.py +8 -13
  238. massgen/tests/test_v3_three_agents.py +11 -18
  239. massgen/tests/test_v3_two_agents.py +8 -13
  240. massgen/token_manager/__init__.py +7 -0
  241. massgen/token_manager/token_manager.py +400 -0
  242. massgen/utils.py +52 -16
  243. massgen/v1/agent.py +45 -91
  244. massgen/v1/agents.py +18 -53
  245. massgen/v1/backends/gemini.py +50 -153
  246. massgen/v1/backends/grok.py +21 -54
  247. massgen/v1/backends/oai.py +39 -111
  248. massgen/v1/cli.py +36 -93
  249. massgen/v1/config.py +8 -12
  250. massgen/v1/logging.py +43 -127
  251. massgen/v1/main.py +18 -32
  252. massgen/v1/orchestrator.py +68 -209
  253. massgen/v1/streaming_display.py +62 -163
  254. massgen/v1/tools.py +8 -12
  255. massgen/v1/types.py +9 -23
  256. massgen/v1/utils.py +5 -23
  257. massgen-0.1.0.dist-info/METADATA +1245 -0
  258. massgen-0.1.0.dist-info/RECORD +273 -0
  259. massgen-0.1.0.dist-info/entry_points.txt +2 -0
  260. massgen/frontend/logging/__init__.py +0 -9
  261. massgen/frontend/logging/realtime_logger.py +0 -197
  262. massgen-0.0.3.dist-info/METADATA +0 -568
  263. massgen-0.0.3.dist-info/RECORD +0 -76
  264. massgen-0.0.3.dist-info/entry_points.txt +0 -2
  265. /massgen/backend/{Function calling openai responses.md → docs/Function calling openai responses.md} +0 -0
  266. {massgen-0.0.3.dist-info → massgen-0.1.0.dist-info}/WHEEL +0 -0
  267. {massgen-0.0.3.dist-info → massgen-0.1.0.dist-info}/licenses/LICENSE +0 -0
  268. {massgen-0.0.3.dist-info → massgen-0.1.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,1050 @@
1
+ Gemini MCP Integration Analysis
2
+
3
+ ## Executive Summary
4
+
5
+ Gemini uses MCP sessions as tools by passing an MCP client session directly to the Gemini SDK, which automatically manages the session and enables seamless tool calling with MCP servers
6
+
7
+ Key features of the Gemini MCP integration include:
8
+
9
+ - **Session-based tool execution**: MCP tools are exposed as sessions to the Gemini SDK, which handles tool calling automatically
10
+ - **Robust error handling**: Circuit breaker patterns, exponential backoff retry logic, and graceful degradation
11
+ - **Resource management**: Proper connection lifecycle management with async context managers
12
+ - **Comprehensive monitoring**: Tool usage tracking, performance metrics, and detailed logging
13
+ - **Fallback mechanisms**: Automatic fallback to builtin tools when MCP connections fail
14
+
15
+ This approach provides a more streamlined user experience compared to manual tool calling, as the SDK handles the complexity of determining when and how to use tools based on the conversation context.
16
+
17
+ ## MCP Integration Architecture in Gemini Backend
18
+
19
+ ### Architecture Overview
20
+
21
+ The Gemini backend implements a sophisticated layered architecture for MCP integration. The visual diagrams below illustrate:
22
+
23
+ 1. **Architecture Overview**: Complete system architecture showing all layers and data flow
24
+ 2. **Detailed Component Architecture**: Internal structure of key components
25
+ 3. **Session-Based Tool Calling Process**: Sequence diagram of tool execution flow
26
+ 4. **Circuit Breaker State Machine**: State transitions for fault tolerance
27
+ 5. **Error Handling Decision Tree**: Logic flow for different error scenarios
28
+ 6. **Data Flow Architecture**: End-to-end data flow from input to output
29
+
30
+ These diagrams provide a comprehensive visual understanding of how MCP tools are integrated, configured, and executed within the Gemini backend:
31
+
32
+ ```mermaid
33
+ graph TB
34
+ %% Configuration Flow
35
+ subgraph "Configuration Layer"
36
+ A1[YAML Configuration] --> A2[mcp_servers<br/>allowed_tools<br/>exclude_tools]
37
+ A3[Circuit Breaker<br/>Settings] --> A2
38
+ end
39
+
40
+ %% Backend Initialization
41
+ subgraph "Backend Layer"
42
+ B1[GeminiBackend<br/>__init__] --> B2[MCP Config<br/>Extraction]
43
+ B2 --> B3[Circuit Breaker<br/>Initialization]
44
+ B3 --> B4[MultiMCPClient<br/>Reference Setup]
45
+ end
46
+
47
+ %% MCP Integration Core
48
+ subgraph "MCP Integration Layer"
49
+ C1[_setup_mcp_tools<br/>Method] --> C2[Configuration<br/>Validation]
50
+ C2 --> C3[Server<br/>Normalization]
51
+ C3 --> C4[Circuit Breaker<br/>Filtering]
52
+ C4 --> C5[MultiMCPClient<br/>Connection]
53
+
54
+ C6[stream_with_tools<br/>Method] --> C7[MCP Client<br/>Health Check]
55
+ C7 --> C8[Active Sessions<br/>Retrieval]
56
+ C8 --> C9[Gemini SDK<br/>Integration]
57
+ end
58
+
59
+ %% External Components
60
+ subgraph "External Systems"
61
+ D1[MCP Servers<br/>stdio/streamable-http]
62
+ D2[Gemini API<br/>SDK]
63
+ D3[Circuit Breaker<br/>State Store]
64
+ end
65
+
66
+ %% Data Flow
67
+ A1 --> B1
68
+ B4 --> C1
69
+ C5 --> D1
70
+ C9 --> D2
71
+ B3 --> D3
72
+
73
+ %% Tool Execution Flow
74
+ subgraph "Tool Execution Flow"
75
+ E1[User Query] --> E2[Gemini SDK<br/>Analysis]
76
+ E2 --> E3{Requires<br/>MCP Tools?}
77
+ E3 -->|Yes| E4[Automatic Tool<br/>Selection]
78
+ E3 -->|No| E5[Direct Response]
79
+ E4 --> E6[Tool Execution<br/>via Sessions]
80
+ E6 --> E7[Result Integration]
81
+ E7 --> E8[Final Response<br/>to User]
82
+ end
83
+
84
+ %% Error Handling
85
+ subgraph "Error Handling & Resilience"
86
+ F1[Connection<br/>Failures] --> F2[Circuit Breaker<br/>Activation]
87
+ F2 --> F3[Server<br/>Filtering]
88
+ F3 --> F4[Retry Logic<br/>with Backoff]
89
+ F4 --> F5{Recovery<br/>Possible?}
90
+ F5 -->|Yes| F6[Reconnection<br/>Attempt]
91
+ F5 -->|No| F7[Fallback to<br/>Builtin Tools]
92
+ F7 --> F8[Graceful<br/>Degradation]
93
+ end
94
+
95
+ %% Monitoring & Metrics
96
+ subgraph "Monitoring Layer"
97
+ G1[Tool Usage<br/>Counters] --> G2[Performance<br/>Metrics]
98
+ G2 --> G3[Error Tracking<br/>and Logging]
99
+ G3 --> G4[Circuit Breaker<br/>Events]
100
+ G4 --> G5[Health<br/>Monitoring]
101
+ end
102
+
103
+ %% Connect layers
104
+ C1 -.-> E1
105
+ D1 -.-> E6
106
+ E6 -.-> C9
107
+ F1 -.-> C7
108
+ C5 -.-> G1
109
+
110
+ style A1 fill:#e1f5fe
111
+ style B1 fill:#f3e5f5
112
+ style C1 fill:#e8f5e8
113
+ style D1 fill:#fff3e0
114
+ style E1 fill:#fce4ec
115
+ style F1 fill:#ffebee
116
+ style G1 fill:#f9fbe7
117
+ ```
118
+
119
+ ### Detailed Component Architecture
120
+
121
+ ```mermaid
122
+ graph TD
123
+ subgraph "GeminiBackend Class"
124
+ GB1[Configuration<br/>Management] --> GB2[MCP Client<br/>Lifecycle]
125
+ GB2 --> GB3[Tool Execution<br/>Coordination]
126
+ GB3 --> GB4[Error Handling<br/>& Recovery]
127
+ GB4 --> GB5[Resource<br/>Management]
128
+ end
129
+
130
+ subgraph "MultiMCPClient"
131
+ MC1[Server<br/>Discovery] --> MC2[Connection<br/>Management]
132
+ MC2 --> MC3[Session<br/>Handling]
133
+ MC3 --> MC4[Tool<br/>Registry]
134
+ MC4 --> MC5[Health<br/>Monitoring]
135
+ end
136
+
137
+ subgraph "Circuit Breaker System"
138
+ CB1[Failure<br/>Detection] --> CB2[State<br/>Management]
139
+ CB2 --> CB3[Recovery<br/>Logic]
140
+ CB3 --> CB4[Server<br/>Filtering]
141
+ CB4 --> CB5[Metrics<br/>Collection]
142
+ end
143
+
144
+ subgraph "Error Handler Components"
145
+ EH1[MCPErrorHandler] --> EH2[Error<br/>Classification]
146
+ EH2 --> EH3[Retry<br/>Decisions]
147
+ EH3 --> EH4[User<br/>Feedback]
148
+ EH4 --> EH5[Fallback<br/>Management]
149
+ end
150
+
151
+ subgraph "Backend Utils Integration"
152
+ BU1[MCPSetupManager] --> BU2[Server<br/>Normalization]
153
+ BU2 --> BU3[MCPExecutionManager]
154
+ BU3 --> BU4[MCPMessageManager]
155
+ BU4 --> BU5[MCPConfigHelper]
156
+ end
157
+
158
+ %% Data flow connections
159
+ GB1 --> MC1
160
+ GB2 --> MC2
161
+ GB3 --> MC3
162
+ GB4 --> CB1
163
+ CB2 --> MC5
164
+ EH1 --> GB4
165
+ BU1 --> GB1
166
+ BU3 --> GB3
167
+
168
+ %% Styling
169
+ classDef backend fill:#e3f2fd,stroke:#1976d2,stroke-width:2px
170
+ classDef client fill:#f3e5f5,stroke:#7b1fa2,stroke-width:2px
171
+ classDef circuit fill:#fff3e0,stroke:#f57c00,stroke-width:2px
172
+ classDef error fill:#ffebee,stroke:#d32f2f,stroke-width:2px
173
+ classDef utils fill:#e8f5e8,stroke:#388e3c,stroke-width:2px
174
+
175
+ class GB1,GB2,GB3,GB4,GB5 backend
176
+ class MC1,MC2,MC3,MC4,MC5 client
177
+ class CB1,CB2,CB3,CB4,CB5 circuit
178
+ class EH1,EH2,EH3,EH4,EH5 error
179
+ class BU1,BU2,BU3,BU4,BU5 utils
180
+ ```
181
+
182
+ ### Session-Based Tool Calling Process
183
+
184
+ ```mermaid
185
+ sequenceDiagram
186
+ participant U as User
187
+ participant GB as GeminiBackend
188
+ participant MMC as MultiMCPClient
189
+ participant GS as Gemini SDK
190
+ participant MS as MCP Server
191
+ participant CB as Circuit Breaker
192
+
193
+ Note over U,CB: User Query Processing
194
+
195
+ U->>GB: User query with tool requirements
196
+ GB->>GB: Check MCP client initialization
197
+ alt MCP client not initialized
198
+ GB->>MMC: Create MultiMCPClient
199
+ MMC->>CB: Check server health
200
+ CB-->>MMC: Return healthy servers
201
+ MMC->>MS: Connect to MCP servers
202
+ MS-->>MMC: Connection established
203
+ MMC-->>GB: Client ready
204
+ end
205
+
206
+ GB->>MMC: Get active sessions
207
+ MMC-->>GB: Return active MCP sessions
208
+
209
+ Note over GB,GS: Session Integration with SDK
210
+
211
+ GB->>GS: Generate content with MCP sessions
212
+ GS->>GS: Analyze query and available tools
213
+ GS->>GS: Determine tool requirements
214
+
215
+ alt Tool required
216
+ GS->>MMC: Execute tool via session
217
+ MMC->>MS: Forward tool call
218
+ MS->>MS: Execute tool logic
219
+ MS-->>MMC: Return tool result
220
+ MMC-->>GS: Tool result
221
+ GS->>GS: Integrate result into response
222
+ end
223
+
224
+ GS-->>GB: Generated response
225
+ GB->>CB: Record success/failure
226
+ GB-->>U: Final response with tool results
227
+
228
+ Note over U,CB: Error Handling Path
229
+
230
+ alt Tool execution fails
231
+ MMC->>GB: Tool execution error
232
+ GB->>CB: Record failure
233
+ CB->>CB: Update circuit breaker state
234
+ GB->>GB: Activate fallback mechanism
235
+ GB->>GS: Retry with builtin tools
236
+ GS-->>GB: Fallback response
237
+ GB-->>U: Response with fallback content
238
+ end
239
+ ```
240
+
241
+ ### Circuit Breaker State Machine
242
+
243
+ ```mermaid
244
+ stateDiagram-v2
245
+ [*] --> Closed: Initialization
246
+ Closed --> Open: Failure threshold reached
247
+ Open --> HalfOpen: Recovery timeout
248
+ HalfOpen --> Closed: Test request succeeds
249
+ HalfOpen --> Open: Test request fails
250
+
251
+ state Closed as "Closed State\n✅ All servers available\n✅ Normal operation"
252
+ state Open as "Open State\n❌ Server disabled\n❌ Requests blocked"
253
+ state HalfOpen as "Half-Open State\n⚠️ Testing recovery\n⚠️ Limited requests"
254
+ ```
255
+
256
+ ### Error Handling Decision Tree
257
+
258
+ ```mermaid
259
+ flowchart TD
260
+ A[Error Occurred] --> B{Error Type?}
261
+ B -->|MCPConnectionError| C[Connection Issue]
262
+ B -->|MCPTimeoutError| D[Timeout Issue]
263
+ B -->|MCPServerError| E[Server Error]
264
+ B -->|MCPValidationError| F[Validation Error]
265
+ B -->|Other| G[Unknown Error]
266
+
267
+ C --> H{Retry Count < Max?}
268
+ D --> H
269
+ E --> I{Contains retry keywords?}
270
+ F --> J[Immediate Failure]
271
+ G --> K[Log and Re-raise]
272
+
273
+ H -->|Yes| L[Exponential Backoff]
274
+ H -->|No| M[Fallback to Builtin]
275
+ I -->|timeout,connection,network,temporary,unavailable,5xx| N[Retryable Server Error]
276
+ I -->|other| O[Non-retryable Server Error]
277
+
278
+ L --> P[Retry Connection]
279
+ M --> Q[Use Builtin Tools]
280
+ N --> H
281
+ O --> J
282
+
283
+ P --> R{Success?}
284
+ Q --> S[Generate Response]
285
+
286
+ R -->|Yes| T[Continue Normal Flow]
287
+ R -->|No| H
288
+
289
+ J --> U[Raise Exception]
290
+ S --> V[Return Response]
291
+
292
+ style A fill:#ffebee,stroke:#d32f2f
293
+ style Q fill:#e8f5e8,stroke:#2e7d32
294
+ style U fill:#ffebee,stroke:#d32f2f
295
+ style T fill:#e8f5e8,stroke:#2e7d32
296
+ ```
297
+
298
+ ### Data Flow Architecture
299
+
300
+ ```mermaid
301
+ graph LR
302
+ subgraph "Input Sources"
303
+ Y1[YAML Config<br/>mcp_servers]
304
+ Y2[Environment<br/>Variables]
305
+ Y3[Runtime<br/>Parameters]
306
+ end
307
+
308
+ subgraph "Configuration Processing"
309
+ C1[MCPConfigValidator<br/>validate_backend_mcp_config]
310
+ C2[MCPSetupManager<br/>normalize_mcp_servers]
311
+ C3[MCPConfigHelper<br/>build_circuit_breaker_config]
312
+ end
313
+
314
+ subgraph "Backend Integration"
315
+ B1[GeminiBackend<br/>__init__]
316
+ B2[_setup_mcp_tools]
317
+ B3[stream_with_tools]
318
+ B4[Error Handlers]
319
+ end
320
+
321
+ subgraph "MCP Layer"
322
+ M1[MultiMCPClient<br/>create_and_connect]
323
+ M2[MCPClient<br/>Individual Servers]
324
+ M3[Active Sessions<br/>Tool Registry]
325
+ end
326
+
327
+ subgraph "Gemini SDK"
328
+ G1[generate_content_stream<br/>with sessions]
329
+ G2[Automatic Tool<br/>Detection]
330
+ G3[Tool Result<br/>Integration]
331
+ end
332
+
333
+ subgraph "Output"
334
+ O1[Stream Chunks<br/>to UI]
335
+ O2[Tool Results<br/>Integrated]
336
+ O3[Error Messages<br/>Fallback Content]
337
+ end
338
+
339
+ %% Data flow connections
340
+ Y1 --> C1
341
+ Y2 --> C1
342
+ Y3 --> B1
343
+
344
+ C1 --> C2
345
+ C2 --> C3
346
+ C3 --> B1
347
+
348
+ B1 --> B2
349
+ B2 --> M1
350
+ M1 --> M2
351
+ M2 --> M3
352
+
353
+ B3 --> G1
354
+ M3 -.-> G1
355
+ G1 --> G2
356
+ G2 --> G3
357
+
358
+ G3 --> O1
359
+ B4 --> O3
360
+ M2 -.-> O2
361
+
362
+ %% Styling
363
+ classDef input fill:#e3f2fd,stroke:#1976d2,stroke-width:2px
364
+ classDef config fill:#f3e5f5,stroke:#7b1fa2,stroke-width:2px
365
+ classDef backend fill:#fff3e0,stroke:#f57c00,stroke-width:2px
366
+ classDef mcp fill:#e8f5e8,stroke:#388e3c,stroke-width:2px
367
+ classDef sdk fill:#fce4ec,stroke:#c2185b,stroke-width:2px
368
+ classDef output fill:#f9fbe7,stroke:#689f38,stroke-width:2px
369
+
370
+ class Y1,Y2,Y3 input
371
+ class C1,C2,C3 config
372
+ class B1,B2,B3,B4 backend
373
+ class M1,M2,M3 mcp
374
+ class G1,G2,G3 sdk
375
+ class O1,O2,O3 output
376
+ ```
377
+
378
+ ### Initialization and Configuration Handling
379
+
380
+ The Gemini backend's MCP integration begins in the `__init__` method, where MCP-related configuration is extracted and validated:
381
+
382
+ ```python
383
+ def __init__(self, api_key: Optional[str] = None, **kwargs):
384
+ # MCP integration setup
385
+ self.mcp_servers = kwargs.pop("mcp_servers", [])
386
+ self.allowed_tools = kwargs.pop("allowed_tools", None)
387
+ self.exclude_tools = kwargs.pop("exclude_tools", None)
388
+ self._mcp_client: Optional[MultiMCPClient] = None
389
+ self._mcp_initialized = False
390
+ ```
391
+
392
+ The initialization process includes:
393
+
394
+ 1. **Configuration extraction**: MCP servers, tool filtering parameters, and circuit breaker settings
395
+ 2. **State initialization**: Client references, monitoring counters, and connection flags
396
+ 3. **Circuit breaker setup**: Optional circuit breaker initialization for fault tolerance
397
+ 4. **Validation preparation**: Setting up for later configuration validation
398
+
399
+ ### MCP Client Setup Through `_setup_mcp_tools()`
400
+
401
+ The `_setup_mcp_tools()` method handles the complex process of establishing MCP connections:
402
+
403
+ ```python
404
+ async def _setup_mcp_tools(self, agent_id: Optional[str] = None) -> None:
405
+ # Configuration validation
406
+ if MCPConfigValidator is not None:
407
+ validator = MCPConfigValidator()
408
+ validated_config = validator.validate_backend_mcp_config(backend_config)
409
+
410
+ # Server normalization and filtering
411
+ normalized_servers = self._normalize_mcp_servers()
412
+ filtered_servers = self._apply_mcp_tools_circuit_breaker_filtering(normalized_servers)
413
+
414
+ # Client creation with retry logic
415
+ self._mcp_client = await MultiMCPClient.create_and_connect(
416
+ filtered_servers,
417
+ timeout_seconds=30,
418
+ allowed_tools=allowed_tools,
419
+ exclude_tools=exclude_tools
420
+ )
421
+ ```
422
+
423
+ Key aspects of the setup process:
424
+
425
+ - **Validation**: Uses `MCPConfigValidator` to ensure configuration correctness
426
+ - **Normalization**: Converts various server configuration formats to standardized dictionaries
427
+ - **Circuit breaker filtering**: Removes servers that are currently failing based on circuit breaker state
428
+ - **Connection establishment**: Creates and connects to MCP servers with timeout handling
429
+ - **Success tracking**: Records successful connections for circuit breaker management
430
+
431
+ ### Session-Based Tool Execution in `stream_with_tools()`
432
+
433
+ The core of the Gemini MCP integration lies in the session-based approach within `stream_with_tools()`:
434
+
435
+ ```python
436
+ # Reuse active sessions from MultiMCPClient
437
+ mcp_sessions = self._mcp_client.get_active_sessions()
438
+ session_config = dict(config)
439
+ session_config["tools"] = mcp_sessions
440
+
441
+ # SDK handles automatic tool calling
442
+ stream = await client.aio.models.generate_content_stream(
443
+ model=model_name, contents=full_content, config=session_config
444
+ )
445
+ ```
446
+
447
+ This approach differs significantly from manual tool calling:
448
+
449
+ - **Automatic execution**: The Gemini SDK determines when tools are needed and calls them automatically
450
+ - **Session reuse**: Active MCP sessions are passed directly to the SDK
451
+ - **Transparent integration**: Tool results are seamlessly integrated into the response stream
452
+ - **No manual parsing**: No need to parse tool calls or manage execution manually
453
+
454
+ ### Relationship with MultiMCPClient
455
+
456
+ The Gemini backend relies heavily on the `MultiMCPClient` from `mcp_tools.client`:
457
+
458
+ - **Connection management**: Handles multiple MCP server connections simultaneously
459
+ - **Session provision**: Provides active sessions that can be used by the Gemini SDK
460
+ - **Tool filtering**: Applies allowed/excluded tool filters at the client level
461
+ - **Health monitoring**: Tracks connection status and provides server information
462
+
463
+ ## Specific mcp_tools Utilities Used
464
+
465
+ ### Used Utilities from backend_utils.py
466
+
467
+ The Gemini backend leverages several utilities from the `mcp_tools.backend_utils` module:
468
+
469
+ #### MCPSetupManager
470
+ - **Purpose**: Server configuration normalization and validation
471
+ - **Usage**: `normalize_mcp_servers()` method converts various server config formats
472
+ - **Implementation**: Validates required fields and ensures consistent structure
473
+
474
+ #### MCPExecutionManager
475
+ - **Purpose**: Function execution with retry logic (used in fallback scenarios)
476
+ - **Usage**: `execute_function_with_retry()` for manual tool calling when sessions fail
477
+ - **Features**: Exponential backoff, circuit breaker integration, statistics tracking
478
+
479
+ #### MCPErrorHandler
480
+ - **Purpose**: Standardized error handling and classification
481
+ - **Usage**: `get_error_details()`, `is_transient_error()`, `log_error()` methods
482
+ - **Benefits**: Consistent error messaging and retry decision logic
483
+
484
+ #### MCPCircuitBreakerManager
485
+ - **Purpose**: Circuit breaker operations for fault tolerance
486
+ - **Usage**: Server filtering, success/failure recording, event management
487
+ - **Methods**: `apply_circuit_breaker_filtering()`, `record_success()`, `record_failure()`
488
+
489
+ #### MCPMessageManager
490
+ - **Purpose**: Message history management
491
+ - **Usage**: `trim_message_history()` to prevent unbounded memory growth
492
+ - **Implementation**: Preserves system messages while limiting total message count
493
+
494
+ #### MCPConfigHelper
495
+ - **Purpose**: Configuration validation and circuit breaker setup
496
+ - **Usage**: `build_circuit_breaker_config()` for creating circuit breaker configurations
497
+ - **Features**: Transport-type-specific configurations, validation support
498
+
499
+ ### Used Exceptions from exceptions.py
500
+
501
+ The Gemini backend imports and handles all MCP exception types:
502
+
503
+ - **MCPError**: Base exception for all MCP-related errors
504
+ - **MCPConnectionError**: Connection establishment failures
505
+ - **MCPTimeoutError**: Operation timeout errors
506
+ - **MCPServerError**: Server-side errors and failures
507
+ - **MCPValidationError**: Configuration and input validation errors
508
+ - **MCPAuthenticationError**: Authentication and authorization failures
509
+ - **MCPResourceError**: Resource availability and access errors
510
+
511
+
512
+ ### Circuit Breaker Integration
513
+
514
+ The circuit breaker integration provides fault tolerance:
515
+
516
+ ```python
517
+ # Initialization
518
+ from ..mcp_tools.circuit_breaker import MCPCircuitBreaker
519
+ mcp_tools_config = MCPConfigHelper.build_circuit_breaker_config("mcp_tools")
520
+ self._mcp_tools_circuit_breaker = MCPCircuitBreaker(mcp_tools_config)
521
+
522
+ # Usage
523
+ filtered_servers = self._apply_mcp_tools_circuit_breaker_filtering(servers)
524
+ await self._record_mcp_tools_success(connected_servers)
525
+ await self._record_mcp_tools_failure(failed_servers, error_message)
526
+ ```
527
+
528
+ ## Configuration Flow
529
+
530
+ ### mcp_servers Parameter Handling
531
+
532
+ The configuration flow begins with the `mcp_servers` parameter in the constructor:
533
+
534
+ 1. **Extraction**: Retrieved from kwargs during backend initialization
535
+ 2. **Storage**: Stored as instance variable for later use
536
+ 3. **Validation**: Passed through `MCPConfigValidator` if available
537
+ 4. **Normalization**: Converted to standardized format using `MCPSetupManager`
538
+
539
+ ### Configuration Validation Using MCPConfigValidator
540
+
541
+ ```python
542
+ if MCPConfigValidator is not None:
543
+ validator = MCPConfigValidator()
544
+ validated_config = validator.validate_backend_mcp_config({
545
+ "mcp_servers": self.mcp_servers,
546
+ "allowed_tools": self.allowed_tools,
547
+ "exclude_tools": self.exclude_tools
548
+ })
549
+ ```
550
+
551
+ The validation process:
552
+ - **Schema validation**: Ensures required fields are present
553
+ - **Type checking**: Validates data types and formats
554
+ - **Security validation**: Checks security configurations
555
+ - **Tool filtering validation**: Validates allowed/excluded tool lists
556
+
557
+ ### Server Normalization and Filtering
558
+
559
+ Server configurations undergo multiple processing steps:
560
+
561
+ 1. **Normalization**: Convert various formats (dict, list) to standardized list of dictionaries
562
+ 2. **Required field validation**: Ensure 'type' and 'name' fields are present
563
+ 3. **Circuit breaker filtering**: Remove servers that are currently failing
564
+ 4. **Transport type separation**: Separate stdio/streamable-http from http servers
565
+
566
+ ### Tool Filtering with allowed_tools and exclude_tools
567
+
568
+ Tool filtering is applied at multiple levels:
569
+
570
+ - **Configuration level**: Specified in YAML configuration
571
+ - **Validation level**: Validated by `MCPConfigValidator`
572
+ - **Client level**: Applied when creating `MultiMCPClient`
573
+ - **Session level**: Enforced when sessions are created
574
+
575
+ ## Session-Based vs Manual Tool Calling
576
+
577
+ ### Session-Based Approach
578
+
579
+ The Gemini backend's session-based approach offers several advantages:
580
+
581
+ **Automatic Tool Selection**:
582
+ ```python
583
+ # Sessions are passed to SDK, which handles tool calling automatically
584
+ session_config["tools"] = mcp_sessions
585
+ stream = await client.aio.models.generate_content_stream(
586
+ model=model_name, contents=full_content, config=session_config
587
+ )
588
+ ```
589
+
590
+ **Benefits**:
591
+ - No manual tool call parsing required
592
+ - SDK determines optimal tool usage timing
593
+ - Seamless integration of tool results into response
594
+ - Reduced complexity in backend implementation
595
+
596
+ ### Automatic Tool Calling
597
+
598
+ The Gemini SDK handles tool calling automatically:
599
+
600
+ 1. **Context analysis**: SDK analyzes conversation context to determine tool needs
601
+ 2. **Tool selection**: Chooses appropriate tools from available sessions
602
+ 3. **Execution**: Calls tools and integrates results
603
+ 4. **Response generation**: Incorporates tool results into final response
604
+
605
+ ### Fallback Mechanisms
606
+
607
+ When MCP sessions fail, the system gracefully falls back:
608
+
609
+ ```python
610
+ except (MCPConnectionError, MCPTimeoutError, MCPServerError, MCPError) as e:
611
+ # Emit user-friendly error message
612
+ async for chunk in self._handle_mcp_error_and_fallback(e):
613
+ yield chunk
614
+
615
+ # Fallback to non-MCP streaming
616
+ manual_config = dict(config)
617
+ if all_tools:
618
+ manual_config["tools"] = all_tools
619
+ ```
620
+
621
+ **Fallback sequence**:
622
+ 1. MCP session failure detected
623
+ 2. User notification via stream chunks
624
+ 3. Fallback to builtin tools (search, code execution)
625
+ 4. Continue with standard Gemini capabilities
626
+
627
+
628
+ ## Error Handling and Resilience
629
+
630
+ ### Circuit Breaker Implementation
631
+
632
+ The circuit breaker prevents cascading failures:
633
+
634
+ ```python
635
+ class MCPCircuitBreaker:
636
+ def should_skip_server(self, server_name: str) -> bool:
637
+ # Check if server should be skipped due to failures
638
+
639
+ def record_success(self, server_name: str) -> None:
640
+ # Record successful operation
641
+
642
+ def record_failure(self, server_name: str) -> None:
643
+ # Record failure and potentially open circuit
644
+ ```
645
+
646
+ **Circuit breaker states**:
647
+ - **Closed**: Normal operation, all servers available
648
+ - **Open**: Server temporarily disabled due to failures
649
+ - **Half-open**: Testing server recovery
650
+
651
+ ### Retry Logic
652
+
653
+ Exponential backoff with jitter is implemented for connection attempts:
654
+
655
+ ```python
656
+ for retry_count in range(1, max_mcp_retries + 1):
657
+ try:
658
+ # Connection attempt
659
+ await asyncio.sleep(0.5 * retry_count) # Progressive backoff
660
+ self._mcp_client = await MultiMCPClient.create_and_connect(...)
661
+ break
662
+ except Exception as e:
663
+ # Handle retry logic
664
+ ```
665
+
666
+ **Retry characteristics**:
667
+ - Maximum 5 retry attempts
668
+ - Progressive backoff (0.5s, 1.0s, 1.5s, 2.0s, 2.5s)
669
+ - Circuit breaker integration
670
+ - User feedback on retry attempts
671
+
672
+ ### Error Classification
673
+
674
+ Different MCP error types receive different handling:
675
+
676
+ - **MCPConnectionError**: Retryable, circuit breaker tracked
677
+ - **MCPTimeoutError**: Retryable, may indicate server load
678
+ - **MCPServerError**: Conditionally retryable based on error message keywords
679
+ - **MCPAuthenticationError**: Non-retryable, immediate failure
680
+ - **MCPValidationError**: Non-retryable, configuration issue
681
+
682
+ ### Graceful Degradation
683
+
684
+ The system degrades gracefully when MCP tools fail:
685
+
686
+ 1. **Error detection**: Specific error types are identified
687
+ 2. **User notification**: Clear error messages via stream chunks
688
+ 3. **Fallback activation**: Switch to builtin tools or basic capabilities
689
+ 4. **Continued operation**: System remains functional without MCP tools
690
+
691
+ ### User Feedback
692
+
693
+ Error communication through stream chunks provides real-time feedback:
694
+
695
+ ```python
696
+ yield StreamChunk(
697
+ type="content",
698
+ content=f"\n⚠️ {user_message} ({error}); continuing without MCP tools\n",
699
+ )
700
+ ```
701
+
702
+ **Feedback characteristics**:
703
+ - Real-time error notifications
704
+ - User-friendly error messages
705
+ - Clear indication of fallback behavior
706
+ - No interruption of conversation flow
707
+
708
+ ## Tool Execution Flow
709
+
710
+ ### MCP Client Connection
711
+
712
+ The connection process involves multiple steps:
713
+
714
+ 1. **Configuration validation**: Validate server configurations
715
+ 2. **Server normalization**: Convert to standard format
716
+ 3. **Circuit breaker filtering**: Remove failing servers
717
+ 4. **Connection establishment**: Create MultiMCPClient connections
718
+ 5. **Session retrieval**: Get active sessions for SDK use
719
+
720
+ ### Session Management
721
+
722
+ Active sessions are managed through the MultiMCPClient:
723
+
724
+ ```python
725
+ mcp_sessions = self._mcp_client.get_active_sessions()
726
+ if not mcp_sessions:
727
+ raise RuntimeError("No active MCP sessions available")
728
+ ```
729
+
730
+ **Session characteristics**:
731
+ - Represent active MCP server connections
732
+ - Contain tool definitions and capabilities
733
+ - Can be passed directly to Gemini SDK
734
+ - Automatically handle tool execution
735
+
736
+ ### Streaming Integration
737
+
738
+ MCP tool results are integrated into the response stream:
739
+
740
+ ```python
741
+ async for chunk in stream:
742
+ if hasattr(chunk, "text") and chunk.text:
743
+ chunk_text = chunk.text
744
+ full_content_text += chunk_text
745
+ yield StreamChunk(type="content", content=chunk_text)
746
+ ```
747
+
748
+ **Integration features**:
749
+ - Real-time streaming of tool results
750
+ - Seamless integration with model responses
751
+ - Automatic tool usage indicators
752
+ - Comprehensive logging of tool activities
753
+
754
+ ### Tool Call Tracking
755
+
756
+ Comprehensive monitoring tracks tool usage:
757
+
758
+ ```python
759
+ # Track MCP tool usage attempt
760
+ self._mcp_tool_calls_count += 1
761
+ log_tool_call(agent_id, "mcp_session_tools", {
762
+ "session_count": len(mcp_sessions),
763
+ "call_number": self._mcp_tool_calls_count
764
+ })
765
+ ```
766
+
767
+ **Tracking metrics**:
768
+ - Total tool calls attempted
769
+ - Successful tool executions
770
+ - Failed tool attempts
771
+ - Connection retry counts
772
+
773
+ ### Success/Failure Recording
774
+
775
+ Circuit breaker events are recorded for each operation:
776
+
777
+ ```python
778
+ # Record success for connected servers
779
+ await self._record_mcp_tools_success(connected_servers)
780
+
781
+ # Record failure for circuit breaker
782
+ await self._record_mcp_tools_failure(failed_servers, error_message)
783
+ ```
784
+
785
+ ## Resource Management
786
+
787
+ ### Connection Lifecycle
788
+
789
+ MCP connections follow a structured lifecycle:
790
+
791
+ **Setup in `__aenter__`**:
792
+ ```python
793
+ async def __aenter__(self) -> "GeminiBackend":
794
+ await self._setup_mcp_tools(agent_id=self.agent_id)
795
+ return self
796
+ ```
797
+
798
+ **Cleanup in `__aexit__`**:
799
+ ```python
800
+ async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:
801
+ await self.cleanup_mcp()
802
+ ```
803
+
804
+ ### Client Cleanup
805
+
806
+ Proper disconnection is handled in the `cleanup_mcp()` method:
807
+
808
+ ```python
809
+ async def cleanup_mcp(self):
810
+ if self._mcp_client:
811
+ try:
812
+ await self._mcp_client.disconnect()
813
+ log_backend_activity("gemini", "MCP client disconnected", {})
814
+ except Exception as e:
815
+ self._mcp_error_details(e, "disconnect", log=True)
816
+ finally:
817
+ self._mcp_client = None
818
+ self._mcp_initialized = False
819
+ ```
820
+
821
+ ### Resource Tracking
822
+
823
+ Connection state and tool usage are continuously monitored:
824
+
825
+ - **Connection status**: Tracked through `_mcp_initialized` flag
826
+ - **Active sessions**: Monitored via `MultiMCPClient.get_active_sessions()`
827
+ - **Tool usage**: Counters for calls, successes, and failures
828
+ - **Circuit breaker state**: Per-server failure tracking
829
+
830
+ ### Memory Management
831
+
832
+ MCPMessageManager.trim_message_history() is called via gemini backend's _trim_message_history() wrapper method.
833
+
834
+ Message history is trimmed to prevent unbounded growth:
835
+
836
+ ```python
837
+ def _trim_message_history(self, messages: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
838
+ max_items = getattr(self, '_max_mcp_message_history', 200)
839
+ return MCPMessageManager.trim_message_history(messages, max_items)
840
+ ```
841
+
842
+ **Memory management features**:
843
+ - Configurable message history limits
844
+ - Preservation of system messages
845
+ - Automatic trimming during execution
846
+ - Prevention of memory leaks
847
+
848
+ ## Integration Points with MassGen Framework
849
+
850
+ ### Agent Configuration
851
+
852
+ MCP configuration flows from YAML to backend:
853
+
854
+ ```yaml
855
+ agents:
856
+ - id: "gemini_mcp_agent"
857
+ backend:
858
+ type: "gemini"
859
+ model: "gemini-2.5-flash"
860
+ mcp_servers:
861
+ - name: "weather"
862
+ type: "stdio"
863
+ command: "npx"
864
+ args: ["-y", "@fak111/weather-mcp"]
865
+ ```
866
+
867
+ **Configuration flow**:
868
+ 1. YAML parsing by MassGen orchestrator
869
+ 2. Backend instantiation with mcp_servers parameter
870
+ 3. Configuration validation and normalization
871
+ 4. MCP client setup and connection
872
+
873
+ ### Logging Integration
874
+
875
+ Comprehensive logging integrates with MassGen's logging system:
876
+
877
+ ```python
878
+ log_backend_activity("gemini", "MCP sessions initialized", {}, agent_id=agent_id)
879
+ log_tool_call(agent_id, "mcp_session_tools", tool_data, backend_name="gemini")
880
+ log_stream_chunk("backend.gemini", "content", chunk_text, agent_id)
881
+ ```
882
+
883
+ **Logging categories**:
884
+ - Backend activity logging
885
+ - Tool call logging
886
+ - Stream chunk logging
887
+ - Error and warning logging
888
+
889
+ ### UI Streaming
890
+
891
+ MCP tool results are streamed to the display layer:
892
+
893
+ ```python
894
+ yield StreamChunk(type="content", content="🔧 [MCP Tools] Session-based tools used\n")
895
+ yield StreamChunk(type="tool_calls", tool_calls=tool_calls_detected)
896
+ yield StreamChunk(type="complete_message", complete_message=complete_message)
897
+ ```
898
+
899
+ **Streaming features**:
900
+ - Real-time tool usage indicators
901
+ - Tool call result streaming
902
+ - Error message streaming
903
+ - Complete message assembly
904
+
905
+ ### Orchestrator Coordination
906
+
907
+ The orchestrator manages backend lifecycle:
908
+
909
+ - **Initialization**: Backend creation with MCP configuration
910
+ - **Context management**: Async context manager entry/exit
911
+ - **Resource cleanup**: Automatic cleanup on completion
912
+ - **Error handling**: Orchestrator-level error management
913
+
914
+ ## Performance and Monitoring
915
+
916
+ ### Tool Usage Tracking
917
+
918
+ Comprehensive metrics track MCP tool performance:
919
+
920
+ ```python
921
+ # Tool usage counters
922
+ self._mcp_tool_calls_count = 0
923
+ self._mcp_tool_failures = 0
924
+ self._mcp_tool_successes = 0
925
+ self._mcp_connection_retries = 0
926
+ ```
927
+
928
+ **Tracked metrics**:
929
+ - Total tool calls attempted
930
+ - Successful tool executions
931
+ - Failed tool attempts
932
+ - Connection retry attempts
933
+
934
+ ### Connection Retry Tracking
935
+
936
+ Connection attempts are monitored and logged:
937
+
938
+ ```python
939
+ for retry_count in range(1, max_mcp_retries + 1):
940
+ self._mcp_connection_retries = retry_count
941
+ log_backend_activity("gemini", "MCP connection retry", {
942
+ "attempt": retry_count,
943
+ "max_retries": max_mcp_retries
944
+ })
945
+ ```
946
+
947
+ ### Cost Calculation
948
+
949
+ MCP tool usage affects cost estimates:
950
+
951
+ ```python
952
+ def calculate_cost(self, input_tokens: int, output_tokens: int, model: str) -> float:
953
+ # Base model costs
954
+ input_cost = (input_tokens / 1_000_000) * rate
955
+ output_cost = (output_tokens / 1_000_000) * rate
956
+
957
+ # Tool usage costs (estimates)
958
+ tool_costs = 0.0
959
+ if self.search_count > 0:
960
+ tool_costs += self.search_count * 0.01
961
+
962
+ return input_cost + output_cost + tool_costs
963
+ ```
964
+
965
+ ### Circuit Breaker Metrics
966
+
967
+ Circuit breaker state provides failure tracking:
968
+
969
+ - **Failure counts**: Per-server failure tracking
970
+ - **Recovery timing**: Circuit breaker reset times
971
+ - **Success rates**: Success/failure ratios
972
+ - **Server availability**: Current server status
973
+
974
+ ## Configuration Examples
975
+
976
+ ### Single Server Setup (gemini_mcp_example.yaml)
977
+
978
+ ```yaml
979
+ agents:
980
+ - id: "gemini2.5flash_mcp_weather"
981
+ backend:
982
+ type: "gemini"
983
+ model: "gemini-2.5-flash"
984
+ mcp_servers:
985
+ - name: "weather"
986
+ type: "stdio"
987
+ command: "npx"
988
+ args: ["-y", "@fak111/weather-mcp"]
989
+ ```
990
+
991
+ **Configuration breakdown**:
992
+ - **Single server**: One MCP server for weather information
993
+ - **stdio transport**: Uses standard input/output for communication
994
+ - **NPM package**: Weather MCP server from npm registry
995
+ - **Simple setup**: Minimal configuration for basic MCP integration
996
+
997
+ ### Multi-Server Configuration (multimcp_gemini.yaml)
998
+
999
+ ```yaml
1000
+ mcp_servers:
1001
+ - name: "airbnb_search"
1002
+ type: "stdio"
1003
+ command: "npx"
1004
+ args: ["-y", "@openbnb/mcp-server-airbnb", "--ignore-robots-txt"]
1005
+ security:
1006
+ level: "moderate"
1007
+ - name: "brave_search"
1008
+ type: "stdio"
1009
+ command: "npx"
1010
+ args: ["-y", "@modelcontextprotocol/server-brave-search"]
1011
+ env:
1012
+ BRAVE_API_KEY: "${BRAVE_API_KEY}"
1013
+ security:
1014
+ level: "moderate"
1015
+ ```
1016
+
1017
+ **Configuration features**:
1018
+ - **Multiple servers**: Airbnb search and Brave search integration
1019
+ - **Environment variables**: Secure API key handling
1020
+ - **Security levels**: Moderate security configuration
1021
+ - **Complex workflows**: Support for multi-tool research tasks
1022
+
1023
+ ### Environment Variable Handling
1024
+
1025
+ Environment variables are securely managed:
1026
+
1027
+ ```yaml
1028
+ env:
1029
+ BRAVE_API_KEY: "${BRAVE_API_KEY}"
1030
+ ```
1031
+
1032
+ **Security features**:
1033
+ - **Variable substitution**: Runtime environment variable resolution
1034
+ - **Secure storage**: API keys stored in environment, not configuration
1035
+ - **Validation**: Environment variable presence validation
1036
+ - **Error handling**: Clear errors for missing required variables
1037
+
1038
+ ### Security Configuration
1039
+
1040
+ Security levels provide different protection levels:
1041
+
1042
+ ```yaml
1043
+ security:
1044
+ level: "moderate"
1045
+ ```
1046
+
1047
+ **Security levels**:
1048
+ - **strict**: Maximum security, limited tool access
1049
+ - **moderate**: Balanced security and functionality
1050
+ - **permissive**: Minimal restrictions, maximum functionality