1
---
2
name: ruby-mcp-server-generator
3
description: 'Generate a complete Model Context Protocol server project in Ruby using the official MCP Ruby SDK gem.'
4
---
5
6
# Ruby MCP Server Generator
7
8
Generate a complete, production-ready MCP server in Ruby using the official Ruby SDK.
9
10
## Project Generation
11
12
When asked to create a Ruby MCP server, generate a complete project with this structure:
13
14
```
15
my-mcp-server/
16
├── Gemfile
17
├── Rakefile
18
├── lib/
19
│ ├── my_mcp_server.rb
20
│ ├── my_mcp_server/
21
│ │ ├── server.rb
22
│ │ ├── tools/
23
│ │ │ ├── greet_tool.rb
24
│ │ │ └── calculate_tool.rb
25
│ │ ├── prompts/
26
│ │ │ └── code_review_prompt.rb
27
│ │ └── resources/
28
│ │ └── example_resource.rb
29
├── bin/
30
│ └── mcp-server
31
├── test/
32
│ ├── test_helper.rb
33
│ └── tools/
34
│ ├── greet_tool_test.rb
35
│ └── calculate_tool_test.rb
36
└── README.md
37
```
38
39
## Gemfile Template
40
41
```ruby
42
source 'https://rubygems.org'
43
44
gem 'mcp', '~> 0.4.0'
45
46
group :development, :test do
47
gem 'minitest', '~> 5.0'
48
gem 'rake', '~> 13.0'
49
gem 'rubocop', '~> 1.50'
50
end
51
```
52
53
## Rakefile Template
54
55
```ruby
56
require 'rake/testtask'
57
require 'rubocop/rake_task'
58
59
Rake::TestTask.new(:test) do |t|
60
t.libs << 'test'
61
t.libs << 'lib'
62
t.test_files = FileList['test/**/*_test.rb']
63
end
64
65
RuboCop::RakeTask.new
66
67
task default: %i[test rubocop]
68
```
69
70
## lib/my_mcp_server.rb Template
71
72
```ruby
73
# frozen_string_literal: true
74
75
require 'mcp'
76
require_relative 'my_mcp_server/server'
77
require_relative 'my_mcp_server/tools/greet_tool'
78
require_relative 'my_mcp_server/tools/calculate_tool'
79
require_relative 'my_mcp_server/prompts/code_review_prompt'
80
require_relative 'my_mcp_server/resources/example_resource'
81
82
module MyMcpServer
83
VERSION = '1.0.0'
84
end
85
```
86
87
## lib/my_mcp_server/server.rb Template
88
89
```ruby
90
# frozen_string_literal: true
91
92
module MyMcpServer
93
class Server
94
attr_reader :mcp_server
95
96
def initialize(server_context: {})
97
@mcp_server = MCP::Server.new(
98
name: 'my_mcp_server',
99
version: MyMcpServer::VERSION,
100
tools: [
101
Tools::GreetTool,
102
Tools::CalculateTool
103
],
104
prompts: [
105
Prompts::CodeReviewPrompt
106
],
107
resources: [
108
Resources::ExampleResource.resource
109
],
110
server_context: server_context
111
)
112
113
setup_resource_handler
114
end
115
116
def handle_json(json_string)
117
mcp_server.handle_json(json_string)
118
end
119
120
def start_stdio
121
transport = MCP::Server::Transports::StdioTransport.new(mcp_server)
122
transport.open
123
end
124
125
private
126
127
def setup_resource_handler
128
mcp_server.resources_read_handler do |params|
129
Resources::ExampleResource.read(params[:uri])
130
end
131
end
132
end
133
end
134
```
135
136
## lib/my_mcp_server/tools/greet_tool.rb Template
137
138
```ruby
139
# frozen_string_literal: true
140
141
module MyMcpServer
142
module Tools
143
class GreetTool < MCP::Tool
144
tool_name 'greet'
145
description 'Generate a greeting message'
146
147
input_schema(
148
properties: {
149
name: {
150
type: 'string',
151
description: 'Name to greet'
152
}
153
},
154
required: ['name']
155
)
156
157
output_schema(
158
properties: {
159
message: { type: 'string' },
160
timestamp: { type: 'string', format: 'date-time' }
161
},
162
required: ['message', 'timestamp']
163
)
164
165
annotations(
166
read_only_hint: true,
167
idempotent_hint: true
168
)
169
170
def self.call(name:, server_context:)
171
timestamp = Time.now.iso8601
172
message = "Hello, #{name}! Welcome to MCP."
173
174
structured_data = {
175
message: message,
176
timestamp: timestamp
177
}
178
179
MCP::Tool::Response.new(
180
[{ type: 'text', text: message }],
181
structured_content: structured_data
182
)
183
end
184
end
185
end
186
end
187
```
188
189
## lib/my_mcp_server/tools/calculate_tool.rb Template
190
191
```ruby
192
# frozen_string_literal: true
193
194
module MyMcpServer
195
module Tools
196
class CalculateTool < MCP::Tool
197
tool_name 'calculate'
198
description 'Perform mathematical calculations'
199
200
input_schema(
201
properties: {
202
operation: {
203
type: 'string',
204
description: 'Operation to perform',
205
enum: ['add', 'subtract', 'multiply', 'divide']
206
},
207
a: {
208
type: 'number',
209
description: 'First operand'
210
},
211
b: {
212
type: 'number',
213
description: 'Second operand'
214
}
215
},
216
required: ['operation', 'a', 'b']
217
)
218
219
output_schema(
220
properties: {
221
result: { type: 'number' },
222
operation: { type: 'string' }
223
},
224
required: ['result', 'operation']
225
)
226
227
annotations(
228
read_only_hint: true,
229
idempotent_hint: true
230
)
231
232
def self.call(operation:, a:, b:, server_context:)
233
result = case operation
234
when 'add' then a + b
235
when 'subtract' then a - b
236
when 'multiply' then a * b
237
when 'divide'
238
return error_response('Division by zero') if b.zero?
239
a / b.to_f
240
else
241
return error_response("Unknown operation: #{operation}")
242
end
243
244
structured_data = {
245
result: result,
246
operation: operation
247
}
248
249
MCP::Tool::Response.new(
250
[{ type: 'text', text: "Result: #{result}" }],
251
structured_content: structured_data
252
)
253
end
254
255
def self.error_response(message)
256
MCP::Tool::Response.new(
257
[{ type: 'text', text: message }],
258
is_error: true
259
)
260
end
261
end
262
end
263
end
264
```
265
266
## lib/my_mcp_server/prompts/code_review_prompt.rb Template
267
268
```ruby
269
# frozen_string_literal: true
270
271
module MyMcpServer
272
module Prompts
273
class CodeReviewPrompt < MCP::Prompt
274
prompt_name 'code_review'
275
description 'Generate a code review prompt'
276
277
arguments [
278
MCP::Prompt::Argument.new(
279
name: 'language',
280
description: 'Programming language',
281
required: true
282
),
283
MCP::Prompt::Argument.new(
284
name: 'focus',
285
description: 'Review focus area (e.g., performance, security)',
286
required: false
287
)
288
]
289
290
meta(
291
version: '1.0',
292
category: 'development'
293
)
294
295
def self.template(args, server_context:)
296
language = args['language'] || 'Ruby'
297
focus = args['focus'] || 'general quality'
298
299
MCP::Prompt::Result.new(
300
description: "Code review for #{language} with focus on #{focus}",
301
messages: [
302
MCP::Prompt::Message.new(
303
role: 'user',
304
content: MCP::Content::Text.new(
305
"Please review this #{language} code with focus on #{focus}."
306
)
307
),
308
MCP::Prompt::Message.new(
309
role: 'assistant',
310
content: MCP::Content::Text.new(
311
"I'll review the code focusing on #{focus}. Please share the code."
312
)
313
),
314
MCP::Prompt::Message.new(
315
role: 'user',
316
content: MCP::Content::Text.new(
317
'[paste code here]'
318
)
319
)
320
]
321
)
322
end
323
end
324
end
325
end
326
```
327
328
## lib/my_mcp_server/resources/example_resource.rb Template
329
330
```ruby
331
# frozen_string_literal: true
332
333
module MyMcpServer
334
module Resources
335
class ExampleResource
336
RESOURCE_URI = 'resource://data/example'
337
338
def self.resource
339
MCP::Resource.new(
340
uri: RESOURCE_URI,
341
name: 'example-data',
342
description: 'Example resource data',
343
mime_type: 'application/json'
344
)
345
end
346
347
def self.read(uri)
348
return [] unless uri == RESOURCE_URI
349
350
data = {
351
message: 'Example resource data',
352
timestamp: Time.now.iso8601,
353
version: MyMcpServer::VERSION
354
}
355
356
[{
357
uri: uri,
358
mimeType: 'application/json',
359
text: data.to_json
360
}]
361
end
362
end
363
end
364
end
365
```
366
367
## bin/mcp-server Template
368
369
```ruby
370
#!/usr/bin/env ruby
371
# frozen_string_literal: true
372
373
require_relative '../lib/my_mcp_server'
374
375
begin
376
server = MyMcpServer::Server.new
377
server.start_stdio
378
rescue Interrupt
379
warn "\nShutting down server..."
380
exit 0
381
rescue StandardError => e
382
warn "Error: #{e.message}"
383
warn e.backtrace.join("\n")
384
exit 1
385
end
386
```
387
388
Make the file executable:
389
```bash
390
chmod +x bin/mcp-server
391
```
392
393
## test/test_helper.rb Template
394
395
```ruby
396
# frozen_string_literal: true
397
398
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
399
require 'my_mcp_server'
400
require 'minitest/autorun'
401
```
402
403
## test/tools/greet_tool_test.rb Template
404
405
```ruby
406
# frozen_string_literal: true
407
408
require 'test_helper'
409
410
module MyMcpServer
411
module Tools
412
class GreetToolTest < Minitest::Test
413
def test_greet_with_name
414
response = GreetTool.call(
415
name: 'Ruby',
416
server_context: {}
417
)
418
419
refute response.is_error
420
assert_equal 1, response.content.length
421
assert_match(/Ruby/, response.content.first[:text])
422
423
assert response.structured_content
424
assert_equal 'Hello, Ruby! Welcome to MCP.', response.structured_content[:message]
425
end
426
427
def test_output_schema_validation
428
response = GreetTool.call(
429
name: 'Test',
430
server_context: {}
431
)
432
433
assert response.structured_content.key?(:message)
434
assert response.structured_content.key?(:timestamp)
435
end
436
end
437
end
438
end
439
```
440
441
## test/tools/calculate_tool_test.rb Template
442
443
```ruby
444
# frozen_string_literal: true
445
446
require 'test_helper'
447
448
module MyMcpServer
449
module Tools
450
class CalculateToolTest < Minitest::Test
451
def test_addition
452
response = CalculateTool.call(
453
operation: 'add',
454
a: 5,
455
b: 3,
456
server_context: {}
457
)
458
459
refute response.is_error
460
assert_equal 8, response.structured_content[:result]
461
end
462
463
def test_subtraction
464
response = CalculateTool.call(
465
operation: 'subtract',
466
a: 10,
467
b: 4,
468
server_context: {}
469
)
470
471
refute response.is_error
472
assert_equal 6, response.structured_content[:result]
473
end
474
475
def test_multiplication
476
response = CalculateTool.call(
477
operation: 'multiply',
478
a: 6,
479
b: 7,
480
server_context: {}
481
)
482
483
refute response.is_error
484
assert_equal 42, response.structured_content[:result]
485
end
486
487
def test_division
488
response = CalculateTool.call(
489
operation: 'divide',
490
a: 15,
491
b: 3,
492
server_context: {}
493
)
494
495
refute response.is_error
496
assert_equal 5.0, response.structured_content[:result]
497
end
498
499
def test_division_by_zero
500
response = CalculateTool.call(
501
operation: 'divide',
502
a: 10,
503
b: 0,
504
server_context: {}
505
)
506
507
assert response.is_error
508
assert_match(/Division by zero/, response.content.first[:text])
509
end
510
511
def test_unknown_operation
512
response = CalculateTool.call(
513
operation: 'modulo',
514
a: 10,
515
b: 3,
516
server_context: {}
517
)
518
519
assert response.is_error
520
assert_match(/Unknown operation/, response.content.first[:text])
521
end
522
end
523
end
524
end
525
```
526
527
## README.md Template
528
529
````markdown
530
# My MCP Server
531
532
A Model Context Protocol server built with Ruby and the official MCP Ruby SDK.
533
534
## Features
535
536
- ✅ Tools: greet, calculate
537
- ✅ Prompts: code_review
538
- ✅ Resources: example-data
539
- ✅ Input/output schemas
540
- ✅ Tool annotations
541
- ✅ Structured content
542
- ✅ Full test coverage
543
544
## Requirements
545
546
- Ruby 3.0 or later
547
548
## Installation
549
550
```bash
551
bundle install
552
```
553
554
## Usage
555
556
### Stdio Transport
557
558
Run the server:
559
560
```bash
561
bundle exec bin/mcp-server
562
```
563
564
Then send JSON-RPC requests:
565
566
```bash
567
{"jsonrpc":"2.0","id":"1","method":"ping"}
568
{"jsonrpc":"2.0","id":"2","method":"tools/list"}
569
{"jsonrpc":"2.0","id":"3","method":"tools/call","params":{"name":"greet","arguments":{"name":"Ruby"}}}
570
```
571
572
### Rails Integration
573
574
Add to your Rails controller:
575
576
```ruby
577
class McpController < ApplicationController
578
def index
579
server = MyMcpServer::Server.new(
580
server_context: { user_id: current_user.id }
581
)
582
render json: server.handle_json(request.body.read)
583
end
584
end
585
```
586
587
## Testing
588
589
Run tests:
590
591
```bash
592
bundle exec rake test
593
```
594
595
Run linter:
596
597
```bash
598
bundle exec rake rubocop
599
```
600
601
Run all checks:
602
603
```bash
604
bundle exec rake
605
```
606
607
## Integration with Claude Desktop
608
609
Add to `claude_desktop_config.json`:
610
611
```json
612
{
613
"mcpServers": {
614
"my-mcp-server": {
615
"command": "bundle",
616
"args": ["exec", "bin/mcp-server"],
617
"cwd": "/path/to/my-mcp-server"
618
}
619
}
620
}
621
```
622
623
## Project Structure
624
625
```
626
my-mcp-server/
627
├── Gemfile # Dependencies
628
├── Rakefile # Build tasks
629
├── lib/ # Source code
630
│ ├── my_mcp_server.rb # Main entry point
631
│ └── my_mcp_server/ # Module namespace
632
│ ├── server.rb # Server setup
633
│ ├── tools/ # Tool implementations
634
│ ├── prompts/ # Prompt templates
635
│ └── resources/ # Resource handlers
636
├── bin/ # Executables
637
│ └── mcp-server # Stdio server
638
├── test/ # Test suite
639
│ ├── test_helper.rb # Test configuration
640
│ └── tools/ # Tool tests
641
└── README.md # This file
642
```
643
644
## License
645
646
MIT
647
````
648
649
## Generation Instructions
650
651
1. **Ask for project name and description**
652
2. **Generate all files** with proper naming and module structure
653
3. **Use classes for tools and prompts** for better organization
654
4. **Include input/output schemas** for type safety
655
5. **Add tool annotations** for behavior hints
656
6. **Include structured content** in responses
657
7. **Implement comprehensive tests** for all tools
658
8. **Follow Ruby conventions** (snake_case, modules, frozen_string_literal)
659
9. **Add proper error handling** with is_error flag
660
10. **Provide both stdio and HTTP** usage examples
661