@@ -28,55 +28,47 @@ async def call_add_tool():
2828
2929 try :
3030 # Use streamable HTTP client to connect to the MCP server
31- try :
32- async with streamablehttp_client (mcp_url , headers = { 'Authorization' : f'Bearer { token } ' }) as (read_stream , write_stream , _ ):
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
3334 try :
34- async with ClientSession (read_stream , write_stream ) as session :
35- # Initialize the session
36- try :
37- await session .initialize ()
38- except Exception as e :
39- print (f"[Initialize] Error initializing session: { e } " )
40- raise e
41-
42- # List available tools
43- try :
44- tools_result = await session .list_tools ()
45- available_tools = [tool .name for tool in tools_result .tools ]
46- expected_available_tools = [
47- "add" , "subtract" , "multiply" , "divide" , "power" , "square_root" , "nth_root" ,
48- "sin" , "cos" , "tan" , "log10" , "natural_log" , "log_base" , "mean" , "median" , "standard_deviation" ,
49- "complex_add" , "complex_multiply" , "convert_temperature" , "solve_quadratic" , "get_constants"
50- ]
51-
52- print (f"Available tools: { available_tools } " )
53- except Exception as e :
54- print (f"[Tools] Error listing tools: { e } " )
55- raise e
56-
57- if set (available_tools ) != set (expected_available_tools ):
58- raise AssertionError (f"Tool sets don't match. Expected: { set (expected_available_tools )} , Got: { set (available_tools )} " )
59-
60- # Call the add tool directly
61- try :
62- call_tool_result = await session .call_tool (name = "add" , arguments = {"a" : 7 , "b" : 5 })
63-
64- expected_result = "12.0"
65- actual_result = call_tool_result .content [0 ].text if call_tool_result .content else None
66- except Exception as e :
67- print (f"[Add Tool] Error calling add tool: { e } " )
68- raise e
69-
70- if actual_result != expected_result :
71- raise AssertionError (f"Expected { expected_result } , got { actual_result } " )
72-
73- print ("Test completed successfully" )
35+ await session .initialize ()
7436 except Exception as e :
75- print (f"[Client Session ] Error during MCP session: { e } " )
37+ print (f"[Initialize ] Error initializing session: { e } " )
7638 raise e
77- except Exception as e :
78- print (f"[Streamable HTTP Client] Error connecting to MCP server: { e } " )
79- raise e
39+
40+ # List available tools
41+ try :
42+ tools_result = await session .list_tools ()
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+ except Exception as e :
52+ print (f"[Tools] Error listing tools: { e } " )
53+ raise e
54+
55+ if set (available_tools ) != set (expected_available_tools ):
56+ raise AssertionError (f"Tool sets don't match. Expected: { set (expected_available_tools )} , Got: { set (available_tools )} " )
57+
58+ # Call the add tool directly
59+ try :
60+ call_tool_result = await session .call_tool (name = "add" , arguments = {"a" : 7 , "b" : 5 })
61+
62+ expected_result = "12.0"
63+ actual_result = call_tool_result .content [0 ].text if call_tool_result .content else None
64+ except Exception as e :
65+ print (f"[Add Tool] Error calling add tool: { e } " )
66+ raise e
67+
68+ if actual_result != expected_result :
69+ raise AssertionError (f"Expected { expected_result } , got { actual_result } " )
70+
71+ print ("Test completed successfully" )
8072 except Exception as e :
8173 print (f"Unexpected error connecting to MCP server: { e } " )
8274 raise AssertionError (f"Connection error, { e } " ) from e
0 commit comments