File size: 2,623 Bytes
116524e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import asyncio
import os
import sys

try:
    from mcp.client.session import ClientSession
    from mcp.client.stdio import get_default_environment
    from mcp.client.stdio import stdio_client
except ModuleNotFoundError as exc:  # pragma: no cover - optional dependency guard
    if (exc.name or "").split(".")[0] == "mcp":
        print(
            "This example requires the optional MCP extra. "
            'Install it with `pip install "ace-framework[mcp]"` or '
            '`uv add "ace-framework[mcp]"`.',
            file=sys.stderr,
        )
        raise SystemExit(1) from exc
    raise

try:
    from mcp.client.stdio import StdioServerParameters
except ImportError:  # pragma: no cover - fallback for SDK layout variants
    import mcp.client.stdio as mcp_stdio

    StdioServerParameters = mcp_stdio.StdioServerParameters


async def main():
    if len(sys.argv) < 2:
        print(
            "Usage: uv run python examples/ace/mcp_client_demo.py <path_to_ace_mcp_cli>"
        )
        sys.exit(1)

    server_path = sys.argv[1]

    server_params = StdioServerParameters(
        command=server_path,  # usually "ace-mcp" or "uv"
        args=["run", "ace-mcp"] if "uv" in server_path else [],
        env={
            **get_default_environment(),
            "OPENAI_API_KEY": os.environ.get("OPENAI_API_KEY", ""),
            "GOOGLE_API_KEY": os.environ.get("GOOGLE_API_KEY", ""),
            "GEMINI_API_KEY": os.environ.get("GEMINI_API_KEY", ""),
            "MISTRAL_API_KEY": os.environ.get("MISTRAL_API_KEY", ""),
            "ACE_MCP_DEFAULT_MODEL": os.environ.get("ACE_MCP_DEFAULT_MODEL", ""),
        },
    )

    async with stdio_client(server_params) as (read_stream, write_stream):
        async with ClientSession(read_stream, write_stream) as session:
            await session.initialize()

            print("Connected to ACE MCP Server.")

            # List available tools
            tools = await session.list_tools()
            print("\\nAvailable Tools:")
            for tool in tools.tools:
                print(f" - {tool.name}")

            print("\\nTesting ace.ask...")
            ask_args = {"session_id": "demo-session-1", "question": "What is 2 + 2?"}

            result = await session.call_tool("ace.ask", ask_args)
            if result.isError:
                print(f"Error calling tool: {result.content}")
            else:
                for content in result.content:
                    print(f"Response: {content.text}")


if __name__ == "__main__":
    asyncio.run(main())