test_mcp_connection.pyā¢2.51 kB
#!/usr/bin/env python3
"""Test MCP server connection without Claude Desktop."""
import asyncio
import json
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def test_mcp_server():
"""Test connecting to the MCP server and listing tools."""
# Server parameters
server_params = StdioServerParameters(
command="/Users/trunghuynh/.local/bin/uv",
args=[
"run",
"python",
"/Users/trunghuynh/development/finizi-mcp/run_mcp_server.py"
],
env={
"B4B_API_BASE_URL": "http://localhost:8000",
"B4B_API_VERSION": "v1"
}
)
print("š Connecting to MCP server...")
try:
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
# Initialize
print("š Initializing session...")
await session.initialize()
print("ā
Session initialized!")
# List tools
print("\nš Listing available tools...")
tools_result = await session.list_tools()
print(f"\nā
Found {len(tools_result.tools)} tools:\n")
for i, tool in enumerate(tools_result.tools, 1):
print(f"{i:2d}. {tool.name}")
if tool.description:
desc = tool.description.split('\n')[0][:80]
print(f" {desc}")
# Test login tool
print("\n\nš Testing login tool...")
try:
result = await session.call_tool(
"login",
arguments={
"phone": "+84909495665",
"password": "Admin123@"
}
)
print("ā
Login tool called successfully!")
print(f" Result: {result.content[0].text if result.content else 'No content'}")
except Exception as e:
print(f"ā ļø Login test failed: {e}")
print("\nā
MCP server is working correctly!")
return True
except Exception as e:
print(f"\nā Error connecting to MCP server:")
print(f" {e}")
return False
if __name__ == "__main__":
success = asyncio.run(test_mcp_server())
exit(0 if success else 1)