|
| 1 | +from mcp import ClientSession, StdioServerParameters, types |
| 2 | +from mcp.client.stdio import stdio_client |
| 3 | +import asyncio |
| 4 | + |
| 5 | +# Create server parameters for stdio connection |
| 6 | +server_params = StdioServerParameters( |
| 7 | + command="python", # Executable |
| 8 | + args=["agentic_security/mcp/main.py"], # Your server script |
| 9 | + env=None, # Optional environment variables |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +async def run(): |
| 14 | + async with stdio_client(server_params) as (read, write): |
| 15 | + async with ClientSession(read, write) as session: |
| 16 | + # Initialize the connection |
| 17 | + await session.initialize() |
| 18 | + |
| 19 | + # List available prompts, resources, and tools |
| 20 | + prompts = await session.list_prompts() |
| 21 | + print(f"Available prompts: {prompts}") |
| 22 | + |
| 23 | + resources = await session.list_resources() |
| 24 | + print(f"Available resources: {resources}") |
| 25 | + |
| 26 | + tools = await session.list_tools() |
| 27 | + print(f"Available tools: {tools}") |
| 28 | + |
| 29 | + # Call the echo tool |
| 30 | + echo_result = await session.call_tool( |
| 31 | + "echo_tool", arguments={"message": "Hello from client!"} |
| 32 | + ) |
| 33 | + print(f"Tool result: {echo_result}") |
| 34 | + |
| 35 | + # # Read the echo resource |
| 36 | + # echo_content, mime_type = await session.read_resource( |
| 37 | + # "echo://Hello_resource" |
| 38 | + # ) |
| 39 | + # print(f"Resource content: {echo_content}") |
| 40 | + # print(f"Resource MIME type: {mime_type}") |
| 41 | + |
| 42 | + # # Get and use the echo prompt |
| 43 | + # prompt_result = await session.get_prompt( |
| 44 | + # "echo_prompt", arguments={"message": "Hello prompt!"} |
| 45 | + # ) |
| 46 | + # print(f"Prompt result: {prompt_result}") |
| 47 | + |
| 48 | + # You can perform additional operations here as needed |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + asyncio.run(run()) |
0 commit comments