blob: 9709718d972737b48049cd1eccfd5d42ce7f3148 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
import pytest
import asyncio
from unittest.mock import AsyncMock, MagicMock, patch
import mcp.types as types
from mcp_server_hello_world.server import HelloWorldServer, create_server, GREETING_PROMPT_TEMPLATE
class TestPrompts:
"""Test the prompt functionality."""
@pytest.mark.asyncio
async def test_list_prompts(self):
"""Test the list_prompts method."""
# Create a server
mcp = create_server()
# Get the list of prompts
prompts = await mcp.list_prompts()
# Verify the result
assert len(prompts) == 1
assert prompts[0].name == "greeting"
assert "simple greeting prompt" in prompts[0].description.lower()
@pytest.mark.asyncio
async def test_get_prompt(self):
"""Test the get_prompt method."""
# Create a server
mcp = create_server()
# Get the prompt
prompt = await mcp.get_prompt("greeting", {"name": "Test User"})
# Verify the result
expected_prompt = GREETING_PROMPT_TEMPLATE.format(name="Test User")
assert len(prompt.messages) > 0
assert prompt.messages[0].role == "user"
assert prompt.messages[0].content.type == "text"
assert expected_prompt.strip() in prompt.messages[0].content.text
# Test with unknown prompt
with pytest.raises(ValueError):
await mcp.get_prompt("unknown", {"name": "Test User"})
# Test with missing name argument
with pytest.raises(ValueError) as excinfo:
await mcp.get_prompt("greeting", {})
assert "Missing required arguments" in str(excinfo.value)
|