Skip to content

Extended prompt support #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions langchain_mcp_adapters/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,25 @@ async def load_mcp_prompt(
convert_mcp_prompt_message_to_langchain_message(message)
for message in response.messages
]

# Added a function to list ALL available MCP prompts

async def list_mcp_prompts(session: ClientSession) -> list[dict[str, Any]]:
"""List all available MCP prompts.

Args:
session: The MCP client session.

Returns:
A list of dictionaries containing prompt details.
"""
prompts = await session.list_prompts()

return [
{
"name": prompt.name,
"description": prompt.description,
"arguments": prompt.arguments
}
for prompt in prompts.prompts
]
41 changes: 41 additions & 0 deletions tests/test_prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
PromptMessage,
TextContent,
TextResourceContents,
PromptArgument,
)

from langchain_mcp_adapters.prompts import (
convert_mcp_prompt_message_to_langchain_message,
load_mcp_prompt,
list_mcp_prompts,
)


Expand Down Expand Up @@ -81,3 +83,42 @@ async def test_load_mcp_prompt():
assert result[0].content == "Hello"
assert isinstance(result[1], AIMessage)
assert result[1].content == "Hi"

@pytest.mark.asyncio
async def test_list_mcp_prompts():
greeting_mock = AsyncMock()
greeting_mock.name = 'greeting'
greeting_mock.description = 'A simple greeting prompt'
greeting_mock.arguments = [
PromptArgument(name="name", description="User's name", required=True),
PromptArgument(name="language", description="Language code", required=False)
]

summary_mock = AsyncMock()
summary_mock.name = 'summary'
summary_mock.description = 'Summarize text content'
summary_mock.arguments = [
PromptArgument(name="text", description="Text to summarize", required=True),
PromptArgument(name="max_length", description="Maximum length", required=False)
]

session = AsyncMock()
session.list_prompts = AsyncMock(
return_value=AsyncMock(
prompts=[greeting_mock, summary_mock]
)
)

result = await list_mcp_prompts(session)

assert len(result) == 2
assert result[0]["name"] == "greeting"
assert result[0]["description"] == "A simple greeting prompt"
assert len(result[0]["arguments"]) == 2
assert result[0]["arguments"][0].name == "name"
assert result[0]["arguments"][1].name == "language"
assert result[1]["name"] == "summary"
assert result[1]["description"] == "Summarize text content"
assert len(result[1]["arguments"]) == 2
assert result[1]["arguments"][0].name == "text"
assert result[1]["arguments"][1].name == "max_length"