Skip to content

Commit 81f5c28

Browse files
committed
test: add test error handling
1 parent 818bf4c commit 81f5c28

File tree

3 files changed

+2511
-77
lines changed

3 files changed

+2511
-77
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "uipath-mcp"
3-
version = "0.0.104"
3+
version = "0.0.105"
44
description = "UiPath MCP SDK"
55
readme = { file = "README.md", content-type = "text/markdown" }
66
requires-python = ">=3.11"

testcases/ground-to-cloud/test.py

Lines changed: 81 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,81 @@
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+
except Exception as e:
40+
print (f"Error when listing tools: {e}")
41+
raise e
42+
43+
available_tools = [tool.name for tool in tools_result.tools]
44+
expected_available_tools = [
45+
"add", "subtract", "multiply", "divide", "power", "square_root", "nth_root",
46+
"sin", "cos", "tan", "log10", "natural_log", "log_base", "mean", "median", "standard_deviation",
47+
"complex_add", "complex_multiply", "convert_temperature", "solve_quadratic", "get_constants"
48+
]
49+
50+
print (f"Available tools: {available_tools}")
51+
52+
if set(available_tools) != set(expected_available_tools):
53+
raise AssertionError(f"Tool sets don't match. Expected: {set(expected_available_tools)}, Got: {set(available_tools)}")
54+
55+
# Call the add tool directly
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+
61+
if actual_result != expected_result:
62+
raise AssertionError(f"Expected {expected_result}, got {actual_result}")
63+
64+
print("Test completed successfully")
65+
except Exception as e:
66+
print(f"Unexpected error connecting to MCP server: {e}")
67+
raise AssertionError(f"Connection error, {e}") from e
68+
69+
async def main():
70+
"""Main async function to run the test."""
71+
try:
72+
load_dotenv()
73+
74+
await call_add_tool()
75+
except Exception as e:
76+
print(f"Test failed with error: {e}")
77+
sys.exit(1)
78+
79+
80+
if __name__ == '__main__':
81+
asyncio.run(main())

0 commit comments

Comments
 (0)