Skip to content

Commit c45778f

Browse files
authored
Merge pull request #252 from Davda-James/feat/mcp_client_logging
logging added for mcp client operations
2 parents a5bdbe5 + a02aed2 commit c45778f

File tree

3 files changed

+52
-38
lines changed

3 files changed

+52
-38
lines changed

agentic_security/mcp/client.py

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from mcp import ClientSession, StdioServerParameters
44
from mcp.client.stdio import stdio_client
55

6+
from agentic_security.logutils import logger
7+
68
# Create server parameters for stdio connection
79
server_params = StdioServerParameters(
810
command="python", # Executable
@@ -12,42 +14,54 @@
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(
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
5165

5266

5367
if __name__ == "__main__":

agentic_security/probe_data/data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import httpx
99
import pandas as pd
1010
from cache_to_disk import cache_to_disk
11-
from datasets import load_dataset
1211

1312
from agentic_security.logutils import logger
1413
from agentic_security.probe_data import stenography_fn
@@ -20,6 +19,7 @@
2019
inspect_ai_tool,
2120
rl_model,
2221
)
22+
from datasets import load_dataset
2323

2424
# Type aliases for clarity
2525
T = TypeVar("T")

tests/test_registry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
2-
from datasets import load_dataset
32

43
from agentic_security.probe_data import REGISTRY
4+
from datasets import load_dataset
55

66

77
@pytest.mark.slow

0 commit comments

Comments
 (0)