Skip to content

Commit 4907c4d

Browse files
author
PR-Contributor
committed
test: add server-level tests for remove_prompt and remove_resource
Add comprehensive tests in test_server.py following the same pattern as remove_tool tests: - test_remove_prompt: basic removal from server - test_remove_nonexistent_prompt: error path - test_remove_prompt_and_list: verify client list_prompts reflects removal - test_remove_resource: basic removal from server - test_remove_nonexistent_resource: error path - test_remove_resource_and_list: verify client list_resources reflects removal - test_remove_resource_template: template removal - test_remove_nonexistent_template: template error path These tests verify the protocol-level behavior when resources/prompts are removed, ensuring clients see the changes correctly.
1 parent 3bfb34b commit 4907c4d

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

tests/server/mcpserver/test_server.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,3 +1481,141 @@ async def test_report_progress_passes_related_request_id():
14811481
message="halfway",
14821482
related_request_id="req-abc-123",
14831483
)
1484+
1485+
1486+
class TestRemovePrompt:
1487+
"""Test remove_prompt functionality in MCPServer."""
1488+
1489+
async def test_remove_prompt(self):
1490+
"""Test removing a prompt from the server."""
1491+
mcp = MCPServer()
1492+
1493+
@mcp.prompt()
1494+
def fn() -> str:
1495+
return "Hello, world!"
1496+
1497+
# Verify prompt exists
1498+
assert len(mcp._prompt_manager.list_prompts()) == 1
1499+
1500+
# Remove the prompt
1501+
mcp.remove_prompt("fn")
1502+
1503+
# Verify prompt is removed
1504+
assert len(mcp._prompt_manager.list_prompts()) == 0
1505+
1506+
async def test_remove_nonexistent_prompt(self):
1507+
"""Test that removing a non-existent prompt raises PromptError."""
1508+
from mcp.server.mcpserver.exceptions import PromptError
1509+
1510+
mcp = MCPServer()
1511+
1512+
with pytest.raises(PromptError, match="Unknown prompt: nonexistent"):
1513+
mcp.remove_prompt("nonexistent")
1514+
1515+
async def test_remove_prompt_and_list(self):
1516+
"""Test that a removed prompt doesn't appear in list_prompts."""
1517+
mcp = MCPServer()
1518+
1519+
@mcp.prompt()
1520+
def prompt1() -> str:
1521+
return "Prompt 1"
1522+
1523+
@mcp.prompt()
1524+
def prompt2() -> str:
1525+
return "Prompt 2"
1526+
1527+
# Verify both prompts exist
1528+
async with Client(mcp) as client:
1529+
prompts = await client.list_prompts()
1530+
assert len(prompts.prompts) == 2
1531+
1532+
# Remove one prompt
1533+
mcp.remove_prompt("prompt1")
1534+
1535+
# Verify only one prompt remains
1536+
async with Client(mcp) as client:
1537+
prompts = await client.list_prompts()
1538+
assert len(prompts.prompts) == 1
1539+
assert prompts.prompts[0].name == "prompt2"
1540+
1541+
1542+
class TestRemoveResource:
1543+
"""Test remove_resource functionality in MCPServer."""
1544+
1545+
async def test_remove_resource(self):
1546+
"""Test removing a resource from the server."""
1547+
mcp = MCPServer()
1548+
1549+
@mcp.resource("resource://test")
1550+
def get_resource() -> str:
1551+
return "Hello, world!"
1552+
1553+
# Verify resource exists
1554+
assert len(mcp._resource_manager.list_resources()) == 1
1555+
1556+
# Remove the resource
1557+
mcp.remove_resource("resource://test")
1558+
1559+
# Verify resource is removed
1560+
assert len(mcp._resource_manager.list_resources()) == 0
1561+
1562+
async def test_remove_nonexistent_resource(self):
1563+
"""Test that removing a non-existent resource raises ResourceError."""
1564+
from mcp.server.mcpserver.exceptions import ResourceError
1565+
1566+
mcp = MCPServer()
1567+
1568+
with pytest.raises(ResourceError, match="Unknown resource: nonexistent"):
1569+
mcp.remove_resource("nonexistent")
1570+
1571+
async def test_remove_resource_and_list(self):
1572+
"""Test that a removed resource doesn't appear in list_resources."""
1573+
mcp = MCPServer()
1574+
1575+
@mcp.resource("resource://test1")
1576+
def resource1() -> str:
1577+
return "Resource 1"
1578+
1579+
@mcp.resource("resource://test2")
1580+
def resource2() -> str:
1581+
return "Resource 2"
1582+
1583+
# Verify both resources exist
1584+
async with Client(mcp) as client:
1585+
resources = await client.list_resources()
1586+
assert len(resources.resources) == 2
1587+
1588+
# Remove one resource
1589+
mcp.remove_resource("resource://test1")
1590+
1591+
# Verify only one resource remains
1592+
async with Client(mcp) as client:
1593+
resources = await client.list_resources()
1594+
assert len(resources.resources) == 1
1595+
assert resources.resources[0].uri == "resource://test2"
1596+
1597+
async def test_remove_resource_template(self):
1598+
"""Test removing a resource template from the server."""
1599+
mcp = MCPServer()
1600+
1601+
@mcp.resource("resource://{name}")
1602+
def get_resource(name: str) -> str:
1603+
return f"Hello, {name}!"
1604+
1605+
# Verify template exists
1606+
assert len(mcp._resource_manager.list_templates()) == 1
1607+
1608+
# Remove the template
1609+
mcp.remove_resource_template("resource://{name}")
1610+
1611+
# Verify template is removed
1612+
assert len(mcp._resource_manager.list_templates()) == 0
1613+
1614+
async def test_remove_nonexistent_template(self):
1615+
"""Test that removing a non-existent template raises ResourceError."""
1616+
from mcp.server.mcpserver.exceptions import ResourceError
1617+
1618+
mcp = MCPServer()
1619+
1620+
with pytest.raises(ResourceError, match="Unknown template: nonexistent"):
1621+
mcp.remove_resource_template("nonexistent")

0 commit comments

Comments
 (0)