Coverage for mcp_server/example_client.py: 0%
36 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-16 15:54 +0300
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-16 15:54 +0300
1#!/usr/bin/env python
2"""
3Example client for the OpenShift Python Wrapper MCP Server
5This example demonstrates how to connect to and use the MCP server.
6"""
8import asyncio
9from fastmcp import FastMCPClient
12async def main():
13 # Connect to the MCP server
14 async with FastMCPClient() as client:
15 # Connect to the server running on stdio
16 await client.connect_stdio(cmd=["python", "mcp_server/server.py"])
18 print("Connected to OCP Resources MCP Server")
19 print("=" * 50)
21 # Get available resource types
22 print("\n1. Getting available resource types:")
23 result = await client.call_tool(name="get_resource_types")
24 print(f"Available resource types: {len(result['resource_types'])}")
25 print(f"Categories: {list(result['categories'].keys())}")
27 # List namespaces
28 print("\n2. Listing namespaces:")
29 result = await client.call_tool(name="list_resources", arguments={"resource_type": "namespace", "limit": 5})
30 print(f"Found {result['count']} namespaces")
31 for ns in result["resources"][:3]:
32 print(f" - {ns['name']}")
34 # Get a specific namespace
35 print("\n3. Getting default namespace:")
36 result = await client.call_tool(
37 name="get_resource", arguments={"resource_type": "namespace", "name": "default"}
38 )
39 print(f" Name: {result['name']}")
40 print(f" UID: {result['uid']}")
41 print(f" Created: {result['creationTimestamp']}")
43 # List pods in default namespace
44 print("\n4. Listing pods in default namespace:")
45 result = await client.call_tool(
46 name="list_resources", arguments={"resource_type": "pod", "namespace": "default"}
47 )
48 print(f"Found {result['count']} pods")
50 # Example: Create a ConfigMap
51 print("\n5. Creating a ConfigMap:")
52 yaml_content = """
53apiVersion: v1
54kind: ConfigMap
55metadata:
56 name: example-config
57 namespace: default
58data:
59 key1: value1
60 key2: value2
61"""
62 result = await client.call_tool(name="apply_yaml", arguments={"yaml_content": yaml_content})
63 if result.get("successful"):
64 print(" ConfigMap created successfully!")
66 # Get events
67 print("\n6. Getting recent events:")
68 result = await client.call_tool(
69 name="get_resource_events", arguments={"resource_type": "Pod", "namespace": "default", "limit": 5}
70 )
71 print(f"Found {result['event_count']} events")
72 for event in result["events"][:3]:
73 print(f" - {event['type']}: {event['reason']} - {event['message']}")
76if __name__ == "__main__":
77 asyncio.run(main())