Skip to content

Commit 089fdc4

Browse files
committed
client/http: add send_notification method for JSON-RPC notifications
- Add send_notification method to send notifications without expecting response - Automatically updates session header before sending - Silently handles errors as notifications don't expect responses
1 parent 137887a commit 089fdc4

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

lib/mcp/client/http.rb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ def send_request(request:)
2222

2323
# Update session header if we have one
2424
update_session_header!
25-
25+
2626
response = client.post("", request)
27-
27+
2828
# Store session ID from response headers if present
2929
if response.headers["Mcp-Session-Id"]
3030
@session_id = response.headers["Mcp-Session-Id"]
3131
end
3232

3333
# Handle different response types based on content-type
3434
content_type = response.headers["content-type"]
35-
35+
3636
parsed_body = if content_type&.include?("text/event-stream")
3737
# Parse SSE response
3838
parse_sse_response(response.body)
@@ -86,6 +86,20 @@ def send_request(request:)
8686
)
8787
end
8888

89+
# Sends a JSON-RPC notification (no response expected).
90+
#
91+
# @param notification [Hash] The JSON-RPC notification to send
92+
# @return [nil]
93+
def send_notification(notification:)
94+
update_session_header!
95+
client.post("", notification)
96+
nil
97+
rescue Faraday::Error
98+
# Notifications don't expect a response, so we can silently fail
99+
# or log if needed
100+
nil
101+
end
102+
89103
private
90104

91105
attr_reader :headers
@@ -103,11 +117,11 @@ def client
103117
headers.each do |key, value|
104118
faraday.headers[key] = value
105119
end
106-
120+
107121
# Use a middleware that doesn't auto-parse to handle both content types
108122
faraday.response do |env|
109123
content_type = env.response_headers["content-type"]
110-
124+
111125
# Only auto-parse JSON, leave SSE as raw text
112126
if content_type&.include?("application/json")
113127
require "json"

0 commit comments

Comments
 (0)