-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrack_middleware.rb
executable file
·100 lines (83 loc) · 2.55 KB
/
rack_middleware.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env ruby
# frozen_string_literal: true
# Example of using MCP as a Rack middleware
require 'bundler/setup'
Bundler.require(:default, :examples)
require 'fast_mcp'
require 'rack'
require 'rack/handler/puma'
# Define tools using the class inheritance approach
class GreetTool < FastMcp::Tool
description 'Greet a person'
arguments do
required(:name).filled(:string).description('The name of the person to greet')
end
def call(name:)
"Hello, #{name}!"
end
end
class CalculateTool < FastMcp::Tool
description 'Perform a calculation'
arguments do
required(:operation).filled(:string).value(included_in?: %w[add subtract multiply
divide]).description('The operation to perform')
required(:x).filled(:float).description('The first number')
required(:y).filled(:float).description('The second number')
end
def call(operation:, x:, y:) # rubocop:disable Naming/MethodParameterName
case operation
when 'add'
x + y
when 'subtract'
x - y
when 'multiply'
x * y
when 'divide'
x / y
else
raise "Unknown operation: #{operation}"
end
end
end
class HelloWorldResource < FastMcp::Resource
uri 'file://hello_world'
resource_name 'Hello World'
description 'A simple hello world program'
mime_type 'text/plain'
def content
'puts "Hello, world!"'
end
end
# Create a simple Rack application
app = lambda do |_env|
[200, { 'Content-Type' => 'text/html' },
['<html><body><h1>Hello from Rack!</h1><p>This is a simple Rack app with MCP middleware.</p></body></html>']]
end
# Create the MCP middleware
mcp_app = FastMcp.rack_middleware(
app,
name: 'example-mcp-server', version: '1.0.0',
logger: Logger.new($stdout)
) do |server|
# Register tool classes
server.register_tools(GreetTool, CalculateTool)
# Register a sample resource
server.register_resource(HelloWorldResource)
end
# Run the Rack application with Puma
puts 'Starting Rack application with MCP middleware on http://localhost:9292'
puts 'MCP endpoints:'
puts ' - http://localhost:9292/mcp/sse (SSE endpoint)'
puts ' - http://localhost:9292/mcp/messages (JSON-RPC endpoint)'
puts 'Press Ctrl+C to stop'
# Use the Puma server directly instead of going through Rack::Handler
require 'puma'
require 'puma/configuration'
require 'puma/launcher'
app = Rack::Builder.new { run mcp_app }
config = Puma::Configuration.new do |user_config|
user_config.bind 'tcp://localhost:9292'
user_config.app app
end
launcher = Puma::Launcher.new(config)
launcher.run