|
3 | 3 | from mcp import ClientSession, StdioServerParameters |
4 | 4 | from mcp.client.stdio import stdio_client |
5 | 5 |
|
| 6 | +from agentic_security.logutils import logger |
| 7 | + |
6 | 8 | # Create server parameters for stdio connection |
7 | 9 | server_params = StdioServerParameters( |
8 | 10 | command="python", # Executable |
|
12 | 14 |
|
13 | 15 |
|
14 | 16 | async def run() -> None: |
15 | | - async with stdio_client(server_params) as (read, write): |
16 | | - async with ClientSession(read, write) as session: |
17 | | - # Initialize the connection --> connection does not work |
18 | | - await session.initialize() |
19 | | - |
20 | | - # List available prompts, resources, and tools --> no avalialbe tools |
21 | | - prompts = await session.list_prompts() |
22 | | - print(f"Available prompts: {prompts}") |
23 | | - |
24 | | - resources = await session.list_resources() |
25 | | - print(f"Available resources: {resources}") |
26 | | - |
27 | | - tools = await session.list_tools() |
28 | | - print(f"Available tools: {tools}") |
29 | | - |
30 | | - # Call the echo tool --> echo tool iisue |
31 | | - echo_result = await session.call_tool( |
32 | | - "echo_tool", arguments={"message": "Hello from client!"} |
33 | | - ) |
34 | | - print(f"Tool result: {echo_result}") |
35 | | - |
36 | | - # # Read the echo resource |
37 | | - # echo_content, mime_type = await session.read_resource( |
38 | | - # "echo://Hello_resource" |
39 | | - # ) |
40 | | - # print(f"Resource content: {echo_content}") |
41 | | - # print(f"Resource MIME type: {mime_type}") |
42 | | - |
43 | | - # # Get and use the echo prompt |
44 | | - # prompt_result = await session.get_prompt( |
45 | | - # "echo_prompt", arguments={"message": "Hello prompt!"} |
46 | | - # ) |
47 | | - # print(f"Prompt result: {prompt_result}") |
48 | | - |
49 | | - # You can perform additional operations here as needed |
50 | | - return prompts, resources, tools |
| 17 | + try: |
| 18 | + logger.info( |
| 19 | + "Starting stdio client session with server parameters: %s", server_params |
| 20 | + ) |
| 21 | + async with stdio_client(server_params) as (read, write): |
| 22 | + async with ClientSession(read, write) as session: |
| 23 | + # Initialize the connection --> connection does not work |
| 24 | + logger.info("Initializing client session...") |
| 25 | + await session.initialize() |
| 26 | + |
| 27 | + # List available prompts, resources, and tools --> no avalialbe tools |
| 28 | + logger.info("Listing available prompts...") |
| 29 | + prompts = await session.list_prompts() |
| 30 | + logger.info(f"Available prompts: {prompts}") |
| 31 | + |
| 32 | + logger.info("Listing available resources...") |
| 33 | + resources = await session.list_resources() |
| 34 | + logger.info(f"Available resources: {resources}") |
| 35 | + |
| 36 | + logger.info("Listing available tools...") |
| 37 | + tools = await session.list_tools() |
| 38 | + logger.info(f"Available tools: {tools}") |
| 39 | + |
| 40 | + # Call the echo tool --> echo tool issue |
| 41 | + logger.info("Calling echo_tool with message...") |
| 42 | + echo_result = await session.call_tool( |
| 43 | + "echo_tool", arguments={"message": "Hello from client!"} |
| 44 | + ) |
| 45 | + logger.info(f"Tool result: {echo_result}") |
| 46 | + |
| 47 | + # # Read the echo resource |
| 48 | + # echo_content, mime_type = await session.read_resource( |
| 49 | + # "echo://Hello_resource" |
| 50 | + # ) |
| 51 | + # logger.info(f"Resource content: {echo_content}") |
| 52 | + # logger.info(f"Resource MIME type: {mime_type}") |
| 53 | + |
| 54 | + # # Get and use the echo prompt |
| 55 | + # prompt_result = await session.get_prompt( |
| 56 | + # "echo_prompt", arguments={"message": "Hello prompt!"} |
| 57 | + # ) |
| 58 | + # logger.info(f"Prompt result: {prompt_result}") |
| 59 | + |
| 60 | + logger.info("Client operations completed successfully.") |
| 61 | + return prompts, resources, tools |
| 62 | + except Exception as e: |
| 63 | + logger.error(f"An error occurred during client operations: {e}", exc_info=True) |
| 64 | + raise |
51 | 65 |
|
52 | 66 |
|
53 | 67 | if __name__ == "__main__": |
|
0 commit comments