Skip to content

Commit e88deb6

Browse files
committed
Add prompts/list and prompts/get support to client
This commit adds two new methods to `MCP::Client`: - prompts: Lists available prompts from the server via `prompts/list` - get_prompt: Retrieves a specific prompt by name via `prompts/get` Both methods follow the same pattern as the existing resources support, including comprehensive test coverage and README documentation updates. ref. https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/docs/specification/2025-03-26/server/prompts.mdx#protocol-messages
1 parent 92a08ba commit e88deb6

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,8 @@ This class supports:
830830
- Tool invocation via the `tools/call` method (`MCP::Client#call_tools`)
831831
- Resource listing via the `resources/list` method (`MCP::Client#resources`)
832832
- Resource reading via the `resources/read` method (`MCP::Client#read_resources`)
833+
- Prompt listing via the `prompts/list` method (`MCP::Client#prompts`)
834+
- Prompt retrieval via the `prompts/get` method (`MCP::Client#get_prompt`)
833835
- Automatic JSON-RPC 2.0 message formatting
834836
- UUID request ID generation
835837

lib/mcp/client.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ def resources
5858
response.dig("result", "resources") || []
5959
end
6060

61+
# Returns the list of prompts available from the server.
62+
# Each call will make a new request – the result is not cached.
63+
#
64+
# @return [Array<Hash>] An array of available prompts.
65+
def prompts
66+
response = transport.send_request(request: {
67+
jsonrpc: JsonRpcHandler::Version::V2_0,
68+
id: request_id,
69+
method: "prompts/list",
70+
})
71+
72+
response.dig("result", "prompts") || []
73+
end
74+
6175
# Calls a tool via the transport layer and returns the full response from the server.
6276
#
6377
# @param tool [MCP::Client::Tool] The tool to be called.
@@ -96,6 +110,21 @@ def read_resource(uri:)
96110
response.dig("result", "contents") || []
97111
end
98112

113+
# Gets a prompt from the server by name and returns its details.
114+
#
115+
# @param name [String] The name of the prompt to get.
116+
# @return [Hash] A hash containing the prompt details.
117+
def get_prompt(name:)
118+
response = transport.send_request(request: {
119+
jsonrpc: JsonRpcHandler::Version::V2_0,
120+
id: request_id,
121+
method: "prompts/get",
122+
params: { name: name },
123+
})
124+
125+
response.fetch("result", {})
126+
end
127+
99128
private
100129

101130
def request_id

test/mcp/client_test.rb

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,135 @@ def test_read_resource_returns_empty_array_when_no_contents
141141

142142
assert_empty(contents)
143143
end
144+
145+
def test_prompts_sends_request_to_transport_and_returns_prompts_array
146+
transport = mock
147+
mock_response = {
148+
"result" => {
149+
"prompts" => [
150+
{
151+
"name" => "prompt_1",
152+
"description" => "First prompt",
153+
"arguments" => [
154+
{
155+
"name" => "code_1",
156+
"description" => "The code_1 to review",
157+
"required" => true,
158+
},
159+
],
160+
},
161+
{
162+
"name" => "prompt_2",
163+
"description" => "Second prompt",
164+
"arguments" => [
165+
{
166+
"name" => "code_2",
167+
"description" => "The code_2 to review",
168+
"required" => true,
169+
},
170+
],
171+
},
172+
],
173+
},
174+
}
175+
176+
# Only checking for the essential parts of the request
177+
transport.expects(:send_request).with do |args|
178+
args in { request: { method: "prompts/list", jsonrpc: "2.0" } }
179+
end.returns(mock_response).once
180+
181+
client = Client.new(transport: transport)
182+
prompts = client.prompts
183+
184+
assert_equal(2, prompts.size)
185+
assert_equal("prompt_1", prompts.first["name"])
186+
assert_equal("First prompt", prompts.first["description"])
187+
assert_equal("code_1", prompts.first["arguments"].first["name"])
188+
assert_equal("The code_1 to review", prompts.first["arguments"].first["description"])
189+
assert(prompts.first["arguments"].first["required"])
190+
191+
assert_equal("prompt_2", prompts.last["name"])
192+
assert_equal("Second prompt", prompts.last["description"])
193+
assert_equal("code_2", prompts.last["arguments"].first["name"])
194+
assert_equal("The code_2 to review", prompts.last["arguments"].first["description"])
195+
assert(prompts.last["arguments"].first["required"])
196+
end
197+
198+
def test_prompts_returns_empty_array_when_no_prompts
199+
transport = mock
200+
mock_response = { "result" => { "prompts" => [] } }
201+
202+
transport.expects(:send_request).returns(mock_response).once
203+
204+
client = Client.new(transport: transport)
205+
prompts = client.prompts
206+
207+
assert_empty(prompts)
208+
end
209+
210+
def test_get_prompt_sends_request_to_transport_and_returns_contents
211+
transport = mock
212+
name = "first_prompt"
213+
mock_response = {
214+
"result" => {
215+
"description" => "First prompt",
216+
"messages" => [
217+
{
218+
"role" => "user",
219+
"content" => {
220+
"text" => "First prompt content",
221+
"type" => "text",
222+
},
223+
},
224+
],
225+
},
226+
}
227+
228+
# Only checking for the essential parts of the request
229+
transport.expects(:send_request).with do |args|
230+
args in {
231+
request: {
232+
method: "prompts/get",
233+
jsonrpc: "2.0",
234+
params: {
235+
name: name,
236+
},
237+
},
238+
}
239+
end.returns(mock_response).once
240+
241+
client = Client.new(transport: transport)
242+
contents = client.get_prompt(name: name)
243+
244+
assert_equal("First prompt", contents["description"])
245+
assert_equal("user", contents["messages"].first["role"])
246+
assert_equal("First prompt content", contents["messages"].first["content"]["text"])
247+
end
248+
249+
def test_get_prompt_returns_empty_hash_when_no_contents
250+
transport = mock
251+
name = "nonexistent_prompt"
252+
mock_response = { "result" => {} }
253+
254+
transport.expects(:send_request).returns(mock_response).once
255+
256+
client = Client.new(transport: transport)
257+
contents = client.get_prompt(name: name)
258+
259+
assert_empty(contents)
260+
end
261+
262+
def test_get_prompt_returns_empty_hash
263+
transport = mock
264+
name = "nonexistent_prompt"
265+
mock_response = {}
266+
267+
transport.expects(:send_request).returns(mock_response).once
268+
269+
client = Client.new(transport: transport)
270+
contents = client.get_prompt(name: name)
271+
272+
assert_empty(contents)
273+
end
144274
end
145275
end

0 commit comments

Comments
 (0)