Skip to content

Commit bcf1ddb

Browse files
committed
revert: remove additional async method and just keep sync
1 parent a3c1834 commit bcf1ddb

File tree

3 files changed

+6
-162
lines changed

3 files changed

+6
-162
lines changed

src/strands/tools/mcp/mcp_client.py

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -191,31 +191,6 @@ async def _list_prompts_async() -> ListPromptsResult:
191191

192192
return list_prompts_response
193193

194-
async def list_prompts_async(self, pagination_token: Optional[str] = None) -> ListPromptsResult:
195-
"""Asynchronously retrieves the list of available prompts from the MCP server.
196-
197-
This method calls the asynchronous list_prompts method on the MCP session
198-
and returns the raw ListPromptsResult with pagination support.
199-
200-
Args:
201-
pagination_token: Optional token for pagination
202-
203-
Returns:
204-
ListPromptsResult: The raw MCP response containing prompts and pagination info
205-
"""
206-
self._log_debug_with_thread("listing MCP prompts asynchronously")
207-
if not self._is_session_active():
208-
raise MCPClientInitializationError(CLIENT_SESSION_NOT_RUNNING_ERROR_MESSAGE)
209-
210-
async def _list_prompts_async() -> ListPromptsResult:
211-
return await self._background_thread_session.list_prompts(cursor=pagination_token)
212-
213-
future = self._invoke_on_background_thread(_list_prompts_async())
214-
list_prompts_response: ListPromptsResult = await asyncio.wrap_future(future)
215-
self._log_debug_with_thread("received %d prompts from MCP server", len(list_prompts_response.prompts))
216-
217-
return list_prompts_response
218-
219194
def get_prompt_sync(self, prompt_id: str, args: dict[str, Any]) -> GetPromptResult:
220195
"""Synchronously retrieves a prompt from the MCP server.
221196
@@ -238,29 +213,6 @@ async def _get_prompt_async() -> GetPromptResult:
238213

239214
return get_prompt_response
240215

241-
async def get_prompt_async(self, prompt_id: str, args: dict[str, Any]) -> GetPromptResult:
242-
"""Asynchronously retrieves a prompt from the MCP server.
243-
244-
Args:
245-
prompt_id: The ID of the prompt to retrieve
246-
args: Optional arguments to pass to the prompt
247-
248-
Returns:
249-
GetPromptResult: The prompt response from the MCP server
250-
"""
251-
self._log_debug_with_thread("getting MCP prompt asynchronously")
252-
if not self._is_session_active():
253-
raise MCPClientInitializationError(CLIENT_SESSION_NOT_RUNNING_ERROR_MESSAGE)
254-
255-
async def _get_prompt_async() -> GetPromptResult:
256-
return await self._background_thread_session.get_prompt(prompt_id, arguments=args)
257-
258-
future = self._invoke_on_background_thread(_get_prompt_async())
259-
get_prompt_response: GetPromptResult = await asyncio.wrap_future(future)
260-
self._log_debug_with_thread("received prompt from MCP server")
261-
262-
return get_prompt_response
263-
264216
def call_tool_sync(
265217
self,
266218
tool_use_id: str,

tests/strands/tools/mcp/test_mcp_client.py

Lines changed: 0 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -401,112 +401,4 @@ def test_get_prompt_sync_session_not_active():
401401
client.get_prompt_sync("test_prompt_id", {})
402402

403403

404-
# Prompt Tests - Async Methods
405404

406-
407-
@pytest.mark.asyncio
408-
async def test_list_prompts_async(mock_transport, mock_session):
409-
"""Test that list_prompts_async correctly retrieves prompts."""
410-
mock_prompt = Prompt(name="test_prompt", description="A test prompt", id="prompt_1")
411-
mock_result = ListPromptsResult(prompts=[mock_prompt])
412-
mock_session.list_prompts.return_value = mock_result
413-
414-
with MCPClient(mock_transport["transport_callable"]) as client:
415-
with (
416-
patch("asyncio.run_coroutine_threadsafe") as mock_run_coroutine_threadsafe,
417-
patch("asyncio.wrap_future") as mock_wrap_future,
418-
):
419-
mock_future = MagicMock()
420-
mock_run_coroutine_threadsafe.return_value = mock_future
421-
422-
async def mock_awaitable():
423-
return mock_result
424-
425-
mock_wrap_future.return_value = mock_awaitable()
426-
427-
result = await client.list_prompts_async()
428-
429-
mock_run_coroutine_threadsafe.assert_called_once()
430-
mock_wrap_future.assert_called_once_with(mock_future)
431-
432-
assert len(result.prompts) == 1
433-
assert result.prompts[0].name == "test_prompt"
434-
assert result.nextCursor is None
435-
436-
437-
@pytest.mark.asyncio
438-
async def test_list_prompts_async_with_pagination(mock_transport, mock_session):
439-
"""Test that list_prompts_async correctly handles pagination."""
440-
mock_prompt = Prompt(name="test_prompt", description="A test prompt", id="prompt_1")
441-
mock_result = ListPromptsResult(prompts=[mock_prompt], nextCursor="next_token")
442-
mock_session.list_prompts.return_value = mock_result
443-
444-
with MCPClient(mock_transport["transport_callable"]) as client:
445-
with (
446-
patch("asyncio.run_coroutine_threadsafe") as mock_run_coroutine_threadsafe,
447-
patch("asyncio.wrap_future") as mock_wrap_future,
448-
):
449-
mock_future = MagicMock()
450-
mock_run_coroutine_threadsafe.return_value = mock_future
451-
452-
async def mock_awaitable():
453-
return mock_result
454-
455-
mock_wrap_future.return_value = mock_awaitable()
456-
457-
result = await client.list_prompts_async(pagination_token="current_token")
458-
459-
mock_run_coroutine_threadsafe.assert_called_once()
460-
mock_wrap_future.assert_called_once_with(mock_future)
461-
462-
assert len(result.prompts) == 1
463-
assert result.prompts[0].name == "test_prompt"
464-
assert result.nextCursor == "next_token"
465-
466-
467-
@pytest.mark.asyncio
468-
async def test_list_prompts_async_session_not_active():
469-
"""Test that list_prompts_async raises an error when session is not active."""
470-
client = MCPClient(MagicMock())
471-
472-
with pytest.raises(MCPClientInitializationError, match="client session is not running"):
473-
await client.list_prompts_async()
474-
475-
476-
@pytest.mark.asyncio
477-
async def test_get_prompt_async(mock_transport, mock_session):
478-
"""Test that get_prompt_async correctly retrieves a prompt."""
479-
mock_message = PromptMessage(role="assistant", content=MCPTextContent(type="text", text="This is a test prompt"))
480-
mock_result = GetPromptResult(messages=[mock_message])
481-
mock_session.get_prompt.return_value = mock_result
482-
483-
with MCPClient(mock_transport["transport_callable"]) as client:
484-
with (
485-
patch("asyncio.run_coroutine_threadsafe") as mock_run_coroutine_threadsafe,
486-
patch("asyncio.wrap_future") as mock_wrap_future,
487-
):
488-
mock_future = MagicMock()
489-
mock_run_coroutine_threadsafe.return_value = mock_future
490-
491-
async def mock_awaitable():
492-
return mock_result
493-
494-
mock_wrap_future.return_value = mock_awaitable()
495-
496-
result = await client.get_prompt_async("test_prompt_id", {"key": "value"})
497-
498-
mock_run_coroutine_threadsafe.assert_called_once()
499-
mock_wrap_future.assert_called_once_with(mock_future)
500-
501-
assert len(result.messages) == 1
502-
assert result.messages[0].role == "assistant"
503-
assert result.messages[0].content.text == "This is a test prompt"
504-
505-
506-
@pytest.mark.asyncio
507-
async def test_get_prompt_async_session_not_active():
508-
"""Test that get_prompt_async raises an error when session is not active."""
509-
client = MCPClient(MagicMock())
510-
511-
with pytest.raises(MCPClientInitializationError, match="client session is not running"):
512-
await client.get_prompt_async("test_prompt_id", {})

tests_integ/test_mcp_client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,23 +214,23 @@ async def test_mcp_client_async():
214214
sse_mcp_client = MCPClient(lambda: sse_client("http://127.0.0.1:8005/sse"))
215215

216216
with sse_mcp_client:
217-
# Test async prompts functionality
218-
prompts_result = await sse_mcp_client.list_prompts_async()
217+
# Test sync prompts functionality
218+
prompts_result = sse_mcp_client.list_prompts_sync()
219219
assert len(prompts_result.prompts) >= 2
220220

221221
prompt_names = [prompt.name for prompt in prompts_result.prompts]
222222
assert "greeting_prompt" in prompt_names
223223
assert "math_prompt" in prompt_names
224224

225-
# Test get_prompt_async
226-
greeting_result = await sse_mcp_client.get_prompt_async("greeting_prompt", {"name": "Bob"})
225+
# Test get_prompt_sync
226+
greeting_result = sse_mcp_client.get_prompt_sync("greeting_prompt", {"name": "Bob"})
227227
assert len(greeting_result.messages) > 0
228228
prompt_text = greeting_result.messages[0].content.text
229229
assert "Hello, Bob!" in prompt_text
230230
assert "How are you today?" in prompt_text
231231

232-
# Test async pagination for prompts
233-
prompts_with_pagination = await sse_mcp_client.list_prompts_async(pagination_token="test_token")
232+
# Test sync pagination for prompts
233+
prompts_with_pagination = sse_mcp_client.list_prompts_sync(pagination_token="test_token")
234234
assert len(prompts_with_pagination.prompts) >= 0
235235

236236
# Test async tools functionality (existing)

0 commit comments

Comments
 (0)