-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall_mcp_protocol.py
More file actions
74 lines (59 loc) · 2.35 KB
/
call_mcp_protocol.py
File metadata and controls
74 lines (59 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
"""Call MCP tool through the actual MCP protocol using the MCP client SDK"""
import asyncio
import sys
from pathlib import Path
try:
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
except ImportError:
print("Error: MCP client SDK not available")
sys.exit(1)
async def call_mcp_tool():
"""Call create_network_map tool via MCP protocol"""
server_script = Path(__file__).parent / "lab_testing" / "server.py"
project_root = Path(__file__).parent
# Set up server parameters with PYTHONPATH
import os
env = os.environ.copy()
env["PYTHONPATH"] = str(project_root)
server_params = StdioServerParameters(
command=sys.executable, args=[str(server_script)], env=env
)
print("Connecting to MCP server via stdio protocol...")
print("=" * 70)
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
# Initialize the session
await session.initialize()
# Call the tool
print("\nCalling create_network_map tool...")
result = await session.call_tool(
"create_network_map",
{"quick_mode": True, "scan_networks": False, "test_configured_devices": True},
)
print(f"\nTool returned {len(result.content)} content item(s):\n")
# Display results
for i, content in enumerate(result.content, 1):
if hasattr(content, "text"):
if content.text.startswith("```mermaid"):
print(f"Content {i}: Mermaid Diagram")
print("=" * 70)
print(content.text)
print("=" * 70)
else:
print(f"Content {i}: Text")
print(content.text)
elif hasattr(content, "data"):
print(f"Content {i}: Image")
print(f" MIME Type: {content.mimeType}")
print(f" Data length: {len(content.data)} bytes")
return result
if __name__ == "__main__":
try:
result = asyncio.run(call_mcp_tool())
except Exception as e:
print(f"Error: {e}")
import traceback
traceback.print_exc()
sys.exit(1)