feature added for logging of mcp client

This commit is contained in:
DavdaJames committed 2025-08-10 13:42:32 +05:30
1 parent c5406e8a0e
commit c09ce32def
1 file changed
+38 -29
+38 -29
View File
@@ -1,4 +1,6 @@
import asyncio import asyncio
from agentic_security.logutils import logger
from mcp import ClientSession, StdioServerParameters from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client from mcp.client.stdio import stdio_client
@@ -12,42 +14,49 @@ server_params = StdioServerParameters(
async def run() -> None: async def run() -> None:
async with stdio_client(server_params) as (read, write): try:
async with ClientSession(read, write) as session: logger.info("Starting stdio client session with server parameters: %s", server_params)
# Initialize the connection --> connection does not work async with stdio_client(server_params) as (read, write):
await session.initialize() async with ClientSession(read, write) as session:
logger.info("Initializing client session...")
await session.initialize()
# List available prompts, resources, and tools --> no avalialbe tools logger.info("Listing available prompts...")
prompts = await session.list_prompts() prompts = await session.list_prompts()
print(f"Available prompts: {prompts}") logger.info(f"Available prompts: {prompts}")
resources = await session.list_resources() logger.info("Listing available resources...")
print(f"Available resources: {resources}") resources = await session.list_resources()
logger.info(f"Available resources: {resources}")
tools = await session.list_tools() logger.info("Listing available tools...")
print(f"Available tools: {tools}") tools = await session.list_tools()
logger.info(f"Available tools: {tools}")
# Call the echo tool --> echo tool iisue logger.info("Calling echo_tool with message...")
echo_result = await session.call_tool( echo_result = await session.call_tool(
"echo_tool", arguments={"message": "Hello from client!"} "echo_tool", arguments={"message": "Hello from client!"}
) )
print(f"Tool result: {echo_result}") logger.info(f"Tool result: {echo_result}")
# # Read the echo resource # # Read the echo resource
# echo_content, mime_type = await session.read_resource( # echo_content, mime_type = await session.read_resource(
# "echo://Hello_resource" # "echo://Hello_resource"
# ) # )
# print(f"Resource content: {echo_content}") # logger.info(f"Resource content: {echo_content}")
# print(f"Resource MIME type: {mime_type}") # logger.info(f"Resource MIME type: {mime_type}")
# # Get and use the echo prompt # # Get and use the echo prompt
# prompt_result = await session.get_prompt( # prompt_result = await session.get_prompt(
# "echo_prompt", arguments={"message": "Hello prompt!"} # "echo_prompt", arguments={"message": "Hello prompt!"}
# ) # )
# print(f"Prompt result: {prompt_result}") # logger.info(f"Prompt result: {prompt_result}")
# You can perform additional operations here as needed logger.info("Client operations completed successfully.")
return prompts, resources, tools return prompts, resources, tools
except Exception as e:
logger.error(f"An error occurred during client operations: {e}", exc_info=True)
raise
if __name__ == "__main__": if __name__ == "__main__":