Skip to content

Commit 001db85

Browse files
committed
Update tests for MCPAgent
1 parent e5b1a91 commit 001db85

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

coagent/agents/mcp_agent.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __init__(
3636
self._mcp_client_session: ClientSession | None = None
3737

3838
self._mcp_swarm_agent: SwarmAgent | None = None
39-
self._mcp_system_prompt: Prompt | None = system
39+
self._mcp_system_prompt_config: Prompt | None = system
4040

4141
@property
4242
def mcp_server_base_url(self) -> str:
@@ -85,7 +85,7 @@ async def _handle_data(self) -> None:
8585

8686
async def get_swarm_agent(self) -> SwarmAgent:
8787
if not self._mcp_swarm_agent:
88-
system = await self._get_system_prompt()
88+
system = await self._get_prompt(self._mcp_system_prompt_config)
8989
tools = await self._get_tools()
9090
self._mcp_swarm_agent = SwarmAgent(
9191
name=self.name,
@@ -95,13 +95,13 @@ async def get_swarm_agent(self) -> SwarmAgent:
9595
)
9696
return self._mcp_swarm_agent
9797

98-
async def _get_system_prompt(self) -> str:
99-
if not self._mcp_system_prompt:
98+
async def _get_prompt(self, prompt_config: Prompt | None) -> str:
99+
if not prompt_config:
100100
return ""
101101

102102
try:
103103
prompt = await self._mcp_client_session.get_prompt(
104-
**dataclasses.asdict(self._mcp_system_prompt),
104+
**dataclasses.asdict(prompt_config),
105105
)
106106
except McpError as exc:
107107
raise InternalError(str(exc))

tests/agents/mcp_server.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
mcp = FastMCP("Weather")
44

55

6+
@mcp.prompt()
7+
def system_prompt(role: str) -> str:
8+
"""Create a system prompt."""
9+
return f"You are a helpful {role}."
10+
11+
612
@mcp.tool()
713
def query_weather(city: str) -> str:
814
"""Query the weather in the given city."""

tests/agents/test_mcp_agent.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,30 @@
22

33
import pytest
44

5-
from coagent.agents.mcp_agent import MCPAgent
5+
from coagent.agents.mcp_agent import MCPAgent, Prompt
6+
from coagent.core.exceptions import BaseError
67

78

89
class TestMCPAgent:
10+
@pytest.mark.skipif(sys.platform == "win32", reason="Does not run on Windows.")
11+
@pytest.mark.asyncio
12+
async def test_get_prompt(self):
13+
agent = MCPAgent(mcp_server_base_url="python tests/agents/mcp_server.py")
14+
await agent.started()
15+
16+
# Success
17+
config = Prompt(name="system_prompt", arguments={"role": "Weather Reporter"})
18+
prompt = await agent._get_prompt(config)
19+
assert prompt == "You are a helpful Weather Reporter."
20+
21+
# Error
22+
config = Prompt(name="x", arguments={"role": "Weather Reporter"})
23+
with pytest.raises(BaseError) as exc:
24+
_ = await agent._get_prompt(config)
25+
assert str(exc.value).startswith("Unknown prompt: x")
26+
27+
await agent.stopped()
28+
929
@pytest.mark.skipif(sys.platform == "win32", reason="Does not run on Windows.")
1030
@pytest.mark.asyncio
1131
async def test_get_tools(self):

0 commit comments

Comments
 (0)