client.pyā¢1.42 kB
import asyncio
import sys
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def main():
server_params = StdioServerParameters(
command="python",
args=["/app/src/mcpdocker/server.py"], # Use absolute path
env=None
)
try:
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
tools_response = await session.list_tools()
tools = tools_response.tools
print(f"Available tools: {[tool.name for tool in tools]}")
# Look for hello-world tool (not hello-world2)
echo_tool = next((tool for tool in tools if tool.name == "hello-world"), None)
if echo_tool:
message = "Hello"
result = await session.call_tool(echo_tool.name, {"greeting": message})
print(f"Echo tool response: {result.content}")
else:
print("Echo tool not found on the server.")
print(f"Available tools: {[tool.name for tool in tools]}")
except Exception as e:
print(f"Error occurred: {e}", file=sys.stderr)
import traceback
traceback.print_exc()
if __name__ == "__main__":
asyncio.run(main())