Skip to content

Commit 7b848d2

Browse files
committed
test: add logs
1 parent 12e0861 commit 7b848d2

File tree

1 file changed

+84
-76
lines changed

1 file changed

+84
-76
lines changed

testcases/ground-to-cloud/test.py

Lines changed: 84 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,84 @@
1-
import asyncio
2-
import os
3-
import sys
4-
5-
from dotenv import load_dotenv
6-
from mcp.client.session import ClientSession
7-
from mcp.client.streamable_http import streamablehttp_client
8-
from retry import retry
9-
10-
11-
def get_required_env_var(name: str) -> str:
12-
"""Get required environment variable or raise an error if not set."""
13-
value = os.getenv(name)
14-
if not value:
15-
raise ValueError(f"Required environment variable {name} is not set")
16-
return value
17-
18-
@retry(tries=3, delay=2, backoff=2)
19-
async def call_add_tool():
20-
# Load configuration from environment variables
21-
base_url = get_required_env_var("BASE_URL")
22-
folder_key = get_required_env_var("UIPATH_FOLDER_KEY")
23-
token = get_required_env_var("UIPATH_ACCESS_TOKEN")
24-
mcp_server_name = get_required_env_var("MCP_SERVER_NAME")
25-
26-
# Construct the MCP server URL
27-
mcp_url = f"{base_url}/agenthub_/mcp/{folder_key}/{mcp_server_name}"
28-
29-
try:
30-
# Use streamable HTTP client to connect to the MCP server
31-
async with streamablehttp_client(mcp_url, headers={ 'Authorization': f'Bearer {token}' }) as (read_stream, write_stream, _):
32-
async with ClientSession(read_stream, write_stream) as session:
33-
# Initialize the session
34-
await session.initialize()
35-
36-
# List available tools
37-
tools_result = await session.list_tools()
38-
available_tools = [tool.name for tool in tools_result.tools]
39-
expected_available_tools = [
40-
"add", "subtract", "multiply", "divide", "power", "square_root", "nth_root",
41-
"sin", "cos", "tan", "log10", "natural_log", "log_base", "mean", "median", "standard_deviation",
42-
"complex_add", "complex_multiply", "convert_temperature", "solve_quadratic", "get_constants"
43-
]
44-
45-
print (f"Available tools: {available_tools}")
46-
47-
if set(available_tools) != set(expected_available_tools):
48-
raise AssertionError(f"Tool sets don't match. Expected: {set(expected_available_tools)}, Got: {set(available_tools)}")
49-
50-
# Call the add tool directly
51-
call_tool_result = await session.call_tool(name="add", arguments={"a": 7, "b": 5})
52-
53-
expected_result = "12.0"
54-
actual_result = call_tool_result.content[0].text if call_tool_result.content else None
55-
56-
if actual_result != expected_result:
57-
raise AssertionError(f"Expected {expected_result}, got {actual_result}")
58-
59-
print("Test completed successfully")
60-
except Exception as e:
61-
print(f"Unexpected error connecting to MCP server: {e}")
62-
raise AssertionError(f"Connection error, {e}") from e
63-
64-
async def main():
65-
"""Main async function to run the test."""
66-
try:
67-
load_dotenv()
68-
69-
await call_add_tool()
70-
except Exception as e:
71-
print(f"Test failed with error: {e}")
72-
sys.exit(1)
73-
74-
75-
if __name__ == '__main__':
76-
asyncio.run(main())
1+
import asyncio
2+
import os
3+
import sys
4+
5+
from dotenv import load_dotenv
6+
from mcp.client.session import ClientSession
7+
from mcp.client.streamable_http import streamablehttp_client
8+
from retry import retry
9+
10+
11+
def get_required_env_var(name: str) -> str:
12+
"""Get required environment variable or raise an error if not set."""
13+
value = os.getenv(name)
14+
if not value:
15+
raise ValueError(f"Required environment variable {name} is not set")
16+
return value
17+
18+
@retry(tries=3, delay=2, backoff=2)
19+
async def call_add_tool():
20+
# Load configuration from environment variables
21+
base_url = get_required_env_var("BASE_URL")
22+
folder_key = get_required_env_var("UIPATH_FOLDER_KEY")
23+
token = get_required_env_var("UIPATH_ACCESS_TOKEN")
24+
mcp_server_name = get_required_env_var("MCP_SERVER_NAME")
25+
26+
# Construct the MCP server URL
27+
mcp_url = f"{base_url}/agenthub_/mcp/{folder_key}/{mcp_server_name}"
28+
29+
try:
30+
# Use streamable HTTP client to connect to the MCP server
31+
async with streamablehttp_client(mcp_url, headers={ 'Authorization': f'Bearer {token}' }) as (read_stream, write_stream, _):
32+
async with ClientSession(read_stream, write_stream) as session:
33+
# Initialize the session
34+
await session.initialize()
35+
36+
# List available tools
37+
try:
38+
tools_result = await session.list_tools()
39+
available_tools = [tool.name for tool in tools_result.tools]
40+
expected_available_tools = [
41+
"add", "subtract", "multiply", "divide", "power", "square_root", "nth_root",
42+
"sin", "cos", "tan", "log10", "natural_log", "log_base", "mean", "median", "standard_deviation",
43+
"complex_add", "complex_multiply", "convert_temperature", "solve_quadratic", "get_constants"
44+
]
45+
46+
print (f"Available tools: {available_tools}")
47+
except Exception as e:
48+
print(f"[Tools] Error listing tools: {e}")
49+
raise AssertionError(f"Error listing tools: {e}") from e
50+
51+
if set(available_tools) != set(expected_available_tools):
52+
raise AssertionError(f"Tool sets don't match. Expected: {set(expected_available_tools)}, Got: {set(available_tools)}")
53+
54+
# Call the add tool directly
55+
try:
56+
call_tool_result = await session.call_tool(name="add", arguments={"a": 7, "b": 5})
57+
58+
expected_result = "12.0"
59+
actual_result = call_tool_result.content[0].text if call_tool_result.content else None
60+
except Exception as e:
61+
print(f"[Add Tool] Error calling add tool: {e}")
62+
raise AssertionError(f"Error calling add tool: {e}") from e
63+
64+
if actual_result != expected_result:
65+
raise AssertionError(f"Expected {expected_result}, got {actual_result}")
66+
67+
print("Test completed successfully")
68+
except Exception as e:
69+
print(f"Unexpected error connecting to MCP server: {e}")
70+
raise AssertionError(f"Connection error, {e}") from e
71+
72+
async def main():
73+
"""Main async function to run the test."""
74+
try:
75+
load_dotenv()
76+
77+
await call_add_tool()
78+
except Exception as e:
79+
print(f"Test failed with error: {e}")
80+
sys.exit(1)
81+
82+
83+
if __name__ == '__main__':
84+
asyncio.run(main())

0 commit comments

Comments
 (0)