Skip to content

Commit 620c241

Browse files
committed
Added get_prompts to the MultiServerMCPClient
1 parent 16d9202 commit 620c241

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

langchain_mcp_adapters/client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from langchain_core.messages import AIMessage, HumanMessage
1515
from langchain_core.tools import BaseTool
1616
from mcp import ClientSession
17+
from mcp.types import Prompt
1718

1819
from langchain_mcp_adapters.callbacks import CallbackContext, Callbacks
1920
from langchain_mcp_adapters.interceptors import ToolCallInterceptor
@@ -202,6 +203,14 @@ async def get_prompt(
202203
async with self.session(server_name) as session:
203204
return await load_mcp_prompt(session, prompt_name, arguments=arguments)
204205

206+
async def get_prompts(
207+
self, server_name: str, *, cursor: str | None = None
208+
) -> list[Prompt]:
209+
"""Get prompts from a given MCP server."""
210+
async with self.session(server_name) as session:
211+
prompts = await session.list_prompts(cursor=cursor)
212+
return prompts.prompts
213+
205214
async def get_resources(
206215
self,
207216
server_name: str,

tests/test_client.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import os
22
from pathlib import Path
33

4+
import pytest
45
from langchain_core.messages import AIMessage
56
from langchain_core.tools import BaseTool
7+
from mcp.types import Prompt
68

79
from langchain_mcp_adapters.client import MultiServerMCPClient
810
from langchain_mcp_adapters.tools import load_mcp_tools
@@ -161,3 +163,108 @@ async def test_get_prompt():
161163
assert isinstance(messages[0], AIMessage)
162164
assert "You are a helpful assistant" in messages[0].content
163165
assert "math, addition, multiplication" in messages[0].content
166+
167+
168+
async def test_get_prompts():
169+
"""Test retrieving prompts from MCP servers."""
170+
# Get the absolute path to the server scripts
171+
current_dir = Path(__file__).parent
172+
math_server_path = os.path.join(current_dir, "servers/math_server.py")
173+
174+
client = MultiServerMCPClient(
175+
{
176+
"math": {
177+
"command": "python3",
178+
"args": [math_server_path],
179+
"transport": "stdio",
180+
}
181+
},
182+
)
183+
# Test getting prompts from the math server
184+
prompts = await client.get_prompts(
185+
"math",
186+
)
187+
188+
# Check that we got multiple Prompts back
189+
assert len(prompts) == 3
190+
assert all(isinstance(prompt, Prompt) for prompt in prompts)
191+
192+
# Check the first prompt (configure_assistant)
193+
configure_prompt = [p for p in prompts if p.name == "configure_assistant"][0]
194+
assert configure_prompt.description == ""
195+
assert configure_prompt.title is None
196+
assert len(configure_prompt.arguments) == 1
197+
assert configure_prompt.arguments[0].name == "skills"
198+
assert configure_prompt.arguments[0].required is True
199+
200+
# Check the second prompt (math_problem_solver)
201+
solver_prompt = [p for p in prompts if p.name == "math_problem_solver"][0]
202+
assert solver_prompt.description == ""
203+
assert len(solver_prompt.arguments) == 1
204+
assert solver_prompt.arguments[0].name == "problem_type"
205+
assert solver_prompt.arguments[0].required is True
206+
207+
# Check the third prompt (calculation_guide)
208+
guide_prompt = [p for p in prompts if p.name == "calculation_guide"][0]
209+
assert guide_prompt.description == ""
210+
# This prompt has no arguments
211+
assert guide_prompt.arguments == []
212+
213+
214+
async def test_get_prompts_invalid_server():
215+
"""Test that get_prompts raises an error for invalid server name."""
216+
current_dir = Path(__file__).parent
217+
math_server_path = os.path.join(current_dir, "servers/math_server.py")
218+
219+
client = MultiServerMCPClient(
220+
{
221+
"math": {
222+
"command": "python3",
223+
"args": [math_server_path],
224+
"transport": "stdio",
225+
}
226+
},
227+
)
228+
229+
# Test getting prompts from a non-existent server
230+
with pytest.raises(ValueError) as exc_info:
231+
await client.get_prompts("nonexistent_server")
232+
233+
error_msg = str(exc_info.value)
234+
assert "Couldn't find a server with name 'nonexistent_server'" in error_msg
235+
assert "expected one of '['math']'" in error_msg
236+
237+
238+
async def test_get_prompts_multiple_servers(
239+
socket_enabled,
240+
websocket_server,
241+
websocket_server_port: int,
242+
):
243+
"""Test retrieving prompts from multiple servers."""
244+
current_dir = Path(__file__).parent
245+
math_server_path = os.path.join(current_dir, "servers/math_server.py")
246+
weather_server_path = os.path.join(current_dir, "servers/weather_server.py")
247+
248+
client = MultiServerMCPClient(
249+
{
250+
"math": {
251+
"command": "python3",
252+
"args": [math_server_path],
253+
"transport": "stdio",
254+
},
255+
"weather": {
256+
"command": "python3",
257+
"args": [weather_server_path],
258+
"transport": "stdio",
259+
},
260+
},
261+
)
262+
263+
# Test getting prompts from math server
264+
math_prompts = await client.get_prompts("math")
265+
assert len(math_prompts) == 3
266+
267+
# Test getting prompts from weather server (may have different prompts)
268+
weather_prompts = await client.get_prompts("weather")
269+
# Weather server may or may not have prompts, just verify it doesn't crash
270+
assert isinstance(weather_prompts, list)

0 commit comments

Comments
 (0)