Skip to content

Commit 6881c0b

Browse files
committed
feat: add get_instances_by_security_group prompt
Add a prompt that guides LLM to find compute instances associated with a specific security group using existing tools. Closes #101
1 parent 5a1df75 commit 6881c0b

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from fastmcp import FastMCP
2+
3+
4+
def register_prompt(mcp: FastMCP):
5+
"""
6+
Register Openstack MCP prompts.
7+
"""
8+
9+
@mcp.prompt()
10+
def get_instances_by_security_group(security_group_name: str) -> str:
11+
"""
12+
Get instances associated with a specific security group.
13+
14+
:param security_group_name: The name of the security group to filter instances by.
15+
"""
16+
return (
17+
f"Find all compute instances that have the security group "
18+
f"'{security_group_name}' attached.\n\n"
19+
f"Steps:\n"
20+
f"1. Call get_servers to list all servers.\n"
21+
f"2. Check each server's security_groups field.\n"
22+
f"3. Return only the servers where security_groups contains "
23+
f"an entry with name '{security_group_name}'.\n"
24+
f"4. For each matching server, show the server name, ID, "
25+
f"status, and the full list of its security groups."
26+
)

src/openstack_mcp_server/server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from fastmcp.server.middleware.error_handling import ErrorHandlingMiddleware
33
from fastmcp.server.middleware.logging import LoggingMiddleware
44

5+
from openstack_mcp_server.prompts import register_prompt
56
from openstack_mcp_server.tools import register_tool
67

78

@@ -13,7 +14,7 @@ def serve(transport: str, **kwargs):
1314

1415
register_tool(mcp)
1516
# resister_resources(mcp)
16-
# register_prompt(mcp)
17+
register_prompt(mcp)
1718

1819
# Add middlewares
1920
mcp.add_middleware(ErrorHandlingMiddleware())

tests/prompts/test_prompts.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from openstack_mcp_server.prompts import register_prompt
2+
3+
4+
def test_get_instances_by_security_group_prompt_registered():
5+
"""Test that the prompt is registered with the MCP instance."""
6+
from unittest.mock import MagicMock
7+
8+
mcp = MagicMock()
9+
register_prompt(mcp)
10+
mcp.prompt.assert_called()
11+
12+
13+
def test_get_instances_by_security_group_prompt_content():
14+
"""Test that the prompt returns expected content."""
15+
from fastmcp import FastMCP
16+
17+
mcp = FastMCP("test")
18+
register_prompt(mcp)
19+
20+
prompts = mcp._prompt_manager._prompts
21+
assert "get_instances_by_security_group" in prompts
22+
23+
prompt_obj = prompts["get_instances_by_security_group"]
24+
assert prompt_obj.fn is not None
25+
26+
result = prompt_obj.fn(security_group_name="my-sg")
27+
assert "my-sg" in result
28+
assert "get_servers" in result
29+
assert "security_groups" in result

0 commit comments

Comments
 (0)