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 mcp import ClientSession , StdioServerParameters , stdio_client
6+ from retry import retry
7+
8+
9+ @retry (tries = 3 , delay = 2 , backoff = 2 )
10+ async def call_add_tool ():
11+ server_script = os .path .join (os .path .dirname (__file__ ), "server.py" )
12+
13+ server_params = StdioServerParameters (
14+ command = "python" ,
15+ args = [server_script ]
16+ )
17+
18+ try :
19+ async with stdio_client (server_params ) as (read_stream , write_stream ):
20+ async with ClientSession (read_stream , write_stream ) as session :
21+ # Initialize the session
22+ await session .initialize ()
23+
24+ # List available tools
25+ tools_result = await session .list_tools ()
26+ available_tools = [tool .name for tool in tools_result .tools ]
27+ expected_available_tools = [
28+ "add" , "subtract" , "multiply" , "divide" , "power" , "square_root" , "nth_root" ,
29+ "sin" , "cos" , "tan" , "log10" , "natural_log" , "log_base" , "mean" , "median" , "standard_deviation" ,
30+ "complex_add" , "complex_multiply" , "convert_temperature" , "solve_quadratic" , "get_constants"
31+ ]
32+
33+ print (f"Available tools: { available_tools } " )
34+
35+ if set (available_tools ) != set (expected_available_tools ):
36+ raise AssertionError (f"Tool sets don't match. Expected: { set (expected_available_tools )} , Got: { set (available_tools )} " )
37+
38+ # Call the add tool directly
39+ call_tool_result = await session .call_tool (name = "add" , arguments = {"a" : 7 , "b" : 5 })
40+
41+ expected_result = "12.0"
42+ actual_result = call_tool_result .content [0 ].text if call_tool_result .content else None
43+
44+ if actual_result != expected_result :
45+ raise AssertionError (f"Expected { expected_result } , got { actual_result } " )
46+
47+ print ("Test completed successfully" )
48+ except Exception as e :
49+ print (f"Unexpected error connecting to MCP server: { e } " )
50+ raise AssertionError (f"Connection error, { e } " ) from e
51+
52+ async def main ():
53+ """Main async function to run the test."""
54+ try :
55+ await call_add_tool ()
56+ except Exception as e :
57+ print (f"Test failed with error: { e } " )
58+ sys .exit (1 )
59+
60+
61+ if __name__ == '__main__' :
62+ asyncio .run (main ())
0 commit comments