forked from modelcontextprotocol/ruby-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtransport.rb
More file actions
59 lines (48 loc) · 1.56 KB
/
transport.rb
File metadata and controls
59 lines (48 loc) · 1.56 KB
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
# frozen_string_literal: true
require "securerandom"
module MCP
class Transport
# Initialize the transport with the server instance
def initialize(server)
@server = server
server.transport = self
end
# Send a response to the client
def send_response(response)
raise NotImplementedError, "Subclasses must implement send_response"
end
# Open the transport connection
def open
raise NotImplementedError, "Subclasses must implement open"
end
# Close the transport connection
def close
raise NotImplementedError, "Subclasses must implement close"
end
# Handle a JSON request
# Returns a response that should be sent back to the client
def handle_json_request(request)
response = @server.handle_json(request)
send_response(response) if response
end
# Handle an incoming request
# Returns a response that should be sent back to the client
def handle_request(request)
response = @server.handle(request)
send_response(response) if response
end
# Send a notification to the client.
# Returns true if the notification was sent successfully.
def send_notification(method, params = nil)
raise NotImplementedError, "Subclasses must implement send_notification"
end
# Send a JSON-RPC request to the client and wait for a response.
def send_request(method, params = nil)
raise NotImplementedError, "Subclasses must implement send_request"
end
private
def generate_request_id
SecureRandom.uuid
end
end
end