Skip to content

Commit c09ce32

Browse files
committed
feature added for logging of mcp client
1 parent c5406e8 commit c09ce32

File tree

1 file changed

+45
-36
lines changed

1 file changed

+45
-36
lines changed

agentic_security/mcp/client.py

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
12
import asyncio
3+
from agentic_security.logutils import logger
24

35
from mcp import ClientSession, StdioServerParameters
46
from mcp.client.stdio import stdio_client
@@ -12,42 +14,49 @@
1214

1315

1416
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("Starting stdio client session with server parameters: %s", server_params)
19+
async with stdio_client(server_params) as (read, write):
20+
async with ClientSession(read, write) as session:
21+
logger.info("Initializing client session...")
22+
await session.initialize()
23+
24+
logger.info("Listing available prompts...")
25+
prompts = await session.list_prompts()
26+
logger.info(f"Available prompts: {prompts}")
27+
28+
logger.info("Listing available resources...")
29+
resources = await session.list_resources()
30+
logger.info(f"Available resources: {resources}")
31+
32+
logger.info("Listing available tools...")
33+
tools = await session.list_tools()
34+
logger.info(f"Available tools: {tools}")
35+
36+
logger.info("Calling echo_tool with message...")
37+
echo_result = await session.call_tool(
38+
"echo_tool", arguments={"message": "Hello from client!"}
39+
)
40+
logger.info(f"Tool result: {echo_result}")
41+
42+
# # Read the echo resource
43+
# echo_content, mime_type = await session.read_resource(
44+
# "echo://Hello_resource"
45+
# )
46+
# logger.info(f"Resource content: {echo_content}")
47+
# logger.info(f"Resource MIME type: {mime_type}")
48+
49+
# # Get and use the echo prompt
50+
# prompt_result = await session.get_prompt(
51+
# "echo_prompt", arguments={"message": "Hello prompt!"}
52+
# )
53+
# logger.info(f"Prompt result: {prompt_result}")
54+
55+
logger.info("Client operations completed successfully.")
56+
return prompts, resources, tools
57+
except Exception as e:
58+
logger.error(f"An error occurred during client operations: {e}", exc_info=True)
59+
raise
5160

5261

5362
if __name__ == "__main__":

0 commit comments

Comments
 (0)