debug-mcp 0.1.2

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 (122) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/CHANGELOG.md +83 -0
  4. data/LICENSE +21 -0
  5. data/README.ja.md +383 -0
  6. data/README.md +384 -0
  7. data/examples/01_simple_bug.rb +43 -0
  8. data/examples/02_data_pipeline.rb +93 -0
  9. data/examples/03_recursion.rb +96 -0
  10. data/examples/RAILS_SCENARIOS.md +350 -0
  11. data/examples/SCENARIOS.md +142 -0
  12. data/examples/rails_test_app/setup.sh +428 -0
  13. data/examples/rails_test_app/testapp/.dockerignore +10 -0
  14. data/examples/rails_test_app/testapp/.ruby-version +1 -0
  15. data/examples/rails_test_app/testapp/Dockerfile +23 -0
  16. data/examples/rails_test_app/testapp/Gemfile +17 -0
  17. data/examples/rails_test_app/testapp/README.md +65 -0
  18. data/examples/rails_test_app/testapp/Rakefile +6 -0
  19. data/examples/rails_test_app/testapp/app/assets/images/.keep +0 -0
  20. data/examples/rails_test_app/testapp/app/assets/stylesheets/application.css +1 -0
  21. data/examples/rails_test_app/testapp/app/controllers/application_controller.rb +4 -0
  22. data/examples/rails_test_app/testapp/app/controllers/concerns/.keep +0 -0
  23. data/examples/rails_test_app/testapp/app/controllers/dashboard_controller.rb +38 -0
  24. data/examples/rails_test_app/testapp/app/controllers/health_controller.rb +11 -0
  25. data/examples/rails_test_app/testapp/app/controllers/orders_controller.rb +100 -0
  26. data/examples/rails_test_app/testapp/app/controllers/posts_controller.rb +82 -0
  27. data/examples/rails_test_app/testapp/app/controllers/sessions_controller.rb +25 -0
  28. data/examples/rails_test_app/testapp/app/controllers/users_controller.rb +44 -0
  29. data/examples/rails_test_app/testapp/app/helpers/application_helper.rb +2 -0
  30. data/examples/rails_test_app/testapp/app/models/application_record.rb +3 -0
  31. data/examples/rails_test_app/testapp/app/models/comment.rb +8 -0
  32. data/examples/rails_test_app/testapp/app/models/concerns/.keep +0 -0
  33. data/examples/rails_test_app/testapp/app/models/order.rb +56 -0
  34. data/examples/rails_test_app/testapp/app/models/order_item.rb +16 -0
  35. data/examples/rails_test_app/testapp/app/models/post.rb +29 -0
  36. data/examples/rails_test_app/testapp/app/models/user.rb +34 -0
  37. data/examples/rails_test_app/testapp/app/services/order_report_service.rb +40 -0
  38. data/examples/rails_test_app/testapp/app/views/layouts/application.html.erb +28 -0
  39. data/examples/rails_test_app/testapp/app/views/pwa/manifest.json.erb +22 -0
  40. data/examples/rails_test_app/testapp/app/views/pwa/service-worker.js +26 -0
  41. data/examples/rails_test_app/testapp/bin/ci +6 -0
  42. data/examples/rails_test_app/testapp/bin/dev +2 -0
  43. data/examples/rails_test_app/testapp/bin/rails +4 -0
  44. data/examples/rails_test_app/testapp/bin/rake +4 -0
  45. data/examples/rails_test_app/testapp/bin/setup +35 -0
  46. data/examples/rails_test_app/testapp/config/application.rb +42 -0
  47. data/examples/rails_test_app/testapp/config/boot.rb +3 -0
  48. data/examples/rails_test_app/testapp/config/ci.rb +14 -0
  49. data/examples/rails_test_app/testapp/config/database.yml +32 -0
  50. data/examples/rails_test_app/testapp/config/environment.rb +5 -0
  51. data/examples/rails_test_app/testapp/config/environments/development.rb +54 -0
  52. data/examples/rails_test_app/testapp/config/environments/production.rb +67 -0
  53. data/examples/rails_test_app/testapp/config/environments/test.rb +42 -0
  54. data/examples/rails_test_app/testapp/config/initializers/content_security_policy.rb +29 -0
  55. data/examples/rails_test_app/testapp/config/initializers/filter_parameter_logging.rb +8 -0
  56. data/examples/rails_test_app/testapp/config/initializers/inflections.rb +16 -0
  57. data/examples/rails_test_app/testapp/config/locales/en.yml +31 -0
  58. data/examples/rails_test_app/testapp/config/puma.rb +39 -0
  59. data/examples/rails_test_app/testapp/config/routes.rb +34 -0
  60. data/examples/rails_test_app/testapp/config.ru +6 -0
  61. data/examples/rails_test_app/testapp/db/migrate/20260216002916_create_users.rb +12 -0
  62. data/examples/rails_test_app/testapp/db/migrate/20260216002919_create_posts.rb +13 -0
  63. data/examples/rails_test_app/testapp/db/migrate/20260216002922_create_comments.rb +11 -0
  64. data/examples/rails_test_app/testapp/db/migrate/20260222000001_create_orders.rb +14 -0
  65. data/examples/rails_test_app/testapp/db/migrate/20260222000002_create_order_items.rb +13 -0
  66. data/examples/rails_test_app/testapp/db/schema.rb +71 -0
  67. data/examples/rails_test_app/testapp/db/seeds.rb +85 -0
  68. data/examples/rails_test_app/testapp/docker-compose.yml +21 -0
  69. data/examples/rails_test_app/testapp/docker-entrypoint.sh +10 -0
  70. data/examples/rails_test_app/testapp/lib/tasks/.keep +0 -0
  71. data/examples/rails_test_app/testapp/log/.keep +0 -0
  72. data/examples/rails_test_app/testapp/public/400.html +135 -0
  73. data/examples/rails_test_app/testapp/public/404.html +135 -0
  74. data/examples/rails_test_app/testapp/public/406-unsupported-browser.html +135 -0
  75. data/examples/rails_test_app/testapp/public/422.html +135 -0
  76. data/examples/rails_test_app/testapp/public/500.html +135 -0
  77. data/examples/rails_test_app/testapp/public/icon.png +0 -0
  78. data/examples/rails_test_app/testapp/public/icon.svg +3 -0
  79. data/examples/rails_test_app/testapp/public/robots.txt +1 -0
  80. data/examples/rails_test_app/testapp/script/.keep +0 -0
  81. data/examples/rails_test_app/testapp/storage/.keep +0 -0
  82. data/examples/rails_test_app/testapp/tmp/.keep +0 -0
  83. data/examples/rails_test_app/testapp/tmp/pids/.keep +0 -0
  84. data/examples/rails_test_app/testapp/tmp/storage/.keep +0 -0
  85. data/examples/rails_test_app/testapp/vendor/.keep +0 -0
  86. data/exe/debug-mcp +39 -0
  87. data/exe/debug-rails +127 -0
  88. data/lib/debug_mcp/client_cleanup.rb +102 -0
  89. data/lib/debug_mcp/code_safety_analyzer.rb +124 -0
  90. data/lib/debug_mcp/debug_client.rb +1143 -0
  91. data/lib/debug_mcp/exit_message_builder.rb +112 -0
  92. data/lib/debug_mcp/pending_http_helper.rb +25 -0
  93. data/lib/debug_mcp/rails_helper.rb +155 -0
  94. data/lib/debug_mcp/server.rb +364 -0
  95. data/lib/debug_mcp/session_manager.rb +436 -0
  96. data/lib/debug_mcp/stop_event_annotator.rb +152 -0
  97. data/lib/debug_mcp/tcp_session_discovery.rb +226 -0
  98. data/lib/debug_mcp/tools/connect.rb +669 -0
  99. data/lib/debug_mcp/tools/continue_execution.rb +161 -0
  100. data/lib/debug_mcp/tools/disconnect.rb +169 -0
  101. data/lib/debug_mcp/tools/evaluate_code.rb +354 -0
  102. data/lib/debug_mcp/tools/finish.rb +84 -0
  103. data/lib/debug_mcp/tools/get_context.rb +217 -0
  104. data/lib/debug_mcp/tools/get_source.rb +193 -0
  105. data/lib/debug_mcp/tools/inspect_object.rb +107 -0
  106. data/lib/debug_mcp/tools/list_debug_sessions.rb +60 -0
  107. data/lib/debug_mcp/tools/list_files.rb +189 -0
  108. data/lib/debug_mcp/tools/list_paused_sessions.rb +108 -0
  109. data/lib/debug_mcp/tools/next.rb +70 -0
  110. data/lib/debug_mcp/tools/rails_info.rb +200 -0
  111. data/lib/debug_mcp/tools/rails_model.rb +362 -0
  112. data/lib/debug_mcp/tools/rails_routes.rb +186 -0
  113. data/lib/debug_mcp/tools/read_file.rb +214 -0
  114. data/lib/debug_mcp/tools/remove_breakpoint.rb +173 -0
  115. data/lib/debug_mcp/tools/run_debug_command.rb +55 -0
  116. data/lib/debug_mcp/tools/run_script.rb +293 -0
  117. data/lib/debug_mcp/tools/set_breakpoint.rb +206 -0
  118. data/lib/debug_mcp/tools/step.rb +67 -0
  119. data/lib/debug_mcp/tools/trigger_request.rb +515 -0
  120. data/lib/debug_mcp/version.rb +5 -0
  121. data/lib/debug_mcp.rb +40 -0
  122. metadata +251 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4e1fb7afee534aad7a8eefa7dcf23af611f4a4ca0b630ce15abb8be9e4ee1bdb
4
+ data.tar.gz: ba299cabca9196b1e77ebf871891caf1452ea76c0f1b5010502875d65173a43e
5
+ SHA512:
6
+ metadata.gz: '0935b47a3b99932f43cc0d43ef4bfdb41a386e1f8ead818643281f86e2c68d0b3457f94e6807d1d16743a356c335c6b08f9e4133abe6142b4fefd92531380360'
7
+ data.tar.gz: 79a9660c468b25d384ddffcf7d6be41228dadd11b37a5cccc3e096a6cf43e6932b1392a755d796b109d8feea85f424042be57729222dbc363029dd29e40cf5d7
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,83 @@
1
+ # Changelog
2
+
3
+ ## Renamed: `girb-mcp` → `debug-mcp` (2026-04-28)
4
+
5
+ This gem was previously released on RubyGems as `girb-mcp`. It has been renamed to
6
+ `debug-mcp` to better reflect its purpose: an MCP server for Ruby's debug gem.
7
+
8
+ The first `debug-mcp` release is **0.1.2** (see entry below for internal-namespace
9
+ changes). If you used `girb-mcp`, replace it with `debug-mcp` in your Gemfile and
10
+ MCP client config:
11
+
12
+ ```ruby
13
+ # Gemfile
14
+ gem "debug-mcp" # was: gem "girb-mcp"
15
+ ```
16
+
17
+ ```json
18
+ // MCP client config
19
+ {
20
+ "mcpServers": {
21
+ "debug-mcp": { // was: "girb-mcp"
22
+ "command": "debug-mcp", // was: "girb-mcp"
23
+ "args": []
24
+ }
25
+ }
26
+ }
27
+ ```
28
+
29
+ The executable `girb-rails` was likewise renamed to `debug-rails`.
30
+
31
+ The version history for 0.1.0 and 0.1.1 below was originally published under the
32
+ name `girb-mcp`; the implementation is unchanged.
33
+
34
+ ## 0.1.2 — 2026-04-28
35
+
36
+ First release under the `debug-mcp` name.
37
+
38
+ ### Changes
39
+
40
+ - **Rename internal namespace from `girb` to `debug_mcp`** — Globals, symbols, and
41
+ log paths injected into the debugged Ruby process are now namespaced with
42
+ `debug_mcp` to match the gem name:
43
+ - `$_girb_orig_int`, `$_girb_int_at` → `$_debug_mcp_orig_int`, `$_debug_mcp_int_at`
44
+ (SIGINT trap save/restore)
45
+ - `$__girb_err`, `$__girb_cap` → `$__debug_mcp_err`, `$__debug_mcp_cap`
46
+ (`evaluate_code` error capture and stdout redirect)
47
+ - `:girb_health_check` → `:debug_mcp_health_check` (force_reset health probe)
48
+ - `/tmp/girb_debug.log` → `/tmp/debug_mcp.log` (internal debug log)
49
+
50
+ This is internal to debug-mcp and does not change any public API. If you wrote
51
+ Ruby code that read these globals from the debugged process directly, update
52
+ the names accordingly.
53
+
54
+ - **Add `base64` runtime dependency** — `base64` was removed from Ruby's default
55
+ gems in 3.4.0. `debug-mcp` uses `Base64.strict_encode64` to safely transmit
56
+ multi-line / non-ASCII code over the debug gem's line-based protocol, so it is
57
+ now declared explicitly in the gemspec to avoid `LoadError` on Ruby 3.4+.
58
+
59
+ ## 0.1.1 — 2026-03-01
60
+
61
+ ### Bug Fixes
62
+
63
+ - **Fix stale `pause` protocol messages causing session deadlock on remote connections** — For remote/Docker connections, `auto_repause!` sent 3–4 `pause PID\n` messages but only 1 was consumed; the rest accumulated in the debug gem's read buffer and fired as unexpected SIGURGs after `c` (continue), re-pausing the process with no client connected and blocking future connections. Fixed by adding a `check_paused` method that waits for the process to pause without sending a new `pause` message, and using it for all retry attempts in `auto_repause!`, `disconnect`, and `connect` (force_reset). Now only 1 `pause` message is sent per repause cycle.
64
+
65
+ - **Fix `auto_repause!` returning true while process is still running** — After `trigger_request` completes without hitting a breakpoint, `auto_repause!` reported success but `@paused` was actually `false`, causing all subsequent operations (`evaluate_code`, `set_breakpoint`, `disconnect`) to fail with "Process is not paused". Root cause: `attempt_trap_escape!` used passive `ensure_paused` (no SIGURG) instead of active `repause` when escape failed, leaving the process unpaused. Fixed by:
66
+ - Using active `repause` in `attempt_trap_escape!` when escape fails
67
+ - Adding recovery repause in `auto_repause!` after failed trap escape
68
+ - Returning actual `client.paused` state from `attempt_repause_after_no_hit` instead of unconditional `true`
69
+
70
+ ## 0.1.0 — 2026-03-01
71
+
72
+ Initial release.
73
+
74
+ ### Features
75
+
76
+ - **MCP server** with STDIO and Streamable HTTP transports
77
+ - **21 debugging tools**: connect, evaluate_code, inspect_object, get_context, get_source, read_file, list_files, set_breakpoint, remove_breakpoint, continue_execution, step, next, finish, run_script, trigger_request, disconnect, and more
78
+ - **Rails integration**: auto-detected rails_info, rails_routes, rails_model tools
79
+ - **Docker support**: TCP and Unix socket connections with automatic remote file reading
80
+ - **Signal trap context handling**: auto-escape on connect and after trigger_request
81
+ - **Code safety checker**: warns about dangerous operations in evaluate_code
82
+ - **Session management**: multiple concurrent sessions with automatic timeout cleanup
83
+ - **debug-rails CLI**: launch Rails server with debug enabled in one command
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Rira
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.ja.md ADDED
@@ -0,0 +1,383 @@
1
+ # debug-mcp
2
+
3
+ LLMエージェントをRubyの[debug gem](https://github.com/ruby/debug)に接続し、停止中のRubyプロセスの実行時コンテキストへのアクセスを提供するMCP (Model Context Protocol) サーバーです。
4
+
5
+ LLMエージェントが、停止中のRubyプロセスに接続し、変数の調査・コード評価・ブレークポイント設定・実行制御をMCPツール呼び出しだけで完結できます。MCP対応クライアントであれば何でも利用可能です。
6
+
7
+ ## できること
8
+
9
+ 既存のRuby/Rails向けMCPサーバーは静的解析やアプリケーションレベルのAPIにとどまっています。debug-mcpはdebug gem経由で**実行中のRubyプロセス**に接続し、その実行時状態をLLMエージェントに公開します。
10
+
11
+ ```
12
+ Agent → connect(host: "localhost", port: 12345)
13
+ Agent → get_context()
14
+ → ローカル変数、インスタンス変数、コールスタック
15
+ Agent → evaluate_code(code: "user.valid?")
16
+ → false
17
+ Agent → evaluate_code(code: "user.errors.full_messages")
18
+ → ["Email can't be blank"]
19
+ Agent → continue_execution()
20
+ ```
21
+
22
+ ## インストール
23
+
24
+ ```ruby
25
+ gem "debug-mcp"
26
+ ```
27
+
28
+ または直接インストール:
29
+
30
+ ```
31
+ gem install debug-mcp
32
+ ```
33
+
34
+ Ruby >= 3.2.0が必要です。
35
+
36
+ > **`girb-mcp` から移行する場合:** このgemは以前 `girb-mcp` という名前で
37
+ > RubyGemsに公開されていました(最終バージョン: 0.1.1)。0.1.2以降、用途を
38
+ > より明確にするため `debug-mcp` にリネームされました。Gemfileの
39
+ > `gem "girb-mcp"` を `gem "debug-mcp"` に置き換え、MCPクライアント設定の
40
+ > `girb-mcp` → `debug-mcp`、`girb-rails` → `debug-rails` に更新してください。
41
+ > 詳細は [CHANGELOG.md](CHANGELOG.md) を参照。
42
+
43
+ ## クイックスタート
44
+
45
+ ### 1. デバッガ付きでRubyプロセスを起動
46
+
47
+ ```bash
48
+ # スクリプト
49
+ rdbg --open --port=12345 my_script.rb
50
+
51
+ # 環境変数で指定
52
+ RUBY_DEBUG_OPEN=true RUBY_DEBUG_PORT=12345 ruby my_script.rb
53
+
54
+ # コード中に `debugger` / `binding.break` を記述して実行
55
+ rdbg --open my_script.rb
56
+ ```
57
+
58
+ ### 2. MCPクライアントの設定
59
+
60
+ debug-mcpはMCP対応クライアントであれば何でも動作します。お使いのクライアントのMCPサーバー設定に追加してください:
61
+
62
+ #### Claude Code
63
+
64
+ `~/.claude/settings.json`(またはプロジェクトの`.claude/settings.json`)に追加:
65
+
66
+ ```json
67
+ {
68
+ "mcpServers": {
69
+ "debug-mcp": {
70
+ "command": "debug-mcp",
71
+ "args": []
72
+ }
73
+ }
74
+ }
75
+ ```
76
+
77
+ Bundler経由の場合:
78
+
79
+ ```json
80
+ {
81
+ "mcpServers": {
82
+ "debug-mcp": {
83
+ "command": "bundle",
84
+ "args": ["exec", "debug-mcp"]
85
+ }
86
+ }
87
+ }
88
+ ```
89
+
90
+ #### Gemini CLI
91
+
92
+ `~/.gemini/settings.json` に追加:
93
+
94
+ ```json
95
+ {
96
+ "mcpServers": {
97
+ "debug-mcp": {
98
+ "command": "debug-mcp",
99
+ "args": []
100
+ }
101
+ }
102
+ }
103
+ ```
104
+
105
+ Bundler経由の場合:
106
+
107
+ ```json
108
+ {
109
+ "mcpServers": {
110
+ "debug-mcp": {
111
+ "command": "bundle",
112
+ "args": ["exec", "debug-mcp"]
113
+ }
114
+ }
115
+ }
116
+ ```
117
+
118
+ ### 3. デバッグ開始
119
+
120
+ エージェントに接続とデバッグを依頼:
121
+
122
+ > 「ポート12345のデバッグセッションに接続して現在の状態を見せて」
123
+
124
+ > 「app/models/user.rbの42行目にブレークポイントを設定して、/users/1にGETリクエストを送って」
125
+
126
+ ## 使い方
127
+
128
+ ```
129
+ Usage: debug-mcp [options]
130
+ -t, --transport TRANSPORT トランスポート: stdio(デフォルト)または http
131
+ -p, --port PORT HTTPポート(デフォルト: 6029、httpトランスポート時のみ)
132
+ --host HOST HTTPホスト(デフォルト: 127.0.0.1、httpトランスポート時のみ)
133
+ --session-timeout SECONDS セッションタイムアウト秒数(デフォルト: 1800)
134
+ -v, --version バージョン表示
135
+ -h, --help ヘルプ表示
136
+ ```
137
+
138
+ ### STDIOトランスポート(デフォルト)
139
+
140
+ 標準的なMCPクライアント向け。追加設定は不要です。
141
+
142
+ ```bash
143
+ debug-mcp
144
+ ```
145
+
146
+ ### HTTPトランスポート(Streamable HTTP)
147
+
148
+ ブラウザベースのクライアントやHTTP対応MCPクライアント向け。
149
+
150
+ ```bash
151
+ debug-mcp --transport http --port 8080
152
+ ```
153
+
154
+ MCPエンドポイントは `http://127.0.0.1:8080/mcp` で利用可能になります。
155
+
156
+ ### セッションタイムアウト
157
+
158
+ デバッグセッションは30分間操作がないと自動的にクリーンアップされます。変更するには:
159
+
160
+ ```bash
161
+ debug-mcp --session-timeout 3600 # 1時間
162
+ ```
163
+
164
+ セッションマネージャーは、対象プロセスが終了したセッションも検出して自動的にクリーンアップします。
165
+
166
+ ## ツール一覧
167
+
168
+ ### 接続・検出
169
+
170
+ | ツール | 説明 |
171
+ |------|------|
172
+ | `list_debug_sessions` | 利用可能なデバッグセッション一覧(Unixソケット) |
173
+ | `connect` | ソケットパスまたはTCPでデバッグセッションに接続 |
174
+ | `list_paused_sessions` | 接続中のセッション一覧 |
175
+
176
+ ### 調査
177
+
178
+ | ツール | 説明 |
179
+ |------|------|
180
+ | `evaluate_code` | 停止中のbindingでRubyコードを実行 |
181
+ | `inspect_object` | オブジェクトのクラス・値・インスタンス変数を取得 |
182
+ | `get_context` | ローカル変数・インスタンス変数・コールスタック・ブレークポイントを一括取得 |
183
+ | `get_source` | メソッドまたはクラスのソースコードを取得 |
184
+ | `read_file` | ソースファイルの読み取り(行範囲指定可) |
185
+ | `list_files` | ディレクトリ内のファイル一覧(globパターンでフィルタ可) |
186
+
187
+ ### 実行制御
188
+
189
+ | ツール | 説明 |
190
+ |------|------|
191
+ | `set_breakpoint` | ブレークポイント設定:行(file + line)、メソッド(`User#save`)、例外クラス |
192
+ | `remove_breakpoint` | file + line、メソッド名、例外クラス、または番号でブレークポイントを削除 |
193
+ | `continue_execution` | 次のブレークポイントまたは終了まで実行を再開 |
194
+ | `step` | ステップイン(メソッド呼び出しに入る) |
195
+ | `next` | ステップオーバー(次の行へ進む) |
196
+ | `finish` | 現在のメソッド/ブロックがreturnするまで実行 |
197
+ | `run_debug_command` | 任意のデバッガコマンドを直接実行 |
198
+ | `disconnect` | セッション切断とプロセス終了 |
199
+
200
+ ### 入口ツール
201
+
202
+ | ツール | 説明 |
203
+ |------|------|
204
+ | `run_script` | rdbg経由でRubyスクリプトを起動して接続 |
205
+ | `trigger_request` | デバッグ中のRailsアプリにHTTPリクエストを送信 |
206
+
207
+ ### Railsツール(自動検出)
208
+
209
+ Railsプロセスを検出すると自動的に登録されます。
210
+
211
+ | ツール | 説明 |
212
+ |------|------|
213
+ | `rails_info` | アプリ名・Rails/Rubyバージョン・環境・ルートパスを表示 |
214
+ | `rails_routes` | ルーティング一覧(verb, path, controller#action)、コントローラ・パスでフィルタ可能 |
215
+ | `rails_model` | モデル構造:カラム・アソシエーション・バリデーション・enum・スコープを表示 |
216
+
217
+ ## ワークフロー例
218
+
219
+ ### Rubyスクリプトのデバッグ
220
+
221
+ ```
222
+ Agent: run_script(file: "my_script.rb")
223
+ Agent: get_context()
224
+ Agent: evaluate_code(code: "result")
225
+ Agent: next()
226
+ Agent: evaluate_code(code: "result")
227
+ Agent: continue_execution()
228
+ ```
229
+
230
+ ### メソッドブレークポイント
231
+
232
+ ```
233
+ Agent: run_script(file: "my_script.rb", breakpoints: ["DataPipeline#validate"])
234
+ → スクリプトが起動し、DataPipeline#validate で停止
235
+ Agent: evaluate_code(code: "records")
236
+ Agent: continue_execution()
237
+ ```
238
+
239
+ ### 例外のキャッチとデバッグ
240
+
241
+ ```
242
+ Agent: run_script(file: "my_script.rb")
243
+ Agent: set_breakpoint(exception_class: "NoMethodError")
244
+ Agent: continue_execution()
245
+ → 例外が伝播する前に実行が停止
246
+ Agent: get_context()
247
+ Agent: evaluate_code(code: "$!.message")
248
+ ```
249
+
250
+ ### クラッシュ後の再起動
251
+
252
+ ```
253
+ → NoMethodError でプログラムがクラッシュ
254
+ Agent: run_script(file: "my_script.rb", restore_breakpoints: true)
255
+ → 前回のブレークポイントが自動復元
256
+ Agent: set_breakpoint(exception_class: "NoMethodError")
257
+ Agent: continue_execution()
258
+ → クラッシュ前に例外をキャッチ
259
+ ```
260
+
261
+ ### Railsリクエストのデバッグ
262
+
263
+ `debug-rails` でデバッグ有効状態のRailsサーバーを起動:
264
+
265
+ ```bash
266
+ debug-rails # RUBY_DEBUG_OPEN=true bin/rails server と同等
267
+ debug-rails s -p 4000 # ポート指定
268
+ debug-rails --debug-port 3333 # TCPデバッグポートを指定(Docker内で便利)
269
+ ```
270
+
271
+ エージェントにデバッグを依頼:
272
+
273
+ ```
274
+ Agent: connect()
275
+ Agent: set_breakpoint(file: "app/controllers/users_controller.rb", line: 15)
276
+ Agent: trigger_request(method: "GET", url: "http://localhost:3000/users/1")
277
+ Agent: get_context()
278
+ Agent: evaluate_code(code: "@user.attributes")
279
+ Agent: continue_execution()
280
+ ```
281
+
282
+ ### Docker内のRailsアプリをデバッグ
283
+
284
+ > **セキュリティ注意:** debug gemには認証機能がありません。デバッグポートにアクセスできれば、誰でもコンテナ内で任意のコードを実行できます。以下のようにアクセスを制限してください。
285
+
286
+ #### Option A: localhost限定TCP(シンプル)
287
+
288
+ デバッグポートを `127.0.0.1` にバインドし、ローカルプロセスのみ接続可能にします:
289
+
290
+ ```yaml
291
+ services:
292
+ web:
293
+ build: .
294
+ ports:
295
+ - "3000:3000"
296
+ - "127.0.0.1:12345:12345" # localhostのみ
297
+ environment:
298
+ - RUBY_DEBUG_OPEN=true
299
+ - RUBY_DEBUG_HOST=0.0.0.0
300
+ - RUBY_DEBUG_PORT=12345
301
+ ```
302
+
303
+ ```
304
+ Agent: connect(port: 12345)
305
+ ```
306
+
307
+ ローカルにソースコードがなくても、エージェントがコンテナ内のファイルを閲覧・読み取りできます。
308
+
309
+ #### Option B: Unixソケットボリュームマウント(推奨)
310
+
311
+ デバッグソケット用の共有ディレクトリをマウントします。ポート公開は不要です:
312
+
313
+ ```yaml
314
+ services:
315
+ web:
316
+ build: .
317
+ ports:
318
+ - "3000:3000"
319
+ environment:
320
+ - RUBY_DEBUG_OPEN=true
321
+ - RUBY_DEBUG_SOCK_PATH=/debug/rdbg.sock
322
+ volumes:
323
+ - debug_sock:/debug
324
+
325
+ volumes:
326
+ debug_sock:
327
+ ```
328
+
329
+ ```
330
+ Agent: connect(path: "/path/to/debug_sock/rdbg.sock")
331
+ ```
332
+
333
+ ### 既存のブレークポイントに接続
334
+
335
+ ```bash
336
+ # ターミナル: アプリが `debugger` 文に到達
337
+ rdbg --open my_app.rb
338
+ ```
339
+
340
+ ```
341
+ Agent: list_debug_sessions()
342
+ Agent: connect(path: "/tmp/rdbg-1000/rdbg-12345")
343
+ Agent: get_context()
344
+ ```
345
+
346
+ ## 仕組み
347
+
348
+ ```
349
+ ┌──────────────┐ STDIO or Streamable HTTP ┌───────────┐ TCP/Unixソケット ┌──────────────┐
350
+ │ MCPクライアント │ ◄──────────────────────► │ debug-mcp │ ◄──────────────────► │ Rubyプロセス │
351
+ │ │ (JSON-RPC) │(MCPサーバー) │ debug gemプロトコル │ (rdbg) │
352
+ └──────────────┘ └───────────┘ └──────────────┘
353
+ ```
354
+
355
+ 1. debug-mcpはSTDIO(デフォルト)またはStreamable HTTPで通信するMCPサーバーとして動作
356
+ 2. debug gem(`rdbg --open`)が対象Rubyプロセスにソケットを公開
357
+ 3. debug-mcpがdebug gemのワイヤープロトコルでそのソケットに接続
358
+ 4. MCPツール呼び出しがデバッガコマンドに変換され、結果が返される
359
+ 5. アイドル状態のセッションは設定可能なタイムアウト後に自動クリーンアップされる
360
+
361
+ ## セキュリティ
362
+
363
+ debug-mcpはデバッグツールであり、実行中のRubyプロセスへの深いランタイムアクセスを提供します。
364
+
365
+ **専用ツールにより任意コード実行を最小化。** 変数の確認・ソースコードの閲覧・モデル構造の調査など、ほとんどのデバッグ操作は任意コードを実行しない専用ツールで行われます。ランタイム検査用に`evaluate_code`も利用可能で、危険な操作に対しては組み込みの安全性チェッカーが警告します。
366
+
367
+ **debug gemには認証機能がありません。** デバッグソケットにアクセスできれば、対象プロセスで任意のコードを実行できます。必ずlocalhost(`127.0.0.1`)にバインドするか、Unixソケットを使用してください。具体的な設定例は[Docker節](#docker内のrailsアプリをデバッグ)を参照してください。
368
+
369
+ ## 関連プロジェクト
370
+
371
+ - [girb](https://github.com/rira100000000/girb) — 同じ思想を共有する、人間向けのAI搭載IRBアシスタント
372
+
373
+ ## 開発
374
+
375
+ ```bash
376
+ git clone https://github.com/rira100000000/debug-mcp.git
377
+ cd debug-mcp
378
+ bundle install
379
+ ```
380
+
381
+ ## ライセンス
382
+
383
+ MIT